• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
46 import androidx.test.filters.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                 eq("managedprovisioning"));
142         verifyNoMoreInteractions(mockIPackageManager);
143     }
144 
testPackageRequiresUpdate_notPresent()145     public void testPackageRequiresUpdate_notPresent() throws Exception {
146         // GIVEN that the requested package is not present on the device
147         // WHEN checking whether an update is required
148         when(mockPackageManager.getPackageInfo(TEST_PACKAGE_NAME_1, 0))
149                 .thenThrow(new NameNotFoundException());
150         // THEN an update is required
151         assertTrue(mUtils.packageRequiresUpdate(TEST_PACKAGE_NAME_1, 0, mockContext));
152     }
153 
testPackageRequiresUpdate()154     public void testPackageRequiresUpdate() throws Exception {
155         // GIVEN a package that is installed on the device
156         PackageInfo pi = new PackageInfo();
157         pi.packageName = TEST_PACKAGE_NAME_1;
158         pi.versionCode = 1;
159         when(mockPackageManager.getPackageInfo(TEST_PACKAGE_NAME_1, 0)).thenReturn(pi);
160         // WHEN checking whether an update is required
161         // THEN verify that update required returns the correct result depending on the minimum
162         // version code requested.
163         assertFalse(mUtils.packageRequiresUpdate(TEST_PACKAGE_NAME_1, 0, mockContext));
164         assertFalse(mUtils.packageRequiresUpdate(TEST_PACKAGE_NAME_1, 1, mockContext));
165         assertTrue(mUtils.packageRequiresUpdate(TEST_PACKAGE_NAME_1, 2, mockContext));
166     }
167 
testIsConnectedToNetwork()168     public void testIsConnectedToNetwork() throws Exception {
169         // GIVEN the device is currently connected to mobile network
170         setCurrentNetworkMock(ConnectivityManager.TYPE_MOBILE, true);
171         // WHEN checking connectivity
172         // THEN utils should return true
173         assertTrue(mUtils.isConnectedToNetwork(mockContext));
174 
175         // GIVEN the device is currently connected to wifi
176         setCurrentNetworkMock(ConnectivityManager.TYPE_WIFI, true);
177         // WHEN checking connectivity
178         // THEN utils should return true
179         assertTrue(mUtils.isConnectedToNetwork(mockContext));
180 
181         // GIVEN the device is currently disconnected on wifi
182         setCurrentNetworkMock(ConnectivityManager.TYPE_WIFI, false);
183         // WHEN checking connectivity
184         // THEN utils should return false
185         assertFalse(mUtils.isConnectedToNetwork(mockContext));
186     }
187 
testIsConnectedToWifi()188     public void testIsConnectedToWifi() throws Exception {
189         // GIVEN the device is currently connected to mobile network
190         setCurrentNetworkMock(ConnectivityManager.TYPE_MOBILE, true);
191         // WHEN checking whether connected to wifi
192         // THEN utils should return false
193         assertFalse(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_WIFI));
194 
195         // GIVEN the device is currently connected to wifi
196         setCurrentNetworkMock(ConnectivityManager.TYPE_WIFI, true);
197         // WHEN checking whether connected to wifi
198         // THEN utils should return true
199         assertTrue(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_WIFI));
200 
201         // GIVEN the device is currently disconnected on wifi
202         setCurrentNetworkMock(ConnectivityManager.TYPE_WIFI, false);
203         // WHEN checking whether connected to wifi
204         // THEN utils should return false
205         assertFalse(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_WIFI));
206     }
207 
testIsConnectedToEthernet()208     public void testIsConnectedToEthernet() throws Exception {
209         // GIVEN the device is currently connected to mobile network
210         setCurrentNetworkMock(ConnectivityManager.TYPE_MOBILE, true);
211         // WHEN checking whether connected to wifi
212         // THEN utils should return false
213         assertFalse(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_ETHERNET));
214 
215         // GIVEN the device is currently connected to wifi
216         setCurrentNetworkMock(ConnectivityManager.TYPE_ETHERNET, true);
217         // WHEN checking whether connected to wifi
218         // THEN utils should return true
219         assertTrue(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_ETHERNET));
220 
221         // GIVEN the device is currently disconnected on wifi
222         setCurrentNetworkMock(ConnectivityManager.TYPE_ETHERNET, false);
223         // WHEN checking whether connected to wifi
224         // THEN utils should return false
225         assertFalse(mUtils.isNetworkTypeConnected(mockContext, ConnectivityManager.TYPE_ETHERNET));
226     }
227 
testGetActiveNetworkInfo()228     public void testGetActiveNetworkInfo() throws Exception {
229         // GIVEN the device is connected to a network.
230         final NetworkInfo networkInfo =
231                 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0, null, null);
232         when(mockConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);
233         // THEN calling getActiveNetworkInfo should return the correct network info.
234         assertEquals(mUtils.getActiveNetworkInfo(mockContext), networkInfo);
235     }
236 
testCurrentLauncherSupportsManagedProfiles_noLauncherSet()237     public void testCurrentLauncherSupportsManagedProfiles_noLauncherSet() throws Exception {
238         // GIVEN there currently is no default launcher set
239         when(mockPackageManager.resolveActivity(any(Intent.class), anyInt()))
240                 .thenReturn(null);
241         // WHEN checking whether the current launcher support managed profiles
242         // THEN utils should return false
243         assertFalse(mUtils.currentLauncherSupportsManagedProfiles(mockContext));
244     }
245 
testCurrentLauncherSupportsManagedProfiles()246     public void testCurrentLauncherSupportsManagedProfiles() throws Exception {
247         // GIVEN the current default launcher is built against lollipop
248         setLauncherMock(Build.VERSION_CODES.LOLLIPOP);
249         // WHEN checking whether the current launcher support managed profiles
250         // THEN utils should return true
251         assertTrue(mUtils.currentLauncherSupportsManagedProfiles(mockContext));
252 
253         // GIVEN the current default launcher is built against kitkat
254         setLauncherMock(Build.VERSION_CODES.KITKAT);
255         // WHEN checking whether the current launcher support managed profiles
256         // THEN utils should return false
257         assertFalse(mUtils.currentLauncherSupportsManagedProfiles(mockContext));
258     }
259 
testFindDeviceAdmin_ComponentName()260     public void testFindDeviceAdmin_ComponentName() throws Exception {
261         // GIVEN a package info with more than one device admin
262         setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME, TEST_DEVICE_ADMIN_NAME_2);
263 
264         // THEN calling findDeviceAdmin returns the correct admin
265         assertEquals(TEST_COMPONENT_NAME_2,
266                 mUtils.findDeviceAdmin(null, TEST_COMPONENT_NAME_2, mockContext, TEST_USER_ID));
267     }
268 
testFindDeviceAdmin_PackageName()269     public void testFindDeviceAdmin_PackageName() throws Exception {
270         // GIVEN a package info with one device admin
271         setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME);
272 
273         // THEN calling findDeviceAdmin returns the correct admin
274         assertEquals(TEST_COMPONENT_NAME,
275                 mUtils.findDeviceAdmin(
276                         TEST_PACKAGE_NAME_1, null, mockContext, TEST_USER_ID));
277     }
278 
testFindDeviceAdmin_NoPackageName()279     public void testFindDeviceAdmin_NoPackageName() throws Exception {
280         // GIVEN no package info file
281         when(mockPackageManager.getPackageInfoAsUser(TEST_PACKAGE_NAME_1,
282                 PackageManager.GET_RECEIVERS | PackageManager.MATCH_DISABLED_COMPONENTS,
283                 TEST_USER_ID))
284                 .thenReturn(null);
285 
286         // THEN throw IllegalProvisioningArgumentException
287         try {
288             mUtils.findDeviceAdmin(
289                     TEST_PACKAGE_NAME_1, null, mockContext, TEST_USER_ID);
290             fail();
291         } catch (IllegalProvisioningArgumentException e) {
292             // expected
293         }
294     }
295 
testFindDeviceAdmin_AnotherComponentName()296     public void testFindDeviceAdmin_AnotherComponentName() throws Exception {
297         // GIVEN a package info with one device admin
298         setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME);
299 
300         // THEN looking another device admin throws IllegalProvisioningArgumentException
301         try {
302             mUtils.findDeviceAdmin(
303                     null, TEST_COMPONENT_NAME_2, mockContext, TEST_USER_ID);
304             fail();
305         } catch (IllegalProvisioningArgumentException e) {
306             // expected
307         }
308     }
309 
testFindDeviceAdminInPackageInfo_Success()310     public void testFindDeviceAdminInPackageInfo_Success() throws Exception {
311         // GIVEN a package info with one device admin
312         PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME);
313 
314         // THEN calling findDeviceAdminInPackageInfo returns the correct admin
315         assertEquals(TEST_COMPONENT_NAME,
316                 mUtils.findDeviceAdminInPackageInfo(TEST_PACKAGE_NAME_1, null, packageInfo));
317     }
318 
testFindDeviceAdminInPackageInfo_PackageNameMismatch()319     public void testFindDeviceAdminInPackageInfo_PackageNameMismatch() throws Exception {
320         // GIVEN a package info with one device admin
321         PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME);
322 
323         // THEN calling findDeviceAdminInPackageInfo with the wrong package name return null
324         assertNull(mUtils.findDeviceAdminInPackageInfo(TEST_PACKAGE_NAME_2, null, packageInfo));
325     }
326 
testFindDeviceAdminInPackageInfo_NoAdmin()327     public void testFindDeviceAdminInPackageInfo_NoAdmin() throws Exception {
328         // GIVEN a package info with no device admin
329         PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1);
330 
331         // THEN calling findDeviceAdminInPackageInfo returns null
332         assertNull(mUtils.findDeviceAdminInPackageInfo(TEST_PACKAGE_NAME_1, null, packageInfo));
333     }
334 
testFindDeviceAdminInPackageInfo_TwoAdmins()335     public void testFindDeviceAdminInPackageInfo_TwoAdmins() throws Exception {
336         // GIVEN a package info with more than one device admin
337         PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME,
338                 TEST_DEVICE_ADMIN_NAME_2);
339 
340         // THEN calling findDeviceAdminInPackageInfo returns null
341         assertNull(mUtils.findDeviceAdminInPackageInfo(TEST_PACKAGE_NAME_1, null, packageInfo));
342     }
343 
testFindDeviceAdminInPackageInfo_TwoAdminsWithComponentName()344     public void testFindDeviceAdminInPackageInfo_TwoAdminsWithComponentName() throws Exception {
345         // GIVEN a package info with more than one device admin
346         PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME,
347                 TEST_DEVICE_ADMIN_NAME_2);
348 
349         // THEN calling findDeviceAdminInPackageInfo return component 1
350         assertEquals(TEST_COMPONENT_NAME, mUtils.findDeviceAdminInPackageInfo(
351                 TEST_PACKAGE_NAME_1, TEST_COMPONENT_NAME, packageInfo));
352     }
353 
354 
testFindDeviceAdminInPackageInfo_InvalidComponentName()355     public void testFindDeviceAdminInPackageInfo_InvalidComponentName() throws Exception {
356         // GIVEN a package info with component 1
357         PackageInfo packageInfo = setUpPackage(TEST_PACKAGE_NAME_1, TEST_DEVICE_ADMIN_NAME);
358 
359         // THEN calling findDeviceAdminInPackageInfo with component 2 returns null
360         assertNull(mUtils.findDeviceAdminInPackageInfo(
361                 TEST_PACKAGE_NAME_1, TEST_COMPONENT_NAME_2, packageInfo));
362     }
363 
testComputeHashOfByteArray()364     public void testComputeHashOfByteArray() {
365         // GIVEN a byte array
366         byte[] bytes = "TESTARRAY".getBytes();
367         // GIVEN its Sha256 hash
368         byte[] sha256 = new byte[] {100, -45, -118, -68, -104, -15, 63, -60, -84, -44, -13, -63,
369                 53, -50, 104, -63, 38, 122, 16, -44, -85, -50, 67, 98, 78, 121, 11, 72, 79, 40, 107,
370                 125};
371 
372         // THEN computeHashOfByteArray returns the correct result
373         assertTrue(Arrays.equals(sha256, mUtils.computeHashOfByteArray(bytes)));
374     }
375 
testComputeHashOfFile()376     public void testComputeHashOfFile() {
377         // GIVEN a file with test data
378         final String fileLocation = getContext().getFilesDir().toString() + "/" + TEST_FILE_NAME;
379         String string = "Hello world!";
380         FileOutputStream outputStream;
381         try {
382             outputStream = getContext().openFileOutput(TEST_FILE_NAME, Context.MODE_PRIVATE);
383             outputStream.write(string.getBytes());
384             outputStream.close();
385         } catch (Exception e) {
386             e.printStackTrace();
387         }
388         // GIVEN the file's Sha256 hash
389         byte[] sha256 = new byte[] {-64, 83, 94, 75, -30, -73, -97, -3, -109, 41, 19, 5, 67, 107,
390                 -8, -119, 49, 78, 74, 63, -82, -64, 94, -49, -4, -69, 125, -13, 26, -39, -27, 26};
391 
392         //THEN the Sha256 hash is correct
393         assertTrue(
394                 Arrays.equals(sha256, mUtils.computeHashOfFile(fileLocation, Utils.SHA256_TYPE)));
395     }
396 
testComputeHashOfFile_NotPresent()397     public void testComputeHashOfFile_NotPresent() {
398         // GIVEN no file is present
399         final String fileLocation = getContext().getFilesDir().toString() + "/" + TEST_FILE_NAME;
400         getContext().deleteFile(TEST_FILE_NAME);
401 
402         // THEN computeHashOfFile should return null
403         assertNull(mUtils.computeHashOfFile(fileLocation, Utils.SHA256_TYPE));
404     }
405 
testCanResolveIntentAsUser()406     public void testCanResolveIntentAsUser() {
407         // GIVEN intent is null
408         // THEN intent should not be resolved
409         assertFalse(mUtils.canResolveIntentAsUser(mockContext, null, TEST_USER_ID));
410 
411         // GIVEN a valid intent
412         Intent intent = new Intent();
413 
414         // WHEN resolve activity as user returns null
415         when(mockPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
416                 .thenReturn(null);
417         // THEN intent should not be resolved for user
418         assertFalse(mUtils.canResolveIntentAsUser(mockContext, intent, TEST_USER_ID));
419 
420         // WHEN resolve activity as user returns valid resolve info
421         when(mockPackageManager.resolveActivityAsUser(any(Intent.class), anyInt(), anyInt()))
422                 .thenReturn(new ResolveInfo());
423         // THEN intent should be resolved
424         assertTrue(mUtils.canResolveIntentAsUser(mockContext, intent, TEST_USER_ID));
425     }
426 
createApplicationInfo( String packageName, boolean system, boolean hiddenUntilInstalled)427     private ApplicationInfo createApplicationInfo(
428             String packageName, boolean system, boolean hiddenUntilInstalled) {
429         ApplicationInfo ai = new ApplicationInfo();
430         ai.packageName = packageName;
431         if (system) {
432             ai.flags = ApplicationInfo.FLAG_SYSTEM;
433         }
434         ai.hiddenUntilInstalled = hiddenUntilInstalled;
435         return ai;
436     }
437 
setCurrentNetworkMock(int type, boolean connected)438     private void setCurrentNetworkMock(int type, boolean connected) {
439         NetworkInfo networkInfo = new NetworkInfo(type, 0, null, null);
440         networkInfo.setDetailedState(
441                 connected ? NetworkInfo.DetailedState.CONNECTED
442                         : NetworkInfo.DetailedState.DISCONNECTED,
443                 null, null);
444         when(mockConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);
445     }
446 
setLauncherMock(int targetSdkVersion)447     private void setLauncherMock(int targetSdkVersion) throws Exception {
448         ApplicationInfo appInfo = new ApplicationInfo();
449         appInfo.targetSdkVersion = targetSdkVersion;
450         ActivityInfo actInfo = new ActivityInfo();
451         actInfo.packageName = TEST_PACKAGE_NAME_1;
452         ResolveInfo resInfo = new ResolveInfo();
453         resInfo.activityInfo = actInfo;
454 
455         when(mockPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(resInfo);
456         when(mockPackageManager.getApplicationInfo(TEST_PACKAGE_NAME_1, 0)).thenReturn(appInfo);
457     }
458 
setUpPackage(String packageName, String... adminNames)459     private PackageInfo setUpPackage(String packageName, String... adminNames)
460             throws NameNotFoundException {
461         PackageInfo packageInfo = new PackageInfo();
462         packageInfo.packageName = packageName;
463         packageInfo.receivers = new ActivityInfo[adminNames.length];
464         for (int i = 0; i < adminNames.length; i++) {
465             ActivityInfo receiver = new ActivityInfo();
466             receiver.permission = android.Manifest.permission.BIND_DEVICE_ADMIN;
467             receiver.name = adminNames[i];
468             packageInfo.receivers[i] = receiver;
469         }
470         when(mockPackageManager.getPackageInfoAsUser(packageName,
471                 PackageManager.GET_RECEIVERS | PackageManager.MATCH_DISABLED_COMPONENTS,
472                 TEST_USER_ID))
473                 .thenReturn(packageInfo);
474 
475         return packageInfo;
476     }
477 }
478