gecko/content/events/test/test_eventctors.html

644 lines
18 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=675884
-->
<head>
<title>Test for Bug 675884</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=675884">Mozilla Bug 675884</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 675884 **/
var receivedEvent;
document.addEventListener("hello", function(e) { receivedEvent = e; }, true);
// Event
var e;
var ex = false;
try {
e = new Event();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
try {
e = new Event("foo", 123);
} catch(exp) {
ex = true;
}
ok(ex, "2nd parameter should be an object!");
ex = false;
try {
e = new Event("foo", "asdf");
} catch(exp) {
ex = true;
}
ok(ex, "2nd parameter should be an object!");
ex = false;
try {
e = new Event("foo", false);
} catch(exp) {
ex = true;
}
ok(ex, "2nd parameter should be an object!");
ex = false;
e = new Event("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.eventPhase, Event.NONE, "Wrong event phase");
document.dispatchEvent(e);
is(e.eventPhase, Event.NONE, "Wrong event phase");
is(receivedEvent, e, "Wrong event!");
e = new Event("hello", null);
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.eventPhase, Event.NONE, "Wrong event phase");
document.dispatchEvent(e);
is(e.eventPhase, Event.NONE, "Wrong event phase");
is(receivedEvent, e, "Wrong event!");
e = new Event("hello", undefined);
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.eventPhase, Event.NONE, "Wrong event phase");
document.dispatchEvent(e);
is(e.eventPhase, Event.NONE, "Wrong event phase");
is(receivedEvent, e, "Wrong event!");
e = new Event("hello", {});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.eventPhase, Event.NONE, "Wrong event phase");
document.dispatchEvent(e);
is(e.eventPhase, Event.NONE, "Wrong event phase");
is(receivedEvent, e, "Wrong event!");
e = new Event("hello", { bubbles: true, cancelable: true });
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// CustomEvent
try {
e = new CustomEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
e = new CustomEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new CustomEvent("hello", { bubbles: true, cancelable: true, detail: window });
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.detail, window , "Wrong event.detail!");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new CustomEvent("hello", { cancelable: true, detail: window });
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.detail, window , "Wrong event.detail!");
e = new CustomEvent("hello", { detail: 123 });
is(e.detail, 123, "Wrong event.detail!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
var dict = { get detail() { return document.body } };
e = new CustomEvent("hello", dict);
is(e.detail, dict.detail, "Wrong event.detail!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
var dict = { get detail() { throw "foo"; } };
try {
e = new CustomEvent("hello", dict);
} catch (exp) {
ex = true;
}
ok(ex, "Should have thrown an exception!");
ex = false;
// CloseEvent
try {
e = new CloseEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
e = new CloseEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.wasClean, false, "wasClean should be false!");
is(e.code, 0, "code should be 0!");
is(e.reason, "", "reason should be ''!");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new CloseEvent("hello",
{ bubbles: true, cancelable: true, wasClean: true, code: 1, reason: "foo" });
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.wasClean, true, "wasClean should be true!");
is(e.code, 1, "code should be 1!");
is(e.reason, "foo", "reason should be 'foo'!");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new CloseEvent("hello",
{ bubbles: true, cancelable: true, wasClean: true, code: 1 });
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.wasClean, true, "wasClean should be true!");
is(e.code, 1, "code should be 1!");
is(e.reason, "", "reason should be ''!");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// HashChangeEvent
try {
e = new HashChangeEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
e = new HashChangeEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.oldURL, "", "oldURL should be ''");
is(e.newURL, "", "newURL should be ''");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new HashChangeEvent("hello",
{ bubbles: true, cancelable: true, oldURL: "old", newURL: "new" });
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.oldURL, "old", "oldURL should be 'old'");
is(e.newURL, "new", "newURL should be 'new'");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new HashChangeEvent("hello",
{ bubbles: true, cancelable: true, newURL: "new" });
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.oldURL, "", "oldURL should be ''");
is(e.newURL, "new", "newURL should be 'new'");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// PageTransitionEvent
try {
e = new PageTransitionEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
e = new PageTransitionEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.persisted, false, "persisted should be false");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new PageTransitionEvent("hello",
{ bubbles: true, cancelable: true, persisted: true});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.persisted, true, "persisted should be true");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new PageTransitionEvent("hello", { persisted: true});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.persisted, true, "persisted should be true");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// PopStateEvent
try {
e = new PopStateEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
e = new PopStateEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.state, null, "persisted should be null");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new PopStateEvent("hello",
{ bubbles: true, cancelable: true, state: window});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.state, window, "persisted should be window");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new PopStateEvent("hello", { state: window});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.state, window, "persisted should be window");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// UIEvent
try {
e = new UIEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
try {
e = new UIEvent("foo", { view: {} });
e.view.onunload;
} catch(exp) {
ex = true;
}
ok(ex, "{} isn't a valid value.");
ex = false;
try {
e = new UIEvent("foo", { view: null });
} catch(exp) {
ex = true;
}
ok(!ex, "null is a valid value.");
is(e.view, null);
ex = false;
e = new UIEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.detail, 0, "detail should be 0");
is(e.view, null, "view should be null");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new UIEvent("hello",
{ bubbles: true, cancelable: true, view: window, detail: 1});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.detail, 1, "detail should be 1");
is(e.view, window, "view should be window");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// StorageEvent
e = document.createEvent("StorageEvent");
ok(e, "Should have created an event!");
try {
e = new StorageEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
e = new StorageEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.key, null, "key should be null");
is(e.oldValue, null, "oldValue should be null");
is(e.newValue, null, "newValue should be null");
is(e.url, "", "url should be ''");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new StorageEvent("hello",
{ bubbles: true, cancelable: true, key: "key",
oldValue: "oldValue", newValue: "newValue", url: "url",
storageArea: localStorage });
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.key, "key", "Wrong value");
is(e.oldValue, "oldValue", "Wrong value");
is(e.newValue, "newValue", "Wrong value");
is(e.url, "url", "Wrong value");
is(e.storageArea, localStorage, "Wrong value");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// DeviceProximityEvent
e = new DeviceProximityEvent("hello", {min: 0, value: 1, max: 2});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event should not be trusted");
is(e.value, 1, "value should be 1");
is(e.min, 0, "min should be 0");
is(e.max, 2, "max should be 2");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new DeviceProximityEvent("hello");
is(e.value, Infinity, "Uninitialized value should be infinity");
is(e.min, -Infinity, "Uninitialized min should be -infinity");
is(e.max, Infinity, "Uninitialized max should be infinity");
// UserProximityEvent
e = new UserProximityEvent("hello", {near: true});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event should not be trusted");
is(e.near, true, "near should be true");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// DeviceLightEvent
e = new DeviceLightEvent("hello", {value: 1} );
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event should not be trusted");
is(e.value, 1, "value should be 1");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// DeviceOrientationEvent
e = new DeviceOrientationEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event should not be trusted");
is(e.alpha, 0);
is(e.beta, 0);
is(e.gamma, 0);
is(e.absolute, false);
e = new DeviceOrientationEvent("hello", { alpha: 1, beta: 2, gamma: 3, absolute: true } );
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event should not be trusted");
is(e.alpha, 1);
is(e.beta, 2);
is(e.gamma, 3);
is(e.absolute, true);
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// MouseEvent
try {
e = new MouseEvent();
} catch(exp) {
ex = true;
}
ok(ex, "MouseEvent: First parameter is required!");
ex = false;
e = new MouseEvent("hello");
ok(e.type, "hello", "MouseEvent: Wrong event type!");
ok(!e.isTrusted, "MouseEvent: Event shouldn't be trusted!");
ok(!e.bubbles, "MouseEvent: Event shouldn't bubble!");
ok(!e.cancelable, "MouseEvent: Event shouldn't be cancelable!");
document.dispatchEvent(e);
is(receivedEvent, e, "MouseEvent: Wrong event!");
var mouseEventProps =
[ { screenX: 0 },
{ screenY: 0 },
{ clientX: 0 },
{ clientY: 0 },
{ ctrlKey: false },
{ shiftKey: false },
{ altKey: false },
{ metaKey: false },
{ button: 0 },
{ buttons: 0 },
{ relatedTarget: null }
];
var testProps =
[
{ screenX: 1 },
{ screenY: 2 },
{ clientX: 3 },
{ clientY: 4 },
{ ctrlKey: true },
{ shiftKey: true },
{ altKey: true },
{ metaKey: true },
{ button: 5 },
{ buttons: 6 },
{ relatedTarget: window }
];
var defaultMouseEventValues = {};
for (var i = 0; i < mouseEventProps.length; ++i) {
for (prop in mouseEventProps[i]) {
ok(prop in e, "MouseEvent: MouseEvent doesn't have property " + prop + "!");
defaultMouseEventValues[prop] = mouseEventProps[i][prop];
}
}
while (testProps.length) {
var p = testProps.shift();
e = new MouseEvent("foo", p);
for (var def in defaultMouseEventValues) {
if (!(def in p)) {
is(e[def], defaultMouseEventValues[def],
"MouseEvent: Wrong default value for " + def + "!");
} else {
is(e[def], p[def], "MouseEvent: Wrong event init value for " + def + "!");
}
}
}
// PopupBlockedEvent
try {
e = new PopupBlockedEvent();
} catch(exp) {
ex = true;
}
ok(ex, "PopupBlockedEvent: First parameter is required!");
ex = false;
e = new PopupBlockedEvent("hello");
ok(e.type, "hello", "PopupBlockedEvent: Wrong event type!");
ok(!e.isTrusted, "PopupBlockedEvent: Event shouldn't be trusted!");
ok(!e.bubbles, "PopupBlockedEvent: Event shouldn't bubble!");
ok(!e.cancelable, "PopupBlockedEvent: Event shouldn't be cancelable!");
document.dispatchEvent(e);
is(receivedEvent, e, "PopupBlockedEvent: Wrong event!");
e = new PopupBlockedEvent("hello",
{ requestingWindow: window,
popupWindowFeatures: "features",
popupWindowName: "name"
});
is(e.requestingWindow, window);
is(e.popupWindowFeatures, "features");
is(e.popupWindowName, "name");
// WheelEvent
try {
e = new WheelEvent();
} catch(exp) {
ex = true;
}
ok(ex, "WheelEvent: First parameter is required!");
ex = false;
e = new WheelEvent("hello");
ok(e.type, "hello", "WheelEvent: Wrong event type!");
ok(!e.isTrusted, "WheelEvent: Event shouldn't be trusted!");
ok(!e.bubbles, "WheelEvent: Event shouldn't bubble!");
ok(!e.cancelable, "WheelEvent: Event shouldn't be cancelable!");
document.dispatchEvent(e);
is(receivedEvent, e, "WheelEvent: Wrong event!");
var wheelEventProps =
[ { screenX: 0 },
{ screenY: 0 },
{ clientX: 0 },
{ clientY: 0 },
{ ctrlKey: false },
{ shiftKey: false },
{ altKey: false },
{ metaKey: false },
{ button: 0 },
{ buttons: 0 },
{ relatedTarget: null },
{ deltaX: 0.0 },
{ deltaY: 0.0 },
{ deltaZ: 0.0 },
{ deltaMode: 0 }
];
var testWheelProps =
[
{ screenX: 1 },
{ screenY: 2 },
{ clientX: 3 },
{ clientY: 4 },
{ ctrlKey: true },
{ shiftKey: true },
{ altKey: true },
{ metaKey: true },
{ button: 5 },
{ buttons: 6 },
{ relatedTarget: window },
{ deltaX: 7.8 },
{ deltaY: 9.1 },
{ deltaZ: 2.3 },
{ deltaMode: 4 }
];
var defaultWheelEventValues = {};
for (var i = 0; i < wheelEventProps.length; ++i) {
for (prop in wheelEventProps[i]) {
ok(prop in e, "WheelEvent: WheelEvent doesn't have property " + prop + "!");
defaultWheelEventValues[prop] = wheelEventProps[i][prop];
}
}
while (testWheelProps.length) {
var p = testWheelProps.shift();
e = new WheelEvent("foo", p);
for (var def in defaultWheelEventValues) {
if (!(def in p)) {
is(e[def], defaultWheelEventValues[def],
"WheelEvent: Wrong default value for " + def + "!");
} else {
is(e[def], p[def], "WheelEvent: Wrong event init value for " + def + "!");
}
}
}
</script>
</pre>
</body>
</html>