Commit e37c39aa authored by Troy Hildebrandt's avatar Troy Hildebrandt Committed by Commit Bot

Fix NewApi Lint errors for SearchActivityTest.

NewApi warnings were suppressed accidentally and an AssertionError
constructor that requires API 19 was used. The NewApi warnings are back,
so this fix avoids using the AssertionError, and throws the exceptions
as they are.

Since a couple calls that were being caught in a try/catch block before
need to throw exceptions but can't from a Runnable, a waitForActivity
method in ActivityUtils was added that takes a Callable<Void> was
added. The original that takes a Runnable is just a wrapper around this
new method so other tests are not affected.

Bug: 805509,803484
Change-Id: I7960b2725b4fad1496fbfde7aafa8e2cf52ec686
Reviewed-on: https://chromium-review.googlesource.com/884362
Commit-Queue: Troy Hildebrandt <thildebr@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/master@{#531951}
parent 7a379f73
......@@ -193,11 +193,15 @@ public class SearchActivityTest {
// Monitor for ChromeTabbedActivity.
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
waitForChromeTabbedActivityToStart(() -> {
// Type in a URL that should get kicked to ChromeTabbedActivity.
setUrlBarText(searchActivity, url);
final UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar);
KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_ENTER);
waitForChromeTabbedActivityToStart(new Callable<Void>() {
@Override
public Void call() {
// Type in a URL that should get kicked to ChromeTabbedActivity.
setUrlBarText(searchActivity, url);
final UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar);
KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_ENTER);
return null;
}
}, url);
}
......@@ -234,10 +238,14 @@ public class SearchActivityTest {
OmniboxTestUtils.waitForOmniboxSuggestions(locationBar);
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
waitForChromeTabbedActivityToStart(() -> {
// Hitting enter should submit the URL and kick the user to the browser.
UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar);
KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_ENTER);
waitForChromeTabbedActivityToStart(new Callable<Void>() {
@Override
public Void call() {
// Hitting enter should submit the URL and kick the user to the browser.
UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar);
KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_ENTER);
return null;
}
}, ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL);
}
......@@ -260,19 +268,19 @@ public class SearchActivityTest {
Assert.assertEquals(searchActivity, ApplicationStatus.getLastTrackedFocusedActivity());
Assert.assertFalse(searchActivity.isFinishing());
waitForChromeTabbedActivityToStart(() -> {
// Finish initialization. It should notice the URL is queued up and start the
// browser.
ThreadUtils.runOnUiThreadBlocking(
() -> { searchActivity.startDelayedNativeInitialization(); });
Assert.assertEquals(
1, mTestDelegate.shouldDelayNativeInitializationCallback.getCallCount());
try {
waitForChromeTabbedActivityToStart(new Callable<Void>() {
@Override
public Void call() throws InterruptedException, TimeoutException {
// Finish initialization. It should notice the URL is queued up and start the
// browser.
ThreadUtils.runOnUiThreadBlocking(
() -> { searchActivity.startDelayedNativeInitialization(); });
Assert.assertEquals(
1, mTestDelegate.shouldDelayNativeInitializationCallback.getCallCount());
mTestDelegate.showSearchEngineDialogIfNeededCallback.waitForCallback(0);
mTestDelegate.onFinishDeferredInitializationCallback.waitForCallback(0);
} catch (InterruptedException | TimeoutException e) {
throw new AssertionError("Unexpected exception", e);
return null;
}
}, ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL);
}
......@@ -348,10 +356,14 @@ public class SearchActivityTest {
OmniboxTestUtils.waitForOmniboxSuggestions(locationBar, OMNIBOX_SHOW_TIMEOUT_MS);
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
waitForChromeTabbedActivityToStart(() -> {
// Hitting enter should submit the URL and kick the user to the browser.
UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar);
KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_ENTER);
waitForChromeTabbedActivityToStart(new Callable<Void>() {
@Override
public Void call() {
// Hitting enter should submit the URL and kick the user to the browser.
UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar);
KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_ENTER);
return null;
}
}, ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL);
}
......@@ -384,10 +396,14 @@ public class SearchActivityTest {
OmniboxTestUtils.waitForOmniboxSuggestions(locationBar);
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
waitForChromeTabbedActivityToStart(() -> {
// Hitting enter should submit the URL and kick the user to the browser.
UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar);
KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_ENTER);
waitForChromeTabbedActivityToStart(new Callable<Void>() {
@Override
public Void call() {
// Hitting enter should submit the URL and kick the user to the browser.
UrlBar urlBar = (UrlBar) searchActivity.findViewById(R.id.url_bar);
KeyUtils.singleKeyEventView(instrumentation, urlBar, KeyEvent.KEYCODE_ENTER);
return null;
}
}, ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL);
}
......@@ -471,7 +487,7 @@ public class SearchActivityTest {
return (SearchActivity) searchActivity;
}
private void waitForChromeTabbedActivityToStart(Runnable trigger, String expectedUrl)
private void waitForChromeTabbedActivityToStart(Callable<Void> trigger, String expectedUrl)
throws Exception {
final ChromeTabbedActivity cta = ActivityUtils.waitForActivity(
InstrumentationRegistry.getInstrumentation(), ChromeTabbedActivity.class, trigger);
......
......@@ -29,6 +29,7 @@ import org.chromium.content.browser.test.util.CriteriaHelper;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
/**
* Collection of activity utilities.
......@@ -105,6 +106,33 @@ public class ActivityUtils {
*/
public static <T> T waitForActivity(Instrumentation instrumentation, Class<T> activityType,
Runnable activityTrigger) {
Callable<Void> callableWrapper = new Callable<Void>() {
@Override
public Void call() {
activityTrigger.run();
return null;
}
};
try {
return waitForActivityWithTimeout(
instrumentation, activityType, callableWrapper, ACTIVITY_START_TIMEOUT_MS);
} catch (Exception e) {
// We just ignore checked exceptions here since Runnables can't throw them.
}
return null;
}
/**
* Captures an activity of a particular type that is triggered from some action.
*
* @param <T> The type of activity to wait for.
* @param activityType The class type of the activity.
* @param activityTrigger The action that will trigger the new activity (run in this thread).
* @return The spawned activity.
*/
public static <T> T waitForActivity(Instrumentation instrumentation, Class<T> activityType,
Callable<Void> activityTrigger) throws Exception {
return waitForActivityWithTimeout(instrumentation, activityType, activityTrigger,
ACTIVITY_START_TIMEOUT_MS);
}
......@@ -118,11 +146,11 @@ public class ActivityUtils {
* @return The spawned activity.
*/
public static <T> T waitForActivityWithTimeout(Instrumentation instrumentation,
Class<T> activityType, Runnable activityTrigger, long timeOut) {
Class<T> activityType, Callable<Void> activityTrigger, long timeOut) throws Exception {
ActivityMonitor monitor =
instrumentation.addMonitor(activityType.getCanonicalName(), null, false);
activityTrigger.run();
activityTrigger.call();
instrumentation.waitForIdleSync();
Activity activity = monitor.getLastActivity();
if (activity == null) {
......
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