Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertTrue; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.any; |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 24 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 25 | import static org.mockito.ArgumentMatchers.anyInt; |
| 26 | import static org.mockito.ArgumentMatchers.eq; |
Lucas Dupin | 9131b28 | 2019-11-19 16:25:03 -0800 | [diff] [blame] | 27 | import static org.mockito.Mockito.clearInvocations; |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 28 | import static org.mockito.Mockito.doNothing; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 29 | import static org.mockito.Mockito.mock; |
Lucas Dupin | 9131b28 | 2019-11-19 16:25:03 -0800 | [diff] [blame] | 30 | import static org.mockito.Mockito.never; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 31 | import static org.mockito.Mockito.reset; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 32 | import static org.mockito.Mockito.times; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 33 | import static org.mockito.Mockito.verify; |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 34 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 35 | import static org.mockito.Mockito.when; |
| 36 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 37 | import android.app.Instrumentation; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 38 | import android.app.admin.DevicePolicyManager; |
| 39 | import android.app.trust.TrustManager; |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 41 | import android.content.Context; |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 42 | import android.content.Intent; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 43 | import android.graphics.Color; |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 44 | import android.hardware.biometrics.BiometricSourceType; |
| 45 | import android.hardware.face.FaceManager; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 46 | import android.hardware.fingerprint.FingerprintManager; |
Lucas Dupin | 9131b28 | 2019-11-19 16:25:03 -0800 | [diff] [blame] | 47 | import android.os.BatteryManager; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 48 | import android.os.Looper; |
Lucas Dupin | 9131b28 | 2019-11-19 16:25:03 -0800 | [diff] [blame] | 49 | import android.os.RemoteException; |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 50 | import android.os.UserManager; |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 51 | import android.view.View; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 52 | import android.view.ViewGroup; |
| 53 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 54 | import androidx.test.InstrumentationRegistry; |
| 55 | import androidx.test.filters.SmallTest; |
| 56 | import androidx.test.runner.AndroidJUnit4; |
| 57 | |
Lucas Dupin | 9131b28 | 2019-11-19 16:25:03 -0800 | [diff] [blame] | 58 | import com.android.internal.app.IBatteryStats; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 59 | import com.android.keyguard.KeyguardUpdateMonitor; |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 60 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 61 | import com.android.settingslib.Utils; |
Raff Tsai | f4ea562 | 2019-06-26 16:15:21 +0800 | [diff] [blame] | 62 | import com.android.settingslib.fuelgauge.BatteryStatus; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 63 | import com.android.systemui.R; |
| 64 | import com.android.systemui.SysuiTestCase; |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 65 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 66 | import com.android.systemui.dock.DockManager; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 67 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 68 | import com.android.systemui.statusbar.phone.KeyguardIndicationTextView; |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 69 | import com.android.systemui.statusbar.phone.LockIcon; |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 70 | import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager; |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 71 | import com.android.systemui.statusbar.policy.KeyguardStateController; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 72 | import com.android.systemui.util.wakelock.WakeLockFake; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 73 | |
| 74 | import org.junit.Before; |
| 75 | import org.junit.Test; |
| 76 | import org.junit.runner.RunWith; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 77 | import org.mockito.ArgumentCaptor; |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 78 | import org.mockito.Captor; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 79 | import org.mockito.Mock; |
| 80 | import org.mockito.MockitoAnnotations; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 81 | |
| 82 | @SmallTest |
| 83 | @RunWith(AndroidJUnit4.class) |
| 84 | public class KeyguardIndicationControllerTest extends SysuiTestCase { |
| 85 | |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 86 | private static final String ORGANIZATION_NAME = "organization"; |
| 87 | |
| 88 | private String mDisclosureWithOrganization; |
| 89 | |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 90 | @Mock |
| 91 | private DevicePolicyManager mDevicePolicyManager; |
| 92 | @Mock |
| 93 | private ViewGroup mIndicationArea; |
| 94 | @Mock |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 95 | private KeyguardStateController mKeyguardStateController; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 96 | @Mock |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 97 | private KeyguardIndicationTextView mDisclosure; |
| 98 | @Mock |
| 99 | private BroadcastDispatcher mBroadcastDispatcher; |
| 100 | @Mock |
| 101 | private LockIcon mLockIcon; |
| 102 | @Mock |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 103 | private StatusBarStateController mStatusBarStateController; |
| 104 | @Mock |
| 105 | private KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 106 | @Mock |
| 107 | private StatusBarKeyguardViewManager mStatusBarKeyguardViewManager; |
| 108 | @Mock |
| 109 | private UserManager mUserManager; |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 110 | @Mock |
Lucas Dupin | 9131b28 | 2019-11-19 16:25:03 -0800 | [diff] [blame] | 111 | private IBatteryStats mIBatteryStats; |
| 112 | @Mock |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 113 | private DockManager mDockManager; |
| 114 | @Captor |
| 115 | private ArgumentCaptor<DockManager.AlignmentStateListener> mAlignmentListener; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 116 | private KeyguardIndicationTextView mTextView; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 117 | |
| 118 | private KeyguardIndicationController mController; |
Dave Mankoff | c0d9a7d | 2020-02-27 17:15:52 -0500 | [diff] [blame] | 119 | private WakeLockFake.Builder mWakeLockBuilder; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 120 | private WakeLockFake mWakeLock; |
| 121 | private Instrumentation mInstrumentation; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 122 | |
| 123 | @Before |
| 124 | public void setUp() throws Exception { |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 125 | MockitoAnnotations.initMocks(this); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 126 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 127 | mTextView = new KeyguardIndicationTextView(mContext); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 128 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 129 | mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager); |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 130 | mContext.addMockSystemService(UserManager.class, mUserManager); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 131 | mContext.addMockSystemService(Context.TRUST_SERVICE, mock(TrustManager.class)); |
| 132 | mContext.addMockSystemService(Context.FINGERPRINT_SERVICE, mock(FingerprintManager.class)); |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 133 | mDisclosureWithOrganization = mContext.getString(R.string.do_disclosure_with_name, |
| 134 | ORGANIZATION_NAME); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 135 | |
Haining Chen | c06c481 | 2020-01-13 20:38:53 -0800 | [diff] [blame] | 136 | when(mKeyguardUpdateMonitor.isUnlockingWithBiometricAllowed(anyBoolean())).thenReturn(true); |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 137 | when(mKeyguardUpdateMonitor.isScreenOn()).thenReturn(true); |
| 138 | when(mKeyguardUpdateMonitor.isUserUnlocked(anyInt())).thenReturn(true); |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 139 | |
| 140 | when(mIndicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure)) |
| 141 | .thenReturn(mDisclosure); |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 142 | when(mIndicationArea.findViewById(R.id.keyguard_indication_text)).thenReturn(mTextView); |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 143 | when(mDisclosure.getAlpha()).thenReturn(1f); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 144 | |
| 145 | mWakeLock = new WakeLockFake(); |
Dave Mankoff | c0d9a7d | 2020-02-27 17:15:52 -0500 | [diff] [blame] | 146 | mWakeLockBuilder = new WakeLockFake.Builder(mContext); |
| 147 | mWakeLockBuilder.setWakeLock(mWakeLock); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | private void createController() { |
| 151 | if (Looper.myLooper() == null) { |
| 152 | Looper.prepare(); |
| 153 | } |
Dave Mankoff | c0d9a7d | 2020-02-27 17:15:52 -0500 | [diff] [blame] | 154 | |
| 155 | mController = new KeyguardIndicationController(mContext, mWakeLockBuilder, |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 156 | mKeyguardStateController, mStatusBarStateController, mKeyguardUpdateMonitor, |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 157 | mDockManager, mBroadcastDispatcher, mDevicePolicyManager, mIBatteryStats); |
Dave Mankoff | c0d9a7d | 2020-02-27 17:15:52 -0500 | [diff] [blame] | 158 | mController.setIndicationArea(mIndicationArea); |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 159 | mController.setStatusBarKeyguardViewManager(mStatusBarKeyguardViewManager); |
Lucas Dupin | 9131b28 | 2019-11-19 16:25:03 -0800 | [diff] [blame] | 160 | clearInvocations(mIBatteryStats); |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 161 | verify(mDisclosure).getAlpha(); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Test |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 165 | public void createController_addsAlignmentListener() { |
| 166 | createController(); |
| 167 | |
| 168 | verify(mDockManager).addAlignmentStateListener( |
| 169 | any(DockManager.AlignmentStateListener.class)); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void onAlignmentStateChanged_showsSlowChargingIndication() { |
Wilson Wu | 0e72713 | 2019-12-26 16:17:56 +0800 | [diff] [blame] | 174 | mInstrumentation.runOnMainSync(() -> { |
| 175 | createController(); |
| 176 | verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); |
| 177 | mController.setVisible(true); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 178 | |
Wilson Wu | 0e72713 | 2019-12-26 16:17:56 +0800 | [diff] [blame] | 179 | mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_POOR); |
| 180 | }); |
| 181 | mInstrumentation.waitForIdleSync(); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 182 | |
| 183 | assertThat(mTextView.getText()).isEqualTo( |
| 184 | mContext.getResources().getString(R.string.dock_alignment_slow_charging)); |
| 185 | assertThat(mTextView.getCurrentTextColor()).isEqualTo( |
| 186 | Utils.getColorError(mContext).getDefaultColor()); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void onAlignmentStateChanged_showsNotChargingIndication() { |
Wilson Wu | 0e72713 | 2019-12-26 16:17:56 +0800 | [diff] [blame] | 191 | mInstrumentation.runOnMainSync(() -> { |
| 192 | createController(); |
| 193 | verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); |
| 194 | mController.setVisible(true); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 195 | |
Wilson Wu | 0e72713 | 2019-12-26 16:17:56 +0800 | [diff] [blame] | 196 | mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_TERRIBLE); |
| 197 | }); |
| 198 | mInstrumentation.waitForIdleSync(); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 199 | |
| 200 | assertThat(mTextView.getText()).isEqualTo( |
| 201 | mContext.getResources().getString(R.string.dock_alignment_not_charging)); |
| 202 | assertThat(mTextView.getCurrentTextColor()).isEqualTo( |
| 203 | Utils.getColorError(mContext).getDefaultColor()); |
| 204 | } |
| 205 | |
| 206 | @Test |
| 207 | public void onAlignmentStateChanged_whileDozing_showsSlowChargingIndication() { |
Wilson Wu | 0e72713 | 2019-12-26 16:17:56 +0800 | [diff] [blame] | 208 | mInstrumentation.runOnMainSync(() -> { |
| 209 | createController(); |
| 210 | verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); |
| 211 | mController.setVisible(true); |
| 212 | mController.setDozing(true); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 213 | |
Wilson Wu | 0e72713 | 2019-12-26 16:17:56 +0800 | [diff] [blame] | 214 | mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_POOR); |
| 215 | }); |
| 216 | mInstrumentation.waitForIdleSync(); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 217 | |
| 218 | assertThat(mTextView.getText()).isEqualTo( |
| 219 | mContext.getResources().getString(R.string.dock_alignment_slow_charging)); |
Lucas Dupin | 5567b89 | 2020-04-16 14:05:35 -0700 | [diff] [blame] | 220 | assertThat(mTextView.getCurrentTextColor()).isEqualTo( |
Wilson Wu | bb51d09 | 2020-04-21 16:00:59 +0800 | [diff] [blame] | 221 | mContext.getColor(R.color.misalignment_text_color)); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public void onAlignmentStateChanged_whileDozing_showsNotChargingIndication() { |
Wilson Wu | 0e72713 | 2019-12-26 16:17:56 +0800 | [diff] [blame] | 226 | mInstrumentation.runOnMainSync(() -> { |
| 227 | createController(); |
| 228 | verify(mDockManager).addAlignmentStateListener(mAlignmentListener.capture()); |
| 229 | mController.setVisible(true); |
| 230 | mController.setDozing(true); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 231 | |
Wilson Wu | 0e72713 | 2019-12-26 16:17:56 +0800 | [diff] [blame] | 232 | mAlignmentListener.getValue().onAlignmentStateChanged(DockManager.ALIGN_STATE_TERRIBLE); |
| 233 | }); |
| 234 | mInstrumentation.waitForIdleSync(); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 235 | |
| 236 | assertThat(mTextView.getText()).isEqualTo( |
| 237 | mContext.getResources().getString(R.string.dock_alignment_not_charging)); |
Lucas Dupin | 5567b89 | 2020-04-16 14:05:35 -0700 | [diff] [blame] | 238 | assertThat(mTextView.getCurrentTextColor()).isEqualTo( |
Wilson Wu | bb51d09 | 2020-04-21 16:00:59 +0800 | [diff] [blame] | 239 | mContext.getColor(R.color.misalignment_text_color)); |
Jerry Chang | 6a8fb02 | 2019-11-15 06:52:54 +0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Test |
Jeff DeCew | a87cddb | 2020-06-12 12:13:52 -0400 | [diff] [blame] | 243 | public void disclosure_unmanaged() { |
| 244 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 245 | createController(); |
| 246 | |
| 247 | verify(mDisclosure).setVisibility(View.GONE); |
| 248 | verifyNoMoreInteractions(mDisclosure); |
| 249 | } |
| 250 | |
| 251 | @Test |
| 252 | public void disclosure_managedNoOwnerName() { |
| 253 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 254 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); |
| 255 | createController(); |
| 256 | |
| 257 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 258 | verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); |
| 259 | verifyNoMoreInteractions(mDisclosure); |
| 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void disclosure_hiddenWhenDozing() { |
| 264 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 265 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); |
| 266 | createController(); |
| 267 | |
| 268 | mController.setVisible(true); |
| 269 | mController.onDozeAmountChanged(1, 1); |
| 270 | mController.setDozing(true); |
| 271 | |
| 272 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 273 | verify(mDisclosure).setAlpha(0f); |
| 274 | verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); |
| 275 | verifyNoMoreInteractions(mDisclosure); |
| 276 | } |
| 277 | |
| 278 | @Test |
| 279 | public void disclosure_visibleWhenDozing() { |
| 280 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 281 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); |
| 282 | createController(); |
| 283 | |
| 284 | mController.setVisible(true); |
| 285 | mController.onDozeAmountChanged(0, 0); |
| 286 | mController.setDozing(false); |
| 287 | |
| 288 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 289 | verify(mDisclosure).setAlpha(1f); |
| 290 | verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); |
| 291 | verifyNoMoreInteractions(mDisclosure); |
| 292 | } |
| 293 | |
| 294 | @Test |
| 295 | public void disclosure_managedOwnerName() { |
| 296 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 297 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME); |
| 298 | createController(); |
| 299 | |
| 300 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 301 | verify(mDisclosure).switchIndication(mDisclosureWithOrganization); |
| 302 | verifyNoMoreInteractions(mDisclosure); |
| 303 | } |
| 304 | |
| 305 | @Test |
| 306 | public void disclosure_updateOnTheFly() { |
| 307 | ArgumentCaptor<BroadcastReceiver> receiver = ArgumentCaptor.forClass( |
| 308 | BroadcastReceiver.class); |
| 309 | doNothing().when(mBroadcastDispatcher).registerReceiver(receiver.capture(), any()); |
| 310 | |
| 311 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 312 | createController(); |
| 313 | |
| 314 | final KeyguardUpdateMonitorCallback monitor = mController.getKeyguardCallback(); |
| 315 | reset(mDisclosure); |
| 316 | |
| 317 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 318 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); |
| 319 | receiver.getValue().onReceive(mContext, new Intent()); |
| 320 | |
| 321 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 322 | verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); |
| 323 | verifyNoMoreInteractions(mDisclosure); |
| 324 | reset(mDisclosure); |
| 325 | |
| 326 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 327 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME); |
| 328 | receiver.getValue().onReceive(mContext, new Intent()); |
| 329 | |
| 330 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 331 | verify(mDisclosure).switchIndication(mDisclosureWithOrganization); |
| 332 | verifyNoMoreInteractions(mDisclosure); |
| 333 | reset(mDisclosure); |
| 334 | |
| 335 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 336 | receiver.getValue().onReceive(mContext, new Intent()); |
| 337 | |
| 338 | verify(mDisclosure).setVisibility(View.GONE); |
| 339 | verifyNoMoreInteractions(mDisclosure); |
| 340 | } |
| 341 | |
| 342 | @Test |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 343 | public void transientIndication_holdsWakeLock_whenDozing() { |
| 344 | createController(); |
| 345 | |
| 346 | mController.setDozing(true); |
| 347 | mController.showTransientIndication("Test"); |
| 348 | |
| 349 | assertTrue(mWakeLock.isHeld()); |
| 350 | } |
| 351 | |
| 352 | @Test |
| 353 | public void transientIndication_releasesWakeLock_afterHiding() { |
| 354 | createController(); |
| 355 | |
| 356 | mController.setDozing(true); |
| 357 | mController.showTransientIndication("Test"); |
| 358 | mController.hideTransientIndication(); |
| 359 | |
| 360 | assertFalse(mWakeLock.isHeld()); |
| 361 | } |
| 362 | |
| 363 | @Test |
| 364 | public void transientIndication_releasesWakeLock_afterHidingDelayed() throws Throwable { |
| 365 | mInstrumentation.runOnMainSync(() -> { |
| 366 | createController(); |
| 367 | |
| 368 | mController.setDozing(true); |
| 369 | mController.showTransientIndication("Test"); |
| 370 | mController.hideTransientIndicationDelayed(0); |
| 371 | }); |
| 372 | mInstrumentation.waitForIdleSync(); |
| 373 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 374 | Boolean[] held = new Boolean[1]; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 375 | mInstrumentation.runOnMainSync(() -> { |
| 376 | held[0] = mWakeLock.isHeld(); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 377 | }); |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 378 | assertFalse("WakeLock expected: RELEASED, was: HELD", held[0]); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 379 | } |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 380 | |
| 381 | @Test |
| 382 | public void transientIndication_visibleWhenDozing() { |
| 383 | createController(); |
| 384 | |
| 385 | mController.setVisible(true); |
| 386 | mController.showTransientIndication("Test"); |
| 387 | mController.setDozing(true); |
| 388 | |
| 389 | assertThat(mTextView.getText()).isEqualTo("Test"); |
| 390 | assertThat(mTextView.getCurrentTextColor()).isEqualTo(Color.WHITE); |
| 391 | assertThat(mTextView.getAlpha()).isEqualTo(1f); |
| 392 | } |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 393 | |
| 394 | @Test |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 395 | public void transientIndication_visibleWhenDozing_unlessSwipeUp_fromHelp() { |
| 396 | createController(); |
| 397 | String message = "A message"; |
| 398 | |
| 399 | mController.setVisible(true); |
| 400 | mController.getKeyguardCallback().onBiometricHelp( |
| 401 | KeyguardUpdateMonitor.BIOMETRIC_HELP_FACE_NOT_RECOGNIZED, message, |
| 402 | BiometricSourceType.FACE); |
| 403 | assertThat(mTextView.getText()).isEqualTo(message); |
| 404 | mController.setDozing(true); |
| 405 | |
| 406 | assertThat(mTextView.getText()).isNotEqualTo(message); |
| 407 | } |
| 408 | |
| 409 | @Test |
| 410 | public void transientIndication_visibleWhenDozing_unlessSwipeUp_fromError() { |
| 411 | createController(); |
| 412 | String message = mContext.getString(R.string.keyguard_unlock); |
| 413 | |
| 414 | mController.setVisible(true); |
| 415 | mController.getKeyguardCallback().onBiometricError(FaceManager.FACE_ERROR_TIMEOUT, |
| 416 | "A message", BiometricSourceType.FACE); |
| 417 | |
| 418 | assertThat(mTextView.getText()).isEqualTo(message); |
| 419 | mController.setDozing(true); |
| 420 | |
| 421 | assertThat(mTextView.getText()).isNotEqualTo(message); |
| 422 | } |
| 423 | |
| 424 | @Test |
| 425 | public void transientIndication_swipeUpToRetry() { |
| 426 | createController(); |
| 427 | String message = mContext.getString(R.string.keyguard_retry); |
| 428 | when(mStatusBarKeyguardViewManager.isBouncerShowing()).thenReturn(true); |
| 429 | |
| 430 | mController.setVisible(true); |
| 431 | mController.getKeyguardCallback().onBiometricError(FaceManager.FACE_ERROR_TIMEOUT, |
| 432 | "A message", BiometricSourceType.FACE); |
| 433 | |
| 434 | verify(mStatusBarKeyguardViewManager).showBouncerMessage(eq(message), any()); |
| 435 | } |
| 436 | |
| 437 | @Test |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 438 | public void updateMonitor_listenerUpdatesIndication() { |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 439 | createController(); |
| 440 | String restingIndication = "Resting indication"; |
Lucas Dupin | 85ea420 | 2019-11-04 16:01:51 -0800 | [diff] [blame] | 441 | reset(mKeyguardUpdateMonitor); |
Lucas Dupin | 6c6d573 | 2019-08-27 17:36:05 -0700 | [diff] [blame] | 442 | |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 443 | mController.setVisible(true); |
Lucas Dupin | 6c6d573 | 2019-08-27 17:36:05 -0700 | [diff] [blame] | 444 | assertThat(mTextView.getText()).isEqualTo( |
| 445 | mContext.getString(com.android.internal.R.string.lockscreen_storage_locked)); |
| 446 | |
| 447 | when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(true); |
| 448 | when(mKeyguardUpdateMonitor.isUserUnlocked(anyInt())).thenReturn(true); |
| 449 | mController.setRestingIndication(restingIndication); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 450 | assertThat(mTextView.getText()).isEqualTo(mController.getTrustGrantedIndication()); |
| 451 | |
| 452 | reset(mKeyguardUpdateMonitor); |
Lucas Dupin | 6c6d573 | 2019-08-27 17:36:05 -0700 | [diff] [blame] | 453 | when(mKeyguardUpdateMonitor.isUserUnlocked(anyInt())).thenReturn(true); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 454 | when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(false); |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 455 | mController.onUnlockedChanged(); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 456 | assertThat(mTextView.getText()).isEqualTo(restingIndication); |
| 457 | } |
| 458 | |
| 459 | @Test |
Lucas Dupin | 9131b28 | 2019-11-19 16:25:03 -0800 | [diff] [blame] | 460 | public void onRefreshBatteryInfo_computesChargingTime() throws RemoteException { |
| 461 | createController(); |
| 462 | BatteryStatus status = new BatteryStatus(BatteryManager.BATTERY_STATUS_CHARGING, |
| 463 | 80 /* level */, BatteryManager.BATTERY_PLUGGED_WIRELESS, 100 /* health */, |
| 464 | 0 /* maxChargingWattage */); |
| 465 | |
| 466 | mController.getKeyguardCallback().onRefreshBatteryInfo(status); |
| 467 | verify(mIBatteryStats).computeChargeTimeRemaining(); |
| 468 | } |
| 469 | |
| 470 | @Test |
| 471 | public void onRefreshBatteryInfo_computesChargingTime_onlyWhenCharging() |
| 472 | throws RemoteException { |
| 473 | createController(); |
| 474 | BatteryStatus status = new BatteryStatus(BatteryManager.BATTERY_STATUS_CHARGING, |
| 475 | 80 /* level */, 0 /* plugged */, 100 /* health */, |
| 476 | 0 /* maxChargingWattage */); |
| 477 | |
| 478 | mController.getKeyguardCallback().onRefreshBatteryInfo(status); |
| 479 | verify(mIBatteryStats, never()).computeChargeTimeRemaining(); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Regression test. |
| 484 | * We should not make calls to the system_process when updating the doze state. |
| 485 | */ |
| 486 | @Test |
| 487 | public void setDozing_noIBatteryCalls() throws RemoteException { |
| 488 | createController(); |
| 489 | mController.setVisible(true); |
| 490 | mController.setDozing(true); |
| 491 | mController.setDozing(false); |
| 492 | verify(mIBatteryStats, never()).computeChargeTimeRemaining(); |
| 493 | } |
| 494 | |
| 495 | @Test |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 496 | public void updateMonitor_listener() { |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 497 | createController(); |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame] | 498 | verify(mKeyguardStateController).addCallback(eq(mController)); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 499 | verify(mStatusBarStateController).addCallback(eq(mController)); |
| 500 | verify(mKeyguardUpdateMonitor, times(2)).registerCallback(any()); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 501 | } |
Scott Warner | 778db33 | 2020-01-21 09:45:56 -0500 | [diff] [blame] | 502 | |
| 503 | @Test |
| 504 | public void unlockMethodCache_listenerUpdatesPluggedIndication() { |
| 505 | createController(); |
| 506 | when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(true); |
| 507 | mController.setPowerPluggedIn(true); |
| 508 | mController.setVisible(true); |
| 509 | String powerIndication = mController.computePowerIndication(); |
| 510 | String pluggedIndication = mContext.getString(R.string.keyguard_indication_trust_unlocked); |
| 511 | pluggedIndication = mContext.getString( |
| 512 | R.string.keyguard_indication_trust_unlocked_plugged_in, |
| 513 | pluggedIndication, powerIndication); |
| 514 | assertThat(mTextView.getText()).isEqualTo(pluggedIndication); |
| 515 | } |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 516 | } |