summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/browser/address/browser_address_telemetry.js
blob: 66dbc2495185b59cc53b9c08eb9d4e58989227d0 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
"use strict";

const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

const { AddressTelemetry } = ChromeUtils.import(
  "resource://autofill/AutofillTelemetry.jsm"
);

// Preference definitions
const ENABLED_PREF = ENABLED_AUTOFILL_ADDRESSES_PREF;
const AVAILABLE_PREF = AUTOFILL_ADDRESSES_AVAILABLE_PREF;
const CAPTURE_ENABLE_PREF = ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF;

// Telemetry definitions
const EVENT_CATEGORY = "address";

const SCALAR_DETECTED_SECTION_COUNT =
  "formautofill.addresses.detected_sections_count";
const SCALAR_SUBMITTED_SECTION_COUNT =
  "formautofill.addresses.submitted_sections_count";
const SCALAR_AUTOFILL_PROFILE_COUNT =
  "formautofill.addresses.autofill_profiles_count";

const HISTOGRAM_PROFILE_NUM_USES = "AUTOFILL_PROFILE_NUM_USES";
const HISTOGRAM_PROFILE_NUM_USES_KEY = "address";

// Autofill UI
const MANAGE_DIALOG_URL = MANAGE_ADDRESSES_DIALOG_URL;
const EDIT_DIALOG_URL = EDIT_ADDRESS_DIALOG_URL;
const DIALOG_SIZE = "width=600,height=400";
const MANAGE_RECORD_SELECTOR = "#addresses";

// Test specific definitions
const TEST_PROFILE = TEST_ADDRESS_1;
const TEST_PROFILE_1 = TEST_ADDRESS_1;
const TEST_PROFILE_2 = TEST_ADDRESS_2;
const TEST_PROFILE_3 = TEST_ADDRESS_3;

const TEST_FOCUS_NAME_FIELD = "given-name";
const TEST_FOCUS_NAME_FIELD_SELECTOR = "#" + TEST_FOCUS_NAME_FIELD;

// Used for tests that update address fields after filling
const TEST_NEW_VALUES = {
  "#given-name": "Test User",
  "#organization": "Sesame Street",
  "#street-address": "123 Sesame Street",
  "#tel": "1-345-345-3456",
};

const TEST_UPDATE_PROFILE_2_VALUES = {
  "#email": "profile2@mozilla.org",
};
const TEST_BASIC_ADDRESS_FORM_URL = ADDRESS_FORM_URL;
const TEST_BASIC_ADDRESS_FORM_WITHOUT_AC_URL =
  ADDRESS_FORM_WITHOUT_AUTOCOMPLETE_URL;
// This should be sync with the address fields that appear in TEST_BASIC_ADDRESS_FORM
const TEST_BASIC_ADDRESS_FORM_FIELDS = [
  "street_address",
  "address_level1",
  "address_level2",
  "postal_code",
  "country",
  "given_name",
  "family_name",
  "organization",
  "email",
  "tel",
];

function buildFormExtra(list, fields, fieldValue, defaultValue, aExtra = {}) {
  let extra = {};
  for (const field of list) {
    if (aExtra[field]) {
      extra[field] = aExtra[field];
    } else {
      extra[field] = fields.includes(field) ? fieldValue : defaultValue;
    }
  }
  return extra;
}

/**
 * Utility function to generate expected value for `address_form` and `address_form_ext`
 * telemetry event.
 *
 * @param {string} method see `methods` in `address_form` event telemetry
 * @param {object} defaultExtra default extra object, this will not be overwritten
 * @param {object} fields address fields that will be set to `value` param
 * @param {string} value value to set for fields list in `fields` argument
 * @param {string} defaultValue value to set for address fields that are not listed in `fields` argument`
 */
function formArgs(
  method,
  defaultExtra,
  fields = [],
  value = undefined,
  defaultValue = null
) {
  if (["popup_shown", "filled_modified"].includes(method)) {
    return [["address", method, "address_form", undefined, defaultExtra]];
  }
  let extra = buildFormExtra(
    AddressTelemetry.SUPPORTED_FIELDS_IN_FORM,
    fields,
    value,
    defaultValue,
    defaultExtra
  );

  let extraExt = buildFormExtra(
    AddressTelemetry.SUPPORTED_FIELDS_IN_FORM_EXT,
    fields,
    value,
    defaultValue,
    defaultExtra
  );

  // The order here should sync with AutofillTelemetry.
  return [
    ["address", method, "address_form", undefined, extra],
    ["address", method, "address_form_ext", undefined, extraExt],
  ];
}

