summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser/browser_contentSearchUI_default.js
blob: c69326262a7ad7a12e085e90823f4796e5df3ce1 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const TEST_ENGINE_NAME = "searchSuggestionEngine";
const HANDOFF_PREF =
  "browser.newtabpage.activity-stream.improvesearch.handoffToAwesomebar";

let extension;
let defaultEngine;
let addedEngine;

add_setup(async function () {
  // Disable window occlusion. Bug 1733955
  if (navigator.platform.indexOf("Win") == 0) {
    await SpecialPowers.pushPrefEnv({
      set: [["widget.windows.window_occlusion_tracking.enabled", false]],
    });
  }

  defaultEngine = await Services.search.getDefault();

  extension = await SearchTestUtils.installSearchExtension({
    id: TEST_ENGINE_NAME,
    name: TEST_ENGINE_NAME,
    suggest_url:
      "https://example.com/browser/browser/components/search/test/browser/searchSuggestionEngine.sjs",
    suggest_url_get_params: "query={searchTerms}",
  });

  addedEngine = await Services.search.getEngineByName(TEST_ENGINE_NAME);

  // Enable suggestions in this test. Otherwise, the string in the content
  // search box changes.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.suggest.searches", true]],
  });

  registerCleanupFunction(async () => {
    await Services.search.setDefault(
      defaultEngine,
      Ci.nsISearchService.CHANGE_REASON_UNKNOWN
    );
  });
});

async function ensureIcon(tab, expectedIcon) {
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [expectedIcon],
    async function (icon) {
      await ContentTaskUtils.waitForCondition(() => !content.document.hidden);

      let computedStyle = content.window.getComputedStyle(
        content.document.body
      );
      await ContentTaskUtils.waitForCondition(
        () => computedStyle.getPropertyValue("--newtab-search-icon") != "null",
        "Search Icon not set."
      );

      Assert.equal(
        computedStyle.getPropertyValue("--newtab-search-icon"),
        `url(${icon})`,
        "Should have the expected icon"
      );
    }
  );
}

async function ensurePlaceholder(tab, expectedId, expectedEngine) {
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [expectedId, expectedEngine],
    async function (id, engine) {
      await ContentTaskUtils.waitForCondition(() => !content.document.hidden);

      await ContentTaskUtils.waitForCondition(
        () => content.document.querySelector(".search-handoff-button"),
        "l10n ID not set."
      );
      let buttonNode = content.document.querySelector(".search-handoff-button");
      let expectedAttributes = { id, args: engine ? { engine } : null };
      Assert.deepEqual(
        content.document.l10n.getAttributes(buttonNode),
        expectedAttributes,
        "Expected updated l10n ID and args."
      );
    }
  );
}

async function runNewTabTest(isHandoff) {
  let tab = await BrowserTestUtils.openNewForegroundTab({
    url: "about:newtab",
    gBrowser,
    waitForLoad: false,
  });

  let engineIcon = defaultEngine.getIconURLBySize(16, 16);

  await ensureIcon(tab, engineIcon);
  if (isHandoff) {
    await ensurePlaceholder(
      tab,
      "newtab-search-box-handoff-input",
      Services.search.defaultEngine.name
    );
  }

  await Services.search.setDefault(
    addedEngine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  // We only show the engine's own icon for app provided engines, otherwise show
  // a default. xref https://bugzilla.mozilla.org/show_bug.cgi?id=1449338#c19
  await ensureIcon(tab, "chrome://global/skin/icons/search-glass.svg");
  if (isHandoff) {
    await ensurePlaceholder(tab, "newtab-search-box-handoff-input-no-engine");
  }

  // Disable suggestions in the Urlbar. This should update the placeholder
  // string since handoff will now enter search mode.
  if (isHandoff) {
    await SpecialPowers.pushPrefEnv({
      set: [["browser.urlbar.suggest.searches", false]],
    });
    await ensurePlaceholder(tab, "newtab-search-box-input");
    await SpecialPowers.popPrefEnv();
  }

  await Services.search.setDefault(
    defaultEngine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  BrowserTestUtils.removeTab(tab);
}

add_task(async function test_content_search_attributes() {
  await SpecialPowers.pushPrefEnv({
    set: [[HANDOFF_PREF, true]],
  });

  await runNewTabTest(true);
  await SpecialPowers.popPrefEnv();
});

add_task(async function test_content_search_attributes_no_handoff() {
  await SpecialPowers.pushPrefEnv({
    set: [[HANDOFF_PREF, false]],
  });

  await runNewTabTest(false);
  await SpecialPowers.popPrefEnv();
});

add_task(async function test_content_search_attributes_in_private_window() {
  let win = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
    waitForTabURL: "about:privatebrowsing",
  });
  let tab = win.gBrowser.selectedTab;

  let engineIcon = defaultEngine.getIconURLBySize(16, 16);

  await ensureIcon(tab, engineIcon);
  await ensurePlaceholder(
    tab,
    "about-private-browsing-handoff",
    Services.search.defaultEngine.name
  );

  await Services.search.setDefault(
    addedEngine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  // We only show the engine's own icon for app provided engines, otherwise show
  // a default. xref https://bugzilla.mozilla.org/show_bug.cgi?id=1449338#c19
  await ensureIcon(tab, "chrome://global/skin/icons/search-glass.svg");
  await ensurePlaceholder(tab, "about-private-browsing-handoff-no-engine");

  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.suggest.searches", false]],
  });
  await ensurePlaceholder(tab, "about-private-browsing-search-btn");
  await SpecialPowers.popPrefEnv();

  await Services.search.setDefault(
    defaultEngine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_content_search_permanent_private_browsing() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [HANDOFF_PREF, true],
      ["browser.privatebrowsing.autostart", true],
    ],
  });

  let win = await BrowserTestUtils.openNewBrowserWindow();
  await runNewTabTest(true);
  await BrowserTestUtils.closeWindow(win);
  await SpecialPowers.popPrefEnv();
});