32
32
import com .bumptech .glide .test .ForceDarkOrLightModeActivity ;
33
33
import com .google .common .base .Function ;
34
34
import org .junit .Before ;
35
+ import org .junit .Ignore ;
35
36
import org .junit .Rule ;
36
37
import org .junit .Test ;
37
38
import org .junit .runner .RunWith ;
@@ -50,33 +51,54 @@ public void before() {
50
51
assumeTrue (VERSION .SDK_INT >= VERSION_CODES .Q );
51
52
}
52
53
53
- // TODO(judds): The way we handle data loads in the background for resources is not Theme
54
- // compatible. In particular, the theme gets lost when we convert the resource id to a Uri and
55
- // we don't use the user provided theme. While ResourceBitmapDecoder and ResourceDrawableDecoder
56
- // will use the theme, they're not called for most resource ids because those instead go through
57
- // UriLoader, which just calls contentResolver.openStream. This isn't sufficient to use to theme.
58
- // We could:
59
- // 1. Avoid using contentResolver for android resource Uris and use ResourceBitmapDecoder instead.
60
- // 2. #1 but only for non-raw resources which won't be themed
61
- // 3. Always use Theme.getResources().openRawResource, which, despite the name, works find on
62
- // Drawables and takes into account the theme.
63
- // In addition we'd also need to consider just passing through the theme always, rather than only
64
- // when it's specified by the user. Otherwise whether or not we'd obey dark mode would depend on
65
- // the user also providing the theme from the activity. We'd want to try to make sure that doesn't
66
- // leak the Activity.
67
- @ Test
68
- public void load_withDarkModeActivity_usesLightModeDrawable () {
54
+ @ Test
55
+ public void load_withDarkModeActivity_useDarkModeDrawable () {
56
+ runActivityTest (
57
+ darkModeActivity (),
58
+ R .raw .dog_dark ,
59
+ activity -> Glide .with (activity ).load (R .drawable .dog ).override (Target .SIZE_ORIGINAL ));
60
+ }
61
+
62
+ @ Test
63
+ public void load_withDarkModeActivity_afterLoadingWithLightModeActivity_useDarkModeDrawable () {
64
+ // Load with light mode first.
65
+ runActivityTest (
66
+ lightModeActivity (),
67
+ R .raw .dog_light ,
68
+ activity -> Glide .with (activity ).load (R .drawable .dog ).override (Target .SIZE_ORIGINAL ));
69
+
70
+ // Then again with dark mode to make sure that we do not use the cached resource from the
71
+ // previous load.
69
72
runActivityTest (
70
73
darkModeActivity (),
74
+ R .raw .dog_dark ,
75
+ activity -> Glide .with (activity ).load (R .drawable .dog ).override (Target .SIZE_ORIGINAL ));
76
+ }
77
+
78
+ @ Test
79
+ public void load_withDarkModeActivity_afterLoadingWithLightModeActivity_memoryCacheCleared_useDarkModeDrawable () {
80
+ // Load with light mode first.
81
+ runActivityTest (
82
+ lightModeActivity (),
71
83
R .raw .dog_light ,
72
84
activity -> Glide .with (activity ).load (R .drawable .dog ).override (Target .SIZE_ORIGINAL ));
85
+
86
+ // Then again with dark mode to make sure that we do not use the cached resource from the
87
+ // previous load.
88
+ runActivityTest (
89
+ darkModeActivity (),
90
+ R .raw .dog_dark ,
91
+ activity -> {
92
+ Glide .get (context ).clearMemory ();
93
+ return Glide .with (activity ).load (R .drawable .dog ).override (Target .SIZE_ORIGINAL );
94
+ });
73
95
}
74
96
75
97
@ Test
76
- public void load_withDarkModeFragment_usesLightModeDrawable () {
98
+ public void load_withDarkModeFragment_usesDarkModeDrawable () {
77
99
runFragmentTest (
78
100
darkModeActivity (),
79
- R .raw .dog_light ,
101
+ R .raw .dog_dark ,
80
102
fragment -> Glide .with (fragment ).load (R .drawable .dog ).override (Target .SIZE_ORIGINAL ));
81
103
}
82
104
@@ -120,6 +142,35 @@ public void loadResourceNameUri_withDarkModeActivity_darkModeTheme_usesDarkModeD
120
142
.theme (activity .getTheme ()));
121
143
}
122
144
145
+ @ Test
146
+ public void loadResourceNameUri_withDarkModeActivity_usesDarkModeDrawable () {
147
+ runActivityTest (
148
+ darkModeActivity (),
149
+ R .raw .dog_dark ,
150
+ activity ->
151
+ Glide .with (activity )
152
+ .load (newResourceNameUri (activity , R .drawable .dog ))
153
+ .override (Target .SIZE_ORIGINAL ));
154
+ }
155
+
156
+ @ Test
157
+ public void loadResourceNameUri_withDarkModeActivity_afterLightModeActivity_usesDarkModeDrawable () {
158
+ runActivityTest (
159
+ lightModeActivity (),
160
+ R .raw .dog_light ,
161
+ activity ->
162
+ Glide .with (activity )
163
+ .load (newResourceNameUri (activity , R .drawable .dog ))
164
+ .override (Target .SIZE_ORIGINAL ));
165
+ runActivityTest (
166
+ darkModeActivity (),
167
+ R .raw .dog_dark ,
168
+ activity ->
169
+ Glide .with (activity )
170
+ .load (newResourceNameUri (activity , R .drawable .dog ))
171
+ .override (Target .SIZE_ORIGINAL ));
172
+ }
173
+
123
174
@ Test
124
175
public void loadResourceIdUri_withDarkModeActivity_darkModeTheme_usesDarkModeDrawable () {
125
176
runActivityTest (
@@ -132,6 +183,17 @@ public void loadResourceIdUri_withDarkModeActivity_darkModeTheme_usesDarkModeDra
132
183
.theme (activity .getTheme ()));
133
184
}
134
185
186
+ @ Test
187
+ public void loadResourceIdUri_withDarkModeActivity_usesDarkModeDrawable () {
188
+ runActivityTest (
189
+ darkModeActivity (),
190
+ R .raw .dog_dark ,
191
+ activity ->
192
+ Glide .with (activity )
193
+ .load (newResourceIdUri (activity , R .drawable .dog ))
194
+ .override (Target .SIZE_ORIGINAL ));
195
+ }
196
+
135
197
private static Uri newResourceNameUri (Context context , int resourceId ) {
136
198
Resources resources = context .getResources ();
137
199
return newResourceUriBuilder (context )
@@ -198,6 +260,28 @@ public void load_withApplicationContext_darkTheme_usesDarkModeDrawable() {
198
260
.theme (input .getTheme ()));
199
261
}
200
262
263
+ @ Ignore ("TODO(#3751): Consider how to deal with themes applied for application context loads." )
264
+ @ Test
265
+ public void load_withApplicationContext_lightTheme_thenDarkTheme_usesDarkModeDrawable () {
266
+ runActivityTest (
267
+ lightModeActivity (),
268
+ R .raw .dog_light ,
269
+ input ->
270
+ Glide .with (input .getApplicationContext ())
271
+ .load (R .drawable .dog )
272
+ .override (Target .SIZE_ORIGINAL )
273
+ .theme (input .getTheme ()));
274
+
275
+ runActivityTest (
276
+ darkModeActivity (),
277
+ R .raw .dog_dark ,
278
+ input ->
279
+ Glide .with (input .getApplicationContext ())
280
+ .load (R .drawable .dog )
281
+ .override (Target .SIZE_ORIGINAL )
282
+ .theme (input .getTheme ()));
283
+ }
284
+
201
285
@ Test
202
286
public void loadResourceNameUri_withApplicationContext_darkTheme_usesDarkModeDrawable () {
203
287
runActivityTest (
@@ -210,6 +294,28 @@ public void loadResourceNameUri_withApplicationContext_darkTheme_usesDarkModeDra
210
294
.theme (input .getTheme ()));
211
295
}
212
296
297
+ @ Ignore ("TODO(#3751): Consider how to deal with themes applied for application context loads." )
298
+ @ Test
299
+ public void loadResourceNameUri_withApplicationContext_darkTheme_afterLightTheme_usesDarkModeDrawable () {
300
+ runActivityTest (
301
+ lightModeActivity (),
302
+ R .raw .dog_light ,
303
+ input ->
304
+ Glide .with (input .getApplicationContext ())
305
+ .load (newResourceNameUri (input .getApplicationContext (), R .drawable .dog ))
306
+ .override (Target .SIZE_ORIGINAL )
307
+ .theme (input .getTheme ()));
308
+
309
+ runActivityTest (
310
+ darkModeActivity (),
311
+ R .raw .dog_dark ,
312
+ input ->
313
+ Glide .with (input .getApplicationContext ())
314
+ .load (newResourceNameUri (input .getApplicationContext (), R .drawable .dog ))
315
+ .override (Target .SIZE_ORIGINAL )
316
+ .theme (input .getTheme ()));
317
+ }
318
+
213
319
@ Test
214
320
public void loadResourceIdUri_withApplicationContext_darkTheme_usesDarkModeDrawable () {
215
321
runActivityTest (
0 commit comments