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

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

Issue 869133005: Post a Message from Java to JS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added a test Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: android_webview/javatests/src/org/chromium/android_webview/test/PostMessageTest.java
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/PostMessageTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/PostMessageTest.java
index 3be279c9f71d7a65f44febc63d7573c60046c7ba..8dbc89db94b6c3f863bedec4ac91d09d16aa2c6b 100644
--- a/android_webview/javatests/src/org/chromium/android_webview/test/PostMessageTest.java
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/PostMessageTest.java
@@ -19,6 +19,9 @@ import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.net.test.util.TestWebServer;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+
/**
* The tests for content postMessage API.
*/
@@ -141,6 +144,39 @@ public class PostMessageTest extends AwTestBase {
assertEquals(SOURCE_ORIGIN, mMessageObject.getOrigin());
}
+ @SmallTest
+ @Feature({"AndroidWebView", "Android-PostMessage"})
mnaganov (inactive) 2015/01/27 13:30:17 Perhaps, you can just annotate the test with "andr
sgurun-gerrit only 2015/01/29 03:06:09 In a UI thread test, how do I make sure the test d
mnaganov (inactive) 2015/01/29 16:56:20 Ah, sorry, I missed the fact that all the code is
+ public void testTransferringSamePortTwiceNotAllowed() throws Throwable {
+ loadPage(TEST_PAGE);
+ final AtomicBoolean success = new AtomicBoolean();
+ final CountDownLatch latch = new CountDownLatch(1);
+ runTestOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ValueCallback<MessageChannel> callback = new ValueCallback<MessageChannel>() {
+ @Override
+ public void onReceiveValue(MessageChannel channel) {
+ mAwContents.postMessageToFrame(null, "1", SOURCE_ORIGIN,
+ mWebServer.getBaseUrl(),
+ new MessagePort[]{channel.port2()});
+ // retransfer the port. This should fail with an exception
+ try {
+ mAwContents.postMessageToFrame(null, "2", SOURCE_ORIGIN,
+ mWebServer.getBaseUrl(),
+ new MessagePort[]{channel.port2()});
mnaganov (inactive) 2015/01/27 13:30:17 Perhaps, you can simply insert a call to "fail" af
sgurun-gerrit only 2015/01/29 03:06:09 Done.
+ } catch (IllegalStateException ex) {
+ success.set(true);
+ }
+ latch.countDown();
+ }
+ };
+ mAwContents.createMessageChannel(callback);
+ }
+ });
+ boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS);
+ assertEquals(true, success.get());
+ }
+
private static class ChannelContainer {
private boolean mReady;
private MessageChannel mChannel;
@@ -173,11 +209,10 @@ public class PostMessageTest extends AwTestBase {
}
}
- // Verify that a channel can be created and basic full duplex communication
- // can happen on it.
+ // Verify that messages from JS can be waited on a UI thread.
@SmallTest
@Feature({"AndroidWebView", "Android-PostMessage"})
- public void testCreateChannel() throws Throwable {
+ public void testReceiveMessageInBackgroundThread() throws Throwable {
loadPage(TEST_PAGE);
final ChannelContainer channelContainer = new ChannelContainer();
runTestOnUiThread(new Runnable() {
@@ -218,7 +253,51 @@ public class PostMessageTest extends AwTestBase {
}
});
assertEquals(JS_MESSAGE, channelContainer.getMessage());
- // TODO(sgurun) verify communication from Java to JS on the created channel
+ }
+
+ private static final String ECHO_PAGE =
+ "<!DOCTYPE html><html><body>"
+ + " <script type=\"text/javascript\">"
+ + " onmessage = function (e) {"
+ + " var myPort = e.ports[0];"
+ + " myPort.onmessage = function(e) {"
+ + " myPort.postMessage(e.data); }" // echo recevied message
+ + " }"
+ + " </script>"
+ + "</body></html>";
+
+ // Verify that a channel can be created and basic full duplex communication
+ // can happen on it. Do this by sending a message to JS and let it echo'ing
+ // back to Java.
+ @SmallTest
+ @Feature({"AndroidWebView", "Android-PostMessage"})
+ public void testMessageChannel() throws Throwable {
+ final String hello = "HELLO";
mnaganov (inactive) 2015/01/27 13:30:18 Would it be better to use separate messages for re
sgurun-gerrit only 2015/01/29 03:06:08 Done. that would be a very sad bug :)
+ final ChannelContainer channelContainer = new ChannelContainer();
+ loadPage(ECHO_PAGE);
+ runTestOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ValueCallback<MessageChannel> callback = new ValueCallback<MessageChannel>() {
+ @Override
+ public void onReceiveValue(MessageChannel channel) {
+ channel.port1().setMessageHandler(new MessagePort.MessageHandler() {
+ @Override
+ public void onMessage(String message) {
+ channelContainer.setMessage(message);
+ }
+ });
+ mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, SOURCE_ORIGIN,
+ mWebServer.getBaseUrl(), new MessagePort[]{channel.port2()});
+ channel.port1().postMessage(hello, null);
+ }
+ };
+ mAwContents.createMessageChannel(callback);
+ }
+ });
+ // wait for the asynchronous response from JS
+ channelContainer.waitForMessage();
+ assertEquals(hello, channelContainer.getMessage());
}
private static final String WORKER_MESSAGE = "from_worker";

Powered by Google App Engine
This is Rietveld 408576698