summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/unit/test_LaterRun.js
blob: d78fde2414157d382ef891566ca41db61b0857d3 (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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"use strict";

const kEnabledPref = "browser.laterrun.enabled";
const kPagePrefRoot = "browser.laterrun.pages.";
const kSessionCountPref = "browser.laterrun.bookkeeping.sessionCount";
const kProfileCreationTime = "browser.laterrun.bookkeeping.profileCreationTime";

const { LaterRun } = ChromeUtils.import("resource:///modules/LaterRun.jsm");

Services.prefs.setBoolPref(kEnabledPref, true);
const { updateAppInfo } = ChromeUtils.importESModule(
  "resource://testing-common/AppInfo.sys.mjs"
);
updateAppInfo();

add_task(async function test_page_applies() {
  Services.prefs.setCharPref(
    kPagePrefRoot + "test_LaterRun_unittest.url",
    "https://www.mozilla.org/%VENDOR%/%NAME%/%ID%/%VERSION%/"
  );
  Services.prefs.setIntPref(
    kPagePrefRoot + "test_LaterRun_unittest.minimumHoursSinceInstall",
    10
  );
  Services.prefs.setIntPref(
    kPagePrefRoot + "test_LaterRun_unittest.minimumSessionCount",
    3
  );

  let pages = LaterRun.readPages();
  // We have to filter the pages because it's possible Firefox ships with other URLs
  // that get included in this test.
  pages = pages.filter(
    page => page.pref == kPagePrefRoot + "test_LaterRun_unittest."
  );
  Assert.equal(pages.length, 1, "Got 1 page");
  let page = pages[0];
  Assert.equal(
    page.pref,
    kPagePrefRoot + "test_LaterRun_unittest.",
    "Should know its own pref"
  );
  Assert.equal(
    page.minimumHoursSinceInstall,
    10,
    "Needs to have 10 hours since install"
  );
  Assert.equal(page.minimumSessionCount, 3, "Needs to have 3 sessions");
  Assert.equal(page.requireBoth, false, "Either requirement is enough");
  let expectedURL =
    "https://www.mozilla.org/" +
    Services.appinfo.vendor +
    "/" +
    Services.appinfo.name +
    "/" +
    Services.appinfo.ID +
    "/" +
    Services.appinfo.version +
    "/";
  Assert.equal(page.url, expectedURL, "URL is stored correctly");

  Assert.ok(
    page.applies({ hoursSinceInstall: 1, sessionCount: 3 }),
    "Applies when session count has been met."
  );
  Assert.ok(
    page.applies({ hoursSinceInstall: 1, sessionCount: 4 }),
    "Applies when session count has been exceeded."
  );
  Assert.ok(
    page.applies({ hoursSinceInstall: 10, sessionCount: 2 }),
    "Applies when total session time has been met."
  );
  Assert.ok(
    page.applies({ hoursSinceInstall: 20, sessionCount: 2 }),
    "Applies when total session time has been exceeded."
  );
  Assert.ok(
    page.applies({ hoursSinceInstall: 10, sessionCount: 3 }),
    "Applies when both time and session count have been met."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 1, sessionCount: 1 }),
    "Does not apply when neither time and session count have been met."
  );

  page.requireBoth = true;

  Assert.ok(
    !page.applies({ hoursSinceInstall: 1, sessionCount: 3 }),
    "Does not apply when only session count has been met."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 1, sessionCount: 4 }),
    "Does not apply when only session count has been exceeded."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 10, sessionCount: 2 }),
    "Does not apply when only total session time has been met."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 20, sessionCount: 2 }),
    "Does not apply when only total session time has been exceeded."
  );
  Assert.ok(
    page.applies({ hoursSinceInstall: 10, sessionCount: 3 }),
    "Applies when both time and session count have been met."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 1, sessionCount: 1 }),
    "Does not apply when neither time and session count have been met."
  );

  // Check that pages that have run never apply:
  Services.prefs.setBoolPref(
    kPagePrefRoot + "test_LaterRun_unittest.hasRun",
    true
  );
  page.requireBoth = false;

  Assert.ok(
    !page.applies({ hoursSinceInstall: 1, sessionCount: 3 }),
    "Does not apply when page has already run (sessionCount equal)."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 1, sessionCount: 4 }),
    "Does not apply when page has already run (sessionCount exceeding)."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 10, sessionCount: 2 }),
    "Does not apply when page has already run (hoursSinceInstall equal)."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 20, sessionCount: 2 }),
    "Does not apply when page has already run (hoursSinceInstall exceeding)."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 10, sessionCount: 3 }),
    "Does not apply when page has already run (both criteria equal)."
  );
  Assert.ok(
    !page.applies({ hoursSinceInstall: 1, sessionCount: 1 }),
    "Does not apply when page has already run (both criteria insufficient anyway)."
  );

  clearAllPagePrefs();
});

