Skip to content

Commit

Permalink
Add tests for Event.returnValue
Browse files Browse the repository at this point in the history
See whatwg/dom#625 for details.
  • Loading branch information
cvrebert authored and annevk committed Apr 9, 2018
1 parent 13597c4 commit 24f49ff
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 4 deletions.
28 changes: 27 additions & 1 deletion dom/events/AddEventListenerOptions-passive.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@
testPassiveValue({passive: 1}, false);
}, "preventDefault should be ignored if-and-only-if the passive option is true");

function testPassiveValueOnReturnValue(test, optionsValue, expectedDefaultPrevented) {
var defaultPrevented = undefined;
var handler = test.step_func(e => {
assert_false(e.defaultPrevented, "Event prematurely marked defaultPrevented");
e.returnValue = false;
defaultPrevented = e.defaultPrevented;
});
document.addEventListener('test', handler, optionsValue);
var uncanceled = document.body.dispatchEvent(new Event('test', {bubbles: true, cancelable: true}));

assert_equals(defaultPrevented, expectedDefaultPrevented, "Incorrect defaultPrevented for options: " + JSON.stringify(optionsValue));
assert_equals(uncanceled, !expectedDefaultPrevented, "Incorrect return value from dispatchEvent");

document.removeEventListener('test', handler, optionsValue);
}

async_test(t => {
testPassiveValueOnReturnValue(t, undefined, true);
testPassiveValueOnReturnValue(t, {}, true);
testPassiveValueOnReturnValue(t, {passive: false}, true);
testPassiveValueOnReturnValue(t, {passive: true}, false);
testPassiveValueOnReturnValue(t, {passive: 0}, true);
testPassiveValueOnReturnValue(t, {passive: 1}, false);
t.done();
}, "returnValue should be ignored if-and-only-if the passive option is true");

function testPassiveWithOtherHandlers(optionsValue, expectedDefaultPrevented) {
var handlerInvoked1 = false;
var dummyHandler1 = function() {
Expand All @@ -81,7 +107,7 @@
testPassiveWithOtherHandlers({}, true);
testPassiveWithOtherHandlers({passive: false}, true);
testPassiveWithOtherHandlers({passive: true}, false);
}, "passive behavior of one listener should be unaffeted by the presence of other listeners");
}, "passive behavior of one listener should be unaffected by the presence of other listeners");

function testOptionEquivalence(optionValue1, optionValue2, expectedEquality) {
var invocationCount = 0;
Expand Down
2 changes: 2 additions & 0 deletions dom/events/Event-constructors.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
assert_equals(ev.bubbles, false)
assert_equals(ev.cancelable, false)
assert_equals(ev.defaultPrevented, false)
assert_equals(ev.returnValue, true)
assert_equals(ev.isTrusted, false)
assert_true(ev.timeStamp > 0)
assert_true("initEvent" in ev)
Expand All @@ -39,6 +40,7 @@
assert_equals(ev.bubbles, false)
assert_equals(ev.cancelable, false)
assert_equals(ev.defaultPrevented, false)
assert_equals(ev.returnValue, true)
assert_equals(ev.isTrusted, false)
assert_true(ev.timeStamp > 0)
assert_true("initEvent" in ev)
Expand Down
21 changes: 19 additions & 2 deletions dom/events/Event-defaultPrevented-after-dispatch.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Event.defaultPrevented is not reset after dipatchEvent()</title>
<title>Event.defaultPrevented is not reset after dispatchEvent()</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
Expand All @@ -23,5 +23,22 @@
assert_true(evt.defaultPrevented, "after dispatch");
assert_equals(evt.target, TARGET);
assert_equals(evt.srcElement, TARGET);
});
}, "Default prevention via preventDefault");

