1 /*
2  * Copyright (C) 2018 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 
21 import android.content.Context;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 
25 import com.android.settings.testutils.shadow.ShadowUserManager;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.robolectric.RobolectricTestRunner;
31 import org.robolectric.RuntimeEnvironment;
32 import org.robolectric.annotation.Config;
33 
34 @RunWith(RobolectricTestRunner.class)
35 @Config(shadows = {ShadowUserManager.class})
36 public class UserCapabilitiesTest {
37 
38     private Context mContext;
39     private ShadowUserManager mUserManager;
40 
41     @Before
setUp()42     public void setUp() {
43         mContext = RuntimeEnvironment.application;
44         mUserManager = ShadowUserManager.getShadow();
45     }
46 
47     @Test
disallowUserSwitch_restrictionIsSet_true()48     public void disallowUserSwitch_restrictionIsSet_true() {
49         mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
50                 UserManager.DISALLOW_USER_SWITCH, true);
51 
52         UserCapabilities userCapabilities = UserCapabilities.create(mContext);
53         userCapabilities.updateAddUserCapabilities(mContext);
54 
55         assertThat(userCapabilities.mDisallowSwitchUser).isTrue();
56     }
57 
58     @Test
disallowUserSwitch_restrictionIsNotSet_false()59     public void disallowUserSwitch_restrictionIsNotSet_false() {
60         mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
61                 UserManager.DISALLOW_USER_SWITCH, false);
62 
63         UserCapabilities userCapabilities = UserCapabilities.create(mContext);
64         userCapabilities.updateAddUserCapabilities(mContext);
65 
66         assertThat(userCapabilities.mDisallowSwitchUser).isFalse();
67     }
68 
69     @Test
userSwitchEnabled_off()70     public void userSwitchEnabled_off() {
71         mUserManager.setUserSwitcherEnabled(false);
72 
73         final UserCapabilities userCapabilities = UserCapabilities.create(mContext);
74         userCapabilities.updateAddUserCapabilities(mContext);
75 
76         assertThat(userCapabilities.mUserSwitcherEnabled).isFalse();
77     }
78 
79     @Test
userSwitchEnabled_on()80     public void userSwitchEnabled_on() {
81         mUserManager.setUserSwitcherEnabled(true);
82 
83         final UserCapabilities userCapabilities = UserCapabilities.create(mContext);
84         userCapabilities.updateAddUserCapabilities(mContext);
85 
86         assertThat(userCapabilities.mUserSwitcherEnabled).isTrue();
87     }
88 }
89