• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.settings.users;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.content.pm.UserInfo;
28 import android.os.UserManager;
29 
30 import androidx.preference.PreferenceFragmentCompat;
31 import androidx.preference.PreferenceScreen;
32 import androidx.preference.SwitchPreference;
33 
34 import com.android.settings.testutils.shadow.ShadowContentResolver;
35 import com.android.settings.users.AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment;
36 
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 import org.robolectric.annotation.Config;
46 import org.robolectric.shadows.ShadowApplication;
47 
48 import java.util.ArrayList;
49 import java.util.List;
50 
51 @RunWith(RobolectricTestRunner.class)
52 @Config(shadows = {ShadowContentResolver.class})
53 public class AutoSyncDataPreferenceControllerTest {
54 
55     @Mock(answer = RETURNS_DEEP_STUBS)
56     private PreferenceScreen mScreen;
57     @Mock(answer = RETURNS_DEEP_STUBS)
58     private UserManager mUserManager;
59     @Mock
60     private PreferenceFragmentCompat mFragment;
61 
62     private SwitchPreference mPreference;
63     private Context mContext;
64     private AutoSyncDataPreferenceController mController;
65 
66     @Before
setUp()67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69         ShadowApplication shadowContext = ShadowApplication.getInstance();
70         shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
71         mContext = RuntimeEnvironment.application;
72         mController = new AutoSyncDataPreferenceController(mContext, mFragment);
73         String preferenceKey = mController.getPreferenceKey();
74         mPreference = new SwitchPreference(mContext);
75         mPreference.setKey(preferenceKey);
76         mPreference.setChecked(true);
77         when(mScreen.findPreference(preferenceKey)).thenReturn(mPreference);
78         when(mFragment.findPreference(preferenceKey)).thenReturn(mPreference);
79     }
80 
81     @After
tearDown()82     public void tearDown() {
83         ShadowContentResolver.reset();
84     }
85 
86     @Test
displayPref_managedProfile_shouldNotDisplay()87     public void displayPref_managedProfile_shouldNotDisplay() {
88         when(mUserManager.isManagedProfile()).thenReturn(true);
89 
90         mController.displayPreference(mScreen);
91 
92         assertThat(mPreference.isVisible()).isFalse();
93     }
94 
95     @Test
displayPref_linkedUser_shouldDisplay()96     public void displayPref_linkedUser_shouldDisplay() {
97         when(mUserManager.isManagedProfile()).thenReturn(false);
98         when(mUserManager.isRestrictedProfile()).thenReturn(true);
99 
100         mController.displayPreference(mScreen);
101 
102         assertThat(mPreference.isVisible()).isTrue();
103     }
104 
105     @Test
displayPref_oneProfile_shouldDisplay()106     public void displayPref_oneProfile_shouldDisplay() {
107         List<UserInfo> infos = new ArrayList<>();
108         infos.add(new UserInfo(1, "user 1", 0));
109         when(mUserManager.isManagedProfile()).thenReturn(false);
110         when(mUserManager.isRestrictedProfile()).thenReturn(false);
111         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
112 
113         mController.displayPreference(mScreen);
114 
115         assertThat(mPreference.isVisible()).isTrue();
116     }
117 
118     @Test
displayPref_moreThanOneProfile_shouldNotDisplay()119     public void displayPref_moreThanOneProfile_shouldNotDisplay() {
120         List<UserInfo> infos = new ArrayList<>();
121         infos.add(new UserInfo(1, "user 1", 0));
122         infos.add(new UserInfo(2, "user 2", 0));
123         when(mUserManager.isManagedProfile()).thenReturn(false);
124         when(mUserManager.isRestrictedProfile()).thenReturn(false);
125         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
126 
127         mController.displayPreference(mScreen);
128 
129         assertThat(mPreference.isVisible()).isFalse();
130     }
131 
132     @Test
confirmDialog_uncheckThenOk_shouldUncheck()133     public void confirmDialog_uncheckThenOk_shouldUncheck() {
134         ConfirmAutoSyncChangeFragment confirmSyncFragment =
135                 ConfirmAutoSyncChangeFragment.newInstance(false, 0, mController.getPreferenceKey());
136         confirmSyncFragment.setTargetFragment(mFragment, 0);
137 
138         confirmSyncFragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
139 
140         assertThat(ContentResolver.getMasterSyncAutomaticallyAsUser(0)).isFalse();
141         assertThat(mPreference.isChecked()).isFalse();
142     }
143 
144     @Test
confirmDialog_uncheckThenCancel_shouldNotUncheck()145     public void confirmDialog_uncheckThenCancel_shouldNotUncheck() {
146         ConfirmAutoSyncChangeFragment confirmSyncFragment =
147                 ConfirmAutoSyncChangeFragment.newInstance(false, 0, mController.getPreferenceKey());
148         confirmSyncFragment.setTargetFragment(mFragment, 0);
149 
150         confirmSyncFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
151 
152         assertThat(ContentResolver.getMasterSyncAutomaticallyAsUser(0)).isTrue();
153         assertThat(mPreference.isChecked()).isTrue();
154     }
155 }
156