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