1 /* 2 * Copyright 2016, 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.ota; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 import static org.mockito.Matchers.anyInt; 22 import static org.mockito.Mockito.never; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.verifyZeroInteractions; 25 import static org.mockito.Mockito.when; 26 27 import android.app.admin.DevicePolicyManager; 28 import android.content.ComponentName; 29 import android.content.Context; 30 import android.content.pm.PackageManager; 31 import android.content.pm.UserInfo; 32 import android.os.UserHandle; 33 import android.os.UserManager; 34 import android.support.test.InstrumentationRegistry; 35 import android.support.test.filters.SmallTest; 36 import android.util.Pair; 37 38 import com.android.managedprovisioning.task.AbstractProvisioningTask; 39 import com.android.managedprovisioning.task.CrossProfileIntentFiltersSetter; 40 import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask; 41 import com.android.managedprovisioning.task.DisableInstallShortcutListenersTask; 42 import com.android.managedprovisioning.task.DisallowAddUserTask; 43 import com.android.managedprovisioning.task.InstallExistingPackageTask; 44 45 import java.util.ArrayList; 46 import java.util.Collections; 47 import java.util.List; 48 49 import org.junit.Before; 50 import org.junit.Test; 51 import org.mockito.Mock; 52 import org.mockito.MockitoAnnotations; 53 54 /** 55 * Unit tests for {@link OtaController}. 56 */ 57 @SmallTest 58 public class OtaControllerTest { 59 private static final int DEVICE_OWNER_USER_ID = 12; 60 private static final int MANAGED_PROFILE_USER_ID = 15; 61 62 private static final ComponentName ADMIN_COMPONENT = new ComponentName("com.test.admin", 63 ".AdminReceiver"); 64 65 @Mock private Context mContext; 66 @Mock private DevicePolicyManager mDevicePolicyManager; 67 @Mock private PackageManager mPackageManager; 68 @Mock private UserManager mUserManager; 69 @Mock private CrossProfileIntentFiltersSetter mCrossProfileIntentFiltersSetter; 70 71 private TaskExecutor mTaskExecutor; 72 private OtaController mController; 73 74 private List<Pair<Integer, AbstractProvisioningTask>> mTasks 75 = new ArrayList<>(); 76 private List<UserInfo> mUsers = new ArrayList<>(); 77 private List<UserInfo> mProfiles = new ArrayList<>(); 78 79 @Before setUp()80 public void setUp() { 81 MockitoAnnotations.initMocks(this); 82 83 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) 84 .thenReturn(mDevicePolicyManager); 85 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 86 when(mContext.getPackageManager()).thenReturn(mPackageManager); 87 when(mContext.getResources()) 88 .thenReturn(InstrumentationRegistry.getTargetContext().getResources()); 89 90 when(mUserManager.getUsers()).thenReturn(mUsers); 91 when(mUserManager.getProfiles(UserHandle.USER_SYSTEM)).thenReturn(mProfiles); 92 93 mTaskExecutor = new FakeTaskExecutor(); 94 mController = new OtaController(mContext, mTaskExecutor, mCrossProfileIntentFiltersSetter); 95 96 addSystemUser(); 97 } 98 99 @Test testDeviceOwnerSystemUser()100 public void testDeviceOwnerSystemUser() { 101 // GIVEN that there is a device owner on the system user 102 setDeviceOwner(UserHandle.USER_SYSTEM, ADMIN_COMPONENT); 103 104 // WHEN running the OtaController 105 mController.run(); 106 107 // THEN the task list should contain DeleteNonRequiredAppsTask and DisallowAddUserTask 108 assertTaskList( 109 Pair.create(UserHandle.USER_SYSTEM, DeleteNonRequiredAppsTask.class), 110 Pair.create(UserHandle.USER_SYSTEM, DisallowAddUserTask.class)); 111 112 // THEN cross profile intent filters setter should be invoked for system user 113 verify(mCrossProfileIntentFiltersSetter).resetFilters(UserHandle.USER_SYSTEM); 114 } 115 116 @Test testDeviceOwnerSeparate()117 public void testDeviceOwnerSeparate() { 118 // GIVEN that there is a device owner on a non-system meat user 119 addMeatUser(DEVICE_OWNER_USER_ID); 120 setDeviceOwner(DEVICE_OWNER_USER_ID, ADMIN_COMPONENT); 121 122 // WHEN running the OtaController 123 mController.run(); 124 125 // THEN the task list should contain DeleteNonRequiredAppsTask and DisallowAddUserTask 126 assertTaskList( 127 Pair.create(DEVICE_OWNER_USER_ID, DeleteNonRequiredAppsTask.class), 128 Pair.create(DEVICE_OWNER_USER_ID, DisallowAddUserTask.class)); 129 130 // THEN cross profile intent filters setter should be invoked for both users 131 verify(mCrossProfileIntentFiltersSetter).resetFilters(UserHandle.USER_SYSTEM); 132 verify(mCrossProfileIntentFiltersSetter).resetFilters(DEVICE_OWNER_USER_ID); 133 } 134 135 @Test testManagedProfile()136 public void testManagedProfile() { 137 // GIVEN that there is a managed profile 138 addManagedProfile(MANAGED_PROFILE_USER_ID, ADMIN_COMPONENT); 139 140 // WHEN running the OtaController 141 mController.run(); 142 143 // THEN the task list should contain InstallExistingPackageTask, 144 // DisableInstallShortcutListenersTask and DeleteNonRequiredAppsTask 145 assertTaskList( 146 Pair.create(MANAGED_PROFILE_USER_ID, InstallExistingPackageTask.class), 147 Pair.create(MANAGED_PROFILE_USER_ID, DisableInstallShortcutListenersTask.class), 148 Pair.create(MANAGED_PROFILE_USER_ID, DeleteNonRequiredAppsTask.class)); 149 150 // THEN the cross profile intent filters should be reset 151 verify(mCrossProfileIntentFiltersSetter).resetFilters(UserHandle.USER_SYSTEM); 152 verify(mCrossProfileIntentFiltersSetter, never()).resetFilters(MANAGED_PROFILE_USER_ID); 153 154 // THEN the DISALLOW_WALLPAPER restriction should be set 155 verify(mUserManager).setUserRestriction(UserManager.DISALLOW_WALLPAPER, true, 156 UserHandle.of(MANAGED_PROFILE_USER_ID)); 157 } 158 159 private class FakeTaskExecutor extends TaskExecutor { 160 FakeTaskExecutor()161 public FakeTaskExecutor() { 162 super(); 163 } 164 165 @Override execute(int userId, AbstractProvisioningTask task)166 public synchronized void execute(int userId, AbstractProvisioningTask task) { 167 mTasks.add(Pair.create(userId, task)); 168 } 169 } 170 addMeatUser(int userId)171 private void addMeatUser(int userId) { 172 UserInfo ui = new UserInfo(userId, null, 0); 173 mUsers.add(ui); 174 when(mUserManager.getProfiles(userId)).thenReturn(Collections.singletonList(ui)); 175 } 176 setDeviceOwner(int userId, ComponentName admin)177 private void setDeviceOwner(int userId, ComponentName admin) { 178 when(mDevicePolicyManager.getDeviceOwnerUserId()).thenReturn(userId); 179 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(admin); 180 } 181 addManagedProfile(int userId, ComponentName admin)182 private void addManagedProfile(int userId, ComponentName admin) { 183 UserInfo ui = new UserInfo(userId, null, UserInfo.FLAG_MANAGED_PROFILE); 184 mUsers.add(ui); 185 when(mDevicePolicyManager.getProfileOwnerAsUser(userId)).thenReturn(admin); 186 when(mUserManager.getProfiles(userId)).thenReturn(Collections.singletonList(ui)); 187 mProfiles.add(ui); 188 } 189 addSystemUser()190 private void addSystemUser() { 191 UserInfo ui = new UserInfo(UserHandle.USER_SYSTEM, null, UserInfo.FLAG_PRIMARY); 192 mUsers.add(ui); 193 mProfiles.add(ui); 194 } 195 assertTaskList(Pair<Integer, Class>.... tasks)196 private void assertTaskList(Pair<Integer, Class>... tasks) { 197 assertEquals(tasks.length, mTasks.size()); 198 199 for (Pair<Integer, Class> task : tasks) { 200 assertTaskListContains(task.first, task.second); 201 } 202 } 203 assertTaskListContains(Integer userId, Class taskClass)204 private void assertTaskListContains(Integer userId, Class taskClass) { 205 for (Pair<Integer, AbstractProvisioningTask> task : mTasks) { 206 if (userId == task.first && taskClass.isInstance(task.second)) { 207 return; 208 } 209 } 210 fail("Task for class " + taskClass + " and userId " + userId + " not executed"); 211 } 212 } 213