3 <script src=
"winetest.js" type=
"text/javascript"></script>
7 var testevent_divid2_called
= false, testevent_document_called
= false, cnt
=0;
9 function ondataavailable_test() {
10 var evobj
, div
, b
= false;
12 div
= document
.getElementById("divid");
14 function ondataavailableHandler() {
16 ok(window
.event
=== evobj
, "window.event != evobj");
18 ok(evobj
.srcElement
=== div
, "evobj.srcElement = " + evobj
.srcElement
);
19 ok(evobj
.fromElement
=== null, "evobj.srcElement = " + evobj
.fromElement
);
20 ok(evobj
.toElement
=== null, "evobj.srcElement = " + evobj
.toElement
);
21 ok(evobj
.type
=== "dataavailable", "evobj.type = " + evobj
.type
);
24 function ondataavailableDocHandler() {
26 ok(window
.event
=== evobj
, "window.event != evobj");
28 ok(evobj
.srcElement
=== null, "evobj.srcElement = " + evobj
.srcElement
);
29 ok(evobj
.fromElement
=== null, "evobj.srcElement = " + evobj
.fromElement
);
30 ok(evobj
.toElement
=== null, "evobj.srcElement = " + evobj
.toElement
);
31 ok(evobj
.type
=== "dataavailable", "evobj.type = " + evobj
.type
);
34 div
.attachEvent("ondataavailable", ondataavailableHandler
);
36 evobj
= document
.createEventObject();
37 ok(evobj
.srcElement
=== null, "evobj.srcElement = " + evobj
.srcElement
);
38 ok(evobj
.fromElement
=== null, "evobj.srcElement = " + evobj
.fromElement
);
39 ok(evobj
.toElement
=== null, "evobj.srcElement = " + evobj
.toElement
);
40 ok(evobj
.type
=== "", "evobj.type = " + evobj
.type
);
42 div
.fireEvent("ondataavailable", evobj
);
43 ok(b
, "ondataavailable handler not called?");
45 ok(evobj
.srcElement
=== div
, "evobj.srcElement = " + evobj
.srcElement
);
46 ok(evobj
.type
=== "dataavailable", "evobj.type = " + evobj
.type
);
48 div
.detachEvent("ondataavailable", ondataavailableHandler
);
51 div
.fireEvent("ondataavailable", evobj
);
52 ok(!b
, "ondataavailable handler called?");
54 ok(evobj
.srcElement
=== div
, "evobj.srcElement = " + evobj
.srcElement
);
55 ok(evobj
.type
=== "dataavailable", "evobj.type = " + evobj
.type
);
57 document
.attachEvent("ondataavailable", ondataavailableDocHandler
);
59 evobj
= document
.createEventObject();
61 document
.fireEvent("ondataavailable", evobj
);
62 ok(b
, "ondataavailable handler not called?");
64 ok(evobj
.srcElement
=== null, "evobj.srcElement = " + evobj
.srcElement
);
65 ok(evobj
.type
=== "dataavailable", "evobj.type = " + evobj
.type
);
68 function test_scriptfor() {
69 var div
= document
.getElementById("divid2");
70 ok("onclick" in div
, "testevent not in div");
71 ok(typeof(div
.onclick
) === "function", "typeof(div.onclick) = " + typeof(div
.onclick
));
72 ok(testevent_divid2_called
=== false, "testevent_divid2_called = " + testevent_divid2_called
);
73 ok(typeof(document
.onclick
) === "function", "typeof(document.onclick) = " + typeof(document
.onclick
));
74 ok(testevent_document_called
=== false, "testevent_document_called = " + testevent_document_called
);
76 ok(testevent_divid2_called
=== true, "testevent_divid2_called = " + testevent_divid2_called
);
77 ok(testevent_document_called
=== true, "testevent_document_called = " + testevent_document_called
);
79 ok(!("ontest" in div
), "testevent in div");
80 ok(typeof(div
.ontest
) === "undefined", "typeof(div.ontest) = " + typeof(div
.ontest
));
83 function test_handler_this() {
84 document
.body
.innerHTML
= '<div id="d1"><div id="d2"></div></div>';
86 var div1
= document
.getElementById("d1");
87 var div2
= document
.getElementById("d2");
88 var calls
= new Array();
90 function createHandler(name
, node
) {
92 ok(this === node
, "this !== node");
97 function registerHandler(name
, target
) {
98 var b
= target
.attachEvent("onclick", function(event_arg
) {
99 ok(this === window
, "this !== window");
100 calls
.push(name
+"*");
102 ok(event_arg
!= window
.event
, "event_arg == window.event");
104 ok(b
, "attachEvent failed");
107 registerHandler("div1", div1
);
108 registerHandler("div2", div2
);
109 registerHandler("body", document
.body
);
110 div1
.onclick
= createHandler("div1", div1
);
111 div2
.onclick
= createHandler("div2", div2
);
112 document
.body
.onclick
= createHandler("body", document
.body
);
115 ok(calls
== "div2,div2*,div1,div1*,body,body*", "calls = " + calls
);
118 function test_insert_script() {
119 var e
= document
.createElement("script");
122 e
.src
= "externscr.js";
124 var readystatechange_log
= "";
125 e
.onreadystatechange = function() {
126 ok(window
.event
.srcElement
=== e
, "srcElement != e");
127 readystatechange_log
+= "," + e
.readyState
;
128 ok(extern_cnt
=== 1, "extern_cnt = " + extern_cnt
);
129 if(e
.readyState
=== "complete") {
130 ok(readystatechange_log
=== "append,complete", "readystatechange_log = " + readystatechange_log
);
135 document
.body
.appendChild(e
);
136 ok(extern_cnt
=== 1, "extern_cnt = " + extern_cnt
);
137 readystatechange_log
= "append";
140 var string_handler_called
= false;
142 function test_string_event_handler() {
143 var e
= document
.createElement("div");
144 var event_str
= "string_handler_called = true;";
146 document
.body
.appendChild(e
);
147 e
.onclick
= event_str
;
148 ok(e
.onclick
=== event_str
, "e.onclick = " + e
.onclick
);
150 ok(string_handler_called
=== false, "string handler called");
152 e
.setAttribute("onclick", event_str
);
153 ok(e
.onclick
=== event_str
, "e.onclick = " + e
.onclick
);
155 ok(string_handler_called
=== false, "string handler called");
158 function test_body_events() {
159 var f = function() {}, g = function() {};
161 document
.body
.onload
= f
;
162 ok(document
.body
.onload
=== f
, "body.onload != f");
163 ok(window
.onload
=== f
, "window.onload != f");
165 document
.body
.onfocus
= f
;
166 ok(document
.body
.onfocus
=== f
, "body.onfocus != f");
167 ok(window
.onfocus
=== f
, "window.onfocus != f");
169 ok(document
.body
.onfocus
=== g
, "body.onfocus != g");
170 ok(window
.onfocus
=== g
, "window.onfocus != g");
172 var onclick_called
= false;
173 var onclick_handler = function() { onclick_called
= true; };
174 document
.body
.attachEvent("onclick", onclick_handler
);
175 window
.detachEvent("onclick", onclick_handler
);
176 document
.body
.click();
177 ok(onclick_called
, "onclick handler not invoked");
180 function test_event_target() {
181 var div
= document
.createElement("div");
182 document
.body
.appendChild(div
);
185 div
.attachEvent("onclick", function(event_arg
) {
186 ok(event_arg
.srcElement
=== div
, "srcElement != div");
187 last_event_arg
= event_arg
;
192 ok(last_event_arg
.srcElement
=== null, "srcElement != null");
194 document
.body
.removeChild(div
);
197 function test_attach_event() {
200 var div
= document
.createElement("div");
201 document
.body
.appendChild(div
);
203 function listener() {
204 calls
+= "listener,";
207 function listener2() {
208 calls
+= "listener2,";
211 /* if the same listener is added twice, it will be called twice */
212 div
.attachEvent("onclick", listener
);
213 div
.attachEvent("onclick", listener2
);
214 div
.attachEvent("onclick", listener
);
218 ok(calls
=== "listener,listener2,listener,", "calls = " + calls
);
220 /* remove listener once, it will be called once */
221 div
.detachEvent("onclick", listener
);
225 ok(calls
=== "listener2,listener,", "calls = " + calls
);
227 div
.detachEvent("onclick", listener
);
228 div
.detachEvent("onclick", listener2
);
232 ok(calls
=== "", "calls = " + calls
);
234 document
.body
.removeChild(div
);
237 function test_listener_order() {
238 if(!("documentMode" in document
)) {
239 win_skip("Skipping test_listener_order on too old IE");
243 var div
= document
.createElement("div");
244 document
.body
.appendChild(div
);
247 function record_call(msg
) {
248 return function() { calls
+= msg
+ "," };
251 div
.attachEvent("onclick", record_call("click1"));
252 div
.onclick
= record_call("onclick");
253 div
.attachEvent("onclick", record_call("click2"));
254 div
.attachEvent("onclick", record_call("click3"));
258 ok(calls
=== "onclick,click3,click2,click1,", "calls = " + calls
);
260 document
.body
.removeChild(div
);
263 function test_attach_in_attach() {
266 var div
= document
.createElement("div");
267 document
.body
.appendChild(div
);
269 /* listener attached inside onevent handler will be invoked in this propagation */
270 div
.onclick = function () {
271 calls
+= "div.onclick,";
272 div
.attachEvent("onclick", function() {
273 calls
+= "div.click,";
274 /* listener attached inside another attached listener will not be invoked */
275 div
.attachEvent("onclick", function () { ok(false, "unexpected call"); });
281 ok(calls
=== "div.onclick,div.click,", "calls = " + calls
);
283 document
.body
.removeChild(div
);
286 window
.onload = function() {
288 ok(inlscr_complete_called
, "onreadystatechange not fired");
289 ok(extern_res_script_rs
=== "eval,complete1," || extern_res_script_rs
== "loaded0,eval,complete1,",
290 "extern_res_script_rs = " + extern_res_script_rs
+ " expected (loaded0)?,eval,complete1,");
291 ok(extern_http_script_rs
=== "loading,loaded,complete," || extern_http_script_rs
=== "complete,",
292 "extern_http_script_rs = " + extern_http_script_rs
+ " expected loading,loaded,complete, or complete,");
294 ok(cnt
== 1, "cnt=" + cnt
+ " exception during loading?");
295 ok(this === window
, "this !== window");
297 ok(typeof(window
.onload
) === "function", "typeof(window.onload) = " + typeof(window
.onload
));
298 ok(document
.body
.onload
=== window
.onload
, "document.body.onload !== window.onload");
300 ok(img_onload_called
, "img.onload not called before window.onload");
303 ondataavailable_test();
305 test_insert_script();
306 test_string_event_handler();
310 test_listener_order();
311 test_attach_in_attach();
313 ok(false, "Got an exception: " + e
.message
);
318 var img
= document
.createElement("img");
319 img
.src
= "http://test.winehq.org/tests/winehq_snapshot/index_files/winehq_logo_glass.png";
321 var img_onload_called
= false;
322 img
.onload = function() {
324 img_onload_called
= true;
326 img
.onerror = function() {
327 trace("img.onerror");
328 img_onload_called
= true;
333 <div id=
"divid"></div>
334 <div id=
"divid2"></div>
335 <script event=
"onclick" for=
"divid2">
336 testevent_divid2_called = true;
338 <script event=
"onclick" for=
"document">
339 testevent_document_called = true;
341 <script event=
"ontest" for=
"divid2">
342 ok(false,
"unexpected ontest");
346 var div
= document
.getElementById("divid2");
347 ok(div
.onclick
=== null, "div.onclick = null");
352 var inlscr_complete_called
= false, extern_res_script_rs
= "", extern_http_script_rs
= "";
355 function expect_inlscr_complete() {
356 var elem
= document
.getElementById("inlscr");
357 ok(elem
.readyState
=== "complete" || broken(elem
.readyState
=== "loading"), "expect_inlscr_complete: elem.readyState = " + elem
.readyState
);
358 if(elem
.readyState
=== "complete")
359 inlscr_complete_called
= true;
362 function extern_res_script_readystatechange() {
363 var elem
= document
.getElementById("extscr");
364 if(elem
.readyState
!= "loading")
365 extern_res_script_rs
+= elem
.readyState
+ extern_cnt
+ ",";
367 trace("Got loading readyState from external res script.");
370 function http_script_readystatechange() {
371 var elem
= document
.getElementById("httpscr");
372 extern_http_script_rs
+= elem
.readyState
+ ",";
375 function on_externscr() {
376 var elem
= document
.getElementById("extscr");
377 ok(elem
.readyState
=== "interactive" || broken(elem
.readyState
=== "loading"),
378 "on_externscr: elem.readyState = " + elem
.readyState
);
379 extern_res_script_rs
+= "eval,";
383 <script id=
"inlscr" onreadystatechange=
"expect_inlscr_complete();">
385 ok(inlscr_complete_called === false || broken(inlscr_complete_called),
"onreadystatechange fired too early");
387 var elem = document.getElementById(
"inlscr");
388 ok(elem.readyState ===
"interactive",
"inlscr: elem.readyState = " + elem.readyState);
391 <script id=
"extscr" onreadystatechange=
"extern_res_script_readystatechange()" src=
"externscr.js"></script>
392 <script id=
"httpscr" onreadystatechange=
"http_script_readystatechange()" src=
"https://test.winehq.org/tests/empty.js?123456"></script>
394 document
.body
.appendChild(img
);