Bug 1289528, add tests for keyboard and mouse selection of options, r=mconley

This commit is contained in:
Neil Deakin 2016-08-03 07:45:45 -04:00
Родитель a59eab8dbf
Коммит 16acfd6d48
1 изменённых файлов: 83 добавлений и 53 удалений

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

@ -60,16 +60,19 @@ function openSelectPopup(selectPopup, withMouse, selector = "select")
return popupShownPromise;
}
function hideSelectPopup(selectPopup, withEscape)
function hideSelectPopup(selectPopup, mode = "enter")
{
let popupHiddenPromise = BrowserTestUtils.waitForEvent(selectPopup, "popuphidden");
if (withEscape) {
if (mode == "escape") {
EventUtils.synthesizeKey("KEY_Escape", { code: "Escape" });
}
else {
else if (mode == "enter") {
EventUtils.synthesizeKey("KEY_Enter", { code: "Enter" });
}
else if (mode == "click") {
EventUtils.synthesizeMouseAtCenter(selectPopup.lastChild, { });
}
return popupHiddenPromise;
}
@ -148,7 +151,7 @@ function doSelectTests(contentType, dtd)
// Opening and closing the popup without changing the value should not fire a change event.
yield openSelectPopup(selectPopup, true);
yield hideSelectPopup(selectPopup, true);
yield hideSelectPopup(selectPopup, "escape");
is((yield getInputEvents()), 1, "Open and close with no change - number of input events");
is((yield getChangeEvents()), 1, "Open and close with no change - number of change events");
EventUtils.synthesizeKey("VK_TAB", { });
@ -158,7 +161,7 @@ function doSelectTests(contentType, dtd)
yield openSelectPopup(selectPopup, true);
EventUtils.synthesizeKey("KEY_ArrowDown", { code: "ArrowDown" });
yield hideSelectPopup(selectPopup, true);
yield hideSelectPopup(selectPopup, "escape");
is((yield getInputEvents()), isWindows ? 2 : 1, "Open and close with change - number of input events");
is((yield getChangeEvents()), isWindows ? 2 : 1, "Open and close with change - number of change events");
EventUtils.synthesizeKey("VK_TAB", { });
@ -299,59 +302,86 @@ add_task(function* test_event_order() {
let menulist = document.getElementById("ContentSelectDropdown");
let selectPopup = menulist.menupopup;
yield openSelectPopup(selectPopup, true, "#one");
// According to https://html.spec.whatwg.org/#the-select-element,
// we want to fire input, change, and then click events on the
// <select> (in that order) when it has changed.
let expectedEnter = [
{
type: "input",
cancelable: false,
targetIsOption: false,
},
{
type: "change",
cancelable: false,
targetIsOption: false,
},
];
let eventsPromise = ContentTask.spawn(browser, null, function*() {
// According to https://html.spec.whatwg.org/#the-select-element,
// we want to fire input, change, and then click events on the
// <select> (in that order) when it has changed.
let expected = [
{
type: "input",
cancelable: false,
},
{
type: "change",
cancelable: false,
},
{
type: "mousedown",
cancelable: true,
},
{
type: "mouseup",
cancelable: true,
},
{
type: "click",
cancelable: true,
},
];
let expectedClick = [
{
type: "input",
cancelable: false,
targetIsOption: false,
},
{
type: "change",
cancelable: false,
targetIsOption: false,
},
{
type: "mousedown",
cancelable: true,
targetIsOption: true,
},
{
type: "mouseup",
cancelable: true,
targetIsOption: true,
},
{
type: "click",
cancelable: true,
targetIsOption: true,
},
];
return new Promise((resolve) => {
function onEvent(event) {
select.removeEventListener(event.type, onEvent);
let expectation = expected.shift();
Assert.equal(event.type, expectation.type,
"Expected the right event order");
Assert.ok(event.bubbles, "All of these events should bubble");
Assert.equal(event.cancelable, expectation.cancelable,
"Cancellation property should match");
if (!expected.length) {
resolve();
for (let mode of ["enter", "click"]) {
let expected = mode == "enter" ? expectedEnter : expectedClick;
yield openSelectPopup(selectPopup, true, mode == "enter" ? "#one" : "#two");
let eventsPromise = ContentTask.spawn(browser, { mode, expected }, function*(args) {
let expected = args.expected;
return new Promise((resolve) => {
function onEvent(event) {
select.removeEventListener(event.type, onEvent);
Assert.ok(expected.length, "Unexpected event " + event.type);
let expectation = expected.shift();
Assert.equal(event.type, expectation.type,
"Expected the right event order");
Assert.ok(event.bubbles, "All of these events should bubble");
Assert.equal(event.cancelable, expectation.cancelable,
"Cancellation property should match");
Assert.equal(event.target.localName,
expectation.targetIsOption ? "option" : "select",
"Target matches");
if (!expected.length) {
resolve();
}
}
}
let select = content.document.getElementById("one");
for (let expectation of expected) {
select.addEventListener(expectation.type, onEvent);
}
let select = content.document.getElementById(args.mode == "enter" ? "one" : "two");
for (let event of ["input", "change", "mousedown", "mouseup", "click"]) {
select.addEventListener(event, onEvent);
}
});
});
});
EventUtils.synthesizeKey("KEY_ArrowDown", { code: "ArrowDown" });
yield hideSelectPopup(selectPopup, false);
yield eventsPromise;
EventUtils.synthesizeKey("KEY_ArrowDown", { code: "ArrowDown" });
yield hideSelectPopup(selectPopup, mode);
yield eventsPromise;
}
});
});
@ -399,7 +429,7 @@ add_task(function* test_large_popup() {
"Popup scroll at correct position " + bpBottom);
}
yield hideSelectPopup(selectPopup, false);
yield hideSelectPopup(selectPopup);
position = positions.shift();
if (!position) {