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; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 30 | import static org.mockito.Mockito.when; |
| 31 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 32 | import android.app.Instrumentation; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 33 | import android.app.admin.DevicePolicyManager; |
| 34 | import android.app.trust.TrustManager; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 35 | import android.content.Context; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 36 | import android.graphics.Color; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 37 | import android.hardware.fingerprint.FingerprintManager; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 38 | import android.os.Looper; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 39 | import android.view.View; |
| 40 | import android.view.ViewGroup; |
| 41 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 42 | import androidx.test.InstrumentationRegistry; |
| 43 | import androidx.test.filters.SmallTest; |
| 44 | import androidx.test.runner.AndroidJUnit4; |
| 45 | |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 46 | import com.android.internal.widget.LockPatternUtils; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 47 | import com.android.keyguard.KeyguardUpdateMonitor; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 48 | import com.android.systemui.R; |
| 49 | import com.android.systemui.SysuiTestCase; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 50 | import com.android.systemui.plugins.statusbar.StatusBarStateController; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 51 | import com.android.systemui.statusbar.phone.KeyguardIndicationTextView; |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 52 | import com.android.systemui.statusbar.phone.LockIcon; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 53 | import com.android.systemui.statusbar.phone.ShadeController; |
| 54 | import com.android.systemui.statusbar.policy.AccessibilityController; |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame^] | 55 | import com.android.systemui.statusbar.policy.KeyguardStateController; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 56 | import com.android.systemui.util.wakelock.WakeLockFake; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 57 | |
| 58 | import org.junit.Before; |
| 59 | import org.junit.Test; |
| 60 | import org.junit.runner.RunWith; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 61 | import org.mockito.ArgumentCaptor; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 62 | import org.mockito.Mock; |
| 63 | import org.mockito.MockitoAnnotations; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 64 | |
| 65 | @SmallTest |
| 66 | @RunWith(AndroidJUnit4.class) |
| 67 | public class KeyguardIndicationControllerTest extends SysuiTestCase { |
| 68 | |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 69 | @Mock |
| 70 | private DevicePolicyManager mDevicePolicyManager; |
| 71 | @Mock |
| 72 | private ViewGroup mIndicationArea; |
| 73 | @Mock |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 74 | private LockIcon mLockIcon; |
| 75 | @Mock |
| 76 | private LockPatternUtils mLockPatternUtils; |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 77 | @Mock |
| 78 | private ShadeController mShadeController; |
| 79 | @Mock |
| 80 | private AccessibilityController mAccessibilityController; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 81 | @Mock |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame^] | 82 | private KeyguardStateController mKeyguardStateController; |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 83 | @Mock |
| 84 | private StatusBarStateController mStatusBarStateController; |
| 85 | @Mock |
| 86 | private KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 87 | private KeyguardIndicationTextView mTextView; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 88 | |
| 89 | private KeyguardIndicationController mController; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 90 | private WakeLockFake mWakeLock; |
| 91 | private Instrumentation mInstrumentation; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 92 | |
| 93 | @Before |
| 94 | public void setUp() throws Exception { |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 95 | MockitoAnnotations.initMocks(this); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 96 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 97 | mTextView = new KeyguardIndicationTextView(mContext); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 98 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 99 | mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager); |
| 100 | mContext.addMockSystemService(Context.TRUST_SERVICE, mock(TrustManager.class)); |
| 101 | mContext.addMockSystemService(Context.FINGERPRINT_SERVICE, mock(FingerprintManager.class)); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 102 | |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 103 | when(mIndicationArea.findViewById(R.id.keyguard_indication_text)).thenReturn(mTextView); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 104 | |
| 105 | mWakeLock = new WakeLockFake(); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private void createController() { |
| 109 | if (Looper.myLooper() == null) { |
| 110 | Looper.prepare(); |
| 111 | } |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 112 | mController = new KeyguardIndicationController(mContext, mIndicationArea, mLockIcon, |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 113 | mLockPatternUtils, mWakeLock, mShadeController, mAccessibilityController, |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame^] | 114 | mKeyguardStateController, mStatusBarStateController, mKeyguardUpdateMonitor); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Test |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 118 | public void transientIndication_holdsWakeLock_whenDozing() { |
| 119 | createController(); |
| 120 | |
| 121 | mController.setDozing(true); |
| 122 | mController.showTransientIndication("Test"); |
| 123 | |
| 124 | assertTrue(mWakeLock.isHeld()); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void transientIndication_releasesWakeLock_afterHiding() { |
| 129 | createController(); |
| 130 | |
| 131 | mController.setDozing(true); |
| 132 | mController.showTransientIndication("Test"); |
| 133 | mController.hideTransientIndication(); |
| 134 | |
| 135 | assertFalse(mWakeLock.isHeld()); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void transientIndication_releasesWakeLock_afterHidingDelayed() throws Throwable { |
| 140 | mInstrumentation.runOnMainSync(() -> { |
| 141 | createController(); |
| 142 | |
| 143 | mController.setDozing(true); |
| 144 | mController.showTransientIndication("Test"); |
| 145 | mController.hideTransientIndicationDelayed(0); |
| 146 | }); |
| 147 | mInstrumentation.waitForIdleSync(); |
| 148 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 149 | Boolean[] held = new Boolean[1]; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 150 | mInstrumentation.runOnMainSync(() -> { |
| 151 | held[0] = mWakeLock.isHeld(); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 152 | }); |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 153 | assertFalse("WakeLock expected: RELEASED, was: HELD", held[0]); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 154 | } |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 155 | |
| 156 | @Test |
| 157 | public void transientIndication_visibleWhenDozing() { |
| 158 | createController(); |
| 159 | |
| 160 | mController.setVisible(true); |
| 161 | mController.showTransientIndication("Test"); |
| 162 | mController.setDozing(true); |
| 163 | |
| 164 | assertThat(mTextView.getText()).isEqualTo("Test"); |
| 165 | assertThat(mTextView.getCurrentTextColor()).isEqualTo(Color.WHITE); |
| 166 | assertThat(mTextView.getAlpha()).isEqualTo(1f); |
| 167 | } |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 168 | |
| 169 | @Test |
| 170 | public void lockIcon_click() { |
| 171 | createController(); |
| 172 | |
| 173 | ArgumentCaptor<View.OnLongClickListener> longClickCaptor = ArgumentCaptor.forClass( |
| 174 | View.OnLongClickListener.class); |
| 175 | ArgumentCaptor<View.OnClickListener> clickCaptor = ArgumentCaptor.forClass( |
| 176 | View.OnClickListener.class); |
| 177 | verify(mLockIcon).setOnLongClickListener(longClickCaptor.capture()); |
| 178 | verify(mLockIcon).setOnClickListener(clickCaptor.capture()); |
| 179 | |
| 180 | when(mAccessibilityController.isAccessibilityEnabled()).thenReturn(true); |
| 181 | clickCaptor.getValue().onClick(mLockIcon); |
Lucas Dupin | d54f00e | 2019-05-06 17:44:03 -0700 | [diff] [blame] | 182 | verify(mShadeController).animateCollapsePanels(anyInt(), eq(true)); |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 183 | |
| 184 | longClickCaptor.getValue().onLongClick(mLockIcon); |
| 185 | verify(mLockPatternUtils).requireCredentialEntry(anyInt()); |
Lucas Dupin | ca88e5f | 2019-05-14 16:11:08 -0700 | [diff] [blame] | 186 | verify(mKeyguardUpdateMonitor).onLockIconPressed(); |
Lucas Dupin | 0df60fe | 2019-04-23 10:19:27 -0700 | [diff] [blame] | 187 | } |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 188 | |
| 189 | @Test |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame^] | 190 | public void updateMonitor_listenerUpdatesIndication() { |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 191 | createController(); |
| 192 | String restingIndication = "Resting indication"; |
Lucas Dupin | 6c6d573 | 2019-08-27 17:36:05 -0700 | [diff] [blame] | 193 | |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 194 | mController.setVisible(true); |
Lucas Dupin | 6c6d573 | 2019-08-27 17:36:05 -0700 | [diff] [blame] | 195 | assertThat(mTextView.getText()).isEqualTo( |
| 196 | mContext.getString(com.android.internal.R.string.lockscreen_storage_locked)); |
| 197 | |
| 198 | when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(true); |
| 199 | when(mKeyguardUpdateMonitor.isUserUnlocked(anyInt())).thenReturn(true); |
| 200 | mController.setRestingIndication(restingIndication); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 201 | assertThat(mTextView.getText()).isEqualTo(mController.getTrustGrantedIndication()); |
| 202 | |
| 203 | reset(mKeyguardUpdateMonitor); |
Lucas Dupin | 6c6d573 | 2019-08-27 17:36:05 -0700 | [diff] [blame] | 204 | when(mKeyguardUpdateMonitor.isUserUnlocked(anyInt())).thenReturn(true); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 205 | when(mKeyguardUpdateMonitor.getUserHasTrust(anyInt())).thenReturn(false); |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame^] | 206 | mController.onUnlockedChanged(); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 207 | assertThat(mTextView.getText()).isEqualTo(restingIndication); |
| 208 | } |
| 209 | |
| 210 | @Test |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame^] | 211 | public void updateMonitor_listener() { |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 212 | createController(); |
Lucas Dupin | c8f16e8 | 2019-09-17 18:24:50 -0400 | [diff] [blame^] | 213 | verify(mKeyguardStateController).addCallback(eq(mController)); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 214 | verify(mStatusBarStateController).addCallback(eq(mController)); |
| 215 | verify(mKeyguardUpdateMonitor, times(2)).registerCallback(any()); |
Lucas Dupin | 8e2fd01 | 2019-04-25 16:40:54 -0700 | [diff] [blame] | 216 | } |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 217 | } |