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.app.Activity;
20import android.os.Bundle;
21import android.perftests.utils.BenchmarkState;
22import android.perftests.utils.PerfStatusReporter;
23import android.perftests.utils.StubActivity;
24import android.text.Selection;
25import android.view.KeyEvent;
26import android.view.View.MeasureSpec;
27import android.view.ViewGroup;
28
29import android.support.test.InstrumentationRegistry;
30import android.support.test.filters.LargeTest;
31import android.support.test.rule.ActivityTestRule;
32import android.support.test.runner.AndroidJUnit4;
33
34import java.util.Arrays;
35import java.util.Collection;
36import java.util.Locale;
37
38import org.junit.Assert;
39import org.junit.Rule;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.junit.runners.Parameterized.Parameters;
43import org.junit.runners.Parameterized;
44
45@LargeTest
46@RunWith(Parameterized.class)
47public class EditTextCursorMovementPerfTest {
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
55    @Parameters(name = "{0}")
56    public static Collection cases() {
57        return Arrays.asList(new Object[][] {
58            { "Latin", "aaa", 1 },
59            { "Emoji", BOY + BOY + BOY, 2 },
60            { "Flags", US_FLAG + US_FLAG + US_FLAG, 4 },
61            { "ZwjSequence", FAMILY + FAMILY + FAMILY, 11 },
62        });
63    }
64
65    private final String mMetricKey;
66    private final String mText;
67    private final int mCursorPos;
68
69    private static final KeyEvent LEFT_ARROW_KEY_EVENT =
70            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT);
71    private static final KeyEvent RIGHT_ARROW_KEY_EVENT =
72            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT);
73
74    public EditTextCursorMovementPerfTest(String metricKey, String text, int cursorPos) {
75        mMetricKey = metricKey;
76        mText = text;
77        mCursorPos = cursorPos;
78    }
79
80    @Rule
81    public ActivityTestRule<StubActivity> mActivityRule = new ActivityTestRule(StubActivity.class);
82
83    @Rule
84    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
85
86    @Test
87    public void testCursorMovement() {
88        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
89            @Override
90            public void run() {
91                EditText editText = new EditText(mActivityRule.getActivity());
92
93                editText.setText(mText, TextView.BufferType.EDITABLE);
94                Selection.setSelection(editText.getText(), 0, 0);
95
96                // 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                BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
112                while (state.keepRunning()) {
113                    editText.onKeyDown(RIGHT_ARROW_KEY_EVENT.getKeyCode(), RIGHT_ARROW_KEY_EVENT);
114                    editText.onKeyDown(LEFT_ARROW_KEY_EVENT.getKeyCode(), LEFT_ARROW_KEY_EVENT);
115                }
116            }
117        });
118    }
119}
120