summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/browser/browser_date_telemetry.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/xpconnect/tests/browser/browser_date_telemetry.js')
-rw-r--r--js/xpconnect/tests/browser/browser_date_telemetry.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/js/xpconnect/tests/browser/browser_date_telemetry.js b/js/xpconnect/tests/browser/browser_date_telemetry.js
new file mode 100644
index 0000000000..b9c653db53
--- /dev/null
+++ b/js/xpconnect/tests/browser/browser_date_telemetry.js
@@ -0,0 +1,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;
+ }
+});