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.DialogInterface; 26 import android.content.pm.UserInfo; 27 import android.os.UserManager; 28 import android.support.v14.preference.SwitchPreference; 29 import android.support.v7.preference.Preference; 30 import android.support.v7.preference.PreferenceScreen; 31 32 import com.android.settings.testutils.SettingsRobolectricTestRunner; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.shadows.ShadowApplication; 41 42 import java.util.ArrayList; 43 import java.util.List; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 public class AutoSyncDataPreferenceControllerTest { 47 48 @Mock(answer = RETURNS_DEEP_STUBS) 49 private PreferenceScreen mScreen; 50 @Mock(answer = RETURNS_DEEP_STUBS) 51 private UserManager mUserManager; 52 @Mock 53 private Fragment mFragment; 54 55 private Preference mPreference; 56 private Context mContext; 57 private AutoSyncDataPreferenceController mController; 58 private AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment mConfirmSyncFragment; 59 60 @Before 61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 ShadowApplication shadowContext = ShadowApplication.getInstance(); 64 shadowContext.setSystemService(Context.USER_SERVICE, mUserManager); 65 mContext = shadowContext.getApplicationContext(); 66 mController = new AutoSyncDataPreferenceController(mContext, mFragment); 67 mConfirmSyncFragment = new AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment(); 68 mConfirmSyncFragment.setTargetFragment(mFragment, 0); 69 mPreference = new Preference(mContext); 70 mPreference.setKey(mController.getPreferenceKey()); 71 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 72 } 73 74 @Test 75 public void displayPref_managedProfile_shouldNotDisplay() { 76 when(mUserManager.isManagedProfile()).thenReturn(true); 77 78 mController.displayPreference(mScreen); 79 80 assertThat(mPreference.isVisible()).isFalse(); 81 } 82 83 @Test 84 public void displayPref_linkedUser_shouldDisplay() { 85 when(mUserManager.isManagedProfile()).thenReturn(false); 86 when(mUserManager.isRestrictedProfile()).thenReturn(true); 87 88 mController.displayPreference(mScreen); 89 90 assertThat(mPreference.isVisible()).isTrue(); 91 } 92 93 @Test 94 public void displayPref_oneProfile_shouldDisplay() { 95 List<UserInfo> infos = new ArrayList<>(); 96 infos.add(new UserInfo(1, "user 1", 0)); 97 when(mUserManager.isManagedProfile()).thenReturn(false); 98 when(mUserManager.isRestrictedProfile()).thenReturn(false); 99 when(mUserManager.getProfiles(anyInt())).thenReturn(infos); 100 101 mController.displayPreference(mScreen); 102 103 assertThat(mPreference.isVisible()).isTrue(); 104 } 105 106 @Test 107 public void displayPref_moreThanOneProfile_shouldNotDisplay() { 108 List<UserInfo> infos = new ArrayList<>(); 109 infos.add(new UserInfo(1, "user 1", 0)); 110 infos.add(new UserInfo(2, "user 2", 0)); 111 when(mUserManager.isManagedProfile()).thenReturn(false); 112 when(mUserManager.isRestrictedProfile()).thenReturn(false); 113 when(mUserManager.getProfiles(anyInt())).thenReturn(infos); 114 115 mController.displayPreference(mScreen); 116 117 assertThat(mPreference.isVisible()).isFalse(); 118 } 119 120 @Test 121 public void autoSyncData_shouldNotBeSetOnCancel() { 122 final Context context = RuntimeEnvironment.application; 123 final SwitchPreference preference = new SwitchPreference(context); 124 preference.setChecked(false); 125 mController = new AutoSyncDataPreferenceController(context, mFragment); 126 mConfirmSyncFragment.mPreference = preference; 127 mConfirmSyncFragment.mEnabling = true; 128 129 mConfirmSyncFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE); 130 assertThat(preference.isChecked()).isFalse(); 131 } 132 } 133