/** Test that the progress bar only traverses the page a single time per navigation. */
  @Feature({"Android-Toolbar"})
  @MediumTest
  @Restriction(ChromeRestriction.RESTRICTION_TYPE_PHONE)
  public void testToolbarTraversesScreenOnce() throws InterruptedException, TimeoutException {

    EmbeddedTestServer testServer =
        EmbeddedTestServer.createAndStartFileServer(
            getInstrumentation().getContext(), Environment.getExternalStorageDirectory());

    final WebContents webContents = getActivity().getActivityTab().getWebContents();

    TestWebContentsObserver observer = new TestWebContentsObserver(webContents);
    // Start and stop load events are carefully tracked; there should be two start-stop pairs
    // that do not overlap.
    OnPageStartedHelper startHelper = observer.getOnPageStartedHelper();
    OnPageFinishedHelper finishHelper = observer.getOnPageFinishedHelper();

    ToolbarProgressBar progressBar = (ToolbarProgressBar) getActivity().findViewById(R.id.progress);

    // Reset progress bar start count in case anything else triggered it.
    progressBar.resetStartCountForTesting();

    // Ensure no load events have occured yet.
    assertEquals(0, startHelper.getCallCount());
    assertEquals(0, finishHelper.getCallCount());

    loadUrl(testServer.getURL(TEST_PAGE));

    // Wait for the initial page to be loaded if it hasn't already.
    if (finishHelper.getCallCount() == 0) {
      finishHelper.waitForCallback(finishHelper.getCallCount(), 1);
    }

    // Exactly one start load and one finish load event should have occured.
    assertEquals(1, startHelper.getCallCount());
    assertEquals(1, finishHelper.getCallCount());

    // Load content in the iframe of the test page to trigger another load.
    JavaScriptUtils.executeJavaScript(webContents, "loadIframeInPage();");

    // A load start will be triggered.
    startHelper.waitForCallback(startHelper.getCallCount(), 1);
    assertEquals(2, startHelper.getCallCount());

    // Wait for the iframe to finish loading.
    finishHelper.waitForCallback(finishHelper.getCallCount(), 1);
    assertEquals(2, finishHelper.getCallCount());

    // Though the page triggered two load events, the progress bar should have only appeared a
    // single time.
    assertEquals(1, progressBar.getStartCountForTesting());
  }