@SmallTest
  @Feature({"ShouldInterceptLoadRequest"})
  public void testCalledForExistingFiles() throws Throwable {
    final String tmpDir = getInstrumentation().getTargetContext().getCacheDir().getPath();
    final String fileName = tmpDir + "/testfile.html";
    final String title = "existing file title";
    TestFileUtil.deleteFile(fileName); // Remove leftover file if any.
    TestFileUtil.createNewHtmlFile(fileName, title, "");
    final String existingFileUrl = "file://" + fileName;

    int callCount = mShouldInterceptLoadRequestHelper.getCallCount();
    int onPageFinishedCallCount = mTestHelperBridge.getOnPageFinishedHelper().getCallCount();
    loadUrlAsync(existingFileUrl);
    mShouldInterceptLoadRequestHelper.waitForCallback(callCount);
    assertEquals(existingFileUrl, mShouldInterceptLoadRequestHelper.getUrls().get(0));

    mTestHelperBridge.getOnPageFinishedHelper().waitForCallback(onPageFinishedCallCount);
    assertEquals(title, getTitleOnUiThread());
    assertEquals(
        onPageFinishedCallCount + 1, mTestHelperBridge.getOnPageFinishedHelper().getCallCount());
  }
  /**
   * Test a basic printing flow by emulating the corresponding system calls to the printing
   * controller: onStart, onLayout, onWrite, onFinish. Each one is called once, and in this order,
   * in the UI thread.
   */
  @TargetApi(Build.VERSION_CODES.KITKAT)
  @LargeTest
  @Feature({"Printing"})
  public void testNormalPrintingFlow() throws Throwable {
    if (!ApiCompatibilityUtils.isPrintingSupported()) return;

    final ChromeShellTab currentTab = launchChromeShellWithUrl(URL).getActiveTab();
    assertTrue(waitForActiveShellToBeDoneLoading());

    final PrintingControllerImpl printingController = createControllerOnUiThread();

    startControllerOnUiThread(printingController, currentTab);
    // {@link PrintDocumentAdapter#onStart} is always called first.
    callStartOnUiThread(printingController);

    // Create a temporary file to save the PDF.
    final File cacheDir = getInstrumentation().getTargetContext().getCacheDir();
    final File tempFile = File.createTempFile(TEMP_FILE_NAME, TEMP_FILE_EXTENSION, cacheDir);
    final ParcelFileDescriptor fileDescriptor =
        ParcelFileDescriptor.open(
            tempFile, (ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_READ_WRITE));

    PrintAttributes attributes =
        new PrintAttributes.Builder()
            .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
            .setResolution(new PrintAttributes.Resolution("foo", "bar", 300, 300))
            .setMinMargins(PrintAttributes.Margins.NO_MARGINS)
            .build();

    // Use this to wait for PDF generation to complete, as it will happen asynchronously.
    final FutureTask<Boolean> result =
        new FutureTask<Boolean>(
            new Callable<Boolean>() {
              @Override
              public Boolean call() {
                return true;
              }
            });

    callLayoutOnUiThread(
        printingController,
        null,
        attributes,
        new LayoutResultCallbackWrapperMock() {
          // Called on UI thread
          @Override
          public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
            callWriteOnUiThread(printingController, fileDescriptor, result);
          }
        });

    FileInputStream in = null;
    try {
      // This blocks until the PDF is generated.
      result.get(TEST_TIMEOUT, TimeUnit.MILLISECONDS);
      assertTrue(tempFile.length() > 0);
      in = new FileInputStream(tempFile);
      byte[] b = new byte[PDF_PREAMBLE.length()];
      in.read(b);
      String preamble = new String(b);
      assertEquals(PDF_PREAMBLE, preamble);
    } finally {
      callFinishOnUiThread(printingController);
      if (in != null) in.close();
      // Close the descriptor, if not closed already.
      fileDescriptor.close();
      TestFileUtil.deleteFile(tempFile.getAbsolutePath());
    }
  }