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

/**
 * Tests that a search result has the correct attributes and visits the
 * expected URL for the engine.
 */

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.search.separatePrivateDefault.ui.enabled", true],
      ["browser.search.separatePrivateDefault", false],
    ],
  });

  await SearchTestUtils.installSearchExtension(
    { name: "MozSearch" },
    { setAsDefault: true }
  );
  await SearchTestUtils.installSearchExtension({
    name: "MozSearchPrivate",
    search_url: "https://example.com/private",
  });

  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
    await UrlbarTestUtils.formHistory.clear();
  });
});

async function testSearch(win, expectedName, expectedBaseUrl) {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window: win,
    value: "open a search",
  });
  let result = await UrlbarTestUtils.getDetailsOfResultAt(win, 0);

  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.SEARCH,
    "Should have type search"
  );
  Assert.deepEqual(
    result.searchParams,
    {
      engine: expectedName,
      keyword: undefined,
      query: "open a search",
      suggestion: undefined,
      inPrivateWindow: undefined,
      isPrivateEngine: undefined,
    },
    "Should have the correct result parameters."
  );

  Assert.equal(
    result.image,
    UrlbarUtils.ICON.SEARCH_GLASS,
    "Should have the search icon image"
  );

  let tabPromise = BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
  let element = await UrlbarTestUtils.waitForAutocompleteResultAt(win, 0);
  EventUtils.synthesizeMouseAtCenter(element, {}, win);
  await tabPromise;

  Assert.equal(
    win.gBrowser.selectedBrowser.currentURI.spec,
    expectedBaseUrl + "?q=open+a+search",
    "Should have loaded the correct page"
  );
}

add_task(async function test_search_normal_window() {
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:mozilla"
  );

  registerCleanupFunction(async function () {
    try {
      BrowserTestUtils.removeTab(tab);
    } catch (ex) {
      /* tab may have already been closed in case of failure */
    }
  });

  await testSearch(window, "MozSearch", "https://example.com/");
});

add_task(async function test_search_private_window_no_separate_default() {
  const win = await BrowserTestUtils.openNewBrowserWindow({ private: true });

  registerCleanupFunction(async function () {
    await BrowserTestUtils.closeWindow(win);
  });

  await testSearch(win, "MozSearch", "https://example.com/");
});

add_task(async function test_search_private_window() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.search.separatePrivateDefault", true]],
  });

  let engine = Services.search.getEngineByName("MozSearchPrivate");
  let originalEngine = await Services.search.getDefaultPrivate();
  await Services.search.setDefaultPrivate(
    engine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  registerCleanupFunction(async () => {
    await BrowserTestUtils.closeWindow(win);
    await Services.search.setDefaultPrivate(
      originalEngine,
      Ci.nsISearchService.CHANGE_REASON_UNKNOWN
    );
  });

  const win = await BrowserTestUtils.openNewBrowserWindow({ private: true });

  await testSearch(win, "MozSearchPrivate", "https://example.com/private");
});