summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/browser/browser_date_telemetry.js
blob: b9c653db539cec6e86c69a46db1060d8f3a5d3fc (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

const triggers = [
  "Sep 26 Tues 1995",
  "Sep 26 1995 Tues",
  "Sep 26 1995 Tues 09:30",
  "Sep 26 1995 09:Tues:30",
  "Sep 26 1995 09:30 Tues GMT",
  "Sep 26 1995 09:30 GMT Tues",

  "26 Tues Sep 1995",
  "26 Sep Tues 1995",
  "26 Sep 1995 Tues",

  "1995-09-26 Tues",

  // Multiple occurences should only trigger 1 counter
  "Sep 26 Tues 1995 Tues",
];
const nonTriggers = [
  "Sep 26 1995",
  "Tues Sep 26 1995",
  "Sep Tues 26 1995",

  // Invalid format shouldn't trigger the counter
  "Sep 26 Tues 1995 foo",
];

function getCount() {
  return Glean.useCounterPage.jsLateWeekday.testGetValue() ?? 0;
}

/**
 * Opens and closes a browser tab with minimal JS code which parses
 * the given Date format.
 */
async function parseFormat(format, call = "new Date") {
  let newTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    `data:text/html;charset=utf-8,<script>${call}("${format}")</script>`
  );
  BrowserTestUtils.removeTab(newTab);
}

add_task(async function test_date_telemetry() {
  let sum = getCount();

  // waitForCondition cannot be used to test if nothing has changed,
  // so these tests aren't as reliable as the ones in the next loop.
  // If you encounter an inexplicable failure in any of these tests,
  // debug by adding a delay to the end of the parseFormat function.
  for (const format of nonTriggers) {
    await parseFormat(format);
    const count = getCount();
    is(count, sum, `${format} should not trigger telemetry`);
    sum = count;
  }

  for (const [i, format] of triggers.entries()) {
    // Alternate between Date constructor and Date.parse
    await parseFormat(format, ["new Date", "Date.parse"][i % 2]);
    await BrowserTestUtils.waitForCondition(() => getCount() > sum);
    const count = getCount();
    is(count, sum + 1, `${format} should trigger telemetry`);
    sum = count;
  }
});