Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: android_webview/javatests/src/org/chromium/android_webview/test/PostMessageTest.java

Issue 2375133002: Move MessagePort implementation from android_webview to content (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8 8
9 import android.os.Handler; 9 import android.os.Handler;
10 import android.test.suitebuilder.annotation.SmallTest; 10 import android.test.suitebuilder.annotation.SmallTest;
11 import android.webkit.JavascriptInterface; 11 import android.webkit.JavascriptInterface;
12 12
13 import org.chromium.android_webview.AwContents; 13 import org.chromium.android_webview.AwContents;
14 import org.chromium.android_webview.AwMessagePort;
15 import org.chromium.android_webview.AwMessagePortService;
16 import org.chromium.android_webview.test.util.CommonResources; 14 import org.chromium.android_webview.test.util.CommonResources;
17 import org.chromium.base.ThreadUtils; 15 import org.chromium.base.ThreadUtils;
18 import org.chromium.base.annotations.SuppressFBWarnings; 16 import org.chromium.base.annotations.SuppressFBWarnings;
19 import org.chromium.base.test.util.DisabledTest; 17 import org.chromium.base.test.util.DisabledTest;
20 import org.chromium.base.test.util.Feature; 18 import org.chromium.base.test.util.Feature;
21 import org.chromium.base.test.util.RetryOnFailure; 19 import org.chromium.base.test.util.RetryOnFailure;
20 import org.chromium.content.browser.AppWebMessagePort;
21 import org.chromium.content.browser.AppWebMessagePortService;
22 import org.chromium.content.browser.test.util.Criteria; 22 import org.chromium.content.browser.test.util.Criteria;
23 import org.chromium.content.browser.test.util.CriteriaHelper; 23 import org.chromium.content.browser.test.util.CriteriaHelper;
24 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage FinishedHelper; 24 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage FinishedHelper;
25 import org.chromium.net.test.util.TestWebServer; 25 import org.chromium.net.test.util.TestWebServer;
26 26
27 import java.util.concurrent.Callable; 27 import java.util.concurrent.Callable;
28 import java.util.concurrent.CountDownLatch; 28 import java.util.concurrent.CountDownLatch;
29 29
30 /** 30 /**
31 * The tests for content postMessage API. 31 * The tests for content postMessage API.
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 } 192 }
193 193
194 @SmallTest 194 @SmallTest
195 @Feature({"AndroidWebView", "Android-PostMessage"}) 195 @Feature({"AndroidWebView", "Android-PostMessage"})
196 public void testTransferringSamePortTwiceViaPostMessageToFrameNotAllowed() t hrows Throwable { 196 public void testTransferringSamePortTwiceViaPostMessageToFrameNotAllowed() t hrows Throwable {
197 loadPage(TEST_PAGE); 197 loadPage(TEST_PAGE);
198 final CountDownLatch latch = new CountDownLatch(1); 198 final CountDownLatch latch = new CountDownLatch(1);
199 runTestOnUiThread(new Runnable() { 199 runTestOnUiThread(new Runnable() {
200 @Override 200 @Override
201 public void run() { 201 public void run() {
202 AwMessagePort[] channel = mAwContents.createMessageChannel(); 202 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
203 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ), 203 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ),
204 new AwMessagePort[]{channel[1]}); 204 new AppWebMessagePort[]{channel[1]});
205 // Retransfer the port. This should fail with an exception. 205 // Retransfer the port. This should fail with an exception.
206 try { 206 try {
207 mAwContents.postMessageToFrame(null, "2", mWebServer.getBase Url(), 207 mAwContents.postMessageToFrame(null, "2", mWebServer.getBase Url(),
208 new AwMessagePort[]{channel[1]}); 208 new AppWebMessagePort[]{channel[1]});
209 } catch (IllegalStateException ex) { 209 } catch (IllegalStateException ex) {
210 latch.countDown(); 210 latch.countDown();
211 return; 211 return;
212 } 212 }
213 fail(); 213 fail();
214 } 214 }
215 }); 215 });
216 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 216 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
217 } 217 }
218 218
219 // There are two cases that put a port in a started state. 219 // There are two cases that put a port in a started state.
220 // 1. posting a message 220 // 1. posting a message
221 // 2. setting an event handler. 221 // 2. setting an event handler.
222 // A started port cannot return to "non-started" state. The four tests below verifies 222 // A started port cannot return to "non-started" state. The four tests below verifies
223 // these conditions for both conditions, using message ports and message cha nnels. 223 // these conditions for both conditions, using message ports and message cha nnels.
224 @SmallTest 224 @SmallTest
225 @Feature({"AndroidWebView", "Android-PostMessage"}) 225 @Feature({"AndroidWebView", "Android-PostMessage"})
226 public void testStartedPortCannotBeTransferredUsingPostMessageToFrame1() thr ows Throwable { 226 public void testStartedPortCannotBeTransferredUsingPostMessageToFrame1() thr ows Throwable {
227 loadPage(TEST_PAGE); 227 loadPage(TEST_PAGE);
228 final CountDownLatch latch = new CountDownLatch(1); 228 final CountDownLatch latch = new CountDownLatch(1);
229 runTestOnUiThread(new Runnable() { 229 runTestOnUiThread(new Runnable() {
230 @Override 230 @Override
231 public void run() { 231 public void run() {
232 AwMessagePort[] channel = mAwContents.createMessageChannel(); 232 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
233 channel[1].postMessage("1", null); 233 channel[1].postMessage("1", null);
234 try { 234 try {
235 mAwContents.postMessageToFrame(null, "2", mWebServer.getBase Url(), 235 mAwContents.postMessageToFrame(null, "2", mWebServer.getBase Url(),
236 new AwMessagePort[]{channel[1]}); 236 new AppWebMessagePort[]{channel[1]});
237 } catch (IllegalStateException ex) { 237 } catch (IllegalStateException ex) {
238 latch.countDown(); 238 latch.countDown();
239 return; 239 return;
240 } 240 }
241 fail(); 241 fail();
242 } 242 }
243 }); 243 });
244 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 244 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
245 } 245 }
246 246
247 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageTo Frame1 247 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageTo Frame1
248 @SmallTest 248 @SmallTest
249 @Feature({"AndroidWebView", "Android-PostMessage"}) 249 @Feature({"AndroidWebView", "Android-PostMessage"})
250 public void testStartedPortCannotBeTransferredUsingPostMessageToFrame2() thr ows Throwable { 250 public void testStartedPortCannotBeTransferredUsingPostMessageToFrame2() thr ows Throwable {
251 loadPage(TEST_PAGE); 251 loadPage(TEST_PAGE);
252 final CountDownLatch latch = new CountDownLatch(1); 252 final CountDownLatch latch = new CountDownLatch(1);
253 runTestOnUiThread(new Runnable() { 253 runTestOnUiThread(new Runnable() {
254 @Override 254 @Override
255 public void run() { 255 public void run() {
256 AwMessagePort[] channel = mAwContents.createMessageChannel(); 256 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
257 // set a web event handler, this puts the port in a started stat e. 257 // set a web event handler, this puts the port in a started stat e.
258 channel[1].setMessageCallback(new AwMessagePort.MessageCallback( ) { 258 channel[1].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
259 @Override 259 @Override
260 public void onMessage(String message, AwMessagePort[] sentPo rts) { } 260 public void onMessage(String message, AppWebMessagePort[] se ntPorts) { }
261 }, null); 261 }, null);
262 try { 262 try {
263 mAwContents.postMessageToFrame(null, "2", mWebServer.getBase Url(), 263 mAwContents.postMessageToFrame(null, "2", mWebServer.getBase Url(),
264 new AwMessagePort[]{channel[1]}); 264 new AppWebMessagePort[]{channel[1]});
265 } catch (IllegalStateException ex) { 265 } catch (IllegalStateException ex) {
266 latch.countDown(); 266 latch.countDown();
267 return; 267 return;
268 } 268 }
269 fail(); 269 fail();
270 } 270 }
271 }); 271 });
272 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 272 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
273 } 273 }
274 274
275 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageTo Frame1 275 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageTo Frame1
276 @SmallTest 276 @SmallTest
277 @Feature({"AndroidWebView", "Android-PostMessage"}) 277 @Feature({"AndroidWebView", "Android-PostMessage"})
278 @RetryOnFailure 278 @RetryOnFailure
279 public void testStartedPortCannotBeTransferredUsingMessageChannel1() throws Throwable { 279 public void testStartedPortCannotBeTransferredUsingMessageChannel1() throws Throwable {
280 loadPage(TEST_PAGE); 280 loadPage(TEST_PAGE);
281 final CountDownLatch latch = new CountDownLatch(1); 281 final CountDownLatch latch = new CountDownLatch(1);
282 runTestOnUiThread(new Runnable() { 282 runTestOnUiThread(new Runnable() {
283 @Override 283 @Override
284 public void run() { 284 public void run() {
285 AwMessagePort[] channel1 = mAwContents.createMessageChannel(); 285 AppWebMessagePort[] channel1 = mAwContents.createMessageChannel( );
286 channel1[1].postMessage("1", null); 286 channel1[1].postMessage("1", null);
287 AwMessagePort[] channel2 = mAwContents.createMessageChannel(); 287 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel( );
288 try { 288 try {
289 channel2[0].postMessage("2", new AwMessagePort[]{channel1[1] }); 289 channel2[0].postMessage("2", new AppWebMessagePort[]{channel 1[1]});
290 } catch (IllegalStateException ex) { 290 } catch (IllegalStateException ex) {
291 latch.countDown(); 291 latch.countDown();
292 return; 292 return;
293 } 293 }
294 fail(); 294 fail();
295 } 295 }
296 }); 296 });
297 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 297 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
298 } 298 }
299 299
300 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageTo Frame1 300 // see documentation in testStartedPortCannotBeTransferredUsingPostMessageTo Frame1
301 @SmallTest 301 @SmallTest
302 @Feature({"AndroidWebView", "Android-PostMessage"}) 302 @Feature({"AndroidWebView", "Android-PostMessage"})
303 public void testStartedPortCannotBeTransferredUsingMessageChannel2() throws Throwable { 303 public void testStartedPortCannotBeTransferredUsingMessageChannel2() throws Throwable {
304 loadPage(TEST_PAGE); 304 loadPage(TEST_PAGE);
305 final CountDownLatch latch = new CountDownLatch(1); 305 final CountDownLatch latch = new CountDownLatch(1);
306 runTestOnUiThread(new Runnable() { 306 runTestOnUiThread(new Runnable() {
307 @Override 307 @Override
308 public void run() { 308 public void run() {
309 AwMessagePort[] channel1 = mAwContents.createMessageChannel(); 309 AppWebMessagePort[] channel1 = mAwContents.createMessageChannel( );
310 // set a web event handler, this puts the port in a started stat e. 310 // set a web event handler, this puts the port in a started stat e.
311 channel1[1].setMessageCallback(new AwMessagePort.MessageCallback () { 311 channel1[1].setMessageCallback(new AppWebMessagePort.MessageCall back() {
312 @Override 312 @Override
313 public void onMessage(String message, AwMessagePort[] sentPo rts) { } 313 public void onMessage(String message, AppWebMessagePort[] se ntPorts) { }
314 }, null); 314 }, null);
315 AwMessagePort[] channel2 = mAwContents.createMessageChannel(); 315 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel( );
316 try { 316 try {
317 channel2[0].postMessage("1", new AwMessagePort[]{channel1[1] }); 317 channel2[0].postMessage("1", new AppWebMessagePort[]{channel 1[1]});
318 } catch (IllegalStateException ex) { 318 } catch (IllegalStateException ex) {
319 latch.countDown(); 319 latch.countDown();
320 return; 320 return;
321 } 321 }
322 fail(); 322 fail();
323 } 323 }
324 }); 324 });
325 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 325 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
326 } 326 }
327 327
328 328
329 // channel[0] and channel[1] are entangled ports, establishing a channel. Ve rify 329 // channel[0] and channel[1] are entangled ports, establishing a channel. Ve rify
330 // it is not allowed to transfer channel[0] on channel[0].postMessage. 330 // it is not allowed to transfer channel[0] on channel[0].postMessage.
331 // TODO(sgurun) Note that the related case of posting channel[1] via 331 // TODO(sgurun) Note that the related case of posting channel[1] via
332 // channel[0].postMessage does not throw a JS exception at present. We do no t throw 332 // channel[0].postMessage does not throw a JS exception at present. We do no t throw
333 // an exception in this case either since the information of entangled port is not 333 // an exception in this case either since the information of entangled port is not
334 // available at the source port. We need a new mechanism to implement to pre vent 334 // available at the source port. We need a new mechanism to implement to pre vent
335 // this case. 335 // this case.
336 @SmallTest 336 @SmallTest
337 @Feature({"AndroidWebView", "Android-PostMessage"}) 337 @Feature({"AndroidWebView", "Android-PostMessage"})
338 public void testTransferringSourcePortViaMessageChannelNotAllowed() throws T hrowable { 338 public void testTransferringSourcePortViaMessageChannelNotAllowed() throws T hrowable {
339 loadPage(TEST_PAGE); 339 loadPage(TEST_PAGE);
340 final CountDownLatch latch = new CountDownLatch(1); 340 final CountDownLatch latch = new CountDownLatch(1);
341 runTestOnUiThread(new Runnable() { 341 runTestOnUiThread(new Runnable() {
342 @Override 342 @Override
343 public void run() { 343 public void run() {
344 AwMessagePort[] channel = mAwContents.createMessageChannel(); 344 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
345 try { 345 try {
346 channel[0].postMessage("1", new AwMessagePort[]{channel[0]}) ; 346 channel[0].postMessage("1", new AppWebMessagePort[]{channel[ 0]});
347 } catch (IllegalStateException ex) { 347 } catch (IllegalStateException ex) {
348 latch.countDown(); 348 latch.countDown();
349 return; 349 return;
350 } 350 }
351 fail(); 351 fail();
352 } 352 }
353 }); 353 });
354 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 354 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
355 } 355 }
356 356
357 // Verify a closed port cannot be transferred to a frame. 357 // Verify a closed port cannot be transferred to a frame.
358 @SmallTest 358 @SmallTest
359 @Feature({"AndroidWebView", "Android-PostMessage"}) 359 @Feature({"AndroidWebView", "Android-PostMessage"})
360 public void testSendClosedPortToFrameNotAllowed() throws Throwable { 360 public void testSendClosedPortToFrameNotAllowed() throws Throwable {
361 loadPage(TEST_PAGE); 361 loadPage(TEST_PAGE);
362 final CountDownLatch latch = new CountDownLatch(1); 362 final CountDownLatch latch = new CountDownLatch(1);
363 runTestOnUiThread(new Runnable() { 363 runTestOnUiThread(new Runnable() {
364 @Override 364 @Override
365 public void run() { 365 public void run() {
366 AwMessagePort[] channel = mAwContents.createMessageChannel(); 366 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
367 channel[1].close(); 367 channel[1].close();
368 try { 368 try {
369 mAwContents.postMessageToFrame(null, "1", mWebServer.getBase Url(), 369 mAwContents.postMessageToFrame(null, "1", mWebServer.getBase Url(),
370 new AwMessagePort[]{channel[1]}); 370 new AppWebMessagePort[]{channel[1]});
371 } catch (IllegalStateException ex) { 371 } catch (IllegalStateException ex) {
372 latch.countDown(); 372 latch.countDown();
373 return; 373 return;
374 } 374 }
375 fail(); 375 fail();
376 } 376 }
377 }); 377 });
378 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 378 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
379 } 379 }
380 380
381 // Verify a closed port cannot be transferred to a port. 381 // Verify a closed port cannot be transferred to a port.
382 @SmallTest 382 @SmallTest
383 @Feature({"AndroidWebView", "Android-PostMessage"}) 383 @Feature({"AndroidWebView", "Android-PostMessage"})
384 public void testSendClosedPortToPortNotAllowed() throws Throwable { 384 public void testSendClosedPortToPortNotAllowed() throws Throwable {
385 loadPage(TEST_PAGE); 385 loadPage(TEST_PAGE);
386 final CountDownLatch latch = new CountDownLatch(1); 386 final CountDownLatch latch = new CountDownLatch(1);
387 runTestOnUiThread(new Runnable() { 387 runTestOnUiThread(new Runnable() {
388 @Override 388 @Override
389 public void run() { 389 public void run() {
390 AwMessagePort[] channel1 = mAwContents.createMessageChannel(); 390 AppWebMessagePort[] channel1 = mAwContents.createMessageChannel( );
391 AwMessagePort[] channel2 = mAwContents.createMessageChannel(); 391 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel( );
392 channel2[1].close(); 392 channel2[1].close();
393 try { 393 try {
394 channel1[0].postMessage("1", new AwMessagePort[]{channel2[1] }); 394 channel1[0].postMessage("1", new AppWebMessagePort[]{channel 2[1]});
395 } catch (IllegalStateException ex) { 395 } catch (IllegalStateException ex) {
396 latch.countDown(); 396 latch.countDown();
397 return; 397 return;
398 } 398 }
399 fail(); 399 fail();
400 } 400 }
401 }); 401 });
402 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 402 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
403 } 403 }
404 404
405 // Verify messages cannot be posted to closed ports. 405 // Verify messages cannot be posted to closed ports.
406 @SmallTest 406 @SmallTest
407 @Feature({"AndroidWebView", "Android-PostMessage"}) 407 @Feature({"AndroidWebView", "Android-PostMessage"})
408 public void testPostMessageToClosedPortNotAllowed() throws Throwable { 408 public void testPostMessageToClosedPortNotAllowed() throws Throwable {
409 loadPage(TEST_PAGE); 409 loadPage(TEST_PAGE);
410 final CountDownLatch latch = new CountDownLatch(1); 410 final CountDownLatch latch = new CountDownLatch(1);
411 runTestOnUiThread(new Runnable() { 411 runTestOnUiThread(new Runnable() {
412 @Override 412 @Override
413 public void run() { 413 public void run() {
414 AwMessagePort[] channel = mAwContents.createMessageChannel(); 414 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
415 channel[0].close(); 415 channel[0].close();
416 try { 416 try {
417 channel[0].postMessage("1", null); 417 channel[0].postMessage("1", null);
418 } catch (IllegalStateException ex) { 418 } catch (IllegalStateException ex) {
419 latch.countDown(); 419 latch.countDown();
420 return; 420 return;
421 } 421 }
422 fail(); 422 fail();
423 } 423 }
424 }); 424 });
425 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 425 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
426 } 426 }
427 427
428 // Verify messages posted before closing a port is received at the destinati on port. 428 // Verify messages posted before closing a port is received at the destinati on port.
429 @SmallTest 429 @SmallTest
430 @Feature({"AndroidWebView", "Android-PostMessage"}) 430 @Feature({"AndroidWebView", "Android-PostMessage"})
431 public void testMessagesPostedBeforeClosingPortAreTransferred() throws Throw able { 431 public void testMessagesPostedBeforeClosingPortAreTransferred() throws Throw able {
432 loadPage(TITLE_FROM_POSTMESSAGE_TO_CHANNEL); 432 loadPage(TITLE_FROM_POSTMESSAGE_TO_CHANNEL);
433 runTestOnUiThread(new Runnable() { 433 runTestOnUiThread(new Runnable() {
434 @Override 434 @Override
435 public void run() { 435 public void run() {
436 AwMessagePort[] channel = mAwContents.createMessageChannel(); 436 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
437 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ), 437 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ),
438 new AwMessagePort[]{channel[1]}); 438 new AppWebMessagePort[]{channel[1]});
439 channel[0].postMessage("2", null); 439 channel[0].postMessage("2", null);
440 channel[0].postMessage("3", null); 440 channel[0].postMessage("3", null);
441 channel[0].close(); 441 channel[0].close();
442 } 442 }
443 }); 443 });
444 expectTitle("23"); 444 expectTitle("23");
445 } 445 }
446 446
447 // Verify a transferred port using postmessagetoframe cannot be closed. 447 // Verify a transferred port using postmessagetoframe cannot be closed.
448 @SmallTest 448 @SmallTest
449 @Feature({"AndroidWebView", "Android-PostMessage"}) 449 @Feature({"AndroidWebView", "Android-PostMessage"})
450 public void testClosingTransferredPortToFrameThrowsAnException() throws Thro wable { 450 public void testClosingTransferredPortToFrameThrowsAnException() throws Thro wable {
451 loadPage(TEST_PAGE); 451 loadPage(TEST_PAGE);
452 final CountDownLatch latch = new CountDownLatch(1); 452 final CountDownLatch latch = new CountDownLatch(1);
453 runTestOnUiThread(new Runnable() { 453 runTestOnUiThread(new Runnable() {
454 @Override 454 @Override
455 public void run() { 455 public void run() {
456 AwMessagePort[] channel = mAwContents.createMessageChannel(); 456 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
457 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ), 457 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ),
458 new AwMessagePort[]{channel[1]}); 458 new AppWebMessagePort[]{channel[1]});
459 try { 459 try {
460 channel[1].close(); 460 channel[1].close();
461 } catch (IllegalStateException ex) { 461 } catch (IllegalStateException ex) {
462 latch.countDown(); 462 latch.countDown();
463 return; 463 return;
464 } 464 }
465 fail(); 465 fail();
466 } 466 }
467 }); 467 });
468 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 468 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
469 } 469 }
470 470
471 // Verify a transferred port using postmessagetoframe cannot be closed. 471 // Verify a transferred port using postmessagetoframe cannot be closed.
472 @SmallTest 472 @SmallTest
473 @Feature({"AndroidWebView", "Android-PostMessage"}) 473 @Feature({"AndroidWebView", "Android-PostMessage"})
474 public void testClosingTransferredPortToChannelThrowsAnException() throws Th rowable { 474 public void testClosingTransferredPortToChannelThrowsAnException() throws Th rowable {
475 loadPage(TEST_PAGE); 475 loadPage(TEST_PAGE);
476 final CountDownLatch latch = new CountDownLatch(1); 476 final CountDownLatch latch = new CountDownLatch(1);
477 runTestOnUiThread(new Runnable() { 477 runTestOnUiThread(new Runnable() {
478 @Override 478 @Override
479 public void run() { 479 public void run() {
480 AwMessagePort[] channel1 = mAwContents.createMessageChannel(); 480 AppWebMessagePort[] channel1 = mAwContents.createMessageChannel( );
481 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ), 481 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ),
482 new AwMessagePort[]{channel1[1]}); 482 new AppWebMessagePort[]{channel1[1]});
483 AwMessagePort[] channel2 = mAwContents.createMessageChannel(); 483 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel( );
484 channel1[0].postMessage("2", new AwMessagePort[]{channel2[0]}); 484 channel1[0].postMessage("2", new AppWebMessagePort[]{channel2[0] });
485 try { 485 try {
486 channel2[0].close(); 486 channel2[0].close();
487 } catch (IllegalStateException ex) { 487 } catch (IllegalStateException ex) {
488 latch.countDown(); 488 latch.countDown();
489 return; 489 return;
490 } 490 }
491 fail(); 491 fail();
492 } 492 }
493 }); 493 });
494 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS); 494 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL ISECONDS);
495 } 495 }
496 496
497 // Create two message channels, and while they are in pending state, transfe r the 497 // Create two message channels, and while they are in pending state, transfe r the
498 // second one in the first one. 498 // second one in the first one.
499 @SmallTest 499 @SmallTest
500 @Feature({"AndroidWebView", "Android-PostMessage"}) 500 @Feature({"AndroidWebView", "Android-PostMessage"})
501 public void testPendingPortCanBeTransferredInPendingPort() throws Throwable { 501 public void testPendingPortCanBeTransferredInPendingPort() throws Throwable {
502 loadPage(TITLE_FROM_POSTMESSAGE_TO_CHANNEL); 502 loadPage(TITLE_FROM_POSTMESSAGE_TO_CHANNEL);
503 final TestMessagePort testPort = 503 final TestMessagePort testPort =
504 new TestMessagePort(getAwBrowserContext().getMessagePortService( )); 504 new TestMessagePort(getAwBrowserContext().getMessagePortService( ));
505 runTestOnUiThread(new Runnable() { 505 runTestOnUiThread(new Runnable() {
506 @Override 506 @Override
507 public void run() { 507 public void run() {
508 AwMessagePort[] channel1 = mAwContents.createMessageChannel(); 508 AppWebMessagePort[] channel1 = mAwContents.createMessageChannel( );
509 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ), 509 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ),
510 new AwMessagePort[]{channel1[1]}); 510 new AppWebMessagePort[]{channel1[1]});
511 AwMessagePort[] channel2 = mAwContents.createMessageChannel(); 511 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel( );
512 channel1[0].postMessage("2", new AwMessagePort[]{channel2[0]}); 512 channel1[0].postMessage("2", new AppWebMessagePort[]{channel2[0] });
513 } 513 }
514 }); 514 });
515 expectTitle("2"); 515 expectTitle("2");
516 } 516 }
517 517
518 private static class ChannelContainer { 518 private static class ChannelContainer {
519 private boolean mReady; 519 private boolean mReady;
520 private AwMessagePort[] mChannel; 520 private AppWebMessagePort[] mChannel;
521 private final Object mLock = new Object(); 521 private final Object mLock = new Object();
522 private String mMessage = ""; 522 private String mMessage = "";
523 private int mCount; 523 private int mCount;
524 private int mWaitCount; 524 private int mWaitCount;
525 525
526 public ChannelContainer() { 526 public ChannelContainer() {
527 this(1); 527 this(1);
528 } 528 }
529 529
530 public ChannelContainer(int n) { 530 public ChannelContainer(int n) {
531 mWaitCount = n; 531 mWaitCount = n;
532 } 532 }
533 533
534 public void set(AwMessagePort[] channel) { 534 public void set(AppWebMessagePort[] channel) {
535 mChannel = channel; 535 mChannel = channel;
536 } 536 }
537 public AwMessagePort[] get() { 537 public AppWebMessagePort[] get() {
538 return mChannel; 538 return mChannel;
539 } 539 }
540 540
541 public void setMessage(String message) { 541 public void setMessage(String message) {
542 synchronized (mLock) { 542 synchronized (mLock) {
543 mMessage += message; 543 mMessage += message;
544 if (++mCount < mWaitCount) return; 544 if (++mCount < mWaitCount) return;
545 mReady = true; 545 mReady = true;
546 mLock.notify(); 546 mLock.notify();
547 } 547 }
(...skipping 26 matching lines...) Expand all
574 // to browser to block waiting for UI thread, and this would in turn block r enderer 574 // to browser to block waiting for UI thread, and this would in turn block r enderer
575 // doing the conversion. 575 // doing the conversion.
576 @DisabledTest 576 @DisabledTest
577 @Feature({"AndroidWebView", "Android-PostMessage"}) 577 @Feature({"AndroidWebView", "Android-PostMessage"})
578 public void testReceiveMessageInBackgroundThread() throws Throwable { 578 public void testReceiveMessageInBackgroundThread() throws Throwable {
579 loadPage(TEST_PAGE); 579 loadPage(TEST_PAGE);
580 final ChannelContainer channelContainer = new ChannelContainer(); 580 final ChannelContainer channelContainer = new ChannelContainer();
581 runTestOnUiThread(new Runnable() { 581 runTestOnUiThread(new Runnable() {
582 @Override 582 @Override
583 public void run() { 583 public void run() {
584 AwMessagePort[] channel = mAwContents.createMessageChannel(); 584 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
585 // verify communication from JS to Java. 585 // verify communication from JS to Java.
586 channelContainer.set(channel); 586 channelContainer.set(channel);
587 channel[0].setMessageCallback(new AwMessagePort.MessageCallback( ) { 587 channel[0].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
588 @Override 588 @Override
589 public void onMessage(String message, AwMessagePort[] sentPo rts) { 589 public void onMessage(String message, AppWebMessagePort[] se ntPorts) {
590 channelContainer.setMessage(message); 590 channelContainer.setMessage(message);
591 } 591 }
592 }, null); 592 }, null);
593 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(), 593 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(),
594 new AwMessagePort[]{channel[1]}); 594 new AppWebMessagePort[]{channel[1]});
595 } 595 }
596 }); 596 });
597 mMessageObject.waitForMessage(); 597 mMessageObject.waitForMessage();
598 assertEquals(WEBVIEW_MESSAGE, mMessageObject.getData()); 598 assertEquals(WEBVIEW_MESSAGE, mMessageObject.getData());
599 assertEquals(SOURCE_ORIGIN, mMessageObject.getOrigin()); 599 assertEquals(SOURCE_ORIGIN, mMessageObject.getOrigin());
600 // verify that one message port is received at the js side 600 // verify that one message port is received at the js side
601 assertEquals(1, mMessageObject.getPorts().length); 601 assertEquals(1, mMessageObject.getPorts().length);
602 // wait until we receive a message from JS 602 // wait until we receive a message from JS
603 runTestOnUiThread(new Runnable() { 603 runTestOnUiThread(new Runnable() {
604 @Override 604 @Override
(...skipping 13 matching lines...) Expand all
618 + " <script>" 618 + " <script>"
619 + " onmessage = function (e) {" 619 + " onmessage = function (e) {"
620 + " var myPort = e.ports[0];" 620 + " var myPort = e.ports[0];"
621 + " myPort.onmessage = function(e) {" 621 + " myPort.onmessage = function(e) {"
622 + " myPort.postMessage(e.data + \"" + JS_MESSAGE + "\ "); }" 622 + " myPort.postMessage(e.data + \"" + JS_MESSAGE + "\ "); }"
623 + " }" 623 + " }"
624 + " </script>" 624 + " </script>"
625 + "</body></html>"; 625 + "</body></html>";
626 626
627 // Call on non-UI thread. 627 // Call on non-UI thread.
628 private void waitUntilPortReady(final AwMessagePort port) throws Throwable { 628 private void waitUntilPortReady(final AppWebMessagePort port) throws Throwab le {
629 CriteriaHelper.pollUiThread(new Criteria() { 629 CriteriaHelper.pollUiThread(new Criteria() {
630 @Override 630 @Override
631 public boolean isSatisfied() { 631 public boolean isSatisfied() {
632 return port.isReady(); 632 return port.isReady();
633 } 633 }
634 }); 634 });
635 } 635 }
636 636
637 private static final String HELLO = "HELLO"; 637 private static final String HELLO = "HELLO";
638 638
639 // Message channels are created on UI thread in a pending state. They are 639 // Message channels are created on UI thread in a pending state. They are
640 // initialized at a later stage. Verify that a message port that is initiali zed 640 // initialized at a later stage. Verify that a message port that is initiali zed
641 // can be transferred to JS and full communication can happen on it. 641 // can be transferred to JS and full communication can happen on it.
642 // Do this by sending a message to JS and let it echo'ing the message with 642 // Do this by sending a message to JS and let it echo'ing the message with
643 // some text prepended to it. 643 // some text prepended to it.
644 @SmallTest 644 @SmallTest
645 @Feature({"AndroidWebView", "Android-PostMessage"}) 645 @Feature({"AndroidWebView", "Android-PostMessage"})
646 public void testMessageChannelUsingInitializedPort() throws Throwable { 646 public void testMessageChannelUsingInitializedPort() throws Throwable {
647 final ChannelContainer channelContainer = new ChannelContainer(); 647 final ChannelContainer channelContainer = new ChannelContainer();
648 loadPage(ECHO_PAGE); 648 loadPage(ECHO_PAGE);
649 final AwMessagePort[] channel = ThreadUtils.runOnUiThreadBlocking( 649 final AppWebMessagePort[] channel = ThreadUtils.runOnUiThreadBlocking(
650 new Callable<AwMessagePort[]>() { 650 new Callable<AppWebMessagePort[]>() {
651 @Override 651 @Override
652 public AwMessagePort[] call() { 652 public AppWebMessagePort[] call() {
653 return mAwContents.createMessageChannel(); 653 return mAwContents.createMessageChannel();
654 } 654 }
655 }); 655 });
656 656
657 waitUntilPortReady(channel[0]); 657 waitUntilPortReady(channel[0]);
658 waitUntilPortReady(channel[1]); 658 waitUntilPortReady(channel[1]);
659 runTestOnUiThread(new Runnable() { 659 runTestOnUiThread(new Runnable() {
660 @Override 660 @Override
661 public void run() { 661 public void run() {
662 channel[0].setMessageCallback(new AwMessagePort.MessageCallback( ) { 662 channel[0].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
663 @Override 663 @Override
664 public void onMessage(String message, AwMessagePort[] sentPo rts) { 664 public void onMessage(String message, AppWebMessagePort[] se ntPorts) {
665 channelContainer.setMessage(message); 665 channelContainer.setMessage(message);
666 } 666 }
667 }, null); 667 }, null);
668 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(), 668 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(),
669 new AwMessagePort[]{channel[1]}); 669 new AppWebMessagePort[]{channel[1]});
670 channel[0].postMessage(HELLO, null); 670 channel[0].postMessage(HELLO, null);
671 } 671 }
672 }); 672 });
673 // wait for the asynchronous response from JS 673 // wait for the asynchronous response from JS
674 channelContainer.waitForMessage(); 674 channelContainer.waitForMessage();
675 assertEquals(HELLO + JS_MESSAGE, channelContainer.getMessage()); 675 assertEquals(HELLO + JS_MESSAGE, channelContainer.getMessage());
676 } 676 }
677 677
678 // Verify that a message port can be used immediately (even if it is in 678 // Verify that a message port can be used immediately (even if it is in
679 // pending state) after creation. In particular make sure the message port c an be 679 // pending state) after creation. In particular make sure the message port c an be
680 // transferred to JS and full communication can happen on it. 680 // transferred to JS and full communication can happen on it.
681 // Do this by sending a message to JS and let it echo'ing the message with 681 // Do this by sending a message to JS and let it echo'ing the message with
682 // some text prepended to it. 682 // some text prepended to it.
683 @SmallTest 683 @SmallTest
684 @Feature({"AndroidWebView", "Android-PostMessage"}) 684 @Feature({"AndroidWebView", "Android-PostMessage"})
685 public void testMessageChannelUsingPendingPort() throws Throwable { 685 public void testMessageChannelUsingPendingPort() throws Throwable {
686 final ChannelContainer channelContainer = new ChannelContainer(); 686 final ChannelContainer channelContainer = new ChannelContainer();
687 loadPage(ECHO_PAGE); 687 loadPage(ECHO_PAGE);
688 runTestOnUiThread(new Runnable() { 688 runTestOnUiThread(new Runnable() {
689 @Override 689 @Override
690 public void run() { 690 public void run() {
691 AwMessagePort[] channel = mAwContents.createMessageChannel(); 691 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
692 channel[0].setMessageCallback(new AwMessagePort.MessageCallback( ) { 692 channel[0].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
693 @Override 693 @Override
694 public void onMessage(String message, AwMessagePort[] sentPo rts) { 694 public void onMessage(String message, AppWebMessagePort[] se ntPorts) {
695 channelContainer.setMessage(message); 695 channelContainer.setMessage(message);
696 } 696 }
697 }, null); 697 }, null);
698 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(), 698 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(),
699 new AwMessagePort[]{channel[1]}); 699 new AppWebMessagePort[]{channel[1]});
700 channel[0].postMessage(HELLO, null); 700 channel[0].postMessage(HELLO, null);
701 } 701 }
702 }); 702 });
703 // Wait for the asynchronous response from JS. 703 // Wait for the asynchronous response from JS.
704 channelContainer.waitForMessage(); 704 channelContainer.waitForMessage();
705 assertEquals(HELLO + JS_MESSAGE, channelContainer.getMessage()); 705 assertEquals(HELLO + JS_MESSAGE, channelContainer.getMessage());
706 } 706 }
707 707
708 // Verify that a message port can be used for message transfer when both 708 // Verify that a message port can be used for message transfer when both
709 // ports are owned by same Webview. 709 // ports are owned by same Webview.
710 @SmallTest 710 @SmallTest
711 @Feature({"AndroidWebView", "Android-PostMessage"}) 711 @Feature({"AndroidWebView", "Android-PostMessage"})
712 public void testMessageChannelCommunicationWithinWebView() throws Throwable { 712 public void testMessageChannelCommunicationWithinWebView() throws Throwable {
713 final ChannelContainer channelContainer = new ChannelContainer(); 713 final ChannelContainer channelContainer = new ChannelContainer();
714 loadPage(ECHO_PAGE); 714 loadPage(ECHO_PAGE);
715 runTestOnUiThread(new Runnable() { 715 runTestOnUiThread(new Runnable() {
716 @Override 716 @Override
717 public void run() { 717 public void run() {
718 AwMessagePort[] channel = mAwContents.createMessageChannel(); 718 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
719 channel[1].setMessageCallback(new AwMessagePort.MessageCallback( ) { 719 channel[1].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
720 @Override 720 @Override
721 public void onMessage(String message, AwMessagePort[] sentPo rts) { 721 public void onMessage(String message, AppWebMessagePort[] se ntPorts) {
722 channelContainer.setMessage(message); 722 channelContainer.setMessage(message);
723 } 723 }
724 }, null); 724 }, null);
725 channel[0].postMessage(HELLO, null); 725 channel[0].postMessage(HELLO, null);
726 } 726 }
727 }); 727 });
728 // Wait for the asynchronous response from JS. 728 // Wait for the asynchronous response from JS.
729 channelContainer.waitForMessage(); 729 channelContainer.waitForMessage();
730 assertEquals(HELLO, channelContainer.getMessage()); 730 assertEquals(HELLO, channelContainer.getMessage());
731 } 731 }
732 732
733 // Post a message with a pending port to a frame and then post a bunch of me ssages 733 // Post a message with a pending port to a frame and then post a bunch of me ssages
734 // after that. Make sure that they are not ordered at the receiver side. 734 // after that. Make sure that they are not ordered at the receiver side.
735 @SmallTest 735 @SmallTest
736 @Feature({"AndroidWebView", "Android-PostMessage"}) 736 @Feature({"AndroidWebView", "Android-PostMessage"})
737 public void testPostMessageToFrameNotReordersMessages() throws Throwable { 737 public void testPostMessageToFrameNotReordersMessages() throws Throwable {
738 loadPage(TITLE_FROM_POSTMESSAGE_TO_FRAME); 738 loadPage(TITLE_FROM_POSTMESSAGE_TO_FRAME);
739 runTestOnUiThread(new Runnable() { 739 runTestOnUiThread(new Runnable() {
740 @Override 740 @Override
741 public void run() { 741 public void run() {
742 AwMessagePort[] channel = mAwContents.createMessageChannel(); 742 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
743 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ), 743 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ),
744 new AwMessagePort[]{channel[1]}); 744 new AppWebMessagePort[]{channel[1]});
745 mAwContents.postMessageToFrame(null, "2", mWebServer.getBaseUrl( ), null); 745 mAwContents.postMessageToFrame(null, "2", mWebServer.getBaseUrl( ), null);
746 mAwContents.postMessageToFrame(null, "3", mWebServer.getBaseUrl( ), null); 746 mAwContents.postMessageToFrame(null, "3", mWebServer.getBaseUrl( ), null);
747 } 747 }
748 }); 748 });
749 expectTitle("123"); 749 expectTitle("123");
750 } 750 }
751 751
752 private static final String RECEIVE_JS_MESSAGE_CHANNEL_PAGE = 752 private static final String RECEIVE_JS_MESSAGE_CHANNEL_PAGE =
753 "<!DOCTYPE html><html><body>" 753 "<!DOCTYPE html><html><body>"
754 + " <script>" 754 + " <script>"
(...skipping 18 matching lines...) Expand all
773 // 3. Java sends a message using the new channel in 2. 773 // 3. Java sends a message using the new channel in 2.
774 // 4. Js responds to this message using the channel in 2. 774 // 4. Js responds to this message using the channel in 2.
775 // 5. Java responds to message in 4 using the channel in 2. 775 // 5. Java responds to message in 4 using the channel in 2.
776 @SmallTest 776 @SmallTest
777 @Feature({"AndroidWebView", "Android-PostMessage"}) 777 @Feature({"AndroidWebView", "Android-PostMessage"})
778 public void testCanUseReceivedAwMessagePortFromJS() throws Throwable { 778 public void testCanUseReceivedAwMessagePortFromJS() throws Throwable {
779 loadPage(RECEIVE_JS_MESSAGE_CHANNEL_PAGE); 779 loadPage(RECEIVE_JS_MESSAGE_CHANNEL_PAGE);
780 runTestOnUiThread(new Runnable() { 780 runTestOnUiThread(new Runnable() {
781 @Override 781 @Override
782 public void run() { 782 public void run() {
783 AwMessagePort[] channel = mAwContents.createMessageChannel(); 783 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
784 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ), 784 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ),
785 new AwMessagePort[]{channel[1]}); 785 new AppWebMessagePort[]{channel[1]});
786 channel[0].setMessageCallback(new AwMessagePort.MessageCallback( ) { 786 channel[0].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
787 @Override 787 @Override
788 public void onMessage(String message, final AwMessagePort[] p) { 788 public void onMessage(String message, final AppWebMessagePor t[] p) {
789 p[0].setMessageCallback(new AwMessagePort.MessageCallbac k() { 789 p[0].setMessageCallback(new AppWebMessagePort.MessageCal lback() {
790 @Override 790 @Override
791 public void onMessage(String message, AwMessagePort[ ] q) { 791 public void onMessage(String message, AppWebMessageP ort[] q) {
792 assertEquals("3", message); 792 assertEquals("3", message);
793 p[0].postMessage("4", null); 793 p[0].postMessage("4", null);
794 } 794 }
795 }, null); 795 }, null);
796 p[0].postMessage("2", null); 796 p[0].postMessage("2", null);
797 } 797 }
798 }, null); 798 }, null);
799 } 799 }
800 }); 800 });
801 expectTitle("24"); 801 expectTitle("24");
802 } 802 }
803 803
804 private static class TestMessagePort extends AwMessagePort { 804 private static class TestMessagePort extends AppWebMessagePort {
805 805
806 private boolean mReady; 806 private boolean mReady;
807 private AwMessagePort mPort; 807 private AppWebMessagePort mPort;
808 private final Object mLock = new Object(); 808 private final Object mLock = new Object();
809 809
810 public TestMessagePort(AwMessagePortService service) { 810 public TestMessagePort(AppWebMessagePortService service) {
811 super(service); 811 super(service);
812 } 812 }
813 813
814 public void setMessagePort(AwMessagePort port) { 814 public void setMessagePort(AppWebMessagePort port) {
815 mPort = port; 815 mPort = port;
816 } 816 }
817 817
818 public void setReady(boolean ready) { 818 public void setReady(boolean ready) {
819 synchronized (mLock) { 819 synchronized (mLock) {
820 mReady = ready; 820 mReady = ready;
821 } 821 }
822 } 822 }
823 @Override 823 @Override
824 public boolean isReady() { 824 public boolean isReady() {
(...skipping 15 matching lines...) Expand all
840 } 840 }
841 @Override 841 @Override
842 public boolean isClosed() { 842 public boolean isClosed() {
843 return mPort.isClosed(); 843 return mPort.isClosed();
844 } 844 }
845 @Override 845 @Override
846 public void setMessageCallback(MessageCallback messageCallback, Handler handler) { 846 public void setMessageCallback(MessageCallback messageCallback, Handler handler) {
847 mPort.setMessageCallback(messageCallback, handler); 847 mPort.setMessageCallback(messageCallback, handler);
848 } 848 }
849 @Override 849 @Override
850 public void onMessage(String message, AwMessagePort[] sentPorts) { 850 public void onMessage(String message, AppWebMessagePort[] sentPorts) {
851 mPort.onMessage(message, sentPorts); 851 mPort.onMessage(message, sentPorts);
852 } 852 }
853 @Override 853 @Override
854 public void postMessage(String message, AwMessagePort[] sentPorts) throw s 854 public void postMessage(String message, AppWebMessagePort[] sentPorts) t hrows
855 IllegalStateException { 855 IllegalStateException {
856 mPort.postMessage(message, sentPorts); 856 mPort.postMessage(message, sentPorts);
857 } 857 }
858 } 858 }
859 859
860 // Post a message with a pending port to a frame and then post a message tha t 860 // Post a message with a pending port to a frame and then post a message tha t
861 // is pending after that. Make sure that when first message becomes ready, 861 // is pending after that. Make sure that when first message becomes ready,
862 // the subsequent not-ready message is not sent. 862 // the subsequent not-ready message is not sent.
863 @SmallTest 863 @SmallTest
864 @Feature({"AndroidWebView", "Android-PostMessage"}) 864 @Feature({"AndroidWebView", "Android-PostMessage"})
865 public void testPostMessageToFrameNotSendsPendingMessages() throws Throwable { 865 public void testPostMessageToFrameNotSendsPendingMessages() throws Throwable {
866 loadPage(TITLE_FROM_POSTMESSAGE_TO_FRAME); 866 loadPage(TITLE_FROM_POSTMESSAGE_TO_FRAME);
867 final TestMessagePort testPort = 867 final TestMessagePort testPort =
868 new TestMessagePort(getAwBrowserContext().getMessagePortService( )); 868 new TestMessagePort(getAwBrowserContext().getMessagePortService( ));
869 runTestOnUiThread(new Runnable() { 869 runTestOnUiThread(new Runnable() {
870 @Override 870 @Override
871 public void run() { 871 public void run() {
872 AwMessagePort[] channel = mAwContents.createMessageChannel(); 872 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
873 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ), 873 mAwContents.postMessageToFrame(null, "1", mWebServer.getBaseUrl( ),
874 new AwMessagePort[]{channel[1]}); 874 new AppWebMessagePort[]{channel[1]});
875 mAwContents.postMessageToFrame(null, "2", mWebServer.getBaseUrl( ), null); 875 mAwContents.postMessageToFrame(null, "2", mWebServer.getBaseUrl( ), null);
876 AwMessagePort[] channel2 = mAwContents.createMessageChannel(); 876 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel( );
877 // Test port is in a pending state so it should not be transferr ed. 877 // Test port is in a pending state so it should not be transferr ed.
878 testPort.setMessagePort(channel2[0]); 878 testPort.setMessagePort(channel2[0]);
879 mAwContents.postMessageToFrame(null, "3", mWebServer.getBaseUrl( ), 879 mAwContents.postMessageToFrame(null, "3", mWebServer.getBaseUrl( ),
880 new AwMessagePort[]{testPort}); 880 new AppWebMessagePort[]{testPort});
881 } 881 }
882 }); 882 });
883 expectTitle("12"); 883 expectTitle("12");
884 } 884 }
885 885
886 private static final String WORKER_MESSAGE = "from_worker"; 886 private static final String WORKER_MESSAGE = "from_worker";
887 887
888 // Listen for messages. Pass port 1 to worker and use port 2 to receive mess ages from 888 // Listen for messages. Pass port 1 to worker and use port 2 to receive mess ages from
889 // from worker. 889 // from worker.
890 private static final String TEST_PAGE_FOR_PORT_TRANSFER = 890 private static final String TEST_PAGE_FOR_PORT_TRANSFER =
(...skipping 29 matching lines...) Expand all
920 @SmallTest 920 @SmallTest
921 @Feature({"AndroidWebView", "Android-PostMessage"}) 921 @Feature({"AndroidWebView", "Android-PostMessage"})
922 @RetryOnFailure 922 @RetryOnFailure
923 public void testTransferPortsToWorker() throws Throwable { 923 public void testTransferPortsToWorker() throws Throwable {
924 mWebServer.setResponse("/worker.js", WORKER_SCRIPT, 924 mWebServer.setResponse("/worker.js", WORKER_SCRIPT,
925 CommonResources.getTextJavascriptHeaders(true)); 925 CommonResources.getTextJavascriptHeaders(true));
926 loadPage(TEST_PAGE_FOR_PORT_TRANSFER); 926 loadPage(TEST_PAGE_FOR_PORT_TRANSFER);
927 runTestOnUiThread(new Runnable() { 927 runTestOnUiThread(new Runnable() {
928 @Override 928 @Override
929 public void run() { 929 public void run() {
930 AwMessagePort[] channel = mAwContents.createMessageChannel(); 930 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
931 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(), 931 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(),
932 new AwMessagePort[]{channel[0], channel[1]}); 932 new AppWebMessagePort[]{channel[0], channel[1]});
933 } 933 }
934 }); 934 });
935 mMessageObject.waitForMessage(); 935 mMessageObject.waitForMessage();
936 assertEquals(WORKER_MESSAGE, mMessageObject.getData()); 936 assertEquals(WORKER_MESSAGE, mMessageObject.getData());
937 } 937 }
938 938
939 private static final String POPUP_MESSAGE = "from_popup"; 939 private static final String POPUP_MESSAGE = "from_popup";
940 private static final String POPUP_URL = "/popup.html"; 940 private static final String POPUP_URL = "/popup.html";
941 private static final String IFRAME_URL = "/iframe.html"; 941 private static final String IFRAME_URL = "/iframe.html";
942 private static final String MAIN_PAGE_FOR_POPUP_TEST = "<!DOCTYPE html><html >" 942 private static final String MAIN_PAGE_FOR_POPUP_TEST = "<!DOCTYPE html><html >"
(...skipping 25 matching lines...) Expand all
968 @Feature({"AndroidWebView", "Android-PostMessage"}) 968 @Feature({"AndroidWebView", "Android-PostMessage"})
969 public void testPostMessageToPopup() throws Throwable { 969 public void testPostMessageToPopup() throws Throwable {
970 triggerPopup(mAwContents, mContentsClient, mWebServer, MAIN_PAGE_FOR_POP UP_TEST, ECHO_PAGE, 970 triggerPopup(mAwContents, mContentsClient, mWebServer, MAIN_PAGE_FOR_POP UP_TEST, ECHO_PAGE,
971 POPUP_URL, "createPopup()"); 971 POPUP_URL, "createPopup()");
972 connectPendingPopup(mAwContents); 972 connectPendingPopup(mAwContents);
973 final ChannelContainer channelContainer = new ChannelContainer(); 973 final ChannelContainer channelContainer = new ChannelContainer();
974 974
975 runTestOnUiThread(new Runnable() { 975 runTestOnUiThread(new Runnable() {
976 @Override 976 @Override
977 public void run() { 977 public void run() {
978 AwMessagePort[] channel = mAwContents.createMessageChannel(); 978 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
979 channelContainer.set(channel); 979 channelContainer.set(channel);
980 channel[0].setMessageCallback(new AwMessagePort.MessageCallback( ) { 980 channel[0].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
981 @Override 981 @Override
982 public void onMessage(String message, AwMessagePort[] sentPo rts) { 982 public void onMessage(String message, AppWebMessagePort[] se ntPorts) {
983 channelContainer.setMessage(message); 983 channelContainer.setMessage(message);
984 } 984 }
985 }, null); 985 }, null);
986 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(), 986 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(),
987 new AwMessagePort[] {channel[1]}); 987 new AppWebMessagePort[] {channel[1]});
988 channel[0].postMessage(HELLO, null); 988 channel[0].postMessage(HELLO, null);
989 } 989 }
990 }); 990 });
991 channelContainer.waitForMessage(); 991 channelContainer.waitForMessage();
992 assertEquals(HELLO + JS_MESSAGE, channelContainer.getMessage()); 992 assertEquals(HELLO + JS_MESSAGE, channelContainer.getMessage());
993 } 993 }
994 994
995 // Test if WebView can post a message from/to an iframe in a popup window. 995 // Test if WebView can post a message from/to an iframe in a popup window.
996 @SmallTest 996 @SmallTest
997 @Feature({"AndroidWebView", "Android-PostMessage"}) 997 @Feature({"AndroidWebView", "Android-PostMessage"})
998 public void testPostMessageToIframeInsidePopup() throws Throwable { 998 public void testPostMessageToIframeInsidePopup() throws Throwable {
999 mWebServer.setResponse(IFRAME_URL, ECHO_PAGE, null); 999 mWebServer.setResponse(IFRAME_URL, ECHO_PAGE, null);
1000 triggerPopup(mAwContents, mContentsClient, mWebServer, MAIN_PAGE_FOR_POP UP_TEST, 1000 triggerPopup(mAwContents, mContentsClient, mWebServer, MAIN_PAGE_FOR_POP UP_TEST,
1001 POPUP_PAGE_WITH_IFRAME, POPUP_URL, "createPopup()"); 1001 POPUP_PAGE_WITH_IFRAME, POPUP_URL, "createPopup()");
1002 connectPendingPopup(mAwContents); 1002 connectPendingPopup(mAwContents);
1003 final ChannelContainer channelContainer = new ChannelContainer(); 1003 final ChannelContainer channelContainer = new ChannelContainer();
1004 1004
1005 runTestOnUiThread(new Runnable() { 1005 runTestOnUiThread(new Runnable() {
1006 @Override 1006 @Override
1007 public void run() { 1007 public void run() {
1008 AwMessagePort[] channel = mAwContents.createMessageChannel(); 1008 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
1009 channelContainer.set(channel); 1009 channelContainer.set(channel);
1010 channel[0].setMessageCallback(new AwMessagePort.MessageCallback( ) { 1010 channel[0].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
1011 @Override 1011 @Override
1012 public void onMessage(String message, AwMessagePort[] sentPo rts) { 1012 public void onMessage(String message, AppWebMessagePort[] se ntPorts) {
1013 channelContainer.setMessage(message); 1013 channelContainer.setMessage(message);
1014 } 1014 }
1015 }, null); 1015 }, null);
1016 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(), 1016 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(),
1017 new AwMessagePort[] {channel[1]}); 1017 new AppWebMessagePort[] {channel[1]});
1018 channel[0].postMessage(HELLO, null); 1018 channel[0].postMessage(HELLO, null);
1019 } 1019 }
1020 }); 1020 });
1021 channelContainer.waitForMessage(); 1021 channelContainer.waitForMessage();
1022 assertEquals(HELLO + JS_MESSAGE, channelContainer.getMessage()); 1022 assertEquals(HELLO + JS_MESSAGE, channelContainer.getMessage());
1023 } 1023 }
1024 1024
1025 private static final String TEST_PAGE_FOR_UNSUPPORTED_MESSAGES = "<!DOCTYPE html><html><body>" 1025 private static final String TEST_PAGE_FOR_UNSUPPORTED_MESSAGES = "<!DOCTYPE html><html><body>"
1026 + " <script>" 1026 + " <script>"
1027 + " onmessage = function (e) {" 1027 + " onmessage = function (e) {"
(...skipping 11 matching lines...) Expand all
1039 1039
1040 // Make sure that postmessage can handle unsupported messages gracefully. 1040 // Make sure that postmessage can handle unsupported messages gracefully.
1041 @SmallTest 1041 @SmallTest
1042 @Feature({"AndroidWebView", "Android-PostMessage"}) 1042 @Feature({"AndroidWebView", "Android-PostMessage"})
1043 public void testPostUnsupportedWebMessageToApp() throws Throwable { 1043 public void testPostUnsupportedWebMessageToApp() throws Throwable {
1044 loadPage(TEST_PAGE_FOR_UNSUPPORTED_MESSAGES); 1044 loadPage(TEST_PAGE_FOR_UNSUPPORTED_MESSAGES);
1045 final ChannelContainer channelContainer = new ChannelContainer(); 1045 final ChannelContainer channelContainer = new ChannelContainer();
1046 runTestOnUiThread(new Runnable() { 1046 runTestOnUiThread(new Runnable() {
1047 @Override 1047 @Override
1048 public void run() { 1048 public void run() {
1049 AwMessagePort[] channel = mAwContents.createMessageChannel(); 1049 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
1050 channelContainer.set(channel); 1050 channelContainer.set(channel);
1051 channel[0].setMessageCallback(new AwMessagePort.MessageCallback( ) { 1051 channel[0].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
1052 @Override 1052 @Override
1053 public void onMessage(String message, AwMessagePort[] sentPo rts) { 1053 public void onMessage(String message, AppWebMessagePort[] se ntPorts) {
1054 channelContainer.setMessage(message); 1054 channelContainer.setMessage(message);
1055 } 1055 }
1056 }, null); 1056 }, null);
1057 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(), 1057 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(),
1058 new AwMessagePort[] {channel[1]}); 1058 new AppWebMessagePort[] {channel[1]});
1059 } 1059 }
1060 }); 1060 });
1061 channelContainer.waitForMessage(); 1061 channelContainer.waitForMessage();
1062 assertEquals(JS_MESSAGE, channelContainer.getMessage()); 1062 assertEquals(JS_MESSAGE, channelContainer.getMessage());
1063 // Assert that onMessage is called only once. 1063 // Assert that onMessage is called only once.
1064 assertEquals(1, channelContainer.getMessageCount()); 1064 assertEquals(1, channelContainer.getMessageCount());
1065 } 1065 }
1066 1066
1067 private static final String TEST_TRANSFER_EMPTY_PORTS = "<!DOCTYPE html><htm l><body>" 1067 private static final String TEST_TRANSFER_EMPTY_PORTS = "<!DOCTYPE html><htm l><body>"
1068 + " <script>" 1068 + " <script>"
1069 + " onmessage = function (e) {" 1069 + " onmessage = function (e) {"
1070 + " e.ports[0].postMessage('1', undefined);" 1070 + " e.ports[0].postMessage('1', undefined);"
1071 + " e.ports[0].postMessage('2', []);" 1071 + " e.ports[0].postMessage('2', []);"
1072 + " }" 1072 + " }"
1073 + " </script>" 1073 + " </script>"
1074 + "</body></html>"; 1074 + "</body></html>";
1075 1075
1076 // Make sure that postmessage can handle unsupported messages gracefully. 1076 // Make sure that postmessage can handle unsupported messages gracefully.
1077 @SmallTest 1077 @SmallTest
1078 @Feature({"AndroidWebView", "Android-PostMessage"}) 1078 @Feature({"AndroidWebView", "Android-PostMessage"})
1079 public void testTransferEmptyPortsArray() throws Throwable { 1079 public void testTransferEmptyPortsArray() throws Throwable {
1080 loadPage(TEST_TRANSFER_EMPTY_PORTS); 1080 loadPage(TEST_TRANSFER_EMPTY_PORTS);
1081 final ChannelContainer channelContainer = new ChannelContainer(2); 1081 final ChannelContainer channelContainer = new ChannelContainer(2);
1082 runTestOnUiThread(new Runnable() { 1082 runTestOnUiThread(new Runnable() {
1083 @Override 1083 @Override
1084 public void run() { 1084 public void run() {
1085 AwMessagePort[] channel = mAwContents.createMessageChannel(); 1085 AppWebMessagePort[] channel = mAwContents.createMessageChannel() ;
1086 channelContainer.set(channel); 1086 channelContainer.set(channel);
1087 channel[0].setMessageCallback(new AwMessagePort.MessageCallback( ) { 1087 channel[0].setMessageCallback(new AppWebMessagePort.MessageCallb ack() {
1088 @Override 1088 @Override
1089 public void onMessage(String message, AwMessagePort[] sentPo rts) { 1089 public void onMessage(String message, AppWebMessagePort[] se ntPorts) {
1090 channelContainer.setMessage(message); 1090 channelContainer.setMessage(message);
1091 } 1091 }
1092 }, null); 1092 }, null);
1093 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(), 1093 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer .getBaseUrl(),
1094 new AwMessagePort[] {channel[1]}); 1094 new AppWebMessagePort[] {channel[1]});
1095 } 1095 }
1096 }); 1096 });
1097 channelContainer.waitForMessage(); 1097 channelContainer.waitForMessage();
1098 assertEquals("12", channelContainer.getMessage()); 1098 assertEquals("12", channelContainer.getMessage());
1099 } 1099 }
1100 } 1100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698