1 /*
2  * Copyright (C) 2016 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.settings;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 
22 import android.content.ContentProvider;
23 import android.content.ContentValues;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.sysprop.SetupWizardProperties;
30 import android.text.TextUtils;
31 
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34 import androidx.test.core.app.ApplicationProvider;
35 
36 import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
37 import com.google.android.setupcompat.util.WizardManagerHelper;
38 import com.google.android.setupdesign.util.ThemeHelper;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.robolectric.Robolectric;
44 import org.robolectric.RobolectricTestRunner;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class SetupWizardUtilsTest {
48 
49     private Context mContext;
50 
51     @Before
setup()52     public void setup() {
53         PartnerConfigHelper.resetInstance();
54         mContext = ApplicationProvider.getApplicationContext();
55     }
56 
57     @Test
testCopySetupExtras()58     public void testCopySetupExtras() {
59         Intent fromIntent = new Intent();
60         final String theme = "TEST_THEME";
61         fromIntent.putExtra(WizardManagerHelper.EXTRA_THEME, theme);
62         fromIntent.putExtra(WizardManagerHelper.EXTRA_IS_SETUP_FLOW, true);
63         Intent toIntent = new Intent();
64         SetupWizardUtils.copySetupExtras(fromIntent, toIntent);
65 
66         assertThat(theme).isEqualTo(toIntent.getStringExtra(WizardManagerHelper.EXTRA_THEME));
67         assertThat(toIntent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_SETUP_FLOW, false))
68                 .isTrue();
69         assertThat(toIntent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, true))
70                 .isFalse();
71     }
72 
73     @Test
testCopyLifecycleExtra()74     public void testCopyLifecycleExtra() {
75         Intent fromIntent = new Intent();
76         final String theme = "TEST_THEME";
77         fromIntent.putExtra(WizardManagerHelper.EXTRA_THEME, theme);
78         fromIntent.putExtra(WizardManagerHelper.EXTRA_IS_SETUP_FLOW, true);
79         Bundle dstBundle = new Bundle();
80         dstBundle = SetupWizardUtils.copyLifecycleExtra(fromIntent.getExtras(), dstBundle);
81 
82         assertThat(dstBundle).isNotNull();
83         assertThat(dstBundle.getString(WizardManagerHelper.EXTRA_THEME)).isNull();
84         assertThat(dstBundle.getBoolean(WizardManagerHelper.EXTRA_IS_SETUP_FLOW))
85                 .isTrue();
86         assertThat(dstBundle.getBoolean(WizardManagerHelper.EXTRA_IS_FIRST_RUN))
87                 .isFalse();
88     }
89 
90     @Test
testGetTheme_withIntentExtra_shouldReturnTheme()91     public void testGetTheme_withIntentExtra_shouldReturnTheme() {
92         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF);
93         Intent intent = createSetupWizardIntent();
94         intent.putExtra(WizardManagerHelper.EXTRA_THEME, ThemeHelper.THEME_GLIF_V2);
95 
96         assertThat(SetupWizardUtils.getTheme(mContext, intent))
97                 .isEqualTo(R.style.GlifV2Theme);
98     }
99 
100     @Test
testGetTheme_withEmptyIntent_shouldReturnSystemProperty()101     public void testGetTheme_withEmptyIntent_shouldReturnSystemProperty() {
102         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V2_LIGHT);
103         Intent intent = createSetupWizardIntent();
104 
105         assertThat(SetupWizardUtils.getTheme(mContext, intent))
106                 .isEqualTo(R.style.GlifV2Theme_Light);
107     }
108 
109     @Test
testGetTheme_whenSuwDayNightEnabledAndWithIntentExtra_shouldReturnDayNightTheme()110     public void testGetTheme_whenSuwDayNightEnabledAndWithIntentExtra_shouldReturnDayNightTheme() {
111         FakePartnerContentProvider provider =
112                 Robolectric.setupContentProvider(
113                         FakePartnerContentProvider.class, "com.google.android.setupwizard.partner");
114         provider.injectFakeDayNightEnabledResult(true);
115         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V2_LIGHT);
116         Intent intent = createSetupWizardIntent();
117         intent.putExtra(WizardManagerHelper.EXTRA_THEME, ThemeHelper.THEME_GLIF_V2);
118 
119         assertThat(SetupWizardUtils.getTheme(mContext, intent))
120                 .isEqualTo(R.style.GlifV2Theme_DayNight);
121     }
122 
123     @Test
testGetTheme_glifV3Light_shouldReturnLightTheme()124     public void testGetTheme_glifV3Light_shouldReturnLightTheme() {
125         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V3_LIGHT);
126         Intent intent = createSetupWizardIntent();
127 
128         assertThat(SetupWizardUtils.getTheme(mContext, intent))
129                 .isEqualTo(R.style.GlifV3Theme_Light);
130         assertThat(SetupWizardUtils.getTransparentTheme(mContext, intent))
131                 .isEqualTo(R.style.GlifV3Theme_Light_Transparent);
132     }
133 
134     @Test
testGetTheme_glifV3_shouldReturnTheme()135     public void testGetTheme_glifV3_shouldReturnTheme() {
136         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V3);
137         Intent intent = createSetupWizardIntent();
138 
139         assertThat(SetupWizardUtils.getTheme(mContext, intent))
140                 .isEqualTo(R.style.GlifV3Theme);
141         assertThat(SetupWizardUtils.getTransparentTheme(mContext, intent))
142                 .isEqualTo(R.style.GlifV3Theme_Transparent);
143     }
144 
145     @Test
testGetTheme_whenSuwDayNightDisabledAndGlifV2_shouldReturnLightTheme()146     public void testGetTheme_whenSuwDayNightDisabledAndGlifV2_shouldReturnLightTheme() {
147         FakePartnerContentProvider provider =
148                 Robolectric.setupContentProvider(
149                         FakePartnerContentProvider.class, "com.google.android.setupwizard.partner");
150         provider.injectFakeDayNightEnabledResult(false);
151         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V2_LIGHT);
152         Intent intent = createSetupWizardIntent();
153 
154         assertThat(SetupWizardUtils.getTheme(mContext, intent))
155                 .isEqualTo(R.style.GlifV2Theme_Light);
156     }
157 
158     @Test
testGetTheme_whenSuwDayNightEnabledAndGlifV2_shouldReturnDayNightTheme()159     public void testGetTheme_whenSuwDayNightEnabledAndGlifV2_shouldReturnDayNightTheme() {
160         FakePartnerContentProvider provider =
161                 Robolectric.setupContentProvider(
162                         FakePartnerContentProvider.class, "com.google.android.setupwizard.partner");
163         provider.injectFakeDayNightEnabledResult(true);
164         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V2_LIGHT);
165         Intent intent = createSetupWizardIntent();
166 
167         assertThat(SetupWizardUtils.getTheme(mContext, intent))
168                 .isEqualTo(R.style.GlifV2Theme_DayNight);
169     }
170 
171     @Test
testGetTheme_whenSuwDayNightDisabledAndGlifV3_shouldReturnTheme()172     public void testGetTheme_whenSuwDayNightDisabledAndGlifV3_shouldReturnTheme() {
173         FakePartnerContentProvider provider =
174                 Robolectric.setupContentProvider(
175                         FakePartnerContentProvider.class, "com.google.android.setupwizard.partner");
176         provider.injectFakeDayNightEnabledResult(false);
177         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V3);
178         Intent intent = createSetupWizardIntent();
179 
180         assertThat(SetupWizardUtils.getTheme(mContext, intent))
181                 .isEqualTo(R.style.GlifV3Theme);
182         assertThat(SetupWizardUtils.getTransparentTheme(mContext, intent))
183                 .isEqualTo(R.style.GlifV3Theme_Transparent);
184     }
185 
186     @Test
testGetTheme_whenSuwDayNightEnabledAndGlifV3_shouldReturnDayNightTheme()187     public void testGetTheme_whenSuwDayNightEnabledAndGlifV3_shouldReturnDayNightTheme() {
188         FakePartnerContentProvider provider =
189                 Robolectric.setupContentProvider(
190                         FakePartnerContentProvider.class, "com.google.android.setupwizard.partner");
191         provider.injectFakeDayNightEnabledResult(true);
192         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V3);
193         Intent intent = createSetupWizardIntent();
194 
195         assertThat(SetupWizardUtils.getTheme(mContext, intent))
196                 .isEqualTo(R.style.GlifV3Theme_DayNight);
197         assertThat(SetupWizardUtils.getTransparentTheme(mContext, intent))
198                 .isEqualTo(R.style.GlifV3Theme_DayNight_Transparent);
199     }
200 
201     @Test
testGetTheme_nonSuw_shouldReturnTheme()202     public void testGetTheme_nonSuw_shouldReturnTheme() {
203         SetupWizardProperties.theme(ThemeHelper.THEME_GLIF_V3_LIGHT);
204         Intent intent = new Intent();
205 
206         assertThat(SetupWizardUtils.getTheme(mContext, intent)).isEqualTo(R.style.GlifV3Theme);
207         assertThat(SetupWizardUtils.getTransparentTheme(mContext, intent))
208                 .isEqualTo(R.style.GlifV3Theme_Transparent);
209     }
210 
createSetupWizardIntent()211     private Intent createSetupWizardIntent() {
212         return new Intent()
213                 .putExtra(WizardManagerHelper.EXTRA_IS_SETUP_FLOW, true)
214                 .putExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, true);
215     }
216 
217     private static final class FakePartnerContentProvider extends ContentProvider {
218 
219         private final Bundle mFakeProviderDayNightEnabledResultBundle = new Bundle();
220 
221         @Override
onCreate()222         public boolean onCreate() {
223             return true;
224         }
225 
226         @Override
query( @onNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder)227         public Cursor query(
228                 @NonNull Uri uri,
229                 @Nullable String[] projection,
230                 @Nullable String selection,
231                 @Nullable String[] selectionArgs,
232                 @Nullable String sortOrder) {
233             return null;
234         }
235 
236         @Nullable
237         @Override
getType(@onNull Uri uri)238         public String getType(@NonNull Uri uri) {
239             return null;
240         }
241 
242         @Nullable
243         @Override
insert(@onNull Uri uri, @Nullable ContentValues values)244         public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
245             return null;
246         }
247 
248         @Override
delete( @onNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs)249         public int delete(
250                 @NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
251             return 0;
252         }
253 
254         @Override
update( @onNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs)255         public int update(
256                 @NonNull Uri uri,
257                 @Nullable ContentValues values,
258                 @Nullable String selection,
259                 @Nullable String[] selectionArgs) {
260             return 0;
261         }
262 
263         @Override
call(String method, String arg, Bundle extras)264         public Bundle call(String method, String arg, Bundle extras) {
265             if (TextUtils.equals(method, "isSuwDayNightEnabled")) {
266                 return mFakeProviderDayNightEnabledResultBundle;
267             }
268             return null;
269         }
270 
injectFakeDayNightEnabledResult(boolean dayNightEnabled)271         public FakePartnerContentProvider injectFakeDayNightEnabledResult(boolean dayNightEnabled) {
272             mFakeProviderDayNightEnabledResultBundle.putBoolean(
273                     "isSuwDayNightEnabled", dayNightEnabled);
274             return this;
275         }
276     }
277 }
278