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