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.browser.ContentView;
13 import org.chromium.content.browser.ContentViewCore;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.TimeUnit;
19 * Collection of utilities related to the UiThread for navigating
20 * through and working with browser forward and back history.
22 public class HistoryUtils {
24 protected static final long WAIT_TIMEOUT_SECONDS = scaleTimeout(15);
27 * Calls {@link ContentView#canGoBack()} on UI thread.
29 * @param instrumentation an Instrumentation instance.
30 * @param contentViewCore a ContentViewCore instance.
31 * @return result of {@link ContentView#canGoBack()}
34 public static boolean canGoBackOnUiThread(Instrumentation instrumentation,
35 final ContentViewCore contentViewCore) throws Throwable {
36 return InstrumentationUtils.runOnMainSyncAndGetResult(
37 instrumentation, new Callable<Boolean>() {
39 public Boolean call() {
40 return contentViewCore.canGoBack();
46 * Calls {@link ContentViewCore#canGoToOffset(int)} on UI thread.
48 * @param instrumentation an Instrumentation instance.
49 * @param contentViewCore a ContentViewCore instance.
50 * @param offset The number of steps to go on the UI thread, with negative
51 * representing going back.
52 * @return result of {@link ContentViewCore#canGoToOffset(int)}
55 public static boolean canGoToOffsetOnUiThread(Instrumentation instrumentation,
56 final ContentViewCore contentViewCore, final int offset) throws Throwable {
57 return InstrumentationUtils.runOnMainSyncAndGetResult(
58 instrumentation, new Callable<Boolean>() {
60 public Boolean call() throws Exception {
61 return contentViewCore.canGoToOffset(offset);
67 * Calls {@link ContentView#canGoForward()} on UI thread.
69 * @param instrumentation an Instrumentation instance.
70 * @param contentViewCore a ContentViewCore instance.
71 * @return result of {@link ContentView#canGoForward()}
74 public static boolean canGoForwardOnUiThread(Instrumentation instrumentation,
75 final ContentViewCore contentViewCore) throws Throwable {
76 return InstrumentationUtils.runOnMainSyncAndGetResult(
77 instrumentation, new Callable<Boolean>() {
79 public Boolean call() {
80 return contentViewCore.canGoForward();
86 * Calls {@link ContentViewCore#clearHistory()} on UI thread.
88 * @param instrumentation an Instrumentation instance.
89 * @param contentViewCore a ContentViewCore instance.
92 public static void clearHistoryOnUiThread(Instrumentation instrumentation,
93 final ContentViewCore contentViewCore) throws Throwable {
94 instrumentation.runOnMainSync(new Runnable() {
97 contentViewCore.clearHistory();
103 * Calls {@link ContentView#getUrl()} on UI Thread to get the current URL.
105 * @param instrumentation an Instrumentation instance.
106 * @param contentViewCore a ContentViewCore instance.
107 * @return the URL of the current page
110 public static String getUrlOnUiThread(Instrumentation instrumentation,
111 final ContentViewCore contentViewCore) throws Throwable {
112 return InstrumentationUtils.runOnMainSyncAndGetResult(
113 instrumentation, new Callable<String>() {
115 public String call() throws Exception {
116 return contentViewCore.getUrl();
122 * Performs navigation in the history on UI thread and waits until
123 * onPageFinished is called.
125 * @param instrumentation an Instrumentation instance.
126 * @param contentViewCore a ContentViewCore instance.
127 * @param onPageFinishedHelper the CallbackHelper instance associated with the onPageFinished
128 * callback of contentViewCore.
132 public static void goToOffsetSync(Instrumentation instrumentation,
133 final ContentViewCore contentViewCore, CallbackHelper onPageFinishedHelper,
134 final int offset) throws Throwable {
135 int currentCallCount = onPageFinishedHelper.getCallCount();
136 instrumentation.runOnMainSync(new Runnable() {
139 contentViewCore.goToOffset(offset);
143 // Wait for onPageFinished event or timeout after 30s
144 onPageFinishedHelper.waitForCallback(currentCallCount, 1, 30, TimeUnit.SECONDS);
148 * Goes back on UI thread and waits until onPageFinished is called or until
151 * @param instrumentation an Instrumentation instance.
152 * @param contentViewCore a ContentViewCore instance.
153 * @param onPageFinishedHelper the CallbackHelper instance associated with the onPageFinished
154 * callback of contentViewCore.
157 public static void goBackSync(Instrumentation instrumentation,
158 final ContentViewCore contentViewCore,
159 CallbackHelper onPageFinishedHelper) throws Throwable {
160 int currentCallCount = onPageFinishedHelper.getCallCount();
161 instrumentation.runOnMainSync(new Runnable() {
164 contentViewCore.goBack();
168 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
173 * Goes forward on UI thread and waits until onPageFinished is called or until
176 * @param instrumentation an Instrumentation instance.
177 * @param contentViewCore a ContentViewCore instance.
180 public static void goForwardSync(Instrumentation instrumentation,
181 final ContentViewCore contentViewCore,
182 CallbackHelper onPageFinishedHelper) throws Throwable {
183 int currentCallCount = onPageFinishedHelper.getCallCount();
184 instrumentation.runOnMainSync(new Runnable() {
187 contentViewCore.goForward();
191 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,