summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/e10s/browser_events_textchange.js
blob: a1aed52d15abac7f9506d1f909e86754c1a338cd (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
/* 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/. */

/* global EVENT_TEXT_INSERTED, EVENT_TEXT_REMOVED,
          nsIAccessibleTextChangeEvent */

'use strict';

function checkTextChangeEvent(event, id, text, start, end, isInserted, isFromUserInput) {
  let tcEvent = event.QueryInterface(nsIAccessibleTextChangeEvent);
  is(tcEvent.start, start, `Correct start offset for ${prettyName(id)}`);
  is(tcEvent.length, end - start, `Correct length for ${prettyName(id)}`);
  is(tcEvent.isInserted, isInserted,
    `Correct isInserted flag for ${prettyName(id)}`);
  is(tcEvent.modifiedText, text, `Correct text for ${prettyName(id)}`);
  is(tcEvent.isFromUserInput, isFromUserInput,
    `Correct value of isFromUserInput for ${prettyName(id)}`);
}

function* changeText(browser, id, value, events) {
  let onEvents = waitForMultipleEvents(events.map(({ isInserted }) => {
    let eventType = isInserted ? EVENT_TEXT_INSERTED : EVENT_TEXT_REMOVED;
    return { id, eventType };
  }));
  // Change text in the subtree.
  yield ContentTask.spawn(browser, [id, value], ([contentId, contentValue]) => {
    content.document.getElementById(contentId).firstChild.textContent =
      contentValue;
  });
  let resolvedEvents = yield onEvents;

  events.forEach(({ isInserted, str, offset }, idx) =>
    checkTextChangeEvent(resolvedEvents[idx],
      id, str, offset, offset + str.length, isInserted, false));
}

function* removeTextFromInput(browser, id, value, start, end) {
  let onTextRemoved = waitForEvent(EVENT_TEXT_REMOVED, id);
  // Select text and delete it.
  yield ContentTask.spawn(browser, [id, start, end], ([contentId, contentStart, contentEnd]) => {
    let el = content.document.getElementById(contentId);
    el.focus();
    el.setSelectionRange(contentStart, contentEnd);
  });
  yield BrowserTestUtils.sendChar('VK_DELETE', browser);

  let event = yield onTextRemoved;
  checkTextChangeEvent(event, id, value, start, end, false, true);
}

/**
 * Test text change event and its interface:
 *   - start
 *   - length
 *   - isInserted
 *   - modifiedText
 *   - isFromUserInput
 */
addAccessibleTask(`
  <p id="p">abc</p>
  <input id="input" value="input" />`, function*(browser) {
  let events = [
    { isInserted: false, str: 'abc', offset: 0 },
    { isInserted: true, str: 'def', offset: 0 }
  ];
  yield changeText(browser, 'p', 'def', events);

  events = [{ isInserted: true, str: 'DEF', offset: 2 }];
  yield changeText(browser, 'p', 'deDEFf', events);

  // Test isFromUserInput property.
  yield removeTextFromInput(browser, 'input', 'n', 1, 2);
});