Commit 801c5e6c authored by Brandon Wylie's avatar Brandon Wylie Committed by Commit Bot

Add class to ensure view focusing is successful before continuing

This will be helpful in methods such as typeInOmnibox, which rely on
the assumption that the omnibox will have focus. Putting it in its own
util file because it seems generally useful.

Bug: 1000315
Change-Id: I7cb390046462c584ad48785963941fce133da0e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1784882
Commit-Queue: Brandon Wylie <wylieb@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#694835}
parent 48fc6783
...@@ -16,7 +16,6 @@ import org.junit.runner.RunWith; ...@@ -16,7 +16,6 @@ import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.Restriction; import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.R; import org.chromium.chrome.R;
...@@ -178,7 +177,6 @@ public class LocationBarLayoutTest { ...@@ -178,7 +177,6 @@ public class LocationBarLayoutTest {
@Test @Test
@SmallTest @SmallTest
@Restriction(UiRestriction.RESTRICTION_TYPE_PHONE) @Restriction(UiRestriction.RESTRICTION_TYPE_PHONE)
@DisabledTest(message = "https://crbug.com/1000315")
public void testShowingVoiceSearchButtonIfUrlBarIsEmpty() public void testShowingVoiceSearchButtonIfUrlBarIsEmpty()
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
setUrlBarTextAndFocus(""); setUrlBarTextAndFocus("");
......
...@@ -151,6 +151,7 @@ android_library("chrome_java_test_support") { ...@@ -151,6 +151,7 @@ android_library("chrome_java_test_support") {
"javatests/src/org/chromium/chrome/test/util/SadTabRule.java", "javatests/src/org/chromium/chrome/test/util/SadTabRule.java",
"javatests/src/org/chromium/chrome/test/util/TabStripUtils.java", "javatests/src/org/chromium/chrome/test/util/TabStripUtils.java",
"javatests/src/org/chromium/chrome/test/util/TranslateUtil.java", "javatests/src/org/chromium/chrome/test/util/TranslateUtil.java",
"javatests/src/org/chromium/chrome/test/util/WaitForFocusHelper.java",
] ]
deps = [ deps = [
"$google_play_services_package:google_play_services_base_java", "$google_play_services_package:google_play_services_base_java",
......
...@@ -55,6 +55,7 @@ import org.chromium.chrome.test.util.ApplicationTestUtils; ...@@ -55,6 +55,7 @@ import org.chromium.chrome.test.util.ApplicationTestUtils;
import org.chromium.chrome.test.util.ChromeTabUtils; import org.chromium.chrome.test.util.ChromeTabUtils;
import org.chromium.chrome.test.util.MenuUtils; import org.chromium.chrome.test.util.MenuUtils;
import org.chromium.chrome.test.util.NewTabPageTestUtils; import org.chromium.chrome.test.util.NewTabPageTestUtils;
import org.chromium.chrome.test.util.WaitForFocusHelper;
import org.chromium.chrome.test.util.browser.Features; import org.chromium.chrome.test.util.browser.Features;
import org.chromium.content_public.browser.LoadUrlParams; import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
...@@ -567,11 +568,12 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe ...@@ -567,11 +568,12 @@ public class ChromeActivityTestRule<T extends ChromeActivity> extends ActivityTe
* TODO(yolandyan): split this into the seperate test rule, this only applies to tabbed mode * TODO(yolandyan): split this into the seperate test rule, this only applies to tabbed mode
*/ */
public void typeInOmnibox(String text, boolean oneCharAtATime) throws InterruptedException { public void typeInOmnibox(String text, boolean oneCharAtATime) throws InterruptedException {
final UrlBar urlBar = (UrlBar) getActivity().findViewById(R.id.url_bar); final UrlBar urlBar = getActivity().findViewById(R.id.url_bar);
Assert.assertNotNull(urlBar); Assert.assertNotNull(urlBar);
WaitForFocusHelper.acquireFocusForView(urlBar);
TestThreadUtils.runOnUiThreadBlocking(() -> { TestThreadUtils.runOnUiThreadBlocking(() -> {
urlBar.requestFocus();
if (!oneCharAtATime) { if (!oneCharAtATime) {
urlBar.setText(text); urlBar.setText(text);
} }
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.test.util;
import android.view.View;
import org.junit.Assert;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.text.MessageFormat;
/**
* An implementation of View.OnFocusChangeListener which composites an existing listener (if any)
* and allows tests to wait for focus to occur. To use this, call #acquireFocusForView with the
* view you want to have focus.
*/
public class WaitForFocusHelper implements View.OnFocusChangeListener {
private static final String WAITING_FOR_FOCUS_TEMPLATE =
"Failed waiting for url focus for view [{0}]";
/**
* Acquires focus for the given view. Will crash and burn if focus isn't acquired successfully.
* @param view The view which will be focused.
*/
public static void acquireFocusForView(View view) {
try {
WaitForFocusHelper listener = new WaitForFocusHelper(view.getOnFocusChangeListener());
view.setOnFocusChangeListener(listener);
int callCount = listener.getOnFocusCallbackHelper().getCallCount();
TestThreadUtils.runOnUiThreadBlocking(() -> view.requestFocus());
if (!view.hasFocus()) {
listener.getOnFocusCallbackHelper().waitForCallback(
MessageFormat.format(WAITING_FOR_FOCUS_TEMPLATE, view), callCount);
}
} catch (Exception e) {
e.printStackTrace();
assert false : MessageFormat.format(WAITING_FOR_FOCUS_TEMPLATE, view);
}
Assert.assertTrue(view.hasFocus());
}
private CallbackHelper mOnFocusCallbackHelper;
private View.OnFocusChangeListener mExistingListener;
WaitForFocusHelper(View.OnFocusChangeListener existingListener) {
mOnFocusCallbackHelper = new CallbackHelper();
mExistingListener = existingListener;
}
CallbackHelper getOnFocusCallbackHelper() {
return mOnFocusCallbackHelper;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (mExistingListener != null) mExistingListener.onFocusChange(v, hasFocus);
mOnFocusCallbackHelper.notifyCalled();
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment