summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser/browser_searchbar_openpopup.js
blob: a8fe11dbf41621c4ca9343f56a044ce8aafbba87 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
// Tests that the suggestion popup appears at the right times in response to
// focus and user events (mouse, keyboard, drop).

const searchPopup = document.getElementById("PopupSearchAutoComplete");
const kValues = ["long text", "long text 2", "long text 3"];

async function endCustomizing(aWindow = window) {
  if (aWindow.document.documentElement.getAttribute("customizing") != "true") {
    return true;
  }
  let eventPromise = BrowserTestUtils.waitForEvent(
    aWindow.gNavToolbox,
    "aftercustomization"
  );
  aWindow.gCustomizeMode.exit();
  return eventPromise;
}

async function startCustomizing(aWindow = window) {
  if (aWindow.document.documentElement.getAttribute("customizing") == "true") {
    return true;
  }
  let eventPromise = BrowserTestUtils.waitForEvent(
    aWindow.gNavToolbox,
    "customizationready"
  );
  aWindow.gCustomizeMode.enter();
  return eventPromise;
}

let searchbar;
let textbox;
let searchIcon;
let goButton;
let engine;

add_setup(async function () {
  searchbar = await gCUITestUtils.addSearchBar();
  textbox = searchbar.textbox;
  searchIcon = searchbar.querySelector(".searchbar-search-button");
  goButton = searchbar.querySelector(".search-go-button");

  engine = await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + "testEngine.xml",
    setAsDefault: true,
  });

  await clearSearchbarHistory();

  let addOps = kValues.map(value => {
    return { op: "add", fieldname: "searchbar-history", value };
  });
  info("adding search history values: " + kValues);
  await FormHistory.update(addOps);

  registerCleanupFunction(async () => {
    await clearSearchbarHistory();
    gCUITestUtils.removeSearchBar();
  });
});

// Adds a task that shouldn't show the search suggestions popup.
function add_no_popup_task(task) {
  add_task(async function () {
    let sawPopup = false;
    function listener() {
      sawPopup = true;
    }

    info("Entering test " + task.name);
    searchPopup.addEventListener("popupshowing", listener);
    await task();
    searchPopup.removeEventListener("popupshowing", listener);
    ok(!sawPopup, "Shouldn't have seen the suggestions popup");
    info("Leaving test " + task.name);
  });
}

// Simulates the full set of events for a context click
function context_click(target) {
  for (let event of ["mousedown", "contextmenu", "mouseup"]) {
    EventUtils.synthesizeMouseAtCenter(target, { type: event, button: 2 });
  }
}

// Right clicking the icon should not open the popup.
add_no_popup_task(async function open_icon_context() {
  gURLBar.focus();
  let toolbarPopup = document.getElementById("toolbar-context-menu");

  let promise = promiseEvent(toolbarPopup, "popupshown");
  context_click(searchIcon);
  await promise;

  promise = promiseEvent(toolbarPopup, "popuphidden");
  toolbarPopup.hidePopup();
  await promise;
});

// With no text in the search box left clicking the icon should open the popup.
// Clicking the icon again should hide the popup and not show it again.
add_task(async function open_empty() {
  gURLBar.focus();

  let promise = promiseEvent(searchPopup, "popupshown");
  info("Clicking icon");
  EventUtils.synthesizeMouseAtCenter(searchIcon, {});
  await promise;
  is(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should only show the settings"
  );
  is(textbox.mController.searchString, "", "Should be an empty search string");

  let image = searchPopup.querySelector(".searchbar-engine-image");
  Assert.equal(
    image.src,
    engine.getIconURLBySize(16, 16),
    "Should have the correct icon"
  );

  // By giving the textbox some text any next attempt to open the search popup
  // from the click handler will try to search for this text.
  textbox.value = "foo";

  promise = promiseEvent(searchPopup, "popuphidden");

  info("Hiding popup");
  await EventUtils.promiseNativeMouseEventAndWaitForEvent({
    type: "click",
    target: searchIcon,
    atCenter: true,
    eventTypeToWait: "mouseup",
  });
  await promise;

  is(
    textbox.mController.searchString,
    "",
    "Should not have started to search for the new text"
  );

  // Cancel the search if it started.
  if (textbox.mController.searchString != "") {
    textbox.mController.stopSearch();
  }

  textbox.value = "";
});

// With no text in the search box left clicking it should not open the popup.
add_no_popup_task(function click_doesnt_open_popup() {
  gURLBar.focus();

  EventUtils.synthesizeMouseAtCenter(textbox, {});
  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 0, "Should have selected all of the text");
});

