summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/engagementTelemetry/browser/head-interaction.js
blob: 58c55b416fc51acc89e539da37d869e383816c52 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* import-globals-from head.js */

ChromeUtils.defineESModuleGetters(this, {
  CUSTOM_SEARCH_SHORTCUTS:
    "resource://activity-stream/lib/SearchShortcuts.sys.mjs",
  NewTabUtils: "resource://gre/modules/NewTabUtils.sys.mjs",
  SEARCH_SHORTCUTS: "resource://activity-stream/lib/SearchShortcuts.sys.mjs",
  SearchService: "resource://gre/modules/SearchService.sys.mjs",
});

async function doTopsitesTest({ trigger, assert }) {
  await doTest(async () => {
    await addTopSites("https://example.com/");

    await showResultByArrowDown();
    await selectRowByURL("https://example.com/");

    await trigger();
    await assert();
  });
}

async function doTopsitesSearchTest({ trigger, assert }) {
  await doTest(async browser => {
    let extension = await SearchTestUtils.installSearchExtension(
      {
        name: "MozSearch",
        keyword: "@test",
        search_url: "https://example.com/",
        search_url_get_params: "q={searchTerms}",
      },
      { skipUnload: true }
    );

    // Fresh profiles come with an empty set of pinned websites (pref doesn't
    // exist). Search shortcut topsites make this test more complicated because
    // the feature pins a new website on startup. Behaviour can vary when running
    // with --verify so it's more predictable to clear pins entirely.
    Services.prefs.clearUserPref("browser.newtabpage.pinned");
    NewTabUtils.pinnedLinks.resetCache();

    let entry = {
      keyword: "@test",
      shortURL: "example",
      url: "https://example.com/",
    };

    // The array is used to identify sites that should be converted to
    // a Top Site.
    let searchShortcuts = JSON.parse(JSON.stringify(SEARCH_SHORTCUTS));
    SEARCH_SHORTCUTS.push(entry);

    // TopSitesFeed takes a list of app provided engines and determine if the
    // engine containing an alias that matches a keyword inside of this array.
    // If so, the list of search shortcuts in the store will be updated.
    let customSearchShortcuts = JSON.parse(
      JSON.stringify(CUSTOM_SEARCH_SHORTCUTS)
    );
    CUSTOM_SEARCH_SHORTCUTS.push(entry);

    // TopSitesFeed only allows app provided engines to be included as
    // search shortcuts.
    // eslint-disable-next-line mozilla/valid-lazy
    let sandbox = lazy.sinon.createSandbox();
    sandbox
      .stub(SearchService.prototype, "getAppProvidedEngines")
      .resolves([{ aliases: ["@test"] }]);

    let siteToPin = {
      url: "https://example.com",
      label: "@test",
      searchTopSite: true,
    };
    NewTabUtils.pinnedLinks.pin(siteToPin, 0);

    await updateTopSites(sites => {
      return sites && sites[0] && sites[0].url == "https://example.com";
    }, true);

    BrowserTestUtils.startLoadingURIString(browser, "about:newtab");
    await BrowserTestUtils.browserStopped(browser, "about:newtab");

    await BrowserTestUtils.synthesizeMouseAtCenter(
      ".search-shortcut .top-site-button",
      {},
      gBrowser.selectedBrowser
    );

    EventUtils.synthesizeKey("x");
    await UrlbarTestUtils.promiseSearchComplete(window);

    await trigger();
    await assert();

    // Clean up.
    NewTabUtils.pinnedLinks.unpin(siteToPin);
    SEARCH_SHORTCUTS.pop();
    CUSTOM_SEARCH_SHORTCUTS.pop();
    // Sanity check to ensure we're leaving the shortcuts in their default state.
    Assert.deepEqual(
      searchShortcuts,
      SEARCH_SHORTCUTS,
      "SEARCH_SHORTCUTS values"
    );
    Assert.deepEqual(
      customSearchShortcuts,
      CUSTOM_SEARCH_SHORTCUTS,
      "CUSTOM_SEARCH_SHORTCUTS values"
    );
    sandbox.restore();
    Services.prefs.clearUserPref("browser.newtabpage.pinned");
    NewTabUtils.pinnedLinks.resetCache();
    await extension.unload();
  });
}

async function doTypedTest({ trigger, assert }) {
  await doTest(async () => {
    await openPopup("x");

    await trigger();
    await assert();
  });
}

async function doTypedWithResultsPopupTest({ trigger, assert }) {
  await doTest(async () => {
    await showResultByArrowDown();
    EventUtils.synthesizeKey("x");
    await UrlbarTestUtils.promiseSearchComplete(window);

    await trigger();
    await assert();
  });
}

