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