add_task(async function test_get_URL() {
  Services.prefs.setIntPref(
    kProfileCreationTime,
    Math.floor((Date.now() - 11 * 60 * 60 * 1000) / 1000)
  );
  Services.prefs.setCharPref(
    kPagePrefRoot + "test_LaterRun_unittest.url",
    "https://www.mozilla.org/"
  );
  Services.prefs.setIntPref(
    kPagePrefRoot + "test_LaterRun_unittest.minimumHoursSinceInstall",
    10
  );
  Services.prefs.setIntPref(
    kPagePrefRoot + "test_LaterRun_unittest.minimumSessionCount",
    3
  );
  let pages = LaterRun.readPages();
  // We have to filter the pages because it's possible Firefox ships with other URLs
  // that get included in this test.
  pages = pages.filter(
    page => page.pref == kPagePrefRoot + "test_LaterRun_unittest."
  );
  Assert.equal(pages.length, 1, "Should only be 1 matching page");
  let page = pages[0];
  let url;
  do {
    url = LaterRun.getURL();
    // We have to loop because it's possible Firefox ships with other URLs that get triggered by
    // this test.
  } while (url && url != "https://www.mozilla.org/");
  Assert.equal(
    url,
    "https://www.mozilla.org/",
    "URL should be as expected when prefs are set."
  );
  Assert.ok(
    Services.prefs.prefHasUserValue(
      kPagePrefRoot + "test_LaterRun_unittest.hasRun"
    ),
    "Should have set pref"
  );
  Assert.ok(
    Services.prefs.getBoolPref(kPagePrefRoot + "test_LaterRun_unittest.hasRun"),
    "Should have set pref to true"
  );
  Assert.ok(page.hasRun, "Other page objects should know it has run, too.");

  clearAllPagePrefs();
});

add_task(async function test_insecure_urls() {
  Services.prefs.setCharPref(
    kPagePrefRoot + "test_LaterRun_unittest.url",
    "http://www.mozilla.org/"
  );
  Services.prefs.setIntPref(
    kPagePrefRoot + "test_LaterRun_unittest.minimumHoursSinceInstall",
    10
  );
  Services.prefs.setIntPref(
    kPagePrefRoot + "test_LaterRun_unittest.minimumSessionCount",
    3
  );
  let pages = LaterRun.readPages();
  // We have to filter the pages because it's possible Firefox ships with other URLs
  // that get triggered in this test.
  pages = pages.filter(
    page => page.pref == kPagePrefRoot + "test_LaterRun_unittest."
  );
  Assert.equal(pages.length, 0, "URL with non-https scheme should get ignored");
  clearAllPagePrefs();
});

add_task(async function test_dynamic_pref_getter_setter() {
  delete LaterRun._sessionCount;
  Services.prefs.setIntPref(kSessionCountPref, 0);
  Assert.equal(LaterRun.sessionCount, 0, "Should start at 0");

  LaterRun.sessionCount++;
  Assert.equal(LaterRun.sessionCount, 1, "Should increment.");
  Assert.equal(
    Services.prefs.getIntPref(kSessionCountPref),
    1,
    "Should update pref"
  );
});

function clearAllPagePrefs() {
  let allChangedPrefs = Services.prefs.getChildList(kPagePrefRoot);
  for (let pref of allChangedPrefs) {
    Services.prefs.clearUserPref(pref);
  }
}