2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.accounts;
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;
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;
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;
49 @RunWith(SettingsRobolectricTestRunner.class)
50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
51 public class AutoSyncDataPreferenceControllerTest {
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;
60 private Context mContext;
61 private AutoSyncDataPreferenceController mController;
62 private AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment mConfirmSyncFragment;
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);
76 public void displayPref_managedProfile_shouldNotDisplay() {
77 when(mUserManager.isManagedProfile()).thenReturn(true);
79 mController.displayPreference(mScreen);
81 verify(mScreen).removePreference(any(Preference.class));
85 public void displayPref_linkedUser_shouldDisplay() {
86 when(mUserManager.isManagedProfile()).thenReturn(false);
87 when(mUserManager.isLinkedUser()).thenReturn(true);
89 mController.displayPreference(mScreen);
91 verify(mScreen, never()).removePreference(any(Preference.class));
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);
102 mController.displayPreference(mScreen);
104 verify(mScreen, never()).removePreference(any(Preference.class));
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);
116 mController.displayPreference(mScreen);
118 verify(mScreen).removePreference(any(Preference.class));
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;
131 mConfirmSyncFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
132 assertThat(preference.isChecked()).isFalse();