Seigo Nonaka | 917748e | 2017-08-03 17:42:28 -0700 | [diff] [blame] | 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.support.test.InstrumentationRegistry; |
| 29 | import android.support.test.filters.LargeTest; |
| 30 | import android.support.test.runner.AndroidJUnit4; |
| 31 | import android.text.SpannableStringBuilder; |
| 32 | import android.text.Spanned; |
| 33 | import android.text.style.TextAppearanceSpan; |
| 34 | import android.view.LayoutInflater; |
| 35 | |
| 36 | import com.android.perftests.core.R; |
| 37 | |
| 38 | import java.util.Random; |
| 39 | import java.util.Locale; |
| 40 | |
| 41 | import org.junit.Test; |
| 42 | import org.junit.Rule; |
| 43 | import org.junit.runner.RunWith; |
| 44 | |
| 45 | import static org.junit.Assert.assertTrue; |
| 46 | |
| 47 | @LargeTest |
| 48 | @RunWith(AndroidJUnit4.class) |
| 49 | public 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 | } |