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