Seigo Nonaka | e3f1762 | 2016-06-30 18:21:26 +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 | |
Seigo Nonaka | e3f1762 | 2016-06-30 18:21:26 +0900 | [diff] [blame] | 19 | import android.perftests.utils.BenchmarkState; |
| 20 | import android.perftests.utils.PerfStatusReporter; |
| 21 | import android.perftests.utils.StubActivity; |
| 22 | import android.text.Selection; |
| 23 | import android.view.KeyEvent; |
| 24 | import android.view.View.MeasureSpec; |
| 25 | import android.view.ViewGroup; |
| 26 | |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 27 | import androidx.test.InstrumentationRegistry; |
| 28 | import androidx.test.filters.LargeTest; |
| 29 | import androidx.test.rule.ActivityTestRule; |
| 30 | |
Seigo Nonaka | e3f1762 | 2016-06-30 18:21:26 +0900 | [diff] [blame] | 31 | import org.junit.Assert; |
| 32 | import org.junit.Rule; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
Seigo Nonaka | e3f1762 | 2016-06-30 18:21:26 +0900 | [diff] [blame] | 35 | import org.junit.runners.Parameterized; |
Siyamed Sinir | 5f6eda5 | 2017-09-26 22:57:31 -0700 | [diff] [blame] | 36 | import org.junit.runners.Parameterized.Parameters; |
| 37 | |
| 38 | import java.util.Arrays; |
| 39 | import java.util.Collection; |
Seigo Nonaka | e3f1762 | 2016-06-30 18:21:26 +0900 | [diff] [blame] | 40 | |
| 41 | @LargeTest |
| 42 | @RunWith(Parameterized.class) |
| 43 | public class EditTextCursorMovementPerfTest { |
| 44 | |
| 45 | private static final String BOY = "\uD83D\uDC66"; // U+1F466 |
| 46 | private static final String US_FLAG = "\uD83C\uDDFA\uD83C\uDDF8"; // U+1F1FA U+1F1F8 |
| 47 | private static final String FAMILY = |
| 48 | // U+1F469 U+200D U+1F469 U+200D U+1F467 U+200D U+1F467 |
| 49 | "\uD83D\uDC69\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC67"; |
| 50 | |
| 51 | @Parameters(name = "{0}") |
| 52 | public static Collection cases() { |
| 53 | return Arrays.asList(new Object[][] { |
| 54 | { "Latin", "aaa", 1 }, |
| 55 | { "Emoji", BOY + BOY + BOY, 2 }, |
| 56 | { "Flags", US_FLAG + US_FLAG + US_FLAG, 4 }, |
| 57 | { "ZwjSequence", FAMILY + FAMILY + FAMILY, 11 }, |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | private final String mMetricKey; |
| 62 | private final String mText; |
| 63 | private final int mCursorPos; |
| 64 | |
| 65 | private static final KeyEvent LEFT_ARROW_KEY_EVENT = |
| 66 | new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT); |
| 67 | private static final KeyEvent RIGHT_ARROW_KEY_EVENT = |
| 68 | new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT); |
| 69 | |
| 70 | public EditTextCursorMovementPerfTest(String metricKey, String text, int cursorPos) { |
| 71 | mMetricKey = metricKey; |
| 72 | mText = text; |
| 73 | mCursorPos = cursorPos; |
| 74 | } |
| 75 | |
| 76 | @Rule |
| 77 | public ActivityTestRule<StubActivity> mActivityRule = new ActivityTestRule(StubActivity.class); |
| 78 | |
| 79 | @Rule |
| 80 | public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 81 | |
| 82 | @Test |
| 83 | public void testCursorMovement() { |
| 84 | InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { |
| 85 | @Override |
| 86 | public void run() { |
| 87 | EditText editText = new EditText(mActivityRule.getActivity()); |
| 88 | |
| 89 | editText.setText(mText, TextView.BufferType.EDITABLE); |
| 90 | Selection.setSelection(editText.getText(), 0, 0); |
| 91 | |
| 92 | // Layout it here since the cursor movement requires layout information but it |
| 93 | // happens asynchronously even if the view is attached to an Activity. |
| 94 | editText.setLayoutParams(new ViewGroup.LayoutParams( |
| 95 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 96 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 97 | editText.invalidate(); |
| 98 | editText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), |
| 99 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
| 100 | editText.layout(0, 0, 1024, 768); |
| 101 | |
| 102 | // mText contains three grapheme clusters. Move the cursor to the 2nd grapheme |
| 103 | // cluster by forwarding right arrow key event. |
| 104 | editText.onKeyDown(RIGHT_ARROW_KEY_EVENT.getKeyCode(), RIGHT_ARROW_KEY_EVENT); |
| 105 | Assert.assertEquals(mCursorPos, Selection.getSelectionStart(editText.getText())); |
| 106 | |
| 107 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 108 | while (state.keepRunning()) { |
| 109 | editText.onKeyDown(RIGHT_ARROW_KEY_EVENT.getKeyCode(), RIGHT_ARROW_KEY_EVENT); |
| 110 | editText.onKeyDown(LEFT_ARROW_KEY_EVENT.getKeyCode(), LEFT_ARROW_KEY_EVENT); |
| 111 | } |
| 112 | } |
| 113 | }); |
| 114 | } |
| 115 | } |