Aurimas Liutikas | 64ec5b8 | 2018-08-23 15:09:04 -0700 | [diff] [blame] | 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; |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 19 | import android.content.pm.ApplicationInfo; |
| 20 | import android.content.pm.PackageManager; |
| 21 | import android.content.res.Configuration; |
Aurimas Liutikas | 64ec5b8 | 2018-08-23 15:09:04 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 23 | import android.os.UserHandle; |
Aurimas Liutikas | 64ec5b8 | 2018-08-23 15:09:04 -0700 | [diff] [blame] | 24 | import android.perftests.utils.BenchmarkState; |
| 25 | import android.perftests.utils.PerfStatusReporter; |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 26 | import android.view.Display; |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 27 | |
| 28 | import androidx.test.InstrumentationRegistry; |
| 29 | import androidx.test.filters.LargeTest; |
Aurimas Liutikas | 64ec5b8 | 2018-08-23 15:09:04 -0700 | [diff] [blame] | 30 | |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 31 | import org.junit.Assert; |
Aurimas Liutikas | 64ec5b8 | 2018-08-23 15:09:04 -0700 | [diff] [blame] | 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 | |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 44 | private Context mContext; |
| 45 | private int mThemeResId; |
Aurimas Liutikas | 64ec5b8 | 2018-08-23 15:09:04 -0700 | [diff] [blame] | 46 | private Resources.Theme mTheme; |
| 47 | |
| 48 | @Before |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 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 | } |
Aurimas Liutikas | 64ec5b8 | 2018-08-23 15:09:04 -0700 | [diff] [blame] | 55 | |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 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.sharedLibraryFiles, Display.DEFAULT_DISPLAY, |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame^] | 99 | c, mContext.getResources().getCompatibilityInfo(), null, null); |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 100 | 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 Liutikas | 64ec5b8 | 2018-08-23 15:09:04 -0700 | [diff] [blame] | 107 | } |
| 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 Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 116 | } |