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