OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 package org.chromium.content.browser; |
| 6 |
| 7 import android.test.suitebuilder.annotation.SmallTest; |
| 8 |
| 9 import org.chromium.base.test.util.Feature; |
| 10 |
| 11 /** |
| 12 * The tests for content postMessage API. |
| 13 */ |
| 14 public class PostMessageTest extends ContentViewTestBase { |
| 15 |
| 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);" + |
| 21 "}" + |
| 22 "</script>" + |
| 23 "</body></html>"; |
| 24 |
| 25 private static final String MESSAGE = "Foo"; |
| 26 private static final String SOURCE_ORIGIN = "android_webview"; |
| 27 |
| 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; |
| 32 |
| 33 private boolean mReady; |
| 34 private String mData; |
| 35 private String mOrigin; |
| 36 private Object mLock = new Object(); |
| 37 |
| 38 public void setMessageParams(String data, String origin) { |
| 39 synchronized (mLock) { |
| 40 mData = data; |
| 41 mOrigin = origin; |
| 42 mReady = true; |
| 43 mLock.notify(); |
| 44 } |
| 45 } |
| 46 |
| 47 public void waitForMessage() throws InterruptedException { |
| 48 synchronized (mLock) { |
| 49 if (!mReady) mLock.wait(TIMEOUT); |
| 50 } |
| 51 } |
| 52 |
| 53 public String getData() { |
| 54 return mData; |
| 55 } |
| 56 |
| 57 public String getOrigin() { |
| 58 return mOrigin; |
| 59 } |
| 60 } |
| 61 |
| 62 private MessageObject mMessageObject; |
| 63 |
| 64 @Override |
| 65 protected void setUp() throws Exception { |
| 66 super.setUp(); |
| 67 mMessageObject = new MessageObject(); |
| 68 setUpContentView(mMessageObject, "messageObject"); |
| 69 } |
| 70 |
| 71 @SmallTest |
| 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()); |
| 80 } |
| 81 } |
OLD | NEW |