Bug 1665751 Part 3: Rewrite textSelection/test_general.html to use async await instead of event queue, r=yzen DONTBUILD

Differential Revision: https://phabricator.services.mozilla.com/D90860
This commit is contained in:
Marco Zehe 2020-09-21 18:34:45 +00:00
Родитель df24528bee
Коммит c73c6f8406
2 изменённых файлов: 140 добавлений и 152 удалений

Просмотреть файл

@ -40,6 +40,8 @@ const EVENT_DOCUMENT_RELOAD = nsIAccessibleEvent.EVENT_DOCUMENT_RELOAD;
const EVENT_VIRTUALCURSOR_CHANGED =
nsIAccessibleEvent.EVENT_VIRTUALCURSOR_CHANGED;
const EVENT_ALERT = nsIAccessibleEvent.EVENT_ALERT;
const EVENT_TEXT_SELECTION_CHANGED =
nsIAccessibleEvent.EVENT_TEXT_SELECTION_CHANGED;
const EventsLogger = {
enabled: false,

Просмотреть файл

@ -11,196 +11,182 @@
<script type="application/javascript"
src="../common.js"></script>
<script type="application/javascript"
src="../events.js"></script>
src="../promisified-events.js"></script>
<script type="application/javascript">
/**
* Invokers
* Helper function to test selection bounds.
* @param {string} aID The ID to test.
* @param {nsIAccessibleText} acc The accessible to test.
* @param {int} index The selection's index to test.
* @param {array} offsets The start and end offset to test against.
* @param {string} msgStart The start of the message to return in test
* messages.
*/
function addSelections(aID, aSelections) {
this.hyperTextNode = getNode(aID);
this.hyperText = getAccessible(aID, [ nsIAccessibleText ]);
this.initialSelectionCount = this.hyperText.selectionCount;
function testSelectionBounds(aID, acc, index, offsets, msgStart) {
const [expectedStart, expectedEnd] = offsets;
const startOffset = {}, endOffset = {};
acc.getSelectionBounds(index, startOffset, endOffset);
is(startOffset.value, Math.min(expectedStart, expectedEnd),
msgStart + ": Wrong start offset for " + aID);
is(endOffset.value, Math.max(expectedStart, expectedEnd),
msgStart + ": Wrong end offset for " + aID);
}
/**
* Test adding selections to accessibles.
* @param {string} aID The ID of the element to test.
* @param {array} aSelections Array of selection start and end indices.
*/
async function addSelections(aID, aSelections) {
info("Test adding selections to " + aID);
const hyperText = getAccessible(aID, [ nsIAccessibleText ]);
const initialSelectionCount = hyperText.selectionCount;
// Multiple selection changes will be coalesced, so just listen for one.
this.eventSeq = [
new invokerChecker(EVENT_TEXT_SELECTION_CHANGED, aID),
];
const selectionChange = waitForEvent(EVENT_TEXT_SELECTION_CHANGED, aID);
for (let [startOffset, endOffset] of aSelections) {
hyperText.addSelection(startOffset, endOffset);
}
await selectionChange;
this.invoke = function addSelection_invoke() {
for (let [startOffset, endOffset] of aSelections) {
this.hyperText.addSelection(startOffset, endOffset);
}
};
is(hyperText.selectionCount,
aSelections.length + initialSelectionCount,
"addSelection: Wrong selection count for " + aID);
this.finalCheck = function addSelection_finalCheck() {
is(this.hyperText.selectionCount,
aSelections.length + this.initialSelectionCount,
"addSelection: Wrong selection count for " + aID);
for (let i in aSelections) {
testSelectionBounds(aID, hyperText, initialSelectionCount + i,
aSelections[i], "addSelection");
}
for (let i in aSelections) {
let [expectedStart, expectedEnd] = aSelections[i];
let startOffset = {}, endOffset = {};
this.hyperText.getSelectionBounds(this.initialSelectionCount + i,
startOffset, endOffset);
is(startOffset.value, Math.min(expectedStart, expectedEnd),
"addSelection: Wrong start offset for " + aID);
is(endOffset.value, Math.max(expectedStart, expectedEnd),
"addSelection: Wrong end offset for " + aID);
if (i == this.hyperText.selectionCount - 1) {
is(this.hyperText.caretOffset, expectedEnd,
"addSelection: caretOffset not at selection end for " + aID);
}
}
};
this.getID = function addSelection_getID() {
return "nsIAccessibleText::addSelection test for " + aID;
};
is(hyperText.caretOffset, aSelections[hyperText.selectionCount -1][1],
"addSelection: caretOffset not at selection end for " + aID);
}
function changeSelection(aID, aIndex, aSelection) {
let [startOffset, endOffset] = aSelection;
this.hyperTextNode = getNode(aID);
this.hyperText = getAccessible(aID, [ nsIAccessibleText ]);
/**
* Test changing selections in accessibles.
* @param {string} aID The ID of the element to test.
* @param {int} aIndex The index of the selection to change.
* @param {array} aSelection Array of the selection's new start and end
* indices.
*/
async function changeSelection(aID, aIndex, aSelection) {
info("Test changing the selection of " + aID + " at index " + aIndex);
const [startOffset, endOffset] = aSelection;
const hyperText = getAccessible(aID, [ nsIAccessibleText ]);
this.eventSeq = [
new invokerChecker(EVENT_TEXT_SELECTION_CHANGED, aID),
];
const selectionChanged = waitForEvent(EVENT_TEXT_SELECTION_CHANGED, aID);
hyperText.setSelectionBounds(aIndex, startOffset, endOffset);
await selectionChanged;
this.invoke = function changeSelection_invoke() {
this.hyperText.setSelectionBounds(aIndex, startOffset, endOffset);
};
testSelectionBounds(aID, hyperText, aIndex,
aSelection, "setSelectionBounds");
this.finalCheck = function changeSelection_finalCheck() {
var start = {}, end = {};
this.hyperText.getSelectionBounds(aIndex, start, end);
is(start.value, Math.min(startOffset, endOffset),
"setSelectionBounds: Wrong start offset for " + aID);
is(end.value, Math.max(startOffset, endOffset),
"setSelectionBounds: Wrong end offset for " + aID);
is(this.hyperText.caretOffset, endOffset,
"setSelectionBounds: caretOffset not at selection end for " + aID);
};
this.getID = function changeSelection_getID() {
return "nsIAccessibleText::setSelectionBounds test for " + aID;
};
is(hyperText.caretOffset, endOffset,
"setSelectionBounds: caretOffset not at selection end for " + aID);
}
function removeSelections(aID) {
this.hyperText = getAccessible(aID, [ nsIAccessibleText ]);
/**
* Test removing all selections from accessibles.
* @param {string} aID The ID of the element to test.
*/
async function removeSelections(aID) {
info("Testing removal of all selections from " + aID);
const hyperText = getAccessible(aID, [ nsIAccessibleText ]);
this.eventSeq = [
new invokerChecker(EVENT_TEXT_SELECTION_CHANGED, document),
];
let selectionsRemoved = waitForEvent(EVENT_TEXT_SELECTION_CHANGED, document);
const selectionCount = hyperText.selectionCount;
for (let i = 0; i < selectionCount; i++) {
hyperText.removeSelection(0);
}
await selectionsRemoved;
this.invoke = function removeSelection_invoke() {
let selectionCount = this.hyperText.selectionCount;
for (let i = 0; i < selectionCount; i++) {
this.hyperText.removeSelection(0);
}
};
this.finalCheck = function removeSelection_finalCheck() {
is(this.hyperText.selectionCount, 0,
"removeSelection: Wrong selection count for " + aID);
};
this.getID = function removeSelection_getID() {
return "nsIAccessibleText::removeSelection test for " + aID;
};
is(hyperText.selectionCount, 0,
"removeSelection: Wrong selection count for " + aID);
}
function changeDOMSelection(aID, aNodeID1, aNodeOffset1,
aNodeID2, aNodeOffset2,
aTests) {
this.hyperText = getAccessible(aID, [ nsIAccessibleText ]);
/**
* Test that changing the DOM selection is reflected in the accessibles.
* @param {string} aID The container ID to test in
* @param {string} aNodeID1 The start node of the selection
* @param {int} aNodeOffset1 The offset where the selection should start
* @param {string} aNodeID2 The node in which the selection should end
* @param {int} aNodeOffset2 The index at which the selection should end
* @param {array} aTests An array of accessibles and their start and end
* offsets to test.
*/
async function changeDOMSelection(aID, aNodeID1, aNodeOffset1,
aNodeID2, aNodeOffset2,
aTests) {
info("Test that DOM selection changes are reflected in the accessibles");
this.eventSeq = [
new invokerChecker(EVENT_TEXT_SELECTION_CHANGED, aID),
];
let selectionChanged = waitForEvent(EVENT_TEXT_SELECTION_CHANGED, aID);
// HyperTextAccessible::GetSelectionDOMRanges ignores hidden selections.
// Here we may be focusing an editable element (and thus hiding the
// main document selection), so blur it so that we test what we want to
// test.
document.activeElement.blur();
this.invoke = function changeDOMSelection_invoke() {
// HyperTextAccessible::GetSelectionDOMRanges ignores hidden selections.
// Here we may be focusing an editable element (and thus hiding the
// main document selection), so blur it so that we test what we want to
// test.
document.activeElement.blur();
const sel = window.getSelection();
const range = document.createRange();
range.setStart(getNode(aNodeID1), aNodeOffset1);
range.setEnd(getNode(aNodeID2), aNodeOffset2);
sel.addRange(range);
await selectionChanged;
var sel = window.getSelection();
var range = document.createRange();
range.setStart(getNode(aNodeID1), aNodeOffset1);
range.setEnd(getNode(aNodeID2), aNodeOffset2);
sel.addRange(range);
};
this.finalCheck = function changeDOMSelection_finalCheck() {
for (var i = 0; i < aTests.length; i++) {
var text = getAccessible(aTests[i][0], nsIAccessibleText);
is(text.selectionCount, 1,
"setSelectionBounds: Wrong selection count for " + aID);
var startOffset = {}, endOffset = {};
text.getSelectionBounds(0, startOffset, endOffset);
is(startOffset.value, aTests[i][1],
"setSelectionBounds: Wrong start offset for " + aID);
is(endOffset.value, aTests[i][2],
"setSelectionBounds: Wrong end offset for " + aID);
}
};
this.getID = function changeDOMSelection_getID() {
return "DOM selection change for " + aID;
};
for (let i = 0; i < aTests.length; i++) {
const text = getAccessible(aTests[i][0], nsIAccessibleText);
is(text.selectionCount, 1,
"setSelectionBounds: Wrong selection count for " + aID);
testSelectionBounds(aID, text, 0, [aTests[i][1], aTests[i][2]],
"setSelectionBounds");
}
}
function onfocusEventSeq(aID) {
var caretMovedChecker =
new invokerChecker(EVENT_TEXT_CARET_MOVED, aID);
var selChangedChecker =
new invokerChecker(EVENT_TEXT_SELECTION_CHANGED, aID);
selChangedChecker.unexpected = true;
return [ caretMovedChecker, selChangedChecker ];
/**
* Test expected and unexpected events for selecting
* all text and focusing both an input and text area. We expect a caret
* move, but not a text selection change.
* @param {string} aID The ID of the element to test.
*/
async function eventsForSelectingAllTextAndFocus(aID) {
info("Test expected caretMove and unexpected textSelection events for " +aID);
let events = waitForEvents({
expected: [[EVENT_TEXT_CARET_MOVED, aID]],
unexpected: [[EVENT_TEXT_SELECTION_CHANGED, aID]]}, aID);
selectAllTextAndFocus(aID);
await events;
}
/**
* Do tests
*/
// gA11yEventDumpToConsole = true; // debug stuff
var gQueue = null;
function doTests() {
gQueue = new eventQueue();
gQueue.push(new addSelections("paragraph", [[1, 3], [6, 10]]));
gQueue.push(new changeSelection("paragraph", 0, [2, 4]));
gQueue.push(new removeSelections("paragraph"));
async function doTests() {
await addSelections("paragraph", [[1, 3], [6, 10]]);
await changeSelection("paragraph", 0, [2, 4]);
await removeSelections("paragraph");
// reverse selection
gQueue.push(new addSelections("paragraph", [[1, 3], [10, 6]]));
gQueue.push(new removeSelections("paragraph"));
await addSelections("paragraph", [[1, 3], [10, 6]]);
await removeSelections("paragraph");
gQueue.push(new synthFocus("textbox", onfocusEventSeq("textbox")));
gQueue.push(new changeSelection("textbox", 0, [1, 3]));
await eventsForSelectingAllTextAndFocus("textbox");
await changeSelection("textbox", 0, [1, 3]);
// reverse selection
gQueue.push(new changeSelection("textbox", 0, [3, 1]));
await changeSelection("textbox", 0, [3, 1]);
gQueue.push(new synthFocus("textarea", onfocusEventSeq("textarea")));
gQueue.push(new changeSelection("textarea", 0, [1, 3]));
await eventsForSelectingAllTextAndFocus("textarea");
await changeSelection("textarea", 0, [1, 3]);
gQueue.push(new changeDOMSelection("c1", "c1_span1", 0, "c1_span2", 0,
[["c1", 2, 2]]));
gQueue.push(new changeDOMSelection("c2", "c2", 0, "c2_div2", 1,
[["c2", 0, 3], ["c2_div2", 0, 2]]));
gQueue.invoke(); // Will call SimpleTest.finish();
await changeDOMSelection("c1", "c1_span1", 0, "c1_span2", 0,
[["c1", 2, 2]]);
await changeDOMSelection("c2", "c2", 0, "c2_div2", 1,
[["c2", 0, 3], ["c2_div2", 0, 2]]);
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();