OSDN Git Service

7fb6fd768f12aca24c43acb230c33b4581a0e3a5
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / accounts / AutoSyncDataPreferenceControllerTest.java
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.DialogInterface;
21 import android.content.Intent;
22 import android.content.pm.UserInfo;
23 import android.os.UserManager;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceScreen;
26 import android.support.v14.preference.SwitchPreference;
27 import com.android.settings.SettingsRobolectricTestRunner;
28 import com.android.settings.TestConfig;
29
30 import java.util.ArrayList;
31 import java.util.List;
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.annotation.Config;
38 import org.robolectric.shadows.ShadowApplication;
39
40 import static com.google.common.truth.Truth.assertThat;
41 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
42 import static org.mockito.Matchers.any;
43 import static org.mockito.Matchers.anyInt;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.never;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.when;
48
49 @RunWith(SettingsRobolectricTestRunner.class)
50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
51 public class AutoSyncDataPreferenceControllerTest {
52
53     @Mock(answer = RETURNS_DEEP_STUBS)
54     private PreferenceScreen mScreen;
55     @Mock(answer = RETURNS_DEEP_STUBS)
56     private UserManager mUserManager;
57     @Mock(answer = RETURNS_DEEP_STUBS)
58     private Fragment mFragment;
59
60     private Context mContext;
61     private AutoSyncDataPreferenceController mController;
62     private AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment mConfirmSyncFragment;
63
64     @Before
65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67         ShadowApplication shadowContext = ShadowApplication.getInstance();
68         shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
69         mContext = shadowContext.getApplicationContext();
70         mController = new AutoSyncDataPreferenceController(mContext, mFragment);
71         mConfirmSyncFragment = new AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment();
72         mConfirmSyncFragment.setTargetFragment(mFragment, 0);
73     }
74
75     @Test
76     public void displayPref_managedProfile_shouldNotDisplay() {
77         when(mUserManager.isManagedProfile()).thenReturn(true);
78
79         mController.displayPreference(mScreen);
80
81         verify(mScreen).removePreference(any(Preference.class));
82     }
83
84     @Test
85     public void displayPref_linkedUser_shouldDisplay() {
86         when(mUserManager.isManagedProfile()).thenReturn(false);
87         when(mUserManager.isLinkedUser()).thenReturn(true);
88
89         mController.displayPreference(mScreen);
90
91         verify(mScreen, never()).removePreference(any(Preference.class));
92     }
93
94     @Test
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.isLinkedUser()).thenReturn(false);
100         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
101
102         mController.displayPreference(mScreen);
103
104         verify(mScreen, never()).removePreference(any(Preference.class));
105     }
106
107     @Test
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.isLinkedUser()).thenReturn(false);
114         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
115
116         mController.displayPreference(mScreen);
117
118         verify(mScreen).removePreference(any(Preference.class));
119     }
120
121     @Test
122     public void autoSyncData_shouldNotBeSetOnCancel() {
123         final ShadowApplication application = ShadowApplication.getInstance();
124         final Context context = application.getApplicationContext();
125         final SwitchPreference preference = new SwitchPreference(context);
126         preference.setChecked(false);
127         mController = new AutoSyncDataPreferenceController(context, mFragment);
128         mConfirmSyncFragment.mPreference = preference;
129         mConfirmSyncFragment.mEnabling = true;
130
131         mConfirmSyncFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
132         assertThat(preference.isChecked()).isFalse();
133     }
134
135 }