summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_autocomplete_nav_and_tab_key.js
blob: 79e28cb48d40cf7c0eddb52d4cd7e4991f15136e (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// See Bug 585991.

const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html>
<head>
  <script>
    /* Create a prototype-less object so popup does not contain native
     * Object prototype properties.
     */
    window.foo = Object.create(null, Object.getOwnPropertyDescriptors({
      item00: "value0",
      item1: "value1",
      item2: "value2",
      item3: "value3",
    }));
  </script>
</head>
<body>bug 585991 - autocomplete popup navigation and tab key usage test</body>`;

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);
  const { jsterm } = hud;
  info("web console opened");

  const { autocompletePopup: popup } = jsterm;

  ok(!popup.isOpen, "popup is not open");

  const onPopUpOpen = popup.once("popup-opened");
  setInputValue(hud, "window.foo");

  // Shows the popup
  EventUtils.sendString(".");
  await onPopUpOpen;

  ok(popup.isOpen, "popup is open");

  const expectedPopupItems = ["item00", "item1", "item2", "item3"];
  ok(
    hasExactPopupLabels(popup, expectedPopupItems),
    "getItems returns the items we expect"
  );
  is(popup.selectedIndex, 0, "Index of the first item is selected.");

  EventUtils.synthesizeKey("KEY_ArrowUp");

  is(popup.selectedIndex, 3, "index 3 is selected");
  is(popup.selectedItem.label, "item3", "item3 is selected");
  checkInputCompletionValue(hud, "item3", "completeNode.value holds item3");

  EventUtils.synthesizeKey("KEY_ArrowUp");

  is(popup.selectedIndex, 2, "index 2 is selected");
  is(popup.selectedItem.label, "item2", "item2 is selected");
  checkInputCompletionValue(hud, "item2", "completeNode.value holds item2");

  EventUtils.synthesizeKey("KEY_ArrowDown");

  is(popup.selectedIndex, 3, "index 3 is selected");
  is(popup.selectedItem.label, "item3", "item3 is selected");
  checkInputCompletionValue(hud, "item3", "completeNode.value holds item3");

  let currentSelectionIndex = popup.selectedIndex;

  EventUtils.synthesizeKey("KEY_PageUp");
  Assert.less(
    popup.selectedIndex,
    currentSelectionIndex,
    "Index is less after Page UP"
  );

  currentSelectionIndex = popup.selectedIndex;
  EventUtils.synthesizeKey("KEY_PageDown");
  Assert.greater(
    popup.selectedIndex,
    currentSelectionIndex,
    "Index is greater after PGDN"
  );

  EventUtils.synthesizeKey("KEY_Home");
  is(popup.selectedIndex, 0, "index is first after Home");

  EventUtils.synthesizeKey("KEY_End");
  is(
    popup.selectedIndex,
    expectedPopupItems.length - 1,
    "index is last after End"
  );

  info("press Tab and wait for popup to hide");
  const onPopupClose = popup.once("popup-closed");
  EventUtils.synthesizeKey("KEY_Tab");

  await onPopupClose;

  // At this point the completion suggestion should be accepted.
  ok(!popup.isOpen, "popup is not open");
  is(
    getInputValue(hud),
    "window.foo.item3",
    "completion was successful after KEY_Tab"
  );
  ok(!getInputCompletionValue(hud), "completeNode is empty");

  info(
    "Check that hitting Home hides the completion text when the popup is hidden"
  );
  await setInputValueForAutocompletion(hud, "window.foo.item0");
  checkInputCompletionValue(hud, "0", "completeNode has expected value");
  if (Services.appinfo.OS == "Darwin") {
    EventUtils.synthesizeKey("a", { ctrlKey: true });
  } else {
    EventUtils.synthesizeKey("KEY_Home");
  }
  checkInputCompletionValue(
    hud,
    "",
    "completeNode was cleared after hitting Home"
  );

  info(
    "Check that hitting End hides the completion text when the popup is hidden"
  );
  await setInputValueForAutocompletion(hud, "window.foo.item0");
  checkInputCompletionValue(hud, "0", "completeNode has expected value");
  EventUtils.synthesizeKey("KEY_End");
  checkInputCompletionValue(
    hud,
    "",
    "completeNode was cleared after hitting End"
  );
});