summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/mac/browser_text_selection.js
blob: 7e2145631c29aa6a9b92bd646f3c9d4ce08c83be (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
/* 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";

/**
 * Test simple text selection
 */
addAccessibleTask(`<p id="p">Hello World</p>`, async (browser, accDoc) => {
  let macDoc = accDoc.nativeInterface.QueryInterface(
    Ci.nsIAccessibleMacInterface
  );

  let startMarker = macDoc.getAttributeValue("AXStartTextMarker");
  let endMarker = macDoc.getAttributeValue("AXEndTextMarker");
  let range = macDoc.getParameterizedAttributeValue(
    "AXTextMarkerRangeForUnorderedTextMarkers",
    [startMarker, endMarker]
  );
  is(stringForRange(macDoc, range), "Hello World");

  let evt = waitForMacEventWithInfo("AXSelectedTextChanged", (elem, info) => {
    return (
      !info.AXTextStateSync &&
      info.AXTextStateChangeType == AXTextStateChangeTypeSelectionExtend &&
      elem.getAttributeValue("AXRole") == "AXWebArea"
    );
  });
  await SpecialPowers.spawn(browser, [], () => {
    let p = content.document.getElementById("p");
    let r = new content.Range();
    r.setStart(p.firstChild, 1);
    r.setEnd(p.firstChild, 8);

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

  range = macDoc.getAttributeValue("AXSelectedTextMarkerRange");
  is(stringForRange(macDoc, range), "ello Wo");

  let firstWordRange = macDoc.getParameterizedAttributeValue(
    "AXRightWordTextMarkerRangeForTextMarker",
    startMarker
  );
  is(stringForRange(macDoc, firstWordRange), "Hello");

  evt = waitForMacEventWithInfo("AXSelectedTextChanged", (elem, info) => {
    return (
      !info.AXTextStateSync &&
      info.AXTextStateChangeType == AXTextStateChangeTypeSelectionExtend &&
      elem.getAttributeValue("AXRole") == "AXWebArea"
    );
  });
  macDoc.setAttributeValue("AXSelectedTextMarkerRange", firstWordRange);
  await evt;
  range = macDoc.getAttributeValue("AXSelectedTextMarkerRange");
  is(stringForRange(macDoc, range), "Hello");

  // Collapse selection
  evt = waitForMacEventWithInfo("AXSelectedTextChanged", (elem, info) => {
    return (
      info.AXTextStateSync &&
      info.AXTextStateChangeType == AXTextStateChangeTypeSelectionMove &&
      elem.getAttributeValue("AXRole") == "AXWebArea"
    );
  });
  await SpecialPowers.spawn(browser, [], () => {
    let s = content.getSelection();
    s.collapseToEnd();
  });
  await evt;
});

/**
 * Test text selection events caused by focus change
 */
addAccessibleTask(
  `<p>
  Hello <a href="#" id="link">World</a>,
  I <a href="#" style="user-select: none;" id="unselectable_link">love</a>
  <button id="button">you</button></p>`,
  async browser => {
    // Set up an AXSelectedTextChanged listener here. It will get resolved
    // on the first non-root event it encounters, so if we test its data at the end
    // of this test it will show us the first text-selectable object that was focused,
    // which is "link".
    let selTextChanged = waitForMacEvent(
      "AXSelectedTextChanged",
      e => e.getAttributeValue("AXDOMIdentifier") != "body"
    );

    let focusChanged = waitForMacEvent("AXFocusedUIElementChanged");
    await SpecialPowers.spawn(browser, [], () => {
      content.document.getElementById("unselectable_link").focus();
    });
    let focusChangedTarget = await focusChanged;
    is(
      focusChangedTarget.getAttributeValue("AXDOMIdentifier"),
      "unselectable_link",
      "Correct event target"
    );

    focusChanged = waitForMacEvent("AXFocusedUIElementChanged");
    await SpecialPowers.spawn(browser, [], () => {
      content.document.getElementById("button").focus();
    });
    focusChangedTarget = await focusChanged;
    is(
      focusChangedTarget.getAttributeValue("AXDOMIdentifier"),
      "button",
      "Correct event target"
    );

    focusChanged = waitForMacEvent("AXFocusedUIElementChanged");
    await SpecialPowers.spawn(browser, [], () => {
      content.document.getElementById("link").focus();
    });
    focusChangedTarget = await focusChanged;
    is(
      focusChangedTarget.getAttributeValue("AXDOMIdentifier"),
      "link",
      "Correct event target"
    );

    let selTextChangedTarget = await selTextChanged;
    is(
      selTextChangedTarget.getAttributeValue("AXDOMIdentifier"),
      "link",
      "Correct event target"
    );
  }
);

/**
 * Test text selection with focus change
 */
addAccessibleTask(
  `<p id="p">Hello <input id="input"></p>`,
  async (browser, accDoc) => {
    let macDoc = accDoc.nativeInterface.QueryInterface(
      Ci.nsIAccessibleMacInterface
    );

    let evt = waitForMacEventWithInfo("AXSelectedTextChanged", (elem, info) => {
      return (
        !info.AXTextStateSync &&
        info.AXTextStateChangeType == AXTextStateChangeTypeSelectionExtend &&
        elem.getAttributeValue("AXRole") == "AXWebArea"
      );
    });
    await SpecialPowers.spawn(browser, [], () => {
      let p = content.document.getElementById("p");
      let r = new content.Range();
      r.setStart(p.firstChild, 1);
      r.setEnd(p.firstChild, 3);

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

    let range = macDoc.getAttributeValue("AXSelectedTextMarkerRange");
    is(stringForRange(macDoc, range), "el");

    let events = Promise.all([
      waitForMacEvent("AXFocusedUIElementChanged"),
      waitForMacEventWithInfo("AXSelectedTextChanged"),
    ]);
    await SpecialPowers.spawn(browser, [], () => {
      content.document.getElementById("input").focus();
    });
    let [, { data }] = await events;
    ok(
      data.AXTextSelectionChangedFocus,
      "have AXTextSelectionChangedFocus in event info"
    );
    ok(!data.AXTextStateSync, "no AXTextStateSync in editables");
    is(
      data.AXTextSelectionDirection,
      AXTextSelectionDirectionDiscontiguous,
      "discontigous direction"
    );
  }
);