237 lines
8.0 KiB
HTML
237 lines
8.0 KiB
HTML
<!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;
|
|
}
|
|
|
|
span.observe("somethingHappened", observer);
|
|
span.fire("somethingHappened", { index: 1 });
|
|
assert(fired);
|
|
|
|
fired = false;
|
|
span.fire("somethingElseHappened");
|
|
assert(!fired);
|
|
|
|
span.stopObserving("somethingHappened", observer);
|
|
span.fire("somethingHappened");
|
|
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;
|
|
};
|
|
|
|
outer.observe("somethingHappened", observer);
|
|
span.fire("somethingHappened");
|
|
assert(fired);
|
|
|
|
fired = false;
|
|
span.fire("somethingElseHappened");
|
|
assert(!fired);
|
|
|
|
outer.stopObserving("somethingHappened", observer);
|
|
span.fire("somethingHappened");
|
|
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;
|
|
}
|
|
|
|
inner.observe("somethingHappened", innerObserver);
|
|
outer.observe("somethingHappened", outerObserver);
|
|
span.fire("somethingHappened");
|
|
assert(stopped);
|
|
assert(!fired);
|
|
|
|
fired = stopped = false;
|
|
inner.stopObserving("somethingHappened", innerObserver);
|
|
span.fire("somethingHappened");
|
|
assert(!stopped);
|
|
assert(fired);
|
|
|
|
outer.stopObserving("somethingHappened", outerObserver);
|
|
}},
|
|
|
|
testEventObjectIsExtended: function() { with(this) {
|
|
var span = $("span"), event, observedEvent, observer = function(e) { observedEvent = e };
|
|
span.observe("somethingHappened", observer);
|
|
event = span.fire("somethingHappened");
|
|
assertEqual(event, observedEvent);
|
|
assertEqual(Event.Methods.stop.methodize(), event.stop);
|
|
span.stopObserving("somethingHappened", observer);
|
|
}},
|
|
|
|
testEventObserversAreBoundToTheObservedElement: function() { with(this) {
|
|
var span = $("span"), target, observer = function() { target = this };
|
|
|
|
span.observe("somethingHappened", observer);
|
|
span.fire("somethingHappened");
|
|
span.stopObserving("somethingHappened", observer);
|
|
assertEqual(span, target);
|
|
target = null;
|
|
|
|
var outer = $("outer");
|
|
outer.observe("somethingHappened", observer);
|
|
span.fire("somethingHappened");
|
|
outer.stopObserving("somethingHappened", observer);
|
|
assertEqual(outer, target);
|
|
}},
|
|
|
|
testMultipleCustomEventObserversWithTheSameHandler: function() { with(this) {
|
|
var span = $("span"), count = 0, observer = function() { count++ };
|
|
|
|
span.observe("somethingHappened", observer);
|
|
span.observe("somethingElseHappened", observer);
|
|
span.fire("somethingHappened");
|
|
assertEqual(1, count);
|
|
span.fire("somethingElseHappened");
|
|
assertEqual(2, count);
|
|
}},
|
|
|
|
testStopObservingWithoutArguments: function() { with(this) {
|
|
var span = $("span"), count = 0, observer = function() { count++ };
|
|
|
|
span.observe("somethingHappened", observer);
|
|
span.observe("somethingElseHappened", observer);
|
|
span.stopObserving();
|
|
span.fire("somethingHappened");
|
|
assertEqual(0, count);
|
|
span.fire("somethingElseHappened");
|
|
assertEqual(0, count);
|
|
}},
|
|
|
|
testStopObservingWithoutHandlerArgument: function() { with(this) {
|
|
var span = $("span"), count = 0, observer = function() { count++ };
|
|
|
|
span.observe("somethingHappened", observer);
|
|
span.observe("somethingElseHappened", observer);
|
|
span.stopObserving("somethingHappened");
|
|
span.fire("somethingHappened");
|
|
assertEqual(0, count);
|
|
span.fire("somethingElseHappened");
|
|
assertEqual(1, count);
|
|
span.stopObserving("somethingElseHappened");
|
|
span.fire("somethingElseHappened");
|
|
assertEqual(1, count);
|
|
}},
|
|
|
|
testStopObservingRemovesHandlerFromCache: function() { with(this) {
|
|
var span = $("span"), observer = function() { }, eventID;
|
|
|
|
span.observe("somethingHappened", observer);
|
|
eventID = span._eventID;
|
|
|
|
assert(Event.cache[eventID]);
|
|
assert(Object.isArray(Event.cache[eventID].somethingHappened));
|
|
assertEqual(1, Event.cache[eventID].somethingHappened.length);
|
|
|
|
span.stopObserving("somethingHappened", observer);
|
|
assert(Event.cache[eventID]);
|
|
assert(Object.isArray(Event.cache[eventID].somethingHappened));
|
|
assertEqual(0, Event.cache[eventID].somethingHappened.length);
|
|
}},
|
|
|
|
testObserveAndStopObservingAreChainable: function() { with(this) {
|
|
var span = $("span"), observer = function() { };
|
|
|
|
assertEqual(span, span.observe("somethingHappened", observer));
|
|
assertEqual(span, span.stopObserving("somethingHappened", observer));
|
|
|
|
span.observe("somethingHappened", observer);
|
|
assertEqual(span, span.stopObserving("somethingHappened"));
|
|
|
|
span.observe("somethingHappened", observer);
|
|
assertEqual(span, span.stopObserving());
|
|
assertEqual(span, span.stopObserving()); // assert it again, after there are no observers
|
|
|
|
span.observe("somethingHappened", observer);
|
|
assertEqual(span, span.observe("somethingHappened", observer)); // try to reuse the same observer
|
|
span.stopObserving();
|
|
}},
|
|
|
|
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");
|
|
}}
|
|
|
|
}, 'testlog');
|
|
|
|
document.observe("contentloaded", function(event) {
|
|
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>
|