| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 ); |
| 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 ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void rebase() { |
| 67 |
Resources.Theme destTheme = mContext.getResources().newTheme(); |
| 68 |
destTheme.applyStyle(mThemeResId, true ); |
| 69 |
destTheme.applyStyle(android.R.style.Theme_Material, true ); |
| 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 |
|
| 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 |
} |