KeyguardIndicationControllerTest.java revision 5f045007e0cb98d3bc0e6d9a0f0c88e307f2834b
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 android.app.admin.DevicePolicyManager;
20import android.app.trust.TrustManager;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.pm.PackageManager;
24import android.content.res.Resources;
25import android.os.Looper;
26import android.support.test.runner.AndroidJUnit4;
27import android.telephony.SubscriptionManager;
28import android.test.suitebuilder.annotation.SmallTest;
29import android.view.View;
30import android.view.ViewGroup;
31
32import com.android.keyguard.KeyguardUpdateMonitor;
33import com.android.systemui.R;
34import com.android.systemui.SysuiTestCase;
35import com.android.systemui.statusbar.phone.KeyguardIndicationTextView;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.ArgumentCaptor;
41
42import static org.mockito.Mockito.mock;
43import static org.mockito.Mockito.reset;
44import static org.mockito.Mockito.verify;
45import static org.mockito.Mockito.verifyNoMoreInteractions;
46import static org.mockito.Mockito.when;
47
48@SmallTest
49@RunWith(AndroidJUnit4.class)
50public class KeyguardIndicationControllerTest extends SysuiTestCase {
51
52    private final String ORGANIZATION_NAME = "organization";
53    private final String DISCLOSURE_WITH_ORGANIZATION_NAME = "managed by organization";
54
55    private Context mContext = mock(Context.class);
56    private DevicePolicyManager mDevicePolicyManager = mock(DevicePolicyManager.class);
57    private ViewGroup mIndicationArea = mock(ViewGroup.class);
58    private KeyguardIndicationTextView mDisclosure = mock(KeyguardIndicationTextView.class);
59
60    private KeyguardIndicationController mController;
61
62    @Before
63    public void setUp() throws Exception {
64        final Resources resources = mock(Resources.class);
65        when(mContext.getResources()).thenReturn(resources);
66        when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
67        when(mContext.getPackageManager()).thenReturn(mock(PackageManager.class));
68        when(mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)).thenReturn(
69                mock(SubscriptionManager.class));
70        when(mContext.getSystemService(Context.TRUST_SERVICE)).thenReturn(
71                mock(TrustManager.class));
72        when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(
73                mDevicePolicyManager);
74
75        when(resources.getString(R.string.do_disclosure_with_name, ORGANIZATION_NAME))
76                .thenReturn(DISCLOSURE_WITH_ORGANIZATION_NAME);
77
78        when(mIndicationArea.findViewById(R.id.keyguard_indication_enterprise_disclosure))
79                .thenReturn(mDisclosure);
80    }
81
82    private void createController() {
83        if (Looper.myLooper() == null) {
84            Looper.prepare();
85        }
86        mController = new KeyguardIndicationController(mContext, mIndicationArea, null);
87    }
88
89    @Test
90    public void unmanaged() {
91        when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
92        createController();
93
94        verify(mDisclosure).setVisibility(View.GONE);
95        verifyNoMoreInteractions(mDisclosure);
96    }
97
98    @Test
99    public void managedNoOwnerName() {
100        when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
101        when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
102        createController();
103
104        verify(mDisclosure).setVisibility(View.VISIBLE);
105        verify(mDisclosure).switchIndication(R.string.do_disclosure_generic);
106        verifyNoMoreInteractions(mDisclosure);
107    }
108
109    @Test
110    public void managedOwnerName() {
111        when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
112        when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME);
113        createController();
114
115        verify(mDisclosure).setVisibility(View.VISIBLE);
116        verify(mDisclosure).switchIndication(DISCLOSURE_WITH_ORGANIZATION_NAME);
117        verifyNoMoreInteractions(mDisclosure);
118    }
119
120    @Test
121    public void updateOnTheFly() {
122        when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
123        createController();
124
125        final KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
126        reset(mDisclosure);
127
128        when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
129        when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null);
130        monitor.onKeyguardVisibilityChanged(true);
131
132        verify(mDisclosure).setVisibility(View.VISIBLE);
133        verify(mDisclosure).switchIndication(R.string.do_disclosure_generic);
134        verifyNoMoreInteractions(mDisclosure);
135        reset(mDisclosure);
136
137        when(mDevicePolicyManager.isDeviceManaged()).thenReturn(true);
138        when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(ORGANIZATION_NAME);
139        monitor.onKeyguardVisibilityChanged(false);
140        monitor.onKeyguardVisibilityChanged(true);
141
142        verify(mDisclosure).setVisibility(View.VISIBLE);
143        verify(mDisclosure).switchIndication(DISCLOSURE_WITH_ORGANIZATION_NAME);
144        verifyNoMoreInteractions(mDisclosure);
145        reset(mDisclosure);
146
147        when(mDevicePolicyManager.isDeviceManaged()).thenReturn(false);
148        monitor.onKeyguardVisibilityChanged(false);
149        monitor.onKeyguardVisibilityChanged(true);
150
151        verify(mDisclosure).setVisibility(View.GONE);
152        verifyNoMoreInteractions(mDisclosure);
153    }
154}
155