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