Patrick Baumann | 88c1e2a | 2020-01-30 09:53:01 -0800 | [diff] [blame] | 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 android.compat.testing.PlatformCompatChangeRule; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.perftests.utils.BenchmarkState; |
| 28 | import android.perftests.utils.PerfStatusReporter; |
| 29 | |
| 30 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 31 | import androidx.test.filters.LargeTest; |
| 32 | import androidx.test.platform.app.InstrumentationRegistry; |
| 33 | |
Patrick Baumann | d399013 | 2020-02-21 13:16:19 -0800 | [diff] [blame^] | 34 | import org.junit.Before; |
Patrick Baumann | 88c1e2a | 2020-01-30 09:53:01 -0800 | [diff] [blame] | 35 | import org.junit.Rule; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | |
| 39 | @RunWith(AndroidJUnit4.class) |
| 40 | @LargeTest |
| 41 | public class PackageManagerPerfTest { |
| 42 | private static final String PERMISSION_NAME_EXISTS = |
| 43 | "com.android.perftests.packagemanager.TestPermission"; |
| 44 | private static final String PERMISSION_NAME_DOESNT_EXIST = |
| 45 | "com.android.perftests.packagemanager.TestBadPermission"; |
| 46 | private static final String OTHER_PACKAGE_NAME = "com.android.perftests.appenumeration0"; |
| 47 | private static final ComponentName TEST_ACTIVITY = |
| 48 | new ComponentName(OTHER_PACKAGE_NAME, |
| 49 | "android.perftests.utils.PerfTestActivity"); |
| 50 | |
| 51 | @Rule |
| 52 | public final PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 53 | |
| 54 | @Rule |
| 55 | public final PlatformCompatChangeRule mPlatformCompatChangeRule = |
| 56 | new PlatformCompatChangeRule(); |
| 57 | |
| 58 | public PackageManagerPerfTest() throws PackageManager.NameNotFoundException { |
| 59 | final Context context = InstrumentationRegistry.getInstrumentation().getContext(); |
| 60 | } |
| 61 | |
Patrick Baumann | d399013 | 2020-02-21 13:16:19 -0800 | [diff] [blame^] | 62 | @Before |
| 63 | public void setup() { |
| 64 | PackageManager.disableApplicationInfoCache(); |
| 65 | PackageManager.disablePackageInfoCache(); |
| 66 | } |
| 67 | |
Patrick Baumann | 88c1e2a | 2020-01-30 09:53:01 -0800 | [diff] [blame] | 68 | @Test |
| 69 | @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 70 | public void testCheckPermissionExists() { |
| 71 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 72 | final PackageManager pm = |
| 73 | InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager(); |
| 74 | final String packageName = TEST_ACTIVITY.getPackageName(); |
| 75 | |
| 76 | while (state.keepRunning()) { |
| 77 | int ret = pm.checkPermission(PERMISSION_NAME_EXISTS, packageName); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 83 | public void testCheckPermissionExistsWithFiltering() { |
| 84 | testCheckPermissionExists(); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 89 | public void testCheckPermissionDoesntExist() { |
| 90 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 91 | final PackageManager pm = |
| 92 | InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager(); |
| 93 | final String packageName = TEST_ACTIVITY.getPackageName(); |
| 94 | |
| 95 | while (state.keepRunning()) { |
| 96 | int ret = pm.checkPermission(PERMISSION_NAME_DOESNT_EXIST, packageName); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 102 | public void testCheckPermissionDoesntExistWithFiltering() { |
| 103 | testCheckPermissionDoesntExist(); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 108 | public void testQueryIntentActivities() { |
| 109 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 110 | final PackageManager pm = |
| 111 | InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager(); |
| 112 | final Intent intent = new Intent("com.android.perftests.core.PERFTEST"); |
| 113 | |
| 114 | while (state.keepRunning()) { |
| 115 | pm.queryIntentActivities(intent, 0); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 121 | public void testQueryIntentActivitiesWithFiltering() { |
| 122 | testQueryIntentActivities(); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 127 | public void testGetPackageInfo() throws Exception { |
| 128 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 129 | final PackageManager pm = |
| 130 | InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager(); |
| 131 | |
| 132 | while (state.keepRunning()) { |
| 133 | pm.getPackageInfo(OTHER_PACKAGE_NAME, 0); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 139 | public void testGetPackageInfoWithFiltering() throws Exception { |
| 140 | testGetPackageInfo(); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 145 | public void testGetApplicationInfo() throws Exception { |
| 146 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 147 | final PackageManager pm = |
| 148 | InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager(); |
| 149 | |
| 150 | while (state.keepRunning()) { |
| 151 | pm.getApplicationInfo(OTHER_PACKAGE_NAME, 0); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 157 | public void testGetApplicationInfoWithFiltering() throws Exception { |
| 158 | testGetApplicationInfo(); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 163 | public void testGetActivityInfo() throws Exception { |
| 164 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 165 | final PackageManager pm = |
| 166 | InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager(); |
| 167 | |
| 168 | while (state.keepRunning()) { |
| 169 | pm.getActivityInfo(TEST_ACTIVITY, 0); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 175 | public void testGetActivityInfoWithFiltering() throws Exception { |
| 176 | testGetActivityInfo(); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | @DisableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 181 | public void testGetInstalledPackages() throws Exception { |
| 182 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 183 | final PackageManager pm = |
| 184 | InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager(); |
| 185 | |
| 186 | while (state.keepRunning()) { |
| 187 | pm.getInstalledPackages(0); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | @EnableCompatChanges(PackageManager.FILTER_APPLICATION_QUERY) |
| 193 | public void testGetInstalledPackagesWithFiltering() throws Exception { |
| 194 | testGetInstalledPackages(); |
| 195 | } |
| 196 | } |