1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser.test.util;
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
9 import android.app.Instrumentation;
11 import org.chromium.base.test.util.InstrumentationUtils;
12 import org.chromium.content_public.browser.WebContents;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.TimeUnit;
18 * Collection of utilities related to the UiThread for navigating
19 * through and working with browser forward and back history.
21 public class HistoryUtils {
23 protected static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(15);
26 * Calls {@link NavigationController#canGoBack()} on UI thread.
28 * @param instrumentation an Instrumentation instance.
29 * @param contentViewCore a ContentViewCore instance.
30 * @return result of {@link NavigationController#canGoBack()}
33 public static boolean canGoBackOnUiThread(Instrumentation instrumentation,
34 final WebContents webContents) throws Throwable {
35 return InstrumentationUtils.runOnMainSyncAndGetResult(
36 instrumentation, new Callable<Boolean>() {
38 public Boolean call() {
39 return webContents.getNavigationController().canGoBack();
45 * Calls {@link NavigationController#canGoToOffset(int)} on UI thread.
47 * @param instrumentation an Instrumentation instance.
48 * @param contentViewCore a ContentViewCore instance.
49 * @param offset The number of steps to go on the UI thread, with negative
50 * representing going back.
51 * @return result of {@link NavigationController#canGoToOffset(int)}
54 public static boolean canGoToOffsetOnUiThread(Instrumentation instrumentation,
55 final WebContents webContents, final int offset) throws Throwable {
56 return InstrumentationUtils.runOnMainSyncAndGetResult(
57 instrumentation, new Callable<Boolean>() {
59 public Boolean call() throws Exception {
60 return webContents.getNavigationController().canGoToOffset(offset);
66 * Calls {@link NavigationController#canGoForward()} on UI thread.
68 * @param instrumentation an Instrumentation instance.
69 * @param contentViewCore a ContentViewCore instance.
70 * @return result of {@link NavigationController#canGoForward()}
73 public static boolean canGoForwardOnUiThread(Instrumentation instrumentation,
74 final WebContents webContents) throws Throwable {
75 return InstrumentationUtils.runOnMainSyncAndGetResult(
76 instrumentation, new Callable<Boolean>() {
78 public Boolean call() {
79 return webContents.getNavigationController().canGoForward();
85 * Calls {@link NavigationController#clearHistory()} on UI thread.
87 * @param instrumentation an Instrumentation instance.
88 * @param contentViewCore a ContentViewCore instance.
91 public static void clearHistoryOnUiThread(Instrumentation instrumentation,
92 final WebContents webContents) throws Throwable {
93 instrumentation.runOnMainSync(new Runnable() {
96 webContents.getNavigationController().clearHistory();
102 * Calls {@link NavigationController#getUrl()} on UI Thread to get the current URL.
104 * @param instrumentation an Instrumentation instance.
105 * @param contentViewCore a ContentViewCore instance.
106 * @return the URL of the current page
109 public static String getUrlOnUiThread(Instrumentation instrumentation,
110 final WebContents webContents) throws Throwable {
111 return InstrumentationUtils.runOnMainSyncAndGetResult(
112 instrumentation, new Callable<String>() {
114 public String call() throws Exception {
115 return webContents.getUrl();
121 * Goes back on UI thread and waits until onPageFinished is called or until
124 * @param instrumentation an Instrumentation instance.
125 * @param contentViewCore a ContentViewCore instance.
126 * @param onPageFinishedHelper the CallbackHelper instance associated with the onPageFinished
127 * callback of contentViewCore.
130 public static void goBackSync(Instrumentation instrumentation,
131 final WebContents webContents,
132 CallbackHelper onPageFinishedHelper) throws Throwable {
133 int currentCallCount = onPageFinishedHelper.getCallCount();
134 instrumentation.runOnMainSync(new Runnable() {
137 webContents.getNavigationController().goBack();
141 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
146 * Goes forward on UI thread and waits until onPageFinished is called or until
149 * @param instrumentation an Instrumentation instance.
150 * @param contentViewCore a ContentViewCore instance.
153 public static void goForwardSync(Instrumentation instrumentation,
154 final WebContents webContents,
155 CallbackHelper onPageFinishedHelper) throws Throwable {
156 int currentCallCount = onPageFinishedHelper.getCallCount();
157 instrumentation.runOnMainSync(new Runnable() {
160 webContents.getNavigationController().goForward();
164 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,