function getProfiles() {
  return getAddresses();
}

async function assertTelemetry(expected_content, expected_parent) {
  let snapshots;

  info(
    `Waiting for ${expected_content?.length ?? 0} content events and ` +
      `${expected_parent?.length ?? 0} parent events`
  );

  await TestUtils.waitForCondition(
    () => {
      snapshots = Services.telemetry.snapshotEvents(
        Ci.nsITelemetry.DATASET_PRERELEASE_CHANNELS,
        false
      );

      return (
        (snapshots.parent?.length ?? 0) >= (expected_parent?.length ?? 0) &&
        (snapshots.content?.length ?? 0) >= (expected_content?.length ?? 0)
      );
    },
    "Wait for telemetry to be collected",
    100,
    100
  );

  info(JSON.stringify(snapshots, null, 2));

  if (expected_content !== undefined) {
    expected_content = expected_content.map(
      ([category, method, object, value, extra]) => {
        return { category, method, object, value, extra };
      }
    );

    let clear = expected_parent === undefined;

    TelemetryTestUtils.assertEvents(
      expected_content,
      {
        category: EVENT_CATEGORY,
      },
      { clear, process: "content" }
    );
  }

  if (expected_parent !== undefined) {
    expected_parent = expected_parent.map(
      ([category, method, object, value, extra]) => {
        return { category, method, object, value, extra };
      }
    );
    TelemetryTestUtils.assertEvents(
      expected_parent,
      {
        category: EVENT_CATEGORY,
      },
      { process: "parent" }
    );
  }
}

function _assertHistogram(snapshot, expectedNonZeroRanges) {
  // Compute the actual ranges in the format { range1: value1, range2: value2 }.
  let actualNonZeroRanges = {};
  for (let [range, value] of Object.entries(snapshot.values)) {
    if (value > 0) {
      actualNonZeroRanges[range] = value;
    }
  }

  // These are stringified to visualize the differences between the values.
  Assert.equal(
    JSON.stringify(actualNonZeroRanges),
    JSON.stringify(expectedNonZeroRanges)
  );
}

function assertKeyedHistogram(histogramId, key, expected) {
  let snapshot = Services.telemetry
    .getKeyedHistogramById(histogramId)
    .snapshot();

  if (expected == undefined) {
    Assert.deepEqual(snapshot, {});
  } else {
    _assertHistogram(snapshot[key], expected);
  }
}

async function openTabAndUseAutofillProfile(
  idx,
  profile,
  { closeTab = true, submitForm = true } = {}
) {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TEST_BASIC_ADDRESS_FORM_URL
  );
  let browser = tab.linkedBrowser;

  await openPopupOn(browser, "form " + TEST_FOCUS_NAME_FIELD_SELECTOR);

  for (let i = 0; i <= idx; i++) {
    await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);
  }

  await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);
  await waitForAutofill(
    browser,
    TEST_FOCUS_NAME_FIELD_SELECTOR,
    profile[TEST_FOCUS_NAME_FIELD]
  );
  await focusUpdateSubmitForm(
    browser,
    {
      focusSelector: TEST_FOCUS_NAME_FIELD_SELECTOR,
      newValues: {},
    },
    submitForm
  );

  if (!closeTab) {
    return tab;
  }

  await BrowserTestUtils.removeTab(tab);
  return null;
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      [ENABLED_PREF, true],
      [AVAILABLE_PREF, "on"],
      [CAPTURE_ENABLE_PREF, true],
    ],
  });

  Services.telemetry.setEventRecordingEnabled(EVENT_CATEGORY, true);

  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();

  registerCleanupFunction(() => {
    Services.telemetry.setEventRecordingEnabled(EVENT_CATEGORY, false);
  });
});

add_task(async function test_popup_opened() {
  await setStorage(TEST_PROFILE);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: TEST_BASIC_ADDRESS_FORM_URL },
    async function (browser) {
      const focusInput = TEST_FOCUS_NAME_FIELD_SELECTOR;

      await openPopupOn(browser, focusInput);

      // Clean up
      await closePopup(browser);
    }
  );

  const fields = TEST_BASIC_ADDRESS_FORM_FIELDS;
  await assertTelemetry([
    ...formArgs("detected", {}, fields, "true", "false"),
    ...formArgs("popup_shown", { field_name: TEST_FOCUS_NAME_FIELD }),
  ]);

  TelemetryTestUtils.assertScalar(
    TelemetryTestUtils.getProcessScalars("content"),
    SCALAR_DETECTED_SECTION_COUNT,
    1,
    "There should be 1 section detected."
  );
  TelemetryTestUtils.assertScalarUnset(
    TelemetryTestUtils.getProcessScalars("content"),
    SCALAR_SUBMITTED_SECTION_COUNT,
    1
  );

  await removeAllRecords();
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
});

