2 * Copyright (C) 2017 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.
17 package com.android.settings.accounts;
20 import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
21 import static com.google.common.truth.Truth.assertThat;
22 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
23 import static org.mockito.Matchers.anyInt;
24 import static org.mockito.Mockito.when;
26 import android.app.Fragment;
27 import android.content.Context;
28 import android.content.pm.UserInfo;
29 import android.os.UserHandle;
30 import android.os.UserManager;
32 import com.android.settings.SettingsRobolectricTestRunner;
33 import com.android.settings.TestConfig;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.annotation.Config;
42 import java.util.ArrayList;
43 import java.util.List;
45 @RunWith(SettingsRobolectricTestRunner.class)
46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
47 public class AutoSyncWorkDataPreferenceControllerTest {
49 @Mock(answer = RETURNS_DEEP_STUBS)
50 private UserManager mUserManager;
51 @Mock(answer = RETURNS_DEEP_STUBS)
52 private Fragment mFragment;
54 private Context mContext;
56 private AutoSyncWorkDataPreferenceController mController;
60 MockitoAnnotations.initMocks(this);
61 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
63 mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment);
67 public void checkIsAvailable_managedProfile_shouldNotDisplay() {
68 when(mUserManager.isManagedProfile()).thenReturn(true);
70 assertThat(mController.isAvailable()).isFalse();
74 public void checkIsAvailable_linkedUser_shouldNotDisplay() {
75 when(mUserManager.isManagedProfile()).thenReturn(false);
76 when(mUserManager.isLinkedUser()).thenReturn(true);
78 assertThat(mController.isAvailable()).isFalse();
82 public void checkIsAvailable_singleUserProfile_shouldNotDisplay() {
83 final List<UserInfo> infos = new ArrayList<>();
84 infos.add(new UserInfo(1, "user 1", 0));
85 when(mUserManager.isManagedProfile()).thenReturn(false);
86 when(mUserManager.isLinkedUser()).thenReturn(false);
87 when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
89 assertThat(mController.isAvailable()).isFalse();
93 public void multipleProfile_shouldInitWithWorkProfileUserHandle() {
96 final UserInfo managedUser = new UserInfo(id2, "user 2", FLAG_MANAGED_PROFILE);
97 final List<UserHandle> infos = new ArrayList<>();
98 infos.add(new UserHandle(id1));
99 infos.add(new UserHandle(id2));
100 when(mUserManager.getUserProfiles()).thenReturn(infos);
101 when(mUserManager.getUserHandle()).thenReturn(id1);
102 when(mUserManager.getUserInfo(id2)).thenReturn(managedUser);
104 mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment);
106 assertThat(mController.mUserHandle.getIdentifier()).isEqualTo(id2);