428bf8cb57da0017a3fbeb5de0166e566bb6ed24
[reactos.git] / rostests / winetests / mstask / task_scheduler.c
1 /*
2 * Test suite for TaskScheduler 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
25 #include "initguid.h"
26 #include "mstask.h"
27 #include "wine/test.h"
28
29 static ITaskScheduler *test_task_scheduler;
30
31 static const WCHAR does_not_existW[] = {'\\','\\','d','o','e','s','_','n','o','t','_','e','x','i','s','t',0};
32
33 static void test_NewWorkItem(void)
34 {
35 HRESULT hres;
36 ITask *task;
37 const WCHAR task_name[] = {'T', 'e', 's', 't', 'i', 'n', 'g', 0};
38 GUID GUID_BAD;
39
40 /* Initialize a GUID that will not be a recognized CLSID or a IID */
41 CoCreateGuid(&GUID_BAD);
42
43 /* Create TaskScheduler */
44 hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
45 &IID_ITaskScheduler, (void **) &test_task_scheduler);
46 ok(hres == S_OK, "CTaskScheduler CoCreateInstance failed: %08x\n", hres);
47 if (hres != S_OK)
48 {
49 skip("Failed to create task scheduler. Skipping tests.\n");
50 return;
51 }
52
53 /* Test basic task creation */
54 hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name,
55 &CLSID_CTask, &IID_ITask, (IUnknown**)&task);
56 ok(hres == S_OK, "NewNetworkItem failed: %08x\n", hres);
57 if (hres == S_OK)
58 ITask_Release(task);
59
60 /* Task creation attempt using invalid work item class ID */
61 hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name,
62 &GUID_BAD, &IID_ITask, (IUnknown**)&task);
63 ok(hres == CLASS_E_CLASSNOTAVAILABLE,
64 "Expected CLASS_E_CLASSNOTAVAILABLE: %08x\n", hres);
65
66 /* Task creation attempt using invalid interface ID */
67 hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name,
68 &CLSID_CTask, &GUID_BAD, (IUnknown**)&task);
69 ok(hres == E_NOINTERFACE, "Expected E_NOINTERFACE: %08x\n", hres);
70
71 /* Task creation attempt using invalid work item class and interface ID */
72 hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name,
73 &GUID_BAD, &GUID_BAD, (IUnknown**)&task);
74 ok(hres == CLASS_E_CLASSNOTAVAILABLE,
75 "Expected CLASS_E_CLASSNOTAVAILABLE: %08x\n", hres);
76
77 ITaskScheduler_Release(test_task_scheduler);
78 return;
79 }
80
81 static void test_Activate(void)
82 {
83 HRESULT hres;
84 ITask *task = NULL;
85 const WCHAR not_task_name[] =
86 {'N', 'o', 'S', 'u', 'c', 'h', 'T', 'a', 's', 'k', 0};
87
88 /* Create TaskScheduler */
89 hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
90 &IID_ITaskScheduler, (void **) &test_task_scheduler);
91 ok(hres == S_OK, "CTaskScheduler CoCreateInstance failed: %08x\n", hres);
92 if (hres != S_OK)
93 {
94 skip("Failed to create task scheduler. Skipping tests.\n");
95 return;
96 }
97
98 /* Attempt to activate a nonexistent task */
99 hres = ITaskScheduler_Activate(test_task_scheduler, not_task_name,
100 &IID_ITask, (IUnknown**)&task);
101 ok(hres == COR_E_FILENOTFOUND, "Expected COR_E_FILENOTFOUND: %08x\n", hres);
102
103 ITaskScheduler_Release(test_task_scheduler);
104 return;
105 }
106
107 static void test_GetTargetComputer(void)
108 {
109 HRESULT hres;
110 WCHAR *oldname;
111
112 /* Create TaskScheduler */
113 hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
114 &IID_ITaskScheduler, (void **) &test_task_scheduler);
115 ok(hres == S_OK, "CTaskScheduler CoCreateInstance failed: %08x\n", hres);
116 if (hres != S_OK)
117 {
118 skip("Failed to create task scheduler.\n");
119 return;
120 }
121
122 if (0)
123 {
124 /* This crashes on w2k */
125 hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, NULL);
126 ok(hres == E_INVALIDARG, "got 0x%x (expected E_INVALIDARG)\n", hres);
127 }
128
129 hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, &oldname);
130 ok((hres == S_OK) && oldname && oldname[0] == '\\' && oldname[1] == '\\' && oldname[2],
131 "got 0x%x and %s (expected S_OK and an unc name)\n", hres, wine_dbgstr_w(oldname));
132
133 CoTaskMemFree(oldname);
134
135 ITaskScheduler_Release(test_task_scheduler);
136 return;
137 }
138
139 static void test_SetTargetComputer(void)
140 {
141 WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 3]; /* extra space for two '\' and a zero */
142 DWORD len = MAX_COMPUTERNAME_LENGTH + 1; /* extra space for a zero */
143 WCHAR *oldname = NULL;
144 WCHAR *name = NULL;
145 HRESULT hres;
146
147
148 buffer[0] = '\\';
149 buffer[1] = '\\';
150 if (!GetComputerNameW(buffer + 2, &len))
151 return;
152
153 /* Create TaskScheduler */
154 hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
155 &IID_ITaskScheduler, (void **) &test_task_scheduler);
156 ok(hres == S_OK, "CTaskScheduler CoCreateInstance failed: %08x\n", hres);
157 if (hres != S_OK)
158 {
159 skip("Failed to create task scheduler. Skipping tests.\n");
160 return;
161 }
162
163 hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, &oldname);
164 ok(hres == S_OK, "got 0x%x and %s (expected S_OK)\n", hres, wine_dbgstr_w(oldname));
165
166 /* NULL is an alias for the local computer */
167 hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, NULL);
168 ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
169 hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, &name);
170 ok((hres == S_OK && !lstrcmpiW(name, buffer)),
171 "got 0x%x with %s (expected S_OK and %s)\n",
172 hres, wine_dbgstr_w(name), wine_dbgstr_w(buffer));
173 CoTaskMemFree(name);
174
175 /* The name must be valid */
176 hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, does_not_existW);
177 ok(hres == HRESULT_FROM_WIN32(ERROR_BAD_NETPATH), "got 0x%x (expected 0x80070035)\n", hres);
178 /* the name of the target computer is unchanged */
179 hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, &name);
180 ok((hres == S_OK && !lstrcmpiW(name, buffer)),
181 "got 0x%x with %s (expected S_OK and %s)\n",
182 hres, wine_dbgstr_w(name), wine_dbgstr_w(buffer));
183 CoTaskMemFree(name);
184
185 /* the two backslashes are optional */
186 hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, oldname + 2);
187 ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
188
189 /* the case is ignored */
190 CharUpperW(buffer);
191 hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, buffer);
192 ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
193 CharLowerW(buffer);
194 hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, buffer);
195 ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
196
197 /* cleanup */
198 hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, oldname);
199 ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
200
201 CoTaskMemFree(oldname);
202 ITaskScheduler_Release(test_task_scheduler);
203 return;
204 }
205
206
207 START_TEST(task_scheduler)
208 {
209 CoInitialize(NULL);
210 test_NewWorkItem();
211 test_Activate();
212 test_GetTargetComputer();
213 test_SetTargetComputer();
214 CoUninitialize();
215 }