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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.testing.AndroidTestingRunner;
26 import android.testing.TestableLooper.RunWithLooper;
27 import android.view.View;
28 
29 import androidx.test.filters.SmallTest;
30 
31 import com.android.internal.util.LatencyTracker;
32 import com.android.internal.widget.LockPatternUtils;
33 import com.android.keyguard.KeyguardSecurityModel.SecurityMode;
34 import com.android.systemui.R;
35 import com.android.systemui.SysuiTestCase;
36 import com.android.systemui.classifier.FalsingCollector;
37 import com.android.systemui.classifier.FalsingCollectorFake;
38 import com.android.systemui.classifier.SingleTapClassifier;
39 import com.android.systemui.flags.FakeFeatureFlags;
40 import com.android.systemui.flags.Flags;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 
48 @SmallTest
49 @RunWith(AndroidTestingRunner.class)
50 @RunWithLooper
51 public class KeyguardPinBasedInputViewControllerTest extends SysuiTestCase {
52 
53     @Mock
54     private KeyguardPinBasedInputView mPinBasedInputView;
55     @Mock
56     private PasswordTextView mPasswordEntry;
57     @Mock
58     private BouncerKeyguardMessageArea mKeyguardMessageArea;
59     @Mock
60     private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
61     @Mock
62     private SecurityMode mSecurityMode;
63     @Mock
64     private LockPatternUtils mLockPatternUtils;
65     @Mock
66     private KeyguardSecurityCallback mKeyguardSecurityCallback;
67     @Mock
68     private KeyguardMessageAreaController.Factory mKeyguardMessageAreaControllerFactory;
69     @Mock
70     private KeyguardMessageAreaController mKeyguardMessageAreaController;
71     @Mock
72     private LatencyTracker mLatencyTracker;
73     @Mock
74     private LiftToActivateListener mLiftToactivateListener;
75     @Mock
76     private EmergencyButtonController mEmergencyButtonController;
77     private FalsingCollector mFalsingCollector = new FalsingCollectorFake();
78     @Mock
79     private SingleTapClassifier mSingleTapClassifier;
80     @Mock
81     private View mDeleteButton;
82     @Mock
83     private View mOkButton;
84     private NumPadKey[] mButtons = new NumPadKey[]{};
85 
86     private KeyguardPinBasedInputViewController mKeyguardPinViewController;
87 
88     @Before
setup()89     public void setup() {
90         MockitoAnnotations.initMocks(this);
91         when(mKeyguardMessageAreaControllerFactory.create(any(KeyguardMessageArea.class)))
92                 .thenReturn(mKeyguardMessageAreaController);
93         when(mPinBasedInputView.getPasswordTextViewId()).thenReturn(1);
94         when(mPinBasedInputView.findViewById(1)).thenReturn(mPasswordEntry);
95         when(mPinBasedInputView.isAttachedToWindow()).thenReturn(true);
96         when(mPinBasedInputView.getButtons()).thenReturn(mButtons);
97         when(mPinBasedInputView.requireViewById(R.id.bouncer_message_area))
98                 .thenReturn(mKeyguardMessageArea);
99         when(mPinBasedInputView.findViewById(R.id.delete_button))
100                 .thenReturn(mDeleteButton);
101         when(mPinBasedInputView.findViewById(R.id.key_enter))
102                 .thenReturn(mOkButton);
103 
104         when(mPinBasedInputView.getResources()).thenReturn(getContext().getResources());
105         FakeFeatureFlags featureFlags = new FakeFeatureFlags();
106         featureFlags.set(Flags.REVAMPED_BOUNCER_MESSAGES, true);
107 
108         mKeyguardPinViewController = new KeyguardPinBasedInputViewController(mPinBasedInputView,
109                 mKeyguardUpdateMonitor, mSecurityMode, mLockPatternUtils, mKeyguardSecurityCallback,
110                 mKeyguardMessageAreaControllerFactory, mLatencyTracker, mLiftToactivateListener,
111                 mEmergencyButtonController, mFalsingCollector, featureFlags) {
112             @Override
113             public void onResume(int reason) {
114                 super.onResume(reason);
115             }
116         };
117         mKeyguardPinViewController.init();
118     }
119 
120     @Test
onResume_requestsFocus()121     public void onResume_requestsFocus() {
122         mKeyguardPinViewController.onResume(KeyguardSecurityView.SCREEN_ON);
123         verify(mPasswordEntry).requestFocus();
124     }
125 
126     @Test
testGetInitialMessageResId()127     public void testGetInitialMessageResId() {
128         assertThat(mKeyguardPinViewController.getInitialMessageResId()).isNotEqualTo(0);
129     }
130 
131     @Test
testMessageIsSetWhenReset()132     public void testMessageIsSetWhenReset() {
133         mKeyguardPinViewController.resetState();
134         verify(mKeyguardMessageAreaController).setMessage(R.string.keyguard_enter_your_pin);
135     }
136 }
137