• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.os;
18 
19 import static libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
20 import static libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
21 
22 import static org.junit.Assert.assertEquals;
23 
24 import android.Manifest;
25 import android.compat.testing.PlatformCompatChangeRule;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentSender;
30 import android.content.pm.PackageInstaller;
31 import android.content.pm.PackageManager;
32 import android.perftests.utils.BenchmarkState;
33 import android.perftests.utils.PerfStatusReporter;
34 
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 import androidx.test.filters.LargeTest;
37 import androidx.test.platform.app.InstrumentationRegistry;
38 
39 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
40 import com.android.cts.install.lib.Install;
41 import com.android.cts.install.lib.InstallUtils;
42 import com.android.cts.install.lib.LocalIntentSender;
43 import com.android.cts.install.lib.TestApp;
44 
45 import org.junit.Before;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 
50 import java.io.IOException;
51 
52 @RunWith(AndroidJUnit4.class)
53 @LargeTest
54 public class PackageManagerPerfTest {
55     private static final String PERMISSION_NAME_EXISTS =
56             "com.android.perftests.packagemanager.TestPermission";
57     private static final String PERMISSION_NAME_DOESNT_EXIST =
58             "com.android.perftests.packagemanager.TestBadPermission";
59     private static final ComponentName TEST_ACTIVITY =
60             new ComponentName("com.android.perftests.packagemanager",
61                     "android.perftests.utils.PerfTestActivity");
62     private static final String TEST_FIELD = "test";
63     private static final String TEST_VALUE = "value";
64 
65     @Rule
66     public final PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
67 
68     @Rule
69     public final PlatformCompatChangeRule mPlatformCompatChangeRule =
70             new PlatformCompatChangeRule();
71     @Rule
72     public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
73             InstrumentationRegistry.getInstrumentation().getUiAutomation(),
74             Manifest.permission.INSTALL_PACKAGES,
75             Manifest.permission.DELETE_PACKAGES);
76 
77     final Context mContext = InstrumentationRegistry.getInstrumentation().getContext();
78     private PackageInstaller mPackageInstaller;
79 
PackageManagerPerfTest()80     public PackageManagerPerfTest() throws PackageManager.NameNotFoundException {
81         mPackageInstaller = mContext.getPackageManager().getPackageInstaller();
82     }
83 
installTestApp(TestApp testApp)84     private void installTestApp(TestApp testApp) throws IOException, InterruptedException {
85         Install install = Install.single(testApp);
86         final int expectedSessionId = install.createSession();
87         PackageInstaller.Session session =
88                 InstallUtils.openPackageInstallerSession(expectedSessionId);
89         PersistableBundle bundle = new PersistableBundle();
90         bundle.putString(TEST_FIELD, TEST_VALUE);
91         session.setAppMetadata(bundle);
92         LocalIntentSender localIntentSender = new LocalIntentSender();
93         session.commit(localIntentSender.getIntentSender());
94         Intent intent = localIntentSender.getResult();
95         InstallUtils.assertStatusSuccess(intent);
96     }
97 
uninstallTestApp(String packageName)98     private void uninstallTestApp(String packageName) throws InterruptedException {
99         LocalIntentSender localIntentSender = new LocalIntentSender();
100         IntentSender intentSender = localIntentSender.getIntentSender();
101         mPackageInstaller.uninstall(packageName, intentSender);
102         Intent intent = localIntentSender.getResult();
103         InstallUtils.assertStatusSuccess(intent);
104     }
105 
106     @Before
setup()107     public void setup() {
108         PackageManager.disableApplicationInfoCache();
109         PackageManager.disablePackageInfoCache();
110     }
111 
112     @Test
testGetAppMetadata()113     public void testGetAppMetadata() throws Exception {
114         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
115         installTestApp(TestApp.A1);
116         final PackageManager pm =
117                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
118         final String packageName = TestApp.A1.getPackageName();
119 
120         while (state.keepRunning()) {
121             PersistableBundle bundle = pm.getAppMetadata(packageName);
122             state.pauseTiming();
123             assertEquals(bundle.size(), 1);
124             assertEquals(bundle.getString(TEST_FIELD), TEST_VALUE);
125             state.resumeTiming();
126         }
127         uninstallTestApp(packageName);
128     }
129 
130     @Test
131     @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testCheckPermissionExists()132     public void testCheckPermissionExists() {
133         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
134         final PackageManager pm =
135                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
136         final String packageName = TEST_ACTIVITY.getPackageName();
137 
138         while (state.keepRunning()) {
139             int ret = pm.checkPermission(PERMISSION_NAME_EXISTS, packageName);
140         }
141     }
142 
143     @Test
144     @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testCheckPermissionExistsWithFiltering()145     public void testCheckPermissionExistsWithFiltering() {
146         testCheckPermissionExists();
147     }
148 
149     @Test
150     @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testCheckPermissionDoesntExist()151     public void testCheckPermissionDoesntExist() {
152         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
153         final PackageManager pm =
154                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
155         final String packageName = TEST_ACTIVITY.getPackageName();
156 
157         while (state.keepRunning()) {
158             int ret = pm.checkPermission(PERMISSION_NAME_DOESNT_EXIST, packageName);
159         }
160     }
161 
162     @Test
163     @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testCheckPermissionDoesntExistWithFiltering()164     public void testCheckPermissionDoesntExistWithFiltering() {
165         testCheckPermissionDoesntExist();
166     }
167 
168     @Test
169     @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testQueryIntentActivities()170     public void testQueryIntentActivities() {
171         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
172         final PackageManager pm =
173                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
174         final Intent intent = new Intent("com.android.perftests.core.PERFTEST");
175 
176         while (state.keepRunning()) {
177             pm.queryIntentActivities(intent, 0);
178         }
179     }
180 
181     @Test
182     @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testQueryIntentActivitiesWithFiltering()183     public void testQueryIntentActivitiesWithFiltering() {
184         testQueryIntentActivities();
185     }
186 
187     @Test
188     @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testGetPackageInfo()189     public void testGetPackageInfo() throws Exception {
190         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
191         final PackageManager pm =
192                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
193 
194         while (state.keepRunning()) {
195             pm.getPackageInfo(TEST_ACTIVITY.getPackageName(), 0);
196         }
197     }
198 
199     @Test
200     @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testGetPackageInfoWithFiltering()201     public void testGetPackageInfoWithFiltering() throws Exception {
202         testGetPackageInfo();
203     }
204 
205     @Test
206     @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testGetApplicationInfo()207     public void testGetApplicationInfo() throws Exception {
208         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
209         final PackageManager pm =
210                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
211 
212         while (state.keepRunning()) {
213             pm.getApplicationInfo(TEST_ACTIVITY.getPackageName(), 0);
214         }
215     }
216 
217     @Test
218     @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testGetApplicationInfoWithFiltering()219     public void testGetApplicationInfoWithFiltering() throws Exception {
220         testGetApplicationInfo();
221     }
222 
223     @Test
224     @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testGetActivityInfo()225     public void testGetActivityInfo() throws Exception {
226         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
227         final PackageManager pm =
228                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
229 
230         while (state.keepRunning()) {
231             pm.getActivityInfo(TEST_ACTIVITY, 0);
232         }
233     }
234 
235     @Test
236     @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testGetActivityInfoWithFiltering()237     public void testGetActivityInfoWithFiltering() throws Exception {
238         testGetActivityInfo();
239     }
240 
241     @Test
242     @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testGetInstalledPackages()243     public void testGetInstalledPackages() throws Exception {
244         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
245         final PackageManager pm =
246                 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
247 
248         while (state.keepRunning()) {
249             pm.getInstalledPackages(0);
250         }
251     }
252 
253     @Test
254     @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY)
testGetInstalledPackagesWithFiltering()255     public void testGetInstalledPackagesWithFiltering() throws Exception {
256         testGetInstalledPackages();
257     }
258 }
259