• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.base.ThreadUtils;
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 final TestContentViewClient mTestContentViewClient;
19     private TestWebContentsObserver mTestWebContentsObserver;
20 
TestCallbackHelperContainer(final ContentViewCore contentViewCore)21     public TestCallbackHelperContainer(final ContentViewCore contentViewCore) {
22         mTestContentViewClient = new TestContentViewClient();
23         contentViewCore.setContentViewClient(mTestContentViewClient);
24         // TODO(yfriedman): Change callers to be executed on the UI thread. Unfortunately this is
25         // super convenient as the caller is nearly always on the test thread which is fine to block
26         // and it's cumbersome to keep bouncing to the UI thread.
27         ThreadUtils.runOnUiThreadBlocking(new Runnable() {
28             @Override
29             public void run() {
30                 mTestWebContentsObserver = new TestWebContentsObserver(contentViewCore);
31             }
32         });
33     }
34 
TestCallbackHelperContainer( TestContentViewClient viewClient, TestWebContentsObserver contentsObserver)35     protected TestCallbackHelperContainer(
36             TestContentViewClient viewClient, TestWebContentsObserver contentsObserver) {
37         mTestContentViewClient = viewClient;
38         mTestWebContentsObserver = contentsObserver;
39     }
40 
41     /**
42      * CallbackHelper for OnPageFinished.
43      */
44     public static class OnPageFinishedHelper extends CallbackHelper {
45         private String mUrl;
notifyCalled(String url)46         public void notifyCalled(String url) {
47             mUrl = url;
48             notifyCalled();
49         }
getUrl()50         public String getUrl() {
51             assert getCallCount() > 0;
52             return mUrl;
53         }
54     }
55 
56     /**
57      * CallbackHelper for OnPageStarted.
58      */
59     public static class OnPageStartedHelper extends CallbackHelper {
60         private String mUrl;
notifyCalled(String url)61         public void notifyCalled(String url) {
62             mUrl = url;
63             notifyCalled();
64         }
getUrl()65         public String getUrl() {
66             assert getCallCount() > 0;
67             return mUrl;
68         }
69     }
70 
71     /**
72      * CallbackHelper for OnReceivedError.
73      */
74     public static class OnReceivedErrorHelper extends CallbackHelper {
75         private int mErrorCode;
76         private String mDescription;
77         private String mFailingUrl;
notifyCalled(int errorCode, String description, String failingUrl)78         public void notifyCalled(int errorCode, String description, String failingUrl) {
79             mErrorCode = errorCode;
80             mDescription = description;
81             mFailingUrl = failingUrl;
82             notifyCalled();
83         }
getErrorCode()84         public int getErrorCode() {
85             assert getCallCount() > 0;
86             return mErrorCode;
87         }
getDescription()88         public String getDescription() {
89             assert getCallCount() > 0;
90             return mDescription;
91         }
getFailingUrl()92         public String getFailingUrl() {
93             assert getCallCount() > 0;
94             return mFailingUrl;
95         }
96     }
97 
98     /**
99      * CallbackHelper for OnEvaluateJavaScriptResult.
100      * This class wraps the evaluation of JavaScript code allowing test code to
101      * synchronously evaluate JavaScript and then test the result.
102      */
103     public static class OnEvaluateJavaScriptResultHelper extends CallbackHelper {
104         private String mJsonResult;
105 
106         /**
107          * Starts evaluation of a given JavaScript code on a given contentViewCore.
108          * @param contentViewCore A ContentViewCore instance to be used.
109          * @param code A JavaScript code to be evaluated.
110          */
evaluateJavaScript(ContentViewCore contentViewCore, String code)111         public void evaluateJavaScript(ContentViewCore contentViewCore, String code) {
112             ContentViewCore.JavaScriptCallback callback =
113                 new ContentViewCore.JavaScriptCallback() {
114                     @Override
115                     public void handleJavaScriptResult(String jsonResult) {
116                         notifyCalled(jsonResult);
117                     }
118                 };
119             contentViewCore.evaluateJavaScript(code, callback);
120             mJsonResult = null;
121         }
122 
123         /**
124          * Returns true if the evaluation started by evaluateJavaScript() has completed.
125          */
hasValue()126         public boolean hasValue() {
127             return mJsonResult != null;
128         }
129 
130         /**
131          * Returns the JSON result of a previously completed JavaScript evaluation and
132          * resets the helper to accept new evaluations.
133          * @return String JSON result of a previously completed JavaScript evaluation.
134          */
getJsonResultAndClear()135         public String getJsonResultAndClear() {
136             assert hasValue();
137             String result = mJsonResult;
138             mJsonResult = null;
139             return result;
140         }
141 
142 
143         /**
144          * Returns a criteria that checks that the evaluation has finished.
145          */
getHasValueCriteria()146         public Criteria getHasValueCriteria() {
147             return new Criteria() {
148                 @Override
149                 public boolean isSatisfied() {
150                     return hasValue();
151                 }
152             };
153         }
154 
155         /**
156          * Waits till the JavaScript evaluation finishes and returns true if a value was returned,
157          * false if it timed-out.
158          */
waitUntilHasValue(long timeout, TimeUnit timeoutUnits)159         public boolean waitUntilHasValue(long timeout, TimeUnit timeoutUnits)
160                 throws InterruptedException, TimeoutException {
161             waitUntilCriteria(getHasValueCriteria(), timeout, timeoutUnits);
162             return hasValue();
163         }
164 
waitUntilHasValue()165         public boolean waitUntilHasValue() throws InterruptedException, TimeoutException {
166             waitUntilCriteria(getHasValueCriteria());
167             return hasValue();
168         }
169 
notifyCalled(String jsonResult)170         public void notifyCalled(String jsonResult) {
171             assert !hasValue();
172             mJsonResult = jsonResult;
173             notifyCalled();
174         }
175     }
176 
177     /**
178      * CallbackHelper for OnStartContentIntent.
179      */
180     public static class OnStartContentIntentHelper extends CallbackHelper {
181         private String mIntentUrl;
182         public void notifyCalled(String intentUrl) {
183             mIntentUrl = intentUrl;
184             notifyCalled();
185         }
186         public String getIntentUrl() {
187             assert getCallCount() > 0;
188             return mIntentUrl;
189         }
190     }
191 
192     public OnPageStartedHelper getOnPageStartedHelper() {
193         return mTestWebContentsObserver.getOnPageStartedHelper();
194     }
195 
196     public OnPageFinishedHelper getOnPageFinishedHelper() {
197         return mTestWebContentsObserver.getOnPageFinishedHelper();
198     }
199 
200     public OnReceivedErrorHelper getOnReceivedErrorHelper() {
201         return mTestWebContentsObserver.getOnReceivedErrorHelper();
202     }
203 
204     public OnStartContentIntentHelper getOnStartContentIntentHelper() {
205         return mTestContentViewClient.getOnStartContentIntentHelper();
206     }
207 }
208