• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 package com.android.settings.accounts;
17 
18 import static android.provider.Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED;
19 
20 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
21 import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import static org.robolectric.RuntimeEnvironment.application;
32 
33 import android.app.admin.DevicePolicyManager;
34 import android.content.ComponentName;
35 import android.content.Context;
36 import android.os.UserHandle;
37 import android.provider.Settings;
38 import android.util.ArraySet;
39 
40 import com.android.settingslib.RestrictedSwitchPreference;
41 
42 import org.junit.Before;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.RobolectricTestRunner;
49 import org.robolectric.RuntimeEnvironment;
50 import org.robolectric.Shadows;
51 import org.robolectric.shadows.ShadowDevicePolicyManager;
52 
53 import java.util.Arrays;
54 import java.util.Collections;
55 
56 @RunWith(RobolectricTestRunner.class)
57 public class CrossProfileCalendarPreferenceControllerTest {
58 
59     private static final String PREF_KEY = "cross_profile_calendar";
60     private static final int MANAGED_USER_ID = 10;
61     private static final String TEST_PACKAGE_NAME = "com.test";
62     private static final ComponentName TEST_COMPONENT_NAME = new ComponentName("test", "test");
63 
64     @Mock
65     private UserHandle mManagedUser;
66 
67     private RestrictedSwitchPreference mPreference;
68     private Context mContext;
69     private CrossProfileCalendarPreferenceController mController;
70     private ShadowDevicePolicyManager dpm;
71 
72     @Before
setUp()73     public void setUp() throws Exception {
74         MockitoAnnotations.initMocks(this);
75         mContext = spy(RuntimeEnvironment.application);
76         mController = new CrossProfileCalendarPreferenceController(mContext, PREF_KEY);
77         mController.setManagedUser(mManagedUser);
78         mPreference = spy(new RestrictedSwitchPreference(mContext));
79         dpm = Shadows.shadowOf(application.getSystemService(DevicePolicyManager.class));
80 
81         when(mManagedUser.getIdentifier()).thenReturn(MANAGED_USER_ID);
82         doReturn(mContext).when(mContext).createPackageContextAsUser(
83                 any(String.class), anyInt(), any(UserHandle.class));
84     }
85 
86     @Test
getAvailabilityStatus_noManagedUser_shouldBeDisabled()87     public void getAvailabilityStatus_noManagedUser_shouldBeDisabled() {
88         mController.setManagedUser(null);
89 
90         assertThat(mController.getAvailabilityStatus())
91                 .isNotEqualTo(CrossProfileCalendarPreferenceController.AVAILABLE);
92     }
93 
94     @Test
getAvailabilityStatus_noPackageAllowed_shouldBeDisabledForUser()95     public void getAvailabilityStatus_noPackageAllowed_shouldBeDisabledForUser() throws Exception {
96         dpm.setProfileOwner(TEST_COMPONENT_NAME);
97 
98         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
99     }
100 
101     @Test
getAvailabilityStatus_somePackagesAllowed_shouldBeAvailable()102     public void getAvailabilityStatus_somePackagesAllowed_shouldBeAvailable() throws Exception {
103         dpm.setProfileOwner(TEST_COMPONENT_NAME);
104         dpm.setCrossProfileCalendarPackages(TEST_COMPONENT_NAME,
105                 Collections.singleton(TEST_PACKAGE_NAME));
106 
107         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
108     }
109 
110     @Test
getAvailabilityStatus_allPackagesAllowed_shouldBeAvailable()111     public void getAvailabilityStatus_allPackagesAllowed_shouldBeAvailable() throws Exception {
112         dpm.setProfileOwner(TEST_COMPONENT_NAME);
113         dpm.setCrossProfileCalendarPackages(TEST_COMPONENT_NAME, null);
114 
115         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
116     }
117 
118     @Test
updateStateToDisabled_isNotChecked()119     public void updateStateToDisabled_isNotChecked() {
120         Settings.Secure.putIntForUser(mContext.getContentResolver(),
121                 CROSS_PROFILE_CALENDAR_ENABLED, 0, mManagedUser.getIdentifier());
122 
123         mController.updateState(mPreference);
124         assertThat(mPreference.isChecked()).isFalse();
125     }
126 
127     @Test
updateStateToEnabled_isChecked()128     public void updateStateToEnabled_isChecked() throws Exception {
129         // Put 0 first so we know the value is not originally 1.
130         Settings.Secure.putIntForUser(mContext.getContentResolver(),
131                 CROSS_PROFILE_CALENDAR_ENABLED, 0, mManagedUser.getIdentifier());
132         mController.updateState(mPreference);
133         Settings.Secure.putIntForUser(mContext.getContentResolver(),
134                 CROSS_PROFILE_CALENDAR_ENABLED, 1, mManagedUser.getIdentifier());
135 
136         mController.updateState(mPreference);
137         assertThat(mPreference.isChecked()).isTrue();
138     }
139 
140     @Test
onPreferenceChangeToFalse_shouldUpdateProviderValue()141     public void onPreferenceChangeToFalse_shouldUpdateProviderValue() {
142         mController.onPreferenceChange(mPreference, false);
143         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
144                 CROSS_PROFILE_CALENDAR_ENABLED, 1, mManagedUser.getIdentifier()))
145                 .isEqualTo(0);
146     }
147 
148     @Test
onPreferenceChangeToTrue_shouldUpdateProviderValue()149     public void onPreferenceChangeToTrue_shouldUpdateProviderValue() {
150         // Change to false first so we know the value is not originally 1.
151         mController.onPreferenceChange(mPreference, false);
152 
153         mController.onPreferenceChange(mPreference, true);
154         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
155                 CROSS_PROFILE_CALENDAR_ENABLED, 0, mManagedUser.getIdentifier()))
156                 .isEqualTo(1);
157     }
158 }