tor-browser

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

test_selectable_profiles_lifecycle.js (6944B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { MockRegistrar } = ChromeUtils.importESModule(
      7  "resource://testing-common/MockRegistrar.sys.mjs"
      8 );
      9 
     10 // Windows doesn't support the normal permissions and always creates and returns as 0666.
     11 const EXPECTED_PERMISSIONS = AppConstants.platform == "win" ? 0o666 : 0o700;
     12 
     13 const badgingService = {
     14  isRegistered: false,
     15  badge: null,
     16 
     17  // nsIMacDockSupport
     18  setBadgeImage(image, paintContext) {
     19    this.badge = { image, paintContext };
     20  },
     21 
     22  QueryInterface: ChromeUtils.generateQI(["nsIMacDockSupport"]),
     23 
     24  assertBadged(fg, bg) {
     25    if (!this.isRegistered) {
     26      return;
     27    }
     28 
     29    Assert.ok(this.badge?.image, "Should have set a badge image");
     30    Assert.ok(this.badge?.paintContext, "Should have set a paint context");
     31    Assert.equal(
     32      this.badge?.paintContext?.strokeColor,
     33      fg,
     34      "Stroke color should be correct"
     35    );
     36    Assert.equal(
     37      this.badge?.paintContext?.fillColor,
     38      bg,
     39      "Stroke color should be correct"
     40    );
     41  },
     42 
     43  assertNotBadged() {
     44    if (!this.isRegistered) {
     45      return;
     46    }
     47 
     48    Assert.ok(!this.badge?.image, "Should not have set a badge image");
     49    Assert.ok(!this.badge?.paintContext, "Should not have set a paint context");
     50  },
     51 };
     52 
     53 add_setup(() => {
     54  if ("nsIMacDockSupport" in Ci) {
     55    badgingService.isRegistered = true;
     56    MockRegistrar.register(
     57      "@mozilla.org/widget/macdocksupport;1",
     58      badgingService
     59    );
     60  }
     61 });
     62 
     63 add_task(async function test_SelectableProfileLifecycle() {
     64  startProfileService();
     65 
     66  const SelectableProfileService = getSelectableProfileService();
     67  const ProfilesDatastoreService = getProfilesDatastoreService();
     68 
     69  await ProfilesDatastoreService.init();
     70 
     71  Services.prefs.setBoolPref("browser.profiles.enabled", false);
     72  Services.prefs.setBoolPref("browser.profiles.created", false);
     73  await SelectableProfileService.init();
     74  Assert.ok(
     75    !SelectableProfileService.isEnabled,
     76    "Service should not be enabled"
     77  );
     78 
     79  Services.prefs.setBoolPref("browser.profiles.enabled", true);
     80  await SelectableProfileService.init();
     81  Assert.ok(
     82    SelectableProfileService.isEnabled,
     83    "Service should now be enabled"
     84  );
     85 
     86  let profiles = await SelectableProfileService.getAllProfiles();
     87 
     88  Assert.ok(!profiles.length, "No selectable profiles exist yet");
     89 
     90  await SelectableProfileService.maybeSetupDataStore();
     91  let currentProfile = SelectableProfileService.currentProfile;
     92 
     93  badgingService.assertNotBadged();
     94 
     95  const leafName = (await currentProfile.rootDir).leafName;
     96 
     97  const profilePath = PathUtils.join(
     98    Services.dirsvc.get("DefProfRt", Ci.nsIFile).path,
     99    leafName
    100  );
    101 
    102  let profileDirExists = await IOUtils.exists(profilePath);
    103  const profileLocalPath = PathUtils.join(
    104    Services.dirsvc.get("DefProfLRt", Ci.nsIFile).path,
    105    leafName
    106  );
    107  let profileLocalDirExists = await IOUtils.exists(profileLocalPath);
    108 
    109  Assert.ok(
    110    profileDirExists,
    111    `Profile dir was successfully created at ${profilePath}`
    112  );
    113  Assert.ok(
    114    profileLocalDirExists,
    115    `Profile local dir was successfully created at ${profileLocalPath}`
    116  );
    117 
    118  profiles = await SelectableProfileService.getAllProfiles();
    119 
    120  Assert.equal(profiles.length, 1, "One selectable profile exists");
    121 
    122  let selectableProfile = profiles[0];
    123 
    124  Assert.equal(
    125    selectableProfile.id,
    126    SelectableProfileService.currentProfile.id,
    127    "Should be the selected profile."
    128  );
    129 
    130  let profile = await SelectableProfileService.getProfile(selectableProfile.id);
    131 
    132  for (let attr of ["id", "name", "path"]) {
    133    Assert.equal(
    134      profile[attr],
    135      currentProfile[attr],
    136      `We got the correct profile ${attr}`
    137    );
    138 
    139    Assert.equal(
    140      selectableProfile[attr],
    141      currentProfile[attr],
    142      `We got the correct profile ${attr}`
    143    );
    144  }
    145 
    146  selectableProfile.name = "updatedTestProfile";
    147  selectableProfile.theme = {
    148    themeId: "lightTheme",
    149    themeFg: "#e2e1e3",
    150    themeBg: "010203",
    151  };
    152 
    153  await updateNotified();
    154 
    155  profile = await SelectableProfileService.getProfile(selectableProfile.id);
    156 
    157  Assert.equal(
    158    profile.name,
    159    "updatedTestProfile",
    160    "We got the correct profile name: updatedTestProfile"
    161  );
    162 
    163  badgingService.assertNotBadged();
    164 
    165  let newProfile = await createTestProfile({ name: "New profile" });
    166 
    167  await updateNotified();
    168 
    169  let rootDir = await newProfile.rootDir;
    170  let localDir = PathUtils.join(
    171    Services.dirsvc.get("DefProfLRt", Ci.nsIFile).path,
    172    rootDir.leafName
    173  );
    174 
    175  profileDirExists = await IOUtils.exists(rootDir.path);
    176  profileLocalDirExists = await IOUtils.exists(localDir);
    177  Assert.ok(profileDirExists, "Profile dir was successfully created");
    178  Assert.ok(
    179    profileLocalDirExists,
    180    "Profile local dir was successfully created"
    181  );
    182 
    183  Assert.equal(
    184    (await IOUtils.stat(profilePath)).permissions,
    185    EXPECTED_PERMISSIONS,
    186    "Profile dir should have the correct permissions"
    187  );
    188  Assert.equal(
    189    (await IOUtils.stat(profileLocalPath)).permissions,
    190    EXPECTED_PERMISSIONS,
    191    "Profile local dir should have the correct permissions"
    192  );
    193 
    194  let times = PathUtils.join(rootDir.path, "times.json");
    195  Assert.ok(await IOUtils.exists(times), "times.json should exist");
    196  let json = await IOUtils.readJSON(times);
    197  Assert.ok(
    198    json.created <= Date.now() && json.created >= Date.now() - 30000,
    199    "Should have been created roughly now."
    200  );
    201 
    202  let prefs = PathUtils.join(rootDir.path, "prefs.js");
    203  Assert.ok(await IOUtils.exists(prefs), "prefs.js should exist");
    204  let contents = (await IOUtils.readUTF8(prefs)).split("\n");
    205 
    206  let sawStoreID = false;
    207  let sawEnabled = false;
    208  for (let line of contents) {
    209    // Strip the windows \r
    210    line = line.trim();
    211 
    212    if (line == `user_pref("browser.profiles.enabled", true);`) {
    213      sawEnabled = true;
    214    }
    215 
    216    if (
    217      line ==
    218      `user_pref("toolkit.profiles.storeID", "${
    219        getProfileService().currentProfile.storeID
    220      }");`
    221    ) {
    222      sawStoreID = true;
    223    }
    224  }
    225 
    226  Assert.ok(sawStoreID, "Should have seen the store ID defined in prefs.js");
    227  Assert.ok(sawEnabled, "Should have seen the service enabled in prefs.js");
    228 
    229  profiles = await SelectableProfileService.getAllProfiles();
    230  Assert.equal(profiles.length, 2, "Should now be two profiles.");
    231 
    232  badgingService.assertBadged("#e2e1e3", "010203");
    233 
    234  await SelectableProfileService.deleteProfile(newProfile);
    235  await updateNotified();
    236 
    237  profiles = await SelectableProfileService.getAllProfiles();
    238  Assert.equal(profiles.length, 1, "Should now be one profiles.");
    239 
    240  badgingService.assertNotBadged();
    241 
    242  profileDirExists = await IOUtils.exists(rootDir.path);
    243  profileLocalDirExists = await IOUtils.exists(localDir);
    244  Assert.ok(!profileDirExists, "Profile dir was successfully removed");
    245  Assert.ok(
    246    !profileLocalDirExists,
    247    "Profile local dir was successfully removed"
    248  );
    249 });