summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/mac/browser_input.js
blob: 7fa20a9d4b6aa66766522f6b00f99052506fceb2 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

function selectedTextEventPromises(stateChangeType) {
  return [
    waitForMacEventWithInfo("AXSelectedTextChanged", (elem, info) => {
      return (
        info.AXTextStateChangeType == stateChangeType &&
        elem.getAttributeValue("AXDOMIdentifier") == "body"
      );
    }),
    waitForMacEventWithInfo("AXSelectedTextChanged", (elem, info) => {
      return (
        info.AXTextStateChangeType == stateChangeType &&
        elem.getAttributeValue("AXDOMIdentifier") == "input"
      );
    }),
  ];
}

async function testInput(browser, accDoc) {
  let input = getNativeInterface(accDoc, "input");

  is(input.getAttributeValue("AXDescription"), "Name", "Correct input label");
  is(input.getAttributeValue("AXTitle"), "", "Correct input title");
  is(input.getAttributeValue("AXValue"), "Elmer Fudd", "Correct input value");
  is(
    input.getAttributeValue("AXNumberOfCharacters"),
    10,
    "Correct length of value"
  );

  ok(input.attributeNames.includes("AXSelectedText"), "Has AXSelectedText");
  ok(
    input.attributeNames.includes("AXSelectedTextRange"),
    "Has AXSelectedTextRange"
  );

  let evt = Promise.all([
    waitForMacEvent("AXFocusedUIElementChanged", "input"),
    ...selectedTextEventPromises(AXTextStateChangeTypeSelectionMove),
  ]);
  await SpecialPowers.spawn(browser, [], () => {
    content.document.getElementById("input").focus();
  });
  await evt;

  evt = Promise.all(
    selectedTextEventPromises(AXTextStateChangeTypeSelectionExtend)
  );
  await SpecialPowers.spawn(browser, [], () => {
    let elm = content.document.getElementById("input");
    if (elm.setSelectionRange) {
      elm.setSelectionRange(6, 9);
    } else {
      let r = new content.Range();
      let textNode = elm.firstElementChild.firstChild;
      r.setStart(textNode, 6);
      r.setEnd(textNode, 9);

      let s = content.getSelection();
      s.removeAllRanges();
      s.addRange(r);
    }
  });
  await evt;

  is(
    input.getAttributeValue("AXSelectedText"),
    "Fud",
    "Correct text is selected"
  );

  Assert.deepEqual(
    input.getAttributeValue("AXSelectedTextRange"),
    [6, 3],
    "correct range selected"
  );

  ok(
    input.isAttributeSettable("AXSelectedTextRange"),
    "AXSelectedTextRange is settable"
  );

  evt = Promise.all(
    selectedTextEventPromises(AXTextStateChangeTypeSelectionExtend)
  );
  input.setAttributeValue("AXSelectedTextRange", NSRange(1, 7));
  await evt;

  Assert.deepEqual(
    input.getAttributeValue("AXSelectedTextRange"),
    [1, 7],
    "correct range selected"
  );

  is(
    input.getAttributeValue("AXSelectedText"),
    "lmer Fu",
    "Correct text is selected"
  );

  let domSelection = await SpecialPowers.spawn(browser, [], () => {
    let elm = content.document.querySelector("input#input");
    if (elm) {
      return elm.value.substring(elm.selectionStart, elm.selectionEnd);
    }

    return content.getSelection().toString();
  });

  is(domSelection, "lmer Fu", "correct DOM selection");

  is(
    input.getParameterizedAttributeValue("AXStringForRange", NSRange(3, 5)),
    "er Fu",
    "AXStringForRange works"
  );
}

/**
 * Input selection test
 */
addAccessibleTask(
  `<input aria-label="Name" id="input" value="Elmer Fudd">`,
  testInput
);

/**
 * contenteditable selection test
 */
addAccessibleTask(
  `<div aria-label="Name" tabindex="0" role="textbox" aria-multiline="true" id="input" contenteditable>
     <p>Elmer Fudd</p>
   </div>`,
  testInput
);

/**
 * test contenteditable with selection that extends past editable part
 */
addAccessibleTask(
  `<span aria-label="Name"
         tabindex="0"
         role="textbox"
         id="input"
         contenteditable>Elmer Fudd</span> <span id="notinput">is the name</span>`,
  async (browser, accDoc) => {
    let evt = Promise.all([
      waitForMacEvent("AXFocusedUIElementChanged", "input"),
      waitForMacEvent("AXSelectedTextChanged", "body"),
      waitForMacEvent("AXSelectedTextChanged", "input"),
    ]);
    await SpecialPowers.spawn(browser, [], () => {
      content.document.getElementById("input").focus();
    });
    await evt;

    evt = waitForEvent(EVENT_TEXT_CARET_MOVED);
    await SpecialPowers.spawn(browser, [], () => {
      let input = content.document.getElementById("input");
      let notinput = content.document.getElementById("notinput");

      let r = new content.Range();
      r.setStart(input.firstChild, 4);
      r.setEnd(notinput.firstChild, 6);

      let s = content.getSelection();
      s.removeAllRanges();
      s.addRange(r);
    });
    await evt;

    let input = getNativeInterface(accDoc, "input");

    is(
      input.getAttributeValue("AXSelectedText"),
      "r Fudd",
      "Correct text is selected in #input"
    );

    is(
      stringForRange(
        input,
        input.getAttributeValue("AXSelectedTextMarkerRange")
      ),
      "r Fudd is the",
      "Correct text is selected in document"
    );
  }
);

/**
 * test nested content editables and their ancestor getters.
 */
addAccessibleTask(
  `<div id="outer" role="textbox" contenteditable="true">
     <p id="p">Bob <a href="#" id="link">Loblaw's</a></p>
     <div id="inner" role="textbox" contenteditable="true">
       Law <a href="#" id="inner_link">Blog</a>
     </div>
   </div>`,
  (browser, accDoc) => {
    let link = getNativeInterface(accDoc, "link");
    let innerLink = getNativeInterface(accDoc, "inner_link");

    let idmatches = (elem, id) => {
      is(elem.getAttributeValue("AXDOMIdentifier"), id, "Matches ID");
    };

    idmatches(link.getAttributeValue("AXEditableAncestor"), "outer");
    idmatches(link.getAttributeValue("AXFocusableAncestor"), "outer");
    idmatches(link.getAttributeValue("AXHighestEditableAncestor"), "outer");

    idmatches(innerLink.getAttributeValue("AXEditableAncestor"), "inner");
    idmatches(innerLink.getAttributeValue("AXFocusableAncestor"), "inner");
    idmatches(
      innerLink.getAttributeValue("AXHighestEditableAncestor"),
      "outer"
    );
  }
);