summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_UrlbarInput_searchTerms_searchBar.js
blob: 784d8932acc2b138caf6fe68505b841eb3f157c1 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// These tests check the behavior of the Urlbar when a user enables
// the search bar and showSearchTerms is true.

const { CustomizableUITestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/CustomizableUITestUtils.sys.mjs"
);

const gCUITestUtils = new CustomizableUITestUtils(window);
const SEARCH_STRING = "example_string";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.search.widget.inNavBar", true],
      ["browser.urlbar.showSearchTerms.featureGate", true],
    ],
  });

  await SearchTestUtils.installSearchExtension(
    {
      name: "MozSearch",
      search_url: "https://www.example.com/",
      search_url_get_params: "q={searchTerms}&pc=fake_code",
    },
    { setAsDefault: true }
  );

  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
    gCUITestUtils.removeSearchBar();
  });
});

function assertSearchStringIsNotInUrlbar(searchString) {
  Assert.notEqual(
    gURLBar.value,
    searchString,
    `Search string ${searchString} should not be in the url bar.`
  );
  Assert.equal(
    gURLBar.getAttribute("pageproxystate"),
    "valid",
    "Pageproxystate should be valid."
  );
  Assert.equal(
    gBrowser.selectedBrowser.searchTerms,
    "",
    "searchTerms should be blank."
  );
}

// When a user enables the search bar, and does a search in the search bar,
// the search term should not show in the URL bar.
add_task(async function search_bar_on() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  await gCUITestUtils.addSearchBar();

  let browserLoadedPromise = BrowserTestUtils.browserLoaded(
    tab.linkedBrowser,
    false,
    `https://www.example.com/?q=${SEARCH_STRING}&pc=fake_code`
  );

  let searchBar = BrowserSearch.searchBar;
  searchBar.value = SEARCH_STRING;
  searchBar.focus();
  EventUtils.synthesizeKey("KEY_Enter");

  await browserLoadedPromise;
  assertSearchStringIsNotInUrlbar(SEARCH_STRING);

  BrowserTestUtils.removeTab(tab);
});

// When a user enables the search bar, and does a search in the URL bar,
// the search term should still not show in the URL bar.
add_task(async function search_bar_on_with_url_bar_search() {
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  await gCUITestUtils.addSearchBar();

  let browserLoadedPromise = BrowserTestUtils.browserLoaded(
    tab.linkedBrowser,
    false,
    `https://www.example.com/?q=${SEARCH_STRING}&pc=fake_code`
  );

  gURLBar.focus();
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    waitForFocus,
    value: SEARCH_STRING,
    fireInputEvent: true,
  });
  EventUtils.synthesizeKey("KEY_Enter");

  await browserLoadedPromise;
  assertSearchStringIsNotInUrlbar(SEARCH_STRING);

  BrowserTestUtils.removeTab(tab);
});