1 /* 2 * Copyright (C) 2015 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.managedprovisioning.task; 18 19 import android.content.Context; 20 import android.content.pm.UserInfo; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 import android.test.AndroidTestCase; 24 import android.test.suitebuilder.annotation.SmallTest; 25 26 import org.mockito.Mock; 27 import org.mockito.MockitoAnnotations; 28 29 import java.util.Arrays; 30 import java.util.Collections; 31 32 import static org.mockito.Mockito.any; 33 import static org.mockito.Mockito.anyBoolean; 34 import static org.mockito.Mockito.anyString; 35 import static org.mockito.Mockito.eq; 36 import static org.mockito.Mockito.never; 37 import static org.mockito.Mockito.verify; 38 import static org.mockito.Mockito.verifyZeroInteractions; 39 import static org.mockito.Mockito.when; 40 41 /** 42 * Unit-tests for {@link DisallowAddUserTask}. 43 */ 44 public class DisallowAddUserTaskTest extends AndroidTestCase { 45 @Mock private Context mockContext; 46 @Mock private UserManager mockUserManager; 47 48 // Normal cases. 49 private UserInfo primaryUser = new UserInfo(0, "Primary", 50 UserInfo.FLAG_PRIMARY | UserInfo.FLAG_ADMIN); 51 52 // Split-system-user cases. 53 private UserInfo systemUser = new UserInfo(UserHandle.USER_SYSTEM, "System", 0 /* flags */); 54 private UserInfo meatUser = new UserInfo(10, "Primary", 55 UserInfo.FLAG_PRIMARY | UserInfo.FLAG_ADMIN); 56 57 @Override setUp()58 public void setUp() { 59 // this is necessary for mockito to work 60 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 61 62 MockitoAnnotations.initMocks(this); 63 64 // Setup sensible default responses. 65 when(mockUserManager.hasUserRestriction(anyString(), any(UserHandle.class))) 66 .thenReturn(false); 67 } 68 69 @SmallTest testMaybeDisallowAddUsers_normalSystem()70 public void testMaybeDisallowAddUsers_normalSystem() { 71 when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(primaryUser)); 72 73 new DisallowAddUserTask(mockUserManager, primaryUser.id, false /* isSplitSystemUser */) 74 .maybeDisallowAddUsers(); 75 76 verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true, 77 primaryUser.getUserHandle()); 78 } 79 80 @SmallTest testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser()81 public void testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser() { 82 when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(primaryUser)); 83 84 when(mockUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER, 85 primaryUser.getUserHandle())) 86 .thenReturn(true); 87 88 new DisallowAddUserTask(mockUserManager, meatUser.id, false /* isSplitSystemUser */) 89 .maybeDisallowAddUsers(); 90 91 verify(mockUserManager, never()).setUserRestriction(eq(UserManager.DISALLOW_ADD_USER), 92 anyBoolean(), eq(primaryUser.getUserHandle())); 93 } 94 95 @SmallTest testMaybeDisallowAddUsers_splitUserSystem_meatUserDeviceOwner()96 public void testMaybeDisallowAddUsers_splitUserSystem_meatUserDeviceOwner() { 97 when(mockUserManager.getUsers()).thenReturn(Arrays.asList(new UserInfo[]{ 98 systemUser, meatUser})); 99 100 new DisallowAddUserTask(mockUserManager, meatUser.id, true /* isSplitSystemUser */) 101 .maybeDisallowAddUsers(); 102 103 verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true, 104 systemUser.getUserHandle()); 105 verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true, 106 meatUser.getUserHandle()); 107 } 108 109 @SmallTest testMaybeDisallowAddUsers_splitUserSystem_systemDeviceOwner()110 public void testMaybeDisallowAddUsers_splitUserSystem_systemDeviceOwner() { 111 when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(systemUser)); 112 113 new DisallowAddUserTask(mockUserManager, systemUser.id, true /* isSplitSystemUser */) 114 .maybeDisallowAddUsers(); 115 116 verifyZeroInteractions(mockUserManager); 117 } 118 }