Commit 9e1cd985 authored by Beth Rennie's avatar Beth Rennie
Browse files

Bug 1745355 - Remove osfile.jsm from browser/components/shell r=bdahl

parent a1187db7
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ const { E10SUtils } = ChromeUtils.import(
const { HiddenFrame } = ChromeUtils.import(
  "resource://gre/modules/HiddenFrame.jsm"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

// Refrences to the progress listeners to keep them from being gc'ed
@@ -160,9 +159,7 @@ async function takeScreenshot(
      fr.readAsArrayBuffer(blob);
    });

    await OS.File.writeAtomic(path, new Uint8Array(reader.result), {
      flush: true,
    });
    await IOUtils.write(path, new Uint8Array(reader.result));
    dump("Screenshot saved to: " + path + "\n");
  } catch (e) {
    dump("Failure taking screenshot: " + e + "\n");
@@ -225,10 +222,14 @@ let HeadlessShell = {
        var path = cmdLine.handleFlagWithParam("screenshot", true);
        if (!cmdLine.length && !URLlist.length) {
          URLlist.push(path); // Assume the user wanted to specify a URL
          path = OS.Path.join(cmdLine.workingDirectory.path, "screenshot.png");

          path = PathUtils.join(
            cmdLine.workingDirectory.path,
            "screenshot.png"
          );
        }
      } catch (e) {
        path = OS.Path.join(cmdLine.workingDirectory.path, "screenshot.png");
        path = PathUtils.join(cmdLine.workingDirectory.path, "screenshot.png");
        cmdLine.handleFlag("screenshot", true); // Remove `screenshot`
      }

+9 −4
Original line number Diff line number Diff line
"use strict";
add_task(async function() {
  const cwdScreenshotPath = PathUtils.join(
    Services.dirsvc.get("CurWorkD", Ci.nsIFile).path,
    "screenshot.png"
  );

  // Test variations of the "screenshot" argument when a file path
  // isn't specified.
  await testFileCreationPositive(
@@ -7,27 +12,27 @@ add_task(async function() {
      "-screenshot",
      "http://mochi.test:8888/browser/browser/components/shell/test/headless.html",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
  await testFileCreationPositive(
    [
      "http://mochi.test:8888/browser/browser/components/shell/test/headless.html",
      "-screenshot",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
  await testFileCreationPositive(
    [
      "--screenshot",
      "http://mochi.test:8888/browser/browser/components/shell/test/headless.html",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
  await testFileCreationPositive(
    [
      "http://mochi.test:8888/browser/browser/components/shell/test/headless.html",
      "--screenshot",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
});
+11 −6
Original line number Diff line number Diff line
"use strict";

add_task(async function() {
  const cwdScreenshotPath = PathUtils.join(
    Services.dirsvc.get("CurWorkD", Ci.nsIFile).path,
    "screenshot.png"
  );

  // Test invalid URL arguments (either no argument or too many arguments).
  await testFileCreationNegative(["-screenshot"], "screenshot.png");
  await testFileCreationNegative(["-screenshot"], cwdScreenshotPath);
  await testFileCreationNegative(
    [
      "http://mochi.test:8888/browser/browser/components/shell/test/headless.html",
      "http://mochi.test:8888/headless.html",
      "-screenshot",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );

  // Test all four basic variations of the "window-size" argument.
@@ -21,7 +26,7 @@ add_task(async function() {
      "-window-size",
      "800",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
  await testFileCreationPositive(
    [
@@ -30,7 +35,7 @@ add_task(async function() {
      "-screenshot",
      "-window-size=800",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
  await testFileCreationPositive(
    [
@@ -40,7 +45,7 @@ add_task(async function() {
      "--window-size",
      "800",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
  await testFileCreationPositive(
    [
@@ -49,6 +54,6 @@ add_task(async function() {
      "-screenshot",
      "--window-size=800",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
});
+6 −2
Original line number Diff line number Diff line
"use strict";

add_task(async function() {
  const cwdScreenshotPath = PathUtils.join(
    Services.dirsvc.get("CurWorkD", Ci.nsIFile).path,
    "screenshot.png"
  );
  // Test other variations of the "window-size" argument.
  await testWindowSizePositive(800, 600);
  await testWindowSizePositive(1234);
@@ -12,7 +16,7 @@ add_task(async function() {
      "-window-size",
      "hello",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
  await testFileCreationNegative(
    [
@@ -22,6 +26,6 @@ add_task(async function() {
      "-window-size",
      "800,",
    ],
    "screenshot.png"
    cwdScreenshotPath
  );
});
+19 −22
Original line number Diff line number Diff line
@@ -4,12 +4,9 @@ const { Subprocess } = ChromeUtils.import(
  "resource://gre/modules/Subprocess.jsm"
);

const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const TEMP_DIR = Services.dirsvc.get("TmpD", Ci.nsIFile).path;

const screenshotPath = OS.Path.join(
  OS.Constants.Path.tmpDir,
  "headless_test_screenshot.png"
);
const screenshotPath = PathUtils.join(TEMP_DIR, "headless_test_screenshot.png");

async function runFirefox(args) {
  const XRE_EXECUTABLE_FILE = "XREExeF";
@@ -18,15 +15,15 @@ async function runFirefox(args) {
  const mochiPrefsFile = Services.dirsvc.get(NS_APP_PREFS_50_FILE, Ci.nsIFile);
  const mochiPrefsPath = mochiPrefsFile.path;
  const mochiPrefsName = mochiPrefsFile.leafName;
  const profilePath = OS.Path.join(
    OS.Constants.Path.tmpDir,
  const profilePath = PathUtils.join(
    TEMP_DIR,
    "headless_test_screenshot_profile"
  );
  const prefsPath = OS.Path.join(profilePath, mochiPrefsName);
  const prefsPath = PathUtils.join(profilePath, mochiPrefsName);
  const firefoxArgs = ["-profile", profilePath, "-no-remote"];

  await OS.File.makeDir(profilePath);
  await OS.File.copy(mochiPrefsPath, prefsPath);
  await IOUtils.makeDirectory(profilePath);
  await IOUtils.copy(mochiPrefsPath, prefsPath);
  let proc = await Subprocess.call({
    command: firefoxExe,
    arguments: firefoxArgs.concat(args),
@@ -43,29 +40,29 @@ async function runFirefox(args) {
  }
  let { exitCode } = await proc.wait();
  is(exitCode, 0, "Firefox process should exit with code 0");
  await OS.File.removeDir(profilePath);
  await IOUtils.remove(profilePath, { recursive: true });
}

async function testFileCreationPositive(args, path) {
  await runFirefox(args);

  let saved = await OS.File.exists(path);
  let saved = IOUtils.exists(path);
  ok(saved, "A screenshot should be saved as " + path);
  if (!saved) {
    return;
  }

  let info = await OS.File.stat(path);
  let info = await IOUtils.stat(path);
  ok(info.size > 0, "Screenshot should not be an empty file");
  await OS.File.remove(path);
  await IOUtils.remove(path);
}

async function testFileCreationNegative(args, path) {
  await runFirefox(args);

  let saved = await OS.File.exists(path);
  let saved = await IOUtils.exists(path);
  ok(!saved, "A screenshot should not be saved");
  await OS.File.remove(path, { ignoreAbsent: true });
  await IOUtils.remove(path);
}

async function testWindowSizePositive(width, height) {
@@ -83,13 +80,13 @@ async function testWindowSizePositive(width, height) {
    size,
  ]);

  let saved = await OS.File.exists(screenshotPath);
  let saved = await IOUtils.exists(screenshotPath);
  ok(saved, "A screenshot should be saved in the tmp directory");
  if (!saved) {
    return;
  }

  let data = await OS.File.read(screenshotPath);
  let data = await IOUtils.read(screenshotPath);
  await new Promise((resolve, reject) => {
    let blob = new Blob([data], { type: "image/png" });
    let reader = new FileReader();
@@ -114,19 +111,19 @@ async function testWindowSizePositive(width, height) {
    };
    reader.readAsDataURL(blob);
  });
  await OS.File.remove(screenshotPath);
  await IOUtils.remove(screenshotPath);
}

async function testGreen(url, path) {
  await runFirefox(["-url", url, `--screenshot=${path}`]);

  let saved = await OS.File.exists(path);
  let saved = await IOUtils.exists(path);
  ok(saved, "A screenshot should be saved in the tmp directory");
  if (!saved) {
    return;
  }

  let data = await OS.File.read(path);
  let data = await IOUtils.read(path);
  let image = await new Promise((resolve, reject) => {
    let blob = new Blob([data], { type: "image/png" });
    let reader = new FileReader();
@@ -156,5 +153,5 @@ async function testGreen(url, path) {
  }
  ok(found, "There should be a green pixel in the screenshot.");

  await OS.File.remove(path);
  await IOUtils.remove(path);
}