summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/shim/test/test_service_focus.html
blob: d720e0b536fb4b0c7f44f8c4e28525d94e048fbd (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
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1278473
-->
<head>
  <title>Test for Bug 1278473 - replace Services.focus</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css"
        href="chrome://mochikit/content/tests/SimpleTest/test.css">

  <script type="application/javascript;version=1.8">
  "use strict";
  var exports = {}
  var module = {exports};
  </script>

  <script type="application/javascript;version=1.8"
	  src="resource://devtools/client/shared/shim/Services.js"></script>
</head>
<body>
  <span>
    <span id="start" testvalue="0" tabindex="0"> </span>
    <label>
      <input testvalue="1" type="radio">Hi</input>
    </label>
    <label>
      <input type="radio" tabindex="-1">Bye</input>
    </label>
    <label style="display: none">
      <input id="button3" type="radio" tabindex="-1">Invisible</input>
    </label>
    <input id="button4" type="radio" disabled="true">Disabled</input>
    <span testvalue="2" tabindex="0"> </span>
  </span>

<script type="application/javascript;version=1.8">
  "use strict";

  // The test assumes these are identical, so assert it here.
  is(Services.focus.MOVEFOCUS_BACKWARD, SpecialPowers.Services.focus.MOVEFOCUS_BACKWARD,
     "check MOVEFOCUS_BACKWARD");
  is(Services.focus.MOVEFOCUS_FORWARD, SpecialPowers.Services.focus.MOVEFOCUS_FORWARD,
     "check MOVEFOCUS_FORWARD");

  function moveFocus(element, type, expect) {
    let current = document.activeElement;
    const suffix = "(type=" + type + ", to=" + expect + ")";

    // First try with the platform implementation.
    SpecialPowers.Services.focus.moveFocus(window, element, type, 0);
    is(document.activeElement.getAttribute("testvalue"), expect,
       "platform moveFocus " + suffix);

    // Reset the focus and try again with the shim.
    current.focus();
    is(document.activeElement, current, "reset " + suffix);

    Services.focus.moveFocus(window, element, type, 0);
    is(document.activeElement.getAttribute("testvalue"), expect,
       "shim moveFocus " + suffix);
  }

  let start = document.querySelector("#start");
  start.focus();
  is(document.activeElement.getAttribute("testvalue"), "0", "initial focus");

  moveFocus(null, Services.focus.MOVEFOCUS_FORWARD, "1");
  moveFocus(null, Services.focus.MOVEFOCUS_FORWARD, "2");
  let end = document.activeElement;
  moveFocus(null, Services.focus.MOVEFOCUS_BACKWARD, "1");
  moveFocus(null, Services.focus.MOVEFOCUS_BACKWARD, "0");

  moveFocus(start, Services.focus.MOVEFOCUS_FORWARD, "1");
  moveFocus(end, Services.focus.MOVEFOCUS_BACKWARD, "1");
</script>
</body>