• 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.Context;
25 import android.content.DialogInterface;
26 import android.content.pm.UserInfo;
27 import android.os.UserManager;
28 
29 import androidx.fragment.app.Fragment;
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 import androidx.preference.SwitchPreference;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.shadows.ShadowApplication;
42 
43 import java.util.ArrayList;
44 import java.util.List;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class AutoSyncDataPreferenceControllerTest {
48 
49     @Mock(answer = RETURNS_DEEP_STUBS)
50     private PreferenceScreen mScreen;
51     @Mock(answer = RETURNS_DEEP_STUBS)
52     private UserManager mUserManager;
53     @Mock
54     private Fragment mFragment;
55 
56     private Preference mPreference;
57     private Context mContext;
58     private AutoSyncDataPreferenceController mController;
59     private AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment mConfirmSyncFragment;
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         ShadowApplication shadowContext = ShadowApplication.getInstance();
65         shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
66         mContext = RuntimeEnvironment.application;
67         mController = new AutoSyncDataPreferenceController(mContext, mFragment);
68         mConfirmSyncFragment = new AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment();
69         mConfirmSyncFragment.setTargetFragment(mFragment, 0);
70         mPreference = new Preference(mContext);
71         mPreference.setKey(mController.getPreferenceKey());
72         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
73     }
74 
75     @Test
displayPref_managedProfile_shouldNotDisplay()76     public void displayPref_managedProfile_shouldNotDisplay() {
77         when(mUserManager.isManagedProfile()).thenReturn(true);
78 
79         mController.displayPreference(mScreen);
80 
81         assertThat(mPreference.isVisible()).isFalse();
82     }
83 
84     @Test
displayPref_linkedUser_shouldDisplay()85     public void displayPref_linkedUser_shouldDisplay() {
86         when(mUserManager.isManagedProfile()).thenReturn(false);
87         when(mUserManager.isRestrictedProfile()).thenReturn(true);
88 
89         mController.displayPreference(mScreen);
90 
91         assertThat(mPreference.isVisible()).isTrue();
92     }
93 
94     @Test
displayPref_oneProfile_shouldDisplay()95     public void displayPref_oneProfile_shouldDisplay() {
96         List<UserInfo> infos = new ArrayList<>();
97         infos.add(new UserInfo(1, "user 1", 0));
98         when(mUserManager.isManagedProfile()).thenReturn(false);
99         when(mUserManager.isRestrictedProfile()).thenReturn(false);
100         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
101 
102         mController.displayPreference(mScreen);
103 
104         assertThat(mPreference.isVisible()).isTrue();
105     }
106 
107     @Test
displayPref_moreThanOneProfile_shouldNotDisplay()108     public void displayPref_moreThanOneProfile_shouldNotDisplay() {
109         List<UserInfo> infos = new ArrayList<>();
110         infos.add(new UserInfo(1, "user 1", 0));
111         infos.add(new UserInfo(2, "user 2", 0));
112         when(mUserManager.isManagedProfile()).thenReturn(false);
113         when(mUserManager.isRestrictedProfile()).thenReturn(false);
114         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
115 
116         mController.displayPreference(mScreen);
117 
118         assertThat(mPreference.isVisible()).isFalse();
119     }
120 
121     @Test
autoSyncData_shouldNotBeSetOnCancel()122     public void autoSyncData_shouldNotBeSetOnCancel() {
123         final Context context = RuntimeEnvironment.application;
124         final SwitchPreference preference = new SwitchPreference(context);
125         preference.setChecked(false);
126         mController = new AutoSyncDataPreferenceController(context, mFragment);
127         mConfirmSyncFragment.mPreference = preference;
128         mConfirmSyncFragment.mEnabling = true;
129 
130         mConfirmSyncFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
131         assertThat(preference.isChecked()).isFalse();
132     }
133 }
134