-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ThemeTest.java
612 lines (574 loc) · 23.9 KB
/
ThemeTest.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.theme;
import com.google.android.material.R;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static com.google.common.truth.Truth.assertThat;
import androidx.appcompat.view.ContextThemeWrapper;
import com.google.android.material.resources.MaterialAttributes;
import com.google.android.material.testing.ResourceNameLookup;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.ParameterizedRobolectricTestRunner.Parameter;
import org.robolectric.ParameterizedRobolectricTestRunner.Parameters;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.internal.DoNotInstrument;
/** Tests for the Material themes. */
@RunWith(ParameterizedRobolectricTestRunner.class)
@Config(sdk = LOLLIPOP)
@DoNotInstrument
public class ThemeTest {
/** These are motion styles that should be the same for *all* full themes. */
private static final ImmutableList<Integer> MOTION_STYLE_ATTRIBUTES =
ImmutableList.of(
R.attr.motionEasingStandard,
R.attr.motionEasingEmphasized,
R.attr.motionEasingDecelerated,
R.attr.motionEasingAccelerated,
R.attr.motionEasingLinear,
R.attr.motionDurationShort1,
R.attr.motionDurationShort2,
R.attr.motionDurationMedium1,
R.attr.motionDurationMedium2,
R.attr.motionDurationLong1,
R.attr.motionDurationLong2,
R.attr.motionPath);
private static final ImmutableList<Integer> DEPRECATED_TYPOGRAPHY_ATTRIBUTES =
ImmutableList.of(
R.attr.textAppearanceHeadline1,
R.attr.textAppearanceHeadline2,
R.attr.textAppearanceHeadline3,
R.attr.textAppearanceHeadline4,
R.attr.textAppearanceHeadline5,
R.attr.textAppearanceHeadline6,
R.attr.textAppearanceSubtitle1,
R.attr.textAppearanceSubtitle2,
R.attr.textAppearanceBody1,
R.attr.textAppearanceBody2,
R.attr.textAppearanceCaption,
R.attr.textAppearanceButton,
R.attr.textAppearanceOverline);
/** These attributes pairs should be same in Material Light theme. */
private static final ImmutableList<int[]> MATERIAL_INTRA_LIGHT_THEME_ATTRIBUTES =
ImmutableList.of(
new int[] {R.attr.colorPrimarySurface, R.attr.colorPrimary},
new int[] {R.attr.colorOnPrimarySurface, R.attr.colorOnPrimary});
/** These attributes pairs should be same in Material Dark theme. */
private static final ImmutableList<int[]> MATERIAL_INTRA_DARK_THEME_ATTRIBUTES =
ImmutableList.of(
new int[] {R.attr.colorPrimarySurface, R.attr.colorSurface},
new int[] {R.attr.colorOnPrimarySurface, R.attr.colorOnSurface});
/**
* These are color attributes that all themes, including bridge themes, should have.
*
* <p>All Light themes should match {@code Theme.MaterialComponents.Light} and all Dark themes
* should match {@code Theme.MaterialComponents}.
*/
private static final ImmutableList<Integer> MATERIAL_BRIDGE_COLOR_ATTRIBUTES =
ImmutableList.of(
R.attr.colorPrimaryVariant,
R.attr.colorSecondary,
R.attr.colorSecondaryVariant,
R.attr.colorSurface,
R.attr.colorOnPrimary,
R.attr.colorOnSecondary,
R.attr.colorOnBackground,
R.attr.colorOnError,
R.attr.colorOnSurface,
R.attr.colorOnPrimarySurface);
/**
* These are color attributes that all full themes should have in addition to {@link
* #MATERIAL_BRIDGE_COLOR_ATTRIBUTES}.
*
* <p>All Light full themes should match {@code Theme.MaterialComponents.Light} and all Dark
* themes should match {@code Theme.MaterialComponents}.
*/
private static final ImmutableList<Integer> MATERIAL_NON_BRIDGE_COLOR_ATTRIBUTES =
ImmutableList.of(
R.attr.colorPrimary,
R.attr.colorPrimaryDark,
R.attr.colorAccent,
android.R.attr.colorBackground,
R.attr.colorError);
/**
* There are color attributes that all theme overlays should have.
*
* <p>All Light theme overlays shold match {@code Theme.MaterialComponents.Light} and all Dark
* theme overlays should match {@code Theme.MaterialComponents}.
*/
private static final ImmutableList<Integer> MATERIAL_OVERLAY_COLOR_ATTRIBUTES =
ImmutableList.of(
android.R.attr.colorBackground,
R.attr.colorOnBackground,
R.attr.colorSurface,
R.attr.colorOnSurface,
R.attr.colorError,
R.attr.colorOnError);
/**
* These are all the color attributes that all full themes should have.
*
* <p>All Light full themes should match {@code Theme.MaterialComponents.Light} and all Dark full
* themes should match {@code Theme.MaterialComponents}.
*/
private static final ImmutableList<Integer> MATERIAL_FULL_COLOR_ATTRIBUTES =
ImmutableList.<Integer>builder()
.addAll(MATERIAL_BRIDGE_COLOR_ATTRIBUTES)
.addAll(MATERIAL_NON_BRIDGE_COLOR_ATTRIBUTES)
.build();
/**
* These are widget styles that should be the same for *all* bridge and full themes (Light and
* Dark).
*/
private static final ImmutableList<Integer> MATERIAL_BRIDGE_WIDGET_STYLE_ATTRIBUTES =
ImmutableList.of(
R.attr.badgeStyle,
R.attr.chipStyle,
R.attr.chipGroupStyle,
R.attr.chipStandaloneStyle,
R.attr.circularProgressIndicatorStyle,
R.attr.extendedFloatingActionButtonStyle,
R.attr.linearProgressIndicatorStyle,
R.attr.materialButtonStyle,
R.attr.materialButtonOutlinedStyle,
R.attr.materialButtonToggleGroupStyle,
R.attr.materialCardViewStyle,
R.attr.navigationRailStyle,
R.attr.sliderStyle);
/**
* These are widget styles that should be the same for *all* full themes (Light and Dark) in
* addition to the {@link #MATERIAL_BRIDGE_WIDGET_STYLE_ATTRIBUTES}.
*/
private static final ImmutableList<Integer> MATERIAL_NON_BRIDGE_WIDGET_STYLE_ATTRIBUTES =
ImmutableList.of(
R.attr.borderlessButtonStyle,
R.attr.bottomNavigationStyle,
R.attr.checkboxStyle,
R.attr.floatingActionButtonStyle,
R.attr.listPopupWindowStyle,
R.attr.navigationViewStyle,
R.attr.popupMenuStyle,
R.attr.radioButtonStyle,
R.attr.snackbarStyle,
R.attr.snackbarButtonStyle,
R.attr.snackbarTextViewStyle,
R.attr.switchStyle,
R.attr.tabStyle,
R.attr.textInputStyle,
R.attr.textInputOutlinedStyle,
R.attr.textInputFilledStyle,
R.attr.textInputOutlinedDenseStyle,
R.attr.textInputFilledDenseStyle,
R.attr.textInputOutlinedExposedDropdownMenuStyle,
R.attr.textInputFilledExposedDropdownMenuStyle,
R.attr.toolbarStyle);
/** These are all the widget style attributes that should be the same for *all* full themes. */
private static final ImmutableList<Integer> MATERIAL_COMMON_WIDGET_STYLE_ATTRIBUTES =
ImmutableList.<Integer>builder()
.addAll(MATERIAL_BRIDGE_WIDGET_STYLE_ATTRIBUTES)
.addAll(MATERIAL_NON_BRIDGE_WIDGET_STYLE_ATTRIBUTES)
.build();
/**
* These are all the attributes where the light bridge theme should match the light full theme and
* the dark bridge theme should match the dark full theme.
*/
private static final ImmutableList<Integer> MATERIAL_BRIDGE_ATTRIBUTES =
ImmutableList.<Integer>builder()
.addAll(MATERIAL_BRIDGE_COLOR_ATTRIBUTES)
.addAll(MATERIAL_BRIDGE_WIDGET_STYLE_ATTRIBUTES)
.addAll(MOTION_STYLE_ATTRIBUTES)
.addAll(DEPRECATED_TYPOGRAPHY_ATTRIBUTES)
.build();
/**
* These are all the attributes where full themes should match {@code Theme.MaterialComponents} or
* {@code Theme.MaterialComponents.Light}.
*/
private static final ImmutableList<Integer> MATERIAL_FULL_ATTRIBUTES =
ImmutableList.<Integer>builder()
.addAll(MATERIAL_FULL_COLOR_ATTRIBUTES)
.addAll(MATERIAL_COMMON_WIDGET_STYLE_ATTRIBUTES)
.build();
/**
* These are color attributes that all M3 themes should have.
*
* <p>All M3 Light themes should match {@code Theme.Material3.Light} and all M3 Dark themes should
* match {@code Theme.Material3.Dark}.
*/
private static final ImmutableList<Integer> M3_ACCENT_COLOR_ATTRIBUTES =
ImmutableList.of(
// Primary colors.
R.attr.colorPrimary,
R.attr.colorPrimaryDark,
R.attr.colorOnPrimary,
R.attr.colorPrimaryInverse,
R.attr.colorPrimaryContainer,
R.attr.colorOnPrimaryContainer,
// Secondary colors.
R.attr.colorSecondary,
R.attr.colorOnSecondary,
R.attr.colorSecondaryContainer,
R.attr.colorOnSecondaryContainer,
// Tertiary colors.
R.attr.colorTertiary,
R.attr.colorOnTertiary,
R.attr.colorTertiaryContainer,
R.attr.colorOnTertiaryContainer);
/**
* These are color attributes that all themes and theme overlays should have.
*
* <p>All M3 Light themes should match {@code Theme.Material3.Light} and all M3 Dark themes should
* match {@code Theme.Material3.Dark}.
*/
private static final ImmutableList<Integer> M3_ADDITIONAL_COLOR_ATTRIBUTES =
ImmutableList.of(
// Background colors.
android.R.attr.colorBackground,
R.attr.colorOnBackground,
// Surface colors.
R.attr.colorSurface,
R.attr.colorOnSurface,
R.attr.colorSurfaceVariant,
R.attr.colorOnSurfaceVariant,
R.attr.colorSurfaceInverse,
R.attr.colorOnSurfaceInverse,
// Outline color.
R.attr.colorOutline,
// Error colors.
R.attr.colorError,
R.attr.colorOnError,
R.attr.colorErrorContainer,
R.attr.colorOnErrorContainer);
/**
* These are deprecated color attributes in M3. We only have them in themes but not theme
* overlays.
*/
private static final ImmutableList<Integer> M3_DEPRECATED_COLOR_ATTRIBUTES =
ImmutableList.of(R.attr.colorPrimaryVariant, R.attr.colorSecondaryVariant);
private static final ImmutableList<Integer> M3_FULL_COLOR_ATTRIBUTES =
ImmutableList.<Integer>builder()
.addAll(M3_ACCENT_COLOR_ATTRIBUTES)
.addAll(M3_ADDITIONAL_COLOR_ATTRIBUTES)
.addAll(M3_DEPRECATED_COLOR_ATTRIBUTES)
.build();
private static final ImmutableList<Integer> M3_ACTIVE_TYPOGRAPHY_ATTRIBUTES =
ImmutableList.of(
R.attr.textAppearanceDisplayLarge,
R.attr.textAppearanceDisplayMedium,
R.attr.textAppearanceDisplaySmall,
R.attr.textAppearanceHeadlineLarge,
R.attr.textAppearanceHeadlineMedium,
R.attr.textAppearanceHeadlineSmall,
R.attr.textAppearanceTitleLarge,
R.attr.textAppearanceTitleMedium,
R.attr.textAppearanceTitleSmall,
R.attr.textAppearanceBodyLarge,
R.attr.textAppearanceBodyMedium,
R.attr.textAppearanceBodySmall,
R.attr.textAppearanceLabelLarge,
R.attr.textAppearanceLabelMedium,
R.attr.textAppearanceLabelSmall);
private static final ImmutableList<Integer> DEFAULT_FRAMEWORK_TEXT_STYLE_ATTRIBUTES =
ImmutableList.of(
android.R.attr.textAppearance,
android.R.attr.textAppearanceInverse,
android.R.attr.textAppearanceLarge,
android.R.attr.textAppearanceLargeInverse,
android.R.attr.textAppearanceMedium,
android.R.attr.textAppearanceMediumInverse,
android.R.attr.textAppearanceSmall,
android.R.attr.textAppearanceSmallInverse,
android.R.attr.textAppearanceListItem,
android.R.attr.textAppearanceListItemSmall,
android.R.attr.textAppearanceListItemSecondary,
R.attr.textAppearanceListItem,
R.attr.textAppearanceListItemSmall,
R.attr.textAppearanceListItemSecondary);
/** These are typography styles that should be the same for *all* M3 full themes. */
private static final ImmutableList<Integer> M3_FULL_TYPOGRAPHY_ATTRIBUTES =
ImmutableList.<Integer>builder()
.addAll(M3_ACTIVE_TYPOGRAPHY_ATTRIBUTES)
.addAll(DEPRECATED_TYPOGRAPHY_ATTRIBUTES)
.addAll(DEFAULT_FRAMEWORK_TEXT_STYLE_ATTRIBUTES)
.build();
/** These are widget styles that should be the same for *all* M3 full themes. */
private static final ImmutableList<Integer> M3_COMMON_WIDGET_STYLE_ATTRIBUTES =
ImmutableList.of(
R.attr.appBarLayoutStyle,
R.attr.badgeStyle,
R.attr.borderlessButtonStyle,
R.attr.bottomAppBarStyle,
R.attr.bottomNavigationStyle,
R.attr.buttonBarButtonStyle,
R.attr.checkboxStyle,
R.attr.chipStyle,
R.attr.chipGroupStyle,
R.attr.chipStandaloneStyle,
R.attr.circularProgressIndicatorStyle,
R.attr.collapsingToolbarLayoutStyle,
R.attr.collapsingToolbarLayoutMediumStyle,
R.attr.collapsingToolbarLayoutLargeStyle,
R.attr.extendedFloatingActionButtonStyle,
R.attr.extendedFloatingActionButtonPrimaryStyle,
R.attr.extendedFloatingActionButtonSecondaryStyle,
R.attr.extendedFloatingActionButtonTertiaryStyle,
R.attr.extendedFloatingActionButtonSurfaceStyle,
R.attr.floatingActionButtonStyle,
R.attr.floatingActionButtonPrimaryStyle,
R.attr.floatingActionButtonSecondaryStyle,
R.attr.floatingActionButtonTertiaryStyle,
R.attr.floatingActionButtonSurfaceStyle,
R.attr.floatingActionButtonLargeStyle,
R.attr.floatingActionButtonLargePrimaryStyle,
R.attr.floatingActionButtonLargeSecondaryStyle,
R.attr.floatingActionButtonLargeTertiaryStyle,
R.attr.floatingActionButtonLargeSurfaceStyle,
R.attr.linearProgressIndicatorStyle,
R.attr.materialButtonOutlinedStyle,
R.attr.materialButtonStyle,
R.attr.materialCardViewStyle,
R.attr.materialCardViewOutlinedStyle,
R.attr.materialCardViewFilledStyle,
R.attr.materialCardViewElevatedStyle,
R.attr.radioButtonStyle,
R.attr.sliderStyle,
R.attr.snackbarStyle,
R.attr.snackbarButtonStyle,
R.attr.snackbarTextViewStyle,
R.attr.switchStyle,
R.attr.tabStyle,
R.attr.tabSecondaryStyle,
R.attr.textInputStyle,
R.attr.textInputOutlinedStyle,
R.attr.textInputFilledStyle,
R.attr.textInputOutlinedDenseStyle,
R.attr.textInputFilledDenseStyle,
R.attr.textInputOutlinedExposedDropdownMenuStyle,
R.attr.textInputFilledExposedDropdownMenuStyle,
R.attr.toolbarStyle,
R.attr.toolbarSurfaceStyle);
/**
* These are all the attributes where full themes should match {@code Theme.Material3.Light} or
* {@code Theme_Material3_Dark}.
*/
private static final ImmutableList<Integer> M3_FULL_ATTRIBUTES =
ImmutableList.<Integer>builder()
.addAll(M3_FULL_COLOR_ATTRIBUTES)
.addAll(M3_FULL_TYPOGRAPHY_ATTRIBUTES)
.addAll(MOTION_STYLE_ATTRIBUTES)
.addAll(M3_COMMON_WIDGET_STYLE_ATTRIBUTES)
.build();
@Parameters(name = "{0}")
public static ImmutableList<Object[]> getTestData() {
return ImmutableList.<Object[]>builder()
// Within Material Light theme.
.addAll(
createTestData(
R.style.Theme_MaterialComponents_Light, MATERIAL_INTRA_LIGHT_THEME_ATTRIBUTES))
// Within Material Dark theme.
.addAll(
createTestData(R.style.Theme_MaterialComponents, MATERIAL_INTRA_DARK_THEME_ATTRIBUTES))
// Material Dark Themes and Theme Overlays.
.addAll(
createTestData(
R.style.Theme_MaterialComponents_Bridge,
R.style.Theme_MaterialComponents,
MATERIAL_BRIDGE_ATTRIBUTES))
.addAll(
createTestData(
R.style.Theme_MaterialComponents_NoActionBar,
R.style.Theme_MaterialComponents,
MATERIAL_FULL_ATTRIBUTES))
.addAll(
createTestData(
R.style.Theme_MaterialComponents_Dialog,
R.style.Theme_MaterialComponents,
MATERIAL_FULL_ATTRIBUTES))
.addAll(
createTestData(
R.style.ThemeOverlay_MaterialComponents_Dark,
R.style.Theme_MaterialComponents,
MATERIAL_OVERLAY_COLOR_ATTRIBUTES))
// Material Light Themes and Theme Overlays.
.addAll(
createTestData(
R.style.Theme_MaterialComponents_Light_Bridge,
R.style.Theme_MaterialComponents_Light,
MATERIAL_BRIDGE_ATTRIBUTES))
.addAll(
createTestData(
R.style.Theme_MaterialComponents_Light_NoActionBar,
R.style.Theme_MaterialComponents_Light,
MATERIAL_FULL_ATTRIBUTES))
.addAll(
createTestData(
R.style.Theme_MaterialComponents_Light_Dialog,
R.style.Theme_MaterialComponents_Light,
MATERIAL_FULL_ATTRIBUTES))
.addAll(
createTestData(
R.style.ThemeOverlay_MaterialComponents_Light,
R.style.Theme_MaterialComponents_Light,
MATERIAL_OVERLAY_COLOR_ATTRIBUTES))
// Compare Material Light and Dark themes - they should have the same common widget styles.
.addAll(
createTestData(
R.style.Theme_MaterialComponents,
R.style.Theme_MaterialComponents_Light,
MATERIAL_COMMON_WIDGET_STYLE_ATTRIBUTES))
// Compare Material Light and Dark themes - they should have the same motion styles.
.addAll(
createTestData(
R.style.Theme_MaterialComponents,
R.style.Theme_MaterialComponents_Light,
MOTION_STYLE_ATTRIBUTES))
// Compare Material Light and Dark themes - they should have the same typography styles.
.addAll(
createTestData(
R.style.Theme_MaterialComponents,
R.style.Theme_MaterialComponents_Light,
DEPRECATED_TYPOGRAPHY_ATTRIBUTES))
// Compare Material Light and Dark bridge themes - they should have the same bridge widget
// styles.
.addAll(
createTestData(
R.style.Theme_MaterialComponents_Bridge,
R.style.Theme_MaterialComponents_Light_Bridge,
MATERIAL_BRIDGE_WIDGET_STYLE_ATTRIBUTES))
// M3 Dark Themes and Theme Overlays
.addAll(
createTestData(
R.style.Theme_Material3_Dark_NoActionBar,
R.style.Theme_Material3_Dark,
M3_FULL_ATTRIBUTES))
.addAll(
createTestData(
R.style.Theme_Material3_Dark_Dialog,
R.style.Theme_Material3_Dark,
M3_FULL_ATTRIBUTES))
.addAll(
createTestData(
R.style.ThemeOverlay_Material3_Dark,
R.style.Theme_Material3_Dark,
M3_ADDITIONAL_COLOR_ATTRIBUTES))
// M3 Light Themes and Theme Overlays
.addAll(
createTestData(
R.style.Theme_Material3_Light_NoActionBar,
R.style.Theme_Material3_Light,
M3_FULL_ATTRIBUTES))
.addAll(
createTestData(
R.style.Theme_Material3_Light_Dialog,
R.style.Theme_Material3_Light,
M3_FULL_ATTRIBUTES))
.addAll(
createTestData(
R.style.ThemeOverlay_Material3_Light,
R.style.Theme_Material3_Light,
M3_ADDITIONAL_COLOR_ATTRIBUTES))
// Compare M3 Light and Dark themes - they should have the same common widget styles.
.addAll(
createTestData(
R.style.Theme_Material3_Dark,
R.style.Theme_Material3_Light,
M3_COMMON_WIDGET_STYLE_ATTRIBUTES))
// Compare M3 Light and Dark themes - they should have the same typography styles.
.addAll(
createTestData(
R.style.Theme_Material3_Dark,
R.style.Theme_Material3_Light,
M3_FULL_TYPOGRAPHY_ATTRIBUTES))
// Compare M3 Light and Dark themes - they should have the same motion styles.
.addAll(
createTestData(
R.style.Theme_Material3_Dark,
R.style.Theme_Material3_Light,
MOTION_STYLE_ATTRIBUTES))
.build();
}
@Parameter(0)
public String testName;
@Parameter(1)
public int theme;
@Parameter(2)
public int attribute;
@Parameter(3)
public int comparisonTheme;
@Parameter(4)
public int comparisonAttribute;
private static final ImmutableMap<Integer, String> RESOURCE_NAME_MAP =
ResourceNameLookup.createResourceNameMap(R.style.class, R.attr.class, android.R.attr.class);
private static List<Object[]> createTestData(int theme, ImmutableList<int[]> attributes) {
List<Object[]> testData = new ArrayList<>();
for (int[] attr : attributes) {
testData.add(getTestParameters(theme, theme, attr));
}
Iterables.removeIf(testData, Predicates.isNull());
return testData;
}
private static List<Object[]> createTestData(
int theme, int comparisonTheme, ImmutableList<Integer> attributes) {
List<Object[]> testData = new ArrayList<>();
for (int attr : attributes) {
testData.add(getTestParameters(theme, comparisonTheme, attr));
}
Iterables.removeIf(testData, Predicates.isNull());
return testData;
}
private static Object[] getTestParameters(int theme, int comparisonTheme, int... attributes) {
if (attributes.length == 0) {
return null;
}
if (attributes.length > 2) {
throw new IllegalArgumentException("Cannot compare more than 2 attributes in a test.");
}
int attr = attributes[0];
int comparisonAttr = attributes[0];
if (attributes.length > 1) {
comparisonAttr = attributes[1];
}
return new Object[] {
String.format(
"%s %s expected to match %s %s",
RESOURCE_NAME_MAP.get(theme),
RESOURCE_NAME_MAP.get(attr),
RESOURCE_NAME_MAP.get(comparisonTheme),
RESOURCE_NAME_MAP.get(comparisonAttr)),
theme,
attr,
comparisonTheme,
comparisonAttr
};
}
@Test
public void testAttribute() {
assertThat(resolveAttribute(theme, attribute, "missing in theme"))
.isEqualTo(
resolveAttribute(comparisonTheme, comparisonAttribute, "missing in comparison theme"));
}
@SuppressWarnings("RestrictTo")
private static int resolveAttribute(int theme, int attribute, String error) {
return MaterialAttributes.resolveOrThrow(
new ContextThemeWrapper(getApplicationContext(), theme), attribute, error);
}
}