1<html>
2<head>
3<script>
4var testevent_divid2_called = false, testevent_document_called = false, cnt=0;
5
6function ok(b,m) {
7    return external.ok(b, m);
8}
9
10function broken(expr) {
11    return external.broken(expr);
12}
13
14function ondataavailable_test() {
15    var evobj, div, b = false;
16
17    div = document.getElementById("divid");
18
19    function ondataavailableHandler() {
20        b = true;
21        ok(window.event === evobj, "window.event != evobj");
22
23        ok(evobj.srcElement === div, "evobj.srcElement = " + evobj.srcElement);
24        ok(evobj.fromElement === null, "evobj.srcElement = " + evobj.fromElement);
25        ok(evobj.toElement === null, "evobj.srcElement = " + evobj.toElement);
26        ok(evobj.type === "dataavailable", "evobj.type = " + evobj.type);
27    }
28
29    function ondataavailableDocHandler() {
30        b = true;
31        ok(window.event === evobj, "window.event != evobj");
32
33        ok(evobj.srcElement === null, "evobj.srcElement = " + evobj.srcElement);
34        ok(evobj.fromElement === null, "evobj.srcElement = " + evobj.fromElement);
35        ok(evobj.toElement === null, "evobj.srcElement = " + evobj.toElement);
36        ok(evobj.type === "dataavailable", "evobj.type = " + evobj.type);
37    }
38
39    div.attachEvent("ondataavailable", ondataavailableHandler);
40
41    evobj = document.createEventObject();
42    ok(evobj.srcElement === null, "evobj.srcElement = " + evobj.srcElement);
43    ok(evobj.fromElement === null, "evobj.srcElement = " + evobj.fromElement);
44    ok(evobj.toElement === null, "evobj.srcElement = " + evobj.toElement);
45    ok(evobj.type === "", "evobj.type = " + evobj.type);
46
47    div.fireEvent("ondataavailable", evobj);
48    ok(b, "ondataavailable handler not called?");
49
50    ok(evobj.srcElement === div, "evobj.srcElement = " + evobj.srcElement);
51    ok(evobj.type === "dataavailable", "evobj.type = " + evobj.type);
52
53    div.detachEvent("ondataavailable", ondataavailableHandler);
54
55    b = false;
56    div.fireEvent("ondataavailable", evobj);
57    ok(!b, "ondataavailable handler called?");
58
59    ok(evobj.srcElement === div, "evobj.srcElement = " + evobj.srcElement);
60    ok(evobj.type === "dataavailable", "evobj.type = " + evobj.type);
61
62    document.attachEvent("ondataavailable", ondataavailableDocHandler);
63
64    evobj = document.createEventObject();
65
66    document.fireEvent("ondataavailable", evobj);
67    ok(b, "ondataavailable handler not called?");
68
69    ok(evobj.srcElement === null, "evobj.srcElement = " + evobj.srcElement);
70    ok(evobj.type === "dataavailable", "evobj.type = " + evobj.type);
71}
72
73function test_scriptfor() {
74    var div = document.getElementById("divid2");
75    ok("onclick" in div, "testevent not in div");
76    ok(typeof(div.onclick) === "function", "typeof(div.onclick) = " + typeof(div.onclick));
77    ok(testevent_divid2_called === false, "testevent_divid2_called = " + testevent_divid2_called);
78    ok(typeof(document.onclick) === "function", "typeof(document.onclick) = " + typeof(document.onclick));
79    ok(testevent_document_called === false, "testevent_document_called = " + testevent_document_called);
80    div.click();
81    ok(testevent_divid2_called === true, "testevent_divid2_called = " + testevent_divid2_called);
82    ok(testevent_document_called === true, "testevent_document_called = " + testevent_document_called);
83
84    ok(!("ontest" in div), "testevent in div");
85    ok(typeof(div.ontest) === "undefined", "typeof(div.ontest) = " + typeof(div.ontest));
86}
87
88function test_handler_this() {
89    document.body.innerHTML = '<div id="d1"><div id="d2"></div></div>';
90
91    var div1 = document.getElementById("d1");
92    var div2 = document.getElementById("d2");
93    var calls = new Array();
94
95    function createHandler(name, node) {
96        return function() {
97            ok(this === node, "this !== node");
98            calls.push(name);
99        }
100    }
101
102    function registerHandler(name, target) {
103        var b = target.attachEvent("onclick", function() {
104            ok(this === window, "this !== window");
105            calls.push(name+"*");
106        });
107        ok(b, "attachEvent failed");
108    }
109
110    registerHandler("div1", div1);
111    registerHandler("div2", div2);
112    registerHandler("body", document.body);
113    div1.onclick = createHandler("div1", div1);
114    div2.onclick = createHandler("div2", div2);
115    document.body.onclick = createHandler("body", document.body);
116
117    div2.click();
118    ok(calls == "div2,div2*,div1,div1*,body,body*", "calls = " + calls);
119}
120
121function test_insert_script() {
122    var e = document.createElement("script");
123
124    extern_cnt = 0;
125    e.src = "externscr.js";
126
127    var readystatechange_log = "";
128    e.onreadystatechange = function() {
129        readystatechange_log += "," + e.readyState;
130        ok(extern_cnt === 1, "extern_cnt = " + extern_cnt);
131        if(e.readyState === "complete") {
132            ok(readystatechange_log === "append,complete", "readystatechange_log = " + readystatechange_log);
133            external.reportSuccess();
134        }
135    }
136
137    document.body.appendChild(e);
138    ok(extern_cnt === 1, "extern_cnt = " + extern_cnt);
139    readystatechange_log = "append";
140}
141
142var string_handler_called = false;
143
144function test_string_event_handler() {
145    var e = document.createElement("div");
146    var event_str = "string_handler_called = true;";
147
148    document.body.appendChild(e);
149    e.onclick = event_str;
150    ok(e.onclick === event_str, "e.onclick = " + e.onclick);
151    e.click();
152    ok(string_handler_called === false, "string handler called");
153
154    e.setAttribute("onclick", event_str);
155    ok(e.onclick === event_str, "e.onclick = " + e.onclick);
156    e.click();
157    ok(string_handler_called === false, "string handler called");
158}
159
160window.onload = function() {
161    try {
162        ok(inlscr_complete_called, "onreadystatechange not fired");
163        ok(extern_res_script_rs === "eval,complete1,",
164            "extern_res_script_rs = " + extern_res_script_rs + " expected eval,complete1,");
165        ok(extern_http_script_rs === "loading,loaded,complete,",
166            "extern_http_script_rs = " + extern_http_script_rs + " expected loading,loaded,complete,");
167
168        ok(cnt == 1, "cnt=" + cnt + " exception during loading?");
169        ok(this === window, "this !== window");
170
171        ok(typeof(window.onload) === "function", "typeof(window.onload) = " + typeof(window.onload));
172        ok(document.body.onload === window.onload, "document.body.onload !== window.onload");
173
174        ok(img_onload_called, "img.onload not called before window.onload");
175
176        test_scriptfor();
177        ondataavailable_test();
178        test_handler_this();
179        test_insert_script();
180        test_string_event_handler();
181    }catch(e) {
182        ok(false, "Got an exception: " + e.message);
183    }
184}
185</script>
186<script>
187var img = document.createElement("img");
188img.src = "http://test.winehq.org/tests/winehq_snapshot/index_files/winehq_logo_glass.png";
189
190var img_onload_called = false;
191img.onload = function() {
192    external.trace("img.onload");
193    img_onload_called = true;
194}
195</script>
196</head>
197<body>
198<div id="divid"></div>
199<div id="divid2"></div>
200<script event="onclick" for="divid2">
201    testevent_divid2_called = true;
202</script>
203<script event="onclick" for="document">
204    testevent_document_called = true;
205</script>
206<script event="ontest" for="divid2">
207    ok(false, "unexpected ontest");
208</script>
209<script>
210(function() {
211    var div = document.getElementById("divid2");
212    ok(div.onclick === null, "div.onclick = null");
213    cnt++;
214})();
215</script>
216<script>
217var inlscr_complete_called = false, extern_res_script_rs = "", extern_http_script_rs = "";
218var extern_cnt = 0;
219
220function expect_inlscr_complete() {
221    var elem = document.getElementById("inlscr");
222    ok(elem.readyState === "complete" || broken(elem.readyState === "loading"), "expect_inlscr_complete: elem.readyState = " + elem.readyState);
223    if(elem.readyState === "complete")
224        inlscr_complete_called = true;
225}
226
227function extern_res_script_readystatechange() {
228    var elem = document.getElementById("extscr");
229    if(elem.readyState != "loading")
230        extern_res_script_rs += elem.readyState + extern_cnt + ",";
231    else
232        external.trace("Got loading readyState from external res script.");
233}
234
235function http_script_readystatechange() {
236    var elem = document.getElementById("httpscr");
237    extern_http_script_rs += elem.readyState + ",";
238}
239
240function on_externscr() {
241    var elem = document.getElementById("extscr");
242    ok(elem.readyState === "interactive" || broken(elem.readyState === "loading"),
243        "on_externscr: elem.readyState = " + elem.readyState);
244    extern_res_script_rs += "eval,";
245}
246
247</script>
248<script id="inlscr" onreadystatechange="expect_inlscr_complete();">
249(function() {
250    ok(inlscr_complete_called === false || broken(inlscr_complete_called), "onreadystatechange fired too early");
251
252    var elem = document.getElementById("inlscr");
253    ok(elem.readyState === "interactive", "inlscr: elem.readyState = " + elem.readyState);
254})();
255</script>
256<script id="extscr" onreadystatechange="extern_res_script_readystatechange()" src="externscr.js"></script>
257<script id="httpscr" onreadystatechange="http_script_readystatechange()" src="https://test.winehq.org/tests/empty.js?123456"></script>
258<script>
259document.body.appendChild(img);
260</script>
261</body>
262</html>
263