1 /* 2 * Copyright (C) 2020 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.keyguard; 18 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.ArgumentMatchers.anyInt; 21 import static org.mockito.ArgumentMatchers.eq; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.reset; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.verifyZeroInteractions; 27 import static org.mockito.Mockito.when; 28 29 import android.os.SystemClock; 30 import android.platform.test.annotations.EnableFlags; 31 import android.testing.AndroidTestingRunner; 32 import android.testing.TestableLooper.RunWithLooper; 33 import android.view.KeyEvent; 34 35 import androidx.test.filters.SmallTest; 36 37 import com.android.internal.util.LatencyTracker; 38 import com.android.internal.widget.LockPatternUtils; 39 import com.android.keyguard.KeyguardAbsKeyInputView.KeyDownListener; 40 import com.android.keyguard.KeyguardSecurityModel.SecurityMode; 41 import com.android.systemui.Flags; 42 import com.android.systemui.SysuiTestCase; 43 import com.android.systemui.classifier.FalsingCollector; 44 import com.android.systemui.classifier.FalsingCollectorFake; 45 import com.android.systemui.flags.FakeFeatureFlags; 46 import com.android.systemui.res.R; 47 import com.android.systemui.user.domain.interactor.SelectedUserInteractor; 48 49 import org.junit.Before; 50 import org.junit.Test; 51 import org.junit.runner.RunWith; 52 import org.mockito.ArgumentCaptor; 53 import org.mockito.Mock; 54 import org.mockito.MockitoAnnotations; 55 56 @SmallTest 57 @RunWith(AndroidTestingRunner.class) 58 @RunWithLooper 59 public class KeyguardAbsKeyInputViewControllerTest extends SysuiTestCase { 60 61 @Mock 62 private KeyguardAbsKeyInputView mAbsKeyInputView; 63 @Mock 64 private PasswordTextView mPasswordEntry; 65 @Mock 66 private BouncerKeyguardMessageArea mKeyguardMessageArea; 67 @Mock 68 private KeyguardUpdateMonitor mKeyguardUpdateMonitor; 69 @Mock 70 private SecurityMode mSecurityMode; 71 @Mock 72 private LockPatternUtils mLockPatternUtils; 73 @Mock 74 private KeyguardSecurityCallback mKeyguardSecurityCallback; 75 @Mock 76 private KeyguardMessageAreaController.Factory mKeyguardMessageAreaControllerFactory; 77 @Mock 78 private KeyguardMessageAreaController mKeyguardMessageAreaController; 79 @Mock 80 private LatencyTracker mLatencyTracker; 81 private final FalsingCollector mFalsingCollector = new FalsingCollectorFake(); 82 @Mock 83 private EmergencyButtonController mEmergencyButtonController; 84 85 private FakeFeatureFlags mFeatureFlags; 86 @Mock 87 private SelectedUserInteractor mSelectedUserInteractor; 88 private KeyguardAbsKeyInputViewController mKeyguardAbsKeyInputViewController; 89 90 @Before setup()91 public void setup() { 92 MockitoAnnotations.initMocks(this); 93 when(mKeyguardMessageAreaControllerFactory.create(any(KeyguardMessageArea.class))) 94 .thenReturn(mKeyguardMessageAreaController); 95 when(mAbsKeyInputView.getPasswordTextViewId()).thenReturn(1); 96 when(mAbsKeyInputView.findViewById(1)).thenReturn(mPasswordEntry); 97 when(mAbsKeyInputView.isAttachedToWindow()).thenReturn(true); 98 when(mAbsKeyInputView.requireViewById(R.id.bouncer_message_area)) 99 .thenReturn(mKeyguardMessageArea); 100 when(mAbsKeyInputView.getResources()).thenReturn(getContext().getResources()); 101 mFeatureFlags = new FakeFeatureFlags(); 102 mKeyguardAbsKeyInputViewController = createTestObject(); 103 mKeyguardAbsKeyInputViewController.init(); 104 reset(mKeyguardMessageAreaController); // Clear out implicit call to init. 105 } 106 createTestObject()107 private KeyguardAbsKeyInputViewController createTestObject() { 108 return new KeyguardAbsKeyInputViewController(mAbsKeyInputView, 109 mKeyguardUpdateMonitor, mSecurityMode, mLockPatternUtils, mKeyguardSecurityCallback, 110 mKeyguardMessageAreaControllerFactory, mLatencyTracker, mFalsingCollector, 111 mEmergencyButtonController, mFeatureFlags, mSelectedUserInteractor) { 112 @Override 113 void resetState() { 114 } 115 116 @Override 117 public void onResume(int reason) { 118 super.onResume(reason); 119 } 120 121 @Override 122 protected int getInitialMessageResId() { 123 return 0; 124 } 125 }; 126 } 127 128 @Test 129 @EnableFlags(Flags.FLAG_REVAMPED_BOUNCER_MESSAGES) 130 public void withFeatureFlagOn_oldMessage_isHidden() { 131 KeyguardAbsKeyInputViewController underTest = createTestObject(); 132 133 underTest.init(); 134 135 verify(mKeyguardMessageAreaController).disable(); 136 } 137 138 @Test 139 public void onKeyDown_clearsSecurityMessage() { 140 ArgumentCaptor<KeyDownListener> onKeyDownListenerArgumentCaptor = 141 ArgumentCaptor.forClass(KeyDownListener.class); 142 verify(mAbsKeyInputView).setKeyDownListener(onKeyDownListenerArgumentCaptor.capture()); 143 onKeyDownListenerArgumentCaptor.getValue().onKeyDown( 144 KeyEvent.KEYCODE_0, mock(KeyEvent.class)); 145 verify(mKeyguardSecurityCallback).userActivity(); 146 verify(mKeyguardMessageAreaController).setMessage(eq("")); 147 } 148 149 @Test 150 public void onKeyDown_noSecurityMessageInteraction() { 151 ArgumentCaptor<KeyDownListener> onKeyDownListenerArgumentCaptor = 152 ArgumentCaptor.forClass(KeyDownListener.class); 153 verify(mAbsKeyInputView).setKeyDownListener(onKeyDownListenerArgumentCaptor.capture()); 154 onKeyDownListenerArgumentCaptor.getValue().onKeyDown( 155 KeyEvent.KEYCODE_UNKNOWN, mock(KeyEvent.class)); 156 verifyZeroInteractions(mKeyguardSecurityCallback); 157 verifyZeroInteractions(mKeyguardMessageAreaController); 158 } 159 160 @Test 161 public void onPromptReasonNone_doesNotSetMessage() { 162 mKeyguardAbsKeyInputViewController.showPromptReason(0); 163 verify(mKeyguardMessageAreaController, never()).setMessage( 164 getContext().getResources().getString(R.string.kg_prompt_reason_restart_password), 165 false); 166 } 167 168 @Test 169 public void onPromptReason_setsMessage() { 170 when(mAbsKeyInputView.getPromptReasonStringRes(1)).thenReturn( 171 R.string.kg_prompt_reason_restart_password); 172 mKeyguardAbsKeyInputViewController.showPromptReason(1); 173 verify(mKeyguardMessageAreaController).setMessage( 174 getContext().getResources().getString(R.string.kg_prompt_reason_restart_password), 175 false); 176 } 177 178 179 @Test 180 public void testReset() { 181 mKeyguardAbsKeyInputViewController.reset(); 182 verify(mKeyguardMessageAreaController).setMessage("", false); 183 } 184 185 @Test 186 public void testOnViewAttached() { 187 reset(mLockPatternUtils); 188 mKeyguardAbsKeyInputViewController.onViewAttached(); 189 verify(mLockPatternUtils).getLockoutAttemptDeadline(anyInt()); 190 } 191 192 @Test 193 public void testLockedOut_verifyPasswordAndUnlock_doesNotEnableViewInput() { 194 mKeyguardAbsKeyInputViewController.handleAttemptLockout(SystemClock.elapsedRealtime()); 195 verify(mAbsKeyInputView).setPasswordEntryInputEnabled(false); 196 verify(mAbsKeyInputView).setPasswordEntryEnabled(false); 197 verify(mAbsKeyInputView, never()).setPasswordEntryInputEnabled(true); 198 verify(mAbsKeyInputView, never()).setPasswordEntryEnabled(true); 199 } 200 } 201