1 /*
2  * Copyright 2008-2011 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "precomp.h"
20 
21 #include <docobj.h>
22 
23 #define DEFINE_EXPECT(func) \
24     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
25 
26 #define SET_EXPECT(func) \
27     do { called_ ## func = FALSE; expect_ ## func = TRUE; } while(0)
28 
29 #define CHECK_EXPECT2(func) \
30     do { \
31     trace(#func "\n"); \
32         ok(expect_ ##func, "unexpected call " #func "\n"); \
33         called_ ## func = TRUE; \
34     }while(0)
35 
36 #define CHECK_EXPECT(func) \
37     do { \
38         CHECK_EXPECT2(func); \
39         expect_ ## func = FALSE; \
40     }while(0)
41 
42 #define CHECK_CALLED(func) \
43     do { \
44         ok(called_ ## func, "expected " #func "\n"); \
45         expect_ ## func = called_ ## func = FALSE; \
46     }while(0)
47 
48 DEFINE_EXPECT(document_onclick);
49 DEFINE_EXPECT(body_onclick);
50 DEFINE_EXPECT(doc_onclick_attached);
51 DEFINE_EXPECT(div_onclick);
52 DEFINE_EXPECT(div_onclick_attached);
53 DEFINE_EXPECT(timeout);
54 DEFINE_EXPECT(doccp_onclick);
55 DEFINE_EXPECT(doccp_onclick_cancel);
56 DEFINE_EXPECT(div_onclick_disp);
57 DEFINE_EXPECT(invoke_onclick);
58 DEFINE_EXPECT(iframe_onreadystatechange_loading);
59 DEFINE_EXPECT(iframe_onreadystatechange_interactive);
60 DEFINE_EXPECT(iframe_onreadystatechange_complete);
61 DEFINE_EXPECT(iframedoc_onreadystatechange);
62 DEFINE_EXPECT(img_onload);
63 DEFINE_EXPECT(img_onerror);
64 DEFINE_EXPECT(input_onfocus);
65 DEFINE_EXPECT(input_onblur);
66 DEFINE_EXPECT(form_onsubmit);
67 DEFINE_EXPECT(form_onclick);
68 DEFINE_EXPECT(submit_onclick);
69 DEFINE_EXPECT(submit_onclick_cancel);
70 DEFINE_EXPECT(submit_onclick_attached);
71 DEFINE_EXPECT(submit_onclick_attached_check_cancel);
72 DEFINE_EXPECT(submit_onclick_setret);
73 DEFINE_EXPECT(elem2_cp_onclick);
74 DEFINE_EXPECT(iframe_onload);
75 
76 static HWND container_hwnd = NULL;
77 static IHTMLWindow2 *window;
78 static IOleDocumentView *view;
79 static BOOL is_ie9plus;
80 
81 typedef struct {
82     LONG x;
83     LONG y;
84     LONG offset_x;
85     LONG offset_y;
86 } xy_test_t;
87 
88 static const xy_test_t no_xy = {-10,-10,-10,-10};
89 
90 static const char empty_doc_str[] =
91     "<html></html>";
92 
93 static const char click_doc_str[] =
94     "<html><body>"
95     "<div id=\"clickdiv\" style=\"text-align: center; background: red; font-size: 32\">click here</div>"
96     "</body></html>";
97 
98 static const char readystate_doc_str[] =
99     "<html><body><iframe id=\"iframe\"></iframe></body></html>";
100 
101 static const char img_doc_str[] =
102     "<html><body><img id=\"imgid\"></img></body></html>";
103 
104 static const char input_doc_str[] =
105     "<html><body><input id=\"inputid\"></input></body></html>";
106 
107 static const char iframe_doc_str[] =
108     "<html><body><iframe id=\"ifr\">Testing</iframe></body></html>";
109 
110 static const char form_doc_str[] =
111     "<html><body><form id=\"formid\" method=\"post\" action=\"about:blank\">"
112     "<input type=\"text\" value=\"test\" name=\"i\"/>"
113     "<input type=\"submit\" id=\"submitid\" />"
114     "</form></body></html>";
115 
116 static int strcmp_wa(LPCWSTR strw, const char *stra)
117 {
118     CHAR buf[512];
119     WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
120     return lstrcmpA(stra, buf);
121 }
122 
123 static BSTR a2bstr(const char *str)
124 {
125     BSTR ret;
126     int len;
127 
128     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
129     ret = SysAllocStringLen(NULL, len-1);
130     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
131 
132     return ret;
133 }
134 
135 static BOOL iface_cmp(IUnknown *iface1, IUnknown *iface2)
136 {
137     IUnknown *unk1, *unk2;
138 
139     if(iface1 == iface2)
140         return TRUE;
141 
142     IUnknown_QueryInterface(iface1, &IID_IUnknown, (void**)&unk1);
143     IUnknown_Release(unk1);
144     IUnknown_QueryInterface(iface1, &IID_IUnknown, (void**)&unk2);
145     IUnknown_Release(unk2);
146 
147     return unk1 == unk2;
148 }
149 
150 #define test_disp(u,id) _test_disp(__LINE__,u,id)
151 static void _test_disp(unsigned line, IUnknown *unk, const IID *diid)
152 {
153     IDispatchEx *dispex;
154     ITypeInfo *typeinfo;
155     UINT ticnt;
156     HRESULT hres;
157 
158     hres = IUnknown_QueryInterface(unk, &IID_IDispatchEx, (void**)&dispex);
159     ok_(__FILE__,line) (hres == S_OK, "Could not get IDispatch: %08x\n", hres);
160     if(FAILED(hres))
161         return;
162 
163     ticnt = 0xdeadbeef;
164     hres = IDispatchEx_GetTypeInfoCount(dispex, &ticnt);
165     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfoCount failed: %08x\n", hres);
166     ok_(__FILE__,line) (ticnt == 1, "ticnt=%u\n", ticnt);
167 
168     hres = IDispatchEx_GetTypeInfo(dispex, 0, 0, &typeinfo);
169     ok_(__FILE__,line) (hres == S_OK, "GetTypeInfo failed: %08x\n", hres);
170 
171     if(SUCCEEDED(hres)) {
172         TYPEATTR *type_attr;
173 
174         hres = ITypeInfo_GetTypeAttr(typeinfo, &type_attr);
175         ok_(__FILE__,line) (hres == S_OK, "GetTypeAttr failed: %08x\n", hres);
176         ok_(__FILE__,line) (IsEqualGUID(&type_attr->guid, diid), "unexpected guid %s\n",
177                             wine_dbgstr_guid(&type_attr->guid));
178 
179         ITypeInfo_ReleaseTypeAttr(typeinfo, type_attr);
180         ITypeInfo_Release(typeinfo);
181     }
182 
183     IDispatchEx_Release(dispex);
184 }
185 
186 #define get_doc3_iface(u) _get_doc3_iface(__LINE__,u)
187 static IHTMLDocument3 *_get_doc3_iface(unsigned line, IUnknown *unk)
188 {
189     IHTMLDocument3 *doc3;
190     HRESULT hres;
191 
192     hres = IUnknown_QueryInterface(unk, &IID_IHTMLDocument3, (void**)&doc3);
193     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLDocument3 iface: %08x\n", hres);
194 
195     return doc3;
196 }
197 
198 #define get_elem_iface(u) _get_elem_iface(__LINE__,u)
199 static IHTMLElement *_get_elem_iface(unsigned line, IUnknown *unk)
200 {
201     IHTMLElement *elem;
202     HRESULT hres;
203 
204     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement, (void**)&elem);
205     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLElement iface: %08x\n", hres);
206 
207     return elem;
208 }
209 
210 #define get_elem2_iface(u) _get_elem2_iface(__LINE__,u)
211 static IHTMLElement2 *_get_elem2_iface(unsigned line, IUnknown *unk)
212 {
213     IHTMLElement2 *elem2;
214     HRESULT hres;
215 
216     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement2, (void**)&elem2);
217     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLElement2 iface: %08x\n", hres);
218 
219     return elem2;
220 }
221 
222 #define get_elem3_iface(u) _get_elem3_iface(__LINE__,u)
223 static IHTMLElement3 *_get_elem3_iface(unsigned line, IUnknown *unk)
224 {
225     IHTMLElement3 *elem3;
226     HRESULT hres;
227 
228     hres = IUnknown_QueryInterface(unk, &IID_IHTMLElement3, (void**)&elem3);
229     ok_(__FILE__,line) (hres == S_OK, "Could not get IHTMLElement3 iface: %08x\n", hres);
230 
231     return elem3;
232 }
233 
234 #define get_iframe_iface(u) _get_iframe_iface(__LINE__,u)
235 static IHTMLIFrameElement *_get_iframe_iface(unsigned line, IUnknown *unk)
236 {
237     IHTMLIFrameElement *iframe;
238     HRESULT hres;
239 
240     hres = IUnknown_QueryInterface(unk, &IID_IHTMLIFrameElement, (void**)&iframe);
241     ok_(__FILE__,line)(hres == S_OK, "QueryInterface(IID_IHTMLIFrameElement) failed: %08x\n", hres);
242 
243     return iframe;
244 }
245 
246 #define doc_get_body(d) _doc_get_body(__LINE__,d)
247 static IHTMLElement *_doc_get_body(unsigned line, IHTMLDocument2 *doc)
248 {
249     IHTMLElement *body = NULL;
250     HRESULT hres;
251 
252     hres = IHTMLDocument2_get_body(doc, &body);
253     ok_(__FILE__,line) (hres == S_OK, "get_body failed: %08x\n", hres);
254     ok_(__FILE__,line) (body != NULL, "body == NULL\n");
255 
256     return body;
257 }
258 
259 #define get_elem_id(d,i) _get_elem_id(__LINE__,d,i)
260 static IHTMLElement *_get_elem_id(unsigned line, IHTMLDocument2 *doc, const char *id)
261 {
262     IHTMLDocument3 *doc3 = _get_doc3_iface(line, (IUnknown*)doc);
263     IHTMLElement *elem;
264     BSTR str;
265     HRESULT hres;
266 
267     str = a2bstr(id);
268     hres = IHTMLDocument3_getElementById(doc3, str, &elem);
269     SysFreeString(str);
270     IHTMLDocument3_Release(doc3);
271     ok_(__FILE__,line) (hres == S_OK, "getElementById failed: %08x\n", hres);
272 
273     return elem;
274 }
275 
276 #define test_elem_tag(u,n) _test_elem_tag(__LINE__,u,n)
277 static void _test_elem_tag(unsigned line, IUnknown *unk, const char *extag)
278 {
279     IHTMLElement *elem = _get_elem_iface(line, unk);
280     BSTR tag;
281     HRESULT hres;
282 
283     hres = IHTMLElement_get_tagName(elem, &tag);
284     IHTMLElement_Release(elem);
285     ok_(__FILE__, line) (hres == S_OK, "get_tagName failed: %08x\n", hres);
286     ok_(__FILE__, line) (!strcmp_wa(tag, extag), "got tag: %s, expected %s\n", wine_dbgstr_w(tag), extag);
287 
288     SysFreeString(tag);
289 }
290 
291 #define get_event_obj() _get_event_obj(__LINE__)
292 static IHTMLEventObj *_get_event_obj(unsigned line)
293 {
294     IHTMLEventObj *event = NULL;
295     HRESULT hres;
296 
297     hres = IHTMLWindow2_get_event(window, &event);
298     ok_(__FILE__,line) (hres == S_OK, "get_event failed: %08x\n", hres);
299     ok_(__FILE__,line) (event != NULL, "event = NULL\n");
300     _test_disp(line, (IUnknown*)event, &DIID_DispCEventObj);
301 
302     return event;
303 }
304 
305 #define elem_fire_event(a,b,c) _elem_fire_event(__LINE__,a,b,c)
306 static void _elem_fire_event(unsigned line, IUnknown *unk, const char *event, VARIANT *evobj)
307 {
308     IHTMLElement3 *elem3 = _get_elem3_iface(line, unk);
309     VARIANT_BOOL b;
310     BSTR str;
311     HRESULT hres;
312 
313     b = 100;
314     str = a2bstr(event);
315     hres = IHTMLElement3_fireEvent(elem3, str, evobj, &b);
316     SysFreeString(str);
317     ok_(__FILE__,line)(hres == S_OK, "fireEvent failed: %08x\n", hres);
318     ok_(__FILE__,line)(b == VARIANT_TRUE, "fireEvent returned %x\n", b);
319 }
320 
321 #define test_event_args(a,b,c,d,e,f,g) _test_event_args(__LINE__,a,b,c,d,e,f,g)
322 static void _test_event_args(unsigned line, const IID *dispiid, DISPID id, WORD wFlags, DISPPARAMS *pdp,
323         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
324 {
325     ok_(__FILE__,line) (id == DISPID_VALUE, "id = %d\n", id);
326     ok_(__FILE__,line) (wFlags == DISPATCH_METHOD, "wFlags = %x\n", wFlags);
327     ok_(__FILE__,line) (pdp != NULL, "pdp == NULL\n");
328     ok_(__FILE__,line) (pdp->cArgs == 1, "pdp->cArgs = %d\n", pdp->cArgs);
329     ok_(__FILE__,line) (pdp->cNamedArgs == 1, "pdp->cNamedArgs = %d\n", pdp->cNamedArgs);
330     ok_(__FILE__,line) (pdp->rgdispidNamedArgs[0] == DISPID_THIS, "pdp->rgdispidNamedArgs[0] = %d\n",
331                         pdp->rgdispidNamedArgs[0]);
332     ok_(__FILE__,line) (V_VT(pdp->rgvarg) == VT_DISPATCH, "V_VT(rgvarg) = %d\n", V_VT(pdp->rgvarg));
333     ok_(__FILE__,line) (pvarRes != NULL, "pvarRes == NULL\n");
334     ok_(__FILE__,line) (pei != NULL, "pei == NULL");
335     ok_(__FILE__,line) (!pspCaller, "pspCaller != NULL\n");
336 
337     if(dispiid)
338         _test_disp(line, (IUnknown*)V_DISPATCH(pdp->rgvarg), dispiid);
339 }
340 
341 #define test_attached_event_args(a,b,c,d,e) _test_attached_event_args(__LINE__,a,b,c,d,e)
342 static void _test_attached_event_args(unsigned line, DISPID id, WORD wFlags, DISPPARAMS *pdp,
343         VARIANT *pvarRes, EXCEPINFO *pei)
344 {
345     IHTMLEventObj *event;
346 
347     ok(id == DISPID_VALUE, "id = %d\n", id);
348     ok(wFlags == DISPATCH_METHOD, "wFlags = %x\n", wFlags);
349     ok(pdp != NULL, "pDispParams == NULL\n");
350     ok(pdp->cArgs == 1, "pdp->cArgs = %d\n", pdp->cArgs);
351     ok(!pdp->cNamedArgs, "pdp->cNamedArgs = %d\n", pdp->cNamedArgs);
352     ok(!pdp->rgdispidNamedArgs, "pdp->rgdispidNamedArgs = %p\n", pdp->rgdispidNamedArgs);
353     ok(pdp->rgvarg != NULL, "rgvarg = NULL\n");
354     ok(pvarRes != NULL, "pvarRes = NULL\n");
355     ok(pei != NULL, "pei = NULL\n");
356     ok(V_VT(pvarRes) == VT_EMPTY, "V_VT(pVarRes) = %d\n", V_VT(pvarRes));
357     ok(V_VT(pdp->rgvarg) == VT_DISPATCH, "V_VT(pdp->rgvarg) = %d\n", V_VT(pdp->rgvarg));
358     ok(V_DISPATCH(pdp->rgvarg) != NULL, "V_DISPATCH(pdp->rgvarg) = %p\n", V_DISPATCH(pdp->rgvarg));
359 
360     event = _get_event_obj(line);
361     ok(iface_cmp((IUnknown*)event, (IUnknown*)V_DISPATCH(pdp->rgvarg)), "event != arg0\n");
362     IHTMLEventObj_Release(event);
363 }
364 
365 #define get_event_src() _get_event_src(__LINE__)
366 static IHTMLElement *_get_event_src(unsigned line)
367 {
368     IHTMLEventObj *event = _get_event_obj(line);
369     IHTMLElement *src_elem = NULL;
370     HRESULT hres;
371 
372     hres = IHTMLEventObj_get_srcElement(event, &src_elem);
373     IHTMLEventObj_Release(event);
374     ok_(__FILE__,line) (hres == S_OK, "get_srcElement failed: %08x\n", hres);
375 
376     return src_elem;
377 }
378 
379 #define test_event_src(t) _test_event_src(__LINE__,t)
380 static void _test_event_src(unsigned line, const char *src_tag)
381 {
382     IHTMLElement *src_elem = _get_event_src(line);
383 
384     if(src_tag) {
385         ok_(__FILE__,line) (src_elem != NULL, "src_elem = NULL\n");
386         _test_elem_tag(line, (IUnknown*)src_elem, src_tag);
387         IHTMLElement_Release(src_elem);
388     }else {
389         ok_(__FILE__,line) (!src_elem, "src_elem != NULL\n");
390     }
391 }
392 
393 static void _test_event_altkey(unsigned line, IHTMLEventObj *event, VARIANT_BOOL exval)
394 {
395     VARIANT_BOOL b;
396     HRESULT hres;
397 
398     hres = IHTMLEventObj_get_altKey(event, &b);
399     ok_(__FILE__,line)(hres == S_OK, "get_altKey failed: %08x\n", hres);
400     ok_(__FILE__,line)(b == exval, "altKey = %x, expected %x\n", b, exval);
401 }
402 
403 static void _test_event_ctrlkey(unsigned line, IHTMLEventObj *event, VARIANT_BOOL exval)
404 {
405     VARIANT_BOOL b;
406     HRESULT hres;
407 
408     hres = IHTMLEventObj_get_ctrlKey(event, &b);
409     ok_(__FILE__,line)(hres == S_OK, "get_ctrlKey failed: %08x\n", hres);
410     ok_(__FILE__,line)(b == exval, "ctrlKey = %x, expected %x\n", b, exval);
411 }
412 
413 static void _test_event_shiftkey(unsigned line, IHTMLEventObj *event, VARIANT_BOOL exval)
414 {
415     VARIANT_BOOL b;
416     HRESULT hres;
417 
418     hres = IHTMLEventObj_get_shiftKey(event, &b);
419     ok_(__FILE__,line)(hres == S_OK, "get_shiftKey failed: %08x\n", hres);
420     ok_(__FILE__,line)(b == exval, "shiftKey = %x, expected %x\n", b, exval);
421 }
422 
423 #define test_event_cancelbubble(a,b) _test_event_cancelbubble(__LINE__,a,b)
424 static void _test_event_cancelbubble(unsigned line, IHTMLEventObj *event, VARIANT_BOOL exval)
425 {
426     VARIANT_BOOL b;
427     HRESULT hres;
428 
429     hres = IHTMLEventObj_get_cancelBubble(event, &b);
430     ok_(__FILE__,line)(hres == S_OK, "get_cancelBubble failed: %08x\n", hres);
431     ok_(__FILE__,line)(b == exval, "cancelBubble = %x, expected %x\n", b, exval);
432 }
433 
434 static void _test_event_fromelem(unsigned line, IHTMLEventObj *event, const char *from_tag)
435 {
436     IHTMLElement *elem;
437     HRESULT hres;
438 
439     hres = IHTMLEventObj_get_fromElement(event, &elem);
440     ok_(__FILE__,line)(hres == S_OK, "get_fromElement failed: %08x\n", hres);
441     if(from_tag)
442         _test_elem_tag(line, (IUnknown*)elem, from_tag);
443     else
444         ok_(__FILE__,line)(elem == NULL, "fromElement != NULL\n");
445     if(elem)
446         IHTMLElement_Release(elem);
447 }
448 
449 static void _test_event_toelem(unsigned line, IHTMLEventObj *event, const char *to_tag)
450 {
451     IHTMLElement *elem;
452     HRESULT hres;
453 
454     hres = IHTMLEventObj_get_toElement(event, &elem);
455     ok_(__FILE__,line)(hres == S_OK, "get_toElement failed: %08x\n", hres);
456     if(to_tag)
457         _test_elem_tag(line, (IUnknown*)elem, to_tag);
458     else
459         ok_(__FILE__,line)(elem == NULL, "toElement != NULL\n");
460     if(elem)
461         IHTMLElement_Release(elem);
462 }
463 
464 static void _test_event_keycode(unsigned line, IHTMLEventObj *event, LONG exl)
465 {
466     LONG l;
467     HRESULT hres;
468 
469     hres = IHTMLEventObj_get_keyCode(event, &l);
470     ok_(__FILE__,line)(hres == S_OK, "get_keyCode failed: %08x\n", hres);
471     ok_(__FILE__,line)(l == exl, "keyCode = %x, expected %x\n", l, exl);
472 }
473 
474 static void _test_event_button(unsigned line, IHTMLEventObj *event, LONG exl)
475 {
476     LONG l;
477     HRESULT hres;
478 
479     hres = IHTMLEventObj_get_button(event, &l);
480     ok_(__FILE__,line)(hres == S_OK, "get_button failed: %08x\n", hres);
481     ok_(__FILE__,line)(l == exl, "button = %x, expected %x\n", l, exl);
482 }
483 
484 static void _test_event_reason(unsigned line, IHTMLEventObj *event, LONG exl)
485 {
486     LONG l;
487     HRESULT hres;
488 
489     hres = IHTMLEventObj_get_reason(event, &l);
490     ok_(__FILE__,line)(hres == S_OK, "get_reason failed: %08x\n", hres);
491     ok_(__FILE__,line)(l == exl, "reason = %x, expected %x\n", l, exl);
492 }
493 
494 static void _test_event_x(unsigned line, IHTMLEventObj *event, LONG exl)
495 {
496     LONG l;
497     HRESULT hres;
498 
499     hres = IHTMLEventObj_get_x(event, &l);
500     ok_(__FILE__,line)(hres == S_OK, "get_x failed: %08x\n", hres);
501     if(exl != -10) /* don't test the exact value */
502         ok_(__FILE__,line)(l == exl, "x = %d, expected %d\n", l, exl);
503 }
504 
505 static void _test_event_y(unsigned line, IHTMLEventObj *event, LONG exl)
506 {
507     LONG l;
508     HRESULT hres;
509 
510     hres = IHTMLEventObj_get_y(event, &l);
511     ok_(__FILE__,line)(hres == S_OK, "get_y failed: %08x\n", hres);
512     if(exl != -10) /* don't test the exact value */
513         ok_(__FILE__,line)(l == exl, "y = %d, expected %d\n", l, exl);
514 }
515 
516 static void _test_event_clientx(unsigned line, IHTMLEventObj *event, LONG exl)
517 {
518     LONG l;
519     HRESULT hres;
520 
521     hres = IHTMLEventObj_get_clientX(event, &l);
522     ok_(__FILE__,line)(hres == S_OK, "get_clientX failed: %08x\n", hres);
523     if(exl != -10) /* don't test the exact value */
524         ok_(__FILE__,line)(l == exl, "clientX = %d, expected %d\n", l, exl);
525 }
526 
527 static void _test_event_clienty(unsigned line, IHTMLEventObj *event, LONG exl)
528 {
529     LONG l;
530     HRESULT hres;
531 
532     hres = IHTMLEventObj_get_clientY(event, &l);
533     ok_(__FILE__,line)(hres == S_OK, "get_clientY failed: %08x\n", hres);
534     if(exl != -10) /* don't test the exact value */
535         ok_(__FILE__,line)(l == exl, "clientY = %d, expected %d\n", l, exl);
536 }
537 
538 static void _test_event_offsetx(unsigned line, IHTMLEventObj *event, LONG exl)
539 {
540     LONG l;
541     HRESULT hres;
542 
543     hres = IHTMLEventObj_get_offsetX(event, &l);
544     ok_(__FILE__,line)(hres == S_OK, "get_offsetX failed: %08x\n", hres);
545     if(exl != -10) /* don't test the exact value */
546         ok_(__FILE__,line)(l == exl, "offsetX = %d, expected %d\n", l, exl);
547 }
548 
549 static void _test_event_offsety(unsigned line, IHTMLEventObj *event, LONG exl)
550 {
551     LONG l;
552     HRESULT hres;
553 
554     hres = IHTMLEventObj_get_offsetY(event, &l);
555     ok_(__FILE__,line)(hres == S_OK, "get_offsetY failed: %08x\n", hres);
556     if(exl != -10) /* don't test the exact value */
557         ok_(__FILE__,line)(l == exl, "offsetY = %d, expected %d\n", l, exl);
558 }
559 
560 static void _test_event_screenx(unsigned line, IHTMLEventObj *event, LONG exl)
561 {
562     LONG l;
563     HRESULT hres;
564 
565     hres = IHTMLEventObj_get_screenX(event, &l);
566     ok_(__FILE__,line)(hres == S_OK, "get_screenX failed: %08x\n", hres);
567     if(exl != -10) /* don't test the exact value */
568         ok_(__FILE__,line)(l == exl, "screenX = %d, expected %d\n", l, exl);
569 }
570 
571 static void _test_event_screeny(unsigned line, IHTMLEventObj *event, LONG exl)
572 {
573     LONG l;
574     HRESULT hres;
575 
576     hres = IHTMLEventObj_get_screenY(event, &l);
577     ok_(__FILE__,line)(hres == S_OK, "get_screenY failed: %08x\n", hres);
578     if(exl != -10) /* don't test the exact value for -10 */
579         ok_(__FILE__,line)(l == exl, "screenY = %d, expected %d\n", l, exl);
580 }
581 
582 static void _test_event_type(unsigned line, IHTMLEventObj *event, const char *exstr)
583 {
584     BSTR str;
585     HRESULT hres;
586 
587     hres = IHTMLEventObj_get_type(event, &str);
588     ok_(__FILE__,line)(hres == S_OK, "get_type failed: %08x\n", hres);
589     ok_(__FILE__,line)(!strcmp_wa(str, exstr), "type = %s, expected %s\n", wine_dbgstr_w(str), exstr);
590 }
591 
592 static void _test_event_qualifier(unsigned line, IHTMLEventObj *event, const char *exstr)
593 {
594     BSTR str;
595     HRESULT hres;
596 
597     hres = IHTMLEventObj_get_qualifier(event, &str);
598     ok_(__FILE__,line)(hres == S_OK, "get_qualifier failed: %08x\n", hres);
599     if(exstr)
600         ok_(__FILE__,line)(!strcmp_wa(str, exstr), "qualifier = %s, expected %s\n", wine_dbgstr_w(str), exstr);
601     else
602         ok_(__FILE__,line)(!str, "qualifier != NULL\n");
603 }
604 
605 static void _test_event_srcfilter(unsigned line, IHTMLEventObj *event)
606 {
607     IDispatch *disp;
608     HRESULT hres;
609 
610     hres = IHTMLEventObj_get_srcFilter(event, &disp);
611     ok_(__FILE__,line)(hres == S_OK, "get_srcFilter failed: %08x\n", hres);
612     ok_(__FILE__,line)(!disp, "srcFilter != NULL\n");
613 }
614 
615 #define test_event_obj(t,x) _test_event_obj(__LINE__,t,x)
616 static void _test_event_obj(unsigned line, const char *type, const xy_test_t *xy)
617 {
618     IHTMLEventObj *event = _get_event_obj(line);
619     VARIANT v;
620     HRESULT hres;
621 
622     _test_event_altkey(line, event, VARIANT_FALSE);
623     _test_event_ctrlkey(line, event, VARIANT_FALSE);
624     _test_event_shiftkey(line, event, VARIANT_FALSE);
625     _test_event_cancelbubble(line, event, VARIANT_FALSE);
626     _test_event_fromelem(line, event, NULL);
627     _test_event_toelem(line, event, NULL);
628     _test_event_keycode(line, event, 0);
629     _test_event_button(line, event, 0);
630     _test_event_type(line, event, type);
631     _test_event_qualifier(line, event, NULL);
632     _test_event_reason(line, event, 0);
633     _test_event_srcfilter(line, event);
634     _test_event_x(line, event, xy->x);
635     _test_event_y(line, event, xy->y);
636     _test_event_clientx(line, event, -10);
637     _test_event_clienty(line, event, -10);
638     _test_event_offsetx(line, event, xy->offset_x);
639     _test_event_offsety(line, event, xy->offset_y);
640     _test_event_screenx(line, event, -10);
641     _test_event_screeny(line, event, -10);
642 
643     V_VT(&v) = VT_NULL;
644     hres = IHTMLEventObj_get_returnValue(event, &v);
645     ok_(__FILE__,line)(hres == S_OK, "get_returnValue failed: %08x\n", hres);
646     ok_(__FILE__,line)(V_VT(&v) == VT_EMPTY, "V_VT(returnValue) = %d\n", V_VT(&v));
647 
648     IHTMLEventObj_Release(event);
649 }
650 
651 #define elem_attach_event(a,b,c) _elem_attach_event(__LINE__,a,b,c)
652 static void _elem_attach_event(unsigned line, IUnknown *unk, const char *namea, IDispatch *disp)
653 {
654     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
655     VARIANT_BOOL res;
656     BSTR name;
657     HRESULT hres;
658 
659     name = a2bstr(namea);
660     hres = IHTMLElement2_attachEvent(elem, name, disp, &res);
661     IHTMLElement2_Release(elem);
662     SysFreeString(name);
663     ok_(__FILE__,line)(hres == S_OK, "attachEvent failed: %08x\n", hres);
664     ok_(__FILE__,line)(res == VARIANT_TRUE, "attachEvent returned %x\n", res);
665 }
666 
667 #define elem_detach_event(a,b,c) _elem_detach_event(__LINE__,a,b,c)
668 static void _elem_detach_event(unsigned line, IUnknown *unk, const char *namea, IDispatch *disp)
669 {
670     IHTMLElement2 *elem = _get_elem2_iface(line, unk);
671     BSTR name;
672     HRESULT hres;
673 
674     name = a2bstr(namea);
675     hres = IHTMLElement2_detachEvent(elem, name, disp);
676     IHTMLElement2_Release(elem);
677     SysFreeString(name);
678     ok_(__FILE__,line)(hres == S_OK, "detachEvent failed: %08x\n", hres);
679 }
680 
681 #define doc_attach_event(a,b,c) _doc_attach_event(__LINE__,a,b,c)
682 static void _doc_attach_event(unsigned line, IHTMLDocument2 *doc, const char *namea, IDispatch *disp)
683 {
684     IHTMLDocument3 *doc3 = _get_doc3_iface(line, (IUnknown*)doc);
685     VARIANT_BOOL res;
686     BSTR name;
687     HRESULT hres;
688 
689     name = a2bstr(namea);
690     hres = IHTMLDocument3_attachEvent(doc3, name, disp, &res);
691     IHTMLDocument3_Release(doc3);
692     SysFreeString(name);
693     ok_(__FILE__,line)(hres == S_OK, "attachEvent failed: %08x\n", hres);
694     ok_(__FILE__,line)(res == VARIANT_TRUE, "attachEvent returned %x\n", res);
695 }
696 
697 #define doc_detach_event(a,b,c) _doc_detach_event(__LINE__,a,b,c)
698 static void _doc_detach_event(unsigned line, IHTMLDocument2 *doc, const char *namea, IDispatch *disp)
699 {
700     IHTMLDocument3 *doc3 = _get_doc3_iface(line, (IUnknown*)doc);
701     BSTR name;
702     HRESULT hres;
703 
704     name = a2bstr(namea);
705     hres = IHTMLDocument3_detachEvent(doc3, name, disp);
706     IHTMLDocument3_Release(doc3);
707     SysFreeString(name);
708     ok_(__FILE__,line)(hres == S_OK, "detachEvent failed: %08x\n", hres);
709 }
710 
711 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
712 {
713     *ppv = NULL;
714 
715     if(IsEqualGUID(riid, &IID_IUnknown)
716        || IsEqualGUID(riid, &IID_IDispatch)
717        || IsEqualGUID(riid, &IID_IDispatchEx))
718         *ppv = iface;
719     else {
720         ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid));
721         return E_NOINTERFACE;
722     }
723 
724     return S_OK;
725 }
726 
727 static HRESULT WINAPI Dispatch_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
728 {
729     *ppv = NULL;
730 
731     if(IsEqualGUID(riid, &IID_IUnknown)
732        || IsEqualGUID(riid, &IID_IDispatch)) {
733         *ppv = iface;
734     }else if(IsEqualGUID(riid, &IID_IDispatchEx)) {
735         return E_NOINTERFACE;
736     }else {
737         ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid));
738         return E_NOINTERFACE;
739     }
740 
741     return S_OK;
742 }
743 
744 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
745 {
746     return 2;
747 }
748 
749 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
750 {
751     return 1;
752 }
753 
754 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
755 {
756     ok(0, "unexpected call\n");
757     return E_NOTIMPL;
758 }
759 
760 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
761                                               LCID lcid, ITypeInfo **ppTInfo)
762 {
763     ok(0, "unexpected call\n");
764     return E_NOTIMPL;
765 }
766 
767 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
768                                                 LPOLESTR *rgszNames, UINT cNames,
769                                                 LCID lcid, DISPID *rgDispId)
770 {
771     ok(0, "unexpected call\n");
772     return E_NOTIMPL;
773 }
774 
775 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
776                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
777                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
778 {
779     ok(0, "unexpected call\n");
780     return E_NOTIMPL;
781 }
782 
783 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
784 {
785     ok(0, "unexpected call\n");
786     return E_NOTIMPL;
787 }
788 
789 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
790         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
791 {
792     ok(0, "unexpected call\n");
793     return E_NOTIMPL;
794 }
795 
796 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
797 {
798     ok(0, "unexpected call %s %x\n", wine_dbgstr_w(bstrName), grfdex);
799     return E_NOTIMPL;
800 }
801 
802 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
803 {
804     ok(0, "unexpected call\n");
805     return E_NOTIMPL;
806 }
807 
808 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
809 {
810     ok(0, "unexpected call\n");
811     return E_NOTIMPL;
812 }
813 
814 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
815 {
816     ok(0, "unexpected call\n");
817     return E_NOTIMPL;
818 }
819 
820 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
821 {
822     ok(0, "unexpected call\n");
823     return E_NOTIMPL;
824 }
825 
826 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
827 {
828     ok(0, "unexpected call\n");
829     return E_NOTIMPL;
830 }
831 
832 #define EVENT_HANDLER_FUNC_OBJ(event) \
833     static IDispatchExVtbl event ## FuncVtbl = { \
834         DispatchEx_QueryInterface, \
835         DispatchEx_AddRef, \
836         DispatchEx_Release, \
837         DispatchEx_GetTypeInfoCount, \
838         DispatchEx_GetTypeInfo, \
839         DispatchEx_GetIDsOfNames, \
840         DispatchEx_Invoke, \
841         DispatchEx_GetDispID, \
842         event, \
843         DispatchEx_DeleteMemberByName, \
844         DispatchEx_DeleteMemberByDispID, \
845         DispatchEx_GetMemberProperties, \
846         DispatchEx_GetMemberName, \
847         DispatchEx_GetNextDispID, \
848         DispatchEx_GetNameSpaceParent \
849     }; \
850     static IDispatchEx event ## _obj = { &event ## FuncVtbl };
851 
852 static HRESULT WINAPI document_onclick(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
853         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
854 {
855     IHTMLDocument3 *doc3;
856     CHECK_EXPECT(document_onclick);
857     test_event_args(NULL, id, wFlags, pdp, pvarRes, pei, pspCaller);
858     doc3 = get_doc3_iface((IUnknown*)V_DISPATCH(pdp->rgvarg));
859     IHTMLDocument3_Release(doc3);
860     test_event_src("DIV");
861     test_event_obj("click", &no_xy);
862     return S_OK;
863 }
864 
865 EVENT_HANDLER_FUNC_OBJ(document_onclick);
866 
867 static HRESULT WINAPI div_onclick(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
868         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
869 {
870     CHECK_EXPECT(div_onclick);
871     test_event_args(NULL, id, wFlags, pdp, pvarRes, pei, pspCaller);
872     test_event_src("DIV");
873     return S_OK;
874 }
875 
876 EVENT_HANDLER_FUNC_OBJ(div_onclick);
877 
878 static HRESULT WINAPI div_onclick_attached(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
879         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
880 {
881     CHECK_EXPECT(div_onclick_attached);
882 
883     test_attached_event_args(id, wFlags, pdp, pvarRes, pei);
884     test_event_src("DIV");
885     return S_OK;
886 }
887 
888 EVENT_HANDLER_FUNC_OBJ(div_onclick_attached);
889 
890 static HRESULT WINAPI doc_onclick_attached(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
891         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
892 {
893     CHECK_EXPECT(doc_onclick_attached);
894 
895     test_attached_event_args(id, wFlags, pdp, pvarRes, pei);
896     test_event_src("DIV");
897     return S_OK;
898 }
899 
900 EVENT_HANDLER_FUNC_OBJ(doc_onclick_attached);
901 
902 static HRESULT WINAPI body_onclick(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
903         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
904 {
905     CHECK_EXPECT(body_onclick);
906     test_event_args(&DIID_DispHTMLBody, id, wFlags, pdp, pvarRes, pei, pspCaller);
907     test_event_src("DIV");
908     return S_OK;
909 }
910 
911 EVENT_HANDLER_FUNC_OBJ(body_onclick);
912 
913 static HRESULT WINAPI img_onload(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
914         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
915 {
916     CHECK_EXPECT(img_onload);
917     test_event_args(&DIID_DispHTMLImg, id, wFlags, pdp, pvarRes, pei, pspCaller);
918     test_event_src("IMG");
919     return S_OK;
920 }
921 
922 EVENT_HANDLER_FUNC_OBJ(img_onload);
923 
924 static HRESULT WINAPI img_onerror(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
925         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
926 {
927     CHECK_EXPECT(img_onerror);
928     test_event_args(&DIID_DispHTMLImg, id, wFlags, pdp, pvarRes, pei, pspCaller);
929     test_event_src("IMG");
930     return S_OK;
931 }
932 
933 EVENT_HANDLER_FUNC_OBJ(img_onerror);
934 
935 static HRESULT WINAPI input_onfocus(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
936         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
937 {
938     CHECK_EXPECT(input_onfocus);
939     test_event_args(&DIID_DispHTMLInputElement, id, wFlags, pdp, pvarRes, pei, pspCaller);
940     test_event_src("INPUT");
941     return S_OK;
942 }
943 
944 EVENT_HANDLER_FUNC_OBJ(input_onfocus);
945 
946 static HRESULT WINAPI input_onblur(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
947         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
948 {
949     CHECK_EXPECT(input_onblur);
950     test_event_args(&DIID_DispHTMLInputElement, id, wFlags, pdp, pvarRes, pei, pspCaller);
951     test_event_src("INPUT");
952     return S_OK;
953 }
954 
955 EVENT_HANDLER_FUNC_OBJ(input_onblur);
956 
957 static HRESULT WINAPI form_onsubmit(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
958         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
959 {
960     CHECK_EXPECT(form_onsubmit);
961     test_event_args(NULL, id, wFlags, pdp, pvarRes, pei, pspCaller);
962     test_event_src("FORM");
963 
964     V_VT(pvarRes) = VT_BOOL;
965     V_BOOL(pvarRes) = VARIANT_FALSE;
966     return S_OK;
967 }
968 
969 EVENT_HANDLER_FUNC_OBJ(form_onsubmit);
970 
971 static HRESULT WINAPI form_onclick(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
972         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
973 {
974     CHECK_EXPECT(form_onclick);
975     test_event_args(NULL, id, wFlags, pdp, pvarRes, pei, pspCaller);
976 
977     return S_OK;
978 }
979 
980 EVENT_HANDLER_FUNC_OBJ(form_onclick);
981 
982 static HRESULT WINAPI submit_onclick(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
983         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
984 {
985     CHECK_EXPECT(submit_onclick);
986     test_event_args(NULL, id, wFlags, pdp, pvarRes, pei, pspCaller);
987     test_event_src("INPUT");
988 
989     V_VT(pvarRes) = VT_BOOL;
990     V_BOOL(pvarRes) = VARIANT_FALSE;
991     return S_OK;
992 }
993 
994 EVENT_HANDLER_FUNC_OBJ(submit_onclick);
995 
996 static HRESULT WINAPI iframe_onload(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
997         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
998 {
999     CHECK_EXPECT(iframe_onload);
1000     test_event_args(&DIID_DispHTMLIFrame, id, wFlags, pdp, pvarRes, pei, pspCaller);
1001     test_event_src("IFRAME");
1002     return S_OK;
1003 }
1004 
1005 EVENT_HANDLER_FUNC_OBJ(iframe_onload);
1006 
1007 static HRESULT WINAPI submit_onclick_attached(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1008         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1009 {
1010     CHECK_EXPECT(submit_onclick_attached);
1011     test_attached_event_args(id, wFlags, pdp, pvarRes, pei);
1012     test_event_src("INPUT");
1013 
1014     V_VT(pvarRes) = VT_BOOL;
1015     V_BOOL(pvarRes) = VARIANT_FALSE;
1016     return S_OK;
1017 }
1018 
1019 EVENT_HANDLER_FUNC_OBJ(submit_onclick_attached);
1020 
1021 static HRESULT WINAPI submit_onclick_attached_check_cancel(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1022         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1023 {
1024     IHTMLEventObj *event;
1025     HRESULT hres;
1026 
1027     CHECK_EXPECT(submit_onclick_attached_check_cancel);
1028     test_attached_event_args(id, wFlags, pdp, pvarRes, pei);
1029     test_event_src("INPUT");
1030 
1031     event = NULL;
1032     hres = IHTMLWindow2_get_event(window, &event);
1033     ok(hres == S_OK, "get_event failed: %08x\n", hres);
1034     ok(event != NULL, "event == NULL\n");
1035 
1036     test_event_cancelbubble(event, VARIANT_TRUE);
1037     return S_OK;
1038 }
1039 
1040 EVENT_HANDLER_FUNC_OBJ(submit_onclick_attached_check_cancel);
1041 
1042 static VARIANT onclick_retval, onclick_event_retval;
1043 
1044 static HRESULT WINAPI submit_onclick_setret(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1045         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1046 {
1047     IHTMLEventObj *event;
1048     VARIANT v;
1049     HRESULT hres;
1050 
1051     CHECK_EXPECT(submit_onclick_setret);
1052     test_event_args(NULL, id, wFlags, pdp, pvarRes, pei, pspCaller);
1053     test_event_src("INPUT");
1054 
1055     event = NULL;
1056     hres = IHTMLWindow2_get_event(window, &event);
1057     ok(hres == S_OK, "get_event failed: %08x\n", hres);
1058     ok(event != NULL, "event == NULL\n");
1059 
1060     V_VT(&v) = VT_ERROR;
1061     hres = IHTMLEventObj_get_returnValue(event, &v);
1062     ok(hres == S_OK, "get_returnValue failed: %08x\n", hres);
1063     ok(V_VT(&v) == VT_EMPTY, "V_VT(returnValue) = %d\n", V_VT(&v));
1064 
1065     hres = IHTMLEventObj_put_returnValue(event, onclick_event_retval);
1066     ok(hres == S_OK, "put_returnValue failed: %08x\n", hres);
1067 
1068     V_VT(&v) = VT_ERROR;
1069     hres = IHTMLEventObj_get_returnValue(event, &v);
1070     ok(hres == S_OK, "get_returnValue failed: %08x\n", hres);
1071     ok(VarCmp(&v, &onclick_event_retval, 0, 0) == VARCMP_EQ, "unexpected returnValue\n");
1072 
1073     IHTMLEventObj_Release(event);
1074     *pvarRes = onclick_retval;
1075     return S_OK;
1076 }
1077 
1078 EVENT_HANDLER_FUNC_OBJ(submit_onclick_setret);
1079 
1080 static HRESULT WINAPI submit_onclick_cancel(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1081         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1082 {
1083     IHTMLEventObj *event;
1084     HRESULT hres;
1085 
1086     CHECK_EXPECT(submit_onclick_cancel);
1087     test_event_args(NULL, id, wFlags, pdp, pvarRes, pei, pspCaller);
1088     test_event_src("INPUT");
1089 
1090     event = NULL;
1091     hres = IHTMLWindow2_get_event(window, &event);
1092     ok(hres == S_OK, "get_event failed: %08x\n", hres);
1093     ok(event != NULL, "event == NULL\n");
1094 
1095     test_event_cancelbubble(event, VARIANT_FALSE);
1096 
1097     hres = IHTMLEventObj_put_cancelBubble(event, VARIANT_TRUE);
1098     ok(hres == S_OK, "put_returnValue failed: %08x\n", hres);
1099 
1100     test_event_cancelbubble(event, VARIANT_TRUE);
1101     IHTMLEventObj_Release(event);
1102     return S_OK;
1103 }
1104 
1105 EVENT_HANDLER_FUNC_OBJ(submit_onclick_cancel);
1106 
1107 static HRESULT WINAPI iframedoc_onreadystatechange(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1108         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1109 {
1110     IHTMLEventObj *event = NULL;
1111     HRESULT hres;
1112 
1113     CHECK_EXPECT2(iframedoc_onreadystatechange);
1114     test_event_args(&DIID_DispHTMLDocument, id, wFlags, pdp, pvarRes, pei, pspCaller);
1115 
1116     event = (void*)0xdeadbeef;
1117     hres = IHTMLWindow2_get_event(window, &event);
1118     ok(hres == S_OK, "get_event failed: %08x\n", hres);
1119     ok(!event, "event = %p\n", event);
1120 
1121     return S_OK;
1122 }
1123 
1124 EVENT_HANDLER_FUNC_OBJ(iframedoc_onreadystatechange);
1125 
1126 static HRESULT WINAPI iframe_onreadystatechange(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1127         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1128 {
1129     IHTMLWindow2 *iframe_window;
1130     IHTMLDocument2 *iframe_doc;
1131     IHTMLFrameBase2 *iframe;
1132     IHTMLElement2 *elem2;
1133     IHTMLElement *elem;
1134     VARIANT v;
1135     BSTR str, str2;
1136     HRESULT hres;
1137 
1138     test_event_args(&DIID_DispHTMLIFrame, id, wFlags, pdp, pvarRes, pei, pspCaller);
1139     test_event_src("IFRAME");
1140 
1141     elem = get_event_src();
1142     elem2 = get_elem2_iface((IUnknown*)elem);
1143     IHTMLElement_Release(elem);
1144 
1145     V_VT(&v) = VT_EMPTY;
1146     hres = IHTMLElement2_get_readyState(elem2, &v);
1147     ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
1148     ok(V_VT(&v) == VT_BSTR, "V_VT(readyState) = %d\n", V_VT(&v));
1149 
1150     hres = IHTMLElement2_QueryInterface(elem2, &IID_IHTMLFrameBase2, (void**)&iframe);
1151     IHTMLElement2_Release(elem2);
1152     ok(hres == S_OK, "Could not get IHTMLFrameBase2 iface: %08x\n", hres);
1153 
1154     str = NULL;
1155     hres = IHTMLFrameBase2_get_readyState(iframe, &str);
1156     ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
1157     ok(str != NULL, "readyState == NULL\n");
1158     ok(!lstrcmpW(str, V_BSTR(&v)), "ready states differ\n");
1159     VariantClear(&v);
1160 
1161     hres = IHTMLFrameBase2_get_contentWindow(iframe, &iframe_window);
1162     ok(hres == S_OK, "get_contentDocument failed: %08x\n", hres);
1163 
1164     hres = IHTMLWindow2_get_document(iframe_window, &iframe_doc);
1165     IHTMLWindow2_Release(iframe_window);
1166     ok(hres == S_OK, "get_document failed: %08x\n", hres);
1167 
1168     hres = IHTMLDocument2_get_readyState(iframe_doc, &str2);
1169     ok(hres == S_OK, "get_document failed: %08x\n", hres);
1170     ok(!lstrcmpW(str, str2), "unexpected document readyState %s\n", wine_dbgstr_w(str2));
1171     SysFreeString(str2);
1172 
1173     if(!strcmp_wa(str, "loading")) {
1174         CHECK_EXPECT(iframe_onreadystatechange_loading);
1175 
1176         V_VT(&v) = VT_DISPATCH;
1177         V_DISPATCH(&v) = (IDispatch*)&iframedoc_onreadystatechange_obj;
1178         hres = IHTMLDocument2_put_onreadystatechange(iframe_doc, v);
1179         ok(hres == S_OK, "put_onreadystatechange: %08x\n", hres);
1180     }else if(!strcmp_wa(str, "interactive"))
1181         CHECK_EXPECT(iframe_onreadystatechange_interactive);
1182     else if(!strcmp_wa(str, "complete"))
1183         CHECK_EXPECT(iframe_onreadystatechange_complete);
1184     else
1185         ok(0, "unexpected state %s\n", wine_dbgstr_w(str));
1186 
1187     SysFreeString(str);
1188     IHTMLDocument2_Release(iframe_doc);
1189     IHTMLFrameBase2_Release(iframe);
1190     return S_OK;
1191 }
1192 
1193 EVENT_HANDLER_FUNC_OBJ(iframe_onreadystatechange);
1194 
1195 static HRESULT WINAPI nocall(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1196         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1197 {
1198     ok(0, "unexpected call\n");
1199     return S_OK;
1200 }
1201 
1202 EVENT_HANDLER_FUNC_OBJ(nocall);
1203 
1204 #define CONNECTION_POINT_OBJ(cpname, diid) \
1205     static HRESULT WINAPI cpname ## _QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv) \
1206     { \
1207         *ppv = NULL; \
1208         if(IsEqualGUID(riid, &IID_IUnknown) \
1209            || IsEqualGUID(riid, &IID_IDispatch) \
1210            || IsEqualGUID(riid, &diid)) \
1211             *ppv = iface; \
1212         else { \
1213             ok(0, "unexpected riid %s\n", wine_dbgstr_guid(riid)); \
1214             return E_NOINTERFACE; \
1215         } \
1216         return S_OK; \
1217     } \
1218     static IDispatchExVtbl cpname ## Vtbl = { \
1219         cpname ## _QueryInterface, \
1220         DispatchEx_AddRef,  \
1221         DispatchEx_Release, \
1222         DispatchEx_GetTypeInfoCount, \
1223         DispatchEx_GetTypeInfo, \
1224         DispatchEx_GetIDsOfNames, \
1225         cpname, \
1226         DispatchEx_GetDispID, \
1227         DispatchEx_InvokeEx, \
1228         DispatchEx_DeleteMemberByName, \
1229         DispatchEx_DeleteMemberByDispID, \
1230         DispatchEx_GetMemberProperties, \
1231         DispatchEx_GetMemberName, \
1232         DispatchEx_GetNextDispID, \
1233         DispatchEx_GetNameSpaceParent \
1234     }; \
1235     static IDispatchEx cpname ## _obj = { &cpname ## Vtbl }
1236 
1237 #define test_cp_args(a,b,c,d,e,f) _test_cp_args(__LINE__,a,b,c,d,e,f)
1238 static void _test_cp_args(unsigned line, REFIID riid, WORD flags, DISPPARAMS *dp, VARIANT *vres, EXCEPINFO *ei, UINT *argerr)
1239 {
1240     ok_(__FILE__,line)(IsEqualGUID(&IID_NULL, riid), "riid = %s\n", wine_dbgstr_guid(riid));
1241     ok_(__FILE__,line)(flags == DISPATCH_METHOD, "flags = %x\n", flags);
1242     ok_(__FILE__,line)(dp != NULL, "dp == NULL\n");
1243     ok_(__FILE__,line)(!dp->cArgs, "dp->cArgs = %d\n", dp->cArgs);
1244     ok_(__FILE__,line)(!dp->rgvarg, "dp->rgvarg = %p\n", dp->rgvarg);
1245     ok_(__FILE__,line)(!dp->cNamedArgs, "dp->cNamedArgs = %d\n", dp->cNamedArgs);
1246     ok_(__FILE__,line)(!dp->rgdispidNamedArgs, "dp->rgdispidNamedArgs = %p\n", dp->rgdispidNamedArgs);
1247     ok_(__FILE__,line)(vres != NULL, "vres == NULL\n");
1248     ok_(__FILE__,line)(V_VT(vres) == VT_EMPTY, "V_VT(vres) = %d\n", V_VT(vres));
1249     ok_(__FILE__,line)(ei != NULL, "ei == NULL\n");
1250     ok_(__FILE__,line)(argerr != NULL, "argerr == NULL\n");
1251 }
1252 
1253 #define test_cp_eventarg(a,b,c,d,e,f) _test_cp_eventarg(__LINE__,a,b,c,d,e,f)
1254 static void _test_cp_eventarg(unsigned line, REFIID riid, WORD flags, DISPPARAMS *dp, VARIANT *vres, EXCEPINFO *ei, UINT *argerr)
1255 {
1256     IHTMLEventObj *event;
1257 
1258     ok_(__FILE__,line)(IsEqualGUID(&IID_NULL, riid), "riid = %s\n", wine_dbgstr_guid(riid));
1259     ok_(__FILE__,line)(flags == DISPATCH_METHOD, "flags = %x\n", flags);
1260     ok_(__FILE__,line)(dp != NULL, "dp == NULL\n");
1261     ok_(__FILE__,line)(dp->cArgs == 1, "dp->cArgs = %d\n", dp->cArgs);
1262     ok_(__FILE__,line)(dp->rgvarg != NULL, "dp->rgvarg = %p\n", dp->rgvarg);
1263     ok_(__FILE__,line)(!dp->cNamedArgs, "dp->cNamedArgs = %d\n", dp->cNamedArgs);
1264     ok_(__FILE__,line)(!dp->rgdispidNamedArgs, "dp->rgdispidNamedArgs = %p\n", dp->rgdispidNamedArgs);
1265     ok_(__FILE__,line)(vres != NULL, "vres == NULL\n");
1266     ok_(__FILE__,line)(V_VT(vres) == VT_EMPTY, "V_VT(vres) = %d\n", V_VT(vres));
1267     ok_(__FILE__,line)(ei != NULL, "ei == NULL\n");
1268     ok_(__FILE__,line)(argerr != NULL, "argerr == NULL\n");
1269 
1270     ok(V_VT(dp->rgvarg) == VT_DISPATCH, "V_VT(dp->rgvarg) = %d\n", V_VT(dp->rgvarg));
1271     ok(V_DISPATCH(dp->rgvarg) != NULL, "V_DISPATCH(dp->rgvarg) = %p\n", V_DISPATCH(dp->rgvarg));
1272 
1273     event = _get_event_obj(line);
1274     ok(iface_cmp((IUnknown*)event, (IUnknown*)V_DISPATCH(dp->rgvarg)), "event != arg0\n");
1275     IHTMLEventObj_Release(event);
1276 }
1277 
1278 static HRESULT WINAPI doccp(IDispatchEx *iface, DISPID dispIdMember,
1279                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1280                             VARIANT *pVarResult, EXCEPINFO *pei, UINT *puArgErr)
1281 {
1282     switch(dispIdMember) {
1283     case DISPID_HTMLDOCUMENTEVENTS_ONCLICK:
1284         CHECK_EXPECT(doccp_onclick);
1285         test_cp_args(riid, wFlags, pdp, pVarResult, pei, puArgErr);
1286         break;
1287     default:
1288         ok(0, "unexpected call %d\n", dispIdMember);
1289         return E_NOTIMPL;
1290     }
1291 
1292     return S_OK;
1293 }
1294 
1295 CONNECTION_POINT_OBJ(doccp, DIID_HTMLDocumentEvents);
1296 
1297 static HRESULT WINAPI doccp_onclick_cancel(IDispatchEx *iface, DISPID dispIdMember,
1298         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pdp, VARIANT *pVarResult, EXCEPINFO *pei, UINT *puArgErr)
1299 {
1300     switch(dispIdMember) {
1301     case DISPID_HTMLDOCUMENTEVENTS_ONCLICK:
1302         CHECK_EXPECT(doccp_onclick_cancel);
1303         test_cp_args(riid, wFlags, pdp, pVarResult, pei, puArgErr);
1304         V_VT(pVarResult) = VT_BOOL;
1305         V_BOOL(pVarResult) = VARIANT_FALSE;
1306         break;
1307     default:
1308         ok(0, "unexpected call %d\n", dispIdMember);
1309         return E_NOTIMPL;
1310     }
1311 
1312     return S_OK;
1313 }
1314 
1315 CONNECTION_POINT_OBJ(doccp_onclick_cancel, DIID_HTMLDocumentEvents);
1316 
1317 static HRESULT WINAPI elem2_cp(IDispatchEx *iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1318         WORD wFlags, DISPPARAMS *pdp, VARIANT *pVarResult, EXCEPINFO *pei, UINT *puArgErr)
1319 {
1320     switch(dispIdMember) {
1321     case DISPID_HTMLDOCUMENTEVENTS_ONCLICK:
1322         CHECK_EXPECT(elem2_cp_onclick);
1323         test_cp_eventarg(riid, wFlags, pdp, pVarResult, pei, puArgErr);
1324         break;
1325     default:
1326         ok(0, "unexpected call %d\n", dispIdMember);
1327         return E_NOTIMPL;
1328     }
1329 
1330     return S_OK;
1331 }
1332 
1333 CONNECTION_POINT_OBJ(elem2_cp, DIID_HTMLElementEvents2);
1334 
1335 static HRESULT WINAPI timeoutFunc_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1336                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1337                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1338 {
1339     CHECK_EXPECT(timeout);
1340 
1341     ok(dispIdMember == DISPID_VALUE, "dispIdMember = %d\n", dispIdMember);
1342     ok(IsEqualGUID(&IID_NULL, riid), "riid = %s\n", wine_dbgstr_guid(riid));
1343     ok(wFlags == DISPATCH_METHOD, "wFlags = %x\n", wFlags);
1344     ok(!lcid, "lcid = %x\n", lcid);
1345     ok(pDispParams != NULL, "pDispParams == NULL\n");
1346     ok(!pDispParams->cArgs, "pdp->cArgs = %d\n", pDispParams->cArgs);
1347     ok(!pDispParams->cNamedArgs, "pdp->cNamedArgs = %d\n", pDispParams->cNamedArgs);
1348     ok(!pDispParams->rgdispidNamedArgs, "pdp->rgdispidNamedArgs = %p\n", pDispParams->rgdispidNamedArgs);
1349     ok(!pDispParams->rgvarg, "rgvarg = %p\n", pDispParams->rgvarg);
1350     ok(pVarResult != NULL, "pVarResult = NULL\n");
1351     ok(pExcepInfo != NULL, "pExcepInfo = NULL\n");
1352     ok(!puArgErr, "puArgErr = %p\n", puArgErr);
1353     ok(V_VT(pVarResult) == VT_EMPTY, "V_VT(pVarResult) = %d\n", V_VT(pVarResult));
1354 
1355     return S_OK;
1356 }
1357 
1358 static const IDispatchExVtbl timeoutFuncVtbl = {
1359     DispatchEx_QueryInterface,
1360     DispatchEx_AddRef,
1361     DispatchEx_Release,
1362     DispatchEx_GetTypeInfoCount,
1363     DispatchEx_GetTypeInfo,
1364     DispatchEx_GetIDsOfNames,
1365     timeoutFunc_Invoke,
1366     DispatchEx_GetDispID,
1367     DispatchEx_InvokeEx,
1368     DispatchEx_DeleteMemberByName,
1369     DispatchEx_DeleteMemberByDispID,
1370     DispatchEx_GetMemberProperties,
1371     DispatchEx_GetMemberName,
1372     DispatchEx_GetNextDispID,
1373     DispatchEx_GetNameSpaceParent
1374 };
1375 
1376 static IDispatchEx timeoutFunc = { &timeoutFuncVtbl };
1377 
1378 static HRESULT WINAPI div_onclick_disp_Invoke(IDispatchEx *iface, DISPID id,
1379         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1380         VARIANT *pvarRes, EXCEPINFO *pei, UINT *puArgErr)
1381 {
1382     CHECK_EXPECT(div_onclick_disp);
1383 
1384     test_attached_event_args(id, wFlags, pdp, pvarRes, pei);
1385 
1386     ok(IsEqualGUID(&IID_NULL, riid), "riid = %s\n", wine_dbgstr_guid(riid));
1387     ok(!puArgErr, "puArgErr = %p\n", puArgErr);
1388 
1389     return S_OK;
1390 }
1391 
1392 static const IDispatchExVtbl div_onclick_dispVtbl = {
1393     Dispatch_QueryInterface,
1394     DispatchEx_AddRef,
1395     DispatchEx_Release,
1396     DispatchEx_GetTypeInfoCount,
1397     DispatchEx_GetTypeInfo,
1398     DispatchEx_GetIDsOfNames,
1399     div_onclick_disp_Invoke,
1400 };
1401 
1402 static IDispatchEx div_onclick_disp = { &div_onclick_dispVtbl };
1403 
1404 static void pump_msgs(BOOL *b)
1405 {
1406     MSG msg;
1407 
1408     if(b) {
1409         while(!*b && GetMessageW(&msg, NULL, 0, 0)) {
1410             TranslateMessage(&msg);
1411             DispatchMessageW(&msg);
1412         }
1413     }else {
1414         while(PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
1415             TranslateMessage(&msg);
1416             DispatchMessageW(&msg);
1417         }
1418     }
1419 }
1420 
1421 static IConnectionPoint *get_cp(IUnknown *unk, REFIID riid)
1422 {
1423     IConnectionPointContainer *cp_container;
1424     IConnectionPoint *cp;
1425     HRESULT hres;
1426 
1427     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&cp_container);
1428     ok(hres == S_OK, "Could not get IConnectionPointContainer: %08x\n", hres);
1429 
1430     hres = IConnectionPointContainer_FindConnectionPoint(cp_container, riid, &cp);
1431     IConnectionPointContainer_Release(cp_container);
1432     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
1433 
1434     return cp;
1435 }
1436 
1437 static DWORD register_cp(IUnknown *unk, REFIID riid, IUnknown *sink)
1438 {
1439     IConnectionPoint *cp;
1440     DWORD cookie;
1441     HRESULT hres;
1442 
1443     cp = get_cp(unk, riid);
1444     hres = IConnectionPoint_Advise(cp, sink, &cookie);
1445     IConnectionPoint_Release(cp);
1446     ok(hres == S_OK, "Advise failed: %08x\n", hres);
1447 
1448     return cookie;
1449 }
1450 
1451 static void unregister_cp(IUnknown *unk, REFIID riid, DWORD cookie)
1452 {
1453     IConnectionPoint *cp;
1454     HRESULT hres;
1455 
1456     cp = get_cp(unk, riid);
1457     hres = IConnectionPoint_Unadvise(cp, cookie);
1458     IConnectionPoint_Release(cp);
1459     ok(hres == S_OK, "Unadvise failed: %08x\n", hres);
1460 }
1461 
1462 static HRESULT WINAPI EventDispatch_QueryInterface(IDispatch *iface, REFIID riid, void **ppv)
1463 {
1464     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IDispatch, riid)) {
1465         *ppv = iface;
1466         return S_OK;
1467     }
1468 
1469     ok(0, "Unexpected call\n");
1470     return E_NOINTERFACE;
1471 }
1472 
1473 static DWORD WINAPI EventDispatch_AddRef(IDispatch *iface)
1474 {
1475     return 2;
1476 }
1477 
1478 static DWORD WINAPI EventDispatch_Release(IDispatch *iface)
1479 {
1480     return 1;
1481 }
1482 
1483 static HRESULT WINAPI EventDispatch_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo)
1484 {
1485     ok(0, "Unexpected call\n");
1486     return E_NOTIMPL;
1487 }
1488 
1489 static HRESULT WINAPI EventDispatch_GetTypeInfo(IDispatch *iface, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
1490 {
1491     ok(0, "Unexpected call\n");
1492     return E_NOTIMPL;
1493 }
1494 
1495 static HRESULT WINAPI EventDispatch_GetIDsOfNames(IDispatch *iface, REFIID riid, LPOLESTR *rgszNames,
1496         UINT cNames, LCID lcid, DISPID *rgDispId)
1497 {
1498     ok(0, "Unexpected call\n");
1499     return E_NOTIMPL;
1500 }
1501 
1502 static HRESULT WINAPI EventDispatch_Invoke(IDispatch *iface, DISPID dispIdMember, REFIID riid,
1503         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1504         EXCEPINFO *pExcepInfo, UINT *puArgErr)
1505 {
1506     ok(IsEqualGUID(&IID_NULL, riid), "riid = %s\n", wine_dbgstr_guid(riid));
1507     ok(pDispParams != NULL, "pDispParams == NULL\n");
1508     ok(pExcepInfo != NULL, "pExcepInfo == NULL\n");
1509     ok(puArgErr != NULL, "puArgErr == NULL\n");
1510     ok(V_VT(pVarResult) == 0, "V_VT(pVarResult) = %d\n", V_VT(pVarResult));
1511     ok(wFlags == DISPATCH_METHOD, "wFlags = %d\n", wFlags);
1512 
1513     switch(dispIdMember) {
1514     case DISPID_HTMLDOCUMENTEVENTS_ONCLICK:
1515         CHECK_EXPECT2(invoke_onclick);
1516         break;
1517     case DISPID_HTMLDOCUMENTEVENTS2_ONPROPERTYCHANGE:
1518     case DISPID_HTMLDOCUMENTEVENTS2_ONREADYSTATECHANGE:
1519     case 1027:
1520     case 1034:
1521     case 1037:
1522     case 1044:
1523     case 1045:
1524     case 1047:
1525     case 1048:
1526     case 1049:
1527         break; /* TODO */
1528     default:
1529         ok(0, "Unexpected call: %d\n", dispIdMember);
1530     }
1531 
1532     return S_OK;
1533 }
1534 
1535 static const IDispatchVtbl EventDispatchVtbl = {
1536     EventDispatch_QueryInterface,
1537     EventDispatch_AddRef,
1538     EventDispatch_Release,
1539     EventDispatch_GetTypeInfoCount,
1540     EventDispatch_GetTypeInfo,
1541     EventDispatch_GetIDsOfNames,
1542     EventDispatch_Invoke
1543 };
1544 
1545 static IDispatch EventDispatch = { &EventDispatchVtbl };
1546 
1547 static void test_onclick(IHTMLDocument2 *doc)
1548 {
1549     DWORD cp_cookie, elem2_cp_cookie;
1550     IHTMLElement *div, *body;
1551     VARIANT v;
1552     HRESULT hres;
1553 
1554     register_cp((IUnknown*)doc, &IID_IDispatch, (IUnknown*)&EventDispatch);
1555 
1556     div = get_elem_id(doc, "clickdiv");
1557 
1558     elem_attach_event((IUnknown*)div, "abcde", (IDispatch*)&nocall_obj);
1559     elem_attach_event((IUnknown*)div, "onclick", (IDispatch*)&div_onclick_attached_obj);
1560 
1561     V_VT(&v) = VT_EMPTY;
1562     hres = IHTMLElement_get_onclick(div, &v);
1563     ok(hres == S_OK, "get_onclick failed: %08x\n", hres);
1564     ok(V_VT(&v) == VT_NULL, "V_VT(onclick) = %d\n", V_VT(&v));
1565 
1566     V_VT(&v) = VT_EMPTY;
1567     hres = IHTMLElement_put_onclick(div, v);
1568     ok(hres == E_NOTIMPL, "put_onclick failed: %08x\n", hres);
1569 
1570     V_VT(&v) = VT_BSTR;
1571     V_BSTR(&v) = a2bstr("function();");
1572     hres = IHTMLElement_put_onclick(div, v);
1573     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
1574 
1575     if(hres == S_OK) {
1576         V_VT(&v) = VT_EMPTY;
1577         hres = IHTMLElement_get_onclick(div, &v);
1578         ok(hres == S_OK, "get_onclick failed: %08x\n", hres);
1579         ok(V_VT(&v) == VT_BSTR, "V_VT(onclick) = %d\n", V_VT(&v));
1580         ok(!strcmp_wa(V_BSTR(&v), "function();"), "V_BSTR(onclick) = %s\n", wine_dbgstr_w(V_BSTR(&v)));
1581     }
1582     VariantClear(&v);
1583 
1584     V_VT(&v) = VT_DISPATCH;
1585     V_DISPATCH(&v) = (IDispatch*)&div_onclick_obj;
1586     hres = IHTMLElement_put_onclick(div, v);
1587     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
1588 
1589     V_VT(&v) = VT_NULL;
1590     hres = IHTMLElement_put_ondblclick(div, v);
1591     ok(hres == S_OK, "put_ondblclick failed: %08x\n", hres);
1592 
1593     V_VT(&v) = VT_EMPTY;
1594     hres = IHTMLElement_get_onclick(div, &v);
1595     ok(hres == S_OK, "get_onclick failed: %08x\n", hres);
1596     ok(V_VT(&v) == VT_DISPATCH, "V_VT(onclick) = %d\n", V_VT(&v));
1597     ok(V_DISPATCH(&v) == (IDispatch*)&div_onclick_obj, "V_DISPATCH(onclick) != onclickFunc\n");
1598     VariantClear(&v);
1599 
1600     V_VT(&v) = VT_EMPTY;
1601     hres = IHTMLDocument2_get_onclick(doc, &v);
1602     ok(hres == S_OK, "get_onclick failed: %08x\n", hres);
1603     ok(V_VT(&v) == VT_NULL, "V_VT(onclick) = %d\n", V_VT(&v));
1604 
1605     V_VT(&v) = VT_DISPATCH;
1606     V_DISPATCH(&v) = (IDispatch*)&document_onclick_obj;
1607     hres = IHTMLDocument2_put_onclick(doc, v);
1608     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
1609 
1610     V_VT(&v) = VT_EMPTY;
1611     hres = IHTMLDocument2_get_onclick(doc, &v);
1612     ok(hres == S_OK, "get_onclick failed: %08x\n", hres);
1613     ok(V_VT(&v) == VT_DISPATCH, "V_VT(onclick) = %d\n", V_VT(&v));
1614     ok(V_DISPATCH(&v) == (IDispatch*)&document_onclick_obj, "V_DISPATCH(onclick) != onclickFunc\n");
1615     VariantClear(&v);
1616 
1617     body = doc_get_body(doc);
1618 
1619     V_VT(&v) = VT_DISPATCH;
1620     V_DISPATCH(&v) = (IDispatch*)&body_onclick_obj;
1621     hres = IHTMLElement_put_onclick(body, v);
1622     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
1623 
1624     if(winetest_interactive) {
1625         SET_EXPECT(div_onclick);
1626         SET_EXPECT(div_onclick_attached);
1627         SET_EXPECT(body_onclick);
1628         SET_EXPECT(document_onclick);
1629         SET_EXPECT(invoke_onclick);
1630         pump_msgs(&called_document_onclick);
1631         CHECK_CALLED(div_onclick);
1632         CHECK_CALLED(div_onclick_attached);
1633         CHECK_CALLED(body_onclick);
1634         CHECK_CALLED(document_onclick);
1635         CHECK_CALLED(invoke_onclick);
1636     }
1637 
1638     SET_EXPECT(div_onclick);
1639     SET_EXPECT(div_onclick_attached);
1640     SET_EXPECT(body_onclick);
1641     SET_EXPECT(document_onclick);
1642     SET_EXPECT(invoke_onclick);
1643 
1644     hres = IHTMLElement_click(div);
1645     ok(hres == S_OK, "click failed: %08x\n", hres);
1646 
1647     CHECK_CALLED(div_onclick);
1648     CHECK_CALLED(div_onclick_attached);
1649     CHECK_CALLED(body_onclick);
1650     CHECK_CALLED(document_onclick);
1651     CHECK_CALLED(invoke_onclick);
1652 
1653     SET_EXPECT(div_onclick);
1654     SET_EXPECT(div_onclick_attached);
1655     SET_EXPECT(body_onclick);
1656     SET_EXPECT(document_onclick);
1657     SET_EXPECT(invoke_onclick);
1658 
1659     V_VT(&v) = VT_EMPTY;
1660     elem_fire_event((IUnknown*)div, "onclick", &v);
1661 
1662     CHECK_CALLED(div_onclick);
1663     CHECK_CALLED(div_onclick_attached);
1664     CHECK_CALLED(body_onclick);
1665     CHECK_CALLED(document_onclick);
1666     CHECK_CALLED(invoke_onclick);
1667 
1668     cp_cookie = register_cp((IUnknown*)doc, &DIID_HTMLDocumentEvents, (IUnknown*)&doccp_obj);
1669     elem_attach_event((IUnknown*)div, "onclick", (IDispatch*)&div_onclick_disp);
1670     doc_attach_event(doc, "onclick", (IDispatch*)&doc_onclick_attached_obj);
1671 
1672     SET_EXPECT(div_onclick);
1673     SET_EXPECT(div_onclick_disp);
1674     SET_EXPECT(div_onclick_attached);
1675     SET_EXPECT(body_onclick);
1676     SET_EXPECT(document_onclick);
1677     SET_EXPECT(doc_onclick_attached);
1678     SET_EXPECT(doccp_onclick);
1679     SET_EXPECT(invoke_onclick);
1680 
1681     hres = IHTMLElement_click(div);
1682     ok(hres == S_OK, "click failed: %08x\n", hres);
1683 
1684     CHECK_CALLED(div_onclick);
1685     CHECK_CALLED(div_onclick_disp);
1686     CHECK_CALLED(div_onclick_attached);
1687     CHECK_CALLED(body_onclick);
1688     CHECK_CALLED(document_onclick);
1689     CHECK_CALLED(doc_onclick_attached);
1690     CHECK_CALLED(doccp_onclick);
1691     CHECK_CALLED(invoke_onclick);
1692 
1693     elem2_cp_cookie = register_cp((IUnknown*)div, &DIID_HTMLElementEvents2, (IUnknown*)&elem2_cp_obj);
1694 
1695     SET_EXPECT(div_onclick);
1696     SET_EXPECT(div_onclick_disp);
1697     SET_EXPECT(div_onclick_attached);
1698     SET_EXPECT(elem2_cp_onclick);
1699     SET_EXPECT(body_onclick);
1700     SET_EXPECT(document_onclick);
1701     SET_EXPECT(doc_onclick_attached);
1702     SET_EXPECT(doccp_onclick);
1703     SET_EXPECT(invoke_onclick);
1704 
1705     trace("click >>>\n");
1706     hres = IHTMLElement_click(div);
1707     ok(hres == S_OK, "click failed: %08x\n", hres);
1708     trace("click <<<\n");
1709 
1710     CHECK_CALLED(div_onclick);
1711     CHECK_CALLED(div_onclick_disp);
1712     CHECK_CALLED(div_onclick_attached);
1713     CHECK_CALLED(elem2_cp_onclick);
1714     CHECK_CALLED(body_onclick);
1715     CHECK_CALLED(document_onclick);
1716     CHECK_CALLED(doc_onclick_attached);
1717     CHECK_CALLED(doccp_onclick);
1718     CHECK_CALLED(invoke_onclick);
1719 
1720     unregister_cp((IUnknown*)div, &DIID_HTMLElementEvents2, elem2_cp_cookie);
1721     unregister_cp((IUnknown*)doc, &DIID_HTMLDocumentEvents, cp_cookie);
1722 
1723     V_VT(&v) = VT_NULL;
1724     hres = IHTMLElement_put_onclick(div, v);
1725     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
1726 
1727     hres = IHTMLElement_get_onclick(div, &v);
1728     ok(hres == S_OK, "get_onclick failed: %08x\n", hres);
1729     ok(V_VT(&v) == VT_NULL, "get_onclick returned vt %d\n", V_VT(&v));
1730 
1731     elem_detach_event((IUnknown*)div, "onclick", (IDispatch*)&div_onclick_disp);
1732     elem_detach_event((IUnknown*)div, "onclick", (IDispatch*)&div_onclick_disp);
1733     elem_detach_event((IUnknown*)div, "test", (IDispatch*)&div_onclick_disp);
1734     doc_detach_event(doc, "onclick", (IDispatch*)&doc_onclick_attached_obj);
1735 
1736     SET_EXPECT(div_onclick_attached);
1737     SET_EXPECT(body_onclick);
1738     SET_EXPECT(document_onclick);
1739     SET_EXPECT(invoke_onclick);
1740 
1741     hres = IHTMLElement_click(div);
1742     ok(hres == S_OK, "click failed: %08x\n", hres);
1743 
1744     CHECK_CALLED(div_onclick_attached);
1745     CHECK_CALLED(body_onclick);
1746     CHECK_CALLED(document_onclick);
1747     CHECK_CALLED(invoke_onclick);
1748 
1749     IHTMLElement_Release(div);
1750     IHTMLElement_Release(body);
1751 }
1752 
1753 static void test_onreadystatechange(IHTMLDocument2 *doc)
1754 {
1755     IHTMLFrameBase *iframe;
1756     IHTMLElement2 *elem2;
1757     IHTMLElement *elem;
1758     VARIANT v;
1759     BSTR str;
1760     HRESULT hres;
1761 
1762     elem = get_elem_id(doc, "iframe");
1763     elem2 = get_elem2_iface((IUnknown*)elem);
1764     IHTMLElement_Release(elem);
1765 
1766     V_VT(&v) = VT_EMPTY;
1767     hres = IHTMLElement2_get_onreadystatechange(elem2, &v);
1768     ok(hres == S_OK, "get_onreadystatechange failed: %08x\n", hres);
1769     ok(V_VT(&v) == VT_NULL, "V_VT(onreadystatechange) = %d\n", V_VT(&v));
1770 
1771     V_VT(&v) = VT_DISPATCH;
1772     V_DISPATCH(&v) = (IDispatch*)&iframe_onreadystatechange_obj;
1773     hres = IHTMLElement2_put_onreadystatechange(elem2, v);
1774     ok(hres == S_OK, "put_onreadystatechange failed: %08x\n", hres);
1775 
1776     V_VT(&v) = VT_EMPTY;
1777     hres = IHTMLElement2_get_onreadystatechange(elem2, &v);
1778     ok(hres == S_OK, "get_onreadystatechange failed: %08x\n", hres);
1779     ok(V_VT(&v) == VT_DISPATCH, "V_VT(onreadystatechange) = %d\n", V_VT(&v));
1780     ok(V_DISPATCH(&v) == (IDispatch*)&iframe_onreadystatechange_obj, "unexpected onreadystatechange value\n");
1781 
1782     hres = IHTMLElement2_QueryInterface(elem2, &IID_IHTMLFrameBase, (void**)&iframe);
1783     IHTMLElement2_Release(elem2);
1784     ok(hres == S_OK, "Could not get IHTMLFrameBase iface: %08x\n", hres);
1785 
1786     hres = IHTMLFrameBase_put_src(iframe, (str = a2bstr("about:blank")));
1787     SysFreeString(str);
1788     ok(hres == S_OK, "put_src failed: %08x\n", hres);
1789 
1790     SET_EXPECT(iframe_onreadystatechange_loading);
1791     SET_EXPECT(iframedoc_onreadystatechange);
1792     SET_EXPECT(iframe_onreadystatechange_interactive);
1793     SET_EXPECT(iframe_onreadystatechange_complete);
1794     pump_msgs(&called_iframe_onreadystatechange_complete);
1795     CHECK_CALLED(iframe_onreadystatechange_loading);
1796     CHECK_CALLED(iframedoc_onreadystatechange);
1797     CHECK_CALLED(iframe_onreadystatechange_interactive);
1798     CHECK_CALLED(iframe_onreadystatechange_complete);
1799 
1800     IHTMLFrameBase_Release(iframe);
1801 }
1802 
1803 static void test_imgload(IHTMLDocument2 *doc)
1804 {
1805     IHTMLImgElement *img;
1806     IHTMLElement *elem;
1807     VARIANT v;
1808     BSTR str;
1809     HRESULT hres;
1810 
1811     elem = get_elem_id(doc, "imgid");
1812     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLImgElement, (void**)&img);
1813     IHTMLElement_Release(elem);
1814     ok(hres == S_OK, "Could not get IHTMLImgElement iface: %08x\n", hres);
1815 
1816     V_VT(&v) = VT_EMPTY;
1817     hres = IHTMLImgElement_get_onload(img, &v);
1818     ok(hres == S_OK, "get_onload failed: %08x\n", hres);
1819     ok(V_VT(&v) == VT_NULL, "V_VT(onload) = %d\n", V_VT(&v));
1820 
1821     V_VT(&v) = VT_DISPATCH;
1822     V_DISPATCH(&v) = (IDispatch*)&img_onload_obj;
1823     hres = IHTMLImgElement_put_onload(img, v);
1824     ok(hres == S_OK, "put_onload failed: %08x\n", hres);
1825 
1826     V_VT(&v) = VT_EMPTY;
1827     hres = IHTMLImgElement_get_onload(img, &v);
1828     ok(hres == S_OK, "get_onload failed: %08x\n", hres);
1829     ok(V_VT(&v) == VT_DISPATCH, "V_VT(onload) = %d\n", V_VT(&v));
1830     ok(V_DISPATCH(&v) == (IDispatch*)&img_onload_obj, "V_DISPATCH(onload) != onloadkFunc\n");
1831     VariantClear(&v);
1832 
1833     V_VT(&v) = VT_DISPATCH;
1834     V_DISPATCH(&v) = (IDispatch*)&img_onerror_obj;
1835     hres = IHTMLImgElement_put_onerror(img, v);
1836     ok(hres == S_OK, "put_onerror failed: %08x\n", hres);
1837 
1838     V_VT(&v) = VT_EMPTY;
1839     hres = IHTMLImgElement_get_onerror(img, &v);
1840     ok(hres == S_OK, "get_onerror failed: %08x\n", hres);
1841     ok(V_VT(&v) == VT_DISPATCH, "V_VT(onerror) = %d\n", V_VT(&v));
1842     ok(V_DISPATCH(&v) == (IDispatch*)&img_onerror_obj, "V_DISPATCH(onerror) != onerrorFunc\n");
1843     VariantClear(&v);
1844 
1845     str = a2bstr("http://test.winehq.org/tests/winehq_snapshot/index_files/winehq_logo_text.png");
1846     hres = IHTMLImgElement_put_src(img, str);
1847     ok(hres == S_OK, "put_src failed: %08x\n", hres);
1848     SysFreeString(str);
1849 
1850     SET_EXPECT(img_onload);
1851     pump_msgs(&called_img_onload);
1852     CHECK_CALLED(img_onload);
1853 
1854     SET_EXPECT(img_onerror);
1855 
1856     str = a2bstr("about:blank");
1857     hres = IHTMLImgElement_put_src(img, str);
1858     ok(hres == S_OK, "put_src failed: %08x\n", hres);
1859     SysFreeString(str);
1860 
1861     pump_msgs(&called_img_onerror); /* FIXME: should not be needed */
1862 
1863     CHECK_CALLED(img_onerror);
1864 
1865     IHTMLImgElement_Release(img);
1866 }
1867 
1868 static void test_focus(IHTMLDocument2 *doc)
1869 {
1870     IHTMLElement2 *elem2;
1871     IHTMLElement *elem;
1872     VARIANT v;
1873     HRESULT hres;
1874 
1875     elem = get_elem_id(doc, "inputid");
1876     elem2 = get_elem2_iface((IUnknown*)elem);
1877     IHTMLElement_Release(elem);
1878 
1879     V_VT(&v) = VT_EMPTY;
1880     hres = IHTMLElement2_get_onfocus(elem2, &v);
1881     ok(hres == S_OK, "get_onfocus failed: %08x\n", hres);
1882     ok(V_VT(&v) == VT_NULL, "V_VT(onfocus) = %d\n", V_VT(&v));
1883 
1884     V_VT(&v) = VT_DISPATCH;
1885     V_DISPATCH(&v) = (IDispatch*)&input_onfocus_obj;
1886     hres = IHTMLElement2_put_onfocus(elem2, v);
1887     ok(hres == S_OK, "put_onfocus failed: %08x\n", hres);
1888 
1889     V_VT(&v) = VT_EMPTY;
1890     hres = IHTMLElement2_get_onfocus(elem2, &v);
1891     ok(hres == S_OK, "get_onfocus failed: %08x\n", hres);
1892     ok(V_VT(&v) == VT_DISPATCH, "V_VT(onfocus) = %d\n", V_VT(&v));
1893     ok(V_DISPATCH(&v) == (IDispatch*)&input_onfocus_obj, "V_DISPATCH(onfocus) != onfocusFunc\n");
1894     VariantClear(&v);
1895 
1896     if(!winetest_interactive)
1897         ShowWindow(container_hwnd, SW_SHOW);
1898 
1899     SetFocus(NULL);
1900     ok(!IsChild(container_hwnd, GetFocus()), "focus belongs to document window\n");
1901 
1902     hres = IHTMLWindow2_focus(window);
1903     ok(hres == S_OK, "focus failed: %08x\n", hres);
1904 
1905     ok(IsChild(container_hwnd, GetFocus()), "focus does not belong to document window\n");
1906     pump_msgs(NULL);
1907 
1908     SET_EXPECT(input_onfocus);
1909     hres = IHTMLElement2_focus(elem2);
1910     pump_msgs(NULL);
1911     CHECK_CALLED(input_onfocus);
1912     ok(hres == S_OK, "focus failed: %08x\n", hres);
1913 
1914     V_VT(&v) = VT_DISPATCH;
1915     V_DISPATCH(&v) = (IDispatch*)&input_onblur_obj;
1916     hres = IHTMLElement2_put_onblur(elem2, v);
1917     ok(hres == S_OK, "put_onblur failed: %08x\n", hres);
1918 
1919     SET_EXPECT(input_onblur);
1920     hres = IHTMLElement2_blur(elem2);
1921     pump_msgs(NULL);
1922     CHECK_CALLED(input_onblur);
1923     ok(hres == S_OK, "blur failed: %08x\n", hres);
1924 
1925     if(!winetest_interactive)
1926         ShowWindow(container_hwnd, SW_HIDE);
1927 
1928     IHTMLElement2_Release(elem2);
1929 }
1930 
1931 static void test_submit(IHTMLDocument2 *doc)
1932 {
1933     IHTMLElement *elem, *submit;
1934     IHTMLFormElement *form;
1935     VARIANT v;
1936     DWORD cp_cookie;
1937     HRESULT hres;
1938 
1939     elem = get_elem_id(doc, "formid");
1940 
1941     V_VT(&v) = VT_DISPATCH;
1942     V_DISPATCH(&v) = (IDispatch*)&form_onclick_obj;
1943     hres = IHTMLElement_put_onclick(elem, v);
1944     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
1945 
1946     hres = IHTMLElement_QueryInterface(elem, &IID_IHTMLFormElement, (void**)&form);
1947     IHTMLElement_Release(elem);
1948     ok(hres == S_OK, "Could not get IHTMLFormElement iface: %08x\n", hres);
1949 
1950     V_VT(&v) = VT_DISPATCH;
1951     V_DISPATCH(&v) = (IDispatch*)&form_onsubmit_obj;
1952     hres = IHTMLFormElement_put_onsubmit(form, v);
1953     ok(hres == S_OK, "put_onsubmit failed: %08x\n", hres);
1954 
1955     IHTMLFormElement_Release(form);
1956 
1957     submit = get_elem_id(doc, "submitid");
1958 
1959     SET_EXPECT(form_onclick);
1960     SET_EXPECT(form_onsubmit);
1961     hres = IHTMLElement_click(submit);
1962     ok(hres == S_OK, "click failed: %08x\n", hres);
1963     CHECK_CALLED(form_onclick);
1964     CHECK_CALLED(form_onsubmit);
1965 
1966     V_VT(&v) = VT_DISPATCH;
1967     V_DISPATCH(&v) = (IDispatch*)&submit_onclick_obj;
1968     hres = IHTMLElement_put_onclick(submit, v);
1969     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
1970 
1971     SET_EXPECT(form_onclick);
1972     SET_EXPECT(submit_onclick);
1973     hres = IHTMLElement_click(submit);
1974     ok(hres == S_OK, "click failed: %08x\n", hres);
1975     CHECK_CALLED(form_onclick);
1976     CHECK_CALLED(submit_onclick);
1977 
1978     elem_attach_event((IUnknown*)submit, "onclick", (IDispatch*)&submit_onclick_attached_obj);
1979 
1980     SET_EXPECT(form_onclick);
1981     SET_EXPECT(submit_onclick);
1982     SET_EXPECT(submit_onclick_attached);
1983     hres = IHTMLElement_click(submit);
1984     ok(hres == S_OK, "click failed: %08x\n", hres);
1985     CHECK_CALLED(form_onclick);
1986     CHECK_CALLED(submit_onclick);
1987     CHECK_CALLED(submit_onclick_attached);
1988 
1989     V_VT(&v) = VT_NULL;
1990     hres = IHTMLElement_put_onclick(submit, v);
1991     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
1992 
1993     SET_EXPECT(form_onclick);
1994     SET_EXPECT(submit_onclick_attached);
1995     hres = IHTMLElement_click(submit);
1996     ok(hres == S_OK, "click failed: %08x\n", hres);
1997     CHECK_CALLED(form_onclick);
1998     CHECK_CALLED(submit_onclick_attached);
1999 
2000     elem_detach_event((IUnknown*)submit, "onclick", (IDispatch*)&submit_onclick_attached_obj);
2001 
2002     cp_cookie = register_cp((IUnknown*)doc, &DIID_HTMLDocumentEvents, (IUnknown*)&doccp_onclick_cancel_obj);
2003 
2004     SET_EXPECT(form_onclick);
2005     SET_EXPECT(doccp_onclick_cancel);
2006     hres = IHTMLElement_click(submit);
2007     ok(hres == S_OK, "click failed: %08x\n", hres);
2008     CHECK_CALLED(form_onclick);
2009     CHECK_CALLED(doccp_onclick_cancel);
2010 
2011     unregister_cp((IUnknown*)doc, &DIID_HTMLDocumentEvents, cp_cookie);
2012 
2013     V_VT(&v) = VT_DISPATCH;
2014     V_DISPATCH(&v) = (IDispatch*)&submit_onclick_setret_obj;
2015     hres = IHTMLElement_put_onclick(submit, v);
2016     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
2017 
2018     V_VT(&onclick_retval) = VT_BOOL;
2019     V_BOOL(&onclick_retval) = VARIANT_TRUE;
2020     V_VT(&onclick_event_retval) = VT_BOOL;
2021     V_BOOL(&onclick_event_retval) = VARIANT_TRUE;
2022 
2023     SET_EXPECT(submit_onclick_setret);
2024     SET_EXPECT(form_onclick);
2025     SET_EXPECT(form_onsubmit);
2026     hres = IHTMLElement_click(submit);
2027     ok(hres == S_OK, "click failed: %08x\n", hres);
2028     CHECK_CALLED(submit_onclick_setret);
2029     CHECK_CALLED(form_onclick);
2030     CHECK_CALLED(form_onsubmit);
2031 
2032     V_VT(&onclick_event_retval) = VT_BOOL;
2033     V_BOOL(&onclick_event_retval) = VARIANT_FALSE;
2034 
2035     SET_EXPECT(submit_onclick_setret);
2036     SET_EXPECT(form_onclick);
2037     hres = IHTMLElement_click(submit);
2038     ok(hres == S_OK, "click failed: %08x\n", hres);
2039     CHECK_CALLED(submit_onclick_setret);
2040     CHECK_CALLED(form_onclick);
2041 
2042     V_VT(&onclick_retval) = VT_BOOL;
2043     V_BOOL(&onclick_retval) = VARIANT_FALSE;
2044     V_VT(&onclick_event_retval) = VT_BOOL;
2045     V_BOOL(&onclick_event_retval) = VARIANT_TRUE;
2046 
2047     SET_EXPECT(submit_onclick_setret);
2048     SET_EXPECT(form_onclick);
2049     hres = IHTMLElement_click(submit);
2050     ok(hres == S_OK, "click failed: %08x\n", hres);
2051     CHECK_CALLED(submit_onclick_setret);
2052     CHECK_CALLED(form_onclick);
2053 
2054     V_VT(&onclick_event_retval) = VT_BOOL;
2055     V_BOOL(&onclick_event_retval) = VARIANT_FALSE;
2056 
2057     SET_EXPECT(submit_onclick_setret);
2058     SET_EXPECT(form_onclick);
2059     hres = IHTMLElement_click(submit);
2060     ok(hres == S_OK, "click failed: %08x\n", hres);
2061     CHECK_CALLED(submit_onclick_setret);
2062     CHECK_CALLED(form_onclick);
2063 
2064     elem_attach_event((IUnknown*)submit, "onclick", (IDispatch*)&submit_onclick_attached_obj);
2065     elem_attach_event((IUnknown*)submit, "onclick", (IDispatch*)&submit_onclick_attached_check_cancel_obj);
2066 
2067     V_VT(&v) = VT_DISPATCH;
2068     V_DISPATCH(&v) = (IDispatch*)&submit_onclick_cancel_obj;
2069     hres = IHTMLElement_put_onclick(submit, v);
2070     ok(hres == S_OK, "put_onclick failed: %08x\n", hres);
2071 
2072     SET_EXPECT(submit_onclick_cancel);
2073     SET_EXPECT(submit_onclick_attached_check_cancel);
2074     SET_EXPECT(submit_onclick_attached);
2075     hres = IHTMLElement_click(submit);
2076     ok(hres == S_OK, "click failed: %08x\n", hres);
2077     CHECK_CALLED(submit_onclick_cancel);
2078     CHECK_CALLED(submit_onclick_attached_check_cancel);
2079     CHECK_CALLED(submit_onclick_attached);
2080 
2081     if(1)pump_msgs(NULL);
2082 
2083     IHTMLElement_Release(submit);
2084 }
2085 
2086 static void test_timeout(IHTMLDocument2 *doc)
2087 {
2088     IHTMLWindow3 *win3;
2089     VARIANT expr, var;
2090     LONG id;
2091     HRESULT hres;
2092 
2093     hres = IHTMLWindow2_QueryInterface(window, &IID_IHTMLWindow3, (void**)&win3);
2094     ok(hres == S_OK, "Could not get IHTMLWindow3 iface: %08x\n", hres);
2095 
2096     V_VT(&expr) = VT_DISPATCH;
2097     V_DISPATCH(&expr) = (IDispatch*)&timeoutFunc;
2098     V_VT(&var) = VT_EMPTY;
2099     id = 0;
2100     hres = IHTMLWindow3_setTimeout(win3, &expr, 0, &var, &id);
2101     ok(hres == S_OK, "setTimeout failed: %08x\n", hres);
2102     ok(id, "id = 0\n");
2103 
2104     SET_EXPECT(timeout);
2105     pump_msgs(&called_timeout);
2106     CHECK_CALLED(timeout);
2107 
2108     V_VT(&expr) = VT_DISPATCH;
2109     V_DISPATCH(&expr) = (IDispatch*)&timeoutFunc;
2110     V_VT(&var) = VT_EMPTY;
2111     id = 0;
2112     hres = IHTMLWindow3_setTimeout(win3, &expr, 0, &var, &id);
2113     ok(hres == S_OK, "setTimeout failed: %08x\n", hres);
2114     ok(id, "id = 0\n");
2115 
2116     hres = IHTMLWindow2_clearTimeout(window, id);
2117     ok(hres == S_OK, "clearTimeout failed: %08x\n", hres);
2118 
2119     IHTMLWindow3_Release(win3);
2120 }
2121 
2122 static IHTMLElement* find_element_by_id(IHTMLDocument2 *doc, const char *id)
2123 {
2124     HRESULT hres;
2125     IHTMLDocument3 *doc3;
2126     IHTMLElement *result;
2127     BSTR idW = a2bstr(id);
2128 
2129     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument3, (void**)&doc3);
2130     ok(hres == S_OK, "QueryInterface(IID_IHTMLDocument3) failed: %08x\n", hres);
2131 
2132     hres = IHTMLDocument3_getElementById(doc3, idW, &result);
2133     ok(hres == S_OK, "getElementById failed: %08x\n", hres);
2134     ok(result != NULL, "result == NULL\n");
2135     SysFreeString(idW);
2136 
2137     IHTMLDocument3_Release(doc3);
2138     return result;
2139 }
2140 
2141 static IHTMLDocument2* get_iframe_doc(IHTMLIFrameElement *iframe)
2142 {
2143     HRESULT hres;
2144     IHTMLFrameBase2 *base;
2145     IHTMLDocument2 *result = NULL;
2146 
2147     hres = IHTMLIFrameElement_QueryInterface(iframe, &IID_IHTMLFrameBase2, (void**)&base);
2148     ok(hres == S_OK, "QueryInterface(IID_IHTMLFrameBase2) failed: %08x\n", hres);
2149     if(hres == S_OK) {
2150         IHTMLWindow2 *window;
2151 
2152         hres = IHTMLFrameBase2_get_contentWindow(base, &window);
2153         ok(hres == S_OK, "get_contentWindow failed: %08x\n", hres);
2154         ok(window != NULL, "window == NULL\n");
2155         if(window) {
2156             hres = IHTMLWindow2_get_document(window, &result);
2157             ok(hres == S_OK, "get_document failed: %08x\n", hres);
2158             ok(result != NULL, "result == NULL\n");
2159             IHTMLWindow2_Release(window);
2160         }
2161     }
2162     if(base) IHTMLFrameBase2_Release(base);
2163 
2164     return result;
2165 }
2166 
2167 static void test_iframe_connections(IHTMLDocument2 *doc)
2168 {
2169     IHTMLIFrameElement *iframe;
2170     IHTMLDocument2 *iframes_doc;
2171     DWORD cookie;
2172     IConnectionPoint *cp;
2173     IHTMLElement *element;
2174     BSTR str;
2175     HRESULT hres;
2176 
2177     trace("iframe tests...\n");
2178 
2179     element = find_element_by_id(doc, "ifr");
2180     iframe = get_iframe_iface((IUnknown*)element);
2181     IHTMLElement_Release(element);
2182 
2183     iframes_doc = get_iframe_doc(iframe);
2184     IHTMLIFrameElement_Release(iframe);
2185 
2186     cookie = register_cp((IUnknown*)iframes_doc, &IID_IDispatch, (IUnknown*)&div_onclick_disp);
2187 
2188     cp = get_cp((IUnknown*)doc, &IID_IDispatch);
2189     hres = IConnectionPoint_Unadvise(cp, cookie);
2190     IConnectionPoint_Release(cp);
2191     ok(hres == CONNECT_E_NOCONNECTION, "Unadvise returned %08x, expected CONNECT_E_NOCONNECTION\n", hres);
2192 
2193     unregister_cp((IUnknown*)iframes_doc, &IID_IDispatch, cookie);
2194 
2195     if(is_ie9plus) {
2196         IHTMLFrameBase2 *frame_base2;
2197         VARIANT v;
2198 
2199         hres = IHTMLIFrameElement_QueryInterface(iframe, &IID_IHTMLFrameBase2, (void**)&frame_base2);
2200         ok(hres == S_OK, "Could not get IHTMLFrameBase2 iface: %08x\n", hres);
2201 
2202         V_VT(&v) = VT_DISPATCH;
2203         V_DISPATCH(&v) = (IDispatch*)&iframe_onload_obj;
2204         hres = IHTMLFrameBase2_put_onload(frame_base2, v);
2205         ok(hres == S_OK, "put_onload failed: %08x\n", hres);
2206 
2207         IHTMLFrameBase2_Release(frame_base2);
2208 
2209         str = a2bstr("about:blank");
2210         hres = IHTMLDocument2_put_URL(iframes_doc, str);
2211         ok(hres == S_OK, "put_URL failed: %08x\n", hres);
2212         SysFreeString(str);
2213 
2214         SET_EXPECT(iframe_onload);
2215         pump_msgs(&called_iframe_onload);
2216         CHECK_CALLED(iframe_onload);
2217 
2218         str = a2bstr("about:test");
2219         hres = IHTMLDocument2_put_URL(iframes_doc, str);
2220         ok(hres == S_OK, "put_URL failed: %08x\n", hres);
2221         SysFreeString(str);
2222 
2223         SET_EXPECT(iframe_onload);
2224         pump_msgs(&called_iframe_onload);
2225         CHECK_CALLED(iframe_onload);
2226     }else {
2227         win_skip("Skipping iframe onload tests on IE older than 9.\n");
2228     }
2229 
2230     IHTMLDocument2_Release(iframes_doc);
2231 }
2232 
2233 static HRESULT QueryInterface(REFIID,void**);
2234 
2235 static HRESULT WINAPI InPlaceFrame_QueryInterface(IOleInPlaceFrame *iface, REFIID riid, void **ppv)
2236 {
2237     return E_NOINTERFACE;
2238 }
2239 
2240 static ULONG WINAPI InPlaceFrame_AddRef(IOleInPlaceFrame *iface)
2241 {
2242     return 2;
2243 }
2244 
2245 static ULONG WINAPI InPlaceFrame_Release(IOleInPlaceFrame *iface)
2246 {
2247     return 1;
2248 }
2249 
2250 static HRESULT WINAPI InPlaceFrame_GetWindow(IOleInPlaceFrame *iface, HWND *phwnd)
2251 {
2252     return E_NOTIMPL;
2253 }
2254 
2255 static HRESULT WINAPI InPlaceFrame_ContextSensitiveHelp(IOleInPlaceFrame *iface, BOOL fEnterMode)
2256 {
2257     return E_NOTIMPL;
2258 }
2259 
2260 static HRESULT WINAPI InPlaceFrame_GetBorder(IOleInPlaceFrame *iface, LPRECT lprectBorder)
2261 {
2262     return E_NOTIMPL;
2263 }
2264 
2265 static HRESULT WINAPI InPlaceFrame_RequestBorderSpace(IOleInPlaceFrame *iface,
2266         LPCBORDERWIDTHS pborderwidths)
2267 {
2268     return E_NOTIMPL;
2269 }
2270 
2271 static HRESULT WINAPI InPlaceFrame_SetBorderSpace(IOleInPlaceFrame *iface,
2272         LPCBORDERWIDTHS pborderwidths)
2273 {
2274     return S_OK;
2275 }
2276 
2277 static HRESULT WINAPI InPlaceUIWindow_SetActiveObject(IOleInPlaceFrame *iface,
2278         IOleInPlaceActiveObject *pActiveObject, LPCOLESTR pszObjName)
2279 {
2280     return S_OK;
2281 }
2282 
2283 static HRESULT WINAPI InPlaceFrame_SetActiveObject(IOleInPlaceFrame *iface,
2284         IOleInPlaceActiveObject *pActiveObject, LPCOLESTR pszObjName)
2285 {
2286     return S_OK;
2287 }
2288 
2289 static HRESULT WINAPI InPlaceFrame_InsertMenus(IOleInPlaceFrame *iface, HMENU hmenuShared,
2290         LPOLEMENUGROUPWIDTHS lpMenuWidths)
2291 {
2292     return E_NOTIMPL;
2293 }
2294 
2295 static HRESULT WINAPI InPlaceFrame_SetMenu(IOleInPlaceFrame *iface, HMENU hmenuShared,
2296         HOLEMENU holemenu, HWND hwndActiveObject)
2297 {
2298     ok(0, "unexpected call\n");
2299     return E_NOTIMPL;
2300 }
2301 
2302 static HRESULT WINAPI InPlaceFrame_RemoveMenus(IOleInPlaceFrame *iface, HMENU hmenuShared)
2303 {
2304     ok(0, "unexpected call\n");
2305     return E_NOTIMPL;
2306 }
2307 
2308 static HRESULT WINAPI InPlaceFrame_SetStatusText(IOleInPlaceFrame *iface, LPCOLESTR pszStatusText)
2309 {
2310     return S_OK;
2311 }
2312 
2313 static HRESULT WINAPI InPlaceFrame_EnableModeless(IOleInPlaceFrame *iface, BOOL fEnable)
2314 {
2315     return E_NOTIMPL;
2316 }
2317 
2318 static HRESULT WINAPI InPlaceFrame_TranslateAccelerator(IOleInPlaceFrame *iface, LPMSG lpmsg, WORD wID)
2319 {
2320     ok(0, "unexpected call\n");
2321     return E_NOTIMPL;
2322 }
2323 
2324 static const IOleInPlaceFrameVtbl InPlaceFrameVtbl = {
2325     InPlaceFrame_QueryInterface,
2326     InPlaceFrame_AddRef,
2327     InPlaceFrame_Release,
2328     InPlaceFrame_GetWindow,
2329     InPlaceFrame_ContextSensitiveHelp,
2330     InPlaceFrame_GetBorder,
2331     InPlaceFrame_RequestBorderSpace,
2332     InPlaceFrame_SetBorderSpace,
2333     InPlaceFrame_SetActiveObject,
2334     InPlaceFrame_InsertMenus,
2335     InPlaceFrame_SetMenu,
2336     InPlaceFrame_RemoveMenus,
2337     InPlaceFrame_SetStatusText,
2338     InPlaceFrame_EnableModeless,
2339     InPlaceFrame_TranslateAccelerator
2340 };
2341 
2342 static IOleInPlaceFrame InPlaceFrame = { &InPlaceFrameVtbl };
2343 
2344 static const IOleInPlaceFrameVtbl InPlaceUIWindowVtbl = {
2345     InPlaceFrame_QueryInterface,
2346     InPlaceFrame_AddRef,
2347     InPlaceFrame_Release,
2348     InPlaceFrame_GetWindow,
2349     InPlaceFrame_ContextSensitiveHelp,
2350     InPlaceFrame_GetBorder,
2351     InPlaceFrame_RequestBorderSpace,
2352     InPlaceFrame_SetBorderSpace,
2353     InPlaceUIWindow_SetActiveObject,
2354 };
2355 
2356 static IOleInPlaceFrame InPlaceUIWindow = { &InPlaceUIWindowVtbl };
2357 
2358 static HRESULT WINAPI InPlaceSite_QueryInterface(IOleInPlaceSite *iface, REFIID riid, void **ppv)
2359 {
2360     return QueryInterface(riid, ppv);
2361 }
2362 
2363 static ULONG WINAPI InPlaceSite_AddRef(IOleInPlaceSite *iface)
2364 {
2365     return 2;
2366 }
2367 
2368 static ULONG WINAPI InPlaceSite_Release(IOleInPlaceSite *iface)
2369 {
2370     return 1;
2371 }
2372 
2373 static HRESULT WINAPI InPlaceSite_GetWindow(IOleInPlaceSite *iface, HWND *phwnd)
2374 {
2375     *phwnd = container_hwnd;
2376     return S_OK;
2377 }
2378 
2379 static HRESULT WINAPI InPlaceSite_ContextSensitiveHelp(IOleInPlaceSite *iface, BOOL fEnterMode)
2380 {
2381     ok(0, "unexpected call\n");
2382     return E_NOTIMPL;
2383 }
2384 
2385 static HRESULT WINAPI InPlaceSite_CanInPlaceActivate(IOleInPlaceSite *iface)
2386 {
2387     return S_OK;
2388 }
2389 
2390 static HRESULT WINAPI InPlaceSite_OnInPlaceActivate(IOleInPlaceSite *iface)
2391 {
2392     return S_OK;
2393 }
2394 
2395 static HRESULT WINAPI InPlaceSite_OnUIActivate(IOleInPlaceSite *iface)
2396 {
2397     return S_OK;
2398 }
2399 
2400 static HRESULT WINAPI InPlaceSite_GetWindowContext(IOleInPlaceSite *iface,
2401         IOleInPlaceFrame **ppFrame, IOleInPlaceUIWindow **ppDoc, LPRECT lprcPosRect,
2402         LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
2403 {
2404     static const RECT rect = {0,0,300,300};
2405 
2406     *ppFrame = &InPlaceFrame;
2407     *ppDoc = (IOleInPlaceUIWindow*)&InPlaceUIWindow;
2408     *lprcPosRect = rect;
2409     *lprcClipRect = rect;
2410 
2411     ok(lpFrameInfo->cb == sizeof(*lpFrameInfo), "lpFrameInfo->cb = %u, expected %u\n", lpFrameInfo->cb, (unsigned)sizeof(*lpFrameInfo));
2412     lpFrameInfo->fMDIApp = FALSE;
2413     lpFrameInfo->hwndFrame = container_hwnd;
2414     lpFrameInfo->haccel = NULL;
2415     lpFrameInfo->cAccelEntries = 0;
2416 
2417     return S_OK;
2418 }
2419 
2420 static HRESULT WINAPI InPlaceSite_Scroll(IOleInPlaceSite *iface, SIZE scrollExtant)
2421 {
2422     return E_NOTIMPL;
2423 }
2424 
2425 static HRESULT WINAPI InPlaceSite_OnUIDeactivate(IOleInPlaceSite *iface, BOOL fUndoable)
2426 {
2427     return S_OK;
2428 }
2429 
2430 static HRESULT WINAPI InPlaceSite_OnInPlaceDeactivate(IOleInPlaceSite *iface)
2431 {
2432     return S_OK;
2433 }
2434 
2435 static HRESULT WINAPI InPlaceSite_DiscardUndoState(IOleInPlaceSite *iface)
2436 {
2437     return E_NOTIMPL;
2438 }
2439 
2440 static HRESULT WINAPI InPlaceSite_DeactivateAndUndo(IOleInPlaceSite *iface)
2441 {
2442     return E_NOTIMPL;
2443 }
2444 
2445 static HRESULT WINAPI InPlaceSite_OnPosRectChange(IOleInPlaceSite *iface, LPCRECT lprcPosRect)
2446 {
2447     return E_NOTIMPL;
2448 }
2449 
2450 static const IOleInPlaceSiteVtbl InPlaceSiteVtbl = {
2451     InPlaceSite_QueryInterface,
2452     InPlaceSite_AddRef,
2453     InPlaceSite_Release,
2454     InPlaceSite_GetWindow,
2455     InPlaceSite_ContextSensitiveHelp,
2456     InPlaceSite_CanInPlaceActivate,
2457     InPlaceSite_OnInPlaceActivate,
2458     InPlaceSite_OnUIActivate,
2459     InPlaceSite_GetWindowContext,
2460     InPlaceSite_Scroll,
2461     InPlaceSite_OnUIDeactivate,
2462     InPlaceSite_OnInPlaceDeactivate,
2463     InPlaceSite_DiscardUndoState,
2464     InPlaceSite_DeactivateAndUndo,
2465     InPlaceSite_OnPosRectChange,
2466 };
2467 
2468 static IOleInPlaceSite InPlaceSite = { &InPlaceSiteVtbl };
2469 
2470 static HRESULT WINAPI ClientSite_QueryInterface(IOleClientSite *iface, REFIID riid, void **ppv)
2471 {
2472     return QueryInterface(riid, ppv);
2473 }
2474 
2475 static ULONG WINAPI ClientSite_AddRef(IOleClientSite *iface)
2476 {
2477     return 2;
2478 }
2479 
2480 static ULONG WINAPI ClientSite_Release(IOleClientSite *iface)
2481 {
2482     return 1;
2483 }
2484 
2485 static HRESULT WINAPI ClientSite_SaveObject(IOleClientSite *iface)
2486 {
2487     ok(0, "unexpected call\n");
2488     return E_NOTIMPL;
2489 }
2490 
2491 static HRESULT WINAPI ClientSite_GetMoniker(IOleClientSite *iface, DWORD dwAssign, DWORD dwWhichMoniker,
2492         IMoniker **ppmon)
2493 {
2494     ok(0, "unexpected call\n");
2495     return E_NOTIMPL;
2496 }
2497 
2498 static HRESULT WINAPI ClientSite_GetContainer(IOleClientSite *iface, IOleContainer **ppContainer)
2499 {
2500     return E_NOTIMPL;
2501 }
2502 
2503 static HRESULT WINAPI ClientSite_ShowObject(IOleClientSite *iface)
2504 {
2505     ok(0, "unexpected call\n");
2506     return E_NOTIMPL;
2507 }
2508 
2509 static HRESULT WINAPI ClientSite_OnShowWindow(IOleClientSite *iface, BOOL fShow)
2510 {
2511     ok(0, "unexpected call\n");
2512     return E_NOTIMPL;
2513 }
2514 
2515 static HRESULT WINAPI ClientSite_RequestNewObjectLayout(IOleClientSite *iface)
2516 {
2517     ok(0, "unexpected call\n");
2518     return E_NOTIMPL;
2519 }
2520 
2521 static const IOleClientSiteVtbl ClientSiteVtbl = {
2522     ClientSite_QueryInterface,
2523     ClientSite_AddRef,
2524     ClientSite_Release,
2525     ClientSite_SaveObject,
2526     ClientSite_GetMoniker,
2527     ClientSite_GetContainer,
2528     ClientSite_ShowObject,
2529     ClientSite_OnShowWindow,
2530     ClientSite_RequestNewObjectLayout
2531 };
2532 
2533 static IOleClientSite ClientSite = { &ClientSiteVtbl };
2534 
2535 static HRESULT WINAPI DocumentSite_QueryInterface(IOleDocumentSite *iface, REFIID riid, void **ppv)
2536 {
2537     return QueryInterface(riid, ppv);
2538 }
2539 
2540 static ULONG WINAPI DocumentSite_AddRef(IOleDocumentSite *iface)
2541 {
2542     return 2;
2543 }
2544 
2545 static ULONG WINAPI DocumentSite_Release(IOleDocumentSite *iface)
2546 {
2547     return 1;
2548 }
2549 
2550 static HRESULT WINAPI DocumentSite_ActivateMe(IOleDocumentSite *iface, IOleDocumentView *pViewToActivate)
2551 {
2552     RECT rect = {0,0,300,300};
2553     IOleDocument *document;
2554     HRESULT hres;
2555 
2556     hres = IOleDocumentView_QueryInterface(pViewToActivate, &IID_IOleDocument, (void**)&document);
2557     ok(hres == S_OK, "could not get IOleDocument: %08x\n", hres);
2558 
2559     hres = IOleDocument_CreateView(document, &InPlaceSite, NULL, 0, &view);
2560     IOleDocument_Release(document);
2561     ok(hres == S_OK, "CreateView failed: %08x\n", hres);
2562 
2563     hres = IOleDocumentView_SetInPlaceSite(view, &InPlaceSite);
2564     ok(hres == S_OK, "SetInPlaceSite failed: %08x\n", hres);
2565 
2566     hres = IOleDocumentView_UIActivate(view, TRUE);
2567     ok(hres == S_OK, "UIActivate failed: %08x\n", hres);
2568 
2569     hres = IOleDocumentView_SetRect(view, &rect);
2570     ok(hres == S_OK, "SetRect failed: %08x\n", hres);
2571 
2572     hres = IOleDocumentView_Show(view, TRUE);
2573     ok(hres == S_OK, "Show failed: %08x\n", hres);
2574 
2575     return S_OK;
2576 }
2577 
2578 static const IOleDocumentSiteVtbl DocumentSiteVtbl = {
2579     DocumentSite_QueryInterface,
2580     DocumentSite_AddRef,
2581     DocumentSite_Release,
2582     DocumentSite_ActivateMe
2583 };
2584 
2585 static IOleDocumentSite DocumentSite = { &DocumentSiteVtbl };
2586 
2587 static HRESULT QueryInterface(REFIID riid, void **ppv)
2588 {
2589     *ppv = NULL;
2590 
2591     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IOleClientSite, riid))
2592         *ppv = &ClientSite;
2593     else if(IsEqualGUID(&IID_IOleDocumentSite, riid))
2594         *ppv = &DocumentSite;
2595     else if(IsEqualGUID(&IID_IOleWindow, riid) || IsEqualGUID(&IID_IOleInPlaceSite, riid))
2596         *ppv = &InPlaceSite;
2597 
2598     return *ppv ? S_OK : E_NOINTERFACE;
2599 }
2600 
2601 static IHTMLDocument2 *notif_doc;
2602 static BOOL doc_complete;
2603 
2604 static HRESULT WINAPI PropertyNotifySink_QueryInterface(IPropertyNotifySink *iface,
2605         REFIID riid, void**ppv)
2606 {
2607     if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
2608         *ppv = iface;
2609         return S_OK;
2610     }
2611 
2612     ok(0, "unexpected call\n");
2613     return E_NOINTERFACE;
2614 }
2615 
2616 static ULONG WINAPI PropertyNotifySink_AddRef(IPropertyNotifySink *iface)
2617 {
2618     return 2;
2619 }
2620 
2621 static ULONG WINAPI PropertyNotifySink_Release(IPropertyNotifySink *iface)
2622 {
2623     return 1;
2624 }
2625 
2626 static HRESULT WINAPI PropertyNotifySink_OnChanged(IPropertyNotifySink *iface, DISPID dispID)
2627 {
2628     if(dispID == DISPID_READYSTATE){
2629         BSTR state;
2630         HRESULT hres;
2631 
2632         hres = IHTMLDocument2_get_readyState(notif_doc, &state);
2633         ok(hres == S_OK, "get_readyState failed: %08x\n", hres);
2634 
2635         if(!strcmp_wa(state, "complete"))
2636             doc_complete = TRUE;
2637 
2638         SysFreeString(state);
2639     }
2640 
2641     return S_OK;
2642 }
2643 
2644 static HRESULT WINAPI PropertyNotifySink_OnRequestEdit(IPropertyNotifySink *iface, DISPID dispID)
2645 {
2646     ok(0, "unexpected call\n");
2647     return E_NOTIMPL;
2648 }
2649 
2650 static const IPropertyNotifySinkVtbl PropertyNotifySinkVtbl = {
2651     PropertyNotifySink_QueryInterface,
2652     PropertyNotifySink_AddRef,
2653     PropertyNotifySink_Release,
2654     PropertyNotifySink_OnChanged,
2655     PropertyNotifySink_OnRequestEdit
2656 };
2657 
2658 static IPropertyNotifySink PropertyNotifySink = { &PropertyNotifySinkVtbl };
2659 
2660 static void doc_load_string(IHTMLDocument2 *doc, const char *str)
2661 {
2662     IPersistStreamInit *init;
2663     IStream *stream;
2664     HGLOBAL mem;
2665     SIZE_T len;
2666 
2667     notif_doc = doc;
2668 
2669     doc_complete = FALSE;
2670     len = strlen(str);
2671     mem = GlobalAlloc(0, len);
2672     memcpy(mem, str, len);
2673     CreateStreamOnHGlobal(mem, TRUE, &stream);
2674 
2675     IHTMLDocument2_QueryInterface(doc, &IID_IPersistStreamInit, (void**)&init);
2676 
2677     IPersistStreamInit_Load(init, stream);
2678     IPersistStreamInit_Release(init);
2679     IStream_Release(stream);
2680 }
2681 
2682 static void do_advise(IUnknown *unk, REFIID riid, IUnknown *unk_advise)
2683 {
2684     IConnectionPointContainer *container;
2685     IConnectionPoint *cp;
2686     DWORD cookie;
2687     HRESULT hres;
2688 
2689     hres = IUnknown_QueryInterface(unk, &IID_IConnectionPointContainer, (void**)&container);
2690     ok(hres == S_OK, "QueryInterface(IID_IConnectionPointContainer) failed: %08x\n", hres);
2691 
2692     hres = IConnectionPointContainer_FindConnectionPoint(container, riid, &cp);
2693     IConnectionPointContainer_Release(container);
2694     ok(hres == S_OK, "FindConnectionPoint failed: %08x\n", hres);
2695 
2696     hres = IConnectionPoint_Advise(cp, unk_advise, &cookie);
2697     IConnectionPoint_Release(cp);
2698     ok(hres == S_OK, "Advise failed: %08x\n", hres);
2699 }
2700 
2701 static void set_client_site(IHTMLDocument2 *doc, BOOL set)
2702 {
2703     IOleObject *oleobj;
2704     HRESULT hres;
2705 
2706     if(!set && view) {
2707         IOleDocumentView_Show(view, FALSE);
2708         IOleDocumentView_CloseView(view, 0);
2709         IOleDocumentView_SetInPlaceSite(view, NULL);
2710         IOleDocumentView_Release(view);
2711         view = NULL;
2712     }
2713 
2714     hres = IHTMLDocument2_QueryInterface(doc, &IID_IOleObject, (void**)&oleobj);
2715     ok(hres == S_OK, "Could not et IOleObject: %08x\n", hres);
2716 
2717     hres = IOleObject_SetClientSite(oleobj, set ? &ClientSite : NULL);
2718     ok(hres == S_OK, "SetClientSite failed: %08x\n", hres);
2719 
2720     if(set) {
2721         IHlinkTarget *hlink;
2722 
2723         hres = IOleObject_QueryInterface(oleobj, &IID_IHlinkTarget, (void**)&hlink);
2724         ok(hres == S_OK, "Could not get IHlinkTarget iface: %08x\n", hres);
2725 
2726         hres = IHlinkTarget_Navigate(hlink, 0, NULL);
2727         ok(hres == S_OK, "Navgate failed: %08x\n", hres);
2728 
2729         IHlinkTarget_Release(hlink);
2730     }
2731 
2732     IOleObject_Release(oleobj);
2733 }
2734 static IHTMLDocument2 *create_document(void)
2735 {
2736     IHTMLDocument2 *doc;
2737     HRESULT hres;
2738 
2739     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
2740             &IID_IHTMLDocument2, (void**)&doc);
2741     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
2742     return SUCCEEDED(hres) ? doc : NULL;
2743 }
2744 
2745 
2746 typedef void (*testfunc_t)(IHTMLDocument2*);
2747 
2748 static void run_test(const char *str, testfunc_t test)
2749 {
2750     IHTMLDocument2 *doc;
2751     IHTMLElement *body = NULL;
2752     MSG msg;
2753     HRESULT hres;
2754 
2755     doc = create_document();
2756     if (!doc)
2757         return;
2758     set_client_site(doc, TRUE);
2759     doc_load_string(doc, str);
2760     do_advise((IUnknown*)doc, &IID_IPropertyNotifySink, (IUnknown*)&PropertyNotifySink);
2761 
2762     while(!doc_complete && GetMessageA(&msg, NULL, 0, 0)) {
2763         TranslateMessage(&msg);
2764         DispatchMessageA(&msg);
2765     }
2766 
2767     hres = IHTMLDocument2_get_body(doc, &body);
2768     ok(hres == S_OK, "get_body failed: %08x\n", hres);
2769 
2770     if(body) {
2771         IHTMLElement_Release(body);
2772 
2773         hres = IHTMLDocument2_get_parentWindow(doc, &window);
2774         ok(hres == S_OK, "get_parentWindow failed: %08x\n", hres);
2775         ok(window != NULL, "window == NULL\n");
2776 
2777         test(doc);
2778 
2779         IHTMLWindow2_Release(window);
2780         window = NULL;
2781     }else {
2782         skip("Could not get document body. Assuming no Gecko installed.\n");
2783     }
2784 
2785     set_client_site(doc, FALSE);
2786     IHTMLDocument2_Release(doc);
2787 }
2788 
2789 static LRESULT WINAPI wnd_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2790 {
2791     return DefWindowProcA(hwnd, msg, wParam, lParam);
2792 }
2793 
2794 static HWND create_container_window(void)
2795 {
2796     static const CHAR szHTMLDocumentTest[] = "HTMLDocumentTest";
2797     static WNDCLASSEXA wndclass = {
2798         sizeof(WNDCLASSEXA),
2799         0,
2800         wnd_proc,
2801         0, 0, NULL, NULL, NULL, NULL, NULL,
2802         szHTMLDocumentTest,
2803         NULL
2804     };
2805 
2806     RegisterClassExA(&wndclass);
2807     return CreateWindowA(szHTMLDocumentTest, szHTMLDocumentTest,
2808             WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
2809             300, 300, NULL, NULL, NULL, NULL);
2810 }
2811 
2812 static void test_empty_document(void)
2813 {
2814     HRESULT hres;
2815     IHTMLWindow2 *window;
2816     IHTMLDocument2 *windows_doc, *doc;
2817     IConnectionPoint *cp;
2818     DWORD cookie;
2819 
2820     doc = create_document();
2821     if(!doc)
2822         return;
2823 
2824     hres = IHTMLDocument2_get_parentWindow(doc, &window);
2825     ok(hres == S_OK, "get_parentWindow failed: %08x\n", hres);
2826 
2827     hres = IHTMLWindow2_get_document(window, &windows_doc);
2828     IHTMLWindow2_Release(window);
2829     ok(hres == S_OK, "get_document failed: %08x\n", hres);
2830 
2831     cookie = register_cp((IUnknown*)windows_doc, &IID_IDispatch, (IUnknown*)&div_onclick_disp);
2832 
2833     cp = get_cp((IUnknown*)doc, &IID_IDispatch);
2834     hres = IConnectionPoint_Unadvise(cp, cookie);
2835     IConnectionPoint_Release(cp);
2836     ok(hres == S_OK, "Unadvise failed: %08x\n", hres);
2837 
2838     IHTMLDocument2_Release(windows_doc);
2839     IHTMLDocument2_Release(doc);
2840 }
2841 
2842 static BOOL check_ie(void)
2843 {
2844     IHTMLDocument2 *doc;
2845     IHTMLDocument5 *doc5;
2846     IHTMLDocument7 *doc7;
2847     HRESULT hres;
2848 
2849     doc = create_document();
2850     if(!doc)
2851         return FALSE;
2852 
2853     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument7, (void**)&doc7);
2854     if(SUCCEEDED(hres)) {
2855         is_ie9plus = TRUE;
2856         IHTMLDocument7_Release(doc7);
2857     }
2858 
2859     trace("is_ie9plus %x\n", is_ie9plus);
2860 
2861     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
2862     if(SUCCEEDED(hres))
2863         IHTMLDocument5_Release(doc5);
2864 
2865     IHTMLDocument2_Release(doc);
2866     return SUCCEEDED(hres);
2867 }
2868 
2869 START_TEST(events)
2870 {
2871     CoInitialize(NULL);
2872 
2873     if(check_ie()) {
2874         container_hwnd = create_container_window();
2875 
2876         if(winetest_interactive)
2877             ShowWindow(container_hwnd, SW_SHOW);
2878 
2879         run_test(empty_doc_str, test_timeout);
2880         run_test(click_doc_str, test_onclick);
2881         run_test(readystate_doc_str, test_onreadystatechange);
2882         run_test(img_doc_str, test_imgload);
2883         run_test(input_doc_str, test_focus);
2884         run_test(form_doc_str, test_submit);
2885         run_test(iframe_doc_str, test_iframe_connections);
2886 
2887         test_empty_document();
2888 
2889         DestroyWindow(container_hwnd);
2890     }else {
2891         win_skip("Too old IE\n");
2892     }
2893 
2894     CoUninitialize();
2895 }
2896