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 testReceiveMessagePort() throws Throwable {
        final String hello = "HELLO";
        loadPage(CHANNEL_FROM_JS);
        final WebMessagePortCompat[] channel = mOnUiThread.createWebMessageChannelCompat();
        WebMessageCompat message =
                new WebMessageCompat(WEBVIEW_MESSAGE, new WebMessagePortCompat[]{channel[1]});
        mOnUiThread.postWebMessageCompat(message, Uri.parse(BASE_URI));
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                channel[0].setWebMessageCallback(new WebMessageCallbackCompat() {
                    @Override
                    public void onMessage(WebMessagePortCompat port, WebMessageCompat message) {
                        message.getPorts()[0].postMessage(new WebMessageCompat(hello));
                    }
                });
            }
        });
        waitForTitle(hello);
    }
// Test a message port created in JS can be received and used for message transfer.
Test a message port created in JS can be received and used for message transfer.
Test a message port created in JS can be received and used for message transfer .
@Test
    @SdkSuppress(minSdkVersion = 23) 
    public void testReceiveMessagePort() throws Throwable {
        final String hello = "HELLO";
        loadPage(CHANNEL_FROM_JS);
        final WebMessagePortCompat[] channel = mOnUiThread.createWebMessageChannelCompat();
        WebMessageCompat message =
                new WebMessageCompat(WEBVIEW_MESSAGE, new WebMessagePortCompat[]{channel[1]});
        mOnUiThread.postWebMessageCompat(message, Uri.parse(BASE_URI));
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                channel[0].setWebMessageCallback(new WebMessageCallbackCompat() {
                    @Override
                    public void onMessage(WebMessagePortCompat port, WebMessageCompat message) {
                        message.getPorts()[0].postMessage(new WebMessageCompat(hello));
                    }
                });
            }
        });
        waitForTitle(hello);
    }
@ Test @ SdkSuppress ( minSdkVersion = 23 ) public void testReceiveMessagePort ( ) throws Throwable { final String hello = "HELLO" ; loadPage ( CHANNEL_FROM_JS ) ; final WebMessagePortCompat [ ] channel = mOnUiThread . createWebMessageChannelCompat ( ) ; WebMessageCompat message = new WebMessageCompat ( WEBVIEW_MESSAGE , new WebMessagePortCompat [ ] { channel [ 1 ] } ) ; mOnUiThread . postWebMessageCompat ( message , Uri . parse ( BASE_URI ) ) ; InstrumentationRegistry . getInstrumentation ( ) . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { channel [ 0 ] . setWebMessageCallback ( new WebMessageCallbackCompat ( ) { @ Override public void onMessage ( WebMessagePortCompat port , WebMessageCompat message ) { message . getPorts ( ) [ 0 ] . postMessage ( new WebMessageCompat ( hello ) ) ; } } ) ; } } ) ; waitForTitle ( hello ) ; }
Test a message port created in JS can be received and used for message transfer.
Test a message port created in JS can be received and used for message transfer .
// 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.