2007-08-04 07:20:45 +00:00
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
|
|
<head>
|
|
|
|
<title>Prototype Unit test file</title>
|
|
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
|
|
<script src="../../dist/prototype.js" type="text/javascript"></script>
|
|
|
|
<script src="../lib/unittest.js" type="text/javascript"></script>
|
|
|
|
<link rel="stylesheet" href="../test.css" type="text/css" />
|
|
|
|
<style type="text/css" media="screen">
|
|
|
|
/* <![CDATA[ */
|
|
|
|
#testcss1 { font-size:11px; color: #f00; }
|
|
|
|
#testcss2 { font-size:12px; color: #0f0; display: none; }
|
|
|
|
/* ]]> */
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Prototype Unit test file</h1>
|
|
|
|
<p>
|
|
|
|
Test of event handling code in event.js
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<!-- Log output -->
|
|
|
|
<div id="testlog"> </div>
|
|
|
|
|
|
|
|
<div id="outer" style="display: none">
|
|
|
|
<p id="inner">One two three <span id="span">four</span></p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Tests follow -->
|
|
|
|
<script type="text/javascript" language="javascript" charset="utf-8">
|
|
|
|
// <![CDATA[
|
|
|
|
|
|
|
|
var eventResults = { };
|
|
|
|
|
|
|
|
new Test.Unit.Runner({
|
|
|
|
|
|
|
|
// test firing an event and observing it on the element it's fired from
|
|
|
|
testCustomEventFiring: function() { with(this) {
|
|
|
|
var span = $("span"), fired = false, observer = function(event) {
|
|
|
|
assertEqual(span, event.element());
|
|
|
|
assertEqual(1, event.memo.index);
|
|
|
|
fired = true;
|
|
|
|
}
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
|
|
|
span.fire("test:somethingHappened", { index: 1 });
|
2007-08-04 07:20:45 +00:00
|
|
|
assert(fired);
|
|
|
|
|
|
|
|
fired = false;
|
2007-10-11 06:00:45 +00:00
|
|
|
span.fire("test:somethingElseHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assert(!fired);
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.stopObserving("test:somethingHappened", observer);
|
|
|
|
span.fire("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assert(!fired);
|
|
|
|
}},
|
|
|
|
|
|
|
|
// test firing an event and observing it on a containing element
|
|
|
|
testCustomEventBubbling: function() { with(this) {
|
|
|
|
var span = $("span"), outer = $("outer"), fired = false, observer = function(event) {
|
|
|
|
assertEqual(span, event.element());
|
|
|
|
fired = true;
|
|
|
|
};
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
outer.observe("test:somethingHappened", observer);
|
|
|
|
span.fire("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assert(fired);
|
|
|
|
|
|
|
|
fired = false;
|
2007-10-11 06:00:45 +00:00
|
|
|
span.fire("test:somethingElseHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assert(!fired);
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
outer.stopObserving("test:somethingHappened", observer);
|
|
|
|
span.fire("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assert(!fired);
|
|
|
|
}},
|
|
|
|
|
|
|
|
testCustomEventCanceling: function() { with(this) {
|
|
|
|
var span = $("span"), outer = $("outer"), inner = $("inner");
|
|
|
|
var fired = false, stopped = false;
|
|
|
|
|
|
|
|
function outerObserver(event) {
|
|
|
|
fired = span == event.element();
|
|
|
|
}
|
|
|
|
|
|
|
|
function innerObserver(event) {
|
|
|
|
event.stop();
|
|
|
|
stopped = true;
|
|
|
|
}
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
inner.observe("test:somethingHappened", innerObserver);
|
|
|
|
outer.observe("test:somethingHappened", outerObserver);
|
|
|
|
span.fire("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assert(stopped);
|
|
|
|
assert(!fired);
|
|
|
|
|
|
|
|
fired = stopped = false;
|
2007-10-11 06:00:45 +00:00
|
|
|
inner.stopObserving("test:somethingHappened", innerObserver);
|
|
|
|
span.fire("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assert(!stopped);
|
|
|
|
assert(fired);
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
outer.stopObserving("test:somethingHappened", outerObserver);
|
2007-08-04 07:20:45 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testEventObjectIsExtended: function() { with(this) {
|
2007-08-11 18:46:02 +00:00
|
|
|
var span = $("span"), event, observedEvent, observer = function(e) { observedEvent = e };
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
|
|
|
event = span.fire("test:somethingHappened");
|
2007-08-11 18:46:02 +00:00
|
|
|
assertEqual(event, observedEvent);
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(Event.Methods.stop.methodize(), event.stop);
|
2007-10-11 06:00:45 +00:00
|
|
|
span.stopObserving("test:somethingHappened", observer);
|
2007-08-04 07:20:45 +00:00
|
|
|
}},
|
|
|
|
|
2007-08-17 16:17:26 +00:00
|
|
|
testEventObserversAreBoundToTheObservedElement: function() { with(this) {
|
2007-08-04 07:20:45 +00:00
|
|
|
var span = $("span"), target, observer = function() { target = this };
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
|
|
|
span.fire("test:somethingHappened");
|
|
|
|
span.stopObserving("test:somethingHappened", observer);
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(span, target);
|
|
|
|
target = null;
|
|
|
|
|
|
|
|
var outer = $("outer");
|
2007-10-11 06:00:45 +00:00
|
|
|
outer.observe("test:somethingHappened", observer);
|
|
|
|
span.fire("test:somethingHappened");
|
|
|
|
outer.stopObserving("test:somethingHappened", observer);
|
2007-08-17 16:17:26 +00:00
|
|
|
assertEqual(outer, target);
|
2007-08-04 07:20:45 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testMultipleCustomEventObserversWithTheSameHandler: function() { with(this) {
|
|
|
|
var span = $("span"), count = 0, observer = function() { count++ };
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
|
|
|
span.observe("test:somethingElseHappened", observer);
|
|
|
|
span.fire("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(1, count);
|
2007-10-11 06:00:45 +00:00
|
|
|
span.fire("test:somethingElseHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(2, count);
|
|
|
|
}},
|
|
|
|
|
|
|
|
testStopObservingWithoutArguments: function() { with(this) {
|
|
|
|
var span = $("span"), count = 0, observer = function() { count++ };
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
|
|
|
span.observe("test:somethingElseHappened", observer);
|
2007-08-04 07:20:45 +00:00
|
|
|
span.stopObserving();
|
2007-10-11 06:00:45 +00:00
|
|
|
span.fire("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(0, count);
|
2007-10-11 06:00:45 +00:00
|
|
|
span.fire("test:somethingElseHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(0, count);
|
|
|
|
}},
|
|
|
|
|
|
|
|
testStopObservingWithoutHandlerArgument: function() { with(this) {
|
|
|
|
var span = $("span"), count = 0, observer = function() { count++ };
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
|
|
|
span.observe("test:somethingElseHappened", observer);
|
|
|
|
span.stopObserving("test:somethingHappened");
|
|
|
|
span.fire("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(0, count);
|
2007-10-11 06:00:45 +00:00
|
|
|
span.fire("test:somethingElseHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(1, count);
|
2007-10-11 06:00:45 +00:00
|
|
|
span.stopObserving("test:somethingElseHappened");
|
|
|
|
span.fire("test:somethingElseHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
assertEqual(1, count);
|
|
|
|
}},
|
|
|
|
|
2007-08-09 23:45:59 +00:00
|
|
|
testStopObservingRemovesHandlerFromCache: function() { with(this) {
|
|
|
|
var span = $("span"), observer = function() { }, eventID;
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
2007-08-09 23:45:59 +00:00
|
|
|
eventID = span._eventID;
|
|
|
|
|
|
|
|
assert(Event.cache[eventID]);
|
2007-10-11 06:00:45 +00:00
|
|
|
assert(Object.isArray(Event.cache[eventID]["test:somethingHappened"]));
|
|
|
|
assertEqual(1, Event.cache[eventID]["test:somethingHappened"].length);
|
2007-08-09 23:45:59 +00:00
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.stopObserving("test:somethingHappened", observer);
|
2007-08-09 23:45:59 +00:00
|
|
|
assert(Event.cache[eventID]);
|
2007-10-11 06:00:45 +00:00
|
|
|
assert(Object.isArray(Event.cache[eventID]["test:somethingHappened"]));
|
|
|
|
assertEqual(0, Event.cache[eventID]["test:somethingHappened"].length);
|
2007-08-09 23:45:59 +00:00
|
|
|
}},
|
|
|
|
|
2007-08-11 19:53:39 +00:00
|
|
|
testObserveAndStopObservingAreChainable: function() { with(this) {
|
2007-08-11 19:07:21 +00:00
|
|
|
var span = $("span"), observer = function() { };
|
2007-08-11 19:53:39 +00:00
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
assertEqual(span, span.observe("test:somethingHappened", observer));
|
|
|
|
assertEqual(span, span.stopObserving("test:somethingHappened", observer));
|
2007-08-11 19:53:39 +00:00
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
|
|
|
assertEqual(span, span.stopObserving("test:somethingHappened"));
|
2007-08-11 19:53:39 +00:00
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
2007-08-11 19:07:21 +00:00
|
|
|
assertEqual(span, span.stopObserving());
|
|
|
|
assertEqual(span, span.stopObserving()); // assert it again, after there are no observers
|
2007-08-11 19:53:39 +00:00
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
span.observe("test:somethingHappened", observer);
|
|
|
|
assertEqual(span, span.observe("test:somethingHappened", observer)); // try to reuse the same observer
|
2007-08-11 19:53:39 +00:00
|
|
|
span.stopObserving();
|
2007-08-11 19:07:21 +00:00
|
|
|
}},
|
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
testDocumentContentLoadedEventFiresBeforeWindowLoad: function() { with(this) {
|
|
|
|
assert(eventResults.contentLoaded, "contentLoaded");
|
|
|
|
assert(eventResults.contentLoaded.endOfDocument, "contentLoaded.endOfDocument");
|
|
|
|
assert(!eventResults.contentLoaded.windowLoad, "!contentLoaded.windowLoad");
|
|
|
|
assert(eventResults.windowLoad, "windowLoad");
|
|
|
|
assert(eventResults.windowLoad.endOfDocument, "windowLoad.endOfDocument");
|
|
|
|
assert(eventResults.windowLoad.contentLoaded, "windowLoad.contentLoaded");
|
2007-10-26 19:15:35 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testEventStopped: function() { with(this) {
|
|
|
|
var span = $("span"), event;
|
|
|
|
|
|
|
|
span.observe("test:somethingHappened", function() { });
|
|
|
|
event = span.fire("test:somethingHappened");
|
|
|
|
assert(!event.stopped, "event.stopped should be false with an empty observer");
|
|
|
|
span.stopObserving("test:somethingHappened");
|
|
|
|
|
|
|
|
span.observe("test:somethingHappened", function(e) { e.stop() });
|
|
|
|
event = span.fire("test:somethingHappened");
|
|
|
|
assert(event.stopped, "event.stopped should be true for an observer that calls stop");
|
|
|
|
span.stopObserving("test:somethingHappened");
|
2007-08-04 07:20:45 +00:00
|
|
|
}}
|
|
|
|
|
2007-10-26 19:15:35 +00:00
|
|
|
|
2007-08-04 07:20:45 +00:00
|
|
|
}, 'testlog');
|
|
|
|
|
2007-10-11 06:00:45 +00:00
|
|
|
document.observe("dom:loaded", function(event) {
|
2007-08-04 07:20:45 +00:00
|
|
|
eventResults.contentLoaded = {
|
|
|
|
endOfDocument: eventResults.endOfDocument,
|
|
|
|
windowLoad: eventResults.windowLoad
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
Event.observe(window, "load", function(event) {
|
|
|
|
eventResults.windowLoad = {
|
|
|
|
endOfDocument: eventResults.endOfDocument,
|
|
|
|
contentLoaded: eventResults.contentLoaded
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// ]]>
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
<script type="text/javascript">
|
|
|
|
eventResults.endOfDocument = true;
|
|
|
|
</script>
|