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.fingerprint; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.robolectric.RuntimeEnvironment.application; 21 22 import android.app.KeyguardManager; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.content.pm.UserInfo; 26 import android.view.View; 27 import android.widget.Button; 28 29 import com.android.settings.R; 30 import com.android.settings.fingerprint.SetupFingerprintEnrollIntroductionTest.ShadowStorageManagerWrapper; 31 import com.android.settings.password.SetupChooseLockGeneric.SetupChooseLockGenericFragment; 32 import com.android.settings.password.SetupSkipDialog; 33 import com.android.settings.password.StorageManagerWrapper; 34 import com.android.settings.testutils.FakeFeatureFactory; 35 import com.android.settings.testutils.SettingsRobolectricTestRunner; 36 import com.android.settings.testutils.shadow.ShadowFingerprintManager; 37 import com.android.settings.testutils.shadow.ShadowLockPatternUtils; 38 import com.android.settings.testutils.shadow.ShadowUserManager; 39 40 import org.junit.After; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.Robolectric; 47 import org.robolectric.Shadows; 48 import org.robolectric.android.controller.ActivityController; 49 import org.robolectric.annotation.Config; 50 import org.robolectric.annotation.Implementation; 51 import org.robolectric.annotation.Implements; 52 import org.robolectric.shadows.ShadowActivity; 53 import org.robolectric.shadows.ShadowActivity.IntentForResult; 54 import org.robolectric.shadows.ShadowKeyguardManager; 55 56 @RunWith(SettingsRobolectricTestRunner.class) 57 @Config(shadows = { 58 ShadowFingerprintManager.class, 59 ShadowLockPatternUtils.class, 60 ShadowStorageManagerWrapper.class, 61 ShadowUserManager.class 62 }) 63 public class SetupFingerprintEnrollIntroductionTest { 64 65 @Mock 66 private UserInfo mUserInfo; 67 68 private ActivityController<SetupFingerprintEnrollIntroduction> mController; 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 74 Shadows.shadowOf(application.getPackageManager()) 75 .setSystemFeature(PackageManager.FEATURE_FINGERPRINT, true); 76 ShadowFingerprintManager.addToServiceMap(); 77 78 FakeFeatureFactory.setupForTest(); 79 80 final Intent intent = new Intent(); 81 mController = Robolectric.buildActivity(SetupFingerprintEnrollIntroduction.class, intent); 82 83 ShadowUserManager.getShadow().setUserInfo(0, mUserInfo); 84 } 85 86 @After tearDown()87 public void tearDown() { 88 ShadowStorageManagerWrapper.reset(); 89 ShadowFingerprintManager.reset(); 90 } 91 92 @Test testKeyguardNotSecure_shouldFinishWithSetupSkipDialogResultSkip()93 public void testKeyguardNotSecure_shouldFinishWithSetupSkipDialogResultSkip() { 94 getShadowKeyguardManager().setIsKeyguardSecure(false); 95 96 mController.create().resume(); 97 98 final Button skipButton = mController.get().findViewById(R.id.fingerprint_cancel_button); 99 assertThat(skipButton.getVisibility()).named("Skip visible").isEqualTo(View.VISIBLE); 100 skipButton.performClick(); 101 102 ShadowActivity shadowActivity = Shadows.shadowOf(mController.get()); 103 assertThat(mController.get().isFinishing()).named("Is finishing").isTrue(); 104 assertThat(shadowActivity.getResultCode()).named("Result code") 105 .isEqualTo(SetupSkipDialog.RESULT_SKIP); 106 } 107 108 @Test testKeyguardSecure_shouldFinishWithFingerprintResultSkip()109 public void testKeyguardSecure_shouldFinishWithFingerprintResultSkip() { 110 getShadowKeyguardManager().setIsKeyguardSecure(true); 111 112 mController.create().resume(); 113 114 final Button skipButton = mController.get().findViewById(R.id.fingerprint_cancel_button); 115 assertThat(skipButton.getVisibility()).named("Skip visible").isEqualTo(View.VISIBLE); 116 skipButton.performClick(); 117 118 ShadowActivity shadowActivity = Shadows.shadowOf(mController.get()); 119 assertThat(mController.get().isFinishing()).named("Is finishing").isTrue(); 120 assertThat(shadowActivity.getResultCode()).named("Result code") 121 .isEqualTo(FingerprintEnrollBase.RESULT_SKIP); 122 } 123 124 @Test testBackKeyPress_shouldSetIntentDataIfLockScreenAdded()125 public void testBackKeyPress_shouldSetIntentDataIfLockScreenAdded() { 126 getShadowKeyguardManager().setIsKeyguardSecure(false); 127 128 mController.create().resume(); 129 getShadowKeyguardManager().setIsKeyguardSecure(true); 130 SetupFingerprintEnrollIntroduction activity = mController.get(); 131 activity.onBackPressed(); 132 133 ShadowActivity shadowActivity = Shadows.shadowOf(activity); 134 assertThat(shadowActivity.getResultIntent()).isNotNull(); 135 assertThat(shadowActivity.getResultIntent().hasExtra( 136 SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isTrue(); 137 } 138 139 @Test testBackKeyPress_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch()140 public void testBackKeyPress_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch() { 141 getShadowKeyguardManager().setIsKeyguardSecure(true); 142 143 mController.create().resume(); 144 SetupFingerprintEnrollIntroduction activity = mController.get(); 145 activity.onBackPressed(); 146 147 ShadowActivity shadowActivity = Shadows.shadowOf(activity); 148 assertThat(shadowActivity.getResultIntent()).isNull(); 149 } 150 151 @Test testCancelClicked_shouldSetIntentDataIfLockScreenAdded()152 public void testCancelClicked_shouldSetIntentDataIfLockScreenAdded() { 153 getShadowKeyguardManager().setIsKeyguardSecure(false); 154 155 SetupFingerprintEnrollIntroduction activity = mController.create().resume().get(); 156 final Button skipButton = activity.findViewById(R.id.fingerprint_cancel_button); 157 getShadowKeyguardManager().setIsKeyguardSecure(true); 158 skipButton.performClick(); 159 160 ShadowActivity shadowActivity = Shadows.shadowOf(activity); 161 assertThat(shadowActivity.getResultIntent()).isNotNull(); 162 assertThat(shadowActivity.getResultIntent().hasExtra( 163 SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isTrue(); 164 } 165 166 @Test testCancelClicked_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch()167 public void testCancelClicked_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch() { 168 getShadowKeyguardManager().setIsKeyguardSecure(true); 169 170 SetupFingerprintEnrollIntroduction activity = mController.create().resume().get(); 171 final Button skipButton = activity.findViewById(R.id.fingerprint_cancel_button); 172 skipButton.performClick(); 173 174 ShadowActivity shadowActivity = Shadows.shadowOf(activity); 175 assertThat(shadowActivity.getResultIntent()).isNull(); 176 } 177 178 @Test testOnResultFromFindSensor_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch()179 public void testOnResultFromFindSensor_shouldNotSetIntentDataIfLockScreenPresentBeforeLaunch() { 180 getShadowKeyguardManager().setIsKeyguardSecure(true); 181 SetupFingerprintEnrollIntroduction activity = mController.create().resume().get(); 182 activity.onActivityResult(FingerprintEnrollIntroduction.FINGERPRINT_FIND_SENSOR_REQUEST, 183 FingerprintEnrollBase.RESULT_FINISHED, null); 184 assertThat(Shadows.shadowOf(activity).getResultIntent()).isNull(); 185 } 186 187 @Test testOnResultFromFindSensor_shouldSetIntentDataIfLockScreenAdded()188 public void testOnResultFromFindSensor_shouldSetIntentDataIfLockScreenAdded() { 189 getShadowKeyguardManager().setIsKeyguardSecure(false); 190 SetupFingerprintEnrollIntroduction activity = mController.create().resume().get(); 191 getShadowKeyguardManager().setIsKeyguardSecure(true); 192 activity.onActivityResult(FingerprintEnrollIntroduction.FINGERPRINT_FIND_SENSOR_REQUEST, 193 FingerprintEnrollBase.RESULT_FINISHED, null); 194 assertThat(Shadows.shadowOf(activity).getResultIntent()).isNotNull(); 195 } 196 197 @Test testOnResultFromFindSensor_shouldNotSetIntentDataIfLockScreenNotAdded()198 public void testOnResultFromFindSensor_shouldNotSetIntentDataIfLockScreenNotAdded() { 199 getShadowKeyguardManager().setIsKeyguardSecure(false); 200 SetupFingerprintEnrollIntroduction activity = mController.create().resume().get(); 201 activity.onActivityResult(FingerprintEnrollIntroduction.FINGERPRINT_FIND_SENSOR_REQUEST, 202 FingerprintEnrollBase.RESULT_FINISHED, null); 203 assertThat(Shadows.shadowOf(activity).getResultIntent()).isNull(); 204 } 205 206 @Test testLockPattern()207 public void testLockPattern() { 208 ShadowStorageManagerWrapper.sIsFileEncrypted = false; 209 210 mController.create().postCreate(null).resume(); 211 212 SetupFingerprintEnrollIntroduction activity = mController.get(); 213 214 final Button nextButton = activity.findViewById(R.id.fingerprint_next_button); 215 nextButton.performClick(); 216 217 ShadowActivity shadowActivity = Shadows.shadowOf(activity); 218 IntentForResult startedActivity = shadowActivity.getNextStartedActivityForResult(); 219 assertThat(startedActivity).isNotNull(); 220 assertThat(startedActivity.intent.hasExtra( 221 SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isFalse(); 222 } 223 getShadowKeyguardManager()224 private ShadowKeyguardManager getShadowKeyguardManager() { 225 return Shadows.shadowOf(application.getSystemService(KeyguardManager.class)); 226 } 227 228 @Implements(StorageManagerWrapper.class) 229 public static class ShadowStorageManagerWrapper { 230 231 private static boolean sIsFileEncrypted = true; 232 reset()233 public static void reset() { 234 sIsFileEncrypted = true; 235 } 236 237 @Implementation isFileEncryptedNativeOrEmulated()238 public static boolean isFileEncryptedNativeOrEmulated() { 239 return sIsFileEncrypted; 240 } 241 } 242 } 243