add_task(async function test_popup_opened_form_without_autocomplete() {
  await setStorage(TEST_PROFILE);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: TEST_BASIC_ADDRESS_FORM_WITHOUT_AC_URL },
    async function (browser) {
      const focusInput = TEST_FOCUS_NAME_FIELD_SELECTOR;
      await openPopupOn(browser, focusInput);
      await closePopup(browser);
    }
  );

  const fields = TEST_BASIC_ADDRESS_FORM_FIELDS;
  await assertTelemetry([
    ...formArgs("detected", {}, fields, "0", "false"),
    ...formArgs("popup_shown", { field_name: TEST_FOCUS_NAME_FIELD }),
  ]);

  TelemetryTestUtils.assertScalar(
    TelemetryTestUtils.getProcessScalars("content"),
    SCALAR_DETECTED_SECTION_COUNT,
    1,
    "There should be 1 section detected."
  );
  TelemetryTestUtils.assertScalarUnset(
    TelemetryTestUtils.getProcessScalars("content"),
    SCALAR_SUBMITTED_SECTION_COUNT
  );

  await removeAllRecords();
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
});

add_task(async function test_submit_autofill_profile_new() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.formautofill.firstTimeUse", true]],
  });
  async function test_per_command(
    command,
    idx,
    useCount = {},
    expectChanged = undefined
  ) {
    await BrowserTestUtils.withNewTab(
      { gBrowser, url: TEST_BASIC_ADDRESS_FORM_URL },
      async function (browser) {
        let onPopupShown = waitForPopupShown();
        let onChanged;
        if (expectChanged !== undefined) {
          onChanged = TestUtils.topicObserved("formautofill-storage-changed");
        }

        await focusUpdateSubmitForm(browser, {
          focusSelector: TEST_FOCUS_NAME_FIELD_SELECTOR,
          newValues: TEST_NEW_VALUES,
        });

        await onPopupShown;
        await clickDoorhangerButton(command, idx);
        if (expectChanged !== undefined) {
          await onChanged;
          TelemetryTestUtils.assertScalar(
            TelemetryTestUtils.getProcessScalars("parent"),
            SCALAR_AUTOFILL_PROFILE_COUNT,
            expectChanged,
            "There should be ${expectChanged} profile(s) stored."
          );
        }
      }
    );

    assertKeyedHistogram(
      HISTOGRAM_PROFILE_NUM_USES,
      HISTOGRAM_PROFILE_NUM_USES_KEY,
      useCount
    );

    await removeAllRecords();
  }

  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
  Services.telemetry.getKeyedHistogramById(HISTOGRAM_PROFILE_NUM_USES).clear();

  const fields = TEST_BASIC_ADDRESS_FORM_FIELDS;
  let expected_content = [
    ...formArgs("detected", {}, fields, "true", "false"),
    ...formArgs("submitted", {}, fields, "user_filled", "unavailable"),
  ];

  // FTU
  await test_per_command(MAIN_BUTTON, undefined, { 1: 1 }, 1);
  await assertTelemetry(expected_content, [
    [EVENT_CATEGORY, "show", "capture_doorhanger"],
    [EVENT_CATEGORY, "pref", "capture_doorhanger"],
  ]);

  // Need to close preference tab
  BrowserTestUtils.removeTab(gBrowser.selectedTab);

  TelemetryTestUtils.assertScalar(
    TelemetryTestUtils.getProcessScalars("content"),
    SCALAR_DETECTED_SECTION_COUNT,
    1,
    "There should be 1 sections detected."
  );
  TelemetryTestUtils.assertScalar(
    TelemetryTestUtils.getProcessScalars("content"),
    SCALAR_SUBMITTED_SECTION_COUNT,
    1,
    "There should be 1 section submitted."
  );

  await removeAllRecords();
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
});

