blob: e0d268127d9063c99f6b5908b2a4b539626bde23 [file] [log] [blame]
Bartosz Fabianowski5f045002016-12-01 10:36:18 +01001/*
2 * Copyright (C) 2016 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
17package com.android.systemui.statusbar;
18
arangelovc00bd0c2020-06-19 17:13:59 +010019import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
20
Lucas Dupinff6628d2018-10-15 10:12:37 -070021import static com.google.common.truth.Truth.assertThat;
22
Adrian Roosc1b50322017-02-27 21:07:58 +010023import static org.junit.Assert.assertFalse;
24import static org.junit.Assert.assertTrue;
Lucas Dupin8e2fd012019-04-25 16:40:54 -070025import static org.mockito.ArgumentMatchers.any;
Haining Chenc06c4812020-01-13 20:38:53 -080026import static org.mockito.ArgumentMatchers.anyBoolean;
Lucas Dupin0df60fe2019-04-23 10:19:27 -070027import static org.mockito.ArgumentMatchers.anyInt;
28import static org.mockito.ArgumentMatchers.eq;
Lucas Dupin9131b282019-11-19 16:25:03 -080029import static org.mockito.Mockito.clearInvocations;
Jeff DeCewa87cddb2020-06-12 12:13:52 -040030import static org.mockito.Mockito.doNothing;
Jason Monk3cfedd72016-12-09 09:31:37 -050031import static org.mockito.Mockito.mock;
Lucas Dupin9131b282019-11-19 16:25:03 -080032import static org.mockito.Mockito.never;
Jason Monk3cfedd72016-12-09 09:31:37 -050033import static org.mockito.Mockito.reset;
Lucas Dupin8e2fd012019-04-25 16:40:54 -070034import static org.mockito.Mockito.times;
Jason Monk3cfedd72016-12-09 09:31:37 -050035import static org.mockito.Mockito.verify;
Jeff DeCewa87cddb2020-06-12 12:13:52 -040036import static org.mockito.Mockito.verifyNoMoreInteractions;
Jason Monk3cfedd72016-12-09 09:31:37 -050037import static org.mockito.Mockito.when;
38
Adrian Roosc1b50322017-02-27 21:07:58 +010039import android.app.Instrumentation;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010040import android.app.admin.DevicePolicyManager;
41import android.app.trust.TrustManager;
Jeff DeCewa87cddb2020-06-12 12:13:52 -040042import android.content.BroadcastReceiver;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010043import android.content.Context;
Jeff DeCewa87cddb2020-06-12 12:13:52 -040044import android.content.Intent;
arangelovc00bd0c2020-06-19 17:13:59 +010045import android.content.pm.UserInfo;
Lucas Dupinff6628d2018-10-15 10:12:37 -070046import android.graphics.Color;
Lucas Dupin85ea4202019-11-04 16:01:51 -080047import android.hardware.biometrics.BiometricSourceType;
48import android.hardware.face.FaceManager;
Jason Monk3cfedd72016-12-09 09:31:37 -050049import android.hardware.fingerprint.FingerprintManager;
Lucas Dupin9131b282019-11-19 16:25:03 -080050import android.os.BatteryManager;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010051import android.os.Looper;
Lucas Dupin9131b282019-11-19 16:25:03 -080052import android.os.RemoteException;
Lucas Dupin85ea4202019-11-04 16:01:51 -080053import android.os.UserManager;
Jeff DeCewa87cddb2020-06-12 12:13:52 -040054import android.view.View;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010055import android.view.ViewGroup;
56
Brett Chabot84151d92019-02-27 15:37:59 -080057import androidx.test.InstrumentationRegistry;
58import androidx.test.filters.SmallTest;
59import androidx.test.runner.AndroidJUnit4;
60
Lucas Dupin9131b282019-11-19 16:25:03 -080061import com.android.internal.app.IBatteryStats;
Lucas Dupin8e2fd012019-04-25 16:40:54 -070062import com.android.keyguard.KeyguardUpdateMonitor;
Jeff DeCewa87cddb2020-06-12 12:13:52 -040063import com.android.keyguard.KeyguardUpdateMonitorCallback;
Jerry Chang6a8fb022019-11-15 06:52:54 +080064import com.android.settingslib.Utils;
Raff Tsaif4ea5622019-06-26 16:15:21 +080065import com.android.settingslib.fuelgauge.BatteryStatus;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010066import com.android.systemui.R;
67import com.android.systemui.SysuiTestCase;
Jeff DeCewa87cddb2020-06-12 12:13:52 -040068import com.android.systemui.broadcast.BroadcastDispatcher;
Jerry Chang6a8fb022019-11-15 06:52:54 +080069import com.android.systemui.dock.DockManager;
Lucas Dupin8e2fd012019-04-25 16:40:54 -070070import com.android.systemui.plugins.statusbar.StatusBarStateController;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010071import com.android.systemui.statusbar.phone.KeyguardIndicationTextView;
Jeff DeCewa87cddb2020-06-12 12:13:52 -040072import com.android.systemui.statusbar.phone.LockIcon;
Lucas Dupin85ea4202019-11-04 16:01:51 -080073import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
Lucas Dupinc8f16e82019-09-17 18:24:50 -040074import com.android.systemui.statusbar.policy.KeyguardStateController;
Adrian Roosc1b50322017-02-27 21:07:58 +010075import com.android.systemui.util.wakelock.WakeLockFake;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010076
77import org.junit.Before;
78import org.junit.Test;
79import org.junit.runner.RunWith;
Lucas Dupin0df60fe2019-04-23 10:19:27 -070080import org.mockito.ArgumentCaptor;
Jerry Chang6a8fb022019-11-15 06:52:54 +080081import org.mockito.Captor;
Lucas Dupinff6628d2018-10-15 10:12:37 -070082import org.mockito.Mock;
83import org.mockito.MockitoAnnotations;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010084
arangelovc00bd0c2020-06-19 17:13:59 +010085import java.util.Collections;
86
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010087@SmallTest
88@RunWith(AndroidJUnit4.class)
89public class KeyguardIndicationControllerTest extends SysuiTestCase {
90
Jeff DeCewa87cddb2020-06-12 12:13:52 -040091 private static final String ORGANIZATION_NAME = "organization";
92
93 private String mDisclosureWithOrganization;
94
Lucas Dupinff6628d2018-10-15 10:12:37 -070095 @Mock
96 private DevicePolicyManager mDevicePolicyManager;
97 @Mock
98 private ViewGroup mIndicationArea;
99 @Mock
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400100 private KeyguardStateController mKeyguardStateController;
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700101 @Mock
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400102 private KeyguardIndicationTextView mDisclosure;
103 @Mock
104 private BroadcastDispatcher mBroadcastDispatcher;
105 @Mock
106 private LockIcon mLockIcon;
107 @Mock
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700108 private StatusBarStateController mStatusBarStateController;
109 @Mock
110 private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
Lucas Dupin85ea4202019-11-04 16:01:51 -0800111 @Mock
112 private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager;
113 @Mock
114 private UserManager mUserManager;
Jerry Chang6a8fb022019-11-15 06:52:54 +0800115 @Mock
Lucas Dupin9131b282019-11-19 16:25:03 -0800116 private IBatteryStats mIBatteryStats;
117 @Mock
Jerry Chang6a8fb022019-11-15 06:52:54 +0800118 private DockManager mDockManager;
119 @Captor
120 private ArgumentCaptor<DockManager.AlignmentStateListener> mAlignmentListener;
Lucas Dupinff6628d2018-10-15 10:12:37 -0700121 private KeyguardIndicationTextView mTextView;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100122
123 private KeyguardIndicationController mController;
Dave Mankoffc0d9a7d2020-02-27 17:15:52 -0500124 private WakeLockFake.Builder mWakeLockBuilder;
Adrian Roosc1b50322017-02-27 21:07:58 +0100125 private WakeLockFake mWakeLock;
126 private Instrumentation mInstrumentation;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100127
128 @Before
129 public void setUp() throws Exception {
Lucas Dupinff6628d2018-10-15 10:12:37 -0700130 MockitoAnnotations.initMocks(this);
Adrian Roosc1b50322017-02-27 21:07:58 +0100131 mInstrumentation = InstrumentationRegistry.getInstrumentation();
Lucas Dupinff6628d2018-10-15 10:12:37 -0700132 mTextView = new KeyguardIndicationTextView(mContext);
Adrian Roosc1b50322017-02-27 21:07:58 +0100133
Jason Monk3cfedd72016-12-09 09:31:37 -0500134 mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager);
Lucas Dupin85ea4202019-11-04 16:01:51 -0800135 mContext.addMockSystemService(UserManager.class, mUserManager);
Jason Monk3cfedd72016-12-09 09:31:37 -0500136 mContext.addMockSystemService(Context.TRUST_SERVICE, mock(TrustManager.class));
137 mContext.addMockSystemService(Context.FINGERPRINT_SERVICE, mock(FingerprintManager.class));
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400138 mDisclosureWithOrganization = mContext.getString(R.string.do_disclosure_with_name,
139 ORGANIZATION_NAME);
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100140
Haining Chenc06c4812020-01-13 20:38:53 -0800141 when(mKeyguardUpdateMonitor.isUnlockingWithBiometricAllowed(anyBoolean())).thenReturn(true);
Lucas Dupin85ea4202019-11-04 16:01:51 -0800142 when(mKeyguardUpdateMonitor.isScreenOn()).thenReturn(true);
143 when(mKeyguardUpdateMonitor.isUserUnlocked(anyInt())).thenReturn(true);
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400144
145 when(mIndicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure))
146 .thenReturn(mDisclosure);
Lucas Dupinff6628d2018-10-15 10:12:37 -0700147 when(mIndicationArea.findViewById(R.id.keyguard_indication_text)).thenReturn(mTextView);
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400148 when(mDisclosure.getAlpha()).thenReturn(1f);
Adrian Roosc1b50322017-02-27 21:07:58 +0100149
150 mWakeLock = new WakeLockFake();
Dave Mankoffc0d9a7d2020-02-27 17:15:52 -0500151 mWakeLockBuilder = new WakeLockFake.Builder(mContext);
152 mWakeLockBuilder.setWakeLock(mWakeLock);
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100153 }
154
155 private void createController() {
156 if (Looper.myLooper() == null) {
157 Looper.prepare();
158 }
Dave Mankoffc0d9a7d2020-02-27 17:15:52 -0500159
160 mController = new KeyguardIndicationController(mContext, mWakeLockBuilder,
Jerry Chang6a8fb022019-11-15 06:52:54 +0800161 mKeyguardStateController, mStatusBarStateController, mKeyguardUpdateMonitor,
arangelovc00bd0c2020-06-19 17:13:59 +0100162 mDockManager, mBroadcastDispatcher, mDevicePolicyManager, mIBatteryStats,
163 mUserManager);
Dave Mankoffc0d9a7d2020-02-27 17:15:52 -0500164 mController.setIndicationArea(mIndicationArea);
Lucas Dupin85ea4202019-11-04 16:01:51 -0800165 mController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager);
Lucas Dupin9131b282019-11-19 16:25:03 -0800166 clearInvocations(mIBatteryStats);
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400167 verify(mDisclosure).getAlpha();
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100168 }
169
170 @Test
Jerry Chang6a8fb022019-11-15 06:52:54 +0800171 public void createController_addsAlignmentListener() {
172 createController();
173
174 verify(mDockManager).addAlignmentStateListener(
175 any(DockManager.AlignmentStateListener.class));
176 }
177
178 @Test
179 public void onAlignmentStateChanged_showsSlowChargingIndication() {
Wilson Wu0e727132019-12-26 16:17:56 +0800180 mInstrumentation.runOnMainSync(() -> {
181 createController();
182 verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture());
183 mController.setVisible(true);
Jerry Chang6a8fb022019-11-15 06:52:54 +0800184
Wilson Wu0e727132019-12-26 16:17:56 +0800185 mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_POOR);
186 });
187 mInstrumentation.waitForIdleSync();
Jerry Chang6a8fb022019-11-15 06:52:54 +0800188
189 assertThat(mTextView.getText()).isEqualTo(
190 mContext.getResources().getString(R.string.dock_alignment_slow_charging));
191 assertThat(mTextView.getCurrentTextColor()).isEqualTo(
192 Utils.getColorError(mContext).getDefaultColor());
193 }
194
195 @Test
196 public void onAlignmentStateChanged_showsNotChargingIndication() {
Wilson Wu0e727132019-12-26 16:17:56 +0800197 mInstrumentation.runOnMainSync(() -> {
198 createController();
199 verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture());
200 mController.setVisible(true);
Jerry Chang6a8fb022019-11-15 06:52:54 +0800201
Wilson Wu0e727132019-12-26 16:17:56 +0800202 mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_TERRIBLE);
203 });
204 mInstrumentation.waitForIdleSync();
Jerry Chang6a8fb022019-11-15 06:52:54 +0800205
206 assertThat(mTextView.getText()).isEqualTo(
207 mContext.getResources().getString(R.string.dock_alignment_not_charging));
208 assertThat(mTextView.getCurrentTextColor()).isEqualTo(
209 Utils.getColorError(mContext).getDefaultColor());
210 }
211
212 @Test
213 public void onAlignmentStateChanged_whileDozing_showsSlowChargingIndication() {
Wilson Wu0e727132019-12-26 16:17:56 +0800214 mInstrumentation.runOnMainSync(() -> {
215 createController();
216 verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture());
217 mController.setVisible(true);
218 mController.setDozing(true);
Jerry Chang6a8fb022019-11-15 06:52:54 +0800219
Wilson Wu0e727132019-12-26 16:17:56 +0800220 mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_POOR);
221 });
222 mInstrumentation.waitForIdleSync();
Jerry Chang6a8fb022019-11-15 06:52:54 +0800223
224 assertThat(mTextView.getText()).isEqualTo(
225 mContext.getResources().getString(R.string.dock_alignment_slow_charging));
Lucas Dupin5567b892020-04-16 14:05:35 -0700226 assertThat(mTextView.getCurrentTextColor()).isEqualTo(
Wilson Wubb51d092020-04-21 16:00:59 +0800227 mContext.getColor(R.color.misalignment_text_color));
Jerry Chang6a8fb022019-11-15 06:52:54 +0800228 }
229
230 @Test
231 public void onAlignmentStateChanged_whileDozing_showsNotChargingIndication() {
Wilson Wu0e727132019-12-26 16:17:56 +0800232 mInstrumentation.runOnMainSync(() -> {
233 createController();
234 verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture());
235 mController.setVisible(true);
236 mController.setDozing(true);
Jerry Chang6a8fb022019-11-15 06:52:54 +0800237
Wilson Wu0e727132019-12-26 16:17:56 +0800238 mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_TERRIBLE);
239 });
240 mInstrumentation.waitForIdleSync();
Jerry Chang6a8fb022019-11-15 06:52:54 +0800241
242 assertThat(mTextView.getText()).isEqualTo(
243 mContext.getResources().getString(R.string.dock_alignment_not_charging));
Lucas Dupin5567b892020-04-16 14:05:35 -0700244 assertThat(mTextView.getCurrentTextColor()).isEqualTo(
Wilson Wubb51d092020-04-21 16:00:59 +0800245 mContext.getColor(R.color.misalignment_text_color));
Jerry Chang6a8fb022019-11-15 06:52:54 +0800246 }
247
248 @Test
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400249 public void disclosure_unmanaged() {
250 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
arangelovc00bd0c2020-06-19 17:13:59 +0100251 when(mDevicePolicyManager.isOrganizationOwnedDeviceWithManagedProfile()).thenReturn(false);
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400252 createController();
253
254 verify(mDisclosure).setVisibility(View.GONE);
255 verifyNoMoreInteractions(mDisclosure);
256 }
257
258 @Test
arangelovc00bd0c2020-06-19 17:13:59 +0100259 public void disclosure_deviceOwner_noOwnerName() {
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400260 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
261 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
262 createController();
263
264 verify(mDisclosure).setVisibility(View.VISIBLE);
265 verify(mDisclosure).switchIndication(R.string.do_disclosure_generic);
266 verifyNoMoreInteractions(mDisclosure);
267 }
268
269 @Test
arangelovc00bd0c2020-06-19 17:13:59 +0100270 public void disclosure_orgOwnedDeviceWithManagedProfile_noOwnerName() {
271 when(mDevicePolicyManager.isOrganizationOwnedDeviceWithManagedProfile()).thenReturn(true);
272 when(mUserManager.getProfiles(anyInt())).thenReturn(Collections.singletonList(
273 new UserInfo(10, /* name */ null, /* flags */ FLAG_MANAGED_PROFILE)));
274 when(mDevicePolicyManager.getOrganizationNameForUser(eq(10))).thenReturn(null);
275 createController();
276
277 verify(mDisclosure).setVisibility(View.VISIBLE);
278 verify(mDisclosure).switchIndication(R.string.do_disclosure_generic);
279 verifyNoMoreInteractions(mDisclosure);
280 }
281
282 @Test
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400283 public void disclosure_hiddenWhenDozing() {
284 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
285 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
286 createController();
287
288 mController.setVisible(true);
289 mController.onDozeAmountChanged(1, 1);
290 mController.setDozing(true);
291
292 verify(mDisclosure).setVisibility(View.VISIBLE);
293 verify(mDisclosure).setAlpha(0f);
294 verify(mDisclosure).switchIndication(R.string.do_disclosure_generic);
295 verifyNoMoreInteractions(mDisclosure);
296 }
297
298 @Test
299 public void disclosure_visibleWhenDozing() {
300 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
301 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
302 createController();
303
304 mController.setVisible(true);
305 mController.onDozeAmountChanged(0, 0);
306 mController.setDozing(false);
307
308 verify(mDisclosure).setVisibility(View.VISIBLE);
309 verify(mDisclosure).setAlpha(1f);
310 verify(mDisclosure).switchIndication(R.string.do_disclosure_generic);
311 verifyNoMoreInteractions(mDisclosure);
312 }
313
314 @Test
arangelovc00bd0c2020-06-19 17:13:59 +0100315 public void disclosure_deviceOwner_withOwnerName() {
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400316 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
317 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME);
318 createController();
319
320 verify(mDisclosure).setVisibility(View.VISIBLE);
321 verify(mDisclosure).switchIndication(mDisclosureWithOrganization);
322 verifyNoMoreInteractions(mDisclosure);
323 }
324
325 @Test
arangelovc00bd0c2020-06-19 17:13:59 +0100326 public void disclosure_orgOwnedDeviceWithManagedProfile_withOwnerName() {
327 when(mDevicePolicyManager.isOrganizationOwnedDeviceWithManagedProfile()).thenReturn(true);
328 when(mUserManager.getProfiles(anyInt())).thenReturn(Collections.singletonList(
329 new UserInfo(10, /* name */ null, FLAG_MANAGED_PROFILE)));
330 when(mDevicePolicyManager.getOrganizationNameForUser(eq(10))).thenReturn(ORGANIZATION_NAME);
331 createController();
332
333 verify(mDisclosure).setVisibility(View.VISIBLE);
334 verify(mDisclosure).switchIndication(mDisclosureWithOrganization);
335 verifyNoMoreInteractions(mDisclosure);
336 }
337
338 @Test
Jeff DeCewa87cddb2020-06-12 12:13:52 -0400339 public void disclosure_updateOnTheFly() {
340 ArgumentCaptor<BroadcastReceiver> receiver = ArgumentCaptor.forClass(
341 BroadcastReceiver.class);
342 doNothing().when(mBroadcastDispatcher).registerReceiver(receiver.capture(), any());
343
344 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
345 createController();
346
347 final KeyguardUpdateMonitorCallback monitor = mController.getKeyguardCallback();
348 reset(mDisclosure);
349
350 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
351 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
352 receiver.getValue().onReceive(mContext, new Intent());
353
354 verify(mDisclosure).setVisibility(View.VISIBLE);
355 verify(mDisclosure).switchIndication(R.string.do_disclosure_generic);
356 verifyNoMoreInteractions(mDisclosure);
357 reset(mDisclosure);
358
359 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
360 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME);
361 receiver.getValue().onReceive(mContext, new Intent());
362
363 verify(mDisclosure).setVisibility(View.VISIBLE);
364 verify(mDisclosure).switchIndication(mDisclosureWithOrganization);
365 verifyNoMoreInteractions(mDisclosure);
366 reset(mDisclosure);
367
368 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
369 receiver.getValue().onReceive(mContext, new Intent());
370
371 verify(mDisclosure).setVisibility(View.GONE);
372 verifyNoMoreInteractions(mDisclosure);
373 }
374
375 @Test
Adrian Roosc1b50322017-02-27 21:07:58 +0100376 public void transientIndication_holdsWakeLock_whenDozing() {
377 createController();
378
379 mController.setDozing(true);
380 mController.showTransientIndication("Test");
381
382 assertTrue(mWakeLock.isHeld());
383 }
384
385 @Test
386 public void transientIndication_releasesWakeLock_afterHiding() {
387 createController();
388
389 mController.setDozing(true);
390 mController.showTransientIndication("Test");
391 mController.hideTransientIndication();
392
393 assertFalse(mWakeLock.isHeld());
394 }
395
396 @Test
397 public void transientIndication_releasesWakeLock_afterHidingDelayed() throws Throwable {
398 mInstrumentation.runOnMainSync(() -> {
399 createController();
400
401 mController.setDozing(true);
402 mController.showTransientIndication("Test");
403 mController.hideTransientIndicationDelayed(0);
404 });
405 mInstrumentation.waitForIdleSync();
406
Adrian Roosaf45b602017-03-14 13:10:25 -0700407 Boolean[] held = new Boolean[1];
Adrian Roosc1b50322017-02-27 21:07:58 +0100408 mInstrumentation.runOnMainSync(() -> {
409 held[0] = mWakeLock.isHeld();
Adrian Roosc1b50322017-02-27 21:07:58 +0100410 });
Adrian Roosaf45b602017-03-14 13:10:25 -0700411 assertFalse("WakeLock expected: RELEASED, was: HELD", held[0]);
Adrian Roosc1b50322017-02-27 21:07:58 +0100412 }
Lucas Dupinff6628d2018-10-15 10:12:37 -0700413
414 @Test
415 public void transientIndication_visibleWhenDozing() {
416 createController();
417
418 mController.setVisible(true);
419 mController.showTransientIndication("Test");
420 mController.setDozing(true);
421
422 assertThat(mTextView.getText()).isEqualTo("Test");
423 assertThat(mTextView.getCurrentTextColor()).isEqualTo(Color.WHITE);
424 assertThat(mTextView.getAlpha()).isEqualTo(1f);
425 }
Lucas Dupin0df60fe2019-04-23 10:19:27 -0700426
427 @Test
Lucas Dupin85ea4202019-11-04 16:01:51 -0800428 public void transientIndication_visibleWhenDozing_unlessSwipeUp_fromHelp() {
429 createController();
430 String message = "A message";
431
432 mController.setVisible(true);
433 mController.getKeyguardCallback().onBiometricHelp(
434 KeyguardUpdateMonitor.BIOMETRIC_HELP_FACE_NOT_RECOGNIZED, message,
435 BiometricSourceType.FACE);
436 assertThat(mTextView.getText()).isEqualTo(message);
437 mController.setDozing(true);
438
439 assertThat(mTextView.getText()).isNotEqualTo(message);
440 }
441
442 @Test
443 public void transientIndication_visibleWhenDozing_unlessSwipeUp_fromError() {
444 createController();
445 String message = mContext.getString(R.string.keyguard_unlock);
446
447 mController.setVisible(true);
448 mController.getKeyguardCallback().onBiometricError(FaceManager.FACE_ERROR_TIMEOUT,
449 "A message", BiometricSourceType.FACE);
450
451 assertThat(mTextView.getText()).isEqualTo(message);
452 mController.setDozing(true);
453
454 assertThat(mTextView.getText()).isNotEqualTo(message);
455 }
456
457 @Test
458 public void transientIndication_swipeUpToRetry() {
459 createController();
460 String message = mContext.getString(R.string.keyguard_retry);
461 when(mStatusBarKeyguardViewManager.isBouncerShowing()).thenReturn(true);
462
463 mController.setVisible(true);
464 mController.getKeyguardCallback().onBiometricError(FaceManager.FACE_ERROR_TIMEOUT,
465 "A message", BiometricSourceType.FACE);
466
467 verify(mStatusBarKeyguardViewManager).showBouncerMessage(eq(message), any());
468 }
469
470 @Test
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400471 public void updateMonitor_listenerUpdatesIndication() {
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700472 createController();
473 String restingIndication = "Resting indication";
Lucas Dupin85ea4202019-11-04 16:01:51 -0800474 reset(mKeyguardUpdateMonitor);
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700475
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700476 mController.setVisible(true);
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700477 assertThat(mTextView.getText()).isEqualTo(
478 mContext.getString(com.android.internal.R.string.lockscreen_storage_locked));
479
480 when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(true);
481 when(mKeyguardUpdateMonitor.isUserUnlocked(anyInt())).thenReturn(true);
482 mController.setRestingIndication(restingIndication);
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700483 assertThat(mTextView.getText()).isEqualTo(mController.getTrustGrantedIndication());
484
485 reset(mKeyguardUpdateMonitor);
Lucas Dupin6c6d5732019-08-27 17:36:05 -0700486 when(mKeyguardUpdateMonitor.isUserUnlocked(anyInt())).thenReturn(true);
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700487 when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(false);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400488 mController.onUnlockedChanged();
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700489 assertThat(mTextView.getText()).isEqualTo(restingIndication);
490 }
491
492 @Test
Lucas Dupin9131b282019-11-19 16:25:03 -0800493 public void onRefreshBatteryInfo_computesChargingTime() throws RemoteException {
494 createController();
495 BatteryStatus status = new BatteryStatus(BatteryManager.BATTERY_STATUS_CHARGING,
496 80 /* level */, BatteryManager.BATTERY_PLUGGED_WIRELESS, 100 /* health */,
497 0 /* maxChargingWattage */);
498
499 mController.getKeyguardCallback().onRefreshBatteryInfo(status);
500 verify(mIBatteryStats).computeChargeTimeRemaining();
501 }
502
503 @Test
504 public void onRefreshBatteryInfo_computesChargingTime_onlyWhenCharging()
505 throws RemoteException {
506 createController();
507 BatteryStatus status = new BatteryStatus(BatteryManager.BATTERY_STATUS_CHARGING,
508 80 /* level */, 0 /* plugged */, 100 /* health */,
509 0 /* maxChargingWattage */);
510
511 mController.getKeyguardCallback().onRefreshBatteryInfo(status);
512 verify(mIBatteryStats, never()).computeChargeTimeRemaining();
513 }
514
515 /**
516 * Regression test.
517 * We should not make calls to the system_process when updating the doze state.
518 */
519 @Test
520 public void setDozing_noIBatteryCalls() throws RemoteException {
521 createController();
522 mController.setVisible(true);
523 mController.setDozing(true);
524 mController.setDozing(false);
525 verify(mIBatteryStats, never()).computeChargeTimeRemaining();
526 }
527
528 @Test
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400529 public void updateMonitor_listener() {
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700530 createController();
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400531 verify(mKeyguardStateController).addCallback(eq(mController));
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700532 verify(mStatusBarStateController).addCallback(eq(mController));
533 verify(mKeyguardUpdateMonitor, times(2)).registerCallback(any());
Lucas Dupin8e2fd012019-04-25 16:40:54 -0700534 }
Scott Warner778db332020-01-21 09:45:56 -0500535
536 @Test
537 public void unlockMethodCache_listenerUpdatesPluggedIndication() {
538 createController();
539 when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(true);
540 mController.setPowerPluggedIn(true);
541 mController.setVisible(true);
542 String powerIndication = mController.computePowerIndication();
543 String pluggedIndication = mContext.getString(R.string.keyguard_indication_trust_unlocked);
544 pluggedIndication = mContext.getString(
545 R.string.keyguard_indication_trust_unlocked_plugged_in,
546 pluggedIndication, powerIndication);
547 assertThat(mTextView.getText()).isEqualTo(pluggedIndication);
548 }
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100549}