Keisuke Kuroyanagi | b7ff133 | 2016-08-19 23:05:02 +0900 | [diff] [blame] | 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 | |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.perftests.utils.BenchmarkState; |
| 21 | import android.perftests.utils.PerfStatusReporter; |
Riddle Hsu | 1be1dd6 | 2019-09-24 17:54:38 -0600 | [diff] [blame^] | 22 | import android.perftests.utils.PerfTestActivity; |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 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; |
Keisuke Kuroyanagi | b7ff133 | 2016-08-19 23:05:02 +0900 | [diff] [blame] | 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 | |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 36 | import java.util.Arrays; |
| 37 | import java.util.Collection; |
| 38 | import java.util.Random; |
Keisuke Kuroyanagi | b7ff133 | 2016-08-19 23:05:02 +0900 | [diff] [blame] | 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 | { "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 Hsu | 1be1dd6 | 2019-09-24 17:54:38 -0600 | [diff] [blame^] | 62 | public ActivityTestRule<PerfTestActivity> mActivityRule = |
| 63 | new ActivityTestRule<>(PerfTestActivity.class); |
Keisuke Kuroyanagi | b7ff133 | 2016-08-19 23:05:02 +0900 | [diff] [blame] | 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 |
Seigo Nonaka | 36e0f12 | 2016-09-16 11:08:00 +0900 | [diff] [blame] | 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 | } |
Keisuke Kuroyanagi | b7ff133 | 2016-08-19 23:05:02 +0900 | [diff] [blame] | 116 | } |
Seigo Nonaka | 36e0f12 | 2016-09-16 11:08:00 +0900 | [diff] [blame] | 117 | }); |
Keisuke Kuroyanagi | b7ff133 | 2016-08-19 23:05:02 +0900 | [diff] [blame] | 118 | } |
| 119 | } |