tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_1119088.js (6718B)


      1 // Where we save the desktop background to (~/Pictures).
      2 const NS_OSX_PICTURE_DOCUMENTS_DIR = "Pct";
      3 
      4 // Paths used to run the CLI command (python script) that is used to
      5 // 1) check the desktop background image matches what we set it to via
      6 //    nsIShellService::setDesktopBackground() and
      7 // 2) revert the desktop background image to the OS default
      8 
      9 let kPythonPath = "/usr/bin/python";
     10 if (AppConstants.isPlatformAndVersionAtLeast("macosx", 23.0)) {
     11  kPythonPath = "/usr/local/bin/python3";
     12 }
     13 
     14 const kDesktopCheckerScriptPath =
     15  "browser/browser/components/shell/test/mac_desktop_image.py";
     16 const kDefaultBackgroundImage =
     17  "/System/Library/Desktop Pictures/Solid Colors/Teal.png";
     18 
     19 ChromeUtils.defineESModuleGetters(this, {
     20  FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
     21 });
     22 
     23 function getPythonExecutableFile() {
     24  let python = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
     25  info(`Using python at location ${kPythonPath}`);
     26  python.initWithPath(kPythonPath);
     27  return python;
     28 }
     29 
     30 function createProcess() {
     31  return Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
     32 }
     33 
     34 // Use a CLI command to set the desktop background to |imagePath|. Returns the
     35 // exit code of the CLI command which reflects whether or not the background
     36 // image was successfully set. Returns 0 on success.
     37 function setDesktopBackgroundCLI(imagePath) {
     38  let setBackgroundProcess = createProcess();
     39  setBackgroundProcess.init(getPythonExecutableFile());
     40  let args = [
     41    kDesktopCheckerScriptPath,
     42    "--verbose",
     43    "--set-background-image",
     44    imagePath,
     45  ];
     46  setBackgroundProcess.run(true, args, args.length);
     47  return setBackgroundProcess.exitValue;
     48 }
     49 
     50 // Check the desktop background is |imagePath| using a CLI command.
     51 // Returns the exit code of the CLI command which reflects whether or not
     52 // the provided image path matches the path of the current desktop background
     53 // image. A return value of 0 indicates success/match.
     54 function checkDesktopBackgroundCLI(imagePath) {
     55  let checkBackgroundProcess = createProcess();
     56  checkBackgroundProcess.init(getPythonExecutableFile());
     57  let args = [
     58    kDesktopCheckerScriptPath,
     59    "--verbose",
     60    "--check-background-image",
     61    imagePath,
     62  ];
     63  checkBackgroundProcess.run(true, args, args.length);
     64  return checkBackgroundProcess.exitValue;
     65 }
     66 
     67 // Use the python script to set/check the desktop background is |imagePath|
     68 function setAndCheckDesktopBackgroundCLI(imagePath) {
     69  Assert.ok(FileUtils.File(imagePath).exists(), `${imagePath} exists`);
     70 
     71  let setExitCode = setDesktopBackgroundCLI(imagePath);
     72  Assert.equal(setExitCode, 0, `Setting background via CLI to ${imagePath}`);
     73 
     74  let checkExitCode = checkDesktopBackgroundCLI(imagePath);
     75  Assert.equal(checkExitCode, 0, `Checking background via CLI is ${imagePath}`);
     76 }
     77 
     78 // Restore the automation default background image. i.e., the default used
     79 // in the automated test environment, not the OS default.
     80 function restoreDefaultBackground() {
     81  let defaultBackgroundPath;
     82  defaultBackgroundPath = kDefaultBackgroundImage;
     83  setAndCheckDesktopBackgroundCLI(defaultBackgroundPath);
     84 }
     85 
     86 add_setup(async function () {
     87  await SpecialPowers.pushPrefEnv({
     88    set: [["test.wait300msAfterTabSwitch", true]],
     89  });
     90 });
     91 
     92 /**
     93 * Tests "Set As Desktop Background" platform implementation on macOS.
     94 *
     95 * Sets the desktop background image to the browser logo from the about:logo
     96 * page and verifies it was set successfully. Setting the desktop background
     97 * (which uses the nsIShellService::setDesktopBackground() interface method)
     98 * downloads the image to ~/Pictures using a unique file name and sets the
     99 * desktop background to the downloaded file leaving the download in place.
    100 * After setDesktopBackground() is called, the test uses a python script to
    101 * validate that the current desktop background is in fact set to the
    102 * downloaded logo.
    103 */
    104 add_task(async function () {
    105  await BrowserTestUtils.withNewTab(
    106    {
    107      gBrowser,
    108      url: "about:logo",
    109    },
    110    async () => {
    111      let dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(
    112        Ci.nsIDirectoryServiceProvider
    113      );
    114      let uuidGenerator = Services.uuid;
    115      let shellSvc = Cc["@mozilla.org/browser/shell-service;1"].getService(
    116        Ci.nsIShellService
    117      );
    118 
    119      // Ensure we are starting with the default background. Log a
    120      // failure if we can not set the background to the default, but
    121      // ignore the case where the background is not already set as that
    122      // that may be due to a previous test failure.
    123      restoreDefaultBackground();
    124 
    125      // Generate a UUID (with non-alphanumberic characters removed) to build
    126      // up a filename for the desktop background. Use a UUID to distinguish
    127      // between runs so we won't be confused by images that were not properly
    128      // cleaned up after previous runs.
    129      let uuid = uuidGenerator.generateUUID().toString().replace(/\W/g, "");
    130 
    131      // Set the background image path to be $HOME/Pictures/<UUID>.png.
    132      // nsIShellService.setDesktopBackground() downloads the image to this
    133      // path and then sets it as the desktop background image, leaving the
    134      // image in place.
    135      let backgroundImage = dirSvc.getFile(NS_OSX_PICTURE_DOCUMENTS_DIR, {});
    136      backgroundImage.append(uuid + ".png");
    137      if (backgroundImage.exists()) {
    138        backgroundImage.remove(false);
    139      }
    140 
    141      // For simplicity, we're going to reach in and access the image on the
    142      // page directly, which means the page shouldn't be running in a remote
    143      // browser. Thankfully, about:logo runs in the parent process for now.
    144      Assert.ok(
    145        !gBrowser.selectedBrowser.isRemoteBrowser,
    146        "image can be accessed synchronously from the parent process"
    147      );
    148      let image = gBrowser.selectedBrowser.contentDocument.images[0];
    149 
    150      info(`Setting/saving desktop background to ${backgroundImage.path}`);
    151 
    152      // Saves the file in ~/Pictures
    153      shellSvc.setDesktopBackground(image, 0, backgroundImage.leafName);
    154 
    155      await BrowserTestUtils.waitForCondition(() => backgroundImage.exists());
    156      info(`${backgroundImage.path} downloaded`);
    157      Assert.ok(
    158        FileUtils.File(backgroundImage.path).exists(),
    159        `${backgroundImage.path} exists`
    160      );
    161 
    162      // Check that the desktop background image is the image we set above.
    163      let exitCode = checkDesktopBackgroundCLI(backgroundImage.path);
    164      Assert.equal(exitCode, 0, `background should be ${backgroundImage.path}`);
    165 
    166      // Restore the background image to the Mac default.
    167      restoreDefaultBackground();
    168 
    169      // We no longer need the downloaded image.
    170      backgroundImage.remove(false);
    171    }
    172  );
    173 });