add_task(async function test_submit_autofill_profile_update() {
  async function test_per_command(
    command,
    idx,
    useCount = {},
    expectChanged = undefined
  ) {
    await setStorage(TEST_PROFILE_2);
    let profiles = await getProfiles();
    Assert.equal(profiles.length, 1, "1 entry in storage");

    await BrowserTestUtils.withNewTab(
      { gBrowser, url: TEST_BASIC_ADDRESS_FORM_URL },
      async function (browser) {
        let onPopupShown = waitForPopupShown();
        let onChanged;
        if (expectChanged !== undefined) {
          onChanged = TestUtils.topicObserved("formautofill-storage-changed");
        }

        await openPopupOn(browser, TEST_FOCUS_NAME_FIELD_SELECTOR);
        await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);
        await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);

        await waitForAutofill(
          browser,
          TEST_FOCUS_NAME_FIELD_SELECTOR,
          TEST_PROFILE_2[TEST_FOCUS_NAME_FIELD]
        );
        await focusUpdateSubmitForm(browser, {
          focusSelector: TEST_FOCUS_NAME_FIELD_SELECTOR,
          newValues: TEST_UPDATE_PROFILE_2_VALUES,
        });
        await onPopupShown;
        await clickDoorhangerButton(command, idx);
        if (expectChanged !== undefined) {
          await onChanged;
          TelemetryTestUtils.assertScalar(
            TelemetryTestUtils.getProcessScalars("parent"),
            SCALAR_AUTOFILL_PROFILE_COUNT,
            expectChanged,
            "There should be ${expectChanged} profile(s) stored."
          );
        }
      }
    );

    assertKeyedHistogram(
      HISTOGRAM_PROFILE_NUM_USES,
      HISTOGRAM_PROFILE_NUM_USES_KEY,
      useCount
    );

    SpecialPowers.clearUserPref(ENABLED_PREF);

    await removeAllRecords();
  }
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
  Services.telemetry.getKeyedHistogramById(HISTOGRAM_PROFILE_NUM_USES).clear();

  const fields = TEST_BASIC_ADDRESS_FORM_FIELDS;
  let expected_content = [
    ...formArgs("detected", {}, fields, "true", "false"),
    ...formArgs("popup_shown", { field_name: TEST_FOCUS_NAME_FIELD }),
    ...formArgs(
      "filled",
      {
        given_name: "filled",
        street_address: "filled",
        country: "filled",
      },
      fields,
      "not_filled",
      "unavailable"
    ),
    ...formArgs(
      "submitted",
      {
        given_name: "autofilled",
        street_address: "autofilled",
        country: "autofilled",
        email: "user_filled",
      },
      fields,
      "not_filled",
      "unavailable"
    ),
  ];

  await test_per_command(MAIN_BUTTON, undefined, { 1: 1 }, 1);
  await assertTelemetry(expected_content, [
    [EVENT_CATEGORY, "show", "update_doorhanger"],
    [EVENT_CATEGORY, "update", "update_doorhanger"],
  ]);

  await test_per_command(SECONDARY_BUTTON, undefined, { 0: 1, 1: 1 }, 2);
  await assertTelemetry(expected_content, [
    [EVENT_CATEGORY, "show", "update_doorhanger"],
    [EVENT_CATEGORY, "save", "update_doorhanger"],
  ]);

  await removeAllRecords();
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
});

add_task(async function test_removingAutofillProfilesViaKeyboardDelete() {
  await setStorage(TEST_PROFILE);

  let win = window.openDialog(MANAGE_DIALOG_URL, null, DIALOG_SIZE);
  await waitForFocusAndFormReady(win);

  let selRecords = win.document.querySelector(MANAGE_RECORD_SELECTOR);
  Assert.equal(selRecords.length, 1, "One entry");

  EventUtils.synthesizeMouseAtCenter(selRecords.children[0], {}, win);
  EventUtils.synthesizeKey("VK_DELETE", {}, win);
  await BrowserTestUtils.waitForEvent(selRecords, "RecordsRemoved");
  Assert.equal(selRecords.length, 0, "No entry left");

  win.close();

  await assertTelemetry(undefined, [
    [EVENT_CATEGORY, "show", "manage"],
    [EVENT_CATEGORY, "delete", "manage"],
  ]);

  await removeAllRecords();
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
});

