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_public.browser.WebContents; 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 NavigationController#canGoBack()} on UI thread. 27 * 28 * @param instrumentation an Instrumentation instance. 29 * @param contentViewCore a ContentViewCore instance. 30 * @return result of {@link NavigationController#canGoBack()} 31 * @throws Throwable 32 */ 33 public static boolean canGoBackOnUiThread(Instrumentation instrumentation, 34 final WebContents webContents) throws Throwable { 35 return InstrumentationUtils.runOnMainSyncAndGetResult( 36 instrumentation, new Callable<Boolean>() { 37 @Override 38 public Boolean call() { 39 return webContents.getNavigationController().canGoBack(); 40 } 41 }); 42 } 43 44 /** 45 * Calls {@link NavigationController#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 NavigationController#canGoToOffset(int)} 52 * @throws Throwable 53 */ 54 public static boolean canGoToOffsetOnUiThread(Instrumentation instrumentation, 55 final WebContents webContents, final int offset) throws Throwable { 56 return InstrumentationUtils.runOnMainSyncAndGetResult( 57 instrumentation, new Callable<Boolean>() { 58 @Override 59 public Boolean call() throws Exception { 60 return webContents.getNavigationController().canGoToOffset(offset); 61 } 62 }); 63 } 64 65 /** 66 * Calls {@link NavigationController#canGoForward()} on UI thread. 67 * 68 * @param instrumentation an Instrumentation instance. 69 * @param contentViewCore a ContentViewCore instance. 70 * @return result of {@link NavigationController#canGoForward()} 71 * @throws Throwable 72 */ 73 public static boolean canGoForwardOnUiThread(Instrumentation instrumentation, 74 final WebContents webContents) throws Throwable { 75 return InstrumentationUtils.runOnMainSyncAndGetResult( 76 instrumentation, new Callable<Boolean>() { 77 @Override 78 public Boolean call() { 79 return webContents.getNavigationController().canGoForward(); 80 } 81 }); 82 } 83 84 /** 85 * Calls {@link NavigationController#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 WebContents webContents) throws Throwable { 93 instrumentation.runOnMainSync(new Runnable() { 94 @Override 95 public void run() { 96 webContents.getNavigationController().clearHistory(); 97 } 98 }); 99 } 100 101 /** 102 * Calls {@link NavigationController#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 WebContents webContents) throws Throwable { 111 return InstrumentationUtils.runOnMainSyncAndGetResult( 112 instrumentation, new Callable<String>() { 113 @Override 114 public String call() throws Exception { 115 return webContents.getUrl(); 116 } 117 }); 118 } 119 120 /** 121 * Goes back on UI thread and waits until onPageFinished is called or until 122 * it times out. 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 * @throws Throwable 129 */ 130 public static void goBackSync(Instrumentation instrumentation, 131 final WebContents webContents, 132 CallbackHelper onPageFinishedHelper) throws Throwable { 133 int currentCallCount = onPageFinishedHelper.getCallCount(); 134 instrumentation.runOnMainSync(new Runnable() { 135 @Override 136 public void run() { 137 webContents.getNavigationController().goBack(); 138 } 139 }); 140 141 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS, 142 TimeUnit.SECONDS); 143 } 144 145 /** 146 * Goes forward 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 * @throws Throwable 152 */ 153 public static void goForwardSync(Instrumentation instrumentation, 154 final WebContents webContents, 155 CallbackHelper onPageFinishedHelper) throws Throwable { 156 int currentCallCount = onPageFinishedHelper.getCallCount(); 157 instrumentation.runOnMainSync(new Runnable() { 158 @Override 159 public void run() { 160 webContents.getNavigationController().goForward(); 161 } 162 }); 163 164 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS, 165 TimeUnit.SECONDS); 166 } 167 } 168