summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_ProfileAge.js
blob: 9a659a5894569b78a01ab1ed1eb8278ee9c3f7c3 (plain)
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
const { ProfileAge } = ChromeUtils.importESModule(
  "resource://gre/modules/ProfileAge.sys.mjs"
);

const gProfD = do_get_profile();
let ID = 0;

// Creates a unique profile directory to use for a test.
function withDummyProfile(task) {
  return async () => {
    let profile = PathUtils.join(gProfD.path, "" + ID++);
    await IOUtils.makeDirectory(profile);
    await task(profile);
    await IOUtils.remove(profile, { recursive: true });
  };
}

add_task(
  withDummyProfile(async profile => {
    let times = await ProfileAge(profile);
    Assert.ok(
      (await times.created) > 0,
      "We can't really say what this will be, just assume if it is a number it's ok."
    );
    Assert.equal(
      await times.reset,
      undefined,
      "Reset time is undefined in a new profile"
    );
    Assert.ok(
      (await times.firstUse) <= Date.now(),
      "Should have initialised a first use time."
    );
  })
);

add_task(
  withDummyProfile(async profile => {
    const CREATED_TIME = Date.now() - 2000;
    const RESET_TIME = Date.now() - 1000;

    await IOUtils.writeJSON(PathUtils.join(profile, "times.json"), {
      created: CREATED_TIME,
    });

    let times = await ProfileAge(profile);
    Assert.equal(
      await times.created,
      CREATED_TIME,
      "Should have seen the right profile time."
    );
    Assert.equal(
      await times.firstUse,
      undefined,
      "Should be no first use time."
    );

    let times2 = await ProfileAge(profile);
    Assert.equal(times, times2, "Should have got the same instance.");

    let promise = times.recordProfileReset(RESET_TIME);
    Assert.equal(
      await times2.reset,
      RESET_TIME,
      "Should have seen the right reset time in the second instance immediately."
    );
    await promise;

    let results = await IOUtils.readJSON(PathUtils.join(profile, "times.json"));
    Assert.deepEqual(
      results,
      {
        created: CREATED_TIME,
        reset: RESET_TIME,
      },
      "Should have seen the right results."
    );
  })
);

add_task(
  withDummyProfile(async profile => {
    const RESET_TIME = Date.now() - 1000;
    const RESET_TIME2 = Date.now() - 2000;

    // The last call to recordProfileReset should always win.
    let times = await ProfileAge(profile);
    await Promise.all([
      times.recordProfileReset(RESET_TIME),
      times.recordProfileReset(RESET_TIME2),
    ]);

    let results = await IOUtils.readJSON(PathUtils.join(profile, "times.json"));
    delete results.firstUse;
    Assert.deepEqual(
      results,
      {
        reset: RESET_TIME2,
      },
      "Should have seen the right results."
    );
  })
);

add_task(
  withDummyProfile(async profile => {
    const CREATED_TIME = Date.now() - 1000;

    await IOUtils.writeJSON(PathUtils.join(profile, "times.json"), {
      created: CREATED_TIME,
      firstUse: null,
    });

    let times = await ProfileAge(profile);
    Assert.ok(
      (await times.firstUse) <= Date.now(),
      "Should have initialised a first use time."
    );
  })
);