WizardManagerHelperTest.java revision 364d9cd8b8274709e46414b1d1bf6d91ec97160c
1/*
2 * Copyright (C) 2015 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
17package com.android.setupwizardlib.util;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
22import static org.robolectric.RuntimeEnvironment.application;
23
24import android.annotation.TargetApi;
25import android.app.Activity;
26import android.content.Intent;
27import android.os.Build.VERSION_CODES;
28import android.os.Bundle;
29import android.provider.Settings;
30import android.provider.Settings.Global;
31import android.provider.Settings.Secure;
32import android.support.annotation.StyleRes;
33
34import com.android.setupwizardlib.BuildConfig;
35import com.android.setupwizardlib.R;
36import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
37
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.robolectric.annotation.Config;
41
42@RunWith(SuwLibRobolectricTestRunner.class)
43@Config(constants = BuildConfig.class, sdk = Config.NEWEST_SDK)
44public class WizardManagerHelperTest {
45
46    @Test
47    public void testGetNextIntent() {
48        final Intent intent = new Intent("test.intent.ACTION");
49        intent.putExtra("scriptUri", "android-resource://test-script");
50        intent.putExtra("actionId", "test_action_id");
51        intent.putExtra("theme", "test_theme");
52        intent.putExtra("ignoreExtra", "poof"); // extra is ignored because it's not known
53
54        final Intent data = new Intent();
55        data.putExtra("extraData", "shazam");
56
57        final Intent nextIntent =
58                WizardManagerHelper.getNextIntent(intent, Activity.RESULT_OK, data);
59        assertEquals("Next intent action should be NEXT", "com.android.wizard.NEXT",
60                nextIntent.getAction());
61        assertEquals("Script URI should be the same as original intent",
62                "android-resource://test-script", nextIntent.getStringExtra("scriptUri"));
63        assertEquals("Action ID should be the same as original intent", "test_action_id",
64                nextIntent.getStringExtra("actionId"));
65        assertEquals("Theme extra should be the same as original intent", "test_theme",
66                nextIntent.getStringExtra("theme"));
67        assertFalse("ignoreExtra should not be in nextIntent", nextIntent.hasExtra("ignoreExtra"));
68        assertEquals("Result code extra should be RESULT_OK", Activity.RESULT_OK,
69                nextIntent.getIntExtra("com.android.setupwizard.ResultCode", 0));
70        assertEquals("Extra data should surface as extra in nextIntent", "shazam",
71                nextIntent.getStringExtra("extraData"));
72    }
73
74    @Test
75    public void testIsSetupWizardTrue() {
76        final Intent intent = new Intent();
77        intent.putExtra("firstRun", true);
78        assertTrue("Is setup wizard should be true",
79                WizardManagerHelper.isSetupWizardIntent(intent));
80    }
81
82    @Test
83    public void testIsDeferredSetupTrue() {
84        final Intent intent = new Intent();
85        intent.putExtra("deferredSetup", true);
86        assertTrue("Is deferred setup wizard should be true",
87                WizardManagerHelper.isDeferredSetupWizard(intent));
88    }
89
90    @Test
91    public void testIsSetupWizardFalse() {
92        final Intent intent = new Intent();
93        intent.putExtra("firstRun", false);
94        assertFalse("Is setup wizard should be true",
95                WizardManagerHelper.isSetupWizardIntent(intent));
96    }
97
98    @Test
99    public void testHoloIsNotLightTheme() {
100        final Intent intent = new Intent();
101        intent.putExtra("theme", "holo");
102        assertFalse("Theme holo should not be light theme",
103                WizardManagerHelper.isLightTheme(intent, true));
104    }
105
106    @Test
107    public void testHoloLightIsLightTheme() {
108        final Intent intent = new Intent();
109        intent.putExtra("theme", "holo_light");
110        assertTrue("Theme holo_light should be light theme",
111                WizardManagerHelper.isLightTheme(intent, false));
112    }
113
114    @Test
115    public void testMaterialIsNotLightTheme() {
116        final Intent intent = new Intent();
117        intent.putExtra("theme", "material");
118        assertFalse("Theme material should not be light theme",
119                WizardManagerHelper.isLightTheme(intent, true));
120    }
121
122    @Test
123    public void testMaterialLightIsLightTheme() {
124        final Intent intent = new Intent();
125        intent.putExtra("theme", "material_light");
126        assertTrue("Theme material_light should be light theme",
127                WizardManagerHelper.isLightTheme(intent, false));
128    }
129
130    @Test
131    public void testGlifIsDarkTheme() {
132        final Intent intent = new Intent();
133        intent.putExtra("theme", "glif");
134        assertFalse("Theme glif should be dark theme",
135                WizardManagerHelper.isLightTheme(intent, false));
136        assertFalse("Theme glif should be dark theme",
137                WizardManagerHelper.isLightTheme(intent, true));
138    }
139
140    @Test
141    public void testGlifLightIsLightTheme() {
142        final Intent intent = new Intent();
143        intent.putExtra("theme", "glif_light");
144        assertTrue("Theme glif_light should be light theme",
145                WizardManagerHelper.isLightTheme(intent, false));
146        assertTrue("Theme glif_light should be light theme",
147                WizardManagerHelper.isLightTheme(intent, true));
148    }
149
150    @Test
151    public void testGlifV2IsDarkTheme() {
152        final Intent intent = new Intent();
153        intent.putExtra("theme", "glif_v2");
154        assertFalse("Theme glif_v2 should be dark theme",
155                WizardManagerHelper.isLightTheme(intent, false));
156        assertFalse("Theme glif_v2 should be dark theme",
157                WizardManagerHelper.isLightTheme(intent, true));
158    }
159
160    @Test
161    public void testGlifV2LightIsLightTheme() {
162        final Intent intent = new Intent();
163        intent.putExtra("theme", "glif_v2_light");
164        assertTrue("Theme glif_v2_light should be light theme",
165                WizardManagerHelper.isLightTheme(intent, false));
166        assertTrue("Theme glif_v2_light should be light theme",
167                WizardManagerHelper.isLightTheme(intent, true));
168    }
169
170    @Test
171    public void testIsLightThemeDefault() {
172        final Intent intent = new Intent();
173        intent.putExtra("theme", "abracadabra");
174        assertTrue("isLightTheme should return default value true",
175                WizardManagerHelper.isLightTheme(intent, true));
176        assertFalse("isLightTheme should return default value false",
177                WizardManagerHelper.isLightTheme(intent, false));
178    }
179
180    @Test
181    public void testIsLightThemeUnspecified() {
182        final Intent intent = new Intent();
183        assertTrue("isLightTheme should return default value true",
184                WizardManagerHelper.isLightTheme(intent, true));
185        assertFalse("isLightTheme should return default value false",
186                WizardManagerHelper.isLightTheme(intent, false));
187    }
188
189    @Test
190    public void testIsLightThemeString() {
191        assertTrue("isLightTheme should return true for material_light",
192                WizardManagerHelper.isLightTheme("material_light", false));
193        assertFalse("isLightTheme should return false for material",
194                WizardManagerHelper.isLightTheme("material", false));
195        assertTrue("isLightTheme should return true for holo_light",
196                WizardManagerHelper.isLightTheme("holo_light", false));
197        assertFalse("isLightTheme should return false for holo",
198                WizardManagerHelper.isLightTheme("holo", false));
199        assertTrue("isLightTheme should return default value true",
200                WizardManagerHelper.isLightTheme("abracadabra", true));
201        assertFalse("isLightTheme should return default value false",
202                WizardManagerHelper.isLightTheme("abracadabra", false));
203    }
204
205    @Test
206    public void testGetThemeResGlifV2Light() {
207        assertEquals(R.style.SuwThemeGlifV2_Light,
208                WizardManagerHelper.getThemeRes("glif_v2_light", 0));
209    }
210
211    @Test
212    public void testGetThemeResGlifV2() {
213        assertEquals(R.style.SuwThemeGlifV2,
214                WizardManagerHelper.getThemeRes("glif_v2", 0));
215    }
216
217    @Test
218    public void testGetThemeResGlifLight() {
219        assertEquals(R.style.SuwThemeGlif_Light,
220                WizardManagerHelper.getThemeRes("glif_light", 0));
221    }
222
223    @Test
224    public void testGetThemeResGlif() {
225        assertEquals(R.style.SuwThemeGlif,
226                WizardManagerHelper.getThemeRes("glif", 0));
227    }
228
229    @Test
230    public void testGetThemeResMaterialLight() {
231        assertEquals(R.style.SuwThemeMaterial_Light,
232                WizardManagerHelper.getThemeRes("material_light", 0));
233    }
234
235    @Test
236    public void testGetThemeResMaterial() {
237        assertEquals(R.style.SuwThemeMaterial,
238                WizardManagerHelper.getThemeRes("material", 0));
239    }
240
241    @Test
242    public void testGetThemeResDefault() {
243        @StyleRes int def = 123;
244        assertEquals(def, WizardManagerHelper.getThemeRes("abracadabra", def));
245    }
246
247    @Test
248    public void testGetThemeResNull() {
249        @StyleRes int def = 123;
250        assertEquals(def, WizardManagerHelper.getThemeRes((String) null, def));
251    }
252
253    @Test
254    public void testGetThemeResFromIntent() {
255        Intent intent = new Intent();
256        intent.putExtra(WizardManagerHelper.EXTRA_THEME, "material");
257        assertEquals(R.style.SuwThemeMaterial, WizardManagerHelper.getThemeRes(intent, 0));
258    }
259
260    @Test
261    public void testCopyWizardManagerIntent() {
262        Bundle wizardBundle = new Bundle();
263        wizardBundle.putString("foo", "bar");
264        Intent originalIntent = new Intent()
265                .putExtra(WizardManagerHelper.EXTRA_THEME, "test_theme")
266                .putExtra(WizardManagerHelper.EXTRA_WIZARD_BUNDLE, wizardBundle)
267                .putExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, true)
268                .putExtra(WizardManagerHelper.EXTRA_IS_DEFERRED_SETUP, true)
269                // Script URI and Action ID are kept for backwards compatibility
270                .putExtra(WizardManagerHelper.EXTRA_SCRIPT_URI, "test_script_uri")
271                .putExtra(WizardManagerHelper.EXTRA_ACTION_ID, "test_action_id");
272
273        Intent intent = new Intent("test.intent.action");
274        WizardManagerHelper.copyWizardManagerExtras(originalIntent, intent);
275
276        assertEquals("Intent action should be kept", "test.intent.action", intent.getAction());
277        assertEquals("EXTRA_THEME should be copied",
278                "test_theme", intent.getStringExtra(WizardManagerHelper.EXTRA_THEME));
279        Bundle copiedWizardBundle =
280                intent.getParcelableExtra(WizardManagerHelper.EXTRA_WIZARD_BUNDLE);
281        assertEquals("Wizard bundle should be copied", "bar", copiedWizardBundle.getString("foo"));
282
283        assertTrue("EXTRA_IS_FIRST_RUN should be copied",
284                intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_FIRST_RUN, false));
285        assertTrue("EXTRA_IS_DEFERRED_SETUP should be copied",
286                intent.getBooleanExtra(WizardManagerHelper.EXTRA_IS_DEFERRED_SETUP, false));
287
288        // Script URI and Action ID are replaced by Wizard Bundle in M, but are kept for backwards
289        // compatibility
290        assertEquals("EXTRA_SCRIPT_URI should be copied",
291                "test_script_uri", intent.getStringExtra(WizardManagerHelper.EXTRA_SCRIPT_URI));
292        assertEquals("EXTRA_ACTION_ID should be copied",
293                "test_action_id", intent.getStringExtra(WizardManagerHelper.EXTRA_ACTION_ID));
294    }
295
296    @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
297    @Test
298    public void testIsUserSetupComplete() {
299        Settings.Secure.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
300        Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 1);
301        assertTrue(WizardManagerHelper.isUserSetupComplete(application));
302
303        Settings.Secure.putInt(application.getContentResolver(), "user_setup_complete", 0);
304        assertFalse(WizardManagerHelper.isUserSetupComplete(application));
305    }
306
307    @Test
308    @Config(sdk = VERSION_CODES.JELLY_BEAN)
309    public void testIsUserSetupCompleteCompat() {
310        Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1);
311        assertTrue(WizardManagerHelper.isUserSetupComplete(application));
312
313        Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0);
314        assertFalse(WizardManagerHelper.isUserSetupComplete(application));
315    }
316
317    @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
318    @Test
319    public void testIsDeviceProvisioned() {
320        Settings.Secure.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
321        assertTrue(WizardManagerHelper.isDeviceProvisioned(application));
322        Settings.Secure.putInt(application.getContentResolver(), Global.DEVICE_PROVISIONED, 0);
323        assertFalse(WizardManagerHelper.isDeviceProvisioned(application));
324    }
325
326    @Test
327    @Config(sdk = VERSION_CODES.JELLY_BEAN)
328    public void testIsDeviceProvisionedCompat() {
329        Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 1);
330        assertTrue(WizardManagerHelper.isDeviceProvisioned(application));
331        Settings.Secure.putInt(application.getContentResolver(), Secure.DEVICE_PROVISIONED, 0);
332        assertFalse(WizardManagerHelper.isDeviceProvisioned(application));
333    }
334}
335