summaryrefslogtreecommitdiff
path: root/dom/smil/test/test_smilAccessKey.xhtml
blob: ef7e1b73d4ab5bf98ebc4184167b12f77bdd2bda (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
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test for SMIL accessKey support</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank"
  href="https://bugzilla.mozilla.org/show_bug.cgi?id=587910">Mozilla Bug
  587910</a>
<p id="display"></p>
<div id="content" style="display: none">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" width="120px" height="120px">
  <circle cx="20" cy="20" r="15" fill="blue" id="circle"/>
</svg>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
<![CDATA[
/** Test for SMIL accessKey support **/

const gSvgns = "http://www.w3.org/2000/svg";
var gSvg = document.getElementById("svg");
SimpleTest.waitForExplicitFinish();

function main()
{
  gSvg.pauseAnimations();

  // Basic syntax
  testOk('accessKey(a)', 'a');
  testOk(' accessKey(a)  ', 'a');
  testNotOk('accessKey (a)', 'a');
  testNotOk('accessKey( a)', 'a');
  testNotOk('accessKey(a )', 'a');
  testNotOk('accessKey(a)', 'b');
  testNotOk('accessKey()', ' ');

  // Test the test framework itself
  testOk('accessKey(a)', 97);

  // Allow for either accessKey (SVG / SMIL Animation) or accesskey (SMIL2+)
  testOk('accesskey(a)', 'a');

  // Offset
  testOk('accessKey(a)+0s', 'a');
  testOk('accessKey(a) + 0min', 'a');
  testOk('accessKey(a) -0h', 'a');
  testOk('accessKey(a)+100ms', 'a', 0, 0.1);
  testOk('accessKey(a)-0.1s', 'a', 0, -0.1);

  // Id references are not allowed
  testNotOk('svg.accessKey(a)', 'a');
  testNotOk('window.accessKey(a)', 'a');

  // Case sensitivity
  testOk('accessKey(A)', 'A');
  testNotOk('accessKey(a)', 'A');
  testNotOk('accessKey(A)', 'a');

  // Test unusual characters
  testOk('accessKey(-)', '-');
  testOk('accessKey(\\)', '\\');
  testOk('accessKey( )', ' ');
  testOk('accessKey(\x0D)', 0, KeyboardEvent.DOM_VK_RETURN);
  testOk('accessKey(\n)', 0, KeyboardEvent.DOM_VK_RETURN); // New line
  testOk('accessKey(\r)', 0, KeyboardEvent.DOM_VK_RETURN); // Carriage return
  testOk('accessKey(\x08)', 0, KeyboardEvent.DOM_VK_BACK_SPACE);
  testOk('accessKey(\x1B)', 0, KeyboardEvent.DOM_VK_ESCAPE);
  testOk('accessKey(\x7F)', 0, KeyboardEvent.DOM_VK_DELETE);

  // Check some disallowed keys
  // -- For now we don't allow tab since the interaction with focus causes
  //    confusing results
  testNotOk('accessKey(\x09)', 0, 9);  // Tab

  // Test setting the keyCode field
  testNotOk('accessKey(a)', 0, 97);
  testOk('accessKey(a)', 97, 66); // Give priority to charCode field
  testNotOk('accessKey(a)', 98, 97); // Give priority to charCode field

  // Test unicode
  testOk("accessKey(\u20AC)", 8364); // euro-symbol

  // Test an astral character just to make sure we don't crash
  testOk("accessKey(\uD835\uDC00)", 119808); // mathematical bold capital A
                                             // 0x1D400
  // Test bad surrogate pairs don't confuse us either
  testNotOk("accessKey(\uD800\uD800)", 97);
  testNotOk("accessKey(\uD80020)", 97);
  testNotOk("accessKey(\uD800)", 97);

  // Test modifiers
  // -- When matching on charCode ignore shift and alt
  testNotOk('accessKey(a)', 'a', 0, 0, { ctrl: true });
  testNotOk('accessKey(a)', 'a', 0, 0, { meta: true });
  testOk('accessKey(a)', 'a', 0, 0, { alt: true });
  testOk('accessKey(a)', 'a', 0, 0, { shift: true });
  testNotOk('accessKey(a)', 'a', 0, 0, { shift: true, ctrl: true });
  testNotOk('accessKey(a)', 'a', 0, 0, { alt: true, meta: true });
  // -- When matching on keyCode ignore all
  testNotOk('accessKey(\x0D)', 0, 13, 0, { ctrl: true });
  testNotOk('accessKey(\x0D)', 0, 13, 0, { meta: true });
  testNotOk('accessKey(\x0D)', 0, 13, 0, { alt: true });
  testNotOk('accessKey(\x0D)', 0, 13, 0, { shift: true });
  testNotOk('accessKey(\x0D)', 0, 13, 0, { shift: true, ctrl: true });

  testOpenEnd();
  testPreventDefault();
  testDispatchToWindow();
  testAdoptNode();
  testFauxEvent();

  SimpleTest.finish();
}

function testOk(spec, charCode, keyCode, offset, modifiers)
{
  if (typeof offset == 'undefined') offset = 0;
  var msg = "No interval created for '" + spec +
    "' with input [charCode: " + charCode + "; keyCode: " + keyCode + "]" +
    getModifiersDescr(modifiers);
  ok(test(spec, charCode, keyCode, offset, modifiers), msg);
}

function testNotOk(spec, charCode, keyCode, offset, modifiers)
{
  if (typeof offset == 'undefined') offset = 0;
  var msg = "Interval unexpectedly created for '" + spec +
    "' with input [charCode: " + charCode + "; keyCode: " + keyCode + "]" +
    getModifiersDescr(modifiers);
  ok(!test(spec, charCode, keyCode, offset, modifiers), msg);
}

function getModifiersDescr(modifiers)
{
  if (typeof modifiers != 'object')
    return '';
  var str = ' modifiers set:';
  for (var key in modifiers) {
    if (modifiers[key]) str += ' ' + key;
  }
  return str;
}

function test(spec, charCode, keyCode, offset, modifiers)
{
  gSvg.setCurrentTime(1);
  ok(gSvg.animationsPaused(), "Expected animations to be paused");

  var anim = createAnim(spec);
  var evt = createEvent(charCode, keyCode, modifiers);

  document.getElementById('circle').dispatchEvent(evt);

  var gotStartTimeOk = true;
  try {
    var start = anim.getStartTime();
    if (offset) {
      var expected = gSvg.getCurrentTime() + offset;
      ok(Math.abs(expected - start) <= 0.00001,
        "Unexpected start time for animation with begin: " + spec +
          " got " + start + ", expected " + expected);
    } else {
      is(start, gSvg.getCurrentTime() + offset,
         "Unexpected start time for animation with begin: " + spec);
    }
  } catch(e) {
    is(e.name, "InvalidStateError",
       "Unexpected exception: " + e.name);
    is(e.code, DOMException.INVALID_STATE_ERR,
       "Unexpected exception code: " + e.code);
    gotStartTimeOk = false;
  }

  anim.parentNode.removeChild(anim);

  return gotStartTimeOk;
}

function createAnim(beginSpec)
{
  var anim = document.createElementNS(gSvgns, 'animate');
  anim.setAttribute('attributeName', 'cx');
  anim.setAttribute('values', '0; 100');
  anim.setAttribute('dur', '10s');
  anim.setAttribute('begin', beginSpec);
  return document.getElementById('circle').appendChild(anim);
}

function createEvent(charCode, keyCode, modifiers)
{
  if (typeof charCode == 'string') {
    is(charCode.length, 1,
       "If charCode is a string it should be 1 character long");
    charCode = charCode.charCodeAt(0);
  } else if (typeof charCode == 'undefined') {
    charCode = 0;
  }
  args = { ctrl: false, alt: false, shift: false, meta: false };
  if (typeof modifiers == 'object') {
    for (var key in modifiers)
      args[key] = modifiers[key];
  }
  if (typeof keyCode == 'undefined') keyCode = 0;
  var evt = document.createEvent("KeyboardEvent");
  evt.initKeyEvent("keypress", true, true, window,
                   args['ctrl'],
                   args['alt'],
                   args['shift'],
                   args['meta'],
                   keyCode,
                   charCode);
  return evt;
}

function testOpenEnd()
{
  // Test that an end specification with an accesskey value is treated as open
  // ended
  gSvg.setCurrentTime(0);
  ok(gSvg.animationsPaused(), "Expected animations to be paused");

  var anim = createAnim('0s; 2s');
  anim.setAttribute('end', '1s; accessKey(a)');

  gSvg.setCurrentTime(2);

  try {
    is(anim.getStartTime(), 2,
       "Unexpected start time for second interval of open-ended animation");
  } catch(e) {
    is(e.name, "InvalidStateError",
       "Unexpected exception:" + e.name);
    is(e.code, DOMException.INVALID_STATE_ERR,
       "Unexpected exception code:" + e.code);
    ok(false, "Failed to recognise accessKey as qualifying for creating an " +
              "open-ended interval");
  }

  anim.parentNode.removeChild(anim);
}

function testPreventDefault()
{
  // SVG/SMIL don't specify what should happen if preventDefault is called on
  // the keypress event. For now, for consistency with event timing we ignore
  // it.
  gSvg.setCurrentTime(1);
  ok(gSvg.animationsPaused(), "Expected animations to be paused");

  var anim = createAnim('accessKey(a)');
  var evt = createEvent('a');

  var circle = document.getElementById('circle');
  var func = function(evt) { evt.preventDefault(); }
  circle.addEventListener('keypress', func, false);
  circle.dispatchEvent(evt);

  try {
    var start = anim.getStartTime();
  } catch(e) {
    ok(false, "preventDefault() cancelled accessKey handling");
  }

  circle.removeEventListener('keypress', func, false);
  anim.parentNode.removeChild(anim);
}

function testDispatchToWindow()
{
  gSvg.setCurrentTime(1);
  ok(gSvg.animationsPaused(), "Expected animations to be paused");

  var anim = createAnim('accessKey(a)');
  var evt = createEvent('a');

  window.dispatchEvent(evt);

  try {
    var start = anim.getStartTime();
  } catch(e) {
    ok(false, "Key event dispatched to the window failed to trigger " +
              "accesskey handling");
  }

  anim.parentNode.removeChild(anim);
}

function testAdoptNode()
{
  gSvg.setCurrentTime(1);
  ok(gSvg.animationsPaused(), "Expected animations to be paused");

  // Create a new document with an animation element
  var newdoc = document.implementation.createDocument(gSvgns, 'svg', null);
  var anim = newdoc.createElementNS(gSvgns, 'animate');
  anim.setAttribute('attributeName', 'cx');
  anim.setAttribute('values', '0; 100');
  anim.setAttribute('dur', '10s');
  anim.setAttribute('begin', 'accesskey(a)');
  newdoc.documentElement.appendChild(anim);

  // Adopt
  ok(anim.ownerDocument !== document,
     "Expected newly created animation to belong to a different doc");
  document.adoptNode(anim);
  document.getElementById('circle').appendChild(anim);
  ok(anim.ownerDocument === document,
     "Expected newly created animation to belong to the same doc");

  var evt = createEvent('a');

  // Now fire an event at the original window and check nothing happens
  newdoc.dispatchEvent(evt);
  try {
    var start = anim.getStartTime();
    ok(false, "Adopted node still receiving accesskey events from old doc");
  } catch(e) {
    // Ok
  }

  // And then fire at our window
  document.dispatchEvent(evt);
  try {
    var start = anim.getStartTime();
  } catch(e) {
    ok(false, "Adopted node failed to catch accesskey event");
  }

  anim.parentNode.removeChild(anim);
}

function testFauxEvent()
{
  // Test a non-KeyEvent labelled as a key event
  gSvg.setCurrentTime(0);
  ok(gSvg.animationsPaused(), "Expected animations to be paused");

  var anim = createAnim('accessKey(a)');
  var evt = document.createEvent("SVGEvents");
  evt.initEvent("keypress", true, true);
  document.getElementById('circle').dispatchEvent(evt);

  // We're really just testing that the above didn't crash us, but while we're
  // at it, just do a sanity check that we didn't also create an interval
  try {
    var start = anim.getStartTime();
    ok(false, "Faux event generated interval");
  } catch(e) {
    // All is well
  }

  anim.parentNode.removeChild(anim);
}

window.addEventListener("load", main, false);
]]>
</script>
</pre>
</body>
</html>