summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_separatePrivateDefault.js
blob: 8cdc0e746b5003969e773c54d78d25087fb59813 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests the 'Search in a Private Window' result of the address bar.
// Tests here don't have a different private engine, for that see
// browser_separatePrivateDefault_differentPrivateEngine.js

const serverInfo = {
  scheme: "http",
  host: "localhost",
  port: 20709, // Must be identical to what is in searchSuggestionEngine2.xml
};

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

  // Add some history for the empty panel.
  await PlacesTestUtils.addVisits([
    {
      uri: "http://example.com/",
      transition: PlacesUtils.history.TRANSITIONS.TYPED,
    },
  ]);

  // Add a search suggestion engine and move it to the front so that it appears
  // as the first one-off.
  await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + "searchSuggestionEngine.xml",
    setAsDefault: true,
    setAsDefaultPrivate: true,
  });

  // Add another engine in the first one-off position.
  let engine2 = await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + "POSTSearchEngine.xml",
  });
  await Services.search.moveEngine(engine2, 0);

  // Add an engine with an alias.
  await SearchTestUtils.installSearchExtension({
    name: "MozSearch",
    keyword: "alias",
  });

  registerCleanupFunction(async () => {
    await PlacesUtils.history.clear();
  });
});

async function AssertNoPrivateResult(win) {
  let count = await UrlbarTestUtils.getResultCount(win);
  Assert.ok(count > 0, "Sanity check result count");
  for (let i = 0; i < count; ++i) {
    let result = await UrlbarTestUtils.getDetailsOfResultAt(win, i);
    Assert.ok(
      result.type != UrlbarUtils.RESULT_TYPE.SEARCH ||
        !result.searchParams.inPrivateWindow,
      "Check this result is not a 'Search in a Private Window' one"
    );
  }
}

async function AssertPrivateResult(win, engine, isPrivateEngine) {
  let count = await UrlbarTestUtils.getResultCount(win);
  Assert.ok(count > 1, "Sanity check result count");
  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 1);
  Assert.equal(
    result.type,
    UrlbarUtils.RESULT_TYPE.SEARCH,
    "Check result type"
  );
  Assert.ok(result.searchParams.inPrivateWindow, "Check inPrivateWindow");
  Assert.equal(
    result.searchParams.isPrivateEngine,
    isPrivateEngine,
    "Check isPrivateEngine"
  );
  Assert.equal(
    result.searchParams.engine,
    engine.name,
    "Check the search engine"
  );
  return result;
}

add_task(async function test_nonsearch() {
  info(
    "Test that 'Search in a Private Window' does not appear with non-search results"
  );
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "exa",
  });
  await AssertNoPrivateResult(window);
});

add_task(async function test_search() {
  info(
    "Test that 'Search in a Private Window' appears with only search results"
  );
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "unique198273982173",
  });
  await AssertPrivateResult(window, await Services.search.getDefault(), false);
});

add_task(async function test_search_urlbar_result_disabled() {
  info("Test that 'Search in a Private Window' does not appear when disabled");
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.search.separatePrivateDefault.urlbarResult.enabled", false],
    ],
  });
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "unique198273982173",
  });
  await AssertNoPrivateResult(window);
  await SpecialPowers.popPrefEnv();
});

add_task(async function test_search_disabled_suggestions() {
  info(
    "Test that 'Search in a Private Window' appears if suggestions are disabled"
  );
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.suggest.searches", false]],
  });
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "unique198273982173",
  });
  await AssertPrivateResult(window, await Services.search.getDefault(), false);
  await SpecialPowers.popPrefEnv();
});

// TODO: (Bug 1658620) Write a new subtest for this behaviour with the update2
// pref on.
// add_task(async function test_oneoff_selected_keyboard() {
//   info(
//     "Test that 'Search in a Private Window' with keyboard opens the selected one-off engine if there's no private engine"
//   );
//   await SpecialPowers.pushPrefEnv({
//     set: [
//       ["browser.urlbar.update2", false],
//       ["browser.urlbar.update2.oneOffsRefresh", false],
//     ],
//   });
//   await UrlbarTestUtils.promiseAutocompleteResultPopup({
//     window,
//     value: "unique198273982173",
//   });
//   await AssertPrivateResult(window, await Services.search.getDefault(), false);
//   // Select the 'Search in a Private Window' result, alt down to select the
//   // first one-off button, Enter. It should open a pb window, but using the
//   // selected one-off engine.
//   let promiseWindow = BrowserTestUtils.waitForNewWindow({
//     url:
//       "http://mochi.test:8888/browser/browser/components/urlbar/tests/browser/print_postdata.sjs",
//   });
//   // Select the private result.
//   EventUtils.synthesizeKey("KEY_ArrowDown");
//   // Select the first one-off button.
//   EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
//   EventUtils.synthesizeKey("VK_RETURN");
//   let win = await promiseWindow;
//   Assert.ok(
//     PrivateBrowsingUtils.isWindowPrivate(win),
//     "Should open a private window"
//   );
//   await BrowserTestUtils.closeWindow(win);
//   await SpecialPowers.popPrefEnv();
// });

// TODO: (Bug 1658620) Write a new subtest for this behaviour with the update2
// pref on.
// add_task(async function test_oneoff_selected_mouse() {
//   info(
//     "Test that 'Search in a Private Window' with mouse opens the selected one-off engine if there's no private engine"
//   );
//   await SpecialPowers.pushPrefEnv({
//     set: [
//       ["browser.urlbar.update2", false],
//       ["browser.urlbar.update2.oneOffsRefresh", false],
//     ],
//   });
//   await UrlbarTestUtils.promiseAutocompleteResultPopup({
//     window,
//     value: "unique198273982173",
//   });
//   await AssertPrivateResult(window, await Services.search.getDefault(), false);
//   // Select the 'Search in a Private Window' result, alt down to select the
//   // first one-off button, Enter. It should open a pb window, but using the
//   // selected one-off engine.
//   let promiseWindow = BrowserTestUtils.waitForNewWindow({
//     url:
//       "http://mochi.test:8888/browser/browser/components/urlbar/tests/browser/print_postdata.sjs",
//   });
//   // Select the private result.
//   EventUtils.synthesizeKey("KEY_ArrowDown");
//   // Select the first one-off button.
//   EventUtils.synthesizeKey("KEY_ArrowDown", { altKey: true });
//   // Click on the result.
//   let element = UrlbarTestUtils.getSelectedRow(window);
//   EventUtils.synthesizeMouseAtCenter(element, {});
//   let win = await promiseWindow;
//   Assert.ok(
//     PrivateBrowsingUtils.isWindowPrivate(win),
//     "Should open a private window"
//   );
//   await BrowserTestUtils.closeWindow(win);
//   await SpecialPowers.popPrefEnv();
// });