// Left clicking in a non-empty search box when unfocused should focus it and open the popup.
add_task(async function click_opens_popup() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(textbox, {});
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  promise = promiseEvent(searchPopup, "popuphidden");
  searchPopup.hidePopup();
  await promise;

  textbox.value = "";
});

add_task(async function open_empty_hiddenOneOffs() {
  // Disable all the engines but the current one and check the oneoffs.
  let defaultEngine = await Services.search.getDefault();
  let engines = (await Services.search.getVisibleEngines()).filter(
    e => e.name != defaultEngine.name
  );
  await SpecialPowers.pushPrefEnv({
    set: [["browser.search.hiddenOneOffs", engines.map(e => e.name).join(",")]],
  });

  textbox.value = "foo";
  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(textbox, {});
  await promise;

  Assert.ok(
    searchPopup.searchOneOffsContainer.hasAttribute("hidden"),
    "The one-offs buttons container should have the hidden attribute."
  );
  Assert.ok(
    BrowserTestUtils.is_hidden(searchPopup.searchOneOffsContainer),
    "The one-off buttons container should be hidden."
  );

  promise = promiseEvent(searchPopup, "popuphidden");

  info("Hiding popup");
  await EventUtils.promiseNativeMouseEventAndWaitForEvent({
    type: "click",
    target: searchIcon,
    atCenter: true,
    eventTypeToWait: "mouseup",
  });
  await promise;

  await SpecialPowers.popPrefEnv();
  textbox.value = "";
});

// Right clicking in a non-empty search box when unfocused should open the edit context menu.
add_no_popup_task(async function right_click_doesnt_open_popup() {
  gURLBar.focus();
  textbox.value = "foo";

  // Can't wait for an event on the actual menu since it is created
  // lazily the first time it is displayed.
  let promise = new Promise(resolve => {
    let listener = event => {
      if (searchbar._menupopup && event.target == searchbar._menupopup) {
        resolve(searchbar._menupopup);
      }
    };
    window.addEventListener("popupshown", listener);
  });
  context_click(textbox);
  let contextPopup = await promise;

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  promise = promiseEvent(contextPopup, "popuphidden");
  contextPopup.hidePopup();
  await promise;

  textbox.value = "";
});

// Moving focus away from the search box should close the popup
add_task(async function focus_change_closes_popup() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(textbox, {});
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  promise = promiseEvent(searchPopup, "popuphidden");
  let promise2 = promiseEvent(searchbar.textbox, "blur");
  EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
  await promise;
  await promise2;

  textbox.value = "";
});

// Moving focus away from the search box should close the small popup
add_task(async function focus_change_closes_small_popup() {
  gURLBar.focus();

  let promise = promiseEvent(searchPopup, "popupshown");
  // For some reason sending the mouse event immediately doesn't open the popup.
  SimpleTest.executeSoon(() => {
    EventUtils.synthesizeMouseAtCenter(searchIcon, {});
  });
  await promise;
  is(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the small popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );

  promise = promiseEvent(searchPopup, "popuphidden");
  let promise2 = promiseEvent(searchbar.textbox, "blur");
  EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
  await promise;
  await promise2;
});

// Pressing escape should close the popup.
add_task(async function escape_closes_popup() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(textbox, {});
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  promise = promiseEvent(searchPopup, "popuphidden");
  EventUtils.synthesizeKey("KEY_Escape");
  await promise;

  textbox.value = "";
});

// Pressing contextmenu should close the popup.
add_task(async function contextmenu_closes_popup() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(textbox, {});
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  let contextPopup = searchbar._menupopup;
  let contextMenuShownPromise = promiseEvent(contextPopup, "popupshown");
  let searchPopupHiddenPromise = promiseEvent(searchPopup, "popuphidden");
  context_click(textbox);
  await contextMenuShownPromise;
  await searchPopupHiddenPromise;

  let contextMenuHiddenPromise = promiseEvent(contextPopup, "popuphidden");
  contextPopup.hidePopup();
  await contextMenuHiddenPromise;

  textbox.value = "";
});

// Tabbing to the search box should open the popup if it contains text.
add_task(async function tab_opens_popup() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeKey("KEY_Tab");
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  promise = promiseEvent(searchPopup, "popuphidden");
  searchPopup.hidePopup();
  await promise;

  textbox.value = "";
});

// Tabbing to the search box should not open the popup if it doesn't contain text.
add_no_popup_task(function tab_doesnt_open_popup() {
  gURLBar.focus();
  textbox.value = "foo";

  EventUtils.synthesizeKey("KEY_Tab");

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  textbox.value = "";
});

