summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/events/browser_test_textcaret.js
blob: d5065c81f3d5bd46037ed93e2ac49775809b9086 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Caret move events checker.
 */
function caretMoveChecker(target, caretOffset) {
  return function (event) {
    let cmEvent = event.QueryInterface(nsIAccessibleCaretMoveEvent);
    return (
      cmEvent.accessible == getAccessible(target) &&
      cmEvent.caretOffset == caretOffset
    );
  };
}

async function checkURLBarCaretEvents() {
  const kURL = "about:mozilla";
  let newWin = await BrowserTestUtils.openNewBrowserWindow();
  BrowserTestUtils.loadURIString(newWin.gBrowser.selectedBrowser, kURL);
  newWin.gBrowser.selectedBrowser.focus();

  await waitForEvent(EVENT_DOCUMENT_LOAD_COMPLETE, event => {
    try {
      return event.accessible.QueryInterface(nsIAccessibleDocument).URL == kURL;
    } catch (e) {
      return false;
    }
  });
  info("Loaded " + kURL);

  let urlbarInputEl = newWin.gURLBar.inputField;
  let urlbarInput = getAccessible(urlbarInputEl, [nsIAccessibleText]);

  let onCaretMove = waitForEvents([
    [EVENT_TEXT_CARET_MOVED, caretMoveChecker(urlbarInput, kURL.length)],
    [EVENT_FOCUS, urlbarInput],
  ]);

  urlbarInput.caretOffset = -1;
  await onCaretMove;
  ok(true, "Caret move in URL bar #1");

  onCaretMove = waitForEvent(
    EVENT_TEXT_CARET_MOVED,
    caretMoveChecker(urlbarInput, 0)
  );

  urlbarInput.caretOffset = 0;
  await onCaretMove;
  ok(true, "Caret move in URL bar #2");

  await BrowserTestUtils.closeWindow(newWin);
}

add_task(checkURLBarCaretEvents);