add_task(async function test_saveAutofillProfile() {
  Services.telemetry.clearEvents();

  await testDialog(EDIT_DIALOG_URL, win => {
    // TODP: Default to US because the layout will be different
    EventUtils.synthesizeKey("VK_TAB", {}, win);
    EventUtils.synthesizeKey(TEST_PROFILE["given-name"], {}, win);
    EventUtils.synthesizeKey("VK_TAB", {}, win);
    EventUtils.synthesizeKey("VK_TAB", {}, win);
    EventUtils.synthesizeKey(TEST_PROFILE["family-name"], {}, win);
    EventUtils.synthesizeKey("VK_TAB", {}, win);
    EventUtils.synthesizeKey(TEST_PROFILE["street-address"], {}, win);
    EventUtils.synthesizeKey("VK_TAB", {}, win);
    EventUtils.synthesizeKey(TEST_PROFILE["address-level2"], {}, win);
    EventUtils.synthesizeKey("VK_TAB", {}, win);
    EventUtils.synthesizeKey("VK_TAB", {}, win);
    EventUtils.synthesizeKey(TEST_PROFILE["postal-code"], {}, win);
    info("saving one entry");
    win.document.querySelector("#save").click();
  });

  await assertTelemetry(undefined, [[EVENT_CATEGORY, "add", "manage"]]);

  await removeAllRecords();
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
});

add_task(async function test_editAutofillProfile() {
  Services.telemetry.clearEvents();

  await setStorage(TEST_PROFILE);

  let profiles = await getProfiles();
  Assert.equal(profiles.length, 1, "1 entry in storage");
  await testDialog(
    EDIT_DIALOG_URL,
    win => {
      EventUtils.synthesizeKey("VK_TAB", {}, win);
      EventUtils.synthesizeKey("VK_RIGHT", {}, win);
      EventUtils.synthesizeKey("test", {}, win);
      win.document.querySelector("#save").click();
    },
    {
      record: profiles[0],
    }
  );

  await assertTelemetry(undefined, [
    [EVENT_CATEGORY, "show_entry", "manage"],
    [EVENT_CATEGORY, "edit", "manage"],
  ]);

  await removeAllRecords();
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
});

add_task(async function test_histogram() {
  Services.telemetry.getKeyedHistogramById(HISTOGRAM_PROFILE_NUM_USES).clear();

  await setStorage(TEST_PROFILE_1, TEST_PROFILE_2, TEST_PROFILE_3);
  let profiles = await getProfiles();
  Assert.equal(profiles.length, 3, "3 entry in storage");

  assertKeyedHistogram(
    HISTOGRAM_PROFILE_NUM_USES,
    HISTOGRAM_PROFILE_NUM_USES_KEY,
    { 0: 3 }
  );

  await openTabAndUseAutofillProfile(0, TEST_PROFILE_1);
  assertKeyedHistogram(
    HISTOGRAM_PROFILE_NUM_USES,
    HISTOGRAM_PROFILE_NUM_USES_KEY,
    { 0: 2, 1: 1 }
  );

  await openTabAndUseAutofillProfile(1, TEST_PROFILE_2);
  assertKeyedHistogram(
    HISTOGRAM_PROFILE_NUM_USES,
    HISTOGRAM_PROFILE_NUM_USES_KEY,
    { 0: 1, 1: 2 }
  );

  await openTabAndUseAutofillProfile(0, TEST_PROFILE_2);
  assertKeyedHistogram(
    HISTOGRAM_PROFILE_NUM_USES,
    HISTOGRAM_PROFILE_NUM_USES_KEY,
    { 0: 1, 1: 1, 2: 1 }
  );

  await openTabAndUseAutofillProfile(1, TEST_PROFILE_1);
  assertKeyedHistogram(
    HISTOGRAM_PROFILE_NUM_USES,
    HISTOGRAM_PROFILE_NUM_USES_KEY,
    { 0: 1, 2: 2 }
  );

  await openTabAndUseAutofillProfile(2, TEST_PROFILE_3);
  assertKeyedHistogram(
    HISTOGRAM_PROFILE_NUM_USES,
    HISTOGRAM_PROFILE_NUM_USES_KEY,
    { 1: 1, 2: 2 }
  );

  await removeAllRecords();

  assertKeyedHistogram(
    HISTOGRAM_PROFILE_NUM_USES,
    HISTOGRAM_PROFILE_NUM_USES_KEY,
    undefined
  );

  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
});

add_task(async function test_clear_autofill_profile_autofill() {
  // Address does not have clear pref. Keep the test so we know we should implement
  // the test if we support clearing address via autocomplete.
  Assert.ok(true);
});