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 static org.mockito.Mockito.any; 20 import static org.mockito.Mockito.anyBoolean; 21 import static org.mockito.Mockito.anyString; 22 import static org.mockito.Mockito.never; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.pm.UserInfo; 28 import android.os.UserHandle; 29 import android.os.UserManager; 30 import android.test.AndroidTestCase; 31 import android.test.suitebuilder.annotation.SmallTest; 32 33 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker; 34 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 38 import java.util.Arrays; 39 import java.util.Collections; 40 41 /** 42 * Unit-tests for {@link DisallowAddUserTask}. 43 * 44 * <p>Run as {@code atest ManagedProvisioningTests:DisallowAddUserTaskTest}. 45 */ 46 public class DisallowAddUserTaskTest extends AndroidTestCase { 47 @Mock private Context mMockContext; 48 @Mock private UserManager mMockUserManager; 49 @Mock private AbstractProvisioningTask.Callback mCallback; 50 @Mock private ProvisioningAnalyticsTracker mProvisioningAnalyticsTracker; 51 52 // Normal cases. 53 private final UserInfo mSystemUser = new UserInfo(UserHandle.USER_SYSTEM, "System", 54 UserInfo.FLAG_PRIMARY | UserInfo.FLAG_ADMIN); 55 56 // Headless-system-user cases. 57 private final UserInfo mFullUser = new UserInfo(10, "FullUser", UserInfo.FLAG_ADMIN); 58 59 @Override setUp()60 public void setUp() { 61 // this is necessary for mockito to work 62 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 63 64 MockitoAnnotations.initMocks(this); 65 66 when(mMockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager); 67 // Setup sensible default responses. 68 when(mMockUserManager.hasUserRestriction(anyString(), any(UserHandle.class))) 69 .thenReturn(false); 70 } 71 72 @SmallTest testMaybeDisallowAddUsers_normalSystem()73 public void testMaybeDisallowAddUsers_normalSystem() { 74 // GIVEN that only one user exists on the device and the system doesn't have a headless 75 // system user 76 when(mMockUserManager.getUsers()).thenReturn(Collections.singletonList(mSystemUser)); 77 final DisallowAddUserTask task = 78 new DisallowAddUserTask(false, mMockContext, null, mCallback, 79 mProvisioningAnalyticsTracker); 80 81 // WHEN running the DisallowAddUserTask on the single user 82 task.run(mSystemUser.id); 83 84 // THEN the user restriction should be set 85 verify(mMockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true, 86 mSystemUser.getUserHandle()); 87 verify(mCallback).onSuccess(task); 88 } 89 90 @SmallTest testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser()91 public void testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser() { 92 // GIVEN that only one user exists on the device and the system doesn't have a headless 93 // system user 94 when(mMockUserManager.getUsers()).thenReturn(Collections.singletonList(mSystemUser)); 95 final DisallowAddUserTask task = 96 new DisallowAddUserTask(false, mMockContext, null, mCallback, 97 mProvisioningAnalyticsTracker); 98 99 // GIVEN that the user restriction has already been set 100 when(mMockUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER, 101 mSystemUser.getUserHandle())) 102 .thenReturn(true); 103 104 // WHEN running the DisallowAddUserTask on the single user 105 task.run(mSystemUser.id); 106 107 // THEN the user restriction should not be set 108 verify(mMockUserManager, never()).setUserRestriction(anyString(), anyBoolean(), 109 any(UserHandle.class)); 110 verify(mCallback).onSuccess(task); 111 } 112 113 @SmallTest testNeverDisallowAddUsers_headlessSystemUserMode_currentUserIsSystemUser()114 public void testNeverDisallowAddUsers_headlessSystemUserMode_currentUserIsSystemUser() { 115 testNeverDisallowAddUsers_headlessSystemUserMode(/* currentUser= */ mSystemUser); 116 } 117 118 @SmallTest testNeverDisallowAddUsers_headlessSystemUserMode_currentUserIsFullUser()119 public void testNeverDisallowAddUsers_headlessSystemUserMode_currentUserIsFullUser() { 120 // This scenario should not happen on real life, but the end result would be the same - the 121 // restriction is not set on any user 122 testNeverDisallowAddUsers_headlessSystemUserMode(/* currentUser= */ mFullUser); 123 } 124 125 // Not a @Test per se, but a helpers that contains the logic of the 2 126 // testNeverDisallowAddUsers_headlessSystemUserMode() - the behavior and logic is identical, the 127 // only difference is the current user testNeverDisallowAddUsers_headlessSystemUserMode(UserInfo currentUser)128 private void testNeverDisallowAddUsers_headlessSystemUserMode(UserInfo currentUser) { 129 // GIVEN that we have a headless system user, it has a full user as well 130 when(mMockUserManager.getUsers()).thenReturn(Arrays.asList(mSystemUser, mFullUser)); 131 final DisallowAddUserTask task = 132 new DisallowAddUserTask(true, mMockContext, null, mCallback, 133 mProvisioningAnalyticsTracker); 134 135 // WHEN running the DisallowAddUserTask as the given user 136 task.run(currentUser.id); 137 138 // THEN the user restriction should not be set 139 verify(mMockUserManager, never()).setUserRestriction(anyString(), anyBoolean(), 140 any(UserHandle.class)); 141 verify(mCallback).onSuccess(task); 142 } 143 } 144