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.pm.UserInfo;
26 import android.os.UserManager;
27 
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceFragmentCompat;
30 import androidx.preference.PreferenceScreen;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.shadows.ShadowApplication;
40 
41 import java.util.ArrayList;
42 import java.util.List;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class AutoSyncPersonalDataPreferenceControllerTest {
46 
47     @Mock(answer = RETURNS_DEEP_STUBS)
48     private PreferenceScreen mScreen;
49     @Mock(answer = RETURNS_DEEP_STUBS)
50     private UserManager mUserManager;
51     @Mock(answer = RETURNS_DEEP_STUBS)
52     private PreferenceFragmentCompat mFragment;
53 
54     private Context mContext;
55     private Preference mPreference;
56     private AutoSyncPersonalDataPreferenceController mController;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61         mContext = RuntimeEnvironment.application;
62         ShadowApplication shadowContext = ShadowApplication.getInstance();
63         shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
64         mController = new AutoSyncPersonalDataPreferenceController(mContext, mFragment);
65         mPreference = new Preference(mContext);
66         mPreference.setKey(mController.getPreferenceKey());
67         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
68     }
69 
70     @Test
displayPref_managedProfile_shouldNotDisplay()71     public void displayPref_managedProfile_shouldNotDisplay() {
72         when(mUserManager.isManagedProfile()).thenReturn(true);
73 
74         mController.displayPreference(mScreen);
75 
76         assertThat(mPreference.isVisible()).isFalse();
77     }
78 
79     @Test
displayPref_linkedUser_shouldNotDisplay()80     public void displayPref_linkedUser_shouldNotDisplay() {
81         when(mUserManager.isManagedProfile()).thenReturn(false);
82         when(mUserManager.isRestrictedProfile()).thenReturn(true);
83 
84         mController.displayPreference(mScreen);
85 
86         assertThat(mPreference.isVisible()).isFalse();
87     }
88 
89     @Test
displayPref_oneProfile_shouldNotDisplay()90     public void displayPref_oneProfile_shouldNotDisplay() {
91         List<UserInfo> infos = new ArrayList<>();
92         infos.add(new UserInfo(1, "user 1", 0));
93         when(mUserManager.isManagedProfile()).thenReturn(false);
94         when(mUserManager.isRestrictedProfile()).thenReturn(false);
95         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
96 
97         mController.displayPreference(mScreen);
98 
99         assertThat(mPreference.isVisible()).isFalse();
100     }
101 
102     @Test
displayPref_prefAvailable_shouldDisplay()103     public void displayPref_prefAvailable_shouldDisplay() {
104         List<UserInfo> infos = new ArrayList<>();
105         infos.add(new UserInfo(1, "user 1", 0));
106         infos.add(new UserInfo(2, "user 2", 0));
107         when(mUserManager.isManagedProfile()).thenReturn(false);
108         when(mUserManager.isRestrictedProfile()).thenReturn(false);
109         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
110 
111         mController.displayPreference(mScreen);
112 
113         assertThat(mPreference.isVisible()).isTrue();
114     }
115 }
116