Debian Codegraph @

apct-tests/perftests/core/src/android/app/ResourcesThemePerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 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 package android.app;
17
18 import android.content.Context;
19 import android.content.pm.ApplicationInfo;
20 import android.content.pm.PackageManager;
21 import android.content.res.Configuration;
22 import android.content.res.Resources;
23 import android.os.UserHandle;
24 import android.perftests.utils.BenchmarkState;
25 import android.perftests.utils.PerfStatusReporter;
26 import android.view.Display;
27
28 import androidx.test.InstrumentationRegistry;
29 import androidx.test.filters.LargeTest;
30
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35
36 /**
37 * Benchmarks for {@link android.content.res.Resources.Theme}.
38 */
39 @LargeTest
40 public class ResourcesThemePerfTest {
41 @Rule
42 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
43
44 private Context mContext;
45 private int mThemeResId;
46 private Resources.Theme mTheme;
47
48 @Before
49 public void setUp() throws Exception {
50 mContext = InstrumentationRegistry.getTargetContext();
51 mThemeResId = com.android.perftests.core.R.style.Base_V7_Theme_AppCompat;
52 mTheme = mContext.getResources().newTheme();
53 mTheme.applyStyle(mThemeResId, true /* force */);
54 }
55
56 @Test
57 public void applyStyle() {
58 Resources.Theme destTheme = mContext.getResources().newTheme();
59 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
60 while (state.keepRunning()) {
61 destTheme.applyStyle(mThemeResId, true /* force */);
62 }
63 }
64
65 @Test
66 public void rebase() {
67 Resources.Theme destTheme = mContext.getResources().newTheme();
68 destTheme.applyStyle(mThemeResId, true /* force */);
69 destTheme.applyStyle(android.R.style.Theme_Material, true /* force */);
70 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
71 while (state.keepRunning()) {
72 destTheme.rebase();
73 }
74 }
75
76 @Test
77 public void setToSameAssetManager() {
78 Resources.Theme destTheme = mContext.getResources().newTheme();
79 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
80 while (state.keepRunning()) {
81 destTheme.setTo(mTheme);
82 }
83 }
84
85 @Test
86 public void setToDifferentAssetManager() throws Exception {
87 // Create a new Resources object with the same asset paths but a different AssetManager
88 PackageManager packageManager = mContext.getApplicationContext().getPackageManager();
89 ApplicationInfo ai = packageManager.getApplicationInfo(mContext.getPackageName(),
90 UserHandle.myUserId());
91
92 ResourcesManager resourcesManager = ResourcesManager.getInstance();
93 Configuration c = resourcesManager.getConfiguration();
94 c.orientation = (c.orientation == Configuration.ORIENTATION_PORTRAIT)
95 ? Configuration.ORIENTATION_LANDSCAPE : Configuration.ORIENTATION_PORTRAIT;
96
97 Resources destResources = resourcesManager.getResources(null, ai.sourceDir,
98 ai.splitSourceDirs, ai.resourceDirs, ai.overlayPaths, ai.sharedLibraryFiles,
99 Display.DEFAULT_DISPLAY, c, mContext.getResources().getCompatibilityInfo(),
100 null, null);
101 Assert.assertNotEquals(destResources.getAssets(), mContext.getAssets());
102
103 Resources.Theme destTheme = destResources.newTheme();
104 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
105 while (state.keepRunning()) {
106 destTheme.setTo(mTheme);
107 }
108 }
109
110 @Test
111 public void obtainStyledAttributesForViewFromMaterial() {
112 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
113 while (state.keepRunning()) {
114 mTheme.obtainStyledAttributes(android.R.style.Theme_Material, android.R.styleable.View);
115 }
116 }
117 }