1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.managedprovisioning.common;
18 
19 import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
20 import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
21 
22 import static com.android.managedprovisioning.provisioning.Constants.FLAG_ENABLE_LIGHT_DARK_MODE;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import android.content.Context;
27 import android.content.Intent;
28 
29 import androidx.test.InstrumentationRegistry;
30 import androidx.test.filters.SmallTest;
31 
32 import com.google.android.setupcompat.util.WizardManagerHelper;
33 import com.google.android.setupdesign.R;
34 
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 
39 import java.util.Objects;
40 
41 @SmallTest
42 public class ThemeHelperTest {
43     private static final String THEME_TEST_VALUE = "glif_v3_light";
44     private static final Intent INTENT_WITH_THEME =
45             new Intent().putExtra(WizardManagerHelper.EXTRA_THEME, THEME_TEST_VALUE);
46     private static final Intent INTENT_NO_THEME = new Intent();
47 
48     private final Context mContext = InstrumentationRegistry.getTargetContext();
49     private boolean mInitialFeatureFlagValue;
50     private ThemeHelper.SetupWizardBridge mSetupWizardBridge;
51 
52     @Before
setUp()53     public void setUp() {
54         mInitialFeatureFlagValue = FLAG_ENABLE_LIGHT_DARK_MODE;
55     }
56 
57     @After
tearDown()58     public void tearDown() {
59         FLAG_ENABLE_LIGHT_DARK_MODE = mInitialFeatureFlagValue;
60     }
61 
62     @Test
inferThemeResId_themeProvidedAsExtra_suwDayNightEnabled_themesMatch()63     public void inferThemeResId_themeProvidedAsExtra_suwDayNightEnabled_themesMatch() {
64         FLAG_ENABLE_LIGHT_DARK_MODE = true;
65         ThemeHelper themeHelper = createThemeHelperWithDayNightEnabled(true);
66         Intent intent = new Intent()
67                 .putExtra(WizardManagerHelper.EXTRA_THEME, THEME_TEST_VALUE);
68         int expectedResId = mSetupWizardBridge.resolveTheme(
69                 R.style.SudThemeGlifV4_DayNight,
70                 THEME_TEST_VALUE,
71                 /* suppressDayNight= */ false);
72 
73         assertThat(themeHelper.inferThemeResId(mContext, intent)).isEqualTo(expectedResId);
74     }
75 
76     @Test
inferThemeResId_themeProvidedAsExtra_suwDayNightDisabled_themesMatch()77     public void inferThemeResId_themeProvidedAsExtra_suwDayNightDisabled_themesMatch() {
78         FLAG_ENABLE_LIGHT_DARK_MODE = true;
79         ThemeHelper themeHelper = createThemeHelperWithDayModeValues();
80         Intent intent = new Intent()
81                 .putExtra(WizardManagerHelper.EXTRA_THEME, THEME_TEST_VALUE);
82         int expectedResId = mSetupWizardBridge.resolveTheme(
83                 R.style.SudThemeGlifV4_Light,
84                 THEME_TEST_VALUE,
85                 /* suppressDayNight= */ true);
86 
87         assertThat(themeHelper.inferThemeResId(mContext, intent)).isEqualTo(expectedResId);
88     }
89 
90     @Test
91     public void
inferThemeResId_themeProvidedAsExtra_suwDayNightEnabled_flagDisabled_themesMatch()92             inferThemeResId_themeProvidedAsExtra_suwDayNightEnabled_flagDisabled_themesMatch() {
93         FLAG_ENABLE_LIGHT_DARK_MODE = false;
94         ThemeHelper themeHelper = createThemeHelperWithDayNightEnabled(true);
95         Intent intent = new Intent()
96                 .putExtra(WizardManagerHelper.EXTRA_THEME, THEME_TEST_VALUE);
97         int expectedResId = mSetupWizardBridge.resolveTheme(
98                 R.style.SudThemeGlifV4_DayNight,
99                 THEME_TEST_VALUE,
100                 /* suppressDayNight= */ true);
101 
102         assertThat(themeHelper.inferThemeResId(mContext, intent)).isEqualTo(expectedResId);
103     }
104 
105     @Test
106     public void
inferThemeResId_themeProvidedAsExtra_suwDayNightDisabled_flagDisabled_themesMatch()107             inferThemeResId_themeProvidedAsExtra_suwDayNightDisabled_flagDisabled_themesMatch() {
108         FLAG_ENABLE_LIGHT_DARK_MODE = false;
109         ThemeHelper themeHelper = createThemeHelperWithDayModeValues();
110         Intent intent = new Intent()
111                 .putExtra(WizardManagerHelper.EXTRA_THEME, THEME_TEST_VALUE);
112         int expectedResId = mSetupWizardBridge.resolveTheme(
113                 R.style.SudThemeGlifV4_Light,
114                 THEME_TEST_VALUE,
115                 /* suppressDayNight= */ true);
116 
117         assertThat(themeHelper.inferThemeResId(mContext, intent)).isEqualTo(expectedResId);
118     }
119 
120     @Test
inferThemeResId_systemPropertyThemeUsed_suwDayNightEnabled_themesMatch()121     public void inferThemeResId_systemPropertyThemeUsed_suwDayNightEnabled_themesMatch() {
122         FLAG_ENABLE_LIGHT_DARK_MODE = true;
123         ThemeHelper themeHelper =
124                 createThemeHelperWithDayNightEnabledAndSystemPropertyTheme(
125                         true, THEME_TEST_VALUE);
126         int expectedResId = mSetupWizardBridge.resolveTheme(
127                 R.style.SudThemeGlifV4_DayNight,
128                 THEME_TEST_VALUE,
129                 /* suppressDayNight= */ false);
130 
131         assertThat(themeHelper.inferThemeResId(mContext, new Intent())).isEqualTo(expectedResId);
132     }
133 
134     @Test
inferThemeResId_glifLightThemeUsed_suwDayNightEnabled_themesMatch()135     public void inferThemeResId_glifLightThemeUsed_suwDayNightEnabled_themesMatch() {
136         FLAG_ENABLE_LIGHT_DARK_MODE = true;
137         ThemeHelper themeHelper =
138                 createThemeHelperWithDayNightEnabled(true);
139         int expectedResId = mSetupWizardBridge.resolveTheme(
140                 R.style.SudThemeGlifV4_DayNight,
141                 com.google.android.setupdesign.util.ThemeHelper.THEME_GLIF_V4,
142                 /* suppressDayNight= */ false);
143 
144         assertThat(themeHelper.inferThemeResId(mContext, new Intent())).isEqualTo(expectedResId);
145     }
146 
147     @Test
inferThemeResId_suppressDayNight_systemNightMode_noSetupWizard_isDarkMode()148     public void inferThemeResId_suppressDayNight_systemNightMode_noSetupWizard_isDarkMode() {
149         FLAG_ENABLE_LIGHT_DARK_MODE = true;
150         ThemeHelper themeHelper =
151                 createThemeHelperWithDayNightAndSystemNightModeEnabled(
152                         /* setupWizardDayNightEnabled= */ false,
153                         /* isSystemNightMode= */ true);
154         int expectedResId = mSetupWizardBridge.resolveTheme(
155                 R.style.SudThemeGlifV4_Light,
156                 com.google.android.setupdesign.util.ThemeHelper.THEME_GLIF_V4,
157                 /* suppressDayNight= */ true);
158 
159         assertThat(themeHelper.inferThemeResId(mContext, new Intent())).isEqualTo(expectedResId);
160     }
161 
162     @Test
inferThemeResId_suppressDayNight_systemNightMode_noSetupWizard_isLightMode()163     public void inferThemeResId_suppressDayNight_systemNightMode_noSetupWizard_isLightMode() {
164         FLAG_ENABLE_LIGHT_DARK_MODE = true;
165         ThemeHelper themeHelper =
166                 createThemeHelperWithDayNightAndSystemNightModeEnabled(
167                         /* setupWizardDayNightEnabled= */ false,
168                         /* isSystemNightMode= */ false);
169         int expectedResId = mSetupWizardBridge.resolveTheme(
170                 R.style.SudThemeGlifV4_Light,
171                 com.google.android.setupdesign.util.ThemeHelper.THEME_GLIF_V4_LIGHT,
172                 /* suppressDayNight= */ true);
173 
174         assertThat(themeHelper.inferThemeResId(mContext, new Intent())).isEqualTo(expectedResId);
175     }
176 
177     @Test
getDefaultNightMode_returnsYes()178     public void getDefaultNightMode_returnsYes() {
179         FLAG_ENABLE_LIGHT_DARK_MODE = true;
180         ThemeHelper themeHelper = createThemeHelperForNightMode();
181         assertThat(themeHelper.getDefaultNightMode(mContext, INTENT_WITH_THEME))
182                 .isEqualTo(MODE_NIGHT_YES);
183     }
184 
185     @Test
getDefaultNightMode_featureFlagDisabled_returnsNo()186     public void getDefaultNightMode_featureFlagDisabled_returnsNo() {
187         FLAG_ENABLE_LIGHT_DARK_MODE = false;
188         ThemeHelper themeHelper = createThemeHelperForNightMode();
189 
190         assertThat(themeHelper.getDefaultNightMode(mContext, INTENT_WITH_THEME))
191                 .isEqualTo(MODE_NIGHT_NO);
192     }
193 
194     @Test
getDefaultNightMode_setupWizardDayNightDisabled_returnsNo()195     public void getDefaultNightMode_setupWizardDayNightDisabled_returnsNo() {
196         FLAG_ENABLE_LIGHT_DARK_MODE = true;
197         ThemeHelper themeHelper = createThemeHelperWithDayNightEnabled(false);
198 
199         assertThat(themeHelper.getDefaultNightMode(mContext, INTENT_WITH_THEME))
200                 .isEqualTo(MODE_NIGHT_NO);
201     }
202 
203     @Test
getDefaultNightMode_systemNightModeFalse_returnsNo()204     public void getDefaultNightMode_systemNightModeFalse_returnsNo() {
205         FLAG_ENABLE_LIGHT_DARK_MODE = true;
206         ThemeHelper themeHelper = createThemeHelperWithSystemNightModeEnabled(false);
207 
208         assertThat(themeHelper.getDefaultNightMode(mContext, INTENT_WITH_THEME))
209                 .isEqualTo(MODE_NIGHT_NO);
210     }
211 
212     @Test
getDefaultNightMode_systemNightModeFalse_noThemeExtra_returnsNo()213     public void getDefaultNightMode_systemNightModeFalse_noThemeExtra_returnsNo() {
214         FLAG_ENABLE_LIGHT_DARK_MODE = true;
215         ThemeHelper themeHelper = createThemeHelperWithSystemNightModeEnabled(false);
216 
217         assertThat(themeHelper.getDefaultNightMode(mContext, INTENT_NO_THEME))
218                 .isEqualTo(MODE_NIGHT_NO);
219     }
220 
221     @Test
getDefaultNightMode_systemNightModeTrue_noThemeExtra_returnsYes()222     public void getDefaultNightMode_systemNightModeTrue_noThemeExtra_returnsYes() {
223         FLAG_ENABLE_LIGHT_DARK_MODE = true;
224         ThemeHelper themeHelper = createThemeHelperWithSystemNightModeEnabled(true);
225 
226         assertThat(themeHelper.getDefaultNightMode(mContext, INTENT_NO_THEME))
227                 .isEqualTo(MODE_NIGHT_YES);
228     }
229 
createThemeHelperForNightMode()230     private ThemeHelper createThemeHelperForNightMode() {
231         return createThemeHelper(
232                 /* setupWizardDayNightEnabled= */ true,
233                 /* systemPropertySetupWizardTheme= */ null,
234                 /* isSystemNightMode= */ true);
235     }
236 
createThemeHelperWithDayNightEnabled(boolean setupWizardDayNightEnabled)237     private ThemeHelper createThemeHelperWithDayNightEnabled(boolean setupWizardDayNightEnabled) {
238         return createThemeHelper(
239                 setupWizardDayNightEnabled,
240                 /* systemPropertySetupWizardTheme= */ null,
241                 /* isSystemNightMode= */ true);
242     }
243 
createThemeHelperWithDayNightAndSystemNightModeEnabled( boolean setupWizardDayNightEnabled, boolean isSystemNightMode)244     private ThemeHelper createThemeHelperWithDayNightAndSystemNightModeEnabled(
245             boolean setupWizardDayNightEnabled,
246             boolean isSystemNightMode) {
247         return createThemeHelper(
248                 setupWizardDayNightEnabled,
249                 /* systemPropertySetupWizardTheme= */ null,
250                 /* isSystemNightMode= */ isSystemNightMode);
251     }
252 
createThemeHelperWithSystemNightModeEnabled(boolean isSystemNightMode)253     private ThemeHelper createThemeHelperWithSystemNightModeEnabled(boolean isSystemNightMode) {
254         return createThemeHelper(
255                 /* setupWizardDayNightEnabled= */ false,
256                 /* systemPropertySetupWizardTheme= */ null,
257                 isSystemNightMode);
258     }
259 
createThemeHelperWithDayModeValues()260     private ThemeHelper createThemeHelperWithDayModeValues() {
261         return createThemeHelper(
262                 /* setupWizardDayNightEnabled= */ false,
263                 /* systemPropertySetupWizardTheme= */ null,
264                 /* isSystemNightMode= */ false);
265     }
266 
createThemeHelperWithDayNightEnabledAndSystemPropertyTheme( boolean setupWizardDayNightEnabled, String systemPropertySetupWizardTheme)267     private ThemeHelper createThemeHelperWithDayNightEnabledAndSystemPropertyTheme(
268             boolean setupWizardDayNightEnabled, String systemPropertySetupWizardTheme) {
269         return createThemeHelper(
270                 setupWizardDayNightEnabled,
271                 systemPropertySetupWizardTheme,
272                 /* isSystemNightMode= */ false);
273     }
274 
createThemeHelper( boolean setupWizardDayNightEnabled, String systemPropertySetupWizardTheme, boolean isSystemNightMode)275     private ThemeHelper createThemeHelper(
276             boolean setupWizardDayNightEnabled,
277             String systemPropertySetupWizardTheme,
278             boolean isSystemNightMode) {
279         mSetupWizardBridge = new ThemeHelper.SetupWizardBridge() {
280             @Override
281             public boolean isSetupWizardDayNightEnabled(Context context) {
282                 return setupWizardDayNightEnabled;
283             }
284 
285             @Override
286             public String getSystemPropertySetupWizardTheme() {
287                 return systemPropertySetupWizardTheme;
288             }
289 
290             @Override
291             public int resolveTheme(int defaultTheme, String themeName, boolean suppressDayNight) {
292                 return Objects.hash(defaultTheme, themeName, suppressDayNight);
293             }
294         };
295         return new ThemeHelper(context -> isSystemNightMode, mSetupWizardBridge);
296     }
297 }
298