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