summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_autocomplete_import.js
blob: 0be137d88deaf577844d8db08d049729b596edaa (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
const { ChromeMigrationUtils } = ChromeUtils.importESModule(
  "resource:///modules/ChromeMigrationUtils.sys.mjs"
);
const { ExperimentAPI } = ChromeUtils.importESModule(
  "resource://nimbus/ExperimentAPI.sys.mjs"
);
const { ExperimentFakes } = ChromeUtils.importESModule(
  "resource://testing-common/NimbusTestUtils.sys.mjs"
);
const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);

// Dummy migrator to change and detect importable behavior.
const gTestMigrator = {
  profiles: [],

  getSourceProfiles() {
    return this.profiles;
  },

  migrate: sinon
    .stub()
    .callsFake(() =>
      LoginTestUtils.addLogin({ username: "import", password: "pass" })
    ),
};

// Showing importables updates counts delayed, so adjust and cleanup.
add_setup(async function setup() {
  const debounce = sinon
    .stub(LoginManagerParent, "SUGGEST_IMPORT_DEBOUNCE_MS")
    .value(0);
  const importable = sinon
    .stub(ChromeMigrationUtils, "getImportableLogins")
    .resolves(["chrome"]);
  const migrator = sinon
    .stub(MigrationUtils, "getMigrator")
    .resolves(gTestMigrator);

  const doExperimentCleanup = await ExperimentFakes.enrollWithFeatureConfig({
    featureId: "password-autocomplete",
    value: { directMigrateSingleProfile: true },
  });

  // This makes the last autocomplete test *not* show import suggestions.
  Services.prefs.setIntPref("signon.suggestImportCount", 3);

  registerCleanupFunction(async () => {
    await doExperimentCleanup();
    debounce.restore();
    importable.restore();
    migrator.restore();
    Services.prefs.clearUserPref("signon.suggestImportCount");
  });
});

add_task(async function check_fluent_ids() {
  await document.l10n.ready;
  MozXULElement.insertFTLIfNeeded("toolkit/main-window/autocomplete.ftl");

  const host = "testhost.com";
  for (const browser of ChromeMigrationUtils.CONTEXTUAL_LOGIN_IMPORT_BROWSERS) {
    const id = `autocomplete-import-logins-${browser}`;
    const message = await document.l10n.formatValue(id, { host });
    Assert.ok(
      message.includes(`data-l10n-name="line1"`),
      `${id} included line1`
    );
    Assert.ok(
      message.includes(`data-l10n-name="line2"`),
      `${id} included line2`
    );
    Assert.ok(message.includes(host), `${id} replaced host`);
  }
});

/**
 * Tests that if the user selects the password import suggestion from
 * the autocomplete popup, and there is more than one profile available
 * to import from, that the migration wizard opens to guide the user
 * through importing those logins.
 */
add_task(async function import_suggestion_wizard() {
  let wizard;

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "https://example.com" + DIRECTORY_PATH + "form_basic.html",
    },
    async function (browser) {
      const popup = document.getElementById("PopupAutoComplete");
      Assert.ok(popup, "Got popup");
      await openACPopup(popup, browser, "#form-basic-username");

      const importableItem = popup.querySelector(
        `[originaltype="importableLogins"]`
      );
      Assert.ok(importableItem, "Got importable suggestion richlistitem");

      await BrowserTestUtils.waitForCondition(
        () => !importableItem.collapsed,
        "Wait for importable suggestion to show"
      );

      // Pretend there's 2+ profiles to trigger the wizard.
      gTestMigrator.profiles.length = 2;

      info("Clicking on importable suggestion");
      const wizardPromise = BrowserTestUtils.waitForMigrationWizard(window);

      // The modal window blocks execution, so avoid calling directly.
      executeSoon(() => EventUtils.synthesizeMouseAtCenter(importableItem, {}));

      wizard = await wizardPromise;
      Assert.ok(wizard, "Wizard opened");
      Assert.equal(
        gTestMigrator.migrate.callCount,
        0,
        "Direct migrate not used"
      );

      await closePopup(popup);
    }
  );

  // Close the wizard in the end of the test. If we close the wizard when the tab
  // is still opened, the username field will be focused again, which triggers another
  // importable suggestion.
  await BrowserTestUtils.closeMigrationWizard(wizard);
});