// Switching back to the window when the search box has focus from mouse should not open the popup.
add_task(async function refocus_window_doesnt_open_popup_mouse() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(searchbar, {});
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  promise = promiseEvent(searchPopup, "popuphidden");
  let newWin = OpenBrowserWindow();
  await new Promise(resolve => waitForFocus(resolve, newWin));
  await promise;

  function listener() {
    ok(false, "Should not have shown the popup.");
  }
  searchPopup.addEventListener("popupshowing", listener);

  promise = promiseEvent(searchbar.textbox, "focus");
  newWin.close();
  await promise;

  // Wait a few ticks to allow any focus handlers to show the popup if they are going to.
  await new Promise(resolve => executeSoon(resolve));
  await new Promise(resolve => executeSoon(resolve));
  await new Promise(resolve => executeSoon(resolve));

  searchPopup.removeEventListener("popupshowing", listener);
  textbox.value = "";
});

// Switching back to the window when the search box has focus from keyboard should not open the popup.
add_task(async function refocus_window_doesnt_open_popup_keyboard() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeKey("KEY_Tab");
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  promise = promiseEvent(searchPopup, "popuphidden");
  let newWin = OpenBrowserWindow();
  await new Promise(resolve => waitForFocus(resolve, newWin));
  await promise;

  function listener() {
    ok(false, "Should not have shown the popup.");
  }
  searchPopup.addEventListener("popupshowing", listener);

  promise = promiseEvent(searchbar.textbox, "focus");
  newWin.close();
  await promise;

  // Wait a few ticks to allow any focus handlers to show the popup if they are going to.
  await new Promise(resolve => executeSoon(resolve));
  await new Promise(resolve => executeSoon(resolve));
  await new Promise(resolve => executeSoon(resolve));

  searchPopup.removeEventListener("popupshowing", listener);
  textbox.value = "";
});

// Clicking the search go button shouldn't open the popup
add_no_popup_task(async function search_go_doesnt_open_popup() {
  gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);

  gURLBar.focus();
  textbox.value = "foo";
  searchbar.updateGoButtonVisibility();

  let promise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  EventUtils.synthesizeMouseAtCenter(goButton, {});
  await promise;

  textbox.value = "";
  gBrowser.removeCurrentTab();
});

// Clicks outside the search popup should close the popup but not consume the click.
add_task(async function dont_consume_clicks() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(textbox, {});
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  is(textbox.selectionStart, 0, "Should have selected all of the text");
  is(textbox.selectionEnd, 3, "Should have selected all of the text");

  promise = promiseEvent(searchPopup, "popuphidden");
  await EventUtils.promiseNativeMouseEventAndWaitForEvent({
    type: "click",
    target: gURLBar.inputField,
    atCenter: true,
    eventTypeToWait: "mouseup",
  });
  await promise;

  is(
    Services.focus.focusedElement,
    gURLBar.inputField,
    "Should have focused the URL bar"
  );

  textbox.value = "";
});

// Dropping text to the searchbar should open the popup
add_task(async function drop_opens_popup() {
  CustomizableUI.addWidgetToArea("home-button", "nav-bar");
  // The previous task leaves focus in the URL bar. However, in that case drags
  // can be interpreted as being selection drags by the drag manager, which
  // breaks the drag synthesis from EventUtils.js below. To avoid this, focus
  // the browser content instead.
  let focusEventPromise = BrowserTestUtils.waitForEvent(
    gBrowser.selectedBrowser,
    "focus"
  );
  gBrowser.selectedBrowser.focus();
  await focusEventPromise;

  let promise = promiseEvent(searchPopup, "popupshown");
  // Use a source for the drop that is outside of the search bar area, to avoid
  // it receiving a mousedown and causing the popup to sometimes open.
  let homeButton = document.getElementById("home-button");
  EventUtils.synthesizeDrop(
    homeButton,
    textbox,
    [[{ type: "text/plain", data: "foo" }]],
    "move",
    window
  );
  await promise;

  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );
  is(
    Services.focus.focusedElement,
    textbox,
    "Should have focused the search bar"
  );
  promise = promiseEvent(searchPopup, "popuphidden");
  searchPopup.hidePopup();
  await promise;

  textbox.value = "";
  CustomizableUI.removeWidgetFromArea("home-button");
});

