Skip to content

Commit 66581f2

Browse files
Material Design Teamafohrman
authored andcommitted
[M3][Color] Added DynamicColorsTest.
PiperOrigin-RevId: 450691016
1 parent 1a91e68 commit 66581f2

File tree

5 files changed

+156
-31
lines changed

5 files changed

+156
-31
lines changed

lib/javatests/com/google/android/material/color/DynamicColorsTest.java

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.os.Build;
2828
import android.os.Build.VERSION_CODES;
2929
import android.os.Bundle;
30+
import android.util.TypedValue;
3031
import androidx.appcompat.view.ContextThemeWrapper;
3132
import com.google.android.material.color.DynamicColors.Precondition;
3233
import com.google.android.material.resources.MaterialAttributes;
@@ -45,6 +46,7 @@
4546
/** Tests the logic of {@link com.google.android.material.color.DynamicColors} utility class. */
4647
@RunWith(ParameterizedRobolectricTestRunner.class)
4748
public class DynamicColorsTest {
49+
4850
@Parameter(0)
4951
public String testName;
5052

@@ -58,7 +60,7 @@ public class DynamicColorsTest {
5860
@Parameters(name = "{0}")
5961
public static ImmutableList<Object[]> getTestData() {
6062
return ImmutableList.<Object[]>builder()
61-
.add(new Object[] {"Test dynamic colors with Light Theme", R.style.Theme_Material3_Light})
63+
.add(new Object[] {"Test dynamic colors with Light Theme", R.style.Test_Theme_BaseTheme})
6264
.build();
6365
}
6466

@@ -75,51 +77,58 @@ public void testApplyOnApplicationWithDefaultTheme() {
7577
DynamicColors.applyToActivitiesIfAvailable(mockApplication);
7678
mockApplication.capturedCallbacks.onActivityPreCreated(mockActivity, new Bundle());
7779

78-
// TODO(b/230848477): Update tests to make sure dynamic colors theme overlay is indeed applied.
79-
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
80+
assertThat(getThemeId()).isEqualTo(R.id.dynamic_theme_id);
8081
}
8182

8283
@Test
8384
public void testApplyOnApplicationWithCustomTheme() {
84-
final int mockThemeOverlay = 0xABCDABCD;
85-
DynamicColors.applyToActivitiesIfAvailable(mockApplication, mockThemeOverlay);
85+
DynamicColors.applyToActivitiesIfAvailable(
86+
mockApplication,
87+
new DynamicColorsOptions.Builder()
88+
.setThemeOverlay(R.style.Test_ThemeOverlay_CustomTheme)
89+
.build());
8690
mockApplication.capturedCallbacks.onActivityPreCreated(mockActivity, new Bundle());
8791

88-
// TODO(b/230848477): Update tests to make sure dynamic colors theme overlay is indeed applied.
89-
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
92+
assertThat(getThemeId()).isEqualTo(R.id.custom_theme_id);
9093
}
9194

9295
@Test
9396
public void testApplyOnApplicationWithPreconditionFalse() {
9497
final MockPrecondition mockPrecondition = new MockPrecondition();
95-
DynamicColors.applyToActivitiesIfAvailable(mockApplication, mockPrecondition);
98+
DynamicColors.applyToActivitiesIfAvailable(
99+
mockApplication,
100+
new DynamicColorsOptions.Builder().setPrecondition(mockPrecondition).build());
96101
mockApplication.capturedCallbacks.onActivityPreCreated(mockActivity, new Bundle());
97102

98103
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
104+
assertThat(getThemeId()).isEqualTo(R.id.default_theme_id);
99105
}
100106

101107
@Test
102108
public void testApplyOnApplicationWithPreconditionTrue() {
103109
final MockPrecondition mockPrecondition = new MockPrecondition();
104110
mockPrecondition.shouldApplyDynamicColors = true;
105-
DynamicColors.applyToActivitiesIfAvailable(mockApplication, mockPrecondition);
111+
DynamicColors.applyToActivitiesIfAvailable(
112+
mockApplication,
113+
new DynamicColorsOptions.Builder().setPrecondition(mockPrecondition).build());
106114
mockApplication.capturedCallbacks.onActivityPreCreated(mockActivity, new Bundle());
107115

108-
// TODO(b/230848477): Update tests to make sure dynamic colors theme overlay is indeed applied.
109-
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
116+
assertThat(getThemeId()).isEqualTo(R.id.dynamic_theme_id);
110117
}
111118

112119
@Test
113120
public void testApplyOnApplicationWithCustomThemeAndPrecondition() {
114-
final int mockThemeOverlay = 0xABCDABCD;
115121
final MockPrecondition mockPrecondition = new MockPrecondition();
116122
mockPrecondition.shouldApplyDynamicColors = true;
117123
DynamicColors.applyToActivitiesIfAvailable(
118-
mockApplication, mockThemeOverlay, mockPrecondition);
124+
mockApplication,
125+
new DynamicColorsOptions.Builder()
126+
.setPrecondition(mockPrecondition)
127+
.setThemeOverlay(R.style.Test_ThemeOverlay_CustomTheme)
128+
.build());
119129
mockApplication.capturedCallbacks.onActivityPreCreated(mockActivity, new Bundle());
120130

121-
// TODO(b/230848477): Update tests to make sure dynamic colors theme overlay is indeed applied.
122-
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
131+
assertThat(getThemeId()).isEqualTo(R.id.custom_theme_id);
123132
}
124133

125134
@Test
@@ -129,10 +138,15 @@ public void testApplyOnApplicationWithNoDynamicColorAvailable() {
129138
final MockPrecondition mockPrecondition = new MockPrecondition();
130139
mockPrecondition.shouldApplyDynamicColors = true;
131140
DynamicColors.applyToActivitiesIfAvailable(
132-
mockApplication, mockThemeOverlay, mockPrecondition);
141+
mockApplication,
142+
new DynamicColorsOptions.Builder()
143+
.setPrecondition(mockPrecondition)
144+
.setThemeOverlay(mockThemeOverlay)
145+
.build());
133146
mockApplication.capturedCallbacks.onActivityPreCreated(mockActivity, new Bundle());
134147

135148
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
149+
assertThat(getThemeId()).isEqualTo(R.id.default_theme_id);
136150
}
137151