test(function() {
var EVENT = "foo";
var TARGET = document.getElementById("target");
var evt = document.createEvent("Event");
evt.initEvent(EVENT, true, true);

TARGET.addEventListener(EVENT, this.step_func(function(e) {
e.returnValue = false;
assert_true(e.defaultPrevented, "during dispatch");
}), true);
TARGET.dispatchEvent(evt);

assert_true(evt.defaultPrevented, "after dispatch");
assert_equals(evt.target, TARGET);
assert_equals(evt.srcElement, TARGET);
}, "Default prevention via returnValue");
</script>
12 changes: 12 additions & 0 deletions dom/events/Event-defaultPrevented.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
assert_equals(ev.cancelable, false, "cancelable (after)");
assert_equals(ev.defaultPrevented, false, "defaultPrevented");
}, "preventDefault() should not change defaultPrevented if cancelable is false.");
test(function() {
assert_equals(ev.cancelable, false, "cancelable (before)");
ev.returnValue = false;
assert_equals(ev.cancelable, false, "cancelable (after)");
assert_equals(ev.defaultPrevented, false, "defaultPrevented");
}, "returnValue should not change defaultPrevented if cancelable is false.");
test(function() {
ev.initEvent("foo", true, true);
assert_equals(ev.bubbles, true, "bubbles");
Expand All @@ -33,6 +39,12 @@
assert_equals(ev.cancelable, true, "cancelable (after)");
assert_equals(ev.defaultPrevented, true, "defaultPrevented");
}, "preventDefault() should change defaultPrevented if cancelable is true.");
test(function() {
assert_equals(ev.cancelable, true, "cancelable (before)");
ev.returnValue = false;
assert_equals(ev.cancelable, true, "cancelable (after)");
assert_equals(ev.defaultPrevented, true, "defaultPrevented");
}, "returnValue should change defaultPrevented if cancelable is true.");
test(function() {
ev.initEvent("foo", true, true);
assert_equals(ev.bubbles, true, "bubbles");
Expand Down
1 change: 1 addition & 0 deletions dom/events/Event-dispatch-click.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
var clickEvent = new MouseEvent("click")
input.onchange = t.step_func_done(function() {
assert_false(clickEvent.defaultPrevented)
assert_true(clickEvent.returnValue)
assert_equals(clickEvent.eventPhase, 0)
assert_equals(clickEvent.currentTarget, null)
assert_equals(clickEvent.target, input)
Expand Down
1 change: 1 addition & 0 deletions dom/events/Event-initEvent.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// Step 2.
// Stop (immediate) propagation flag is tested later
assert_equals(e.defaultPrevented, false, "defaultPrevented")
assert_equals(e.returnValue, true, "returnValue")
// Step 3.
assert_equals(e.isTrusted, false, "isTrusted")
// Step 4.
Expand Down
64 changes: 64 additions & 0 deletions dom/events/Event-returnValue.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Event.returnValue</title>
<link rel="author" title="Chris Rebert" href="http://chrisrebert.com">
<link rel="help" href="https://dom.spec.whatwg.org/#dom-event-returnvalue">
<meta name="flags" content="dom">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<div id="log"></div>
<script>
test(function() {
var ev = new Event("foo");
assert_true(ev.returnValue, "returnValue");
}, "When an event is created, returnValue should be initialized to true.");
test(function() {
var ev = new Event("foo", {"cancelable": false});
assert_false(ev.cancelable, "cancelable (before)");
ev.preventDefault();
assert_false(ev.cancelable, "cancelable (after)");
assert_true(ev.returnValue, "returnValue");
}, "preventDefault() should not change returnValue if cancelable is false.");
test(function() {
var ev = new Event("foo", {"cancelable": false});
assert_false(ev.cancelable, "cancelable (before)");
ev.returnValue = false;
assert_false(ev.cancelable, "cancelable (after)");
assert_true(ev.returnValue, "returnValue");
}, "returnValue=false should have no effect if cancelable is false.");
test(function() {
var ev = new Event("foo", {"cancelable": true});
assert_true(ev.cancelable, "cancelable (before)");
ev.preventDefault();
assert_true(ev.cancelable, "cancelable (after)");
assert_false(ev.returnValue, "returnValue");
}, "preventDefault() should change returnValue if cancelable is true.");
test(function() {
var ev = new Event("foo", {"cancelable": true});
assert_true(ev.cancelable, "cancelable (before)");
ev.returnValue = false;
assert_true(ev.cancelable, "cancelable (after)");
assert_false(ev.returnValue, "returnValue");
}, "returnValue should change returnValue if cancelable is true.");
test(function() {
var ev = document.createEvent("Event");
ev.returnValue = false;
ev.initEvent("foo", true, true);
assert_true(ev.bubbles, "bubbles");
assert_true(ev.cancelable, "cancelable");
assert_true(ev.returnValue, "returnValue");
}, "initEvent should unset returnValue.");
test(function() {
var ev = new Event("foo");
ev.preventDefault();
ev.returnValue = true;// no-op
assert_true(ev.defaultPrevented);
assert_false(ev.returnValue);
}, "returnValue=true should have no effect once the canceled flag was set.");
</script>
</body>
</html>
30 changes: 29 additions & 1 deletion dom/events/EventTarget-dispatchEvent-returnvalue.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<title>EventTarget.dispatchEvent: return value</title>
<link rel="help" href="https://dom.spec.whatwg.org/#concept-event-dispatch">
<link rel="help" href="https://dom.spec.whatwg.org/#dom-event-preventdefault">
<link rel="help" href="https://dom.spec.whatwg.org/#dom-event-returnvalue">
<link rel="help" href="https://dom.spec.whatwg.org/#dom-event-defaultprevented">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
Expand All @@ -25,11 +26,13 @@
var target = document.getElementById("target");
var parent = document.getElementById("parent");
var default_prevented;
var return_value;

parent.addEventListener(event_type, function(e) {}, true);
target.addEventListener(event_type, function(e) {
evt.preventDefault();
default_prevented = evt.defaultPrevented;
return_value = evt.returnValue;
}, true);
target.addEventListener(event_type, function(e) {}, true);

Expand All @@ -39,5 +42,30 @@
assert_true(parent.dispatchEvent(evt));
assert_false(target.dispatchEvent(evt));
assert_true(default_prevented);
}, "Return value of EventTarget.dispatchEvent.");
assert_false(return_value);
}, "Return value of EventTarget.dispatchEvent() affected by preventDefault().");

test(function() {
var event_type = "foo";
var target = document.getElementById("target");
var parent = document.getElementById("parent");
var default_prevented;
var return_value;

parent.addEventListener(event_type, function(e) {}, true);
target.addEventListener(event_type, function(e) {
evt.returnValue = false;
default_prevented = evt.defaultPrevented;
return_value = evt.returnValue;
}, true);
target.addEventListener(event_type, function(e) {}, true);

var evt = document.createEvent("Event");
evt.initEvent(event_type, true, true);

assert_true(parent.dispatchEvent(evt));
assert_false(target.dispatchEvent(evt));
assert_true(default_prevented);
assert_false(return_value);
}, "Return value of EventTarget.dispatchEvent() affected by returnValue.");
</script>
1 change: 1 addition & 0 deletions interfaces/dom.idl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Event {

readonly attribute boolean bubbles;
readonly attribute boolean cancelable;
attribute boolean returnValue;
void preventDefault();
readonly attribute boolean defaultPrevented;

Expand Down

0 comments on commit 24f49ff

Please sign in to comment.