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