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. 4 5package org.chromium.content.browser.test.util; 6 7import android.app.Instrumentation; 8 9import org.chromium.base.test.util.InstrumentationUtils; 10import org.chromium.content.browser.ContentView; 11import org.chromium.content.browser.ContentViewCore; 12 13import java.util.concurrent.Callable; 14import java.util.concurrent.TimeUnit; 15 16/** 17 * Collection of utilities related to the UiThread for navigating 18 * through and working with browser forward and back history. 19 */ 20public class HistoryUtils { 21 22 protected final static int WAIT_TIMEOUT_SECONDS = 15; 23 24 /** 25 * Calls {@link ContentView#canGoBack()} on UI thread. 26 * 27 * @param instrumentation an Instrumentation instance. 28 * @param contentViewCore a ContentViewCore instance. 29 * @return result of {@link ContentView#canGoBack()} 30 * @throws Throwable 31 */ 32 public static boolean canGoBackOnUiThread(Instrumentation instrumentation, 33 final ContentViewCore contentViewCore) throws Throwable { 34 return InstrumentationUtils.runOnMainSyncAndGetResult( 35 instrumentation, new Callable<Boolean>() { 36 @Override 37 public Boolean call() { 38 return contentViewCore.canGoBack(); 39 } 40 }); 41 } 42 43 /** 44 * Calls {@link ContentView#canGoToOffset(int)} on UI thread. 45 * 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)} 51 * @throws Throwable 52 */ 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>() { 57 @Override 58 public Boolean call() throws Exception { 59 return contentViewCore.canGoToOffset(offset); 60 } 61 }); 62 } 63 64 /** 65 * Calls {@link ContentView#canGoForward()} on UI thread. 66 * 67 * @param instrumentation an Instrumentation instance. 68 * @param contentViewCore a ContentViewCore instance. 69 * @return result of {@link ContentView#canGoForward()} 70 * @throws Throwable 71 */ 72 public static boolean canGoForwardOnUiThread(Instrumentation instrumentation, 73 final ContentViewCore contentViewCore) throws Throwable { 74 return InstrumentationUtils.runOnMainSyncAndGetResult( 75 instrumentation, new Callable<Boolean>() { 76 @Override 77 public Boolean call() { 78 return contentViewCore.canGoForward(); 79 } 80 }); 81 } 82 83 /** 84 * Calls {@link ContentView#clearHistory()} on UI thread. 85 * 86 * @param instrumentation an Instrumentation instance. 87 * @param contentViewCore a ContentViewCore instance. 88 * @throws Throwable 89 */ 90 public static void clearHistoryOnUiThread(Instrumentation instrumentation, 91 final ContentViewCore contentViewCore) throws Throwable { 92 instrumentation.runOnMainSync(new Runnable() { 93 @Override 94 public void run() { 95 contentViewCore.clearHistory(); 96 } 97 }); 98 } 99 100 /** 101 * Calls {@link ContentView#getUrl()} on UI Thread to get the current URL. 102 * 103 * @param instrumentation an Instrumentation instance. 104 * @param contentViewCore a ContentViewCore instance. 105 * @return the URL of the current page 106 * @throws Throwable 107 */ 108 public static String getUrlOnUiThread(Instrumentation instrumentation, 109 final ContentViewCore contentViewCore) throws Throwable { 110 return InstrumentationUtils.runOnMainSyncAndGetResult( 111 instrumentation, new Callable<String>() { 112 @Override 113 public String call() throws Exception { 114 return contentViewCore.getUrl(); 115 } 116 }); 117 } 118 119 /** 120 * Performs navigation in the history on UI thread and waits until 121 * onPageFinished is called. 122 * 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. 127 * @param offset 128 * @throws Throwable 129 */ 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() { 135 @Override 136 public void run() { 137 contentViewCore.goToOffset(offset); 138 } 139 }); 140 141 // Wait for onPageFinished event or timeout after 30s 142 onPageFinishedHelper.waitForCallback(currentCallCount, 1, 30, TimeUnit.SECONDS); 143 } 144 145 /** 146 * Goes back on UI thread and waits until onPageFinished is called or until 147 * it times out. 148 * 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. 153 * @throws Throwable 154 */ 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() { 160 @Override 161 public void run() { 162 contentViewCore.goBack(); 163 } 164 }); 165 166 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS, 167 TimeUnit.SECONDS); 168 } 169 170 /** 171 * Goes forward on UI thread and waits until onPageFinished is called or until 172 * it times out. 173 * 174 * @param instrumentation an Instrumentation instance. 175 * @param contentViewCore a ContentViewCore instance. 176 * @throws Throwable 177 */ 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() { 183 @Override 184 public void run() { 185 contentViewCore.goForward(); 186 } 187 }); 188 189 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS, 190 TimeUnit.SECONDS); 191 } 192} 193