summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_savedFieldNames.js
blob: 6e3474c06dd5567f5f8e3fd8b3b55d07b75555f1 (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
/*
 * Test for keeping the valid fields information in sharedData.
 */

"use strict";

let FormAutofillStatus;

add_setup(async () => {
  ({ FormAutofillStatus } = ChromeUtils.importESModule(
    "resource://autofill/FormAutofillParent.sys.mjs"
  ));
});

add_task(async function test_profileSavedFieldNames_init() {
  FormAutofillStatus.init();
  sinon.stub(FormAutofillStatus, "updateSavedFieldNames");

  await FormAutofillStatus.formAutofillStorage.initialize();
  Assert.equal(FormAutofillStatus.updateSavedFieldNames.called, true);

  FormAutofillStatus.uninit();
});

add_task(async function test_profileSavedFieldNames_observe() {
  FormAutofillStatus.init();

  // profile changed => Need to trigger updateValidFields
  ["add", "update", "remove", "reconcile", "removeAll"].forEach(event => {
    FormAutofillStatus.observe(null, "formautofill-storage-changed", event);
    Assert.equal(FormAutofillStatus.updateSavedFieldNames.called, true);
  });

  // profile metadata updated => no need to trigger updateValidFields
  FormAutofillStatus.updateSavedFieldNames.resetHistory();
  FormAutofillStatus.observe(
    null,
    "formautofill-storage-changed",
    "notifyUsed"
  );
  Assert.equal(FormAutofillStatus.updateSavedFieldNames.called, false);
  FormAutofillStatus.updateSavedFieldNames.restore();
});

add_task(async function test_profileSavedFieldNames_update() {
  registerCleanupFunction(function cleanup() {
    Services.prefs.clearUserPref("extensions.formautofill.addresses.enabled");
  });

  Object.defineProperty(
    FormAutofillStatus.formAutofillStorage.addresses,
    "_data",
    { writable: true }
  );

  FormAutofillStatus.formAutofillStorage.addresses._data = [];

  // The set is empty if there's no profile in the store.
  await FormAutofillStatus.updateSavedFieldNames();
  Assert.equal(
    Services.ppmm.sharedData.get("FormAutofill:savedFieldNames").size,
    0
  );

  // 2 profiles with 4 valid fields.
  FormAutofillStatus.formAutofillStorage.addresses._data = [
    {
      guid: "test-guid-1",
      organization: "Sesame Street",
      "street-address": "123 Sesame Street.",
      tel: "1-345-345-3456",
      email: "",
      timeCreated: 0,
      timeLastUsed: 0,
      timeLastModified: 0,
      timesUsed: 0,
    },
    {
      guid: "test-guid-2",
      organization: "Mozilla",
      "street-address": "331 E. Evelyn Avenue",
      tel: "1-650-903-0800",
      country: "US",
      timeCreated: 0,
      timeLastUsed: 0,
      timeLastModified: 0,
      timesUsed: 0,
    },
  ];

  await FormAutofillStatus.updateSavedFieldNames();

  let autofillSavedFieldNames = Services.ppmm.sharedData.get(
    "FormAutofill:savedFieldNames"
  );
  Assert.equal(autofillSavedFieldNames.size, 4);
  Assert.equal(autofillSavedFieldNames.has("organization"), true);
  Assert.equal(autofillSavedFieldNames.has("street-address"), true);
  Assert.equal(autofillSavedFieldNames.has("tel"), true);
  Assert.equal(autofillSavedFieldNames.has("email"), false);
  Assert.equal(autofillSavedFieldNames.has("guid"), false);
  Assert.equal(autofillSavedFieldNames.has("timeCreated"), false);
  Assert.equal(autofillSavedFieldNames.has("timeLastUsed"), false);
  Assert.equal(autofillSavedFieldNames.has("timeLastModified"), false);
  Assert.equal(autofillSavedFieldNames.has("timesUsed"), false);
});