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

Side by Side Diff: content/public/android/javatests/src/org/chromium/content/browser/PostMessageTest.java

Issue 332693004: Add a content API for postMessage (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address jochen's review Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698