Home | History | Annotate | Download | only in task
      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 org.mockito.Mock;
     34 import org.mockito.MockitoAnnotations;
     35 
     36 import java.util.Arrays;
     37 import java.util.Collections;
     38 
     39 /**
     40  * Unit-tests for {@link DisallowAddUserTask}.
     41  */
     42 public class DisallowAddUserTaskTest extends AndroidTestCase {
     43     @Mock private Context mockContext;
     44     @Mock private UserManager mockUserManager;
     45     @Mock private AbstractProvisioningTask.Callback mCallback;
     46 
     47     // Normal cases.
     48     private UserInfo primaryUser = new UserInfo(0, "Primary",
     49             UserInfo.FLAG_PRIMARY | UserInfo.FLAG_ADMIN);
     50 
     51     // Split-system-user cases.
     52     private UserInfo systemUser = new UserInfo(UserHandle.USER_SYSTEM, "System", 0 /* flags */);
     53     private UserInfo meatUser = new UserInfo(10, "Primary",
     54             UserInfo.FLAG_PRIMARY | UserInfo.FLAG_ADMIN);
     55 
     56     @Override
     57     public void setUp() {
     58         // this is necessary for mockito to work
     59         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
     60 
     61         MockitoAnnotations.initMocks(this);
     62 
     63         when(mockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mockUserManager);
     64         // Setup sensible default responses.
     65         when(mockUserManager.hasUserRestriction(anyString(), any(UserHandle.class)))
     66                 .thenReturn(false);
     67     }
     68 
     69     @SmallTest
     70     public void testMaybeDisallowAddUsers_normalSystem() {
     71         // GIVEN that only one user exists on the device and the system doesn't have a split system
     72         // user
     73         when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(primaryUser));
     74         final DisallowAddUserTask task =
     75                 new DisallowAddUserTask(false, mockContext, null, mCallback);
     76 
     77         // WHEN running the DisallowAddUserTask on the single user
     78         task.run(primaryUser.id);
     79 
     80         // THEN the user restriction should be set
     81         verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true,
     82                 primaryUser.getUserHandle());
     83         verify(mCallback).onSuccess(task);
     84     }
     85 
     86     @SmallTest
     87     public void testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser() {
     88         // GIVEN that only one user exists on the device and the system doesn't have a split system
     89         // user
     90         when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(primaryUser));
     91         final DisallowAddUserTask task =
     92                 new DisallowAddUserTask(false, mockContext, null, mCallback);
     93 
     94         // GIVEN that the user restriction has already been set
     95         when(mockUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER,
     96                 primaryUser.getUserHandle()))
     97                 .thenReturn(true);
     98 
     99         // WHEN running the DisallowAddUserTask on the single user
    100         task.run(primaryUser.id);
    101 
    102         // THEN the user restriction should not be set
    103         verify(mockUserManager, never()).setUserRestriction(anyString(), anyBoolean(),
    104                 any(UserHandle.class));
    105         verify(mCallback).onSuccess(task);
    106     }
    107 
    108     @SmallTest
    109     public void testMaybeDisallowAddUsers_splitUserSystem_meatUserDeviceOwner() {
    110         // GIVEN that we have a split system user and a single meat user on the device
    111         when(mockUserManager.getUsers()).thenReturn(Arrays.asList(new UserInfo[]{
    112                 systemUser, meatUser}));
    113         final DisallowAddUserTask task =
    114                 new DisallowAddUserTask(true, mockContext, null, mCallback);
    115 
    116         // WHEN running the DisallowAddUserTask on the meat user
    117         task.run(meatUser.id);
    118 
    119         // THEN the user restriction should be added on both users
    120         verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true,
    121                 systemUser.getUserHandle());
    122         verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true,
    123                 meatUser.getUserHandle());
    124         verify(mCallback).onSuccess(task);
    125     }
    126 
    127     @SmallTest
    128     public void testMaybeDisallowAddUsers_splitUserSystem_systemDeviceOwner() {
    129         // GIVEN that we have a split system user and only the system user on the device
    130         when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(systemUser));
    131         final DisallowAddUserTask task =
    132                 new DisallowAddUserTask(true, mockContext, null, mCallback);
    133 
    134         // WHEN running the DisallowAddUserTask on the system user
    135         task.run(systemUser.id);
    136 
    137         // THEN the user restriction should not be set
    138         verify(mockUserManager, never()).setUserRestriction(anyString(), anyBoolean(),
    139                 any(UserHandle.class));
    140         verify(mCallback).onSuccess(task);
    141     }
    142 }
    143