/
ShadowThemeTest.java
337 lines (290 loc) · 13.2 KB
/
ShadowThemeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package org.robolectric.shadows;
import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Robolectric.buildActivity;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.ColorDrawable;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.R;
import org.robolectric.Robolectric;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import org.robolectric.res.android.Registries;
import org.robolectric.shadows.testing.TestActivity;
import org.robolectric.util.ReflectionHelpers;
import org.xmlpull.v1.XmlPullParser;
@RunWith(AndroidJUnit4.class)
public class ShadowThemeTest {
private Resources resources;
@Before
public void setUp() throws Exception {
resources = ApplicationProvider.getApplicationContext().getResources();
}
@After
public void tearDown() {
ShadowLegacyAssetManager.strictErrors = false;
}
@Test
public void
whenExplicitlySetOnActivity_afterSetContentView_activityGetsThemeFromActivityInManifest() {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();
activity.setTheme(R.style.Theme_Robolectric);
Button theButton = activity.findViewById(R.id.button);
ColorDrawable background = (ColorDrawable) theButton.getBackground();
assertThat(background.getColor()).isEqualTo(0xffff0000);
}
@Test
public void whenExplicitlySetOnActivity_beforeSetContentView_activityUsesNewTheme() {
ActivityController<TestActivityWithAnotherTheme> activityController = buildActivity(TestActivityWithAnotherTheme.class);
TestActivity activity = activityController.get();
activity.setTheme(R.style.Theme_Robolectric);
activityController.create();
Button theButton = activity.findViewById(R.id.button);
ColorDrawable background = (ColorDrawable) theButton.getBackground();
assertThat(background.getColor()).isEqualTo(0xff00ff00);
}
@Test
public void whenSetOnActivityInManifest_activityGetsThemeFromActivityInManifest() {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();
Button theButton = activity.findViewById(R.id.button);
ColorDrawable background = (ColorDrawable) theButton.getBackground();
assertThat(background.getColor()).isEqualTo(0xffff0000);
}
@Test
public void whenNotSetOnActivityInManifest_activityGetsThemeFromApplicationInManifest() {
TestActivity activity = buildActivity(TestActivity.class).create().get();
Button theButton = activity.findViewById(R.id.button);
ColorDrawable background = (ColorDrawable) theButton.getBackground();
assertThat(background.getColor()).isEqualTo(0xff00ff00);
}
@Test
public void shouldResolveReferencesThatStartWithAQuestionMark() {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();
Button theButton = activity.findViewById(R.id.button);
assertThat(theButton.getMinWidth()).isEqualTo(8);
assertThat(theButton.getMinHeight()).isEqualTo(8);
}
@Test
public void forStylesWithImplicitParents_shouldInheritValuesNotDefinedInChild() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.Theme_Robolectric_EmptyParent, true);
assertThat(theme.obtainStyledAttributes(new int[] {R.attr.string1}).hasValue(0)).isFalse();
}
@Test
public void shouldApplyParentStylesFromAttrs() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.SimpleParent, true);
assertThat(theme.obtainStyledAttributes(new int[] {R.attr.parent_string}).getString(0))
.isEqualTo("parent string");
}
@Test
public void applyStyle_shouldOverrideParentAttrs() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.SimpleChildWithOverride, true);
assertThat(theme.obtainStyledAttributes(new int[] {R.attr.parent_string}).getString(0))
.isEqualTo("parent string overridden by child");
}
@Test
public void applyStyle_shouldOverrideImplicitParentAttrs() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.SimpleParent_ImplicitChild, true);
assertThat(theme.obtainStyledAttributes(new int[] {R.attr.parent_string}).getString(0))
.isEqualTo("parent string overridden by child");
}
@Test
public void applyStyle_shouldInheritParentAttrs() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.SimpleChildWithAdditionalAttributes, true);
assertThat(theme.obtainStyledAttributes(new int[] {R.attr.child_string}).getString(0))
.isEqualTo("child string");
assertThat(theme.obtainStyledAttributes(new int[] {R.attr.parent_string}).getString(0))
.isEqualTo("parent string");
}
@Test
public void setTo_shouldCopyAllAttributesToEmptyTheme() {
Resources.Theme theme1 = resources.newTheme();
theme1.applyStyle(R.style.Theme_Robolectric, false);
assertThat(theme1.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from Theme.Robolectric");
Resources.Theme theme2 = resources.newTheme();
theme2.setTo(theme1);
assertThat(theme2.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from Theme.Robolectric");
}
@Test
public void setTo_whenDestThemeIsModified_sourceThemeShouldNotMutate() {
Resources.Theme sourceTheme = resources.newTheme();
sourceTheme.applyStyle(R.style.StyleA, false);
Resources.Theme destTheme = resources.newTheme();
destTheme.setTo(sourceTheme);
destTheme.applyStyle(R.style.StyleB, true);
assertThat(destTheme.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from style B");
assertThat(sourceTheme.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from style A");
}
@Test
public void setTo_whenSourceThemeIsModified_destThemeShouldNotMutate() {
Resources.Theme sourceTheme = resources.newTheme();
sourceTheme.applyStyle(R.style.StyleA, false);
Resources.Theme destTheme = resources.newTheme();
destTheme.setTo(sourceTheme);
sourceTheme.applyStyle(R.style.StyleB, true);
assertThat(destTheme.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from style A");
assertThat(sourceTheme.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from style B");
}
@Test
public void applyStyle_withForceFalse_shouldApplyButNotOverwriteExistingAttributeValues() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.StyleA, false);
assertThat(theme.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from style A");
theme.applyStyle(R.style.StyleB, false);
assertThat(theme.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from style A");
}
@Test
public void applyStyle_withForceTrue_shouldApplyAndOverwriteExistingAttributeValues() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.StyleA, false);
assertThat(theme.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from style A");
theme.applyStyle(R.style.StyleB, true);
assertThat(theme.obtainStyledAttributes(new int[]{R.attr.string1}).getString(0))
.isEqualTo("string 1 from style B");
}
@Test
public void whenStyleSpecifiesAttr_obtainStyledAttribute_findsCorrectValue() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.Theme_Robolectric, false);
theme.applyStyle(R.style.Theme_ThemeContainingStyleReferences, true);
assertThat(theme.obtainStyledAttributes(
Robolectric.buildAttributeSet().setStyleAttribute("?attr/styleReference").build(),
new int[]{R.attr.string2}, 0, 0).getString(0))
.isEqualTo("string 2 from YetAnotherStyle");
assertThat(theme.obtainStyledAttributes(
Robolectric.buildAttributeSet().setStyleAttribute("?styleReference").build(),
new int[]{R.attr.string2}, 0, 0).getString(0))
.isEqualTo("string 2 from YetAnotherStyle");
}
@Test
public void xml_whenStyleSpecifiesAttr_obtainStyledAttribute_findsCorrectValue() throws Exception {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.Theme_Robolectric, false);
theme.applyStyle(R.style.Theme_ThemeContainingStyleReferences, true);
assertThat(theme.obtainStyledAttributes(getFirstElementAttrSet(R.xml.temp),
new int[]{R.attr.string2}, 0, 0).getString(0))
.isEqualTo("string 2 from YetAnotherStyle");
assertThat(theme.obtainStyledAttributes(getFirstElementAttrSet(R.xml.temp_parent),
new int[]{R.attr.string2}, 0, 0).getString(0))
.isEqualTo("string 2 from YetAnotherStyle");
}
@Test
public void whenAttrSetAttrSpecifiesAttr_obtainStyledAttribute_returnsItsValue() {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.Theme_Robolectric, false);
theme.applyStyle(R.style.Theme_ThemeContainingStyleReferences, true);
assertThat(theme.obtainStyledAttributes(
Robolectric.buildAttributeSet().addAttribute(R.attr.string2, "?attr/string1").build(),
new int[]{R.attr.string2}, 0, 0).getString(0))
.isEqualTo("string 1 from Theme.Robolectric");
}
@Test
public void dimenRef() {
AttributeSet attributeSet = Robolectric.buildAttributeSet()
.addAttribute(android.R.attr.layout_height, "@dimen/test_px_dimen")
.build();
TypedArray typedArray = resources.newTheme().obtainStyledAttributes(
attributeSet, new int[]{android.R.attr.layout_height}, 0, 0);
assertThat(typedArray.getDimensionPixelSize(0, -1)).isEqualTo(15);
}
@Test
public void dimenRefRef() {
AttributeSet attributeSet = Robolectric.buildAttributeSet()
.addAttribute(android.R.attr.layout_height, "@dimen/ref_to_px_dimen")
.build();
TypedArray typedArray = resources.newTheme().obtainStyledAttributes(
attributeSet, new int[]{android.R.attr.layout_height}, 0, 0);
assertThat(typedArray.getDimensionPixelSize(0, -1)).isEqualTo(15);
}
@Test
public void obtainStyledAttributes_shouldFindAttributeInDefaultStyle() {
Theme theme = resources.newTheme();
TypedArray typedArray = theme.obtainStyledAttributes(R.style.StyleA, new int[]{R.attr.string1});
assertThat(typedArray.getString(0)).isEqualTo("string 1 from style A");
}
@Test
public void shouldApplyFromStyleAttribute() {
TestWithStyleAttrActivity activity = buildActivity(TestWithStyleAttrActivity.class).create().get();
View button = activity.findViewById(R.id.button);
assertThat(button.getLayoutParams().width).isEqualTo(42); // comes via style attr
}
@Test
@Config(minSdk = VERSION_CODES.N)
public void shouldFreeNativeObjectInRegistry() {
final AtomicLong themeId = new AtomicLong(0);
Supplier<Theme> themeSupplier =
() -> {
Theme theme = resources.newTheme();
long nativeId =
ReflectionHelpers.getField(ReflectionHelpers.getField(theme, "mThemeImpl"), "mTheme");
themeId.set(nativeId);
return theme;
};
WeakReference<Theme> weakRef = new WeakReference<>(themeSupplier.get());
awaitFinalized(weakRef);
assertThat(Registries.NATIVE_THEME9_REGISTRY.peekNativeObject(themeId.get())).isNull();
}
private static <T> void awaitFinalized(WeakReference<T> weakRef) {
final CountDownLatch latch = new CountDownLatch(1);
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (System.nanoTime() < deadline) {
if (weakRef.get() == null) {
return;
}
try {
System.gc();
latch.await(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}
}
;
////////////////////////////
private XmlResourceParser getFirstElementAttrSet(int resId) throws Exception {
XmlResourceParser xml = resources.getXml(resId);
assertThat(xml.next()).isEqualTo(XmlPullParser.START_DOCUMENT);
assertThat(xml.nextTag()).isEqualTo(XmlPullParser.START_TAG);
return (XmlResourceParser) Xml.asAttributeSet(xml);
}
public static class TestActivityWithAnotherTheme extends TestActivity {
}
public static class TestWithStyleAttrActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.styles_button_with_style_layout);
}
}
}