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