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

/**
 * These tests test the UrlbarController in association with the model.
 */

"use strict";

const TEST_URL = "http://example.com";
const match = new UrlbarResult(
  UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
  UrlbarUtils.RESULT_SOURCE.TABS,
  { url: TEST_URL }
);
let controller;

add_setup(async function () {
  controller = UrlbarTestUtils.newMockController();
});

add_task(async function test_basic_search() {
  let provider = registerBasicTestProvider([match]);
  const context = createContext(TEST_URL, { providers: [provider.name] });

  let startedPromise = promiseControllerNotification(
    controller,
    "onQueryStarted"
  );
  let resultsPromise = promiseControllerNotification(
    controller,
    "onQueryResults"
  );

  controller.startQuery(context);

  let params = await startedPromise;

  Assert.equal(params[0], context);

  params = await resultsPromise;

  Assert.deepEqual(
    params[0].results,
    [match],
    "Should have the expected match"
  );
});

add_task(async function test_cancel_search() {
  let providerCanceledDeferred = Promise.withResolvers();
  let provider = registerBasicTestProvider(
    [match],
    providerCanceledDeferred.resolve
  );
  const context = createContext(TEST_URL, { providers: [provider.name] });

  let startedPromise = promiseControllerNotification(
    controller,
    "onQueryStarted"
  );
  let cancelPromise = promiseControllerNotification(
    controller,
    "onQueryCancelled"
  );

  let delayResultsPromise = new Promise(resolve => {
    controller.addQueryListener({
      async onQueryResults(queryContext) {
        controller.removeQueryListener(this);
        controller.cancelQuery(queryContext);
        resolve();
      },
    });
  });

  let result = new UrlbarResult(
    UrlbarUtils.RESULT_TYPE.URL,
    UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
    { url: "https://example.com/1", title: "example" }
  );

  // We are awaiting for asynchronous work on initialization.
  // For this test, we need the query objects to be created. We ensure this by
  // using a delayed Provider. We wait for onQueryResults, then cancel the
  // query. By that time the query objects are created. Then we unblock the
  // delayed provider.
  let delayedProvider = new UrlbarTestUtils.TestProvider({
    delayResultsPromise,
    results: [result],
    type: UrlbarUtils.PROVIDER_TYPE.PROFILE,
  });

  UrlbarProvidersManager.registerProvider(delayedProvider);

  controller.startQuery(context);

  let params = await startedPromise;
  Assert.equal(params[0], context);

  info("Should have notified the provider the query is canceled");
  await providerCanceledDeferred.promise;

  params = await cancelPromise;
  UrlbarProvidersManager.unregisterProvider(delayedProvider);
});