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 testClose() 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));
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                try {
                    channel[0].close();
                    channel[0].postMessage(new WebMessageCompat(WEBVIEW_MESSAGE));
                } catch (IllegalStateException ex) {
                    // expect to receive an exception
                    return;
                }
                Assert.fail("A closed port cannot be used to transfer messages");
            }
        });
    }
// Test that a message port that is closed cannot used to send a message
Test that a message port that is closed cannot used to send a message
Test that a message port that is closed cannot used to send a message
@Test
    @SdkSuppress(minSdkVersion = 23) 
    public void testClose() 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));
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                try {
                    channel[0].close();
                    channel[0].postMessage(new WebMessageCompat(WEBVIEW_MESSAGE));
                } catch (IllegalStateException ex) {
                    return;
                }
                Assert.fail("A closed port cannot be used to transfer messages");
            }
        });
    }
@ Test @ SdkSuppress ( minSdkVersion = 23 ) public void testClose ( ) 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 ) ) ; InstrumentationRegistry . getInstrumentation ( ) . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { try { channel [ 0 ] . close ( ) ; channel [ 0 ] . postMessage ( new WebMessageCompat ( WEBVIEW_MESSAGE ) ) ; } catch ( IllegalStateException ex ) { return ; } Assert . fail ( "A closed port cannot be used to transfer messages" ) ; } } ) ; }
Test that a message port that is closed cannot used to send a message
Test that a message port that is closed cannot used to send a message
// 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. // expect to receive an exception