blob: f4c0a172710b1aa304655c0e2f56e2c137461945 [file] [log] [blame]
Aurimas Liutikas64ec5b82018-08-23 15:09:04 -07001/*
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 */
16package android.app;
17
18import android.content.Context;
Ryan Mitchell4043ca72019-06-03 16:11:24 -070019import android.content.pm.ApplicationInfo;
20import android.content.pm.PackageManager;
21import android.content.res.Configuration;
Aurimas Liutikas64ec5b82018-08-23 15:09:04 -070022import android.content.res.Resources;
Ryan Mitchell4043ca72019-06-03 16:11:24 -070023import android.os.UserHandle;
Aurimas Liutikas64ec5b82018-08-23 15:09:04 -070024import android.perftests.utils.BenchmarkState;
25import android.perftests.utils.PerfStatusReporter;
Ryan Mitchell4043ca72019-06-03 16:11:24 -070026import android.view.Display;
KOUSHIK PANUGANTI412796e2018-12-17 14:16:52 -080027
28import androidx.test.InstrumentationRegistry;
29import androidx.test.filters.LargeTest;
Aurimas Liutikas64ec5b82018-08-23 15:09:04 -070030
Ryan Mitchell4043ca72019-06-03 16:11:24 -070031import org.junit.Assert;
Aurimas Liutikas64ec5b82018-08-23 15:09:04 -070032import org.junit.Before;
33import org.junit.Rule;
34import org.junit.Test;
35
36/**
37 * Benchmarks for {@link android.content.res.Resources.Theme}.
38 */
39@LargeTest
40public class ResourcesThemePerfTest {
41 @Rule
42 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
43
Ryan Mitchell4043ca72019-06-03 16:11:24 -070044 private Context mContext;
45 private int mThemeResId;
Aurimas Liutikas64ec5b82018-08-23 15:09:04 -070046 private Resources.Theme mTheme;
47
48 @Before
Ryan Mitchell4043ca72019-06-03 16:11:24 -070049 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 }
Aurimas Liutikas64ec5b82018-08-23 15:09:04 -070055
Ryan Mitchell4043ca72019-06-03 16:11:24 -070056 @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.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
Ryan Mitchell61158582020-01-14 12:35:08 -080099 c, mContext.getResources().getCompatibilityInfo(), null, null);
Ryan Mitchell4043ca72019-06-03 16:11:24 -0700100 Assert.assertNotEquals(destResources.getAssets(), mContext.getAssets());
101
102 Resources.Theme destTheme = destResources.newTheme();
103 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
104 while (state.keepRunning()) {
105 destTheme.setTo(mTheme);
106 }
Aurimas Liutikas64ec5b82018-08-23 15:09:04 -0700107 }
108
109 @Test
110 public void obtainStyledAttributesForViewFromMaterial() {
111 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
112 while (state.keepRunning()) {
113 mTheme.obtainStyledAttributes(android.R.style.Theme_Material, android.R.styleable.View);
114 }
115 }
Ryan Mitchell4043ca72019-06-03 16:11:24 -0700116}