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; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.reset; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 29 | import android.app.Instrumentation; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 30 | import android.app.admin.DevicePolicyManager; |
| 31 | import android.app.trust.TrustManager; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 32 | import android.content.Context; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 33 | import android.graphics.Color; |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 34 | import android.hardware.fingerprint.FingerprintManager; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 35 | import android.os.Looper; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 36 | import android.view.View; |
| 37 | import android.view.ViewGroup; |
| 38 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 39 | import androidx.test.InstrumentationRegistry; |
| 40 | import androidx.test.filters.SmallTest; |
| 41 | import androidx.test.runner.AndroidJUnit4; |
| 42 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 43 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 44 | import com.android.systemui.R; |
| 45 | import com.android.systemui.SysuiTestCase; |
| 46 | import com.android.systemui.statusbar.phone.KeyguardIndicationTextView; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 47 | import com.android.systemui.util.wakelock.WakeLockFake; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 48 | |
| 49 | import org.junit.Before; |
| 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 52 | import org.mockito.Mock; |
| 53 | import org.mockito.MockitoAnnotations; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 54 | |
| 55 | @SmallTest |
| 56 | @RunWith(AndroidJUnit4.class) |
| 57 | public class KeyguardIndicationControllerTest extends SysuiTestCase { |
| 58 | |
| 59 | private final String ORGANIZATION_NAME = "organization"; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 60 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 61 | private String mDisclosureWithOrganization; |
| 62 | |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 63 | @Mock |
| 64 | private DevicePolicyManager mDevicePolicyManager; |
| 65 | @Mock |
| 66 | private ViewGroup mIndicationArea; |
| 67 | @Mock |
| 68 | private KeyguardIndicationTextView mDisclosure; |
| 69 | private KeyguardIndicationTextView mTextView; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 70 | |
| 71 | private KeyguardIndicationController mController; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 72 | private WakeLockFake mWakeLock; |
| 73 | private Instrumentation mInstrumentation; |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 74 | |
| 75 | @Before |
| 76 | public void setUp() throws Exception { |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 77 | MockitoAnnotations.initMocks(this); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 78 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 79 | mTextView = new KeyguardIndicationTextView(mContext); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 80 | |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 81 | mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager); |
| 82 | mContext.addMockSystemService(Context.TRUST_SERVICE, mock(TrustManager.class)); |
| 83 | mContext.addMockSystemService(Context.FINGERPRINT_SERVICE, mock(FingerprintManager.class)); |
| 84 | mDisclosureWithOrganization = mContext.getString(R.string.do_disclosure_with_name, |
| 85 | ORGANIZATION_NAME); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 86 | |
| 87 | when(mIndicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure)) |
| 88 | .thenReturn(mDisclosure); |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 89 | when(mIndicationArea.findViewById(R.id.keyguard_indication_text)).thenReturn(mTextView); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 90 | |
| 91 | mWakeLock = new WakeLockFake(); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private void createController() { |
| 95 | if (Looper.myLooper() == null) { |
| 96 | Looper.prepare(); |
| 97 | } |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 98 | mController = new KeyguardIndicationController(mContext, mIndicationArea, null, mWakeLock); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void unmanaged() { |
| 103 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 104 | createController(); |
| 105 | |
| 106 | verify(mDisclosure).setVisibility(View.GONE); |
| 107 | verifyNoMoreInteractions(mDisclosure); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void managedNoOwnerName() { |
| 112 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 113 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); |
| 114 | createController(); |
| 115 | |
| 116 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 117 | verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); |
| 118 | verifyNoMoreInteractions(mDisclosure); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void managedOwnerName() { |
| 123 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 124 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME); |
| 125 | createController(); |
| 126 | |
| 127 | verify(mDisclosure).setVisibility(View.VISIBLE); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 128 | verify(mDisclosure).switchIndication(mDisclosureWithOrganization); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 129 | verifyNoMoreInteractions(mDisclosure); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void updateOnTheFly() { |
| 134 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 135 | createController(); |
| 136 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 137 | final KeyguardUpdateMonitorCallback monitor = mController.getKeyguardCallback(); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 138 | reset(mDisclosure); |
| 139 | |
| 140 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 141 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); |
| 142 | monitor.onKeyguardVisibilityChanged(true); |
| 143 | |
| 144 | verify(mDisclosure).setVisibility(View.VISIBLE); |
| 145 | verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); |
| 146 | verifyNoMoreInteractions(mDisclosure); |
| 147 | reset(mDisclosure); |
| 148 | |
| 149 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); |
| 150 | when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME); |
| 151 | monitor.onKeyguardVisibilityChanged(false); |
| 152 | monitor.onKeyguardVisibilityChanged(true); |
| 153 | |
| 154 | verify(mDisclosure).setVisibility(View.VISIBLE); |
Jason Monk | 3cfedd7 | 2016-12-09 09:31:37 -0500 | [diff] [blame] | 155 | verify(mDisclosure).switchIndication(mDisclosureWithOrganization); |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 156 | verifyNoMoreInteractions(mDisclosure); |
| 157 | reset(mDisclosure); |
| 158 | |
| 159 | when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); |
| 160 | monitor.onKeyguardVisibilityChanged(false); |
| 161 | monitor.onKeyguardVisibilityChanged(true); |
| 162 | |
| 163 | verify(mDisclosure).setVisibility(View.GONE); |
| 164 | verifyNoMoreInteractions(mDisclosure); |
| 165 | } |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 166 | |
| 167 | @Test |
| 168 | public void transientIndication_holdsWakeLock_whenDozing() { |
| 169 | createController(); |
| 170 | |
| 171 | mController.setDozing(true); |
| 172 | mController.showTransientIndication("Test"); |
| 173 | |
| 174 | assertTrue(mWakeLock.isHeld()); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void transientIndication_releasesWakeLock_afterHiding() { |
| 179 | createController(); |
| 180 | |
| 181 | mController.setDozing(true); |
| 182 | mController.showTransientIndication("Test"); |
| 183 | mController.hideTransientIndication(); |
| 184 | |
| 185 | assertFalse(mWakeLock.isHeld()); |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void transientIndication_releasesWakeLock_afterHidingDelayed() throws Throwable { |
| 190 | mInstrumentation.runOnMainSync(() -> { |
| 191 | createController(); |
| 192 | |
| 193 | mController.setDozing(true); |
| 194 | mController.showTransientIndication("Test"); |
| 195 | mController.hideTransientIndicationDelayed(0); |
| 196 | }); |
| 197 | mInstrumentation.waitForIdleSync(); |
| 198 | |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 199 | Boolean[] held = new Boolean[1]; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 200 | mInstrumentation.runOnMainSync(() -> { |
| 201 | held[0] = mWakeLock.isHeld(); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 202 | }); |
Adrian Roos | af45b60 | 2017-03-14 13:10:25 -0700 | [diff] [blame] | 203 | assertFalse("WakeLock expected: RELEASED, was: HELD", held[0]); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 204 | } |
Lucas Dupin | ff6628d | 2018-10-15 10:12:37 -0700 | [diff] [blame] | 205 | |
| 206 | @Test |
| 207 | public void transientIndication_visibleWhenDozing() { |
| 208 | createController(); |
| 209 | |
| 210 | mController.setVisible(true); |
| 211 | mController.showTransientIndication("Test"); |
| 212 | mController.setDozing(true); |
| 213 | |
| 214 | assertThat(mTextView.getText()).isEqualTo("Test"); |
| 215 | assertThat(mTextView.getCurrentTextColor()).isEqualTo(Color.WHITE); |
| 216 | assertThat(mTextView.getAlpha()).isEqualTo(1f); |
| 217 | } |
Bartosz Fabianowski | 5f04500 | 2016-12-01 10:36:18 +0100 | [diff] [blame] | 218 | } |