| 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 import org.chromium.content_public.browser.WebContents; | |
| 11 | |
| 12 /** | |
| 13 * The tests for content postMessage API. | |
| 14 */ | |
| 15 public class PostMessageTest extends ContentViewTestBase { | |
| 16 | |
| 17 private static final String URL1 = | |
| 18 "<!DOCTYPE html><html><body>" + | |
| 19 " <script type=\"text/javascript\">" + | |
| 20 " onmessage = function (e) {" + | |
| 21 " messageObject.setMessageParams(e.data, e.origin);" + | |
| 22 " }" + | |
| 23 " </script>" + | |
| 24 "</body></html>"; | |
| 25 | |
| 26 private static final String MESSAGE = "Foo"; | |
| 27 private static final String SOURCE_ORIGIN = "android_webview"; | |
| 28 | |
| 29 // Inject to the page to verify received messages. | |
| 30 private static class MessageObject { | |
| 31 // Timeout to failure, in milliseconds | |
| 32 private static final int TIMEOUT = 5000; | |
| 33 | |
| 34 private boolean mReady; | |
| 35 private String mData; | |
| 36 private String mOrigin; | |
| 37 private Object mLock = new Object(); | |
| 38 | |
| 39 public void setMessageParams(String data, String origin) { | |
| 40 synchronized (mLock) { | |
| 41 mData = data; | |
| 42 mOrigin = origin; | |
| 43 mReady = true; | |
| 44 mLock.notify(); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 public void waitForMessage() throws InterruptedException { | |
| 49 synchronized (mLock) { | |
| 50 if (!mReady) mLock.wait(TIMEOUT); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 public String getData() { | |
| 55 return mData; | |
| 56 } | |
| 57 | |
| 58 public String getOrigin() { | |
| 59 return mOrigin; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 private MessageObject mMessageObject; | |
| 64 | |
| 65 @Override | |
| 66 protected void setUp() throws Exception { | |
| 67 super.setUp(); | |
| 68 mMessageObject = new MessageObject(); | |
| 69 setUpContentView(mMessageObject, "messageObject"); | |
| 70 } | |
| 71 | |
| 72 @SmallTest | |
| 73 @Feature({"AndroidWebView", "Android-PostMessage"}) | |
| 74 public void testPostMessageToMainFrame() throws Throwable { | |
| 75 WebContents webContents = getContentViewCore().getWebContents(); | |
| 76 loadDataSync(webContents.getNavigationController(), URL1, "text/html", f
alse); | |
| 77 webContents.postMessageToFrame(null, MESSAGE, SOURCE_ORIGIN, "*"); | |
| 78 mMessageObject.waitForMessage(); | |
| 79 assertEquals(MESSAGE, mMessageObject.getData()); | |
| 80 assertEquals(SOURCE_ORIGIN, mMessageObject.getOrigin()); | |
| 81 } | |
| 82 } | |
| OLD | NEW |