summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_username_only_form_telemetry.js
blob: 54304c24aca1f45e08748bf8ed3abbb55164e99a (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-disable mozilla/no-arbitrary-setTimeout */

"use strict";

async function setupForms(numUsernameOnly, numBasic, numOther) {
  const TEST_HOSTNAME = "https://example.com";
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TEST_HOSTNAME + DIRECTORY_PATH + "empty.html"
  );

  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [
      {
        numUsernameOnly,
        numBasic,
      },
    ],
    async function (data) {
      // type: 1: basic, 2:usernameOnly, 3:other
      function addForm(type) {
        const form = content.document.createElement("form");
        content.document.body.appendChild(form);

        const user = content.document.createElement("input");
        if (type === 3) {
          user.type = "url";
        } else {
          user.type = "text";
          user.autocomplete = "username";
        }
        form.appendChild(user);

        if (type === 1) {
          const password = content.document.createElement("input");
          password.type = "password";
          form.appendChild(password);
        }
      }
      for (let i = 0; i < data.numBasic; i++) {
        addForm(1);
      }
      for (let i = 0; i < data.numUsernameOnly; i++) {
        addForm(2);
      }
      for (let i = 0; i < data.numOther; i++) {
        addForm(3);
      }
    }
  );

  return tab;
}

async function checkChildHistogram(id, index, expected) {
  let histogram;
  await TestUtils.waitForCondition(() => {
    let histograms = Services.telemetry.getSnapshotForHistograms(
      "main",
      false /* clear */
    ).content;

    histogram = histograms[id];
    return !!histogram && histogram.values[index] == expected;
  });
  Assert.equal(histogram.values[index], expected);
}

add_setup(async function () {
  SpecialPowers.pushPrefEnv({
    set: [
      ["signon.usernameOnlyForm.enabled", true],
      ["signon.usernameOnlyForm.lookupThreshold", 100], // ignore the threshold in test
    ],
  });

  // Wait 1sec to make sure all the telemetry data recorded prior to the beginning of the
  // test is cleared.
  await new Promise(res => setTimeout(res, 1000));
  Services.telemetry.getSnapshotForHistograms("main", true /* clear */);
});

add_task(async function test_oneUsernameOnlyForm() {
  const numUsernameOnlyForms = 1;
  const numBasicForms = 0;

  // number of "other" forms doesn't change the outcome, set it to 2 here and
  // in the following testcase just to ensure it doesn't affect the result.
  let tab = await setupForms(numUsernameOnlyForms, numBasicForms, 2);

  await checkChildHistogram(
    "PWMGR_IS_USERNAME_ONLY_FORM",
    1,
    numUsernameOnlyForms
  );
  await checkChildHistogram(
    "PWMGR_NUM_FORM_HAS_POSSIBLE_USERNAME_EVENT_PER_DOC",
    numUsernameOnlyForms,
    1
  );

  BrowserTestUtils.removeTab(tab);
  Services.telemetry.getSnapshotForHistograms("main", true /* clear */);
});

add_task(async function test_multipleUsernameOnlyForms() {
  const numUsernameOnlyForms = 3;
  const numBasicForms = 2;

  let tab = await setupForms(numUsernameOnlyForms, numBasicForms, 2);

  await checkChildHistogram(
    "PWMGR_IS_USERNAME_ONLY_FORM",
    1,
    numUsernameOnlyForms
  );
  await checkChildHistogram(
    "PWMGR_NUM_FORM_HAS_POSSIBLE_USERNAME_EVENT_PER_DOC",
    5,
    1
  );

  BrowserTestUtils.removeTab(tab);
  Services.telemetry.getSnapshotForHistograms("main", true /* clear */);
});

add_task(async function test_multipleDocument() {
  // The first document
  let numUsernameOnlyForms1 = 2;
  let numBasicForms1 = 2;

  let tab1 = await setupForms(numUsernameOnlyForms1, numBasicForms1, 2);

  await checkChildHistogram(
    "PWMGR_IS_USERNAME_ONLY_FORM",
    1,
    numUsernameOnlyForms1
  );
  await checkChildHistogram(
    "PWMGR_NUM_FORM_HAS_POSSIBLE_USERNAME_EVENT_PER_DOC",
    numUsernameOnlyForms1 + numBasicForms1,
    1
  );

  // The second document
  let numUsernameOnlyForms2 = 15;
  let numBasicForms2 = 3;

  let tab2 = await setupForms(numUsernameOnlyForms2, numBasicForms2, 2);

  await checkChildHistogram(
    "PWMGR_IS_USERNAME_ONLY_FORM",
    1,
    numUsernameOnlyForms1 + numUsernameOnlyForms2
  );

  // the result is stacked, so the new document add a counter to all
  // buckets under "numUsernameOnlyForms2 + numBasicForms2"
  await checkChildHistogram(
    "PWMGR_NUM_FORM_HAS_POSSIBLE_USERNAME_EVENT_PER_DOC",
    numUsernameOnlyForms1 + numBasicForms1,
    2
  );
  await checkChildHistogram(
    "PWMGR_NUM_FORM_HAS_POSSIBLE_USERNAME_EVENT_PER_DOC",
    numUsernameOnlyForms2 + numBasicForms2,
    1
  );

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
  Services.telemetry.getSnapshotForHistograms("main", true /* clear */);
});

add_task(async function test_tooManyUsernameOnlyForms() {
  const numUsernameOnlyForms = 25;
  const numBasicForms = 2;

  let tab = await setupForms(numUsernameOnlyForms, numBasicForms, 2);

  await checkChildHistogram(
    "PWMGR_IS_USERNAME_ONLY_FORM",
    1,
    numUsernameOnlyForms
  );
  await checkChildHistogram(
    "PWMGR_NUM_FORM_HAS_POSSIBLE_USERNAME_EVENT_PER_DOC",
    21,
    numUsernameOnlyForms + numBasicForms - 20 // maximum is 20
  );

  BrowserTestUtils.removeTab(tab);
  Services.telemetry.getSnapshotForHistograms("main", true /* clear */);
});