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.eq; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.reset; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.verifyZeroInteractions; 25 import static org.mockito.Mockito.when; 26 27 import android.testing.AndroidTestingRunner; 28 import android.testing.TestableLooper.RunWithLooper; 29 import android.view.KeyEvent; 30 31 import androidx.test.filters.SmallTest; 32 33 import com.android.internal.util.LatencyTracker; 34 import com.android.internal.widget.LockPatternUtils; 35 import com.android.keyguard.KeyguardAbsKeyInputView.KeyDownListener; 36 import com.android.keyguard.KeyguardSecurityModel.SecurityMode; 37 import com.android.systemui.R; 38 import com.android.systemui.SysuiTestCase; 39 import com.android.systemui.classifier.FalsingCollector; 40 import com.android.systemui.classifier.FalsingCollectorFake; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.ArgumentCaptor; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 49 @SmallTest 50 @RunWith(AndroidTestingRunner.class) 51 @RunWithLooper 52 public class KeyguardAbsKeyInputViewControllerTest extends SysuiTestCase { 53 54 @Mock 55 private KeyguardAbsKeyInputView mAbsKeyInputView; 56 @Mock 57 private PasswordTextView mPasswordEntry; 58 @Mock 59 private KeyguardMessageArea mKeyguardMessageArea; 60 @Mock 61 private KeyguardUpdateMonitor mKeyguardUpdateMonitor; 62 @Mock 63 private SecurityMode mSecurityMode; 64 @Mock 65 private LockPatternUtils mLockPatternUtils; 66 @Mock 67 private KeyguardSecurityCallback mKeyguardSecurityCallback; 68 @Mock 69 private KeyguardMessageAreaController.Factory mKeyguardMessageAreaControllerFactory; 70 @Mock 71 private KeyguardMessageAreaController mKeyguardMessageAreaController; 72 @Mock 73 private LatencyTracker mLatencyTracker; 74 private final FalsingCollector mFalsingCollector = new FalsingCollectorFake(); 75 @Mock 76 private EmergencyButtonController mEmergencyButtonController; 77 78 private KeyguardAbsKeyInputViewController mKeyguardAbsKeyInputViewController; 79 80 @Before setup()81 public void setup() { 82 MockitoAnnotations.initMocks(this); 83 when(mKeyguardMessageAreaControllerFactory.create(any(KeyguardMessageArea.class))) 84 .thenReturn(mKeyguardMessageAreaController); 85 when(mAbsKeyInputView.getPasswordTextViewId()).thenReturn(1); 86 when(mAbsKeyInputView.findViewById(1)).thenReturn(mPasswordEntry); 87 when(mAbsKeyInputView.isAttachedToWindow()).thenReturn(true); 88 when(mAbsKeyInputView.findViewById(R.id.keyguard_message_area)) 89 .thenReturn(mKeyguardMessageArea); 90 mKeyguardAbsKeyInputViewController = new KeyguardAbsKeyInputViewController(mAbsKeyInputView, 91 mKeyguardUpdateMonitor, mSecurityMode, mLockPatternUtils, mKeyguardSecurityCallback, 92 mKeyguardMessageAreaControllerFactory, mLatencyTracker, mFalsingCollector, 93 mEmergencyButtonController) { 94 @Override 95 void resetState() { 96 } 97 98 @Override 99 public void onResume(int reason) { 100 super.onResume(reason); 101 } 102 }; 103 mKeyguardAbsKeyInputViewController.init(); 104 reset(mKeyguardMessageAreaController); // Clear out implicit call to init. 105 } 106 107 @Test onKeyDown_clearsSecurityMessage()108 public void onKeyDown_clearsSecurityMessage() { 109 ArgumentCaptor<KeyDownListener> onKeyDownListenerArgumentCaptor = 110 ArgumentCaptor.forClass(KeyDownListener.class); 111 verify(mAbsKeyInputView).setKeyDownListener(onKeyDownListenerArgumentCaptor.capture()); 112 onKeyDownListenerArgumentCaptor.getValue().onKeyDown( 113 KeyEvent.KEYCODE_0, mock(KeyEvent.class)); 114 verify(mKeyguardSecurityCallback).userActivity(); 115 verify(mKeyguardMessageAreaController).setMessage(eq("")); 116 } 117 118 @Test onKeyDown_noSecurityMessageInteraction()119 public void onKeyDown_noSecurityMessageInteraction() { 120 ArgumentCaptor<KeyDownListener> onKeyDownListenerArgumentCaptor = 121 ArgumentCaptor.forClass(KeyDownListener.class); 122 verify(mAbsKeyInputView).setKeyDownListener(onKeyDownListenerArgumentCaptor.capture()); 123 onKeyDownListenerArgumentCaptor.getValue().onKeyDown( 124 KeyEvent.KEYCODE_UNKNOWN, mock(KeyEvent.class)); 125 verifyZeroInteractions(mKeyguardSecurityCallback); 126 verifyZeroInteractions(mKeyguardMessageAreaController); 127 } 128 } 129