summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/test_dialogfocus.xhtml
blob: 10e60ac77612c3d726b0b22adf5537b8728d3c98 (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
<?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 width="500" height="600"
        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"/>

<button id="test" label="Test"/>

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

<script>
<![CDATA[

SimpleTest.waitForExplicitFinish();
SimpleTest.requestCompleteLog();

var expected = [ "one", "_extra2", "tab", "one", "tabbutton2", "tabbutton", "two", "textbox-yes", "one", "root" ];
// non-Mac will always focus the default button if any of the dialog buttons
// would be focused
if (!navigator.platform.includes("Mac"))
  expected[1] = "_accept";

let extraDialog = "data:application/xhtml+xml,<window id='root'><dialog " +
                  "buttons='none' " +
                  "xmlns='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'>" +
                  "<button id='nonbutton' noinitialfocus='true'/></dialog></window>";

var step = 0;
var fullKeyboardAccess = false;

function startTest()
{
  var testButton = document.getElementById("test");
  synthesizeKey("KEY_Tab");
  fullKeyboardAccess = (document.activeElement == testButton);
  info("We " + (fullKeyboardAccess ? "have" : "don't have") + " full keyboard access");
  runTest();
}

function runTest()
{
  step++;
  info("runTest(), step = " + step + ", expected = " + expected[step - 1]);
  if (step > expected.length || (!fullKeyboardAccess && step == 2)) {
    info("finishing");
    SimpleTest.finish();
    return;
  }

  var expectedFocus = expected[step - 1];
  let filename = expectedFocus == "root" ? "dialog_dialogfocus2.xhtml" : "dialog_dialogfocus.xhtml";
  var win = window.browsingContext.topChromeWindow.openDialog(filename, "_new", "chrome,dialog", step);

  function checkDialogFocus(event)
  {
    info(`checkDialogFocus()`);
    let match = false;
    let activeElement = win.document.activeElement;
    let dialog = win.document.getElementById("dialog-focus");

    if (activeElement == dialog) {
      let shadowActiveElement =
        dialog.shadowRoot.activeElement;
      if (shadowActiveElement) {
        activeElement = shadowActiveElement;
      }
    }
    // if full keyboard access is not on, just skip the tests
    if (fullKeyboardAccess) {
      if (!(Element.isInstance(event.target))) {
        info("target not an Element");
        return;
      }

      if (expectedFocus[0] == "_")
        match = (activeElement.getAttribute("dlgtype") == expectedFocus.substring(1));
      else
        match = (activeElement.id == expectedFocus);
      info("match = " + match);
      if (!match)
        return;
    }
    else {
      match = (activeElement == win.document.documentElement);
      info("match = " + match);
    }

    win.removeEventListener("focus", checkDialogFocusEvent, true);
    dialog.shadowRoot.removeEventListener(
      "focus", checkDialogFocusEvent, true);
    ok(match, "focus step " + step);

    win.close();
    SimpleTest.waitForFocus(runTest, window);
  }

  let finalCheckInitiated = false;
  function checkDialogFocusEvent(event) {
    // Delay to have time for focus/blur to occur.
    if (expectedFocus == "root") {
      if (!finalCheckInitiated) {
        setTimeout(() => {
          is(win.document.activeElement, win.document.documentElement,
             "No other focus but root");
          win.close();
          SimpleTest.waitForFocus(runTest, window);
        }, 0);
        finalCheckInitiated = true;
      }
    } else {
      checkDialogFocus(event);
    }
  }
  win.addEventListener("focus", checkDialogFocusEvent, true);
  win.addEventListener("load", () => {
    win.document.getElementById("dialog-focus").shadowRoot.addEventListener(
      "focus", checkDialogFocusEvent, true);
  });
}

SimpleTest.waitForFocus(startTest, window);

]]>

</script>

</window>