blob: 10bfa424715909717a86ef0e5ece2e28fffd38f1 [file] [log] [blame]
Rahul Ravikumar05336002019-10-14 15:04:32 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package android.text;
17
18import static android.view.View.MeasureSpec.AT_MOST;
19import static android.view.View.MeasureSpec.UNSPECIFIED;
20
21import android.graphics.Canvas;
22import android.graphics.RecordingCanvas;
23import android.graphics.RenderNode;
24import android.perftests.utils.BenchmarkState;
25import android.perftests.utils.PerfStatusReporter;
26import android.text.NonEditableTextGenerator.TextType;
27import android.widget.TextView;
28
29import androidx.test.InstrumentationRegistry;
30import androidx.test.filters.LargeTest;
31
32import org.junit.Rule;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.junit.runners.Parameterized;
36
37import java.util.ArrayList;
38import java.util.Collection;
39import java.util.List;
40import java.util.Locale;
41import java.util.Random;
42
43/**
44 * Performance test for {@link TextView} measure/draw.
45 */
46@LargeTest
47@RunWith(Parameterized.class)
48public class TextViewSetTextMeasurePerfTest {
49
50 private static final boolean[] BOOLEANS = new boolean[]{false, true};
51
52 @Rule
53 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
54
55 @Parameterized.Parameters(name = "cached {3} {1}chars {0}")
56 public static Collection cases() {
57 final List<Object[]> params = new ArrayList<>();
58 for (int length : new int[]{128}) {
59 for (boolean cached : BOOLEANS) {
60 for (TextType textType : new TextType[]{TextType.STRING,
61 TextType.SPANNABLE_BUILDER}) {
62 params.add(new Object[]{textType.name(), length, textType, cached});
63 }
64 }
65 }
66 return params;
67 }
68
69 private final int mLineWidth;
70 private final int mLength;
71 private final TextType mTextType;
72 private final boolean mCached;
73 private final TextPaint mTextPaint;
74
75 public TextViewSetTextMeasurePerfTest(String label, int length, TextType textType,
76 boolean cached) {
77 mLength = length;
78 mTextType = textType;
79 mCached = cached;
80 mTextPaint = new TextPaint();
81 mTextPaint.setTextSize(10);
82 mLineWidth = Integer.MAX_VALUE;
83 }
84
85 /**
86 * Measures the time to setText and measure for a {@link TextView}.
87 */
88 @Test
89 public void timeCreate() throws Exception {
90 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
91
92 state.pauseTiming();
93 Canvas.freeTextLayoutCaches();
94 final CharSequence text = createRandomText(mLength);
95 final TextView textView = new TextView(InstrumentationRegistry.getTargetContext());
96 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE);
97
98 textView.setText(text);
99 state.resumeTiming();
100
101 while (state.keepRunning()) {
102 state.pauseTiming();
103 textView.setTextLocale(Locale.UK);
104 textView.setTextLocale(Locale.US);
105 if (!mCached) Canvas.freeTextLayoutCaches();
106 state.resumeTiming();
107
108 textView.setText(text);
109 textView.measure(AT_MOST | mLineWidth, UNSPECIFIED);
110 }
111 }
112
113 /**
114 * Measures the time to draw for a {@link TextView}.
115 */
116 @Test
117 public void timeDraw() throws Exception {
118 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
119
120 state.pauseTiming();
121 Canvas.freeTextLayoutCaches();
122 final RenderNode node = RenderNode.create("benchmark", null);
123 final CharSequence text = createRandomText(mLength);
124 final TextView textView = new TextView(InstrumentationRegistry.getTargetContext());
125 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE);
126 textView.setText(text);
127 state.resumeTiming();
128
129 while (state.keepRunning()) {
130
131 state.pauseTiming();
132 final RecordingCanvas canvas = node.start(1200, 200);
133 int save = canvas.save();
134 textView.setTextLocale(Locale.UK);
135 textView.setTextLocale(Locale.US);
136 if (!mCached) Canvas.freeTextLayoutCaches();
137 state.resumeTiming();
138
139 textView.draw(canvas);
140
141 state.pauseTiming();
142 canvas.restoreToCount(save);
143 node.end(canvas);
144 state.resumeTiming();
145 }
146 }
147
148 private CharSequence createRandomText(int length) {
149 return new NonEditableTextGenerator(new Random(0))
150 .setSequenceLength(length)
151 .setCreateBoring(false)
152 .setTextType(mTextType)
153 .build();
154 }
155}