138152
@Test
@@ -142,54 +156,64 @@ public void testApplyOnApplicationWithLowSdkVersion() {
142156
final MockPrecondition mockPrecondition = new MockPrecondition();
143157
mockPrecondition.shouldApplyDynamicColors = true;
144158
DynamicColors.applyToActivitiesIfAvailable(
145-
mockApplication, mockThemeOverlay, mockPrecondition);
159+
mockApplication,
160+
new DynamicColorsOptions.Builder()
161+
.setPrecondition(mockPrecondition)
162+
.setThemeOverlay(mockThemeOverlay)
163+
.build());
146164
mockApplication.capturedCallbacks.onActivityPreCreated(mockActivity, new Bundle());
147165

148166
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
167+
assertThat(getThemeId()).isEqualTo(R.id.default_theme_id);
149168
}
150169

151170
@Test
152171
public void testApplyOnActivityWithDefaultTheme() {
153-
DynamicColors.applyIfAvailable(mockActivity);
172+
DynamicColors.applyToActivityIfAvailable(mockActivity);
154173

155-
// TODO(b/230848477): Update tests to make sure dynamic colors theme overlay is indeed applied.
156-
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
174+
assertThat(getThemeId()).isEqualTo(R.id.dynamic_theme_id);
157175
}
158176

159177
@Test
160178
public void testApplyOnActivityWithCustomTheme() {
161-
final int mockThemeOverlay = 0xABCDABCD;
162-
DynamicColors.applyIfAvailable(mockActivity, mockThemeOverlay);
179+
DynamicColors.applyToActivityIfAvailable(
180+
mockActivity,
181+
new DynamicColorsOptions.Builder()
182+
.setThemeOverlay(R.style.Test_ThemeOverlay_CustomTheme)
183+
.build());
163184

164-
// TODO(b/230848477): Update tests to make sure dynamic colors theme overlay is indeed applied.
165-
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
185+
assertThat(getThemeId()).isEqualTo(R.id.custom_theme_id);
166186
}
167187

