OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview.test; | 5 package org.chromium.android_webview.test; |
6 | 6 |
7 import android.app.Instrumentation; | 7 import android.app.Instrumentation; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.test.ActivityInstrumentationTestCase2; | 9 import android.test.ActivityInstrumentationTestCase2; |
10 | 10 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 getInstrumentation().waitForIdleSync(); | 67 getInstrumentation().waitForIdleSync(); |
68 getInstrumentation().runOnMainSync(task); | 68 getInstrumentation().runOnMainSync(task); |
69 try { | 69 try { |
70 return task.get(); | 70 return task.get(); |
71 } catch (ExecutionException e) { | 71 } catch (ExecutionException e) { |
72 // Unwrap the cause of the exception and re-throw it. | 72 // Unwrap the cause of the exception and re-throw it. |
73 throw e.getCause(); | 73 throw e.getCause(); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 protected void enableJavaScriptOnUiThread(final AwContents awContents) { | 77 protected void enableJavaScriptOnUiThread(final ContentViewCore contentViewC
ore) { |
78 getInstrumentation().runOnMainSync(new Runnable() { | 78 getInstrumentation().runOnMainSync(new Runnable() { |
79 @Override | 79 @Override |
80 public void run() { | 80 public void run() { |
81 awContents.getContentViewCore().getContentSettings().setJavaScri
ptEnabled(true); | 81 contentViewCore.getContentSettings().setJavaScriptEnabled(true); |
82 } | 82 } |
83 }); | 83 }); |
84 } | 84 } |
85 | 85 |
86 /** | 86 /** |
87 * Loads url on the UI thread and blocks until onPageFinished is called. | 87 * Loads url on the UI thread and blocks until onPageFinished is called. |
88 */ | 88 */ |
89 protected void loadUrlSync(final AwContents awContents, | 89 protected void loadUrlSync(final ContentViewCore contentViewCore, |
90 CallbackHelper onPageFinishedHelper, | 90 CallbackHelper onPageFinishedHelper, |
91 final String url) throws Throwable { | 91 final String url) throws Throwable { |
92 int currentCallCount = onPageFinishedHelper.getCallCount(); | 92 int currentCallCount = onPageFinishedHelper.getCallCount(); |
93 loadUrlAsync(awContents, url); | 93 loadUrlAsync(contentViewCore, url); |
94 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, | 94 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, |
95 TimeUnit.SECONDS); | 95 TimeUnit.SECONDS); |
96 } | 96 } |
97 | 97 |
98 /** | 98 /** |
99 * Loads url on the UI thread but does not block. | 99 * Loads url on the UI thread but does not block. |
100 */ | 100 */ |
101 protected void loadUrlAsync(final AwContents awContents, | 101 protected void loadUrlAsync(final ContentViewCore contentViewCore, |
102 final String url) throws Throwable { | 102 final String url) throws Throwable { |
103 runTestOnUiThread(new Runnable() { | 103 runTestOnUiThread(new Runnable() { |
104 @Override | 104 @Override |
105 public void run() { | 105 public void run() { |
106 awContents.getContentViewCore().loadUrl(new LoadUrlParams(url)); | 106 contentViewCore.loadUrl(new LoadUrlParams(url)); |
107 } | 107 } |
108 }); | 108 }); |
109 } | 109 } |
110 | 110 |
111 /** | 111 /** |
112 * Loads data on the UI thread and blocks until onPageFinished is called. | 112 * Loads data on the UI thread and blocks until onPageFinished is called. |
113 */ | 113 */ |
114 protected void loadDataSync(final AwContents awContents, | 114 protected void loadDataSync(final ContentViewCore contentViewCore, |
115 CallbackHelper onPageFinishedHelper, | 115 CallbackHelper onPageFinishedHelper, |
116 final String data, final String mimeType, | 116 final String data, final String mimeType, |
117 final boolean isBase64Encoded) throws Throwable
{ | 117 final boolean isBase64Encoded) throws Throwable
{ |
118 int currentCallCount = onPageFinishedHelper.getCallCount(); | 118 int currentCallCount = onPageFinishedHelper.getCallCount(); |
119 loadDataAsync(awContents, data, mimeType, isBase64Encoded); | 119 loadDataAsync(contentViewCore, data, mimeType, isBase64Encoded); |
120 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, | 120 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, |
121 TimeUnit.SECONDS); | 121 TimeUnit.SECONDS); |
122 } | 122 } |
123 | 123 |
124 /** | 124 /** |
125 * Loads data on the UI thread but does not block. | 125 * Loads data on the UI thread but does not block. |
126 */ | 126 */ |
127 protected void loadDataAsync(final AwContents awContents, final String data, | 127 protected void loadDataAsync(final ContentViewCore contentViewCore, final St
ring data, |
128 final String mimeType, final boolean isBase64En
coded) | 128 final String mimeType, final boolean isBase64En
coded) |
129 throws Throwable { | 129 throws Throwable { |
130 runTestOnUiThread(new Runnable() { | 130 runTestOnUiThread(new Runnable() { |
131 @Override | 131 @Override |
132 public void run() { | 132 public void run() { |
133 awContents.loadUrl(LoadUrlParams.createLoadDataParams( | 133 contentViewCore.loadUrl(LoadUrlParams.createLoadDataParams( |
134 data, mimeType, isBase64Encoded)); | 134 data, mimeType, isBase64Encoded)); |
135 } | 135 } |
136 }); | 136 }); |
137 } | 137 } |
138 | 138 |
139 protected AwTestContainerView createAwTestContainerView(final boolean incogn
ito, | 139 protected AwTestContainerView createAwTestContainerView(final boolean incogn
ito, |
140 final AwContentsClient awContentsClient) { | 140 final AwContentsClient contentsClient) { |
141 return createAwTestContainerView(incognito, new AwTestContainerView(getA
ctivity()), | 141 return createAwTestContainerView(incognito, new AwTestContainerView(getA
ctivity()), |
142 awContentsClient); | 142 contentsClient); |
143 } | 143 } |
144 | 144 |
145 protected AwTestContainerView createAwTestContainerView(final boolean incogn
ito, | 145 protected AwTestContainerView createAwTestContainerView(final boolean incogn
ito, |
146 final AwTestContainerView testContainerView, | 146 final AwTestContainerView testContainerView, |
147 final AwContentsClient awContentsClient) { | 147 final AwContentsClient contentsClient) { |
148 ContentViewCore contentViewCore = new ContentViewCore( | 148 ContentViewCore contentViewCore = new ContentViewCore( |
149 getActivity(), ContentViewCore.PERSONALITY_VIEW); | 149 getActivity(), ContentViewCore.PERSONALITY_VIEW); |
150 testContainerView.initialize(contentViewCore, | 150 testContainerView.initialize(contentViewCore, |
151 new AwContents(testContainerView, testContainerView.getInternalA
ccessDelegate(), | 151 new AwContents(testContainerView, testContainerView.getInternalA
ccessDelegate(), |
152 contentViewCore, awContentsClient, new ActivityNativeWindow(getA
ctivity()), | 152 contentViewCore, contentsClient, new ActivityNativeWindow(getAct
ivity()), |
153 incognito, false)); | 153 incognito, false)); |
154 getActivity().addView(testContainerView); | 154 getActivity().addView(testContainerView); |
155 return testContainerView; | 155 return testContainerView; |
156 } | 156 } |
157 | 157 |
158 protected AwTestContainerView createAwTestContainerViewOnMainSync( | 158 protected AwTestContainerView createAwTestContainerViewOnMainSync( |
159 final AwContentsClient client) throws Exception { | 159 final AwContentsClient client) throws Exception { |
160 return createAwTestContainerViewOnMainSync(NORMAL_VIEW, client); | 160 return createAwTestContainerViewOnMainSync(NORMAL_VIEW, client); |
161 } | 161 } |
162 | 162 |
163 protected AwTestContainerView createAwTestContainerViewOnMainSync( | 163 protected AwTestContainerView createAwTestContainerViewOnMainSync( |
164 final boolean incognito, | 164 final boolean incognito, |
165 final AwContentsClient client) throws Exception { | 165 final AwContentsClient client) throws Exception { |
166 final AtomicReference<AwTestContainerView> testContainerView = | 166 final AtomicReference<AwTestContainerView> testContainerView = |
167 new AtomicReference<AwTestContainerView>(); | 167 new AtomicReference<AwTestContainerView>(); |
168 final Context context = getActivity(); | 168 final Context context = getActivity(); |
169 getInstrumentation().runOnMainSync(new Runnable() { | 169 getInstrumentation().runOnMainSync(new Runnable() { |
170 @Override | 170 @Override |
171 public void run() { | 171 public void run() { |
172 testContainerView.set(createAwTestContainerView(incognito, clien
t)); | 172 testContainerView.set(createAwTestContainerView(incognito, clien
t)); |
173 } | 173 } |
174 }); | 174 }); |
175 return testContainerView.get(); | 175 return testContainerView.get(); |
176 } | 176 } |
177 | 177 |
178 protected void destroyAwContentsOnMainSync(final AwContents awContents) { | 178 protected void destroyAwContentsOnMainSync(final AwContents contents) { |
179 if (awContents == null) return; | 179 if (contents == null) return; |
180 getInstrumentation().runOnMainSync(new Runnable() { | 180 getInstrumentation().runOnMainSync(new Runnable() { |
181 @Override | 181 @Override |
182 public void run() { | 182 public void run() { |
183 awContents.destroy(); | 183 contents.destroy(); |
184 } | 184 } |
185 }); | 185 }); |
186 } | 186 } |
187 | 187 |
188 protected String getTitleOnUiThread(final AwContents awContents) throws Thro
wable { | 188 protected String getTitleOnUiThread(final ContentViewCore contentViewCore) t
hrows Throwable { |
189 return runTestOnUiThreadAndGetResult(new Callable<String>() { | 189 return runTestOnUiThreadAndGetResult(new Callable<String>() { |
190 @Override | 190 @Override |
191 public String call() throws Exception { | 191 public String call() throws Exception { |
192 return awContents.getContentViewCore().getTitle(); | 192 return contentViewCore.getTitle(); |
193 } | 193 } |
194 }); | 194 }); |
195 } | 195 } |
196 | 196 |
197 protected ContentSettings getContentSettingsOnUiThread( | 197 protected ContentSettings getContentSettingsOnUiThread( |
198 final AwContents awContents) throws Throwable { | 198 final ContentViewCore contentViewCore) throws Throwable { |
199 return runTestOnUiThreadAndGetResult(new Callable<ContentSettings>() { | 199 return runTestOnUiThreadAndGetResult(new Callable<ContentSettings>() { |
200 @Override | 200 @Override |
201 public ContentSettings call() throws Exception { | 201 public ContentSettings call() throws Exception { |
202 return awContents.getContentViewCore().getContentSettings(); | 202 return contentViewCore.getContentSettings(); |
203 } | 203 } |
204 }); | 204 }); |
205 } | 205 } |
206 | 206 |
207 protected AwSettings getAwSettingsOnUiThread( | 207 protected AwSettings getAwSettingsOnUiThread( |
208 final AwContents awContents) throws Throwable { | 208 final AwContents awContents) throws Throwable { |
209 return runTestOnUiThreadAndGetResult(new Callable<AwSettings>() { | 209 return runTestOnUiThreadAndGetResult(new Callable<AwSettings>() { |
210 @Override | 210 @Override |
211 public AwSettings call() throws Exception { | 211 public AwSettings call() throws Exception { |
212 return awContents.getSettings(); | 212 return awContents.getSettings(); |
213 } | 213 } |
214 }); | 214 }); |
215 } | 215 } |
216 | 216 |
217 /** | 217 /** |
218 * Executes the given snippet of JavaScript code within the given ContentVie
w. Returns the | 218 * Executes the given snippet of JavaScript code within the given ContentVie
w. Returns the |
219 * result of its execution in JSON format. | 219 * result of its execution in JSON format. |
220 */ | 220 */ |
221 protected String executeJavaScriptAndWaitForResult(final AwContents awConten
ts, | 221 protected String executeJavaScriptAndWaitForResult(final ContentViewCore cor
e, |
222 TestAwContentsClient viewClient, final String code) throws Throwable
{ | 222 TestAwContentsClient viewClient, final String code) throws Throwable
{ |
223 final AtomicInteger requestId = new AtomicInteger(); | 223 final AtomicInteger requestId = new AtomicInteger(); |
224 TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper | 224 TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper |
225 onEvaluateJavaScriptResultHelper = viewClient.getOnEvaluateJavaS
criptResultHelper(); | 225 onEvaluateJavaScriptResultHelper = viewClient.getOnEvaluateJavaS
criptResultHelper(); |
226 int currentCallCount = onEvaluateJavaScriptResultHelper.getCallCount(); | 226 int currentCallCount = onEvaluateJavaScriptResultHelper.getCallCount(); |
227 runTestOnUiThread(new Runnable() { | 227 runTestOnUiThread(new Runnable() { |
228 @Override | 228 @Override |
229 public void run() { | 229 public void run() { |
230 requestId.set(awContents.getContentViewCore().evaluateJavaScript
(code)); | 230 requestId.set(core.evaluateJavaScript(code)); |
231 } | 231 } |
232 }); | 232 }); |
233 onEvaluateJavaScriptResultHelper.waitForCallback(currentCallCount); | 233 onEvaluateJavaScriptResultHelper.waitForCallback(currentCallCount); |
234 Assert.assertEquals("Response ID mismatch when evaluating JavaScript.", | 234 Assert.assertEquals("Response ID mismatch when evaluating JavaScript.", |
235 requestId.get(), onEvaluateJavaScriptResultHelper.getId()); | 235 requestId.get(), onEvaluateJavaScriptResultHelper.getId()); |
236 return onEvaluateJavaScriptResultHelper.getJsonResult(); | 236 return onEvaluateJavaScriptResultHelper.getJsonResult(); |
237 } | 237 } |
238 } | 238 } |
OLD | NEW |