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; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.eq; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 26 | import static org.mockito.Mockito.mock; |
| 27 | import static org.mockito.Mockito.reset; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.times; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 31 | import static org.mockito.Mockito.when; |
| 32 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 33 | import android.app.Instrumentation; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 34 | import android.app.admin.DevicePolicyManager; |
| 35 | import android.app.trust.TrustManager; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 36 | import android.content.Context; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 37 | import android.graphics.Color; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 38 | import android.hardware.fingerprint.FingerprintManager; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 39 | import android.os.Looper; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 40 | import android.view.View; |
| 41 | import android.view.ViewGroup; |
| 42 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 43 | import androidx.test.InstrumentationRegistry; |
| 44 | import androidx.test.filters.SmallTest; |
| 45 | import androidx.test.runner.AndroidJUnit4; |
| 46 | |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 47 | import com.android.internal.widget.LockPatternUtils; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 48 | import com.android.keyguard.KeyguardUpdateMonitor; |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 49 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 50 | import com.android.systemui.R; |
| 51 | import com.android.systemui.SysuiTestCase; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 52 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 53 | import com.android.systemui.statusbar.phone.KeyguardIndicationTextView; |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 54 | import com.android.systemui.statusbar.phone.LockIcon; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 55 | import com.android.systemui.statusbar.phone.ShadeController; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 56 | import com.android.systemui.statusbar.phone.UnlockMethodCache; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 57 | import com.android.systemui.statusbar.policy.AccessibilityController; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 58 | import com.android.systemui.util.wakelock.WakeLockFake; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 59 | |
| 60 | import org.junit.Before; |
| 61 | import org.junit.Test; |
| 62 | import org.junit.runner.RunWith; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 63 | import org.mockito.ArgumentCaptor; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 64 | import org.mockito.Mock; |
| 65 | import org.mockito.MockitoAnnotations; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 66 | |
| 67 | @SmallTest |
| 68 | @RunWith(AndroidJUnit4.class) |
| 69 | public class KeyguardIndicationControllerTest extends SysuiTestCase { |
| 70 | |
| 71 | private final String ORGANIZATION_NAME = "organization"; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 72 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 73 | private String mDisclosureWithOrganization; |
| 74 | |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 75 | @Mock |
| 76 | private DevicePolicyManager mDevicePolicyManager; |
| 77 | @Mock |
| 78 | private ViewGroup mIndicationArea; |
| 79 | @Mock |
| 80 | private KeyguardIndicationTextView mDisclosure; |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 81 | @Mock |
| 82 | private LockIcon mLockIcon; |
| 83 | @Mock |
| 84 | private LockPatternUtils mLockPatternUtils; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 85 | @Mock |
| 86 | private ShadeController mShadeController; |
| 87 | @Mock |
| 88 | private AccessibilityController mAccessibilityController; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 89 | @Mock |
| 90 | private UnlockMethodCache mUnlockMethodCache; |
| 91 | @Mock |
| 92 | private StatusBarStateController mStatusBarStateController; |
| 93 | @Mock |
| 94 | private KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 95 | private KeyguardIndicationTextView mTextView; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 96 | |
| 97 | private KeyguardIndicationController mController; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 98 | private WakeLockFake mWakeLock; |
| 99 | private Instrumentation mInstrumentation; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 100 | |
| 101 | @Before |
| 102 | public void setUp() throws Exception { |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 103 | MockitoAnnotations.initMocks(this); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 104 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 105 | mTextView = new KeyguardIndicationTextView(mContext); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 106 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 107 | mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager); |
| 108 | mContext.addMockSystemService(Context.TRUST_SERVICE, mock(TrustManager.class)); |
| 109 | mContext.addMockSystemService(Context.FINGERPRINT_SERVICE, mock(FingerprintManager.class)); |
| 110 | mDisclosureWithOrganization = mContext.getString(R.string.do_disclosure_with_name, |
| 111 | ORGANIZATION_NAME); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 112 | |
| 113 | when(mIndicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure)) |
| 114 | .thenReturn(mDisclosure); |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 115 | when(mIndicationArea.findViewById(R.id.keyguard_indication_text)).thenReturn(mTextView); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 116 | |
| 117 | mWakeLock = new WakeLockFake(); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private void createController() { |
| 121 | if (Looper.myLooper() == null) { |
| 122 | Looper.prepare(); |
| 123 | } |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 124 | mController = new KeyguardIndicationController(mContext, mIndicationArea, mLockIcon, |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 125 | mLockPatternUtils, mWakeLock, mShadeController, mAccessibilityController, |
| 126 | mUnlockMethodCache, mStatusBarStateController, mKeyguardUpdateMonitor); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void unmanaged() { |
| 131 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 132 | createController(); |
| 133 | |
| 134 | verify(mDisclosure).setVisibility(View.GONE); |
| 135 | verifyNoMoreInteractions(mDisclosure); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void managedNoOwnerName() { |
| 140 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 141 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); |
| 142 | createController(); |
| 143 | |
| 144 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 145 | verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); |
| 146 | verifyNoMoreInteractions(mDisclosure); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void managedOwnerName() { |
| 151 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 152 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME); |
| 153 | createController(); |
| 154 | |
| 155 | verify(mDisclosure).setVisibility(View.VISIBLE); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 156 | verify(mDisclosure).switchIndication(mDisclosureWithOrganization); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 157 | verifyNoMoreInteractions(mDisclosure); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void updateOnTheFly() { |
| 162 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 163 | createController(); |
| 164 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 165 | final KeyguardUpdateMonitorCallback monitor = mController.getKeyguardCallback(); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 166 | reset(mDisclosure); |
| 167 | |
| 168 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 169 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); |
| 170 | monitor.onKeyguardVisibilityChanged(true); |
| 171 | |
| 172 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 173 | verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); |
| 174 | verifyNoMoreInteractions(mDisclosure); |
| 175 | reset(mDisclosure); |
| 176 | |
| 177 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 178 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME); |
| 179 | monitor.onKeyguardVisibilityChanged(false); |
| 180 | monitor.onKeyguardVisibilityChanged(true); |
| 181 | |
| 182 | verify(mDisclosure).setVisibility(View.VISIBLE); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 183 | verify(mDisclosure).switchIndication(mDisclosureWithOrganization); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 184 | verifyNoMoreInteractions(mDisclosure); |
| 185 | reset(mDisclosure); |
| 186 | |
| 187 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 188 | monitor.onKeyguardVisibilityChanged(false); |
| 189 | monitor.onKeyguardVisibilityChanged(true); |
| 190 | |
| 191 | verify(mDisclosure).setVisibility(View.GONE); |
| 192 | verifyNoMoreInteractions(mDisclosure); |
| 193 | } |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 194 | |
| 195 | @Test |
| 196 | public void transientIndication_holdsWakeLock_whenDozing() { |
| 197 | createController(); |
| 198 | |
| 199 | mController.setDozing(true); |
| 200 | mController.showTransientIndication("Test"); |
| 201 | |
| 202 | assertTrue(mWakeLock.isHeld()); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void transientIndication_releasesWakeLock_afterHiding() { |
| 207 | createController(); |
| 208 | |
| 209 | mController.setDozing(true); |
| 210 | mController.showTransientIndication("Test"); |
| 211 | mController.hideTransientIndication(); |
| 212 | |
| 213 | assertFalse(mWakeLock.isHeld()); |
| 214 | } |
| 215 | |
| 216 | @Test |
| 217 | public void transientIndication_releasesWakeLock_afterHidingDelayed() throws Throwable { |
| 218 | mInstrumentation.runOnMainSync(() -> { |
| 219 | createController(); |
| 220 | |
| 221 | mController.setDozing(true); |
| 222 | mController.showTransientIndication("Test"); |
| 223 | mController.hideTransientIndicationDelayed(0); |
| 224 | }); |
| 225 | mInstrumentation.waitForIdleSync(); |
| 226 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 227 | Boolean[] held = new Boolean[1]; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 228 | mInstrumentation.runOnMainSync(() -> { |
| 229 | held[0] = mWakeLock.isHeld(); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 230 | }); |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 231 | assertFalse("WakeLock expected: RELEASED, was: HELD", held[0]); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 232 | } |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 233 | |
| 234 | @Test |
| 235 | public void transientIndication_visibleWhenDozing() { |
| 236 | createController(); |
| 237 | |
| 238 | mController.setVisible(true); |
| 239 | mController.showTransientIndication("Test"); |
| 240 | mController.setDozing(true); |
| 241 | |
| 242 | assertThat(mTextView.getText()).isEqualTo("Test"); |
| 243 | assertThat(mTextView.getCurrentTextColor()).isEqualTo(Color.WHITE); |
| 244 | assertThat(mTextView.getAlpha()).isEqualTo(1f); |
| 245 | } |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 246 | |
| 247 | @Test |
| 248 | public void lockIcon_click() { |
| 249 | createController(); |
| 250 | |
| 251 | ArgumentCaptor<View.OnLongClickListener> longClickCaptor = ArgumentCaptor.forClass( |
| 252 | View.OnLongClickListener.class); |
| 253 | ArgumentCaptor<View.OnClickListener> clickCaptor = ArgumentCaptor.forClass( |
| 254 | View.OnClickListener.class); |
| 255 | verify(mLockIcon).setOnLongClickListener(longClickCaptor.capture()); |
| 256 | verify(mLockIcon).setOnClickListener(clickCaptor.capture()); |
| 257 | |
| 258 | when(mAccessibilityController.isAccessibilityEnabled()).thenReturn(true); |
| 259 | clickCaptor.getValue().onClick(mLockIcon); |
Lucas Dupin | d54f00e | 2019-05-06 17:44:03 -0700 | [diff] [blame] | 260 | verify(mShadeController).animateCollapsePanels(anyInt(), eq(true)); |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 261 | |
| 262 | longClickCaptor.getValue().onLongClick(mLockIcon); |
| 263 | verify(mLockPatternUtils).requireCredentialEntry(anyInt()); |
Lucas Dupin | ca88e5f | 2019-05-14 16:11:08 -0700 | [diff] [blame] | 264 | verify(mKeyguardUpdateMonitor).onLockIconPressed(); |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 265 | } |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 266 | |
| 267 | @Test |
| 268 | public void unlockMethodCache_listenerUpdatesIndication() { |
| 269 | createController(); |
| 270 | String restingIndication = "Resting indication"; |
| 271 | when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(true); |
| 272 | mController.setRestingIndication(restingIndication); |
| 273 | mController.setVisible(true); |
| 274 | assertThat(mTextView.getText()).isEqualTo(mController.getTrustGrantedIndication()); |
| 275 | |
| 276 | reset(mKeyguardUpdateMonitor); |
| 277 | when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(false); |
| 278 | mController.onUnlockMethodStateChanged(); |
| 279 | assertThat(mTextView.getText()).isEqualTo(restingIndication); |
| 280 | } |
| 281 | |
| 282 | @Test |
| 283 | public void unlockMethodCache_listener() { |
| 284 | createController(); |
| 285 | verify(mUnlockMethodCache).addListener(eq(mController)); |
| 286 | verify(mStatusBarStateController).addCallback(eq(mController)); |
| 287 | verify(mKeyguardUpdateMonitor, times(2)).registerCallback(any()); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 288 | } |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 289 | } |