tor-browser

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

test_backupPrefFile.js (1455B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Tests that we can create a backup of the preferences state to
      8 * a file asynchronously.
      9 */
     10 add_task(async function test_backupPrefFile() {
     11  // Create a backup of the preferences state to a file.
     12  Services.prefs.setBoolPref("test.backup", true);
     13 
     14  let backupFilePath = PathUtils.join(PathUtils.tempDir, "prefs-backup.js");
     15  let backupFile = await IOUtils.getFile(backupFilePath);
     16  await Services.prefs.backupPrefFile(backupFile);
     17 
     18  // Verify that the backup file was created and contains the expected content.
     19  let backupContent = await IOUtils.read(backupFilePath, { encoding: "utf-8" });
     20 
     21  let sawTestValue = false;
     22 
     23  // Now parse the backup file and verify that it contains the expected
     24  // preference value. We'll not worry about any of the other preferences.
     25  let observer = {
     26    onStringPref() {},
     27    onIntPref() {},
     28    onBoolPref(kind, name, value, _isSticky, _isLocked) {
     29      if (name == "test.backup" && value) {
     30        sawTestValue = true;
     31      }
     32    },
     33    onError(message) {
     34      Assert.ok(false, "Error while parsing backup file: " + message);
     35    },
     36  };
     37  Services.prefs.parsePrefsFromBuffer(backupContent, observer);
     38 
     39  Assert.ok(
     40    sawTestValue,
     41    "The backup file contains the expected preference value."
     42  );
     43 
     44  // Clean up the backup file.
     45  await IOUtils.remove(backupFilePath);
     46 });