@Test public void testShouldOverrideUrlLoading() throws InterruptedException { Assume.assumeTrue( WebViewFeature.isFeatureSupported(WebViewFeature.SHOULD_OVERRIDE_WITH_REDIRECTS)); Assume.assumeTrue( WebViewFeature.isFeatureSupported(WebViewFeature.WEB_RESOURCE_REQUEST_IS_REDIRECT)); String data = "<html><body>" + "<a href=\"" + TEST_URL + "\" id=\"link\">new page</a>" + "</body></html>"; mWebViewOnUiThread.loadDataAndWaitForCompletion(data, "text/html", null); final CountDownLatch pageFinishedLatch = new CountDownLatch(1); final MockWebViewClient webViewClient = new MockWebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); pageFinishedLatch.countDown(); } }; mWebViewOnUiThread.setWebViewClient(webViewClient); mWebViewOnUiThread.getSettings().setJavaScriptEnabled(true); clickOnLinkUsingJs("link", mWebViewOnUiThread); pageFinishedLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS); Assert.assertEquals(TEST_URL, webViewClient.getLastShouldOverrideResourceRequest().getUrl().toString()); WebResourceRequest request = webViewClient.getLastShouldOverrideResourceRequest(); Assert.assertNotNull(request); Assert.assertTrue(request.isForMainFrame()); Assert.assertFalse(WebResourceRequestCompat.isRedirect(request)); Assert.assertFalse(request.hasGesture()); }
@Override @SuppressWarnings("deprecation") @RequiresApi(21) public boolean shouldOverrideUrlLoading(@NonNull WebView view, @NonNull WebResourceRequest request) { if (Build.VERSION.SDK_INT < 21) return false; return shouldOverrideUrlLoading(view, request.getUrl().toString()); }
WebViewClientCompat extends WebViewClient implements WebViewClientBoundaryInterface { @Override @SuppressWarnings("deprecation") @RequiresApi(21) public boolean shouldOverrideUrlLoading(@NonNull WebView view, @NonNull WebResourceRequest request) { if (Build.VERSION.SDK_INT < 21) return false; return shouldOverrideUrlLoading(view, request.getUrl().toString()); } }
WebViewClientCompat extends WebViewClient implements WebViewClientBoundaryInterface { @Override @SuppressWarnings("deprecation") @RequiresApi(21) public boolean shouldOverrideUrlLoading(@NonNull WebView view, @NonNull WebResourceRequest request) { if (Build.VERSION.SDK_INT < 21) return false; return shouldOverrideUrlLoading(view, request.getUrl().toString()); }  }
WebViewClientCompat extends WebViewClient implements WebViewClientBoundaryInterface { @Override @SuppressWarnings("deprecation") @RequiresApi(21) public boolean shouldOverrideUrlLoading(@NonNull WebView view, @NonNull WebResourceRequest request) { if (Build.VERSION.SDK_INT < 21) return false; return shouldOverrideUrlLoading(view, request.getUrl().toString()); }  @Override @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) final String[] getSupportedFeatures(); @Override void onPageCommitVisible(@NonNull WebView view, @NonNull String url); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Override @RequiresApi(21) final void onReceivedError(@NonNull WebView view, @NonNull WebResourceRequest request,
            /* WebResourceError */ @NonNull InvocationHandler handler); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Override @RequiresApi(23) final void onReceivedError(@NonNull WebView view, @NonNull WebResourceRequest request,
            @NonNull WebResourceError error); @SuppressWarnings("deprecation") // for invoking the old onReceivedError. @RequiresApi(21) void onReceivedError(@NonNull WebView view, @NonNull WebResourceRequest request,
            @NonNull WebResourceErrorCompat error); @Override void onReceivedHttpError(@NonNull WebView view, @NonNull WebResourceRequest request,
            @NonNull WebResourceResponse errorResponse); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Override final void onSafeBrowsingHit(@NonNull WebView view, @NonNull WebResourceRequest request,
            @SafeBrowsingThreat int threatType,
            /* SafeBrowsingResponse */ @NonNull InvocationHandler handler); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Override @RequiresApi(27) final void onSafeBrowsingHit(@NonNull WebView view, @NonNull WebResourceRequest request,
            @SafeBrowsingThreat int threatType, @NonNull SafeBrowsingResponse response); void onSafeBrowsingHit(@NonNull WebView view, @NonNull WebResourceRequest request,
            @SafeBrowsingThreat int threatType, @NonNull SafeBrowsingResponseCompat callback); @Override @SuppressWarnings("deprecation") // for invoking the old shouldOverrideUrlLoading. @RequiresApi(21) boolean shouldOverrideUrlLoading(@NonNull WebView view,
            @NonNull WebResourceRequest request); }
WebViewClientCompat extends WebViewClient implements WebViewClientBoundaryInterface { @Override @SuppressWarnings("deprecation") @RequiresApi(21) public boolean shouldOverrideUrlLoading(@NonNull WebView view, @NonNull WebResourceRequest request) { if (Build.VERSION.SDK_INT < 21) return false; return shouldOverrideUrlLoading(view, request.getUrl().toString()); }  @Override @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) final String[] getSupportedFeatures(); @Override void onPageCommitVisible(@NonNull WebView view, @NonNull String url); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Override @RequiresApi(21) final void onReceivedError(@NonNull WebView view, @NonNull WebResourceRequest request,
            /* WebResourceError */ @NonNull InvocationHandler handler); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Override @RequiresApi(23) final void onReceivedError(@NonNull WebView view, @NonNull WebResourceRequest request,
            @NonNull WebResourceError error); @SuppressWarnings("deprecation") // for invoking the old onReceivedError. @RequiresApi(21) void onReceivedError(@NonNull WebView view, @NonNull WebResourceRequest request,
            @NonNull WebResourceErrorCompat error); @Override void onReceivedHttpError(@NonNull WebView view, @NonNull WebResourceRequest request,
            @NonNull WebResourceResponse errorResponse); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Override final void onSafeBrowsingHit(@NonNull WebView view, @NonNull WebResourceRequest request,
            @SafeBrowsingThreat int threatType,
            /* SafeBrowsingResponse */ @NonNull InvocationHandler handler); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Override @RequiresApi(27) final void onSafeBrowsingHit(@NonNull WebView view, @NonNull WebResourceRequest request,
            @SafeBrowsingThreat int threatType, @NonNull SafeBrowsingResponse response); void onSafeBrowsingHit(@NonNull WebView view, @NonNull WebResourceRequest request,
            @SafeBrowsingThreat int threatType, @NonNull SafeBrowsingResponseCompat callback); @Override @SuppressWarnings("deprecation") // for invoking the old shouldOverrideUrlLoading. @RequiresApi(21) boolean shouldOverrideUrlLoading(@NonNull WebView view,
            @NonNull WebResourceRequest request);  }