1 /* 2 * Copyright (C) 2017 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.password; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.robolectric.RuntimeEnvironment.application; 21 import static org.robolectric.Shadows.shadowOf; 22 23 import android.app.AlertDialog; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.widget.Button; 28 29 import com.android.settings.R; 30 import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment; 31 import com.android.settings.password.ChooseLockPassword.IntentBuilder; 32 import com.android.settings.password.SetupChooseLockPassword.SetupChooseLockPasswordFragment; 33 import com.android.settings.testutils.SettingsRobolectricTestRunner; 34 import com.android.settings.testutils.shadow.SettingsShadowResources; 35 import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl; 36 import com.android.settings.testutils.shadow.ShadowUtils; 37 38 import org.junit.After; 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.robolectric.Robolectric; 43 import org.robolectric.Shadows; 44 import org.robolectric.annotation.Config; 45 import org.robolectric.annotation.Implementation; 46 import org.robolectric.annotation.Implements; 47 import org.robolectric.shadows.ShadowActivity; 48 import org.robolectric.shadows.ShadowAlertDialog; 49 import org.robolectric.shadows.ShadowDialog; 50 51 import java.util.Collections; 52 import java.util.List; 53 54 @RunWith(SettingsRobolectricTestRunner.class) 55 @Config(shadows = { 56 SettingsShadowResources.class, 57 SettingsShadowResourcesImpl.class, 58 SettingsShadowResources.SettingsShadowTheme.class, 59 ShadowUtils.class 60 }) 61 public class SetupChooseLockPasswordTest { 62 63 @Before setUp()64 public void setUp() throws Exception { 65 SettingsShadowResources.overrideResource( 66 com.android.internal.R.string.config_headlineFontFamily, ""); 67 } 68 69 @After tearDown()70 public void tearDown() { 71 SettingsShadowResources.reset(); 72 } 73 74 @Test createActivity_shouldNotCrash()75 public void createActivity_shouldNotCrash() { 76 // Basic sanity test for activity created without crashing 77 Robolectric.buildActivity(SetupChooseLockPassword.class, 78 SetupChooseLockPassword.modifyIntentForSetup( 79 application, 80 new IntentBuilder(application).build())) 81 .setup().get(); 82 } 83 84 @Test createActivity_withShowOptionsButtonExtra_shouldShowButton()85 public void createActivity_withShowOptionsButtonExtra_shouldShowButton() { 86 SetupChooseLockPassword activity = createSetupChooseLockPassword(); 87 Button optionsButton = activity.findViewById(R.id.screen_lock_options); 88 assertThat(optionsButton).isNotNull(); 89 optionsButton.performClick(); 90 assertThat(ShadowDialog.getLatestDialog()).isNotNull(); 91 } 92 93 @Test 94 @Config(shadows = ShadowChooseLockGenericController.class) createActivity_withShowOptionsButtonExtra_buttonNotVisibleIfNoVisibleLockTypes()95 public void createActivity_withShowOptionsButtonExtra_buttonNotVisibleIfNoVisibleLockTypes() { 96 SetupChooseLockPassword activity = createSetupChooseLockPassword(); 97 Button optionsButton = activity.findViewById(R.id.screen_lock_options); 98 assertThat(optionsButton).isNotNull(); 99 assertThat(optionsButton.getVisibility()).isEqualTo(View.GONE); 100 } 101 102 @Test allSecurityOptions_shouldBeShown_When_OptionsButtonIsClicked()103 public void allSecurityOptions_shouldBeShown_When_OptionsButtonIsClicked() { 104 SetupChooseLockPassword activity = createSetupChooseLockPassword(); 105 activity.findViewById(R.id.screen_lock_options).performClick(); 106 AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog(); 107 int count = Shadows.shadowOf(latestAlertDialog).getAdapter().getCount(); 108 assertThat(count).named("List items shown").isEqualTo(3); 109 } 110 111 @Test createActivity_clickDifferentOption_extrasShouldBePropagated()112 public void createActivity_clickDifferentOption_extrasShouldBePropagated() { 113 Bundle bundle = new Bundle(); 114 bundle.putString("foo", "bar"); 115 116 Intent intent = new IntentBuilder(application).build(); 117 intent.putExtra(ChooseLockGenericFragment.EXTRA_CHOOSE_LOCK_GENERIC_EXTRAS, bundle); 118 intent = SetupChooseLockPassword.modifyIntentForSetup(application, intent); 119 intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true); 120 121 SetupChooseLockPassword activity = 122 Robolectric.buildActivity(SetupChooseLockPassword.class, intent).setup().get(); 123 124 SetupChooseLockPasswordFragment fragment = 125 (SetupChooseLockPasswordFragment) activity.getFragmentManager() 126 .findFragmentById(R.id.main_content); 127 fragment.onLockTypeSelected(ScreenLockType.PATTERN); 128 129 ShadowActivity shadowActivity = shadowOf(activity); 130 final Intent nextStartedActivity = shadowActivity.getNextStartedActivity(); 131 assertThat(nextStartedActivity).isNotNull(); 132 assertThat(nextStartedActivity.getBooleanExtra( 133 ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, false)).isTrue(); 134 assertThat(nextStartedActivity.getStringExtra("foo")).named("Foo extra") 135 .isEqualTo("bar"); 136 } 137 createSetupChooseLockPassword()138 private SetupChooseLockPassword createSetupChooseLockPassword() { 139 Intent intent = SetupChooseLockPassword.modifyIntentForSetup( 140 application, 141 new IntentBuilder(application).build()); 142 intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true); 143 return Robolectric.buildActivity(SetupChooseLockPassword.class, intent).setup().get(); 144 } 145 146 @Implements(ChooseLockGenericController.class) 147 public static class ShadowChooseLockGenericController { 148 @Implementation getVisibleScreenLockTypes(int quality, boolean includeDisabled)149 public List<ScreenLockType> getVisibleScreenLockTypes(int quality, 150 boolean includeDisabled) { 151 return Collections.emptyList(); 152 } 153 } 154 } 155