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
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.test.suitebuilder.annotation.SmallTest;
9 import org.chromium.base.test.util.Feature;
12 * The tests for content postMessage API.
14 public class PostMessageTest extends ContentViewTestBase {
16 private static final String URL1 =
17 "<!DOCTYPE html><html><body>" +
18 "<script type=\"text/javascript\">" +
19 "onmessage = function (e) {" +
20 "messageObject.setMessageParams(e.data, e.origin);" +
25 private static final String MESSAGE = "Foo";
26 private static final String SOURCE_ORIGIN = "android_webview";
28 // Inject to the page to verify received messages.
29 private static class MessageObject {
30 // Timeout to failure, in milliseconds
31 private static final int TIMEOUT = 5000;
33 private boolean mReady;
35 private String mOrigin;
36 private Object mLock = new Object();
38 public void setMessageParams(String data, String origin) {
39 synchronized (mLock) {
47 public void waitForMessage() throws InterruptedException {
48 synchronized (mLock) {
49 if (!mReady) mLock.wait(TIMEOUT);
53 public String getData() {
57 public String getOrigin() {
62 private MessageObject mMessageObject;
65 protected void setUp() throws Exception {
67 mMessageObject = new MessageObject();
68 setUpContentView(mMessageObject, "messageObject");
72 @Feature({"AndroidWebView", "Android-PostMessage"})
73 public void testPostMessageToMainFrame() throws Throwable {
74 ContentViewCore contentViewCore = getContentViewCore();
75 loadDataSync(contentViewCore, URL1, "text/html", false);
76 contentViewCore.postMessageToFrame(null, MESSAGE, SOURCE_ORIGIN, "*");
77 mMessageObject.waitForMessage();
78 assertEquals(MESSAGE, mMessageObject.getData());
79 assertEquals(SOURCE_ORIGIN, mMessageObject.getOrigin());