// Moving the caret using the cursor keys should not close the popup.
add_task(async function dont_rollup_oncaretmove() {
  gURLBar.focus();
  textbox.value = "long text";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(textbox, {});
  await promise;

  // Deselect the text
  EventUtils.synthesizeKey("KEY_ArrowRight");
  is(
    textbox.selectionStart,
    9,
    "Should have moved the caret (selectionStart after deselect right)"
  );
  is(
    textbox.selectionEnd,
    9,
    "Should have moved the caret (selectionEnd after deselect right)"
  );
  is(searchPopup.state, "open", "Popup should still be open");

  EventUtils.synthesizeKey("KEY_ArrowLeft");
  is(
    textbox.selectionStart,
    8,
    "Should have moved the caret (selectionStart after left)"
  );
  is(
    textbox.selectionEnd,
    8,
    "Should have moved the caret (selectionEnd after left)"
  );
  is(searchPopup.state, "open", "Popup should still be open");

  EventUtils.synthesizeKey("KEY_ArrowRight");
  is(
    textbox.selectionStart,
    9,
    "Should have moved the caret (selectionStart after right)"
  );
  is(
    textbox.selectionEnd,
    9,
    "Should have moved the caret (selectionEnd after right)"
  );
  is(searchPopup.state, "open", "Popup should still be open");

  // Ensure caret movement works while a suggestion is selected.
  is(textbox.popup.selectedIndex, -1, "No selected item in list");
  EventUtils.synthesizeKey("KEY_ArrowDown");
  is(textbox.popup.selectedIndex, 0, "Selected item in list");
  is(
    textbox.selectionStart,
    9,
    "Should have moved the caret to the end (selectionStart after selection)"
  );
  is(
    textbox.selectionEnd,
    9,
    "Should have moved the caret to the end (selectionEnd after selection)"
  );

  EventUtils.synthesizeKey("KEY_ArrowLeft");
  is(
    textbox.selectionStart,
    8,
    "Should have moved the caret again (selectionStart after left)"
  );
  is(
    textbox.selectionEnd,
    8,
    "Should have moved the caret again (selectionEnd after left)"
  );
  is(searchPopup.state, "open", "Popup should still be open");

  EventUtils.synthesizeKey("KEY_ArrowLeft");
  is(
    textbox.selectionStart,
    7,
    "Should have moved the caret (selectionStart after left)"
  );
  is(
    textbox.selectionEnd,
    7,
    "Should have moved the caret (selectionEnd after left)"
  );
  is(searchPopup.state, "open", "Popup should still be open");

  EventUtils.synthesizeKey("KEY_ArrowRight");
  is(
    textbox.selectionStart,
    8,
    "Should have moved the caret (selectionStart after right)"
  );
  is(
    textbox.selectionEnd,
    8,
    "Should have moved the caret (selectionEnd after right)"
  );
  is(searchPopup.state, "open", "Popup should still be open");

  if (!navigator.platform.includes("Mac")) {
    EventUtils.synthesizeKey("KEY_Home");
    is(
      textbox.selectionStart,
      0,
      "Should have moved the caret (selectionStart after home)"
    );
    is(
      textbox.selectionEnd,
      0,
      "Should have moved the caret (selectionEnd after home)"
    );
    is(searchPopup.state, "open", "Popup should still be open");
  }

  // Close the popup again
  promise = promiseEvent(searchPopup, "popuphidden");
  EventUtils.synthesizeKey("KEY_Escape");
  await promise;

  textbox.value = "";
});

// Entering customization mode shouldn't open the popup.
add_task(async function dont_open_in_customization() {
  gURLBar.focus();
  textbox.value = "foo";

  let promise = promiseEvent(searchPopup, "popupshown");
  EventUtils.synthesizeKey("KEY_Tab");
  await promise;
  isnot(
    searchPopup.getAttribute("showonlysettings"),
    "true",
    "Should show the full popup"
  );

  info("Entering customization mode");
  let sawPopup = false;
  function listener() {
    sawPopup = true;
  }
  searchPopup.addEventListener("popupshowing", listener);
  await gCUITestUtils.openMainMenu();
  promise = promiseEvent(searchPopup, "popuphidden");
  await startCustomizing();
  await promise;

  searchPopup.removeEventListener("popupshowing", listener);
  ok(!sawPopup, "Shouldn't have seen the suggestions popup");

  await endCustomizing();
  textbox.value = "";
});

add_task(async function cleanup() {
  info("removing search history values: " + kValues);
  let removeOps = kValues.map(value => {
    return { op: "remove", fieldname: "searchbar-history", value };
  });
  FormHistory.update(removeOps);
});