| Index: content/public/test/android/javatests/src/org/chromium/content/browser/test/util/KeyUtils.java
|
| diff --git a/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/KeyUtils.java b/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/KeyUtils.java
|
| index 0abbe97bb2970f3b7a2e2cc41116a17cdce2e0f9..f421503f63ab880d38bc74cd01a5673f05237aac 100644
|
| --- a/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/KeyUtils.java
|
| +++ b/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/KeyUtils.java
|
| @@ -5,61 +5,56 @@
|
| package org.chromium.content.browser.test.util;
|
|
|
| import android.app.Instrumentation;
|
| +import android.os.SystemClock;
|
| import android.view.KeyEvent;
|
| +import android.view.View;
|
| +
|
| +import org.chromium.base.ThreadUtils;
|
|
|
| /**
|
| * Collection of keyboard utilities.
|
| */
|
| public class KeyUtils {
|
| /**
|
| - * Press "Enter".
|
| + * Sends (synchronously) a single key down/up pair of events to the specified view.
|
| + * <p>
|
| + * Does not use the event injecting framework, but instead relies on
|
| + * {@link View#dispatchKeyEventPreIme(KeyEvent)} and {@link View#dispatchKeyEvent(KeyEvent)} of
|
| + * the view itself
|
| + * <p>
|
| + * The event injecting framework requires INJECT_EVENTS permission and that has been flaky on
|
| + * our perf bots. So until a root cause of the issue can be found, we should use this instead
|
| + * of the functionality provided by {@link #sendKeys(int...)}.
|
| + *
|
| + * @param i The application being instrumented.
|
| + * @param v The view to receive the key event.
|
| + * @param keyCode The keycode for the event to be issued.
|
| */
|
| - public static void pressEnter(Instrumentation instrumentation) {
|
| - instrumentation.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
|
| - KeyEvent.KEYCODE_ENTER));
|
| - instrumentation.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
|
| - KeyEvent.KEYCODE_ENTER));
|
| - instrumentation.waitForIdleSync();
|
| - }
|
| + public static void singleKeyEventView(Instrumentation i, final View v, int keyCode) {
|
| + long downTime = SystemClock.uptimeMillis();
|
| + long eventTime = SystemClock.uptimeMillis();
|
|
|
| - /**
|
| - * Press "Tab".
|
| - */
|
| - public static void pressTab(Instrumentation instrumentation) {
|
| - instrumentation.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
|
| - KeyEvent.KEYCODE_TAB));
|
| - instrumentation.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
|
| - KeyEvent.KEYCODE_TAB));
|
| - instrumentation.waitForIdleSync();
|
| - }
|
| + final KeyEvent downEvent =
|
| + new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN, keyCode, 0);
|
| + dispatchKeyEvent(i, v, downEvent);
|
|
|
| - /**
|
| - * Press "Backspace".
|
| - */
|
| - public static void pressBackspace(Instrumentation instrumentation) {
|
| - instrumentation.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
|
| - KeyEvent.KEYCODE_DEL));
|
| - instrumentation.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
|
| - KeyEvent.KEYCODE_DEL));
|
| - instrumentation.waitForIdleSync();
|
| + downTime = SystemClock.uptimeMillis();
|
| + eventTime = SystemClock.uptimeMillis();
|
| + final KeyEvent upEvent =
|
| + new KeyEvent(downTime, eventTime, KeyEvent.ACTION_UP, keyCode, 0);
|
| + dispatchKeyEvent(i, v, upEvent);
|
| }
|
|
|
| - /**
|
| - * Press "Back".
|
| - */
|
| - public static void pressBack(Instrumentation instrumentation) {
|
| - instrumentation.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
|
| - KeyEvent.KEYCODE_BACK));
|
| - instrumentation.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
|
| - KeyEvent.KEYCODE_BACK));
|
| - instrumentation.waitForIdleSync();
|
| - }
|
| -
|
| - /**
|
| - * Input a String.
|
| - */
|
| - public static void inputString(Instrumentation instrumentation, String text) {
|
| - instrumentation.sendStringSync(text);
|
| - instrumentation.waitForIdleSync();
|
| + private static void dispatchKeyEvent(final Instrumentation i, final View v,
|
| + final KeyEvent event) {
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + if (!v.dispatchKeyEventPreIme(event)) {
|
| + v.dispatchKeyEvent(event);
|
| + }
|
| + }
|
| + });
|
| + i.waitForIdleSync();
|
| }
|
| -}
|
| +}
|
|
|