blob: 18ffbb5062a7985dfb69986e5527b57439925761 [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
Lucas Dupinff6628d2018-10-15 10:12:37 -070019import static com.google.common.truth.Truth.assertThat;
20
Adrian Roosc1b50322017-02-27 21:07:58 +010021import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertTrue;
Jason Monk3cfedd72016-12-09 09:31:37 -050023import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.reset;
25import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.verifyNoMoreInteractions;
27import static org.mockito.Mockito.when;
28
Adrian Roosc1b50322017-02-27 21:07:58 +010029import android.app.Instrumentation;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010030import android.app.admin.DevicePolicyManager;
31import android.app.trust.TrustManager;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010032import android.content.Context;
Lucas Dupinff6628d2018-10-15 10:12:37 -070033import android.graphics.Color;
Jason Monk3cfedd72016-12-09 09:31:37 -050034import android.hardware.fingerprint.FingerprintManager;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010035import android.os.Looper;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010036import android.view.View;
37import android.view.ViewGroup;
38
Brett Chabot84151d92019-02-27 15:37:59 -080039import androidx.test.InstrumentationRegistry;
40import androidx.test.filters.SmallTest;
41import androidx.test.runner.AndroidJUnit4;
42
Adrian Roosaf45b602017-03-14 13:10:25 -070043import com.android.keyguard.KeyguardUpdateMonitorCallback;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010044import com.android.systemui.R;
45import com.android.systemui.SysuiTestCase;
46import com.android.systemui.statusbar.phone.KeyguardIndicationTextView;
Adrian Roosc1b50322017-02-27 21:07:58 +010047import com.android.systemui.util.wakelock.WakeLockFake;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010048
49import org.junit.Before;
50import org.junit.Test;
51import org.junit.runner.RunWith;
Lucas Dupinff6628d2018-10-15 10:12:37 -070052import org.mockito.Mock;
53import org.mockito.MockitoAnnotations;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010054
55@SmallTest
56@RunWith(AndroidJUnit4.class)
57public class KeyguardIndicationControllerTest extends SysuiTestCase {
58
59 private final String ORGANIZATION_NAME = "organization";
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010060
Jason Monk3cfedd72016-12-09 09:31:37 -050061 private String mDisclosureWithOrganization;
62
Lucas Dupinff6628d2018-10-15 10:12:37 -070063 @Mock
64 private DevicePolicyManager mDevicePolicyManager;
65 @Mock
66 private ViewGroup mIndicationArea;
67 @Mock
68 private KeyguardIndicationTextView mDisclosure;
69 private KeyguardIndicationTextView mTextView;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010070
71 private KeyguardIndicationController mController;
Adrian Roosc1b50322017-02-27 21:07:58 +010072 private WakeLockFake mWakeLock;
73 private Instrumentation mInstrumentation;
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010074
75 @Before
76 public void setUp() throws Exception {
Lucas Dupinff6628d2018-10-15 10:12:37 -070077 MockitoAnnotations.initMocks(this);
Adrian Roosc1b50322017-02-27 21:07:58 +010078 mInstrumentation = InstrumentationRegistry.getInstrumentation();
Lucas Dupinff6628d2018-10-15 10:12:37 -070079 mTextView = new KeyguardIndicationTextView(mContext);
Adrian Roosc1b50322017-02-27 21:07:58 +010080
Jason Monk3cfedd72016-12-09 09:31:37 -050081 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 Fabianowski5f045002016-12-01 10:36:18 +010086
87 when(mIndicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure))
88 .thenReturn(mDisclosure);
Lucas Dupinff6628d2018-10-15 10:12:37 -070089 when(mIndicationArea.findViewById(R.id.keyguard_indication_text)).thenReturn(mTextView);
Adrian Roosc1b50322017-02-27 21:07:58 +010090
91 mWakeLock = new WakeLockFake();
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010092 }
93
94 private void createController() {
95 if (Looper.myLooper() == null) {
96 Looper.prepare();
97 }
Adrian Roosc1b50322017-02-27 21:07:58 +010098 mController = new KeyguardIndicationController(mContext, mIndicationArea, null, mWakeLock);
Bartosz Fabianowski5f045002016-12-01 10:36:18 +010099 }
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 Monk3cfedd72016-12-09 09:31:37 -0500128 verify(mDisclosure).switchIndication(mDisclosureWithOrganization);
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100129 verifyNoMoreInteractions(mDisclosure);
130 }
131
132 @Test
133 public void updateOnTheFly() {
134 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
135 createController();
136
Adrian Roosaf45b602017-03-14 13:10:25 -0700137 final KeyguardUpdateMonitorCallback monitor = mController.getKeyguardCallback();
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100138 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 Monk3cfedd72016-12-09 09:31:37 -0500155 verify(mDisclosure).switchIndication(mDisclosureWithOrganization);
Bartosz Fabianowski5f045002016-12-01 10:36:18 +0100156 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 Roosc1b50322017-02-27 21:07:58 +0100166
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 Roosaf45b602017-03-14 13:10:25 -0700199 Boolean[] held = new Boolean[1];
Adrian Roosc1b50322017-02-27 21:07:58 +0100200 mInstrumentation.runOnMainSync(() -> {
201 held[0] = mWakeLock.isHeld();
Adrian Roosc1b50322017-02-27 21:07:58 +0100202 });
Adrian Roosaf45b602017-03-14 13:10:25 -0700203 assertFalse("WakeLock expected: RELEASED, was: HELD", held[0]);
Adrian Roosc1b50322017-02-27 21:07:58 +0100204 }
Lucas Dupinff6628d2018-10-15 10:12:37 -0700205
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 Fabianowski5f045002016-12-01 10:36:18 +0100218}