add_task(async function import_suggestion_learn_more() {
  let supportTab;
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "https://example.com" + DIRECTORY_PATH + "form_basic.html",
    },
    async function (browser) {
      const popup = document.getElementById("PopupAutoComplete");
      Assert.ok(popup, "Got popup");
      await openACPopup(popup, browser, "#form-basic-username");

      const learnMoreItem = popup.querySelector(`[type="importableLearnMore"]`);
      Assert.ok(learnMoreItem, "Got importable learn more richlistitem");

      await BrowserTestUtils.waitForCondition(
        () => !learnMoreItem.collapsed,
        "Wait for importable learn more to show"
      );

      info("Clicking on importable learn more");
      const supportTabPromise = BrowserTestUtils.waitForNewTab(
        gBrowser,
        Services.urlFormatter.formatURLPref("app.support.baseURL") +
          "password-import"
      );
      EventUtils.synthesizeMouseAtCenter(learnMoreItem, {});
      supportTab = await supportTabPromise;
      Assert.ok(supportTab, "Support tab opened");

      await closePopup(popup);
    }
  );

  // Close the tab in the end of the test to avoid the username field being
  // focused again.
  await BrowserTestUtils.removeTab(supportTab);
});

add_task(async function import_suggestion_migrate() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "https://example.com" + DIRECTORY_PATH + "form_basic.html",
    },
    async function (browser) {
      const popup = document.getElementById("PopupAutoComplete");
      Assert.ok(popup, "Got popup");
      await openACPopup(popup, browser, "#form-basic-username");

      const importableItem = popup.querySelector(
        `[originaltype="importableLogins"]`
      );
      Assert.ok(importableItem, "Got importable suggestion richlistitem");

      await BrowserTestUtils.waitForCondition(
        () => !importableItem.collapsed,
        "Wait for importable suggestion to show"
      );

      // Pretend there's 1 profile to trigger migrate.
      gTestMigrator.profiles.length = 1;

      info("Clicking on importable suggestion");
      const migratePromise = BrowserTestUtils.waitForCondition(
        () => gTestMigrator.migrate.callCount,
        "Wait for direct migration attempt"
      );
      EventUtils.synthesizeMouseAtCenter(importableItem, {});

      const callCount = await migratePromise;
      Assert.equal(callCount, 1, "Direct migrate used once");

      const importedItem = await BrowserTestUtils.waitForCondition(
        () => popup.querySelector(`[originaltype="loginWithOrigin"]`),
        "Wait for imported login to show"
      );
      EventUtils.synthesizeMouseAtCenter(importedItem, {});

      const username = await SpecialPowers.spawn(
        browser,
        [],
        () => content.document.getElementById("form-basic-username").value
      );
      Assert.equal(username, "import", "username from import filled in");

      LoginTestUtils.clearData();
    }
  );
});

add_task(async function import_suggestion_not_shown() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: "https://example.com" + DIRECTORY_PATH + "form_basic.html",
    },
    async function (browser) {
      const popup = document.getElementById("PopupAutoComplete");
      Assert.ok(popup, "Got popup");
      let opened = false;
      openACPopup(popup, browser, "#form-basic-password").then(
        () => (opened = true)
      );

      await TestUtils.waitForCondition(() => {
        EventUtils.synthesizeKey("KEY_ArrowDown");
        return opened;
      });

      const footer = popup.querySelector(`[originaltype="loginsFooter"]`);
      Assert.ok(footer, "Got footer richlistitem");

      await TestUtils.waitForCondition(() => {
        return !EventUtils.isHidden(footer);
      }, "Waiting for footer to become visible");

      Assert.ok(
        !popup.querySelector(`[originaltype="importableLogins"]`),
        "No importable suggestion shown"
      );

      await closePopup(popup);
    }
  );
});