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 8 import org.chromium.content.browser.ContentView; 9 import org.chromium.content.browser.ContentViewCore; 10 11 import java.util.concurrent.TimeUnit; 12 import java.util.concurrent.TimeoutException; 13 14 /** 15 * This class is used to provide callback hooks for tests and related classes. 16 */ 17 public class TestCallbackHelperContainer { 18 private TestContentViewClient mTestContentViewClient; 19 private TestWebContentsObserver mTestWebContentsObserver; 20 TestCallbackHelperContainer(ContentView contentView)21 public TestCallbackHelperContainer(ContentView contentView) { 22 mTestContentViewClient = new TestContentViewClient(); 23 contentView.getContentViewCore().setContentViewClient(mTestContentViewClient); 24 mTestWebContentsObserver = new TestWebContentsObserver(contentView.getContentViewCore()); 25 } 26 TestCallbackHelperContainer( TestContentViewClient viewClient, TestWebContentsObserver contentsObserver)27 protected TestCallbackHelperContainer( 28 TestContentViewClient viewClient, TestWebContentsObserver contentsObserver) { 29 mTestContentViewClient = viewClient; 30 mTestWebContentsObserver = contentsObserver; 31 } 32 33 public static class OnPageFinishedHelper extends CallbackHelper { 34 private String mUrl; notifyCalled(String url)35 public void notifyCalled(String url) { 36 mUrl = url; 37 notifyCalled(); 38 } getUrl()39 public String getUrl() { 40 assert getCallCount() > 0; 41 return mUrl; 42 } 43 } 44 45 public static class OnPageStartedHelper extends CallbackHelper { 46 private String mUrl; notifyCalled(String url)47 public void notifyCalled(String url) { 48 mUrl = url; 49 notifyCalled(); 50 } getUrl()51 public String getUrl() { 52 assert getCallCount() > 0; 53 return mUrl; 54 } 55 } 56 57 public static class OnReceivedErrorHelper extends CallbackHelper { 58 private int mErrorCode; 59 private String mDescription; 60 private String mFailingUrl; notifyCalled(int errorCode, String description, String failingUrl)61 public void notifyCalled(int errorCode, String description, String failingUrl) { 62 mErrorCode = errorCode; 63 mDescription = description; 64 mFailingUrl = failingUrl; 65 notifyCalled(); 66 } getErrorCode()67 public int getErrorCode() { 68 assert getCallCount() > 0; 69 return mErrorCode; 70 } getDescription()71 public String getDescription() { 72 assert getCallCount() > 0; 73 return mDescription; 74 } getFailingUrl()75 public String getFailingUrl() { 76 assert getCallCount() > 0; 77 return mFailingUrl; 78 } 79 } 80 81 public static class OnEvaluateJavaScriptResultHelper extends CallbackHelper { 82 private String mJsonResult; 83 84 /** 85 * Starts evaluation of a given JavaScript code on a given contentViewCore. 86 * @param contentViewCore A ContentViewCore instance to be used. 87 * @param code A JavaScript code to be evaluated. 88 */ evaluateJavaScript(ContentViewCore contentViewCore, String code)89 public void evaluateJavaScript(ContentViewCore contentViewCore, String code) { 90 ContentViewCore.JavaScriptCallback callback = 91 new ContentViewCore.JavaScriptCallback() { 92 @Override 93 public void handleJavaScriptResult(String jsonResult) { 94 notifyCalled(jsonResult); 95 } 96 }; 97 contentViewCore.evaluateJavaScript(code, callback); 98 mJsonResult = null; 99 } 100 101 /** 102 * Returns true if the evaluation started by evaluateJavaScript() has completed. 103 */ hasValue()104 public boolean hasValue() { 105 return mJsonResult != null; 106 } 107 108 /** 109 * Returns the JSON result of a previously completed JavaScript evaluation and 110 * resets the helper to accept new evaluations. 111 * @return String JSON result of a previously completed JavaScript evaluation. 112 */ getJsonResultAndClear()113 public String getJsonResultAndClear() { 114 assert hasValue(); 115 String result = mJsonResult; 116 mJsonResult = null; 117 return result; 118 } 119 120 121 /** 122 * Returns a criteria that checks that the evaluation has finished. 123 */ getHasValueCriteria()124 public Criteria getHasValueCriteria() { 125 return new Criteria() { 126 @Override 127 public boolean isSatisfied() { 128 return hasValue(); 129 } 130 }; 131 } 132 133 /** 134 * Waits till the JavaScript evaluation finishes and returns true if a value was returned, 135 * false if it timed-out. 136 */ waitUntilHasValue(long timeout, TimeUnit timeoutUnits)137 public boolean waitUntilHasValue(long timeout, TimeUnit timeoutUnits) 138 throws InterruptedException, TimeoutException { 139 waitUntilCriteria(getHasValueCriteria(), timeout, timeoutUnits); 140 return hasValue(); 141 } 142 waitUntilHasValue()143 public boolean waitUntilHasValue() throws InterruptedException, TimeoutException { 144 waitUntilCriteria(getHasValueCriteria()); 145 return hasValue(); 146 } 147 notifyCalled(String jsonResult)148 public void notifyCalled(String jsonResult) { 149 assert !hasValue(); 150 mJsonResult = jsonResult; 151 notifyCalled(); 152 } 153 } 154 155 public static class OnStartContentIntentHelper extends CallbackHelper { 156 private String mIntentUrl; 157 public void notifyCalled(String intentUrl) { 158 mIntentUrl = intentUrl; 159 notifyCalled(); 160 } 161 public String getIntentUrl() { 162 assert getCallCount() > 0; 163 return mIntentUrl; 164 } 165 } 166 167 public OnPageStartedHelper getOnPageStartedHelper() { 168 return mTestWebContentsObserver.getOnPageStartedHelper(); 169 } 170 171 public OnPageFinishedHelper getOnPageFinishedHelper() { 172 return mTestWebContentsObserver.getOnPageFinishedHelper(); 173 } 174 175 public OnReceivedErrorHelper getOnReceivedErrorHelper() { 176 return mTestWebContentsObserver.getOnReceivedErrorHelper(); 177 } 178 179 public OnEvaluateJavaScriptResultHelper getOnEvaluateJavaScriptResultHelper() { 180 return mTestContentViewClient.getOnEvaluateJavaScriptResultHelper(); 181 } 182 183 public OnStartContentIntentHelper getOnStartContentIntentHelper() { 184 return mTestContentViewClient.getOnStartContentIntentHelper(); 185 } 186 } 187