Debian Codegraph @

apct-tests/perftests/core/src/android/widget/TextViewOnMeasurePerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 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.widget;
18
19 import static android.view.View.MeasureSpec.AT_MOST;
20 import static android.view.View.MeasureSpec.EXACTLY;
21 import static android.view.View.MeasureSpec.UNSPECIFIED;
22
23 import android.content.Context;
24 import android.content.res.ColorStateList;
25 import android.graphics.Typeface;
26 import android.perftests.utils.BenchmarkState;
27 import android.perftests.utils.PerfStatusReporter;
28 import android.text.SpannableStringBuilder;
29 import android.text.Spanned;
30 import android.text.style.TextAppearanceSpan;
31
32 import androidx.test.InstrumentationRegistry;
33 import androidx.test.filters.LargeTest;
34 import androidx.test.runner.AndroidJUnit4;
35
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39
40 import java.util.Locale;
41 import java.util.Random;
42
43 @LargeTest
44 @RunWith(AndroidJUnit4.class)
45 public class TextViewOnMeasurePerfTest {
46 private static final String MULTILINE_TEXT =
47 "Lorem ipsum dolor sit amet, \n"+
48 "consectetur adipiscing elit, \n" +
49 "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n" +
50 "Ut enim ad minim veniam, \n" +
51 "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n" +
52 "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat " +
53 "nulla pariatur.\n" +
54 "Excepteur sint occaecat cupidatat non proident, \n" +
55 "sunt in culpa qui officia deserunt mollit anim id est laborum.\n";
56
57 private static final int VIEW_WIDTH = 1000;
58 private static final int VIEW_HEIGHT = 1000;
59 private static final CharSequence COMPLEX_MULTILINE_TEXT;
60 static {
61 final SpannableStringBuilder ssb = new SpannableStringBuilder();
62
63 // To emphasize, append multiline text 10 times.
64 for (int i = 0; i < 10; ++i) {
65 ssb.append(MULTILINE_TEXT);
66 }
67
68 final ColorStateList[] COLORS = {
69 ColorStateList.valueOf(0xFFFF0000), // RED
70 ColorStateList.valueOf(0xFF00FF00), // GREEN
71 ColorStateList.valueOf(0xFF0000FF), // BLUE
72 };
73
74 final int[] STYLES = {
75 Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC
76 };
77
78 final String[] FAMILIES = { "sans-serif", "serif", "monospace" };
79
80 // Append random span to text.
81 final Random random = new Random(0);
82 for (int pos = 0; pos < ssb.length();) {
83 final TextAppearanceSpan span = new TextAppearanceSpan(
84 FAMILIES[random.nextInt(FAMILIES.length)],
85 STYLES[random.nextInt(STYLES.length)],
86 24 + random.nextInt(32), // text size. minimum 24
87 COLORS[random.nextInt(COLORS.length)],
88 COLORS[random.nextInt(COLORS.length)]);
89 int spanLength = 1 + random.nextInt(9); // Up to 9 span length.
90 if (pos + spanLength > ssb.length()) {
91 spanLength = ssb.length() - pos;
92 }
93 ssb.setSpan(span, pos, pos + spanLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
94 pos += spanLength;
95 }
96 COMPLEX_MULTILINE_TEXT = ssb;
97 }
98
99 @Rule
100 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
101
102 @Test
103 public void testMeasure_AtMost() throws Throwable {
104 final Context context = InstrumentationRegistry.getTargetContext();
105 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
106
107 final TextView textView = new TextView(context);
108 textView.setText(COMPLEX_MULTILINE_TEXT);
109
110 while (state.keepRunning()) {
111 // Changing locale to invalidate internal layout.
112 textView.setTextLocale(Locale.UK);
113 textView.setTextLocale(Locale.US);
114
115 textView.measure(AT_MOST | VIEW_WIDTH, AT_MOST | VIEW_HEIGHT);
116 }
117 }
118
119 @Test
120 public void testMeasure_Exactly() throws Throwable {
121 final Context context = InstrumentationRegistry.getTargetContext();
122 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
123
124 final TextView textView = new TextView(context);
125 textView.setText(COMPLEX_MULTILINE_TEXT);
126
127 while (state.keepRunning()) {
128 // Changing locale to invalidate internal layout.
129 textView.setTextLocale(Locale.UK);
130 textView.setTextLocale(Locale.US);
131
132 textView.measure(EXACTLY | VIEW_WIDTH, EXACTLY | VIEW_HEIGHT);
133 }
134 }
135
136 @Test
137 public void testMeasure_Unspecified() throws Throwable {
138 final Context context = InstrumentationRegistry.getTargetContext();
139 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
140
141 final TextView textView = new TextView(context);
142 textView.setText(COMPLEX_MULTILINE_TEXT);
143
144 while (state.keepRunning()) {
145 // Changing locale to invalidate internal layout.
146 textView.setTextLocale(Locale.UK);
147 textView.setTextLocale(Locale.US);
148
149 textView.measure(UNSPECIFIED, UNSPECIFIED);
150 }
151 }
152 }