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 21 import static org.robolectric.RuntimeEnvironment.application; 22 23 import android.content.Intent; 24 import android.os.Bundle; 25 import android.view.View; 26 import android.widget.Button; 27 28 import androidx.appcompat.app.AlertDialog; 29 30 import com.android.settings.R; 31 import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment; 32 import com.android.settings.password.ChooseLockPassword.ChooseLockPasswordFragment.Stage; 33 import com.android.settings.password.ChooseLockPassword.IntentBuilder; 34 import com.android.settings.password.SetupChooseLockPassword.SetupChooseLockPasswordFragment; 35 import com.android.settings.testutils.shadow.SettingsShadowResources; 36 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; 37 import com.android.settings.testutils.shadow.ShadowLockPatternUtils; 38 import com.android.settings.testutils.shadow.ShadowUtils; 39 import com.android.settings.widget.ScrollToParentEditText; 40 41 import com.google.android.setupcompat.PartnerCustomizationLayout; 42 import com.google.android.setupcompat.template.FooterBarMixin; 43 44 import org.junit.After; 45 import org.junit.Before; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.robolectric.RobolectricTestRunner; 49 import org.robolectric.Shadows; 50 import org.robolectric.android.controller.ActivityController; 51 import org.robolectric.annotation.Config; 52 import org.robolectric.annotation.Implementation; 53 import org.robolectric.annotation.Implements; 54 import org.robolectric.shadows.ShadowActivity; 55 import org.robolectric.shadows.ShadowDialog; 56 57 import java.util.Collections; 58 import java.util.List; 59 60 @RunWith(RobolectricTestRunner.class) 61 @Config( 62 shadows = { 63 SettingsShadowResources.class, 64 ShadowLockPatternUtils.class, 65 ShadowUtils.class, 66 ShadowAlertDialogCompat.class 67 }) 68 public class SetupChooseLockPasswordTest { 69 70 @Before setUp()71 public void setUp() { 72 SettingsShadowResources.overrideResource( 73 com.android.internal.R.string.config_headlineFontFamily, ""); 74 } 75 76 @After tearDown()77 public void tearDown() { 78 SettingsShadowResources.reset(); 79 } 80 81 @Test createActivity_shouldNotCrash()82 public void createActivity_shouldNotCrash() { 83 // Basic sanity test for activity created without crashing 84 final Intent intent = 85 SetupChooseLockPassword.modifyIntentForSetup( 86 application, 87 new IntentBuilder(application).build()); 88 89 ActivityController.of(new SetupChooseLockPassword(), intent).setup().get(); 90 } 91 92 @Test createActivity_withShowOptionsButtonExtra_shouldShowButton()93 public void createActivity_withShowOptionsButtonExtra_shouldShowButton() { 94 SetupChooseLockPassword activity = createSetupChooseLockPassword(); 95 Button optionsButton = activity.findViewById(R.id.screen_lock_options); 96 assertThat(optionsButton).isNotNull(); 97 optionsButton.performClick(); 98 assertThat(ShadowDialog.getLatestDialog()).isNotNull(); 99 } 100 101 @Test 102 @Config(shadows = ShadowChooseLockGenericController.class) createActivity_withShowOptionsButtonExtra_buttonNotVisibleIfNoVisibleLockTypes()103 public void createActivity_withShowOptionsButtonExtra_buttonNotVisibleIfNoVisibleLockTypes() { 104 SetupChooseLockPassword activity = createSetupChooseLockPassword(); 105 Button optionsButton = activity.findViewById(R.id.screen_lock_options); 106 assertThat(optionsButton).isNotNull(); 107 assertThat(optionsButton.getVisibility()).isEqualTo(View.GONE); 108 } 109 110 @Test allSecurityOptions_shouldBeShown_When_OptionsButtonIsClicked()111 public void allSecurityOptions_shouldBeShown_When_OptionsButtonIsClicked() { 112 SetupChooseLockPassword activity = createSetupChooseLockPassword(); 113 activity.findViewById(R.id.screen_lock_options).performClick(); 114 AlertDialog latestAlertDialog = (AlertDialog) ShadowDialog.getLatestDialog(); 115 int count = latestAlertDialog.getListView().getCount(); 116 assertThat(count).named("List items shown").isEqualTo(3); 117 } 118 119 @Test createActivity_clickDifferentOption_extrasShouldBePropagated()120 public void createActivity_clickDifferentOption_extrasShouldBePropagated() { 121 Bundle bundle = new Bundle(); 122 bundle.putString("foo", "bar"); 123 124 Intent intent = new IntentBuilder(application).build(); 125 intent.putExtra(ChooseLockGenericFragment.EXTRA_CHOOSE_LOCK_GENERIC_EXTRAS, bundle); 126 intent = SetupChooseLockPassword.modifyIntentForSetup(application, intent); 127 intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true); 128 129 SetupChooseLockPassword activity = 130 ActivityController.of(new SetupChooseLockPassword(), intent).setup().get(); 131 132 SetupChooseLockPasswordFragment fragment = 133 (SetupChooseLockPasswordFragment) activity.getSupportFragmentManager() 134 .findFragmentById(R.id.main_content); 135 fragment.onLockTypeSelected(ScreenLockType.PATTERN); 136 137 ShadowActivity shadowActivity = Shadows.shadowOf(activity); 138 final Intent nextStartedActivity = shadowActivity.getNextStartedActivity(); 139 assertThat(nextStartedActivity).isNotNull(); 140 assertThat(nextStartedActivity.getBooleanExtra( 141 ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, false)).isTrue(); 142 assertThat(nextStartedActivity.getStringExtra("foo")).named("Foo extra") 143 .isEqualTo("bar"); 144 } 145 146 @Test createActivity_skipButtonInIntroductionStage_shouldBeVisible()147 public void createActivity_skipButtonInIntroductionStage_shouldBeVisible() { 148 SetupChooseLockPassword activity = createSetupChooseLockPassword(); 149 150 final PartnerCustomizationLayout layout = activity.findViewById(R.id.setup_wizard_layout); 151 final Button skipOrClearButton = 152 layout.getMixin(FooterBarMixin.class).getSecondaryButtonView(); 153 assertThat(skipOrClearButton).isNotNull(); 154 assertThat(skipOrClearButton.getVisibility()).isEqualTo(View.VISIBLE); 155 156 skipOrClearButton.performClick(); 157 final AlertDialog chooserDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); 158 assertThat(chooserDialog).isNotNull(); 159 } 160 161 @Test createActivity_inputPasswordInConfirmStage_clearButtonShouldBeShown()162 public void createActivity_inputPasswordInConfirmStage_clearButtonShouldBeShown() { 163 SetupChooseLockPassword activity = createSetupChooseLockPassword(); 164 165 SetupChooseLockPasswordFragment fragment = 166 (SetupChooseLockPasswordFragment) activity.getSupportFragmentManager() 167 .findFragmentById(R.id.main_content); 168 169 ScrollToParentEditText passwordEntry = activity.findViewById(R.id.password_entry); 170 passwordEntry.setText(""); 171 fragment.updateStage(Stage.NeedToConfirm); 172 173 final PartnerCustomizationLayout layout = activity.findViewById(R.id.setup_wizard_layout); 174 final Button skipOrClearButton = 175 layout.getMixin(FooterBarMixin.class).getSecondaryButtonView(); 176 assertThat(skipOrClearButton.isEnabled()).isTrue(); 177 assertThat(skipOrClearButton.getVisibility()).isEqualTo(View.GONE); 178 179 passwordEntry.setText("1234"); 180 fragment.updateUi(); 181 assertThat(skipOrClearButton.getVisibility()).isEqualTo(View.VISIBLE); 182 assertThat(skipOrClearButton.getText()) 183 .isEqualTo(application.getString(R.string.lockpassword_clear_label)); 184 } 185 createSetupChooseLockPassword()186 private SetupChooseLockPassword createSetupChooseLockPassword() { 187 final Intent intent = 188 SetupChooseLockPassword.modifyIntentForSetup( 189 application, 190 new IntentBuilder(application).build()); 191 intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true); 192 return ActivityController.of(new SetupChooseLockPassword(), intent).setup().get(); 193 } 194 195 @Implements(ChooseLockGenericController.class) 196 public static class ShadowChooseLockGenericController { 197 @Implementation getVisibleScreenLockTypes(int quality, boolean includeDisabled)198 protected List<ScreenLockType> getVisibleScreenLockTypes(int quality, 199 boolean includeDisabled) { 200 return Collections.emptyList(); 201 } 202 } 203 } 204