168188
@Test
169189
public void testApplyOnActivityWithPreconditionFalse() {
170190
final MockPrecondition mockPrecondition = new MockPrecondition();
171-
DynamicColors.applyIfAvailable(mockActivity, mockPrecondition);
191+
DynamicColors.applyToActivityIfAvailable(
192+
mockActivity, new DynamicColorsOptions.Builder().setPrecondition(mockPrecondition).build());
172193

173194
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
195+
assertThat(getThemeId()).isEqualTo(R.id.default_theme_id);
174196
}
175197

176198
@Test
177199
public void testApplyOnActivityWithPreconditionTrue() {
178200
final MockPrecondition mockPrecondition = new MockPrecondition();
179201
mockPrecondition.shouldApplyDynamicColors = true;
180-
DynamicColors.applyIfAvailable(mockActivity, mockPrecondition);
202+
DynamicColors.applyToActivityIfAvailable(
203+
mockActivity, new DynamicColorsOptions.Builder().setPrecondition(mockPrecondition).build());
181204

182-
// TODO(b/230848477): Update tests to make sure dynamic colors theme overlay is indeed applied.
183-
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
205+
assertThat(getThemeId()).isEqualTo(R.id.dynamic_theme_id);
184206
}
185207

186208
@Test
187209
public void testApplyOnActivityWithNoDynamicColorAvailable() {
188210
setDynamicColorAvailability(false);
189211
final int mockThemeOverlay = 0xABCDABCD;
190-
DynamicColors.applyIfAvailable(mockActivity, mockThemeOverlay);
212+
DynamicColors.applyToActivityIfAvailable(
213+
mockActivity, new DynamicColorsOptions.Builder().setThemeOverlay(mockThemeOverlay).build());
191214

192215
assertThat(getThemeResId(mockActivity)).isEqualTo(baseTheme);
216+
assertThat(getThemeId()).isEqualTo(R.id.default_theme_id);
193217
}
194218

195219
@Test
@@ -234,6 +258,7 @@ public void testWrapContextWithNoDynamicColorAvailable() {
234258
Context context = DynamicColors.wrapContextIfAvailable(mockActivity, mockThemeOverlay);
235259

236260
assertThat(getThemeResId(context)).isEqualTo(baseTheme);
261+
assertThat(getThemeId()).isEqualTo(R.id.default_theme_id);
237262
}
238263

239264
private void setDynamicColorAvailability(boolean available) {
@@ -242,8 +267,7 @@ private void setDynamicColorAvailability(boolean available) {
242267
}
243268

244269
private void setSdkVersion(int sdkVersion) {
245-
ReflectionHelpers.setStaticField(
246-
Build.VERSION.class, "SDK_INT", sdkVersion);
270+
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", sdkVersion);
247271
}
248272

249273
@SuppressWarnings("RestrictTo")
@@ -276,4 +300,10 @@ public boolean shouldApplyDynamicColors(Activity activity, int theme) {
276300
return shouldApplyDynamicColors;
277301
}
278302
}
303+
304+
private int getThemeId() {
305+
TypedValue typedValue = new TypedValue();
306+
mockActivity.getTheme().resolveAttribute(R.attr.themeId, typedValue, true);
307+
return typedValue.resourceId;
308+
}
279309
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2022 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<resources>
18+
19+
<attr name="themeId" format="integer"/>
20+
</resources>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2022 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<resources>
18+
19+
<item name="default_theme_id" type="id" />
20+
<item name="custom_theme_id" type="id" />
21+
<item name="dynamic_theme_id" type="id" />
22+
</resources>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2022 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<resources xmlns:tools="http://schemas.android.com/tools">
19+
20+
<style name="Test.ThemeOverlay.CustomTheme" parent="@style/ThemeOverlay.Material3.DynamicColors.DayNight">
21+
<item name="themeId">@id/custom_theme_id</item>
22+
</style>
23+
24+
<style name="Test.ThemeOverlay.DynamicColors" parent="">
25+
<item name="themeId">@id/dynamic_theme_id</item>
26+
</style>
27+
28+
</resources>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2022 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<resources xmlns:tools="http://schemas.android.com/tools">
19+
20+
<style name="Test.Theme.BaseTheme" parent="@style/Theme.Material3.Light">
21+
<item name="themeId">@id/default_theme_id</item>
22+
<item name="dynamicColorThemeOverlay">@style/Test.ThemeOverlay.DynamicColors</item>
23+
</style>
24+
25+
</resources>

0 commit comments

Comments
 (0)