jscript: Added if statement implementation.
[wine] / dlls / jscript / tests / jscript.c
1 /*
2  * Copyright 2008 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 #define COBJMACROS
20 #define CONST_VTABLE
21
22 #include <initguid.h>
23 #include <ole2.h>
24 #include <activscp.h>
25 #include <objsafe.h>
26 #include <dispex.h>
27
28 #include "wine/test.h"
29
30 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
31
32 static const CLSID CLSID_JScript =
33     {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
34
35 #define DEFINE_EXPECT(func) \
36     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
37
38 #define SET_EXPECT(func) \
39     expect_ ## func = TRUE
40
41 #define SET_CALLED(func) \
42     called_ ## func = TRUE
43
44 #define CHECK_EXPECT2(func) \
45     do { \
46         ok(expect_ ##func, "unexpected call " #func "\n"); \
47         called_ ## func = TRUE; \
48     }while(0)
49
50 #define CHECK_EXPECT(func) \
51     do { \
52         CHECK_EXPECT2(func); \
53         expect_ ## func = FALSE; \
54     }while(0)
55
56 #define CHECK_CALLED(func) \
57     do { \
58         ok(called_ ## func, "expected " #func "\n"); \
59         expect_ ## func = called_ ## func = FALSE; \
60     }while(0)
61
62 DEFINE_EXPECT(GetLCID);
63 DEFINE_EXPECT(OnStateChange_STARTED);
64 DEFINE_EXPECT(OnStateChange_CLOSED);
65 DEFINE_EXPECT(OnStateChange_INITIALIZED);
66 DEFINE_EXPECT(OnEnterScript);
67 DEFINE_EXPECT(OnLeaveScript);
68
69 #define test_state(s,ss) _test_state(__LINE__,s,ss)
70 static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate)
71 {
72     SCRIPTSTATE state = -1;
73     HRESULT hres;
74
75     hres = IActiveScript_GetScriptState(script, &state);
76     ok_(__FILE__,line) (hres == S_OK, "GetScriptState failed: %08x\n", hres);
77     ok_(__FILE__,line) (state == exstate, "state=%d, expected %d\n", state, exstate);
78 }
79
80 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
81 {
82     *ppv = NULL;
83
84     if(IsEqualGUID(&IID_IUnknown, riid))
85         *ppv = iface;
86     else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
87         *ppv = iface;
88     else
89         return E_NOINTERFACE;
90
91     IUnknown_AddRef((IUnknown*)*ppv);
92     return S_OK;
93 }
94
95 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
96 {
97     return 2;
98 }
99
100 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
101 {
102     return 1;
103 }
104
105 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
106 {
107     CHECK_EXPECT(GetLCID);
108     return E_NOTIMPL;
109 }
110
111 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
112         DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
113 {
114     ok(0, "unexpected call\n");
115     return E_NOTIMPL;
116 }
117
118 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
119 {
120     ok(0, "unexpected call\n");
121     return E_NOTIMPL;
122 }
123
124 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
125         const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
126 {
127     ok(0, "unexpected call\n");
128     return E_NOTIMPL;
129 }
130
131 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
132 {
133     switch(ssScriptState) {
134     case SCRIPTSTATE_STARTED:
135         CHECK_EXPECT(OnStateChange_STARTED);
136         return S_OK;
137     case SCRIPTSTATE_CLOSED:
138         CHECK_EXPECT(OnStateChange_CLOSED);
139         return S_OK;
140     case SCRIPTSTATE_INITIALIZED:
141         CHECK_EXPECT(OnStateChange_INITIALIZED);
142         return S_OK;
143     default:
144         ok(0, "unexpected call %d\n", ssScriptState);
145     }
146
147     return E_NOTIMPL;
148 }
149
150 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
151 {
152     ok(0, "unexpected call\n");
153     return E_NOTIMPL;
154 }
155
156 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
157 {
158     CHECK_EXPECT(OnEnterScript);
159     return S_OK;
160 }
161
162 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
163 {
164     CHECK_EXPECT(OnLeaveScript);
165     return S_OK;
166 }
167
168 #undef ACTSCPSITE_THIS
169
170 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
171     ActiveScriptSite_QueryInterface,
172     ActiveScriptSite_AddRef,
173     ActiveScriptSite_Release,
174     ActiveScriptSite_GetLCID,
175     ActiveScriptSite_GetItemInfo,
176     ActiveScriptSite_GetDocVersionString,
177     ActiveScriptSite_OnScriptTerminate,
178     ActiveScriptSite_OnStateChange,
179     ActiveScriptSite_OnScriptError,
180     ActiveScriptSite_OnEnterScript,
181     ActiveScriptSite_OnLeaveScript
182 };
183
184 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
185
186 static void test_script_dispatch(IActiveScript *script, BOOL initialized)
187 {
188     IDispatchEx *dispex;
189     IDispatch *disp;
190     HRESULT hres;
191
192     disp = (void*)0xdeadbeef;
193     hres = IActiveScript_GetScriptDispatch(script, NULL, &disp);
194     if(!initialized) {
195         ok(hres == E_UNEXPECTED, "hres = %08x, expected E_UNEXPECTED\n", hres);
196         ok(!disp, "disp != NULL\n");
197         return;
198     }
199
200     ok(hres == S_OK, "GetScriptDispatch failed: %08x\n", hres);
201     if(FAILED(hres))
202         return;
203
204     ok(disp != NULL, "disp == NULL\n");
205     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
206     IDispatch_Release(disp);
207     ok(hres == S_OK, "Could not get IDispatchEx interface: %08x\n", hres);
208
209     IDispatchEx_Release(dispex);
210 }
211
212 static void test_safety(IUnknown *unk)
213 {
214     IObjectSafety *safety;
215     DWORD supported, enabled;
216     HRESULT hres;
217
218     hres = IUnknown_QueryInterface(unk, &IID_IObjectSafety, (void**)&safety);
219     ok(hres == S_OK, "Could not get IObjectSafety: %08x\n", hres);
220     if(FAILED(hres))
221         return;
222
223     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, NULL);
224     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
225     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, NULL, &enabled);
226     ok(hres == E_POINTER, "GetInterfaceSafetyOptions failed: %08x, expected E_POINTER\n", hres);
227
228     supported = enabled = 0xdeadbeef;
229     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_NULL, &supported, &enabled);
230     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
231     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
232        "supported=%x\n", supported);
233     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
234
235     supported = enabled = 0xdeadbeef;
236     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScript, &supported, &enabled);
237     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
238     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
239        "supported=%x\n", supported);
240     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
241
242     supported = enabled = 0xdeadbeef;
243     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
244     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
245     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
246        "supported=%x\n", supported);
247     ok(enabled == INTERFACE_USES_DISPEX, "enabled=%x\n", enabled);
248
249     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
250             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER
251                 |INTERFACESAFE_FOR_UNTRUSTED_CALLER,
252             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
253     ok(hres == E_FAIL, "SetInterfaceSafetyOptions failed: %08x, expected E_FAIL\n", hres);
254
255     hres = IObjectSafety_SetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse,
256             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER,
257             INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER);
258     ok(hres == S_OK, "SetInterfaceSafetyOptions failed: %08x\n", hres);
259
260     supported = enabled = 0xdeadbeef;
261     hres = IObjectSafety_GetInterfaceSafetyOptions(safety, &IID_IActiveScriptParse, &supported, &enabled);
262     ok(hres == S_OK, "GetInterfaceSafetyOptions failed: %08x\n", hres);
263     ok(supported == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
264        "supported=%x\n", supported);
265     ok(enabled == (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACE_USES_DISPEX|INTERFACE_USES_SECURITY_MANAGER),
266        "enabled=%x\n", enabled);
267
268     IObjectSafety_Release(safety);
269 }
270
271 static void test_jscript(void)
272 {
273     IActiveScriptParse *parse;
274     IActiveScript *script;
275     IUnknown *unk;
276     ULONG ref;
277     HRESULT hres;
278
279     hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
280             &IID_IUnknown, (void**)&unk);
281     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
282     if(FAILED(hres))
283         return;
284
285     hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
286     ok(hres == S_OK, "Could not get IActiveScript: %08x\n", hres);
287
288     hres = IUnknown_QueryInterface(unk, &IID_IActiveScriptParse, (void**)&parse);
289     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
290
291     test_state(script, SCRIPTSTATE_UNINITIALIZED);
292     test_safety(unk);
293
294     hres = IActiveScriptParse_InitNew(parse);
295     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
296
297     hres = IActiveScriptParse_InitNew(parse);
298     ok(hres == E_UNEXPECTED, "InitNew failed: %08x, expected E_UNEXPECTED\n", hres);
299
300     hres = IActiveScript_SetScriptSite(script, NULL);
301     ok(hres == E_POINTER, "SetScriptSite failed: %08x, expected E_POINTER\n", hres);
302
303     test_state(script, SCRIPTSTATE_UNINITIALIZED);
304     test_script_dispatch(script, FALSE);
305
306     SET_EXPECT(GetLCID);
307     SET_EXPECT(OnStateChange_INITIALIZED);
308     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
309     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
310     CHECK_CALLED(GetLCID);
311     CHECK_CALLED(OnStateChange_INITIALIZED);
312
313     test_state(script, SCRIPTSTATE_INITIALIZED);
314
315     hres = IActiveScript_SetScriptSite(script, &ActiveScriptSite);
316     ok(hres == E_UNEXPECTED, "SetScriptSite failed: %08x, expected E_UNEXPECTED\n", hres);
317
318     test_script_dispatch(script, TRUE);
319
320     SET_EXPECT(OnStateChange_STARTED);
321     hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
322     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08x\n", hres);
323     CHECK_CALLED(OnStateChange_STARTED);
324
325     test_state(script, SCRIPTSTATE_STARTED);
326
327     SET_EXPECT(OnStateChange_CLOSED);
328     hres = IActiveScript_Close(script);
329     ok(hres == S_OK, "Close failed: %08x\n", hres);
330     CHECK_CALLED(OnStateChange_CLOSED);
331
332     test_state(script, SCRIPTSTATE_CLOSED);
333     test_script_dispatch(script, FALSE);
334
335     IActiveScriptParse_Release(parse);
336     IActiveScript_Release(script);
337
338     ref = IUnknown_Release(unk);
339     ok(!ref, "ref = %d\n", ref);
340 }
341
342 START_TEST(jscript)
343 {
344     CoInitialize(NULL);
345
346     test_jscript();
347
348     CoUninitialize();
349 }