OSDN Git Service

Use AfW user handle when setting auto_sync for work
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / accounts / AutoSyncWorkDataPreferenceControllerTest.java
1 /*
2  * Copyright (C) 2017 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.accounts;
18
19
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;
25
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;
31
32 import com.android.settings.SettingsRobolectricTestRunner;
33 import com.android.settings.TestConfig;
34
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;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 @RunWith(SettingsRobolectricTestRunner.class)
46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
47 public class AutoSyncWorkDataPreferenceControllerTest {
48
49     @Mock(answer = RETURNS_DEEP_STUBS)
50     private UserManager mUserManager;
51     @Mock(answer = RETURNS_DEEP_STUBS)
52     private Fragment mFragment;
53     @Mock
54     private Context mContext;
55
56     private AutoSyncWorkDataPreferenceController mController;
57
58     @Before
59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
62
63         mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment);
64     }
65
66     @Test
67     public void checkIsAvailable_managedProfile_shouldNotDisplay() {
68         when(mUserManager.isManagedProfile()).thenReturn(true);
69
70         assertThat(mController.isAvailable()).isFalse();
71     }
72
73     @Test
74     public void checkIsAvailable_linkedUser_shouldNotDisplay() {
75         when(mUserManager.isManagedProfile()).thenReturn(false);
76         when(mUserManager.isLinkedUser()).thenReturn(true);
77
78         assertThat(mController.isAvailable()).isFalse();
79     }
80
81     @Test
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);
88
89         assertThat(mController.isAvailable()).isFalse();
90     }
91
92     @Test
93     public void multipleProfile_shouldInitWithWorkProfileUserHandle() {
94         final int id1 = 1;
95         final int id2 = 2;
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);
103
104         mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment);
105
106         assertThat(mController.mUserHandle.getIdentifier()).isEqualTo(id2);
107     }
108 }