summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_activeStatus.js
blob: 47d79b02e5ceda22d6a7f38832350aa699bbbda7 (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
/*
 * Test for status handling in Form Autofill Parent.
 */

"use strict";

let FormAutofillStatus;

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

add_task(async function test_activeStatus_init() {
  sinon.spy(FormAutofillStatus, "updateStatus");

  // Default status is null before initialization
  Assert.equal(FormAutofillStatus._active, null);
  Assert.equal(Services.ppmm.sharedData.get("FormAutofill:enabled"), undefined);

  FormAutofillStatus.init();
  // init shouldn't call updateStatus since that requires storage which will
  // lead to startup time regressions.
  Assert.equal(FormAutofillStatus.updateStatus.called, false);
  Assert.equal(Services.ppmm.sharedData.get("FormAutofill:enabled"), undefined);

  // Initialize profile storage
  await FormAutofillStatus.formAutofillStorage.initialize();
  await FormAutofillStatus.updateSavedFieldNames();
  // Upon first initializing profile storage, status should be computed.
  Assert.equal(FormAutofillStatus.updateStatus.called, true);
  Assert.equal(Services.ppmm.sharedData.get("FormAutofill:enabled"), false);

  FormAutofillStatus.uninit();
});

add_task(async function test_activeStatus_observe() {
  FormAutofillStatus.init();
  sinon.stub(FormAutofillStatus, "computeStatus");
  sinon.spy(FormAutofillStatus, "onStatusChanged");

  // _active = _computeStatus() => No need to trigger _onStatusChanged
  FormAutofillStatus._active = true;
  FormAutofillStatus.computeStatus.returns(true);
  FormAutofillStatus.observe(
    null,
    "nsPref:changed",
    "extensions.formautofill.addresses.enabled"
  );
  FormAutofillStatus.observe(
    null,
    "nsPref:changed",
    "extensions.formautofill.creditCards.enabled"
  );
  Assert.equal(FormAutofillStatus.onStatusChanged.called, false);

  // _active != computeStatus() => Need to trigger onStatusChanged
  FormAutofillStatus.computeStatus.returns(false);
  FormAutofillStatus.onStatusChanged.resetHistory();
  FormAutofillStatus.observe(
    null,
    "nsPref:changed",
    "extensions.formautofill.addresses.enabled"
  );
  FormAutofillStatus.observe(
    null,
    "nsPref:changed",
    "extensions.formautofill.creditCards.enabled"
  );
  Assert.equal(FormAutofillStatus.onStatusChanged.called, true);

  // profile changed => Need to trigger _onStatusChanged
  await Promise.all(
    ["add", "update", "remove", "reconcile"].map(async event => {
      FormAutofillStatus.computeStatus.returns(!FormAutofillStatus._active);
      FormAutofillStatus.onStatusChanged.resetHistory();
      await FormAutofillStatus.observe(
        null,
        "formautofill-storage-changed",
        event
      );
      Assert.equal(FormAutofillStatus.onStatusChanged.called, true);
    })
  );

  // profile metadata updated => No need to trigger onStatusChanged
  FormAutofillStatus.computeStatus.returns(!FormAutofillStatus._active);
  FormAutofillStatus.onStatusChanged.resetHistory();
  await FormAutofillStatus.observe(
    null,
    "formautofill-storage-changed",
    "notifyUsed"
  );
  Assert.equal(FormAutofillStatus.onStatusChanged.called, false);

  FormAutofillStatus.computeStatus.restore();
});

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

  sinon.stub(
    FormAutofillStatus.formAutofillStorage.addresses,
    "getSavedFieldNames"
  );
  FormAutofillStatus.formAutofillStorage.addresses.getSavedFieldNames.returns(
    Promise.resolve(new Set())
  );
  sinon.stub(
    FormAutofillStatus.formAutofillStorage.creditCards,
    "getSavedFieldNames"
  );
  FormAutofillStatus.formAutofillStorage.creditCards.getSavedFieldNames.returns(
    Promise.resolve(new Set())
  );

  // pref is enabled and profile is empty.
  Services.prefs.setBoolPref("extensions.formautofill.addresses.enabled", true);
  Services.prefs.setBoolPref(
    "extensions.formautofill.creditCards.enabled",
    true
  );
  Assert.equal(FormAutofillStatus.computeStatus(), false);

  // pref is disabled and profile is empty.
  Services.prefs.setBoolPref(
    "extensions.formautofill.addresses.enabled",
    false
  );
  Services.prefs.setBoolPref(
    "extensions.formautofill.creditCards.enabled",
    false
  );
  Assert.equal(FormAutofillStatus.computeStatus(), false);

  FormAutofillStatus.formAutofillStorage.addresses.getSavedFieldNames.returns(
    Promise.resolve(new Set(["given-name"]))
  );
  await FormAutofillStatus.observe(null, "formautofill-storage-changed", "add");

  // pref is enabled and profile is not empty.
  Services.prefs.setBoolPref("extensions.formautofill.addresses.enabled", true);
  Assert.equal(FormAutofillStatus.computeStatus(), true);

  // pref is partial enabled and profile is not empty.
  Services.prefs.setBoolPref("extensions.formautofill.addresses.enabled", true);
  Services.prefs.setBoolPref(
    "extensions.formautofill.creditCards.enabled",
    false
  );
  Assert.equal(FormAutofillStatus.computeStatus(), true);
  Services.prefs.setBoolPref(
    "extensions.formautofill.addresses.enabled",
    false
  );
  Services.prefs.setBoolPref(
    "extensions.formautofill.creditCards.enabled",
    true
  );
  Assert.equal(FormAutofillStatus.computeStatus(), true);

  // pref is disabled and profile is not empty.
  Services.prefs.setBoolPref(
    "extensions.formautofill.addresses.enabled",
    false
  );
  Services.prefs.setBoolPref(
    "extensions.formautofill.creditCards.enabled",
    false
  );
  Assert.equal(FormAutofillStatus.computeStatus(), false);
});