1 // Copyright (c) 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 android.app.Instrumentation;
9 import org.chromium.base.test.util.InstrumentationUtils;
10 import org.chromium.content.browser.ContentView;
11 import org.chromium.content.browser.ContentViewCore;
13 import java.util.concurrent.Callable;
14 import java.util.concurrent.TimeUnit;
17 * Collection of utilities related to the UiThread for navigating
18 * through and working with browser forward and back history.
20 public class HistoryUtils {
22 protected final static int WAIT_TIMEOUT_SECONDS = 15;
25 * Calls {@link ContentView#canGoBack()} on UI thread.
27 * @param instrumentation an Instrumentation instance.
28 * @param contentViewCore a ContentViewCore instance.
29 * @return result of {@link ContentView#canGoBack()}
32 public static boolean canGoBackOnUiThread(Instrumentation instrumentation,
33 final ContentViewCore contentViewCore) throws Throwable {
34 return InstrumentationUtils.runOnMainSyncAndGetResult(
35 instrumentation, new Callable<Boolean>() {
37 public Boolean call() {
38 return contentViewCore.canGoBack();
44 * Calls {@link ContentView#canGoToOffset(int)} on UI thread.
46 * @param instrumentation an Instrumentation instance.
47 * @param contentViewCore a ContentViewCore instance.
48 * @param offset The number of steps to go on the UI thread, with negative
49 * representing going back.
50 * @return result of {@link ContentView#canGoToOffset(int)}
53 public static boolean canGoToOffsetOnUiThread(Instrumentation instrumentation,
54 final ContentViewCore contentViewCore, final int offset) throws Throwable {
55 return InstrumentationUtils.runOnMainSyncAndGetResult(
56 instrumentation, new Callable<Boolean>() {
58 public Boolean call() throws Exception {
59 return contentViewCore.canGoToOffset(offset);
65 * Calls {@link ContentView#canGoForward()} on UI thread.
67 * @param instrumentation an Instrumentation instance.
68 * @param contentViewCore a ContentViewCore instance.
69 * @return result of {@link ContentView#canGoForward()}
72 public static boolean canGoForwardOnUiThread(Instrumentation instrumentation,
73 final ContentViewCore contentViewCore) throws Throwable {
74 return InstrumentationUtils.runOnMainSyncAndGetResult(
75 instrumentation, new Callable<Boolean>() {
77 public Boolean call() {
78 return contentViewCore.canGoForward();
84 * Calls {@link ContentView#clearHistory()} on UI thread.
86 * @param instrumentation an Instrumentation instance.
87 * @param contentViewCore a ContentViewCore instance.
90 public static void clearHistoryOnUiThread(Instrumentation instrumentation,
91 final ContentViewCore contentViewCore) throws Throwable {
92 instrumentation.runOnMainSync(new Runnable() {
95 contentViewCore.clearHistory();
101 * Calls {@link ContentView#getUrl()} on UI Thread to get the current URL.
103 * @param instrumentation an Instrumentation instance.
104 * @param contentViewCore a ContentViewCore instance.
105 * @return the URL of the current page
108 public static String getUrlOnUiThread(Instrumentation instrumentation,
109 final ContentViewCore contentViewCore) throws Throwable {
110 return InstrumentationUtils.runOnMainSyncAndGetResult(
111 instrumentation, new Callable<String>() {
113 public String call() throws Exception {
114 return contentViewCore.getUrl();
120 * Performs navigation in the history on UI thread and waits until
121 * onPageFinished is called.
123 * @param instrumentation an Instrumentation instance.
124 * @param contentViewCore a ContentViewCore instance.
125 * @param onPageFinishedHelper the CallbackHelper instance associated with the onPageFinished
126 * callback of contentViewCore.
130 public static void goToOffsetSync(Instrumentation instrumentation,
131 final ContentViewCore contentViewCore, CallbackHelper onPageFinishedHelper,
132 final int offset) throws Throwable {
133 int currentCallCount = onPageFinishedHelper.getCallCount();
134 instrumentation.runOnMainSync(new Runnable() {
137 contentViewCore.goToOffset(offset);
141 // Wait for onPageFinished event or timeout after 30s
142 onPageFinishedHelper.waitForCallback(currentCallCount, 1, 30, TimeUnit.SECONDS);
146 * Goes back on UI thread and waits until onPageFinished is called or until
149 * @param instrumentation an Instrumentation instance.
150 * @param contentViewCore a ContentViewCore instance.
151 * @param onPageFinishedHelper the CallbackHelper instance associated with the onPageFinished
152 * callback of contentViewCore.
155 public static void goBackSync(Instrumentation instrumentation,
156 final ContentViewCore contentViewCore,
157 CallbackHelper onPageFinishedHelper) throws Throwable {
158 int currentCallCount = onPageFinishedHelper.getCallCount();
159 instrumentation.runOnMainSync(new Runnable() {
162 contentViewCore.goBack();
166 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,
171 * Goes forward on UI thread and waits until onPageFinished is called or until
174 * @param instrumentation an Instrumentation instance.
175 * @param contentViewCore a ContentViewCore instance.
178 public static void goForwardSync(Instrumentation instrumentation,
179 final ContentViewCore contentViewCore,
180 CallbackHelper onPageFinishedHelper) throws Throwable {
181 int currentCallCount = onPageFinishedHelper.getCallCount();
182 instrumentation.runOnMainSync(new Runnable() {
185 contentViewCore.goForward();
189 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS,