File: test_async_flush.js

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (184 lines) | stat: -rw-r--r-- 5,886 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Verifies the async flushing methods.
 */

add_task(async () => {
  let defaultProfile = makeRandomProfileDir("default");

  let hash = xreDirProvider.getInstallHash();

  writeCompatibilityIni(defaultProfile);

  writeProfilesIni({
    profiles: [
      {
        name: "default",
        path: defaultProfile.leafName,
        default: false,
      },
    ],
    installs: {
      [hash]: { default: defaultProfile.leafName },
    },
  });

  selectStartupProfile();
  checkStartupReason("default");

  let profileData = readProfilesIni();
  checkProfileService(profileData);

  let service = getProfileService();

  let newProfileDir = makeRandomProfileDir("newProfile");
  service.createProfile(newProfileDir, "new");

  await service.asyncFlush();
  profileData = readProfilesIni();

  Assert.equal(profileData.profiles.length, 2, "Should now have two profiles.");

  checkProfileService(profileData);

  let other1 = makeRandomProfileDir("other1");
  let other2 = makeRandomProfileDir("other2");

  // Write out a different ini file.
  writeProfilesIni({
    profiles: [
      {
        name: "changedname",
        path: defaultProfile.leafName,
      },
      {
        name: "other1",
        path: other1.leafName,
      },
      {
        name: "other2",
        path: other2.leafName,
      },
    ],
    installs: {
      [hash]: { default: defaultProfile.leafName },
    },
  });

  // Change the modified time.
  let profilesini = gDataHome.clone();
  profilesini.append("profiles.ini");
  let oldTime = profilesini.lastModifiedTime;
  profilesini.lastModifiedTime = oldTime - 10000;

  try {
    await service.asyncFlush();
    Assert.ok(false, "Flushing should have failed");
  } catch (e) {
    Assert.ok(true, "Flushing should have failed");
  }

  profileData = readProfilesIni();
  Assert.equal(profileData.profiles.length, 3, "Should have three profiles.");

  let found = profileData.profiles.find(p => p.name == "changedname");
  Assert.ok(found, "Should have found the current profile.");
  Assert.equal(found.path, defaultProfile.leafName);
  Assert.equal(found.storeID, null);

  found = profileData.profiles.find(p => p.name == "other1");
  Assert.ok(found, "Should have found the other1 profile.");
  Assert.equal(found.path, other1.leafName);
  Assert.equal(found.storeID, null);

  found = profileData.profiles.find(p => p.name == "other2");
  Assert.ok(found, "Should have found the other2 profile.");
  Assert.equal(found.path, other2.leafName);
  Assert.equal(found.storeID, null);

  let installData = readInstallsIni();
  Assert.equal(profileData.installs[hash].default, defaultProfile.leafName);
  Assert.equal(installData.installs[hash].default, defaultProfile.leafName);

  if (AppConstants.MOZ_SELECTABLE_PROFILES) {
    // Set a store ID on the profile. Flushing will succeed because the profile path hasn't changed.
    service.currentProfile.storeID = "7126354jdf";

    await service.asyncFlushCurrentProfile();

    profileData = readProfilesIni();
    Assert.equal(profileData.profiles.length, 3, "Should have three profiles.");

    found = profileData.profiles.find(p => p.name == "changedname");
    Assert.ok(found, "Should have found the current profile.");
    Assert.equal(found.path, defaultProfile.leafName);
    Assert.equal(found.storeID, "7126354jdf");

    found = profileData.profiles.find(p => p.name == "other1");
    Assert.ok(found, "Should have found the other1 profile.");
    Assert.equal(found.path, other1.leafName);
    Assert.equal(found.storeID, null);

    found = profileData.profiles.find(p => p.name == "other2");
    Assert.ok(found, "Should have found the other2 profile.");
    Assert.equal(found.path, other2.leafName);
    Assert.equal(found.storeID, null);

    installData = readInstallsIni();
    Assert.equal(profileData.installs[hash].default, defaultProfile.leafName);
    Assert.equal(installData.installs[hash].default, defaultProfile.leafName);

    // Change the profile path. Flushing will succeed because the store ID now matches.
    service.currentProfile.rootDir = newProfileDir;

    await service.asyncFlushCurrentProfile();

    profileData = readProfilesIni();
    Assert.equal(profileData.profiles.length, 3, "Should have three profiles.");

    found = profileData.profiles.find(p => p.name == "changedname");
    Assert.ok(found, "Should have found the current profile.");
    Assert.equal(found.path, newProfileDir.leafName);
    Assert.equal(found.storeID, "7126354jdf");

    found = profileData.profiles.find(p => p.name == "other1");
    Assert.ok(found, "Should have found the other1 profile.");
    Assert.equal(found.path, other1.leafName);
    Assert.equal(found.storeID, null);

    found = profileData.profiles.find(p => p.name == "other2");
    Assert.ok(found, "Should have found the other2 profile.");
    Assert.equal(found.path, other2.leafName);
    Assert.equal(found.storeID, null);

    installData = readInstallsIni();
    Assert.equal(profileData.installs[hash].default, newProfileDir.leafName);
    Assert.equal(installData.installs[hash].default, newProfileDir.leafName);

    // Modify the on-disk data
    writeProfilesIni({
      profiles: [
        {
          name: "some other name",
          path: "some other directory",
          storeID: "7126354jdf",
        },
      ],
      installs: {
        [hash]: { default: "some other directory" },
      },
    });

    await service.asyncFlushCurrentProfile();

    profileData = readProfilesIni();
    Assert.equal(profileData.profiles.length, 1, "Should have one profile.");

    found = profileData.profiles[0];
    Assert.ok(found, "Should have found the current profile.");
    Assert.equal(found.path, newProfileDir.leafName);
    Assert.equal(found.storeID, "7126354jdf");
  }
});