mstask/tests: Conformance test for (Set|Get)Parameters.
[wine] / dlls / mstask / tests / task.c
1 /*
2  * Test suite for Task interface
3  *
4  * Copyright (C) 2008 Google (Roy Shea)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22
23 #include "corerror.h"
24 #include "mstask.h"
25 #include "wine/test.h"
26
27 static ITaskScheduler *test_task_scheduler;
28 static ITask *test_task;
29 static const WCHAR empty[] = {0};
30
31 /* allocate some tmp string space */
32 /* FIXME: this is not 100% thread-safe */
33 static char *get_tmp_space(int size)
34 {
35     static char *list[16];
36     static long pos;
37     char *ret;
38     int idx;
39
40     idx = ++pos % (sizeof(list)/sizeof(list[0]));
41     if ((ret = realloc(list[idx], size)))
42         list[idx] = ret;
43     return ret;
44 }
45
46 static const char *dbgstr_w(LPCWSTR str)
47 {
48     char *buf;
49     int len;
50     if(!str)
51         return "(null)";
52     len = lstrlenW(str) + 1;
53     buf = get_tmp_space(len);
54     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, len, NULL, NULL);
55     return buf;
56 }
57
58 static BOOL setup_task(void)
59 {
60     HRESULT hres;
61     const WCHAR task_name[] = {'T','e','s','t','i','n','g', 0};
62
63     hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
64             &IID_ITaskScheduler, (void **) &test_task_scheduler);
65     if(hres != S_OK)
66         return FALSE;
67     hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name, &CLSID_CTask,
68             &IID_ITask, (IUnknown**)&test_task);
69     if(hres != S_OK)
70     {
71         ITaskScheduler_Release(test_task_scheduler);
72         return FALSE;
73     }
74     return TRUE;
75 }
76
77 static void cleanup_task(void)
78 {
79     ITask_Release(test_task);
80     ITaskScheduler_Release(test_task_scheduler);
81 }
82
83 static LPCWSTR path_resolve_name(LPCWSTR base_name)
84 {
85     static WCHAR buffer[MAX_PATH];
86     int len;
87
88     len = SearchPathW(NULL, base_name, NULL, 0, NULL, NULL);
89     if (len == 0)
90         return base_name;
91     else if (len < MAX_PATH)
92     {
93         SearchPathW(NULL, base_name, NULL, MAX_PATH, buffer, NULL);
94         return buffer;
95     }
96     return NULL;
97 }
98
99 static void test_SetApplicationName_GetApplicationName(void)
100 {
101     BOOL setup;
102     HRESULT hres;
103     LPWSTR stored_name;
104     LPCWSTR full_name;
105     const WCHAR non_application_name[] = {'N','o','S','u','c','h',
106             'A','p','p','l','i','c','a','t','i','o','n', 0};
107     const WCHAR notepad_exe[] = {
108             'n','o','t','e','p','a','d','.','e','x','e', 0};
109     const WCHAR notepad[] = {'n','o','t','e','p','a','d', 0};
110
111     setup = setup_task();
112     ok(setup, "Failed to setup test_task\n");
113     if (!setup)
114     {
115         skip("Failed to create task.  Skipping tests.\n");
116         return;
117     }
118
119     /* Attempt getting before setting application name */
120     hres = ITask_GetApplicationName(test_task, &stored_name);
121     todo_wine ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
122     if (hres == S_OK)
123     {
124         todo_wine ok(!lstrcmpW(stored_name, empty),
125                 "Got %s, expected empty string\n", dbgstr_w(stored_name));
126         CoTaskMemFree(stored_name);
127     }
128
129     /* Set application name to a non-existent application and then get
130      * the application name that is actually stored */
131     hres = ITask_SetApplicationName(test_task, non_application_name);
132     todo_wine ok(hres == S_OK, "Failed setting name %s: %08x\n",
133             dbgstr_w(non_application_name), hres);
134     hres = ITask_GetApplicationName(test_task, &stored_name);
135     todo_wine ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
136     if (hres == S_OK)
137     {
138         full_name = path_resolve_name(non_application_name);
139         todo_wine ok(!lstrcmpW(stored_name, full_name), "Got %s, expected %s\n",
140                 dbgstr_w(stored_name), dbgstr_w(full_name));
141         CoTaskMemFree(stored_name);
142     }
143
144     /* Set a valid application name with program type extension and then
145      * get the stored name */
146     hres = ITask_SetApplicationName(test_task, notepad_exe);
147     todo_wine ok(hres == S_OK, "Failed setting name %s: %08x\n",
148             dbgstr_w(notepad_exe), hres);
149     hres = ITask_GetApplicationName(test_task, &stored_name);
150     todo_wine ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
151     if (hres == S_OK)
152     {
153         full_name = path_resolve_name(notepad_exe);
154         todo_wine ok(!lstrcmpW(stored_name, full_name), "Got %s, expected %s\n",
155                 dbgstr_w(stored_name), dbgstr_w(full_name));
156         CoTaskMemFree(stored_name);
157     }
158
159     /* Set a valid application name without program type extension and
160      * then get the stored name */
161     hres = ITask_SetApplicationName(test_task, notepad);
162     todo_wine ok(hres == S_OK, "Failed setting name %s: %08x\n", dbgstr_w(notepad), hres);
163     hres = ITask_GetApplicationName(test_task, &stored_name);
164     todo_wine ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
165     if (hres == S_OK)
166     {
167         full_name = path_resolve_name(notepad);
168         todo_wine ok(!lstrcmpW(stored_name, full_name), "Got %s, expected %s\n",
169                 dbgstr_w(stored_name), dbgstr_w(full_name));
170         CoTaskMemFree(stored_name);
171     }
172
173     /* After having a valid application name set, set application name
174      * to a non-existant application and then get the name that is
175      * actually stored */
176     hres = ITask_SetApplicationName(test_task, non_application_name);
177     todo_wine ok(hres == S_OK, "Failed setting name %s: %08x\n",
178             dbgstr_w(non_application_name), hres);
179     hres = ITask_GetApplicationName(test_task, &stored_name);
180     todo_wine ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
181     if (hres == S_OK)
182     {
183         full_name = path_resolve_name(non_application_name);
184         todo_wine ok(!lstrcmpW(stored_name, full_name), "Got %s, expected %s\n",
185                 dbgstr_w(stored_name), dbgstr_w(full_name));
186         CoTaskMemFree(stored_name);
187     }
188
189     /* Clear application name */
190     hres = ITask_SetApplicationName(test_task, empty);
191     todo_wine ok(hres == S_OK, "Failed setting name %s: %08x\n", dbgstr_w(empty), hres);
192     hres = ITask_GetApplicationName(test_task, &stored_name);
193     todo_wine ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
194     if (hres == S_OK)
195     {
196         todo_wine ok(!lstrcmpW(stored_name, empty),
197                 "Got %s, expected empty string\n", dbgstr_w(stored_name));
198         CoTaskMemFree(stored_name);
199     }
200
201     cleanup_task();
202     return;
203 }
204
205 static void test_CreateTrigger(void)
206 {
207     BOOL setup;
208     HRESULT hres;
209     WORD trigger_index;
210     ITaskTrigger *test_trigger;
211
212     setup = setup_task();
213     ok(setup, "Failed to setup test_task\n");
214     if (!setup)
215     {
216         skip("Failed to create task.  Skipping tests.\n");
217         return;
218     }
219
220     hres = ITask_CreateTrigger(test_task, &trigger_index, &test_trigger);
221     todo_wine ok(hres == S_OK, "Failed to create trigger: 0x%08x\n", hres);
222     if (hres != S_OK)
223     {
224         cleanup_task();
225         return;
226     }
227
228     ITaskTrigger_Release(test_trigger);
229     cleanup_task();
230     return;
231 }
232
233 static void test_SetParameters_GetParameters(void)
234 {
235     BOOL setup;
236     HRESULT hres;
237     LPWSTR parameters;
238     const WCHAR parameters_a[] = {'f','o','o','.','t','x','t', 0};
239     const WCHAR parameters_b[] = {'f','o','o','.','t','x','t',' ',
240         'b','a','r','.','t','x','t', 0};
241
242     setup = setup_task();
243     ok(setup, "Failed to setup test_task\n");
244     if (!setup)
245     {
246         skip("Failed to create task.  Skipping tests.\n");
247         return;
248     }
249
250     /* Get parameters before setting them */
251     hres = ITask_GetParameters(test_task, &parameters);
252     todo_wine ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
253     if (hres == S_OK)
254     {
255         todo_wine ok(!lstrcmpW(parameters, empty),
256                 "Got %s, expected empty string\n", dbgstr_w(parameters));
257         CoTaskMemFree(parameters);
258     }
259
260     /* Set parameters to a simple string */
261     hres = ITask_SetParameters(test_task, parameters_a);
262     todo_wine ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
263             dbgstr_w(parameters_a), hres);
264     hres = ITask_GetParameters(test_task, &parameters);
265     todo_wine ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
266     if (hres == S_OK)
267     {
268         todo_wine ok(!lstrcmpW(parameters, parameters_a), "Got %s, expected %s\n",
269                 dbgstr_w(parameters), dbgstr_w(parameters_a));
270         CoTaskMemFree(parameters);
271     }
272
273     /* Update parameters to a different simple string */
274     hres = ITask_SetParameters(test_task, parameters_b);
275     todo_wine ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
276             dbgstr_w(parameters_b), hres);
277     hres = ITask_GetParameters(test_task, &parameters);
278     todo_wine ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
279     if (hres == S_OK)
280     {
281         todo_wine ok(!lstrcmpW(parameters, parameters_b), "Got %s, expected %s\n",
282                 dbgstr_w(parameters), dbgstr_w(parameters_b));
283         CoTaskMemFree(parameters);
284     }
285
286     /* Clear parameters */
287     hres = ITask_SetParameters(test_task, empty);
288     todo_wine ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
289             dbgstr_w(empty), hres);
290     hres = ITask_GetParameters(test_task, &parameters);
291     todo_wine ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
292     if (hres == S_OK)
293     {
294         todo_wine ok(!lstrcmpW(parameters, empty),
295                 "Got %s, expected empty string\n", dbgstr_w(parameters));
296         CoTaskMemFree(parameters);
297     }
298
299     cleanup_task();
300     return;
301 }
302
303 START_TEST(task)
304 {
305     CoInitialize(NULL);
306     test_SetApplicationName_GetApplicationName();
307     test_CreateTrigger();
308     test_SetParameters_GetParameters();
309     CoUninitialize();
310 }