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.anyBoolean;
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.ArgumentMatchers.eq;
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.when;
27 
28 import android.testing.AndroidTestingRunner;
29 import android.testing.TestableLooper;
30 import android.view.LayoutInflater;
31 import android.view.ViewGroup;
32 import android.view.WindowInsetsController;
33 
34 import androidx.asynclayoutinflater.view.AsyncLayoutInflater;
35 import androidx.test.filters.SmallTest;
36 
37 import com.android.keyguard.KeyguardSecurityModel.SecurityMode;
38 import com.android.systemui.R;
39 import com.android.systemui.SysuiTestCase;
40 import com.android.systemui.flags.FeatureFlags;
41 
42 import org.junit.Before;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.ArgumentCaptor;
47 import org.mockito.Mock;
48 import org.mockito.junit.MockitoJUnit;
49 import org.mockito.junit.MockitoRule;
50 
51 @SmallTest
52 @RunWith(AndroidTestingRunner.class)
53 @TestableLooper.RunWithLooper()
54 public class KeyguardSecurityViewFlipperControllerTest extends SysuiTestCase {
55 
56     @Rule
57     public MockitoRule mRule = MockitoJUnit.rule();
58 
59     @Mock
60     private KeyguardSecurityViewFlipper mView;
61     @Mock
62     private LayoutInflater mLayoutInflater;
63     @Mock
64     private AsyncLayoutInflater mAsyncLayoutInflater;
65     @Mock
66     private KeyguardInputViewController.Factory mKeyguardSecurityViewControllerFactory;
67     @Mock
68     private EmergencyButtonController.Factory mEmergencyButtonControllerFactory;
69     @Mock
70     private EmergencyButtonController mEmergencyButtonController;
71     @Mock
72     private KeyguardInputViewController mKeyguardInputViewController;
73     @Mock
74     private KeyguardInputView mInputView;
75     @Mock
76     private WindowInsetsController mWindowInsetsController;
77     @Mock
78     private KeyguardSecurityCallback mKeyguardSecurityCallback;
79     @Mock
80     private FeatureFlags mFeatureFlags;
81 
82     private KeyguardSecurityViewFlipperController mKeyguardSecurityViewFlipperController;
83 
84     @Before
setup()85     public void setup() {
86         when(mKeyguardSecurityViewControllerFactory.create(
87                 any(KeyguardInputView.class), any(SecurityMode.class),
88                 any(KeyguardSecurityCallback.class)))
89                 .thenReturn(mKeyguardInputViewController);
90         when(mView.getWindowInsetsController()).thenReturn(mWindowInsetsController);
91         when(mEmergencyButtonControllerFactory.create(any(EmergencyButton.class)))
92                 .thenReturn(mEmergencyButtonController);
93         when(mView.getContext()).thenReturn(getContext());
94 
95         mKeyguardSecurityViewFlipperController = new KeyguardSecurityViewFlipperController(mView,
96                 mLayoutInflater, mAsyncLayoutInflater, mKeyguardSecurityViewControllerFactory,
97                 mEmergencyButtonControllerFactory, mFeatureFlags);
98     }
99 
100     @Test
showSecurityScreen_canInflateAllModes()101     public void showSecurityScreen_canInflateAllModes() {
102         SecurityMode[] modes = SecurityMode.values();
103         // Always return an invalid controller so that we're always making a new one.
104         when(mKeyguardInputViewController.getSecurityMode()).thenReturn(SecurityMode.Invalid);
105         for (SecurityMode mode : modes) {
106             reset(mLayoutInflater);
107             when(mLayoutInflater.inflate(anyInt(), eq(mView), eq(false)))
108                     .thenReturn(mInputView);
109             mKeyguardSecurityViewFlipperController.getSecurityView(mode, mKeyguardSecurityCallback,
110                     controller -> {
111                         if (mode == SecurityMode.Invalid || mode == SecurityMode.None) {
112                             verify(mLayoutInflater, never()).inflate(
113                                     anyInt(), any(ViewGroup.class), anyBoolean());
114                         } else {
115                             verify(mLayoutInflater).inflate(anyInt(), eq(mView), eq(false));
116                         }
117                     });
118         }
119     }
120 
121     @Test
getSecurityView_NotInflated()122     public void getSecurityView_NotInflated() {
123         mKeyguardSecurityViewFlipperController.clearViews();
124         mKeyguardSecurityViewFlipperController.getSecurityView(SecurityMode.PIN,
125                 mKeyguardSecurityCallback,
126                 controller -> {});
127         verify(mAsyncLayoutInflater).inflate(anyInt(), eq(mView), any(
128                 AsyncLayoutInflater.OnInflateFinishedListener.class));
129     }
130 
131     @Test
asynchronouslyInflateView()132     public void asynchronouslyInflateView() {
133         mKeyguardSecurityViewFlipperController.asynchronouslyInflateView(SecurityMode.PIN,
134                 mKeyguardSecurityCallback, null);
135         verify(mAsyncLayoutInflater).inflate(anyInt(), eq(mView), any(
136                 AsyncLayoutInflater.OnInflateFinishedListener.class));
137     }
138 
139     @Test
asynchronouslyInflateView_setNeedsInput()140     public void asynchronouslyInflateView_setNeedsInput() {
141         when(mKeyguardSecurityViewControllerFactory.create(
142                any(), any(SecurityMode.class),
143                 any(KeyguardSecurityCallback.class)))
144                 .thenReturn(mKeyguardInputViewController);
145 
146         ArgumentCaptor<AsyncLayoutInflater.OnInflateFinishedListener> argumentCaptor =
147                 ArgumentCaptor.forClass(AsyncLayoutInflater.OnInflateFinishedListener.class);
148         mKeyguardSecurityViewFlipperController.asynchronouslyInflateView(SecurityMode.PIN,
149                 mKeyguardSecurityCallback, null);
150         verify(mAsyncLayoutInflater).inflate(anyInt(), eq(mView), argumentCaptor.capture());
151         argumentCaptor.getValue().onInflateFinished(
152                 LayoutInflater.from(getContext()).inflate(R.layout.keyguard_password_view, null),
153                 R.layout.keyguard_password_view, mView);
154     }
155 
156     @Test
onDensityOrFontScaleChanged()157     public void onDensityOrFontScaleChanged() {
158         mKeyguardSecurityViewFlipperController.clearViews();
159         verify(mView).removeAllViews();
160     }
161 }
162