async function doPastedTest({ trigger, assert }) {
  await doTest(async () => {
    await doPaste("www.example.com");

    await trigger();
    await assert();
  });
}

async function doPastedWithResultsPopupTest({ trigger, assert }) {
  await doTest(async () => {
    await showResultByArrowDown();
    await doPaste("x");

    await trigger();
    await assert();
  });
}

async function doReturnedRestartedRefinedTest({ trigger, assert }) {
  const testData = [
    {
      firstInput: "x",
      // Just move the focus to the URL bar after blur.
      secondInput: null,
      expected: "returned",
    },
    {
      firstInput: "x",
      secondInput: "x",
      expected: "returned",
    },
    {
      firstInput: "x",
      secondInput: "y",
      expected: "restarted",
    },
    {
      firstInput: "x",
      secondInput: "x y",
      expected: "refined",
    },
    {
      firstInput: "x y",
      secondInput: "x",
      expected: "refined",
    },
  ];

  for (const { firstInput, secondInput, expected } of testData) {
    await doTest(async () => {
      await openPopup(firstInput);
      await doBlur();

      await UrlbarTestUtils.promisePopupOpen(window, () => {
        document.getElementById("Browser:OpenLocation").doCommand();
      });
      if (secondInput) {
        for (let i = 0; i < secondInput.length; i++) {
          EventUtils.synthesizeKey(secondInput.charAt(i));
        }
      }
      await UrlbarTestUtils.promiseSearchComplete(window);

      await trigger();
      await assert(expected);
    });
  }
}

async function doPersistedSearchTermsTest({ trigger, assert }) {
  await doTest(async () => {
    await openPopup("x");
    await doEnter();

    await openPopup("x");

    await trigger();
    await assert();
  });
}

async function doPersistedSearchTermsRestartedRefinedTest({
  enabled,
  trigger,
  assert,
}) {
  const testData = [
    {
      firstInput: "x",
      // Just move the focus to the URL bar after engagement.
      secondInput: null,
      expected: enabled ? "persisted_search_terms" : "topsites",
    },
    {
      firstInput: "x",
      secondInput: "x",
      expected: enabled ? "persisted_search_terms" : "typed",
    },
    {
      firstInput: "x",
      secondInput: "y",
      expected: enabled ? "persisted_search_terms_restarted" : "typed",
    },
    {
      firstInput: "x",
      secondInput: "x y",
      expected: enabled ? "persisted_search_terms_refined" : "typed",
    },
    {
      firstInput: "x y",
      secondInput: "x",
      expected: enabled ? "persisted_search_terms_refined" : "typed",
    },
  ];

  for (const { firstInput, secondInput, expected } of testData) {
    await doTest(async () => {
      await openPopup(firstInput);
      await doEnter();

      await UrlbarTestUtils.promisePopupOpen(window, () => {
        EventUtils.synthesizeKey("l", { accelKey: true });
      });
      if (secondInput) {
        for (let i = 0; i < secondInput.length; i++) {
          EventUtils.synthesizeKey(secondInput.charAt(i));
        }
      }
      await UrlbarTestUtils.promiseSearchComplete(window);

      await trigger();
      await assert(expected);
    });
  }
}

async function doPersistedSearchTermsRestartedRefinedViaAbandonmentTest({
  enabled,
  trigger,
  assert,
}) {
  const testData = [
    {
      firstInput: "x",
      // Just move the focus to the URL bar after blur.
      secondInput: null,
      expected: enabled ? "persisted_search_terms" : "returned",
    },
    {
      firstInput: "x",
      secondInput: "x",
      expected: enabled ? "persisted_search_terms" : "returned",
    },
    {
      firstInput: "x",
      secondInput: "y",
      expected: enabled ? "persisted_search_terms_restarted" : "restarted",
    },
    {
      firstInput: "x",
      secondInput: "x y",
      expected: enabled ? "persisted_search_terms_refined" : "refined",
    },
    {
      firstInput: "x y",
      secondInput: "x",
      expected: enabled ? "persisted_search_terms_refined" : "refined",
    },
  ];

  for (const { firstInput, secondInput, expected } of testData) {
    await doTest(async () => {
      await openPopup("any search");
      await doEnter();

      await openPopup(firstInput);
      await doBlur();

      await UrlbarTestUtils.promisePopupOpen(window, () => {
        EventUtils.synthesizeKey("l", { accelKey: true });
      });
      if (secondInput) {
        for (let i = 0; i < secondInput.length; i++) {
          EventUtils.synthesizeKey(secondInput.charAt(i));
        }
      }
      await UrlbarTestUtils.promiseSearchComplete(window);

      await trigger();
      await assert(expected);
    });
  }
}