summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/aom/test_general.html
blob: dc63fb659bdb87c790bf2a59b293d99be83da001 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Accessibility API: generic</title>
  <link rel="stylesheet" type="text/css"
        href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script>
  "use strict";

  SimpleTest.waitForExplicitFinish();
  const finish = SimpleTest.finish.bind(SimpleTest);
  enablePref()
    .then(createIframe)
    .then(checkImplementation)
    .catch(err => {
      dump(`${err}: ${err.stack}`);
      finish();
    });

  function enablePref() {
    const ops = {
      "set": [
        [ "accessibility.AOM.enabled", true ],
      ],
    };
    return SpecialPowers.pushPrefEnv(ops);
  }

  // WebIDL conditional annotations for an interface are evaluated once per
  // global, so we need to create an iframe to see the effects of calling
  // enablePref().
  function createIframe() {
    return new Promise((resolve) => {
      let iframe = document.createElement("iframe");
      iframe.src = `data:text/html,<html><body>hey</body></html>`;
      iframe.onload = () => resolve(iframe.contentDocument);
      document.body.appendChild(iframe);
      document.body.offsetTop; // We rely on the a11y tree being created
                               // already, and it's created off layout.
    });
  }

  function testStringProp(anode, prop) {
    is(anode[prop], null, `anode.${prop} should be null`);
    let text = "This is a string test";
    anode[prop] = text;
    is(anode[prop], text, `anode.${prop} was assigned "${text}"`);
    anode[prop] = null;
    is(anode[prop], null, `anode.${prop} was assigned null`);
  }

  function testBoolProp(anode, prop) {
    is(anode[prop], null, `anode.${prop} should be null`);
    anode[prop] = true;
    is(anode[prop], true, `anode.${prop} was assigned true`);
    anode[prop] = false;
    is(anode[prop], false, `anode.${prop} was assigned false`);
    anode[prop] = null;
    is(anode[prop], null, `anode.${prop} was assigned null`);
  }

  function testDoubleProp(anode, prop) {
    is(anode[prop], null, `anode.${prop} should be null`);
    anode[prop] = Number.MAX_VALUE;
    is(anode[prop], Number.MAX_VALUE, `anode.${prop} was assigned ${Number.MAX_VALUE}`);
    anode[prop] = null;
    is(anode[prop], null, `anode.${prop} was assigned null`);
  }

  function testIntProp(anode, prop) {
    is(anode[prop], null, `anode.${prop} should be null`);
    anode[prop] = -1;
    is(anode[prop], -1, `anode.${prop} was assigned -1`);
    anode[prop] = null;
    is(anode[prop], null, `anode.${prop} was assigned null`);
  }

  function testUIntProp(anode, prop) {
    is(anode[prop], null, `anode.${prop} should be null`);
    anode[prop] = 4294967295;
    is(anode[prop], 4294967295, `anode.${prop} was assigned 4294967295`);
    anode[prop] = null;
    is(anode[prop], null, `anode.${prop} was assigned null`);
  }

  function testRelationProp(anode, node, prop) {
    is(anode[prop], null, `anode.${prop} should be null`);
    anode[prop] = node.accessibleNode;
    is(anode[prop], node.accessibleNode, `anode.${prop} was assigned AccessibleNode`);
    anode[prop] = null;
    is(anode[prop], null, `anode.${prop} was assigned null`);
  }
  // Check that the WebIDL is as expected.
  function checkImplementation(ifrDoc) {
    let anode = ifrDoc.accessibleNode;
    ok(anode, "DOM document has accessible node");

    is(anode.computedRole, "document", "correct role of a document accessible node");
    is(anode.DOMNode, ifrDoc, "correct DOM Node of a document accessible node");

    // States may differ depending on the document state, for example, if it is
    // loaded or is loading still.
    var states = null;
    switch (anode.states.length) {
      case 5:
        states = [
          "readonly", "focusable", "opaque", "enabled", "sensitive",
        ];
        break;
      case 6:
        states = [
          "readonly", "busy", "focusable", "opaque", "enabled", "sensitive",
        ];
        break;
      case 7:
        states = [
          "readonly", "busy", "focusable", "opaque", "stale", "enabled", "sensitive",
        ];
        break;
      default:
        ok(false, "Unexpected amount of states: " + JSON.stringify(anode.states));
    }
    if (states) {
      for (let i = 0; i < states.length; i++) {
        is(anode.states[i], states[i], `${states[i]} state is expected at ${i}th index`);
      }
    }

    ok(anode.is("document", "focusable"),
       "correct role and state on an accessible node");

    is(anode.get("explicit-name"), "true",
       "correct object attribute value on an accessible node");

    ok(anode.has("explicit-name"),
       "object attributes are present");

    var attrs = [ "explicit-name" ];
    if (anode.attributes.length > 1) {
      attrs = [
        "margin-left", "text-align", "text-indent", "margin-right",
        "tag", "margin-top", "margin-bottom", "display",
        "explicit-name",
      ];
    }

    is(anode.attributes.length, attrs.length, "correct number of attributes");
    for (let i = 0; i < attrs.length; i++) {
      ok(attrs.includes(anode.attributes[i]),
         `${anode.attributes[i]} attribute is expected and found`);
    }

    const strProps = ["autocomplete", "checked", "current", "hasPopUp", "invalid",
                      "keyShortcuts", "label", "live", "orientation", "placeholder",
                      "pressed", "relevant", "role", "roleDescription", "sort",
                      "valueText"];

    for (const strProp of strProps) {
      testStringProp(anode, strProp);
    }

    const boolProps = ["atomic", "busy", "disabled", "expanded", "hidden", "modal",
                       "multiline", "multiselectable", "readOnly", "required", "selected"];

    for (const boolProp of boolProps) {
      testBoolProp(anode, boolProp);
    }

    const doubleProps = ["valueMax", "valueMin", "valueNow"];

    for (const doubleProp of doubleProps) {
      testDoubleProp(anode, doubleProp);
    }

    const intProps = ["colCount", "rowCount", "setSize"];

    for (const intProp of intProps) {
      testIntProp(anode, intProp);
    }

    const uintProps = ["colIndex", "colSpan", "level", "posInSet", "rowIndex", "rowSpan"];

    for (const uintProp of uintProps) {
      testUIntProp(anode, uintProp);
    }

    // Check if an AccessibleNode is properly cached.
    let node = ifrDoc.createElement("div");
    anode = node.accessibleNode;
    is(anode, node.accessibleNode, "an AccessibleNode is properly cached");

    // Adopting node to another document doesn't change .accessibleNode
    let anotherDoc = ifrDoc.implementation.createDocument("", "", null);
    let adopted_node = anotherDoc.adoptNode(node);
    is(anode, adopted_node.accessibleNode, "adopting node to another document doesn't change node.accessibleNode");

    const relationProps = ["activeDescendant", "details", "errorMessage"];

    for (const relationProp of relationProps) {
      testRelationProp(anode, node, relationProp);
    }

    finish();
  }
  </script>
</head>