blob: f4ad5ddd3ed24e04ffa30367e1f259e280d787d9 [file] [log] [blame]
Keisuke Kuroyanagib7ff1332016-08-19 23:05:02 +09001/*
2 * Copyright (C) 2016 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
KOUSHIK PANUGANTI412796e2018-12-17 14:16:52 -080019import android.app.Activity;
20import android.perftests.utils.BenchmarkState;
21import android.perftests.utils.PerfStatusReporter;
Riddle Hsu1be1dd62019-09-24 17:54:38 -060022import android.perftests.utils.PerfTestActivity;
KOUSHIK PANUGANTI412796e2018-12-17 14:16:52 -080023import android.view.KeyEvent;
24import android.view.View.MeasureSpec;
25import android.view.ViewGroup;
26
27import androidx.test.filters.LargeTest;
28import androidx.test.rule.ActivityTestRule;
Keisuke Kuroyanagib7ff1332016-08-19 23:05:02 +090029
30import org.junit.Rule;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.junit.runners.Parameterized;
34import org.junit.runners.Parameterized.Parameters;
35
KOUSHIK PANUGANTI412796e2018-12-17 14:16:52 -080036import java.util.Arrays;
37import java.util.Collection;
38import java.util.Random;
Keisuke Kuroyanagib7ff1332016-08-19 23:05:02 +090039
40@LargeTest
41@RunWith(Parameterized.class)
42public class EditTextLongTextPerfTest {
43 @Parameters(name = "{0}")
44 public static Collection cases() {
45 return Arrays.asList(new Object[][] {
46 { "10x30K", 10, 30000 },
47 { "300x1K", 300, 1000 },
48 });
49 }
50
51 private final String mMetricKey;
52 private final int mChars;
53 private final int mLines;
54
55 public EditTextLongTextPerfTest(String metricKey, int chars, int lines) {
56 mMetricKey = metricKey;
57 mChars = chars;
58 mLines = lines;
59 }
60
61 @Rule
Riddle Hsu1be1dd62019-09-24 17:54:38 -060062 public ActivityTestRule<PerfTestActivity> mActivityRule =
63 new ActivityTestRule<>(PerfTestActivity.class);
Keisuke Kuroyanagib7ff1332016-08-19 23:05:02 +090064
65 @Rule
66 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
67
68 private EditText setupEditText() {
69 final EditText editText = new EditText(mActivityRule.getActivity());
70
71 String alphabet = "abcdefghijklmnopqrstuvwxyz";
72 final long seed = 1234567890;
73 Random r = new Random(seed);
74 StringBuilder sb = new StringBuilder();
75 for (int i = 0; i < mLines; i++) {
76 for (int j = 0; j < mChars; j++) {
77 char c = alphabet.charAt(r.nextInt(alphabet.length()));
78 sb.append(c);
79 }
80 sb.append('\n');
81 }
82
83 final int height = 1000;
84 final int width = 1000;
85 editText.setHeight(height);
86 editText.setWidth(width);
87 editText.setLayoutParams(new ViewGroup.LayoutParams(width, height));
88
89 Activity activity = mActivityRule.getActivity();
90 activity.setContentView(editText);
91
92 editText.setText(sb.toString(), TextView.BufferType.EDITABLE);
93 editText.invalidate();
94 editText.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
95 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
96 editText.layout(0, 0, height, width);
97
98 return editText;
99 }
100
101 @Test
Seigo Nonaka36e0f122016-09-16 11:08:00 +0900102 public void testEditText() throws Throwable {
103 mActivityRule.runOnUiThread(() -> {
104 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
105 final EditText editText = setupEditText();
106 final KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER);
107 final int steps = 100;
108 while (state.keepRunning()) {
109 for (int i = 0; i < steps; i++) {
110 int offset = (editText.getText().length() * i) / steps;
111 editText.setSelection(offset);
112 editText.bringPointIntoView(offset);
113 editText.onKeyDown(keyEvent.getKeyCode(), keyEvent);
114 editText.updateDisplayListIfDirty();
115 }
Keisuke Kuroyanagib7ff1332016-08-19 23:05:02 +0900116 }
Seigo Nonaka36e0f122016-09-16 11:08:00 +0900117 });
Keisuke Kuroyanagib7ff1332016-08-19 23:05:02 +0900118 }
119}