summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_telemetry_SignUpFormRuleset.js
blob: dc8ac9c081476992a49ca5c4b117eaeff737bc58 (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
"use strict";

const SIGNUP_DETECTION_HISTOGRAM = "PWMGR_SIGNUP_FORM_DETECTION_MS";
const TEST_URL = `https://example.com${DIRECTORY_PATH}form_signup_detection.html`;

/**
 *
 * @param {Object} histogramData The histogram data to examine
 * @returns The amount of entries found in the histogram data
 */
function countEntries(histogramData) {
  return histogramData
    ? Object.values(histogramData.values).reduce((a, b) => a + b, 0)
    : null;
}

/**
 * @param {String} id The histogram to examine
 * @param {Number} expected The expected amount of entries for a histogram
 */
async function countEntriesOfChildHistogram(id, expected) {
  let histogram;
  await TestUtils.waitForCondition(() => {
    let histograms = Services.telemetry.getSnapshotForHistograms(
      "main",
      false
    ).content;

    histogram = histograms[id];

    return !!histogram && countEntries(histogram) == expected;
  }, `The histogram ${id} was expected to have ${expected} entries.`);
  Assert.equal(countEntries(histogram), expected);
}

add_setup(async () => {
  await SpecialPowers.pushPrefEnv({
    set: [["signon.signupDetection.enabled", true]],
  });
  Services.telemetry.getHistogramById(SIGNUP_DETECTION_HISTOGRAM).clear();
});

add_task(async () => {
  let formProcessed = listenForTestNotification("FormProcessed", 2);

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);

  await formProcessed;

  await countEntriesOfChildHistogram(SIGNUP_DETECTION_HISTOGRAM, 2);

  gBrowser.removeTab(tab);
});