Seigo Nonaka | 2a154e2 | 2016-07-01 15:50:14 +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 | |
| 19 | import android.app.Activity; |
| 20 | import android.os.Bundle; |
| 21 | import android.perftests.utils.BenchmarkState; |
| 22 | import android.perftests.utils.PerfStatusReporter; |
| 23 | import android.perftests.utils.StubActivity; |
| 24 | import android.text.Selection; |
| 25 | import android.view.KeyEvent; |
| 26 | import android.view.View.MeasureSpec; |
| 27 | import android.view.ViewGroup; |
| 28 | |
| 29 | import android.support.test.InstrumentationRegistry; |
| 30 | import android.support.test.filters.LargeTest; |
| 31 | import android.support.test.rule.ActivityTestRule; |
| 32 | import android.support.test.runner.AndroidJUnit4; |
| 33 | |
| 34 | import java.util.Arrays; |
| 35 | import java.util.Collection; |
| 36 | import java.util.Locale; |
| 37 | |
| 38 | import org.junit.Assert; |
| 39 | import org.junit.Rule; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.junit.runners.Parameterized.Parameters; |
| 43 | import org.junit.runners.Parameterized; |
| 44 | |
| 45 | @LargeTest |
| 46 | @RunWith(Parameterized.class) |
| 47 | public class EditTextBackspacePerfTest { |
| 48 | |
| 49 | private static final String BOY = "\uD83D\uDC66"; // U+1F466 |
| 50 | private static final String US_FLAG = "\uD83C\uDDFA\uD83C\uDDF8"; // U+1F1FA U+1F1F8 |
| 51 | private static final String FAMILY = |
| 52 | // U+1F469 U+200D U+1F469 U+200D U+1F467 U+200D U+1F467 |
| 53 | "\uD83D\uDC69\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC67"; |
| 54 | private static final String EMOJI_MODIFIER = "\uD83C\uDFFD"; // U+1F3FD |
| 55 | private static final String KEYCAP = "\u20E3"; |
| 56 | private static final String COLOR_COPYRIGHT = "\u00A9\uFE0F"; |
| 57 | |
| 58 | @Parameters(name = "{0}") |
| 59 | public static Collection cases() { |
| 60 | return Arrays.asList(new Object[][] { |
| 61 | { "Latin", "aaa", 1 }, |
| 62 | { "Flags", US_FLAG + US_FLAG + US_FLAG, 4 }, |
| 63 | { "EmojiModifier", |
| 64 | BOY + EMOJI_MODIFIER + BOY + EMOJI_MODIFIER + BOY + EMOJI_MODIFIER, 4 }, |
| 65 | { "KeyCap", "1" + KEYCAP + "1" + KEYCAP + "1" + KEYCAP, 2 }, |
| 66 | { "ZwjSequence", FAMILY + FAMILY + FAMILY, 11 }, |
| 67 | { "VariationSelector", COLOR_COPYRIGHT + COLOR_COPYRIGHT + COLOR_COPYRIGHT, 2 }, |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | private final String mMetricKey; |
| 72 | private final String mText; |
| 73 | private final int mCursorPos; |
| 74 | |
| 75 | private static final KeyEvent BACKSPACE_KEY_EVENT = |
| 76 | new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL); |
| 77 | private static final KeyEvent RIGHT_ARROW_KEY_EVENT = |
| 78 | new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT); |
| 79 | |
| 80 | public EditTextBackspacePerfTest(String metricKey, String text, int cursorPos) { |
| 81 | mMetricKey = metricKey; |
| 82 | mText = text; |
| 83 | mCursorPos = cursorPos; |
| 84 | } |
| 85 | |
| 86 | @Rule |
| 87 | public ActivityTestRule<StubActivity> mActivityRule = new ActivityTestRule(StubActivity.class); |
| 88 | |
| 89 | @Rule |
| 90 | public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 91 | |
| 92 | private void prepareTextForBackspace(EditText editText) { |
| 93 | editText.setText(mText, TextView.BufferType.EDITABLE); |
| 94 | Selection.setSelection(editText.getText(), 0, 0); |
| 95 | |
| 96 | // Do layout it here since the cursor movement requires layout information but it |
| 97 | // happens asynchronously even if the view is attached to an Activity. |
| 98 | editText.setLayoutParams(new ViewGroup.LayoutParams( |
| 99 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 100 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 101 | editText.invalidate(); |
| 102 | editText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), |
| 103 | MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); |
| 104 | editText.layout(0, 0, 1024, 768); |
| 105 | |
| 106 | // mText contains three grapheme clusters. Move the cursor to the 2nd grapheme |
| 107 | // cluster by forwarding right arrow key event. |
| 108 | editText.onKeyDown(RIGHT_ARROW_KEY_EVENT.getKeyCode(), RIGHT_ARROW_KEY_EVENT); |
| 109 | Assert.assertEquals(mCursorPos, Selection.getSelectionStart(editText.getText())); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testBackspace() { |
| 114 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 115 | EditText editText = new EditText(mActivityRule.getActivity()); |
| 116 | |
| 117 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 118 | while (state.keepRunning()) { |
| 119 | // Prepare the test data for this iteration with pausing timer. |
| 120 | state.pauseTiming(); |
| 121 | prepareTextForBackspace(editText); |
| 122 | state.resumeTiming(); |
| 123 | |
| 124 | editText.onKeyDown(BACKSPACE_KEY_EVENT.getKeyCode(), BACKSPACE_KEY_EVENT); |
| 125 | } |
| 126 | }); |
| 127 | } |
| 128 | } |