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