1 /*
2  * Copyright (C) 2019 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 org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.never;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23 
24 import android.content.Context;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 
28 import com.android.settings.testutils.shadow.ShadowUserManager;
29 import com.android.settings.widget.SwitchBar;
30 import com.android.settings.widget.SwitchBarController;
31 
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 import org.robolectric.annotation.Config;
39 
40 @RunWith(RobolectricTestRunner.class)
41 @Config(shadows = {ShadowUserManager.class})
42 public class MultiUserSwitchBarControllerTest {
43 
44     private Context mContext;
45     private ShadowUserManager mUserManager;
46     private SwitchBarController mSwitchBarController;
47 
48 
49     @Before
setUp()50     public void setUp() {
51         mContext = RuntimeEnvironment.application;
52         mUserManager = ShadowUserManager.getShadow();
53         mSwitchBarController = spy(new SwitchBarController(new SwitchBar(mContext)));
54         mUserManager.setSupportsMultipleUsers(true);
55     }
56 
57     @After
tearDown()58     public void tearDown() {
59         ShadowUserManager.reset();
60     }
61 
62     @Test
onStart_disallowUserSwitch_shouldSetDisabledByAdmin()63     public void onStart_disallowUserSwitch_shouldSetDisabledByAdmin() {
64         mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
65                 UserManager.DISALLOW_USER_SWITCH, true);
66 
67         final MultiUserSwitchBarController controller = new MultiUserSwitchBarController(mContext,
68                 mSwitchBarController, null);
69 
70         verify(mSwitchBarController).setDisabledByAdmin(any());
71     }
72 
73     @Test
onStart_allowUserSwitch_shouldNotSetDisabledByAdmin()74     public void onStart_allowUserSwitch_shouldNotSetDisabledByAdmin() {
75         mUserManager.setUserRestriction(UserHandle.of(UserHandle.myUserId()),
76                 UserManager.DISALLOW_USER_SWITCH, false);
77 
78         final MultiUserSwitchBarController controller = new MultiUserSwitchBarController(mContext,
79                 mSwitchBarController, null);
80 
81         verify(mSwitchBarController, never()).setDisabledByAdmin(any());
82     }
83 }
84