Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / dom / html / level2 / events / dispatchEvent13.js
blobdad9a005fe93e5b12c01b38e43cd2479b695bd81
1 /*
2 Copyright © 2001-2004 World Wide Web Consortium,
3 (Massachusetts Institute of Technology, European Research Consortium
4 for Informatics and Mathematics, Keio University). All
5 Rights Reserved. This work is distributed under the W3C® Software License [1] in the
6 hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
12 /**
13 * Gets URI that identifies the test.
14 * @return uri identifier of test
16 function getTargetURI() {
17 return "http://www.w3.org/2001/DOM-Test-Suite/level2/events/dispatchEvent13";
20 var docsLoaded = -1000000;
21 var builder = null;
24 // This function is called by the testing framework before
25 // running the test suite.
27 // If there are no configuration exceptions, asynchronous
28 // document loading is started. Otherwise, the status
29 // is set to complete and the exception is immediately
30 // raised when entering the body of the test.
32 function setUpPage() {
33 setUpPageStatus = 'running';
34 try {
36 // creates test document builder, may throw exception
38 builder = createConfiguredBuilder();
40 docsLoaded = 0;
42 var docRef = null;
43 if (typeof(this.doc) != 'undefined') {
44 docRef = this.doc;
46 docsLoaded += preload(docRef, "doc", "hc_staff");
48 if (docsLoaded == 1) {
49 setUpPageStatus = 'complete';
51 } catch(ex) {
52 catchInitializationError(builder, ex);
53 setUpPageStatus = 'complete';
58 // This method is called on the completion of
59 // each asychronous load started in setUpTests.
61 // When every synchronous loaded document has completed,
62 // the page status is changed which allows the
63 // body of the test to be executed.
64 function loadComplete() {
65 if (++docsLoaded == 1) {
66 setUpPageStatus = 'complete';
70 /**
71 * Inner class implementation for variable listener1
73 var listener1;
75 /**
76 * Constructor
78 * @param events Value from value attribute of nested var element
79 * @param listeners Value from value attribute of nested var element
82 function EventListenerN1003B(events, listeners) {
83 this.events = events;
84 this.listeners = listeners;
87 /**
89 This method is called whenever an event occurs of the type for which theEventListenerinterface was registered.
91 * @param evt
92 TheEventcontains contextual information about the event. It also contains thestopPropagationand preventDefaultmethods which are used in determining the event's flow and default action.
95 EventListenerN1003B.prototype.handleEvent = function(evt) {
97 // bring class variables into function scope
99 var events = listener1.events;
100 var listeners = listener1.listeners;
101 var target;
102 var listener;
103 events[events.length] = evt;
104 target = evt.currentTarget;
106 for(var indexN10065 = 0;indexN10065 < listeners.length; indexN10065++) {
107 listener = listeners[indexN10065];
108 target.removeEventListener("foo", listener.handleEvent, false);
114 * Inner class implementation for variable listener2
116 var listener2;
119 * Constructor
121 * @param events Value from value attribute of nested var element
122 * @param listeners Value from value attribute of nested var element
125 function EventListenerN10074(events, listeners) {
126 this.events = events;
127 this.listeners = listeners;
132 This method is called whenever an event occurs of the type for which theEventListenerinterface was registered.
134 * @param evt
135 TheEventcontains contextual information about the event. It also contains thestopPropagationand preventDefaultmethods which are used in determining the event's flow and default action.
138 EventListenerN10074.prototype.handleEvent = function(evt) {
140 // bring class variables into function scope
142 var events = listener2.events;
143 var listeners = listener2.listeners;
144 var target;
145 var listener;
146 events[events.length] = evt;
147 target = evt.currentTarget;
149 for(var indexN10098 = 0;indexN10098 < listeners.length; indexN10098++) {
150 listener = listeners[indexN10098];
151 target.removeEventListener("foo", listener.handleEvent, false);
158 Two listeners are registered on the same target, each of which will remove both itself and
159 the other on the first event. Only one should see the event since event listeners
160 can never be invoked after being removed.
162 * @author Curt Arnold
163 * @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-EventTarget-dispatchEvent
164 * @see http://www.w3.org/TR/DOM-Level-2-Events/events#xpointer(id('Events-EventTarget-dispatchEvent')/raises/exception[@name='EventException']/descr/p[substring-before(.,':')='UNSPECIFIED_EVENT_TYPE_ERR'])
166 function dispatchEvent13() {
167 var success;
168 if(checkInitialization(builder, "dispatchEvent13") != null) return;
169 var doc;
170 var target;
171 var evt;
172 var preventDefault;
173 var listeners = new Array();
175 var events = new Array();
177 listener1 = new EventListenerN1003B(events, listeners);
179 listener2 = new EventListenerN10074(events, listeners);
181 var docRef = null;
182 if (typeof(this.doc) != 'undefined') {
183 docRef = this.doc;
185 doc = load(docRef, "doc", "hc_staff");
186 listeners[listeners.length] = listener1;
187 listeners[listeners.length] = listener2;
188 doc.addEventListener("foo", listener1.handleEvent, false);
189 doc.addEventListener("foo", listener2.handleEvent, false);
190 evt = doc.createEvent("Events");
191 evt.initEvent("foo",true,false);
192 preventDefault = doc.dispatchEvent(evt);
193 assertSize("eventCount",1,events);
197 function runTest() {
198 dispatchEvent13();