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

// This test covers a race condition of input events followed by Enter.
// The test is putting the event bufferer in a situation where a new query has
// already results in the context object, but onQueryResults has not been
// invoked yet. The EventBufferer should wait for onQueryResults to proceed,
// otherwise the view cannot yet contain the updated query string and we may
// end up searching for a partial string.

add_setup(async function () {
  sandbox = sinon.createSandbox();
  await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + "searchSuggestionEngine.xml",
    setAsDefault: true,
  });
  // To reproduce the race condition it's important to disable any provider
  // having `deferUserSelection` == true;
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.suggest.engines", false]],
  });
  await PlacesUtils.history.clear();
  registerCleanupFunction(async () => {
    await PlacesUtils.history.clear();
    sandbox.restore();
  });
});

add_task(async function test() {
  let tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "about:robots",
  });

  let defer = PromiseUtils.defer();
  let waitFirstSearchResults = PromiseUtils.defer();
  let count = 0;
  let original = gURLBar.controller.notify;
  sandbox.stub(gURLBar.controller, "notify").callsFake(async (msg, context) => {
    if (context?.deferUserSelectionProviders.size) {
      Assert.ok(false, "Any provider deferring selection should be disabled");
    }
    if (msg == "onQueryResults") {
      waitFirstSearchResults.resolve();
      count++;
    }
    // Delay any events after the second onQueryResults call.
    if (count >= 2) {
      await defer.promise;
    }
    return original.call(gURLBar.controller, msg, context);
  });

  gURLBar.focus();
  gURLBar.select();
  EventUtils.synthesizeKey("t", {});
  await waitFirstSearchResults.promise;
  EventUtils.synthesizeKey("e", {});

  let promiseLoaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  EventUtils.synthesizeKey("KEY_Enter", {});

  let context = await UrlbarTestUtils.promiseSearchComplete(window);
  await TestUtils.waitForCondition(
    () => context.results.length,
    "Waiting for any result in the QueryContext"
  );
  info("Simulate a request to replay deferred events at this point");
  gURLBar.eventBufferer.replayDeferredEvents(true);

  defer.resolve();
  await promiseLoaded;

  let expectedURL = UrlbarPrefs.isPersistedSearchTermsEnabled()
    ? "http://mochi.test:8888/?terms=" + gURLBar.value
    : gURLBar.untrimmedValue;
  Assert.equal(gBrowser.selectedBrowser.currentURI.spec, expectedURL);

  BrowserTestUtils.removeTab(tab);
  sandbox.restore();
});