blob: fcb13a8d51f159ea137bf59afacd9f6d3d76e98d [file] [log] [blame]
Ryan Mitchell4043ca72019-06-03 16:11:24 -07001/*
2 * Copyright (C) 2019 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
17package android.app;
18
19import static org.junit.Assert.assertTrue;
20
21import android.content.Context;
22import android.content.om.OverlayManager;
23import android.os.UserHandle;
24import android.perftests.utils.BenchmarkState;
25import android.perftests.utils.PerfStatusReporter;
26import android.perftests.utils.TestPackageInstaller;
27
28import androidx.test.InstrumentationRegistry;
29import androidx.test.filters.LargeTest;
30
31import com.android.perftests.core.R;
32
33import org.junit.After;
34import org.junit.AfterClass;
35import org.junit.BeforeClass;
36import org.junit.Rule;
37import org.junit.Test;
38
39import java.util.ArrayList;
40import java.util.concurrent.Executor;
41import java.util.concurrent.FutureTask;
42import java.util.concurrent.TimeUnit;
43
44/**
45 * Benchmarks for {@link android.content.om.OverlayManager}.
46 */
47@LargeTest
48public class OverlayManagerPerfTest {
49 private static final int OVERLAY_PKG_COUNT = 10;
50 private static Context sContext;
51 private static OverlayManager sOverlayManager;
52 private static Executor sExecutor;
53 private static ArrayList<TestPackageInstaller.InstalledPackage> sSmallOverlays =
54 new ArrayList<>();
55 private static ArrayList<TestPackageInstaller.InstalledPackage> sLargeOverlays =
56 new ArrayList<>();
57
58 @Rule
59 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
60
61 @BeforeClass
62 public static void classSetUp() throws Exception {
63 sContext = InstrumentationRegistry.getTargetContext();
64 sOverlayManager = new OverlayManager(sContext);
65 sExecutor = (command) -> new Thread(command).start();
66
67 // Install all of the test overlays.
68 TestPackageInstaller installer = new TestPackageInstaller(sContext);
69 for (int i = 0; i < OVERLAY_PKG_COUNT; i++) {
70 sSmallOverlays.add(installer.installPackage("Overlay" + i +".apk"));
71 sLargeOverlays.add(installer.installPackage("LargeOverlay" + i +".apk"));
72 }
73 }
74
75 @AfterClass
76 public static void classTearDown() throws Exception {
77 for (TestPackageInstaller.InstalledPackage overlay : sSmallOverlays) {
78 overlay.uninstall();
79 }
80
81 for (TestPackageInstaller.InstalledPackage overlay : sLargeOverlays) {
82 overlay.uninstall();
83 }
84 }
85
86 @After
87 public void tearDown() throws Exception {
88 // Disable all test overlays after each test.
89 for (TestPackageInstaller.InstalledPackage overlay : sSmallOverlays) {
90 assertSetEnabled(sContext, overlay.getPackageName(), false);
91 }
92
93 for (TestPackageInstaller.InstalledPackage overlay : sLargeOverlays) {
94 assertSetEnabled(sContext, overlay.getPackageName(), false);
95 }
96 }
97
98 /**
99 * Enables the overlay and waits for the APK path change sto be propagated to the context
100 * AssetManager.
101 */
102 private void assertSetEnabled(Context context, String overlayPackage, boolean eanabled)
103 throws Exception {
104 sOverlayManager.setEnabled(overlayPackage, true, UserHandle.SYSTEM);
105
106 // Wait for the overlay changes to propagate
107 FutureTask<Boolean> task = new FutureTask<>(() -> {
108 while (true) {
109 for (String path : context.getAssets().getApkPaths()) {
110 if (eanabled == path.contains(overlayPackage)) {
111 return true;
112 }
113 }
114 }
115 });
116
117 sExecutor.execute(task);
118 assertTrue("Failed to load overlay " + overlayPackage,
119 task.get(20, TimeUnit.SECONDS));
120 }
121
122 @Test
123 public void setEnabledWarmCache() throws Exception {
124 String packageName = sSmallOverlays.get(0).getPackageName();
125 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
126 while (state.keepRunning()) {
127 assertSetEnabled(sContext, packageName, true);
128
129 // Disable the overlay for the next iteration of the test
130 state.pauseTiming();
131 assertSetEnabled(sContext, packageName, false);
132 state.resumeTiming();
133 }
134 }
135
136 @Test
137 public void setEnabledColdCacheSmallOverlay() throws Exception {
138 String packageName = sSmallOverlays.get(0).getPackageName();
139 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
140 while (state.keepRunning()) {
141 assertSetEnabled(sContext, packageName, true);
142
143 // Disable the overlay and remove the idmap for the next iteration of the test
144 state.pauseTiming();
145 assertSetEnabled(sContext, packageName, false);
146 sOverlayManager.invalidateCachesForOverlay(packageName, UserHandle.SYSTEM);
147 state.resumeTiming();
148 }
149 }
150
151 @Test
152 public void setEnabledColdCacheLargeOverlay() throws Exception {
153 String packageName = sLargeOverlays.get(0).getPackageName();
154 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
155 while (state.keepRunning()) {
156 assertSetEnabled(sContext, packageName, true);
157
158 // Disable the overlay and remove the idmap for the next iteration of the test
159 state.pauseTiming();
160 assertSetEnabled(sContext, packageName, false);
161 sOverlayManager.invalidateCachesForOverlay(packageName, UserHandle.SYSTEM);
162 state.resumeTiming();
163 }
164 }
165
166 @Test
167 public void setEnabledDisable() throws Exception {
168 String packageName = sSmallOverlays.get(0).getPackageName();
169 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
170 while (state.keepRunning()) {
171 state.pauseTiming();
172 assertSetEnabled(sContext, packageName, true);
173 state.resumeTiming();
174
175 assertSetEnabled(sContext, packageName, false);
176 }
177 }
178
179 @Test
180 public void getStringOneSmallOverlay() throws Exception {
181 String packageName = sSmallOverlays.get(0).getPackageName();
182 assertSetEnabled(sContext, packageName, true);
183
184 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
185 while (state.keepRunning()) {
186 sContext.getString(R.string.short_text);
187 }
188
189 assertSetEnabled(sContext, packageName, false);
190 }
191
192 @Test
193 public void getStringOneLargeOverlay() throws Exception {
194 String packageName = sLargeOverlays.get(0).getPackageName();
195 assertSetEnabled(sContext, packageName, true);
196
197 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
198 while (state.keepRunning()) {
199 for (int resId = R.string.short_text000; resId < R.string.short_text255; resId++) {
200 sContext.getString(resId);
201 }
202 }
203
204 assertSetEnabled(sContext, packageName, false);
205 }
206
207 @Test
208 public void getStringTenOverlays() throws Exception {
209 // Enable all test overlays
210 for (TestPackageInstaller.InstalledPackage overlay : sSmallOverlays) {
211 assertSetEnabled(sContext, overlay.getPackageName(), true);
212 }
213
214 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
215 while (state.keepRunning()) {
216 sContext.getString(R.string.short_text);
217 }
218 }
219
220 @Test
221 public void getStringLargeTenOverlays() throws Exception {
222 // Enable all test overlays
223 for (TestPackageInstaller.InstalledPackage overlay : sLargeOverlays) {
224 assertSetEnabled(sContext, overlay.getPackageName(), true);
225 }
226
227 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
228 while (state.keepRunning()) {
229 for (int resId = R.string.short_text000; resId < R.string.short_text255; resId++) {
230 sContext.getString(resId);
231 }
232 }
233 }
234}