OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.android_webview.test; | 5 package org.chromium.android_webview.test; |
6 | 6 |
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
8 | 8 |
9 import android.os.Handler; | |
10 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
11 import android.webkit.JavascriptInterface; | 10 import android.webkit.JavascriptInterface; |
12 | 11 |
13 import org.chromium.android_webview.AwContents; | 12 import org.chromium.android_webview.AwContents; |
14 import org.chromium.android_webview.test.util.CommonResources; | 13 import org.chromium.android_webview.test.util.CommonResources; |
15 import org.chromium.base.ThreadUtils; | 14 import org.chromium.base.ThreadUtils; |
16 import org.chromium.base.annotations.SuppressFBWarnings; | 15 import org.chromium.base.annotations.SuppressFBWarnings; |
17 import org.chromium.base.test.util.DisabledTest; | 16 import org.chromium.base.test.util.DisabledTest; |
18 import org.chromium.base.test.util.Feature; | 17 import org.chromium.base.test.util.Feature; |
19 import org.chromium.base.test.util.RetryOnFailure; | 18 import org.chromium.base.test.util.RetryOnFailure; |
20 import org.chromium.content.browser.AppWebMessagePort; | 19 import org.chromium.content.browser.AppWebMessagePort; |
21 import org.chromium.content.browser.AppWebMessagePortService; | |
22 import org.chromium.content.browser.test.util.Criteria; | 20 import org.chromium.content.browser.test.util.Criteria; |
23 import org.chromium.content.browser.test.util.CriteriaHelper; | 21 import org.chromium.content.browser.test.util.CriteriaHelper; |
24 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; | 22 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage
FinishedHelper; |
25 import org.chromium.content_public.browser.MessagePort; | 23 import org.chromium.content_public.browser.MessagePort; |
26 import org.chromium.net.test.util.TestWebServer; | 24 import org.chromium.net.test.util.TestWebServer; |
27 | 25 |
28 import java.util.concurrent.Callable; | 26 import java.util.concurrent.Callable; |
29 import java.util.concurrent.CountDownLatch; | 27 import java.util.concurrent.CountDownLatch; |
30 | 28 |
31 /** | 29 /** |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 }); | 492 }); |
495 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL
ISECONDS); | 493 boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILL
ISECONDS); |
496 } | 494 } |
497 | 495 |
498 // Create two message channels, and while they are in pending state, transfe
r the | 496 // Create two message channels, and while they are in pending state, transfe
r the |
499 // second one in the first one. | 497 // second one in the first one. |
500 @SmallTest | 498 @SmallTest |
501 @Feature({"AndroidWebView", "Android-PostMessage"}) | 499 @Feature({"AndroidWebView", "Android-PostMessage"}) |
502 public void testPendingPortCanBeTransferredInPendingPort() throws Throwable
{ | 500 public void testPendingPortCanBeTransferredInPendingPort() throws Throwable
{ |
503 loadPage(TITLE_FROM_POSTMESSAGE_TO_CHANNEL); | 501 loadPage(TITLE_FROM_POSTMESSAGE_TO_CHANNEL); |
504 final TestMessagePort testPort = | |
505 new TestMessagePort(getAwBrowserContext().getMessagePortService(
)); | |
506 runTestOnUiThread(new Runnable() { | 502 runTestOnUiThread(new Runnable() { |
507 @Override | 503 @Override |
508 public void run() { | 504 public void run() { |
509 AppWebMessagePort[] channel1 = mAwContents.createMessageChannel(
); | 505 AppWebMessagePort[] channel1 = mAwContents.createMessageChannel(
); |
510 mAwContents.postMessageToFrame( | 506 mAwContents.postMessageToFrame( |
511 null, "1", mWebServer.getBaseUrl(), new AppWebMessagePor
t[] {channel1[1]}); | 507 null, "1", mWebServer.getBaseUrl(), new AppWebMessagePor
t[] {channel1[1]}); |
512 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel(
); | 508 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel(
); |
513 channel1[0].postMessage("2", new AppWebMessagePort[] {channel2[0
]}); | 509 channel1[0].postMessage("2", new AppWebMessagePort[] {channel2[0
]}); |
514 } | 510 } |
515 }); | 511 }); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 } | 791 } |
796 }, null); | 792 }, null); |
797 p[0].postMessage("2", null); | 793 p[0].postMessage("2", null); |
798 } | 794 } |
799 }, null); | 795 }, null); |
800 } | 796 } |
801 }); | 797 }); |
802 expectTitle("24"); | 798 expectTitle("24"); |
803 } | 799 } |
804 | 800 |
805 private static class TestMessagePort extends AppWebMessagePort { | |
806 private boolean mReady; | |
807 private AppWebMessagePort mPort; | |
808 private final Object mLock = new Object(); | |
809 | |
810 public TestMessagePort(AppWebMessagePortService service) { | |
811 super(service); | |
812 } | |
813 | |
814 public void setMessagePort(AppWebMessagePort port) { | |
815 mPort = port; | |
816 } | |
817 | |
818 public void setReady(boolean ready) { | |
819 synchronized (mLock) { | |
820 mReady = ready; | |
821 } | |
822 } | |
823 @Override | |
824 public boolean isReady() { | |
825 synchronized (mLock) { | |
826 return mReady; | |
827 } | |
828 } | |
829 @Override | |
830 public int portId() { | |
831 return mPort.portId(); | |
832 } | |
833 @Override | |
834 public void setPortId(int id) { | |
835 mPort.setPortId(id); | |
836 } | |
837 @Override | |
838 public void close() { | |
839 mPort.close(); | |
840 } | |
841 @Override | |
842 public boolean isClosed() { | |
843 return mPort.isClosed(); | |
844 } | |
845 @Override | |
846 public void setMessageCallback(MessageCallback messageCallback, Handler
handler) { | |
847 mPort.setMessageCallback(messageCallback, handler); | |
848 } | |
849 @Override | |
850 public void onMessage(String message, AppWebMessagePort[] sentPorts) { | |
851 mPort.onMessage(message, sentPorts); | |
852 } | |
853 @Override | |
854 public void postMessage(String message, MessagePort[] sentPorts) | |
855 throws IllegalStateException { | |
856 mPort.postMessage(message, sentPorts); | |
857 } | |
858 } | |
859 | |
860 // Post a message with a pending port to a frame and then post a message tha
t | |
861 // is pending after that. Make sure that when first message becomes ready, | |
862 // the subsequent not-ready message is not sent. | |
863 @SmallTest | |
864 @Feature({"AndroidWebView", "Android-PostMessage"}) | |
865 public void testPostMessageToFrameNotSendsPendingMessages() throws Throwable
{ | |
866 loadPage(TITLE_FROM_POSTMESSAGE_TO_FRAME); | |
867 final TestMessagePort testPort = | |
868 new TestMessagePort(getAwBrowserContext().getMessagePortService(
)); | |
869 runTestOnUiThread(new Runnable() { | |
870 @Override | |
871 public void run() { | |
872 AppWebMessagePort[] channel = mAwContents.createMessageChannel()
; | |
873 mAwContents.postMessageToFrame( | |
874 null, "1", mWebServer.getBaseUrl(), new AppWebMessagePor
t[] {channel[1]}); | |
875 mAwContents.postMessageToFrame(null, "2", mWebServer.getBaseUrl(
), null); | |
876 AppWebMessagePort[] channel2 = mAwContents.createMessageChannel(
); | |
877 // Test port is in a pending state so it should not be transferr
ed. | |
878 testPort.setMessagePort(channel2[0]); | |
879 mAwContents.postMessageToFrame( | |
880 null, "3", mWebServer.getBaseUrl(), new AppWebMessagePor
t[] {testPort}); | |
881 } | |
882 }); | |
883 expectTitle("12"); | |
884 } | |
885 | |
886 private static final String WORKER_MESSAGE = "from_worker"; | 801 private static final String WORKER_MESSAGE = "from_worker"; |
887 | 802 |
888 // Listen for messages. Pass port 1 to worker and use port 2 to receive mess
ages from | 803 // Listen for messages. Pass port 1 to worker and use port 2 to receive mess
ages from |
889 // from worker. | 804 // from worker. |
890 private static final String TEST_PAGE_FOR_PORT_TRANSFER = | 805 private static final String TEST_PAGE_FOR_PORT_TRANSFER = |
891 "<!DOCTYPE html><html><body>" | 806 "<!DOCTYPE html><html><body>" |
892 + " <script>" | 807 + " <script>" |
893 + " var worker = new Worker(\"worker.js\");" | 808 + " var worker = new Worker(\"worker.js\");" |
894 + " onmessage = function (e) {" | 809 + " onmessage = function (e) {" |
895 + " if (e.data == \"" + WEBVIEW_MESSAGE + "\") {" | 810 + " if (e.data == \"" + WEBVIEW_MESSAGE + "\") {" |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1091 } | 1006 } |
1092 }, null); | 1007 }, null); |
1093 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer
.getBaseUrl(), | 1008 mAwContents.postMessageToFrame(null, WEBVIEW_MESSAGE, mWebServer
.getBaseUrl(), |
1094 new AppWebMessagePort[] {channel[1]}); | 1009 new AppWebMessagePort[] {channel[1]}); |
1095 } | 1010 } |
1096 }); | 1011 }); |
1097 channelContainer.waitForMessage(); | 1012 channelContainer.waitForMessage(); |
1098 assertEquals("12", channelContainer.getMessage()); | 1013 assertEquals("12", channelContainer.getMessage()); |
1099 } | 1014 } |
1100 } | 1015 } |
OLD | NEW |