public void testShouldOverrideUrlLoadingWhenNavigatingFromJavaScriptUsingReplace() {
   try {
     final String redirectTargetUrl =
         addPageToTestServer(
             mWebServer,
             REDIRECT_TARGET_PATH,
             CommonResources.makeHtmlPageFrom(
                 "<title>" + TITLE + "</title> ",
                 "<div>This is the end of the redirect chain</div>"));
     loadDataSync(
         null,
         CommonResources.makeHtmlPageFrom(
             "<title>" + TITLE + "</title> ",
             "<img onclick=\"location.replace('"
                 + redirectTargetUrl
                 + "');\" class=\"big\" id=\"link\" />"),
         "text/html",
         false);
     ShouldOverrideUrlLoadingHelper mShouldOverrideUrlLoadingHelper =
         mTestHelperBridge.getShouldOverrideUrlLoadingHelper();
     int callCount = mShouldOverrideUrlLoadingHelper.getCallCount();
     clickOnElementId("link", null);
     mShouldOverrideUrlLoadingHelper.waitForCallback(callCount);
     assertEquals(1, mShouldOverrideUrlLoadingHelper.getCallCount());
   } catch (Exception e) {
     assertTrue(false);
     e.printStackTrace();
   } catch (Throwable e) {
     assertTrue(false);
     e.printStackTrace();
   }
 }
 public void testShouldOverrideUrlLoadingWithCorrectUrl() {
   try {
     final String redirectTargetUrl =
         addPageToTestServer(
             mWebServer,
             REDIRECT_TARGET_PATH,
             CommonResources.makeHtmlPageFrom(
                 "<title>" + TITLE + "</title> ",
                 "<div>This is the end of the redirect chain</div>"));
     loadDataSync(
         null,
         CommonResources.makeHtmlPageWithSimpleLinkTo(redirectTargetUrl),
         "text/html",
         false);
     ShouldOverrideUrlLoadingHelper mShouldOverrideUrlLoadingHelper =
         mTestHelperBridge.getShouldOverrideUrlLoadingHelper();
     int callCount = mShouldOverrideUrlLoadingHelper.getCallCount();
     clickOnElementId("link", null);
     mShouldOverrideUrlLoadingHelper.waitForCallback(callCount);
     assertEquals(
         redirectTargetUrl, mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl());
   } catch (Exception e) {
     assertTrue(false);
     e.printStackTrace();
   } catch (Throwable e) {
     assertTrue(false);
     e.printStackTrace();
   }
 }
 public void testShouldOverrideUrlLoadingForIframeHttpNavigations() {
   try {
     ShouldOverrideUrlLoadingHelper mShouldOverrideUrlLoadingHelper =
         mTestHelperBridge.getShouldOverrideUrlLoadingHelper();
     final String iframeRedirectTargetUrl =
         addPageToTestServer(
             mWebServer,
             REDIRECT_TARGET_PATH,
             CommonResources.makeHtmlPageFrom(
                 "<title>" + TITLE + "</title> ",
                 "<div>This is the end of the redirect chain</div>"));
     final String iframeRedirectUrl = mWebServer.setRedirect("/302.html", iframeRedirectTargetUrl);
     final String pageWithIframeUrl =
         addPageToTestServer(
             mWebServer,
             "/iframe_intercept.html",
             CommonResources.makeHtmlPageFrom(
                 "<title>" + TITLE + "</title> ", "<iframe src=\"" + iframeRedirectUrl + "\" />"));
     assertEquals(0, mWebServer.getRequestCount(REDIRECT_TARGET_PATH));
     loadUrlSync(pageWithIframeUrl);
     // Wait for the redirect target URL to be fetched from the server.
     pollOnUiThread(
         new Callable<Boolean>() {
           @Override
           public Boolean call() throws Exception {
             return mWebServer.getRequestCount(REDIRECT_TARGET_PATH) == 1;
           }
         });
     assertEquals(1, mShouldOverrideUrlLoadingHelper.getCallCount());
   } catch (Exception e) {
     assertTrue(false);
     e.printStackTrace();
   } catch (Throwable e) {
     assertTrue(false);
     e.printStackTrace();
   }
 }
  public void testShouldOverrideUrlLoadingFor302AfterPostNavigations() {
    try {
      final String redirectTargetUrl =
          addPageToTestServer(
              mWebServer,
              REDIRECT_TARGET_PATH,
              CommonResources.makeHtmlPageFrom(
                  "<title>" + TITLE + "</title> ",
                  "<div>This is the end of the redirect chain</div>"));
      final String postToGetRedirectUrl = mWebServer.setRedirect("/302.html", redirectTargetUrl);
      final String postLinkUrl =
          addPageToTestServer(
              mWebServer,
              "/page_with_post_link.html",
              CommonResources.makeHtmlPageWithSimplePostFormTo(postToGetRedirectUrl));
      loadUrlSync(postLinkUrl);
      ShouldOverrideUrlLoadingHelper mShouldOverrideUrlLoadingHelper =
          mTestHelperBridge.getShouldOverrideUrlLoadingHelper();
      final int shouldOverrideUrlLoadingCallCount = mShouldOverrideUrlLoadingHelper.getCallCount();
      clickOnElementId("link", null);
      mShouldOverrideUrlLoadingHelper.waitForCallback(shouldOverrideUrlLoadingCallCount);

      // Wait for the target URL to be fetched from the server.
      pollOnUiThread(
          new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
              return mWebServer.getRequestCount(REDIRECT_TARGET_PATH) == 1;
            }
          });
      assertEquals(
          redirectTargetUrl, mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl());
    } catch (Exception e) {
      assertTrue(false);
      e.printStackTrace();
    } catch (Throwable e) {
      assertTrue(false);
      e.printStackTrace();
    }
  }