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.accounts;
17 
18 import android.app.Fragment;
19 import android.content.Context;
20 import android.content.pm.UserInfo;
21 import android.os.UserManager;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceScreen;
24 import com.android.settings.SettingsRobolectricTestRunner;
25 import com.android.settings.TestConfig;
26 import com.android.settings.search.SearchIndexableRaw;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.annotation.Config;
36 import org.robolectric.shadows.ShadowApplication;
37 
38 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
39 import static org.mockito.Matchers.any;
40 import static org.mockito.Matchers.anyInt;
41 import static org.mockito.Mockito.never;
42 import static org.mockito.Mockito.verify;
43 import static org.mockito.Mockito.when;
44 
45 @RunWith(SettingsRobolectricTestRunner.class)
46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
47 public class AutoSyncPersonalDataPreferenceControllerTest {
48 
49     @Mock(answer = RETURNS_DEEP_STUBS)
50     private PreferenceScreen mScreen;
51     @Mock(answer = RETURNS_DEEP_STUBS)
52     private UserManager mUserManager;
53     @Mock(answer = RETURNS_DEEP_STUBS)
54     private Fragment mFragment;
55     @Mock
56     private Preference mPreference;
57 
58     private Context mContext;
59     private AutoSyncPersonalDataPreferenceController mController;
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 = shadowContext.getApplicationContext();
67         mController = new AutoSyncPersonalDataPreferenceController(mContext, mFragment);
68         when(mScreen.getPreferenceCount()).thenReturn(1);
69         when(mScreen.getPreference(0)).thenReturn(mPreference);
70         when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
71     }
72 
73     @Test
displayPref_managedProfile_shouldNotDisplay()74     public void displayPref_managedProfile_shouldNotDisplay() {
75         when(mUserManager.isManagedProfile()).thenReturn(true);
76 
77         mController.displayPreference(mScreen);
78 
79         verify(mScreen).removePreference(any(Preference.class));
80     }
81 
82     @Test
displayPref_linkedUser_shouldNotDisplay()83     public void displayPref_linkedUser_shouldNotDisplay() {
84         when(mUserManager.isManagedProfile()).thenReturn(false);
85         when(mUserManager.isLinkedUser()).thenReturn(true);
86 
87         mController.displayPreference(mScreen);
88 
89         verify(mScreen).removePreference(any(Preference.class));
90     }
91 
92     @Test
displayPref_oneProfile_shouldNotDisplay()93     public void displayPref_oneProfile_shouldNotDisplay() {
94         List<UserInfo> infos = new ArrayList<>();
95         infos.add(new UserInfo(1, "user 1", 0));
96         when(mUserManager.isManagedProfile()).thenReturn(false);
97         when(mUserManager.isLinkedUser()).thenReturn(false);
98         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
99 
100         mController.displayPreference(mScreen);
101 
102         verify(mScreen).removePreference(any(Preference.class));
103     }
104 
105     @Test
displayPref_prefAvaiable_shouldDisplay()106     public void displayPref_prefAvaiable_shouldDisplay() {
107         List<UserInfo> infos = new ArrayList<>();
108         infos.add(new UserInfo(1, "user 1", 0));
109         infos.add(new UserInfo(2, "user 2", 0));
110         when(mUserManager.isManagedProfile()).thenReturn(false);
111         when(mUserManager.isLinkedUser()).thenReturn(false);
112         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
113 
114         mController.displayPreference(mScreen);
115 
116         verify(mScreen, never()).removePreference(any(Preference.class));
117     }
118 
119 }
120