1 /* 2 * Copyright (C) 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.common; 18 19 import static android.content.pm.PackageManager.MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS; 20 import static android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES; 21 22 import static org.mockito.Mockito.any; 23 import static org.mockito.Mockito.anyInt; 24 import static org.mockito.Mockito.eq; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.verifyNoMoreInteractions; 27 import static org.mockito.Mockito.when; 28 29 import android.accounts.AccountManager; 30 import android.content.ComponentName; 31 import android.content.Context; 32 import android.content.Intent; 33 import android.content.pm.ActivityInfo; 34 import android.content.pm.ApplicationInfo; 35 import android.content.pm.IPackageManager; 36 import android.content.pm.PackageInfo; 37 import android.content.pm.PackageManager; 38 import android.content.pm.PackageManager.NameNotFoundException; 39 import android.content.pm.ParceledListSlice; 40 import android.content.pm.ResolveInfo; 41 import android.graphics.Color; 42 import android.net.ConnectivityManager; 43 import android.net.NetworkInfo; 44 import android.os.Build; 45 import android.test.AndroidTestCase; 46 import android.test.suitebuilder.annotation.SmallTest; 47 48 import org.mockito.Mock; 49 import org.mockito.MockitoAnnotations; 50 51 import java.io.FileOutputStream; 52 import java.util.Arrays; 53 import java.util.List; 54 import java.util.Set; 55 import java.util.stream.Collectors; 56 57 /** 58 * Unit-tests for {@link Utils}. 59 */ 60 @SmallTest 61 public class UtilsTest extends AndroidTestCase { 62 private static final String TEST_PACKAGE_NAME_1 = "com.test.packagea"; 63 private static final String TEST_PACKAGE_NAME_2 = "com.test.packageb"; 64 private static final String TEST_PACKAGE_NAME_3 = "com.test.packagec"; 65 private static final String TEST_DEVICE_ADMIN_NAME = TEST_PACKAGE_NAME_1 + ".DeviceAdmin"; 66 // Another DeviceAdmin in package 1 67 private static final String TEST_DEVICE_ADMIN_NAME_2 = TEST_PACKAGE_NAME_1 + ".DeviceAdmin2"; 68 private static final ComponentName TEST_COMPONENT_NAME = 69 new ComponentName(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME); 70 private static final ComponentName TEST_COMPONENT_NAME_2 = 71 new ComponentName(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME_2); 72 private static final int TEST_USER_ID = 10; 73 private static final String TEST_FILE_NAME = "testfile"; 74 75 @Mock private Context mockContext; 76 @Mock private AccountManager mockAccountManager; 77 @Mock private IPackageManager mockIPackageManager; 78 @Mock private PackageManager mockPackageManager; 79 @Mock private ConnectivityManager mockConnectivityManager; 80 81 private Utils mUtils; 82 83 @Override setUp()84 public void setUp() { 85 // this is necessary for mockito to work 86 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 87 88 MockitoAnnotations.initMocks(this); 89 90 when(mockContext.getSystemService(Context.ACCOUNT_SERVICE)).thenReturn(mockAccountManager); 91 when(mockContext.getPackageManager()).thenReturn(mockPackageManager); 92 when(mockContext.getSystemService(Context.CONNECTIVITY_SERVICE)) 93 .thenReturn(mockConnectivityManager); 94 95 mUtils = new Utils(); 96 } 97 98 @Override tearDown()99 public void tearDown() { 100 mContext.deleteFile(TEST_FILE_NAME); 101 } 102 testGetCurrentSystemApps()103 public void testGetCurrentSystemApps() throws Exception { 104 // GIVEN two currently installed apps, one of which is system 105 final List<ApplicationInfo> systemAppsWithHiddenUntilInstalled = Arrays.asList( 106 createApplicationInfo( 107 TEST_PACKAGE_NAME_1, /* system */ false, /* hiddenUntilInstalled */ false), 108 createApplicationInfo( 109 TEST_PACKAGE_NAME_2, /* system */ true, /* hiddenUntilInstalled */ false), 110 createApplicationInfo( 111 TEST_PACKAGE_NAME_3, /* system */ true, /* hiddenUntilInstalled */ true)); 112 final List<ApplicationInfo> systemApps = 113 systemAppsWithHiddenUntilInstalled.stream() 114 .filter(applicationInfo -> !applicationInfo.hiddenUntilInstalled) 115 .collect(Collectors.toList()); 116 when(mockIPackageManager.getInstalledApplications( 117 MATCH_UNINSTALLED_PACKAGES, TEST_USER_ID)) 118 .thenReturn(new ParceledListSlice<>(systemApps)); 119 when(mockIPackageManager.getInstalledApplications( 120 MATCH_UNINSTALLED_PACKAGES | MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS, TEST_USER_ID)) 121 .thenReturn(new ParceledListSlice<>(systemAppsWithHiddenUntilInstalled)); 122 123 // WHEN requesting the current system apps 124 Set<String> res = mUtils.getCurrentSystemApps(mockIPackageManager, TEST_USER_ID); 125 // THEN two system apps should be returned 126 assertEquals(2, res.size()); 127 assertTrue(res.contains(TEST_PACKAGE_NAME_2)); 128 assertTrue(res.contains(TEST_PACKAGE_NAME_3)); 129 } 130 testSetComponentEnabledSetting()131 public void testSetComponentEnabledSetting() throws Exception { 132 // GIVEN a component name and a user id 133 // WHEN disabling a component 134 mUtils.setComponentEnabledSetting(mockIPackageManager, TEST_COMPONENT_NAME, 135 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, TEST_USER_ID); 136 // THEN the correct method on mockIPackageManager gets invoked 137 verify(mockIPackageManager).setComponentEnabledSetting(eq(TEST_COMPONENT_NAME), 138 eq(PackageManager.COMPONENT_ENABLED_STATE_DISABLED), 139 eq(PackageManager.DONT_KILL_APP), 140 eq(TEST_USER_ID)); 141 verifyNoMoreInteractions(mockIPackageManager); 142 } 143 testPackageRequiresUpdate_notPresent()144 public void testPackageRequiresUpdate_notPresent() throws Exception { 145 // GIVEN that the requested package is not present on the device 146 // WHEN checking whether an update is required 147 when(mockPackageManager.getPackageInfo(TEST_PACKAGE_NAME_1, 0)) 148 .thenThrow(new NameNotFoundException()); 149 // THEN an update is required 150 assertTrue(mUtils.packageRequiresUpdate(TEST_PACKAGE_NAME_1, 0, mockContext)); 151 } 152 testPackageRequiresUpdate()153 public void testPackageRequiresUpdate() throws Exception { 154 // GIVEN a package that is installed on the device 155 PackageInfo pi = new PackageInfo(); 156 pi.packageName = TEST_PACKAGE_NAME_1; 157 pi.versionCode = 1; 158 when(mockPackageManager.getPackageInfo(TEST_PACKAGE_NAME_1, 0)).thenReturn(pi); 159 // WHEN checking whether an update is required 160 // THEN verify that update required returns the correct result depending on the minimum 161 // version code requested. 162 assertFalse(mUtils.packageRequiresUpdate(TEST_PACKAGE_NAME_1, 0, mockContext)); 163 assertFalse(mUtils.packageRequiresUpdate(TEST_PACKAGE_NAME_1, 1, mockContext)); 164 assertTrue(mUtils.packageRequiresUpdate(TEST_PACKAGE_NAME_1, 2, mockContext)); 165 } 166 testIsConnectedToNetwork()167 public void testIsConnectedToNetwork() throws Exception { 168 // GIVEN the device is currently connected to mobile network 169 setCurrentNetworkMock(ConnectivityManager.TYPE_MOBILE, true); 170 // WHEN checking connectivity 171 // THEN utils should return true 172 assertTrue(mUtils.isConnectedToNetwork(mockContext)); 173 174 // GIVEN the device is currently connected to wifi 175 setCurrentNetworkMock(ConnectivityManager.TYPE_WIFI, true); 176 // WHEN checking connectivity 177 // THEN utils should return true 178 assertTrue(mUtils.isConnectedToNetwork(mockContext)); 179 180 // GIVEN the device is currently disconnected on wifi 181 setCurrentNetworkMock(ConnectivityManager.TYPE_WIFI, false); 182 // WHEN checking connectivity 183 // THEN utils should return false 184 assertFalse(mUtils.isConnectedToNetwork(mockContext)); 185 } 186 testIsNetworkTypeWifiConnected()187 public void testIsNetworkTypeWifiConnected() throws Exception { 188 // GIVEN the device is currently connected to mobile network 189 setCurrentNetworkMock(ConnectivityManager.TYPE_MOBILE, true); 190 // WHEN checking whether connected to wifi 191 // THEN utils should return false 192 assertFalse(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_WIFI)); 193 194 // GIVEN the device is currently connected to wifi 195 setCurrentNetworkMock(ConnectivityManager.TYPE_WIFI, true); 196 // WHEN checking whether connected to wifi 197 // THEN utils should return true 198 assertTrue(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_WIFI)); 199 200 // GIVEN the device is currently disconnected on wifi 201 setCurrentNetworkMock(ConnectivityManager.TYPE_WIFI, false); 202 // WHEN checking whether connected to wifi 203 // THEN utils should return false 204 assertFalse(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_WIFI)); 205 } 206 testIsNetworkTypeEthernetConnected()207 public void testIsNetworkTypeEthernetConnected() throws Exception { 208 // GIVEN the device is currently connected to mobile network 209 setCurrentNetworkMock(ConnectivityManager.TYPE_MOBILE, true); 210 // WHEN checking whether connected to wifi 211 // THEN utils should return false 212 assertFalse(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_ETHERNET)); 213 214 // GIVEN the device is currently connected to wifi 215 setCurrentNetworkMock(ConnectivityManager.TYPE_ETHERNET, true); 216 // WHEN checking whether connected to wifi 217 // THEN utils should return true 218 assertTrue(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_ETHERNET)); 219 220 // GIVEN the device is currently disconnected on wifi 221 setCurrentNetworkMock(ConnectivityManager.TYPE_ETHERNET, false); 222 // WHEN checking whether connected to wifi 223 // THEN utils should return false 224 assertFalse(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_ETHERNET)); 225 } 226 testGetActiveNetworkInfo()227 public void testGetActiveNetworkInfo() throws Exception { 228 // GIVEN the device is connected to a network. 229 final NetworkInfo networkInfo = 230 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, null, null); 231 when(mockConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); 232 // THEN calling getActiveNetworkInfo should return the correct network info. 233 assertEquals(mUtils.getActiveNetworkInfo(mockContext), networkInfo); 234 } 235 testCurrentLauncherSupportsManagedProfiles_noLauncherSet()236 public void testCurrentLauncherSupportsManagedProfiles_noLauncherSet() throws Exception { 237 // GIVEN there currently is no default launcher set 238 when(mockPackageManager.resolveActivity(any(Intent.class), anyInt())) 239 .thenReturn(null); 240 // WHEN checking whether the current launcher support managed profiles 241 // THEN utils should return false 242 assertFalse(mUtils.currentLauncherSupportsManagedProfiles(mockContext)); 243 } 244 testCurrentLauncherSupportsManagedProfiles()245 public void testCurrentLauncherSupportsManagedProfiles() throws Exception { 246 // GIVEN the current default launcher is built against lollipop 247 setLauncherMock(Build.VERSION_CODES.LOLLIPOP); 248 // WHEN checking whether the current launcher support managed profiles 249 // THEN utils should return true 250 assertTrue(mUtils.currentLauncherSupportsManagedProfiles(mockContext)); 251 252 // GIVEN the current default launcher is built against kitkat 253 setLauncherMock(Build.VERSION_CODES.KITKAT); 254 // WHEN checking whether the current launcher support managed profiles 255 // THEN utils should return false 256 assertFalse(mUtils.currentLauncherSupportsManagedProfiles(mockContext)); 257 } 258 testFindDeviceAdmin_ComponentName()259 public void testFindDeviceAdmin_ComponentName() throws Exception { 260 // GIVEN a package info with more than one device admin 261 setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME, TEST_DEVICE_ADMIN_NAME_2); 262 263 // THEN calling findDeviceAdmin returns the correct admin 264 assertEquals(TEST_COMPONENT_NAME_2, 265 mUtils.findDeviceAdmin(null, TEST_COMPONENT_NAME_2, mockContext, TEST_USER_ID)); 266 } 267 testFindDeviceAdmin_PackageName()268 public void testFindDeviceAdmin_PackageName() throws Exception { 269 // GIVEN a package info with one device admin 270 setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME); 271 272 // THEN calling findDeviceAdmin returns the correct admin 273 assertEquals(TEST_COMPONENT_NAME, 274 mUtils.findDeviceAdmin( 275 TEST_PACKAGE_NAME_1, null, mockContext, TEST_USER_ID)); 276 } 277 testFindDeviceAdmin_NoPackageName()278 public void testFindDeviceAdmin_NoPackageName() throws Exception { 279 // GIVEN no package info file 280 when(mockPackageManager.getPackageInfoAsUser(TEST_PACKAGE_NAME_1, 281 PackageManager.GET_RECEIVERS | PackageManager.MATCH_DISABLED_COMPONENTS, 282 TEST_USER_ID)) 283 .thenReturn(null); 284 285 // THEN throw IllegalProvisioningArgumentException 286 try { 287 mUtils.findDeviceAdmin( 288 TEST_PACKAGE_NAME_1, null, mockContext, TEST_USER_ID); 289 fail(); 290 } catch (IllegalProvisioningArgumentException e) { 291 // expected 292 } 293 } 294 testFindDeviceAdmin_AnotherComponentName()295 public void testFindDeviceAdmin_AnotherComponentName() throws Exception { 296 // GIVEN a package info with one device admin 297 setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME); 298 299 // THEN looking another device admin throws IllegalProvisioningArgumentException 300 try { 301 mUtils.findDeviceAdmin( 302 null, TEST_COMPONENT_NAME_2, mockContext, TEST_USER_ID); 303 fail(); 304 } catch (IllegalProvisioningArgumentException e) { 305 // expected 306 } 307 } 308 testFindDeviceAdminInPackageInfo_Success()309 public void testFindDeviceAdminInPackageInfo_Success() throws Exception { 310 // GIVEN a package info with one device admin 311 PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME); 312 313 // THEN calling findDeviceAdminInPackageInfo returns the correct admin 314 assertEquals(TEST_COMPONENT_NAME, 315 mUtils.findDeviceAdminInPackageInfo(TEST_PACKAGE_NAME_1, null, packageInfo)); 316 } 317 testFindDeviceAdminInPackageInfo_PackageNameMismatch()318 public void testFindDeviceAdminInPackageInfo_PackageNameMismatch() throws Exception { 319 // GIVEN a package info with one device admin 320 PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME); 321 322 // THEN calling findDeviceAdminInPackageInfo with the wrong package name return null 323 assertNull(mUtils.findDeviceAdminInPackageInfo(TEST_PACKAGE_NAME_2, null, packageInfo)); 324 } 325 testFindDeviceAdminInPackageInfo_NoAdmin()326 public void testFindDeviceAdminInPackageInfo_NoAdmin() throws Exception { 327 // GIVEN a package info with no device admin 328 PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1); 329 330 // THEN calling findDeviceAdminInPackageInfo returns null 331 assertNull(mUtils.findDeviceAdminInPackageInfo(TEST_PACKAGE_NAME_1, null, packageInfo)); 332 } 333 testFindDeviceAdminInPackageInfo_TwoAdmins()334 public void testFindDeviceAdminInPackageInfo_TwoAdmins() throws Exception { 335 // GIVEN a package info with more than one device admin 336 PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME, 337 TEST_DEVICE_ADMIN_NAME_2); 338 339 // THEN calling findDeviceAdminInPackageInfo returns null 340 assertNull(mUtils.findDeviceAdminInPackageInfo(TEST_PACKAGE_NAME_1, null, packageInfo)); 341 } 342 testFindDeviceAdminInPackageInfo_TwoAdminsWithComponentName()343 public void testFindDeviceAdminInPackageInfo_TwoAdminsWithComponentName() throws Exception { 344 // GIVEN a package info with more than one device admin 345 PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME, 346 TEST_DEVICE_ADMIN_NAME_2); 347 348 // THEN calling findDeviceAdminInPackageInfo return component 1 349 assertEquals(TEST_COMPONENT_NAME, mUtils.findDeviceAdminInPackageInfo( 350 TEST_PACKAGE_NAME_1, TEST_COMPONENT_NAME, packageInfo)); 351 } 352 353 testFindDeviceAdminInPackageInfo_InvalidComponentName()354 public void testFindDeviceAdminInPackageInfo_InvalidComponentName() throws Exception { 355 // GIVEN a package info with component 1 356 PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME); 357 358 // THEN calling findDeviceAdminInPackageInfo with component 2 returns null 359 assertNull(mUtils.findDeviceAdminInPackageInfo( 360 TEST_PACKAGE_NAME_1, TEST_COMPONENT_NAME_2, packageInfo)); 361 } 362 testComputeHashOfByteArray()363 public void testComputeHashOfByteArray() { 364 // GIVEN a byte array 365 byte[] bytes = "TESTARRAY".getBytes(); 366 // GIVEN its Sha256 hash 367 byte[] sha256 = new byte[] {100, -45, -118, -68, -104, -15, 63, -60, -84, -44, -13, -63, 368 53, -50, 104, -63, 38, 122, 16, -44, -85, -50, 67, 98, 78, 121, 11, 72, 79, 40, 107, 369 125}; 370 371 // THEN computeHashOfByteArray returns the correct result 372 assertTrue(Arrays.equals(sha256, mUtils.computeHashOfByteArray(bytes))); 373 } 374 testComputeHashOfFile()375 public void testComputeHashOfFile() { 376 // GIVEN a file with test data 377 final String fileLocation = getContext().getFilesDir().toString() + "/" + TEST_FILE_NAME; 378 String string = "Hello world!"; 379 FileOutputStream outputStream; 380 try { 381 outputStream = getContext().openFileOutput(TEST_FILE_NAME, Context.MODE_PRIVATE); 382 outputStream.write(string.getBytes()); 383 outputStream.close(); 384 } catch (Exception e) { 385 e.printStackTrace(); 386 } 387 // GIVEN the file's Sha256 hash 388 byte[] sha256 = new byte[] {-64, 83, 94, 75, -30, -73, -97, -3, -109, 41, 19, 5, 67, 107, 389 -8, -119, 49, 78, 74, 63, -82, -64, 94, -49, -4, -69, 125, -13, 26, -39, -27, 26}; 390 391 //THEN the Sha256 hash is correct 392 assertTrue( 393 Arrays.equals(sha256, mUtils.computeHashOfFile(fileLocation, Utils.SHA256_TYPE))); 394 } 395 testComputeHashOfFile_NotPresent()396 public void testComputeHashOfFile_NotPresent() { 397 // GIVEN no file is present 398 final String fileLocation = getContext().getFilesDir().toString() + "/" + TEST_FILE_NAME; 399 getContext().deleteFile(TEST_FILE_NAME); 400 401 // THEN computeHashOfFile should return null 402 assertNull(mUtils.computeHashOfFile(fileLocation, Utils.SHA256_TYPE)); 403 } 404 testBrightColors()405 public void testBrightColors() { 406 assertTrue(mUtils.isBrightColor(Color.WHITE)); 407 assertTrue(mUtils.isBrightColor(Color.YELLOW)); 408 assertFalse(mUtils.isBrightColor(Color.BLACK)); 409 assertFalse(mUtils.isBrightColor(Color.BLUE)); 410 } 411 testCanResolveIntentAsUser()412 public void testCanResolveIntentAsUser() { 413 // GIVEN intent is null 414 // THEN intent should not be resolved 415 assertFalse(mUtils.canResolveIntentAsUser(mockContext, null, TEST_USER_ID)); 416 417 // GIVEN a valid intent 418 Intent intent = new Intent(); 419 420 // WHEN resolve activity as user returns null 421 when(mockPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt())) 422 .thenReturn(null); 423 // THEN intent should not be resolved for user 424 assertFalse(mUtils.canResolveIntentAsUser(mockContext, intent, TEST_USER_ID)); 425 426 // WHEN resolve activity as user returns valid resolve info 427 when(mockPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt())) 428 .thenReturn(new ResolveInfo()); 429 // THEN intent should be resolved 430 assertTrue(mUtils.canResolveIntentAsUser(mockContext, intent, TEST_USER_ID)); 431 } 432 createApplicationInfo( String packageName, boolean system, boolean hiddenUntilInstalled)433 private ApplicationInfo createApplicationInfo( 434 String packageName, boolean system, boolean hiddenUntilInstalled) { 435 ApplicationInfo ai = new ApplicationInfo(); 436 ai.packageName = packageName; 437 if (system) { 438 ai.flags = ApplicationInfo.FLAG_SYSTEM; 439 } 440 ai.hiddenUntilInstalled = hiddenUntilInstalled; 441 return ai; 442 } 443 setCurrentNetworkMock(int type, boolean connected)444 private void setCurrentNetworkMock(int type, boolean connected) { 445 NetworkInfo networkInfo = new NetworkInfo(type, 0, null, null); 446 networkInfo.setDetailedState( 447 connected ? NetworkInfo.DetailedState.CONNECTED 448 : NetworkInfo.DetailedState.DISCONNECTED, 449 null, null); 450 when(mockConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); 451 } 452 setLauncherMock(int targetSdkVersion)453 private void setLauncherMock(int targetSdkVersion) throws Exception { 454 ApplicationInfo appInfo = new ApplicationInfo(); 455 appInfo.targetSdkVersion = targetSdkVersion; 456 ActivityInfo actInfo = new ActivityInfo(); 457 actInfo.packageName = TEST_PACKAGE_NAME_1; 458 ResolveInfo resInfo = new ResolveInfo(); 459 resInfo.activityInfo = actInfo; 460 461 when(mockPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(resInfo); 462 when(mockPackageManager.getApplicationInfo(TEST_PACKAGE_NAME_1, 0)).thenReturn(appInfo); 463 } 464 setUpPackage(String packageName, String... adminNames)465 private PackageInfo setUpPackage(String packageName, String... adminNames) 466 throws NameNotFoundException { 467 PackageInfo packageInfo = new PackageInfo(); 468 packageInfo.packageName = packageName; 469 packageInfo.receivers = new ActivityInfo[adminNames.length]; 470 for (int i = 0; i < adminNames.length; i++) { 471 ActivityInfo receiver = new ActivityInfo(); 472 receiver.permission = android.Manifest.permission.BIND_DEVICE_ADMIN; 473 receiver.name = adminNames[i]; 474 packageInfo.receivers[i] = receiver; 475 } 476 when(mockPackageManager.getPackageInfoAsUser(packageName, 477 PackageManager.GET_RECEIVERS | PackageManager.MATCH_DISABLED_COMPONENTS, 478 TEST_USER_ID)) 479 .thenReturn(packageInfo); 480 481 return packageInfo; 482 } 483 } 484