Beispiel #1
0
  @DisabledTest // Fails on android-one: crbug.com/540723
  @MediumTest
  @Feature({"Navigation"})
  public void testWindowOpenUrlSpoof() throws Exception {
    TestWebServer webServer = TestWebServer.start();
    try {
      // Make sure that we start with one tab.
      final TabModel model = getActivity().getTabModelSelector().getModel(false);

      final Semaphore urlServedSemaphore = new Semaphore(0);
      Runnable checkAction =
          new Runnable() {
            @Override
            public void run() {
              final Tab tab = TabModelUtils.getCurrentTab(model);

              // Make sure that we are showing the spoofed data and a blank URL.
              String url = getTabUrlOnUIThread(tab);
              boolean spoofedUrl = "".equals(url) || "about:blank".equals(url);
              assertTrue("URL Spoofed", spoofedUrl);
              assertEquals("Not showing mocked content", "\"Spoofed\"", getTabBodyText(tab));
              urlServedSemaphore.release();
            }
          };

      // Mock out the test URL
      final String mockedUrl =
          webServer.setResponseWithRunnableAction(
              "/mockme.html",
              "<html>"
                  + "  <head>"
                  + "    <meta name=\"viewport\""
                  + "        content=\"initial-scale=0.75,maximum-scale=0.75,user-scalable=no\">"
                  + "  </head>"
                  + "  <body>Real</body>"
                  + "</html>",
              null,
              checkAction);

      // Navigate to the spoofable URL
      loadUrl(
          UrlUtils.encodeHtmlDataUri(
              "<head>"
                  + "  <meta name=\"viewport\""
                  + "      content=\"initial-scale=0.5,maximum-scale=0.5,user-scalable=no\">"
                  + "</head>"
                  + "<script>"
                  + "  function spoof() {"
                  + "    var w = open();"
                  + "    w.opener = null;"
                  + "    w.document.write('Spoofed');"
                  + "    w.location = '"
                  + mockedUrl
                  + "'"
                  + "  }"
                  + "</script>"
                  + "<body id='body' onclick='spoof()'></body>"));
      assertWaitForPageScaleFactorMatch(0.5f);

      // Click the page, which triggers the URL load.
      DOMUtils.clickNode(this, getActivity().getCurrentContentViewCore(), "body");

      // Wait for the proper URL to be served.
      assertTrue(urlServedSemaphore.tryAcquire(5, TimeUnit.SECONDS));

      // Wait for the url to change.
      final Tab tab = TabModelUtils.getCurrentTab(model);
      assertWaitForPageScaleFactorMatch(0.75f);
      CriteriaHelper.pollForCriteria(
          new Criteria("Page url didn't change") {
            @Override
            public boolean isSatisfied() {
              return mockedUrl.equals(getTabUrlOnUIThread(tab));
            }
          },
          5000,
          50);

      // Make sure that we're showing new content now.
      assertEquals("Still showing spoofed data", "\"Real\"", getTabBodyText(tab));
    } finally {
      webServer.shutdown();
    }
  }