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 */ 16package com.android.settings.users; 17 18import static com.google.common.truth.Truth.assertThat; 19import static org.mockito.Answers.RETURNS_DEEP_STUBS; 20import static org.mockito.Matchers.anyInt; 21import static org.mockito.Mockito.when; 22 23import android.app.Fragment; 24import android.content.Context; 25import android.content.pm.UserInfo; 26import android.os.UserManager; 27import android.support.v7.preference.Preference; 28import android.support.v7.preference.PreferenceScreen; 29 30import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 32import org.junit.Before; 33import org.junit.Test; 34import org.junit.runner.RunWith; 35import org.mockito.Mock; 36import org.mockito.MockitoAnnotations; 37import org.robolectric.shadows.ShadowApplication; 38 39import java.util.ArrayList; 40import java.util.List; 41 42@RunWith(SettingsRobolectricTestRunner.class) 43public 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 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 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 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 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 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