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 17 package com.android.settings.users; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 21 import static org.mockito.Matchers.eq; 22 import static org.mockito.Mockito.when; 23 24 import android.app.Fragment; 25 import android.content.Context; 26 import android.content.pm.UserInfo; 27 import android.os.UserHandle; 28 import android.os.UserManager; 29 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 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 38 import java.util.ArrayList; 39 import java.util.List; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 public class AutoSyncWorkDataPreferenceControllerTest { 43 44 private static final int MANAGED_PROFILE_ID = 10; 45 46 @Mock(answer = RETURNS_DEEP_STUBS) 47 private UserManager mUserManager; 48 @Mock(answer = RETURNS_DEEP_STUBS) 49 private Fragment mFragment; 50 @Mock 51 private Context mContext; 52 53 private AutoSyncWorkDataPreferenceController mController; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 59 60 mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment); 61 } 62 63 @Test 64 public void checkIsAvailable_managedProfile_shouldNotDisplay() { 65 when(mUserManager.isManagedProfile()).thenReturn(true); 66 67 assertThat(mController.isAvailable()).isFalse(); 68 } 69 70 @Test 71 public void checkIsAvailable_linkedUser_shouldNotDisplay() { 72 when(mUserManager.isManagedProfile()).thenReturn(false); 73 when(mUserManager.isRestrictedProfile()).thenReturn(true); 74 75 assertThat(mController.isAvailable()).isFalse(); 76 } 77 78 @Test 79 public void checkIsAvailable_singleUserProfile_shouldNotDisplay() { 80 when(mUserManager.isManagedProfile()).thenReturn(false); 81 when(mUserManager.isRestrictedProfile()).thenReturn(false); 82 83 final List<UserInfo> infos = new ArrayList<>(); 84 infos.add(new UserInfo(UserHandle.USER_SYSTEM, "user 1", 0 /* flags */)); 85 when(mUserManager.getProfiles(eq(UserHandle.USER_SYSTEM))).thenReturn(infos); 86 87 assertThat(mController.isAvailable()).isFalse(); 88 } 89 90 @Test 91 public void checkIsAvailable_null_workProfileUserHandle_shouldNotDisplay() { 92 when(mUserManager.isManagedProfile()).thenReturn(false); 93 when(mUserManager.isRestrictedProfile()).thenReturn(false); 94 95 final List<UserInfo> infos = new ArrayList<>(); 96 infos.add(new UserInfo(UserHandle.USER_SYSTEM, "user 1", 0 /* flags */)); 97 infos.add(new UserInfo(999, "xspace", 800010)); 98 when(mUserManager.getProfiles(eq(UserHandle.USER_SYSTEM))).thenReturn(infos); 99 mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment); 100 101 assertThat(mController.mUserHandle).isEqualTo(null); 102 assertThat(mController.isAvailable()).isFalse(); 103 } 104 105 @Test 106 public void multipleProfile_shouldInitWithWorkProfileUserHandle() { 107 when(mUserManager.isManagedProfile()).thenReturn(false); 108 when(mUserManager.isRestrictedProfile()).thenReturn(false); 109 110 final List<UserInfo> infos = new ArrayList<>(); 111 infos.add(new UserInfo(UserHandle.USER_SYSTEM, "user 1", 0 /* flags */)); 112 infos.add(new UserInfo( 113 MANAGED_PROFILE_ID, "work profile", UserInfo.FLAG_MANAGED_PROFILE)); 114 when(mUserManager.getProfiles(eq(UserHandle.USER_SYSTEM))).thenReturn(infos); 115 116 mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment); 117 118 assertThat(mController.mUserHandle.getIdentifier()).isEqualTo(MANAGED_PROFILE_ID); 119 assertThat(mController.isAvailable()).isTrue(); 120 } 121 } 122