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 
17 package android.app;
18 
19 import static org.junit.Assert.fail;
20 
21 import android.content.res.AssetManager;
22 import android.content.res.Resources;
23 import android.content.res.XmlResourceParser;
24 import android.perftests.utils.BenchmarkState;
25 import android.perftests.utils.PerfStatusReporter;
26 
27 import androidx.test.filters.LargeTest;
28 
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.xmlpull.v1.XmlPullParser;
34 import org.xmlpull.v1.XmlPullParserException;
35 
36 import java.io.IOException;
37 
38 /**
39  * Benchmarks for {@link android.content.res.Resources}.
40  */
41 @LargeTest
42 public class ResourcesPerfTest {
43     @Rule
44     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
45 
46     private AssetManager mAsset;
47     private Resources mRes;
48 
49     private int mTextId;
50     private int mColorId;
51     private int mIntegerId;
52     private int mLayoutId;
53 
54     @Before
setUp()55     public void setUp() {
56         mAsset = new AssetManager();
57         mAsset.addAssetPath("/system/framework/framework-res.apk");
58         mRes = new Resources(mAsset, null, null);
59 
60         mTextId = mRes.getIdentifier("cancel", "string", "android");
61         mColorId = mRes.getIdentifier("transparent", "color", "android");
62         mIntegerId = mRes.getIdentifier("config_shortAnimTime", "integer", "android");
63         mLayoutId = mRes.getIdentifier("two_line_list_item", "layout", "android");
64     }
65 
66     @After
tearDown()67     public void tearDown() {
68         mAsset.close();
69     }
70 
71     @Test
getText()72     public void getText() {
73         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
74         while (state.keepRunning()) {
75             mRes.getText(mTextId);
76         }
77     }
78 
79     @Test
getColor()80     public void getColor() {
81         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
82         while (state.keepRunning()) {
83             mRes.getColor(mColorId, null);
84         }
85     }
86 
87     @Test
getInteger()88     public void getInteger() {
89         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
90         while (state.keepRunning()) {
91             mRes.getInteger(mIntegerId);
92         }
93     }
94 
95     @Test
getLayoutAndTravese()96     public void getLayoutAndTravese() {
97         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
98         while (state.keepRunning()) {
99             try (XmlResourceParser parser = mRes.getLayout(mLayoutId)) {
100                 while (parser.next() != XmlPullParser.END_DOCUMENT) {
101                     // Walk the entire tree
102                 }
103             } catch (IOException | XmlPullParserException exception) {
104                 fail("Parsing of the layout failed. Something is really broken");
105             }
106         }
107     }
108 }
109