1 /*
2  * Copyright (C) 2020 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.tests.packagemanager.multiuser.app;
18 
19 import android.Manifest;
20 import android.content.Context;
21 import android.content.pm.ModuleInfo;
22 import android.content.pm.PackageInstaller;
23 import android.content.pm.PackageManager;
24 import android.os.Bundle;
25 
26 import androidx.test.InstrumentationRegistry;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.After;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.stream.Collectors;
36 
37 @RunWith(AndroidJUnit4.class)
38 public class PackageManagerMultiUserTest {
39     private static final String ARG_PACKAGE_NAME = "pkgName";
40 
41     @After
tearDown()42     public void tearDown() throws Exception {
43         InstrumentationRegistry.getInstrumentation().getUiAutomation()
44                 .dropShellPermissionIdentity();
45     }
46 
47     @Test
testUninstallExistingPackage()48     public void testUninstallExistingPackage() throws Exception {
49         InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(
50                 Manifest.permission.DELETE_PACKAGES);
51         String pkgName = InstrumentationRegistry.getArguments().getString(ARG_PACKAGE_NAME);
52         Context context = InstrumentationRegistry.getInstrumentation().getContext();
53         PackageManager packageManager = context.getPackageManager();
54         PackageInstaller packageInstaller = packageManager.getPackageInstaller();
55 
56         packageInstaller.uninstallExistingPackage(pkgName, null);
57     }
58 
59     /**
60      * Returns a list of installed modules to the host-side.
61      */
62     @Test
testGetInstalledModules()63     public void testGetInstalledModules() throws Exception {
64         Context context = InstrumentationRegistry.getInstrumentation().getContext();
65         PackageManager packageManager = context.getPackageManager();
66         List<ModuleInfo> modules = packageManager.getInstalledModules(0);
67         List<String> names =
68                 modules.stream().map(info -> info.getPackageName()).collect(Collectors.toList());
69         final Bundle results = new Bundle();
70         results.putStringArrayList("installedModules", new ArrayList<>(names));
71         InstrumentationRegistry.getInstrumentation().addResults(results);
72     }
73 }
74