Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
b96adbed
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b96adbed
编写于
4月 15, 2015
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Avoid throwing on invalid resource ids.
Fixes #413
上级
899e41b8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
41 addition
and
16 deletion
+41
-16
library/src/androidTest/java/com/bumptech/glide/load/model/stream/ResourceLoaderTest.java
.../bumptech/glide/load/model/stream/ResourceLoaderTest.java
+21
-9
library/src/main/java/com/bumptech/glide/load/model/ResourceLoader.java
...in/java/com/bumptech/glide/load/model/ResourceLoader.java
+19
-6
library/src/main/java/com/bumptech/glide/request/GenericRequest.java
.../main/java/com/bumptech/glide/request/GenericRequest.java
+1
-1
未找到文件。
library/src/androidTest/java/com/bumptech/glide/load/model/stream/ResourceLoaderTest.java
浏览文件 @
b96adbed
package
com.bumptech.glide.load.model.stream
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Matchers
.
anyInt
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
atLeastOnce
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
android.net.Uri
;
import
com.bumptech.glide.load.model.ModelLoader
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.robolectric.Robolectric
;
import
org.robolectric.RobolectricTestRunner
;
import
org.robolectric.annotation.Config
;
...
...
@@ -27,17 +29,27 @@ import java.io.InputStream;
@Config
(
manifest
=
Config
.
NONE
,
emulateSdk
=
18
)
public
class
ResourceLoaderTest
{
@Mock
ModelLoader
<
Uri
,
InputStream
>
streamUriLoader
;
private
StreamResourceLoader
resourceLoader
;
@Before
public
void
setUp
()
{
MockitoAnnotations
.
initMocks
(
this
);
resourceLoader
=
new
StreamResourceLoader
(
Robolectric
.
application
,
streamUriLoader
);
}
@Test
public
void
testCanHandleId
()
{
ModelLoader
<
Uri
,
InputStream
>
streamUriLoader
=
mock
(
ModelLoader
.
class
);
when
(
streamUriLoader
.
getResourceFetcher
(
any
(
Uri
.
class
),
anyInt
(),
anyInt
())).
thenReturn
(
null
);
int
id
=
android
.
R
.
drawable
.
star_off
;
StreamResourceLoader
resourceLoader
=
new
StreamResourceLoader
(
Robolectric
.
application
,
streamUriLoader
);
resourceLoader
.
getResourceFetcher
(
id
,
0
,
0
);
Uri
contentUri
=
Uri
.
parse
(
"android.resource://android/drawable/star_off"
);
verify
(
streamUriLoader
,
atLeastOnce
()).
getResourceFetcher
(
eq
(
contentUri
),
anyInt
(),
anyInt
());
verify
(
streamUriLoader
).
getResourceFetcher
(
eq
(
contentUri
),
anyInt
(),
anyInt
());
}
@Test
public
void
testDoesNotThrowOnInvalidOrMissingId
()
{
assertThat
(
resourceLoader
.
getResourceFetcher
(
1234
,
0
,
0
)).
isNull
();
verify
(
streamUriLoader
,
never
()).
getResourceFetcher
(
any
(
Uri
.
class
),
anyInt
(),
anyInt
());
}
}
library/src/main/java/com/bumptech/glide/load/model/ResourceLoader.java
浏览文件 @
b96adbed
...
...
@@ -4,6 +4,7 @@ import android.content.ContentResolver;
import
android.content.Context
;
import
android.content.res.Resources
;
import
android.net.Uri
;
import
android.util.Log
;
import
com.bumptech.glide.load.data.DataFetcher
;
...
...
@@ -14,6 +15,7 @@ import com.bumptech.glide.load.data.DataFetcher;
* @param <T> The type of data that will be loaded for the given android resource.
*/
public
class
ResourceLoader
<
T
>
implements
ModelLoader
<
Integer
,
T
>
{
private
static
final
String
TAG
=
"ResourceLoader"
;
private
final
ModelLoader
<
Uri
,
T
>
uriLoader
;
private
final
Resources
resources
;
...
...
@@ -29,11 +31,22 @@ public class ResourceLoader<T> implements ModelLoader<Integer, T> {
@Override
public
DataFetcher
<
T
>
getResourceFetcher
(
Integer
model
,
int
width
,
int
height
)
{
Uri
uri
=
Uri
.
parse
(
ContentResolver
.
SCHEME_ANDROID_RESOURCE
+
"://"
+
resources
.
getResourcePackageName
(
model
)
+
'/'
+
resources
.
getResourceTypeName
(
model
)
+
'/'
+
resources
.
getResourceEntryName
(
model
));
return
uriLoader
.
getResourceFetcher
(
uri
,
width
,
height
);
Uri
uri
=
null
;
try
{
uri
=
Uri
.
parse
(
ContentResolver
.
SCHEME_ANDROID_RESOURCE
+
"://"
+
resources
.
getResourcePackageName
(
model
)
+
'/'
+
resources
.
getResourceTypeName
(
model
)
+
'/'
+
resources
.
getResourceEntryName
(
model
));
}
catch
(
Resources
.
NotFoundException
e
)
{
if
(
Log
.
isLoggable
(
TAG
,
Log
.
WARN
))
{
Log
.
w
(
TAG
,
"Received invalid resource id: "
+
model
,
e
);
}
}
if
(
uri
!=
null
)
{
return
uriLoader
.
getResourceFetcher
(
uri
,
width
,
height
);
}
else
{
return
null
;
}
}
}
library/src/main/java/com/bumptech/glide/request/GenericRequest.java
浏览文件 @
b96adbed
...
...
@@ -417,7 +417,7 @@ public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallb
final
DataFetcher
<
T
>
dataFetcher
=
modelLoader
.
getResourceFetcher
(
model
,
width
,
height
);
if
(
dataFetcher
==
null
)
{
onException
(
new
Exception
(
"
Got null fetcher from model loader"
));
onException
(
new
Exception
(
"
Failed to load model: "
+
model
));
return
;
}
ResourceTranscoder
<
Z
,
R
>
transcoder
=
loadProvider
.
getTranscoder
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录