Debian Codegraph @

apct-tests/perftests/core/src/android/widget/EditTextLongTextPerfTest.java

raw symbols SCIP
1 /*
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
17 package android.widget;
18
19 import android.app.Activity;
20 import android.perftests.utils.BenchmarkState;
21 import android.perftests.utils.PerfStatusReporter;
22 import android.perftests.utils.PerfTestActivity;
23 import android.view.KeyEvent;
24 import android.view.View.MeasureSpec;
25 import android.view.ViewGroup;
26
27 import androidx.test.filters.LargeTest;
28 import androidx.test.rule.ActivityTestRule;
29
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.Parameterized;
34 import org.junit.runners.Parameterized.Parameters;
35
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.Random;
39
40 @LargeTest
41 @RunWith(Parameterized.class)
42 public class EditTextLongTextPerfTest {
43 @Parameters(name = "{0}")
44 public static Collection cases() {
45 return Arrays.asList(new Object[][] {
46 { "10x3K", 10, 3000 },
47 { "30x1K", 30, 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
62 public ActivityTestRule<PerfTestActivity> mActivityRule =
63 new ActivityTestRule<>(PerfTestActivity.class);
64
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
102 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 }
116 }
117 });
118 }
119 }