• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.SysuiTestCase;
39 import com.android.systemui.flags.FeatureFlags;
40 
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.junit.MockitoJUnit;
47 import org.mockito.junit.MockitoRule;
48 
49 @SmallTest
50 @RunWith(AndroidTestingRunner.class)
51 @TestableLooper.RunWithLooper()
52 public class KeyguardSecurityViewFlipperControllerTest extends SysuiTestCase {
53 
54     @Rule
55     public MockitoRule mRule = MockitoJUnit.rule();
56 
57     @Mock
58     private KeyguardSecurityViewFlipper mView;
59     @Mock
60     private LayoutInflater mLayoutInflater;
61     @Mock
62     private AsyncLayoutInflater mAsyncLayoutInflater;
63     @Mock
64     private KeyguardInputViewController.Factory mKeyguardSecurityViewControllerFactory;
65     @Mock
66     private EmergencyButtonController.Factory mEmergencyButtonControllerFactory;
67     @Mock
68     private EmergencyButtonController mEmergencyButtonController;
69     @Mock
70     private KeyguardInputViewController mKeyguardInputViewController;
71     @Mock
72     private KeyguardInputView mInputView;
73     @Mock
74     private WindowInsetsController mWindowInsetsController;
75     @Mock
76     private KeyguardSecurityCallback mKeyguardSecurityCallback;
77     @Mock
78     private FeatureFlags mFeatureFlags;
79 
80     private KeyguardSecurityViewFlipperController mKeyguardSecurityViewFlipperController;
81 
82     @Before
setup()83     public void setup() {
84         when(mKeyguardSecurityViewControllerFactory.create(
85                 any(KeyguardInputView.class), any(SecurityMode.class),
86                 any(KeyguardSecurityCallback.class)))
87                 .thenReturn(mKeyguardInputViewController);
88         when(mView.getWindowInsetsController()).thenReturn(mWindowInsetsController);
89         when(mEmergencyButtonControllerFactory.create(any(EmergencyButton.class)))
90                 .thenReturn(mEmergencyButtonController);
91         when(mView.getContext()).thenReturn(getContext());
92 
93         mKeyguardSecurityViewFlipperController = new KeyguardSecurityViewFlipperController(mView,
94                 mLayoutInflater, mAsyncLayoutInflater, mKeyguardSecurityViewControllerFactory,
95                 mEmergencyButtonControllerFactory, mFeatureFlags);
96     }
97 
98     @Test
showSecurityScreen_canInflateAllModes()99     public void showSecurityScreen_canInflateAllModes() {
100         SecurityMode[] modes = SecurityMode.values();
101         // Always return an invalid controller so that we're always making a new one.
102         when(mKeyguardInputViewController.getSecurityMode()).thenReturn(SecurityMode.Invalid);
103         for (SecurityMode mode : modes) {
104             reset(mLayoutInflater);
105             when(mLayoutInflater.inflate(anyInt(), eq(mView), eq(false)))
106                     .thenReturn(mInputView);
107             mKeyguardSecurityViewFlipperController.getSecurityView(mode, mKeyguardSecurityCallback);
108             if (mode == SecurityMode.Invalid || mode == SecurityMode.None) {
109                 verify(mLayoutInflater, never()).inflate(
110                         anyInt(), any(ViewGroup.class), anyBoolean());
111             } else {
112                 verify(mLayoutInflater).inflate(anyInt(), eq(mView), eq(false));
113             }
114         }
115     }
116 
117     @Test
asynchronouslyInflateView()118     public void asynchronouslyInflateView() {
119         mKeyguardSecurityViewFlipperController.asynchronouslyInflateView(SecurityMode.PIN,
120                 mKeyguardSecurityCallback, null);
121         verify(mAsyncLayoutInflater).inflate(anyInt(), eq(mView), any(
122                 AsyncLayoutInflater.OnInflateFinishedListener.class));
123     }
124 
125     @Test
onDensityOrFontScaleChanged()126     public void onDensityOrFontScaleChanged() {
127         mKeyguardSecurityViewFlipperController.clearViews();
128         verify(mView).removeAllViews();
129     }
130 }
131