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.ArgumentMatchers.anyBoolean;
23 import static org.mockito.ArgumentMatchers.anyLong;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.hardware.biometrics.BiometricSourceType;
30 import android.testing.AndroidTestingRunner;
31 import android.testing.TestableLooper;
32 import android.text.Editable;
33 import android.text.TextWatcher;
34 
35 import androidx.test.filters.SmallTest;
36 
37 import com.android.systemui.SysuiTestCase;
38 import com.android.systemui.statusbar.policy.ConfigurationController;
39 import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.ArgumentCaptor;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 
48 @SmallTest
49 @TestableLooper.RunWithLooper
50 @RunWith(AndroidTestingRunner.class)
51 public class KeyguardMessageAreaControllerTest extends SysuiTestCase {
52     @Mock
53     private ConfigurationController mConfigurationController;
54     @Mock
55     private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
56     @Mock
57     private KeyguardMessageArea mKeyguardMessageArea;
58     private KeyguardMessageAreaController mMessageAreaController;
59 
60     @Before
setUp()61     public void setUp() throws Exception {
62         MockitoAnnotations.initMocks(this);
63         mMessageAreaController = new KeyguardMessageAreaController.Factory(
64                 mKeyguardUpdateMonitor, mConfigurationController).create(
65                 mKeyguardMessageArea);
66     }
67 
68     @Test
onAttachedToWindow_registersConfigurationCallback()69     public void onAttachedToWindow_registersConfigurationCallback() {
70         ArgumentCaptor<ConfigurationListener> configurationListenerArgumentCaptor =
71                 ArgumentCaptor.forClass(ConfigurationListener.class);
72 
73         mMessageAreaController.onViewAttached();
74         verify(mConfigurationController).addCallback(configurationListenerArgumentCaptor.capture());
75 
76         mMessageAreaController.onViewDetached();
77         verify(mConfigurationController).removeCallback(
78                 eq(configurationListenerArgumentCaptor.getValue()));
79     }
80 
81     @Test
onAttachedToWindow_registersKeyguardUpdateMontiorCallback()82     public void onAttachedToWindow_registersKeyguardUpdateMontiorCallback() {
83         ArgumentCaptor<KeyguardUpdateMonitorCallback> keyguardUpdateMonitorCallbackArgumentCaptor =
84                 ArgumentCaptor.forClass(KeyguardUpdateMonitorCallback.class);
85 
86         mMessageAreaController.onViewAttached();
87         verify(mKeyguardUpdateMonitor).registerCallback(
88                 keyguardUpdateMonitorCallbackArgumentCaptor.capture());
89 
90         mMessageAreaController.onViewDetached();
91         verify(mKeyguardUpdateMonitor).removeCallback(
92                 eq(keyguardUpdateMonitorCallbackArgumentCaptor.getValue()));
93     }
94 
95     @Test
testClearsTextField()96     public void testClearsTextField() {
97         mMessageAreaController.setMessage("");
98         verify(mKeyguardMessageArea).setMessage("", /* animate= */ true);
99     }
100 
101     @Test
textChanged_AnnounceForAccessibility()102     public void textChanged_AnnounceForAccessibility() {
103         ArgumentCaptor<TextWatcher> textWatcherArgumentCaptor = ArgumentCaptor.forClass(
104                 TextWatcher.class);
105         mMessageAreaController.onViewAttached();
106         verify(mKeyguardMessageArea).addTextChangedListener(textWatcherArgumentCaptor.capture());
107 
108         textWatcherArgumentCaptor.getValue().afterTextChanged(
109                 Editable.Factory.getInstance().newEditable("abc"));
110         verify(mKeyguardMessageArea).removeCallbacks(any(Runnable.class));
111         verify(mKeyguardMessageArea).postDelayed(any(Runnable.class), anyLong());
112     }
113 
114     @Test
testSetBouncerVisible()115     public void testSetBouncerVisible() {
116         mMessageAreaController.setIsVisible(true);
117         verify(mKeyguardMessageArea).setIsVisible(true);
118     }
119 
120     @Test
testGetMessage()121     public void testGetMessage() {
122         String msg = "abc";
123         when(mKeyguardMessageArea.getText()).thenReturn(msg);
124         assertThat(mMessageAreaController.getMessage()).isEqualTo(msg);
125     }
126 
127     @Test
testFingerprintMessageUpdate()128     public void testFingerprintMessageUpdate() {
129         String msg = "fpMessage";
130         mMessageAreaController.setMessage(
131                 msg, BiometricSourceType.FINGERPRINT
132         );
133         verify(mKeyguardMessageArea).setMessage(msg, /* animate= */ true);
134 
135         String msg2 = "fpMessage2";
136         mMessageAreaController.setMessage(
137                 msg2, BiometricSourceType.FINGERPRINT
138         );
139         verify(mKeyguardMessageArea).setMessage(msg2, /* animate= */ true);
140     }
141 
142     @Test
testFaceMessageDroppedWhileFingerprintMessageShowing()143     public void testFaceMessageDroppedWhileFingerprintMessageShowing() {
144         String fpMsg = "fpMessage";
145         mMessageAreaController.setMessage(
146                 fpMsg, BiometricSourceType.FINGERPRINT
147         );
148         verify(mKeyguardMessageArea).setMessage(eq(fpMsg), /* animate= */ anyBoolean());
149 
150         String faceMessage = "faceMessage";
151         mMessageAreaController.setMessage(
152                 faceMessage, BiometricSourceType.FACE
153         );
154         verify(mKeyguardMessageArea, never())
155                 .setMessage(eq(faceMessage), /* animate= */ anyBoolean());
156     }
157 
158     @Test
testGenericMessageShowsAfterFingerprintMessageShowing()159     public void testGenericMessageShowsAfterFingerprintMessageShowing() {
160         String fpMsg = "fpMessage";
161         mMessageAreaController.setMessage(
162                 fpMsg, BiometricSourceType.FINGERPRINT
163         );
164         verify(mKeyguardMessageArea).setMessage(eq(fpMsg), /* animate= */ anyBoolean());
165 
166         String genericMessage = "genericMessage";
167         mMessageAreaController.setMessage(
168                 genericMessage, null
169         );
170         verify(mKeyguardMessageArea)
171                 .setMessage(eq(genericMessage), /* animate= */ anyBoolean());
172     }
173 }
174