| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import static org.junit.Assert.fail; |
| 20 |
|
| 21 |
import android.content.Context; |
| 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 |
import android.util.TypedValue; |
| 27 |
|
| 28 |
import androidx.test.InstrumentationRegistry; |
| 29 |
import androidx.test.filters.LargeTest; |
| 30 |
|
| 31 |
import com.android.perftests.core.R; |
| 32 |
|
| 33 |
import org.junit.Before; |
| 34 |
import org.junit.Rule; |
| 35 |
import org.junit.Test; |
| 36 |
import org.xmlpull.v1.XmlPullParser; |
| 37 |
import org.xmlpull.v1.XmlPullParserException; |
| 38 |
|
| 39 |
import java.io.IOException; |
| 40 |
import java.util.Random; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
@LargeTest |
| 46 |
public class ResourcesPerfTest { |
| 47 |
@Rule |
| 48 |
public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 49 |
|
| 50 |
private Resources mRes; |
| 51 |
|
| 52 |
@Before |
| 53 |
public void setUp() { |
| 54 |
Context context = InstrumentationRegistry.getTargetContext(); |
| 55 |
mRes = context.getResources(); |
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void getValue() { |
| 60 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 61 |
TypedValue value = new TypedValue(); |
| 62 |
while (state.keepRunning()) { |
| 63 |
mRes.getValue(R.integer.forty_two, value, false ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void getFrameworkValue() { |
| 69 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 70 |
TypedValue value = new TypedValue(); |
| 71 |
while (state.keepRunning()) { |
| 72 |
mRes.getValue(com.android.internal.R.integer.autofill_max_visible_datasets, value, |
| 73 |
false ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void getValueString() { |
| 79 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 80 |
TypedValue value = new TypedValue(); |
| 81 |
while (state.keepRunning()) { |
| 82 |
mRes.getValue(R.string.long_text, value, false ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void getFrameworkStringValue() { |
| 88 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 89 |
TypedValue value = new TypedValue(); |
| 90 |
while (state.keepRunning()) { |
| 91 |
mRes.getValue(com.android.internal.R.string.cancel, value, false ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void getValueManyConfigurations() { |
| 97 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 98 |
TypedValue value = new TypedValue(); |
| 99 |
while (state.keepRunning()) { |
| 100 |
mRes.getValue(com.android.internal.R.string.mmcc_illegal_me, value, |
| 101 |
false ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void getText() { |
| 107 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 108 |
while (state.keepRunning()) { |
| 109 |
mRes.getText(R.string.long_text); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
@Test |
| 115 |
public void getFont() { |
| 116 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 117 |
while (state.keepRunning()) { |
| 118 |
mRes.getFont(R.font.samplefont); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void getString() { |
| 124 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 125 |
while (state.keepRunning()) { |
| 126 |
mRes.getString(R.string.long_text); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void getQuantityString() { |
| 132 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 133 |
while (state.keepRunning()) { |
| 134 |
mRes.getQuantityString(R.plurals.plurals_text, 5); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void getQuantityText() { |
| 140 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 141 |
while (state.keepRunning()) { |
| 142 |
mRes.getQuantityText(R.plurals.plurals_text, 5); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void getTextArray() { |
| 148 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 149 |
while (state.keepRunning()) { |
| 150 |
mRes.getTextArray(R.array.strings); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void getStringArray() { |
| 156 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 157 |
while (state.keepRunning()) { |
| 158 |
mRes.getStringArray(R.array.strings); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void getIntegerArray() { |
| 164 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 165 |
while (state.keepRunning()) { |
| 166 |
mRes.getIntArray(R.array.ints); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
@Test |
| 171 |
public void getColor() { |
| 172 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 173 |
while (state.keepRunning()) { |
| 174 |
mRes.getColor(R.color.white, null); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void getColorStateList() { |
| 180 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 181 |
while (state.keepRunning()) { |
| 182 |
mRes.getColorStateList(R.color.color_state_list, null); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
@Test |
| 187 |
public void getVectorDrawable() { |
| 188 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 189 |
while (state.keepRunning()) { |
| 190 |
mRes.getDrawable(R.drawable.vector_drawable01, null); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
@Test |
| 195 |
public void getLayoutAndTravese() { |
| 196 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 197 |
while (state.keepRunning()) { |
| 198 |
try (XmlResourceParser parser = mRes.getLayout(R.layout.test_relative_layout)) { |
| 199 |
while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 200 |
|
| 201 |
} |
| 202 |
} catch (IOException | XmlPullParserException exception) { |
| 203 |
fail("Parsing of the layout failed. Something is really broken"); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
@Test |
| 209 |
public void getLayoutAndTraverseInvalidateCaches() { |
| 210 |
mRes.flushLayoutCache(); |
| 211 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 212 |
while (state.keepRunning()) { |
| 213 |
try (XmlResourceParser parser = mRes.getLayout(R.layout.test_relative_layout)) { |
| 214 |
while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 215 |
|
| 216 |
} |
| 217 |
} catch (IOException | XmlPullParserException exception) { |
| 218 |
fail("Parsing of the layout failed. Something is really broken"); |
| 219 |
} |
| 220 |
|
| 221 |
state.pauseTiming(); |
| 222 |
mRes.flushLayoutCache(); |
| 223 |
state.resumeTiming(); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
@Test |
| 228 |
public void getIdentifier() { |
| 229 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 230 |
final Random random = new Random(System.currentTimeMillis()); |
| 231 |
final Context context = InstrumentationRegistry.getTargetContext(); |
| 232 |
final String packageName = context.getPackageName(); |
| 233 |
while (state.keepRunning()) { |
| 234 |
state.pauseTiming(); |
| 235 |
final int expectedInteger = random.nextInt(10001); |
| 236 |
final String expectedString = Integer.toHexString(expectedInteger); |
| 237 |
final String entryName = "i_am_color_" + expectedString; |
| 238 |
state.resumeTiming(); |
| 239 |
|
| 240 |
final int resIdentifier = mRes.getIdentifier(entryName, "color", packageName); |
| 241 |
if (resIdentifier == 0) { |
| 242 |
fail("Color \"" + entryName + "\" is not found"); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |