| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 64 |
for (int i = 0; i < 10; ++i) { |
| 65 |
ssb.append(MULTILINE_TEXT); |
| 66 |
} |
| 67 |
|
| 68 |
final ColorStateList[] COLORS = { |
| 69 |
ColorStateList.valueOf(0xFFFF0000), |
| 70 |
ColorStateList.valueOf(0xFF00FF00), |
| 71 |
ColorStateList.valueOf(0xFF0000FF), |
| 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 |
|
| 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), |
| 87 |
COLORS[random.nextInt(COLORS.length)], |
| 88 |
COLORS[random.nextInt(COLORS.length)]); |
| 89 |
int spanLength = 1 + random.nextInt(9); |
| 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 |
|
| 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 |
|
| 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 |
|
| 146 |
textView.setTextLocale(Locale.UK); |
| 147 |
textView.setTextLocale(Locale.US); |
| 148 |
|
| 149 |
textView.measure(UNSPECIFIED, UNSPECIFIED); |
| 150 |
} |
| 151 |
} |
| 152 |
} |