Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / toolkit / profile / xpcshell / test_select_backgroundtasks_not_ephemeral_create.js
blobebed8893606f361bb71ae6c116aa1104ba0ced74
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 /*
5  * Verify that background tasks that create non-ephemeral profiles update
6  * `profiles.ini` with a salted profile location.
7  */
9 let condition = {
10   skip_if: () => !AppConstants.MOZ_BACKGROUNDTASKS,
13 // MOZ_APP_VENDOR is empty on Thunderbird.
14 let vendor = AppConstants.MOZ_APP_NAME == "thunderbird" ? "" : "Mozilla";
16 add_task(condition, async () => {
17   let hash = xreDirProvider.getInstallHash();
19   writeProfilesIni(BACKGROUNDTASKS_PROFILE_DATA);
21   // Pretend that this is a background task.  For a task that does *not* use an
22   // ephemeral profile, i.e., that uses a persistent profile, `profiles.ini` is
23   // updated with a new entry in section `BackgroundTaskProfiles`.
24   const bts = Cc["@mozilla.org/backgroundtasks;1"].getService(
25     Ci.nsIBackgroundTasks
26   );
27   // "not_ephemeral_profile" is a special name recognized by the
28   // background task system.
29   bts.overrideBackgroundTaskNameForTesting("not_ephemeral_profile");
31   let { didCreate, rootDir } = selectStartupProfile();
32   checkStartupReason("backgroundtask-not-ephemeral");
34   Assert.equal(didCreate, true, "Created new non-ephemeral profile");
36   let profileData = readProfilesIni();
38   // Profile names are lexicographically ordered, and `not_ephemeral_profile`
39   // sorts before `unrelated_task`.
40   Assert.equal(profileData.backgroundTasksProfiles.length, 2);
41   Assert.deepEqual(
42     [profileData.backgroundTasksProfiles[1]],
43     BACKGROUNDTASKS_PROFILE_DATA.backgroundTasksProfiles
44   );
46   let saltedPath = profileData.backgroundTasksProfiles[0].path;
47   Assert.ok(
48     saltedPath.endsWith(
49       `.${vendor}BackgroundTask-${hash}-not_ephemeral_profile`
50     ),
51     `${saltedPath} ends with ".${vendor}BackgroundTask-${hash}-not_ephemeral_profile"`
52   );
53   Assert.ok(
54     !saltedPath.startsWith(
55       `.${vendor}BackgroundTask-${hash}-not_ephemeral_profile`
56     ),
57     `${saltedPath} is really salted`
58   );
59   Assert.deepEqual(profileData.backgroundTasksProfiles[0], {
60     name: `${vendor}BackgroundTask-${hash}-not_ephemeral_profile`,
61     path: saltedPath,
62   });
64   Assert.ok(
65     rootDir.path.endsWith(saltedPath),
66     `rootDir "${rootDir.path}" ends with salted path "${saltedPath}"`
67   );
69   // Really, "UAppData", but this is xpcshell.
70   let backgroundTasksProfilesPath = gDataHome;
71   if (!AppConstants.XP_UNIX || AppConstants.platform === "macosx") {
72     backgroundTasksProfilesPath.append("Background Tasks Profiles");
73   }
74   Assert.ok(
75     rootDir.path.startsWith(backgroundTasksProfilesPath.path),
76     `rootDir "${rootDir.path}" is sibling to user profiles directory ${backgroundTasksProfilesPath}`
77   );
78 });