KeyguardIndicationControllerTest.java revision c1b50324a2286b24b691b8a7190743cbc341727e
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 17package com.android.systemui.statusbar; 18 19import static android.support.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread; 20 21import static org.junit.Assert.assertFalse; 22import static org.junit.Assert.assertTrue; 23import 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 29import android.app.Instrumentation; 30import android.app.admin.DevicePolicyManager; 31import android.app.trust.TrustManager; 32import android.content.Context; 33import android.hardware.fingerprint.FingerprintManager; 34import android.os.Looper; 35import android.support.test.InstrumentationRegistry; 36import android.support.test.filters.SmallTest; 37import android.support.test.internal.runner.junit4.statement.UiThreadStatement; 38import android.support.test.runner.AndroidJUnit4; 39import android.view.View; 40import android.view.ViewGroup; 41 42import com.android.keyguard.KeyguardUpdateMonitor; 43import com.android.systemui.R; 44import com.android.systemui.SysuiTestCase; 45import com.android.systemui.statusbar.phone.KeyguardIndicationTextView; 46import com.android.systemui.util.wakelock.WakeLockFake; 47 48import org.junit.Before; 49import org.junit.Test; 50import org.junit.runner.RunWith; 51 52@SmallTest 53@RunWith(AndroidJUnit4.class) 54public class KeyguardIndicationControllerTest extends SysuiTestCase { 55 56 private final String ORGANIZATION_NAME = "organization"; 57 58 private String mDisclosureWithOrganization; 59 60 private DevicePolicyManager mDevicePolicyManager = mock(DevicePolicyManager.class); 61 private ViewGroup mIndicationArea = mock(ViewGroup.class); 62 private KeyguardIndicationTextView mDisclosure = mock(KeyguardIndicationTextView.class); 63 64 private KeyguardIndicationController mController; 65 private WakeLockFake mWakeLock; 66 private Instrumentation mInstrumentation; 67 68 @Before 69 public void setUp() throws Exception { 70 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 71 72 mContext.addMockSystemService(Context.DEVICE_POLICY_SERVICE, mDevicePolicyManager); 73 mContext.addMockSystemService(Context.TRUST_SERVICE, mock(TrustManager.class)); 74 mContext.addMockSystemService(Context.FINGERPRINT_SERVICE, mock(FingerprintManager.class)); 75 mDisclosureWithOrganization = mContext.getString(R.string.do_disclosure_with_name, 76 ORGANIZATION_NAME); 77 78 when(mIndicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure)) 79 .thenReturn(mDisclosure); 80 81 mWakeLock = new WakeLockFake(); 82 } 83 84 private void createController() { 85 if (Looper.myLooper() == null) { 86 Looper.prepare(); 87 } 88 mController = new KeyguardIndicationController(mContext, mIndicationArea, null, mWakeLock); 89 } 90 91 @Test 92 public void unmanaged() { 93 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); 94 createController(); 95 96 verify(mDisclosure).setVisibility(View.GONE); 97 verifyNoMoreInteractions(mDisclosure); 98 } 99 100 @Test 101 public void managedNoOwnerName() { 102 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); 103 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); 104 createController(); 105 106 verify(mDisclosure).setVisibility(View.VISIBLE); 107 verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); 108 verifyNoMoreInteractions(mDisclosure); 109 } 110 111 @Test 112 public void managedOwnerName() { 113 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); 114 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME); 115 createController(); 116 117 verify(mDisclosure).setVisibility(View.VISIBLE); 118 verify(mDisclosure).switchIndication(mDisclosureWithOrganization); 119 verifyNoMoreInteractions(mDisclosure); 120 } 121 122 @Test 123 public void updateOnTheFly() { 124 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); 125 createController(); 126 127 final KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext); 128 reset(mDisclosure); 129 130 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); 131 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); 132 monitor.onKeyguardVisibilityChanged(true); 133 134 verify(mDisclosure).setVisibility(View.VISIBLE); 135 verify(mDisclosure).switchIndication(R.string.do_disclosure_generic); 136 verifyNoMoreInteractions(mDisclosure); 137 reset(mDisclosure); 138 139 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true); 140 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME); 141 monitor.onKeyguardVisibilityChanged(false); 142 monitor.onKeyguardVisibilityChanged(true); 143 144 verify(mDisclosure).setVisibility(View.VISIBLE); 145 verify(mDisclosure).switchIndication(mDisclosureWithOrganization); 146 verifyNoMoreInteractions(mDisclosure); 147 reset(mDisclosure); 148 149 when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false); 150 monitor.onKeyguardVisibilityChanged(false); 151 monitor.onKeyguardVisibilityChanged(true); 152 153 verify(mDisclosure).setVisibility(View.GONE); 154 verifyNoMoreInteractions(mDisclosure); 155 } 156 157 @Test 158 public void transientIndication_holdsWakeLock_whenDozing() { 159 createController(); 160 161 mController.setDozing(true); 162 mController.showTransientIndication("Test"); 163 164 assertTrue(mWakeLock.isHeld()); 165 } 166 167 @Test 168 public void transientIndication_releasesWakeLock_afterHiding() { 169 createController(); 170 171 mController.setDozing(true); 172 mController.showTransientIndication("Test"); 173 mController.hideTransientIndication(); 174 175 assertFalse(mWakeLock.isHeld()); 176 } 177 178 @Test 179 public void transientIndication_releasesWakeLock_afterHidingDelayed() throws Throwable { 180 mInstrumentation.runOnMainSync(() -> { 181 createController(); 182 183 mController.setDozing(true); 184 mController.showTransientIndication("Test"); 185 mController.hideTransientIndicationDelayed(0); 186 }); 187 mInstrumentation.waitForIdleSync(); 188 189 boolean[] held = new boolean[2]; 190 mInstrumentation.runOnMainSync(() -> { 191 held[0] = mWakeLock.isHeld(); 192 held[1] = true; 193 }); 194 assertFalse("wake lock still held", held[0]); 195 assertTrue("held was not written yet", held[1]); 196 } 197} 198