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