webkit/src/androidTest/java/androidx/webkit/PostMessageTest.java
@Test
    @SdkSuppress(minSdkVersion = 23) // TODO(gsennton) activate this test for pre-M devices when we
    // can pre-install a WebView APK containing support for the WebView Support Library, see
    // b/73454652.
    public void testMessageChannel() throws Throwable {
        loadPage(CHANNEL_MESSAGE);
        final WebMessagePortCompat[] channel = mOnUiThread.createWebMessageChannelCompat();
        WebMessageCompat message =
                new WebMessageCompat(WEBVIEW_MESSAGE, new WebMessagePortCompat[]{channel[1]});
        mOnUiThread.postWebMessageCompat(message, Uri.parse(BASE_URI));
        final int messageCount = 3;
        final CountDownLatch latch = new CountDownLatch(messageCount);
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < messageCount; i++) {
                    channel[0].postMessage(new WebMessageCompat(WEBVIEW_MESSAGE + i));
                }
                channel[0].setWebMessageCallback(new WebMessageCallbackCompat() {
                    @Override
                    public void onMessage(WebMessagePortCompat port, WebMessageCompat message) {
                        int i = messageCount - (int) latch.getCount();
                        assertEquals(WEBVIEW_MESSAGE + i + i, message.getData());
                        latch.countDown();
                    }
                });
            }
        });
        // Wait for all the responses to arrive.
        boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS);
    }
// Create a message channel and make sure it can be used for data transfer to/from js.
Create a message channel and make sure it can be used for data transfer to/from js.
Create a message channel and make sure it can be used for data transfer to / from js .
@Test
    @SdkSuppress(minSdkVersion = 23) 
    public void testMessageChannel() throws Throwable {
        loadPage(CHANNEL_MESSAGE);
        final WebMessagePortCompat[] channel = mOnUiThread.createWebMessageChannelCompat();
        WebMessageCompat message =
                new WebMessageCompat(WEBVIEW_MESSAGE, new WebMessagePortCompat[]{channel[1]});
        mOnUiThread.postWebMessageCompat(message, Uri.parse(BASE_URI));
        final int messageCount = 3;
        final CountDownLatch latch = new CountDownLatch(messageCount);
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < messageCount; i++) {
                    channel[0].postMessage(new WebMessageCompat(WEBVIEW_MESSAGE + i));
                }
                channel[0].setWebMessageCallback(new WebMessageCallbackCompat() {
                    @Override
                    public void onMessage(WebMessagePortCompat port, WebMessageCompat message) {
                        int i = messageCount - (int) latch.getCount();
                        assertEquals(WEBVIEW_MESSAGE + i + i, message.getData());
                        latch.countDown();
                    }
                });
            }
        });
        boolean ignore = latch.await(TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS);
    }
@ Test @ SdkSuppress ( minSdkVersion = 23 ) public void testMessageChannel ( ) throws Throwable { loadPage ( CHANNEL_MESSAGE ) ; final WebMessagePortCompat [ ] channel = mOnUiThread . createWebMessageChannelCompat ( ) ; WebMessageCompat message = new WebMessageCompat ( WEBVIEW_MESSAGE , new WebMessagePortCompat [ ] { channel [ 1 ] } ) ; mOnUiThread . postWebMessageCompat ( message , Uri . parse ( BASE_URI ) ) ; final int messageCount = 3 ; final CountDownLatch latch = new CountDownLatch ( messageCount ) ; InstrumentationRegistry . getInstrumentation ( ) . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { for ( int i = 0 ; i < messageCount ; i ++ ) { channel [ 0 ] . postMessage ( new WebMessageCompat ( WEBVIEW_MESSAGE + i ) ) ; } channel [ 0 ] . setWebMessageCallback ( new WebMessageCallbackCompat ( ) { @ Override public void onMessage ( WebMessagePortCompat port , WebMessageCompat message ) { int i = messageCount - ( int ) latch . getCount ( ) ; assertEquals ( WEBVIEW_MESSAGE + i + i , message . getData ( ) ) ; latch . countDown ( ) ; } } ) ; } } ) ; boolean ignore = latch . await ( TIMEOUT , java . util . concurrent . TimeUnit . MILLISECONDS ) ; }
Create a message channel and make sure it can be used for data transfer to/from js.
Create a message channel and make sure it can be used for data transfer to / from js .
// TODO(gsennton) activate this test for pre-M devices when we // can pre-install a WebView APK containing support for the WebView Support Library, see // b/73454652. // Wait for all the responses to arrive.