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.TestableLooper.RunWithLooper;
26 import android.view.View;
27 import android.view.ViewGroup;
28 
29 import androidx.test.ext.junit.runners.AndroidJUnit4;
30 import androidx.test.filters.SmallTest;
31 
32 import com.android.internal.util.LatencyTracker;
33 import com.android.internal.widget.LockPatternUtils;
34 import com.android.keyguard.KeyguardSecurityModel.SecurityMode;
35 import com.android.keyguard.domain.interactor.KeyguardKeyboardInteractor;
36 import com.android.systemui.SysuiTestCase;
37 import com.android.systemui.classifier.FalsingCollector;
38 import com.android.systemui.classifier.FalsingCollectorFake;
39 import com.android.systemui.flags.FakeFeatureFlags;
40 import com.android.systemui.keyboard.data.repository.FakeKeyboardRepository;
41 import com.android.systemui.res.R;
42 import com.android.systemui.user.domain.interactor.SelectedUserInteractor;
43 
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 
50 @SmallTest
51 @RunWith(AndroidJUnit4.class)
52 // collectFlow in KeyguardPinBasedInputViewController.onViewAttached calls JavaAdapter.CollectFlow,
53 // which calls View.onRepeatWhenAttached, which requires being run on main thread.
54 @RunWithLooper(setAsMainLooper = true)
55 public class KeyguardPinBasedInputViewControllerTest extends SysuiTestCase {
56 
57     @Mock
58     private KeyguardPinBasedInputView mPinBasedInputView;
59     @Mock
60     private PasswordTextView mPasswordEntry;
61     private final ViewGroup.LayoutParams mPasswordEntryLayoutParams =
62             new ViewGroup.LayoutParams(/* width= */ 0, /* height= */ 0);
63     @Mock
64     private BouncerKeyguardMessageArea mKeyguardMessageArea;
65     @Mock
66     private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
67     @Mock
68     private SecurityMode mSecurityMode;
69     @Mock
70     private LockPatternUtils mLockPatternUtils;
71     @Mock
72     private KeyguardSecurityCallback mKeyguardSecurityCallback;
73     @Mock
74     private KeyguardMessageAreaController.Factory mKeyguardMessageAreaControllerFactory;
75     @Mock
76     private KeyguardMessageAreaController mKeyguardMessageAreaController;
77     @Mock
78     private LatencyTracker mLatencyTracker;
79     @Mock
80     private LiftToActivateListener mLiftToactivateListener;
81     @Mock
82     private EmergencyButtonController mEmergencyButtonController;
83     private FalsingCollector mFalsingCollector = new FalsingCollectorFake();
84     @Mock
85     private View mDeleteButton;
86     @Mock
87     private View mOkButton;
88     @Mock
89     private SelectedUserInteractor mSelectedUserInteractor;
90     private NumPadKey[] mButtons = new NumPadKey[]{};
91 
92     private KeyguardPinBasedInputViewController mKeyguardPinViewController;
93 
94     @Before
setup()95     public void setup() {
96         MockitoAnnotations.initMocks(this);
97         when(mKeyguardMessageAreaControllerFactory.create(any(KeyguardMessageArea.class)))
98                 .thenReturn(mKeyguardMessageAreaController);
99         when(mPinBasedInputView.getPasswordTextViewId()).thenReturn(1);
100         when(mPinBasedInputView.findViewById(1)).thenReturn(mPasswordEntry);
101         when(mPinBasedInputView.isAttachedToWindow()).thenReturn(true);
102         when(mPinBasedInputView.getButtons()).thenReturn(mButtons);
103         when(mPinBasedInputView.requireViewById(R.id.bouncer_message_area))
104                 .thenReturn(mKeyguardMessageArea);
105         when(mPinBasedInputView.findViewById(R.id.delete_button))
106                 .thenReturn(mDeleteButton);
107         when(mPinBasedInputView.findViewById(R.id.key_enter))
108                 .thenReturn(mOkButton);
109 
110         when(mPinBasedInputView.getResources()).thenReturn(getContext().getResources());
111         when(mPasswordEntry.getLayoutParams()).thenReturn(mPasswordEntryLayoutParams);
112         KeyguardKeyboardInteractor keyguardKeyboardInteractor =
113                 new KeyguardKeyboardInteractor(new FakeKeyboardRepository());
114         FakeFeatureFlags featureFlags = new FakeFeatureFlags();
115         mSetFlagsRule.enableFlags(com.android.systemui.Flags.FLAG_REVAMPED_BOUNCER_MESSAGES);
116         mKeyguardPinViewController = new KeyguardPinBasedInputViewController(mPinBasedInputView,
117                 mKeyguardUpdateMonitor, mSecurityMode, mLockPatternUtils, mKeyguardSecurityCallback,
118                 mKeyguardMessageAreaControllerFactory, mLatencyTracker, mLiftToactivateListener,
119                 mEmergencyButtonController, mFalsingCollector, featureFlags,
120                 mSelectedUserInteractor, keyguardKeyboardInteractor) {
121             @Override
122             public void onResume(int reason) {
123                 super.onResume(reason);
124             }
125         };
126         mKeyguardPinViewController.init();
127     }
128 
129     @Test
onResume_requestsFocus()130     public void onResume_requestsFocus() {
131         mKeyguardPinViewController.onResume(KeyguardSecurityView.SCREEN_ON);
132         verify(mPasswordEntry).requestFocus();
133     }
134 
135     @Test
testGetInitialMessageResId()136     public void testGetInitialMessageResId() {
137         assertThat(mKeyguardPinViewController.getInitialMessageResId()).isNotEqualTo(0);
138     }
139 
140     @Test
testMessageIsSetWhenReset()141     public void testMessageIsSetWhenReset() {
142         mKeyguardPinViewController.resetState();
143         verify(mKeyguardMessageAreaController).setMessage(R.string.keyguard_enter_your_pin);
144     }
145 }
146