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