summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/window_keys.xhtml
blob: 77a098ef0b8e9a8fee9940eb88133d783234f558 (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>

<window title="Key Tests"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

<script>
<![CDATA[

const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

SimpleTest.waitForExplicitFinish();

var gExpected = null;


const kIsWin = AppConstants.platform == "win";
const kIsMac = AppConstants.platform == "macosx";

// Only on Windows, metaKey state is ignored when there is no shortcut key handler
// which exactly matches with metaKey state.  Therefore the following tests
// checks kIsWin in some cases which has metaKey.
var keysToTest = [
  ["k-v", "V", { } ],
  ["", "V", { shiftKey: true } ],
  ["k-v-scy", "V", { ctrlKey: true } ],
  ["", "V", { altKey: true } ],
  [kIsWin ? "k-v" : "", "V", { metaKey: true } ],
  ["k-v-scy", "V", { shiftKey: true, ctrlKey: true } ],
  ["", "V", { shiftKey: true, ctrlKey: true, altKey: true } ],
  ["k-e-y", "E", { } ],
  ["", "E", { shiftKey: true } ],
  ["", "E", { ctrlKey: true } ],
  ["", "E", { altKey: true } ],
  [kIsWin ? "k-e-y" : "", "E", { metaKey: true } ],
  ["k-d-a", "D", { altKey: true } ],
  ["k-8-m", "8", { metaKey: true } ],
  ["", "B", {} ],
  ["k-c-scaym", "C", { metaKey: true } ],
  ["k-c-scaym", "C", { shiftKey: true, ctrlKey: true, altKey: true, metaKey: true } ],
  ["", "V", { shiftKey: true, ctrlKey: true, altKey: true } ],
  ["k-h-l", "H", { accelKey: true } ],
  [kIsWin || kIsMac ? "k-h-l" : "", "H", { accelKey: true, metaKey: true } ],
//  ["k-j-s", "J", { accessKey: true } ],
  ["", "T", { } ],
  ["k-g-c", "G", { ctrlKey: true } ],
  ["k-g-cm", "G", { ctrlKey: true, metaKey: true } ],
  ["scommand", "Y", { } ],
  ["", "U", { } ],
  ["k-z-c", "Z", { ctrlKey: true } ],
];

function runTest()
{
  let nonInlineKeyFired = false;
  document.getElementById("k-z-c").addEventListener("command", event => {
    nonInlineKeyFired = true;
    checkKey(event);
  });

  iterateKeys(true, "normal");

  ok(nonInlineKeyFired, "non-inline command listener fired");

  var keyset = document.getElementById("keyset");
  keyset.setAttribute("disabled", "true");
  iterateKeys(false, "disabled");

  keyset = document.getElementById("keyset");
  keyset.removeAttribute("disabled");
  iterateKeys(true, "reenabled");

  keyset.remove();
  iterateKeys(false, "removed");

  document.documentElement.appendChild(keyset);
  iterateKeys(true, "appended");

  var accelText = menuitem => menuitem.getAttribute("acceltext").toLowerCase();

  $("menubutton").open = true;

  // now check if a menu updates its accelerator text when a key attribute is changed
  var menuitem1 = $("menuitem1");
  ok(accelText(menuitem1).includes("d"), "menuitem1 accelText before");
  if (kIsWin) {
    ok(accelText(menuitem1).includes("alt"), "menuitem1 accelText modifier before");
  }

  menuitem1.setAttribute("key", "k-s-c");
  ok(accelText(menuitem1).includes("s"), "menuitem1 accelText after");
  if (kIsWin) {
    ok(accelText(menuitem1).includes("ctrl"), "menuitem1 accelText modifier after");
  }

  menuitem1.setAttribute("acceltext", "custom");
  is(accelText(menuitem1), "custom", "menuitem1 accelText set custom");
  menuitem1.removeAttribute("acceltext");
  ok(accelText(menuitem1).includes("s"), "menuitem1 accelText remove");
  if (kIsWin) {
    ok(accelText(menuitem1).includes("ctrl"), "menuitem1 accelText modifier remove");
  }

  var menuitem2 = $("menuitem2");
  is(accelText(menuitem2), "", "menuitem2 accelText before");
  menuitem2.setAttribute("key", "k-s-c");
  ok(accelText(menuitem2).includes("s"), "menuitem2 accelText before");
  if (kIsWin) {
    ok(accelText(menuitem2).includes("ctrl"), "menuitem2 accelText modifier before");
  }

  menuitem2.setAttribute("key", "k-h-l");
  ok(accelText(menuitem2).includes("h"), "menuitem2 accelText after");
  if (kIsWin) {
    ok(accelText(menuitem2).includes("ctrl"), "menuitem2 accelText modifier after");
  }

  menuitem2.removeAttribute("key");
  is(accelText(menuitem2), "", "menuitem2 accelText after remove");

  $("menubutton").open = false;

  window.close();
  window.arguments[0].SimpleTest.finish();
}

function iterateKeys(enabled, testid)
{
  for (var k = 0; k < keysToTest.length; k++) {
    gExpected = keysToTest[k];
    var expectedKey = gExpected[0];
    if (!gExpected[2].accessKey || !navigator.platform.includes("Mac")) {
      synthesizeKey(gExpected[1], gExpected[2]);
      ok((enabled && expectedKey) || expectedKey == "k-d-a" ?
         !gExpected : gExpected, testid + " key step " + (k + 1));
    }
  }
}

function checkKey(event)
{
  // the first element of the gExpected array holds the id of the <key> element
  // that was expected. If this is empty, a handler wasn't expected to be called
  if (gExpected[0])
    is(event.originalTarget.id, gExpected[0], "key " + gExpected[1]);
  else
    is("key " + event.originalTarget.id + " was activated", "", "key " + gExpected[1]);
  gExpected = null;
}

function is(l, r, n) { window.arguments[0].SimpleTest.is(l,r,n); }
function ok(v, n) { window.arguments[0].SimpleTest.ok(v,n); }

SimpleTest.waitForFocus(runTest);

]]>
</script>

<command id="scommand" oncommand="checkKey(event)"/>
<command id="scommand-disabled" disabled="true"/>

<keyset id="keyset">
  <key id="k-v" key="v" oncommand="checkKey(event)"/>
  <key id="k-v-scy" key="v" modifiers="shift any control" oncommand="checkKey(event)"/>
  <key id="k-e-y" key="e" modifiers="any" oncommand="checkKey(event)"/>
  <key id="k-8-m" key="8" modifiers="meta" oncommand="checkKey(event)"/>
  <key id="k-c-scaym" key="c" modifiers="shift control alt any meta" oncommand="checkKey(event)"/>
  <key id="k-h-l" key="h" modifiers="accel" oncommand="checkKey(event)"/>
  <key id="k-j-s" key="j" modifiers="access" oncommand="checkKey(event)"/>
  <key id="k-t-y" disabled="true" key="t" oncommand="checkKey(event)"/>
  <key id="k-g-c" key="g" modifiers="control" oncommand="checkKey(event)"/>
  <key id="k-g-cm" key="g" modifiers="control meta" oncommand="checkKey(event)"/>
  <key id="k-y" key="y" command="scommand"/>
  <key id="k-u" key="u" command="scommand-disabled"/>
  <key id="k-z-c" key="z" modifiers="control"/>
</keyset>

<keyset id="keyset2">
  <key id="k-d-a" key="d" modifiers="alt" oncommand="checkKey(event)"/>
  <key id="k-s-c" key="s" modifiers="control" oncommand="checkKey(event)"/>
</keyset>

<button id="menubutton" label="Menu" type="menu">
  <menupopup>
    <menuitem id="menuitem1" label="Item 1" key="k-d-a"/>
    <menuitem id="menuitem2" label="Item 2"/>
  </menupopup>
</button>

<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>

</window>