task.c 27.7 KB
Newer Older
1 2 3 4
/*
 * Test suite for Task interface
 *
 * Copyright (C) 2008 Google (Roy Shea)
5
 * Copyright (C) 2018 Dmitry Timoshkov
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS

#include "corerror.h"
#include "mstask.h"
#include "wine/test.h"

28 29
static ITaskScheduler *scheduler;
static const WCHAR task_name[] = {'T','e','s','t','i','n','g',0};
30 31
static const WCHAR empty[] = {0};

32 33 34
extern HRESULT taskscheduler_delete(ITaskScheduler*, const WCHAR*);


35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static LPCWSTR path_resolve_name(LPCWSTR base_name)
{
    static WCHAR buffer[MAX_PATH];
    int len;

    len = SearchPathW(NULL, base_name, NULL, 0, NULL, NULL);
    if (len == 0)
        return base_name;
    else if (len < MAX_PATH)
    {
        SearchPathW(NULL, base_name, NULL, MAX_PATH, buffer, NULL);
        return buffer;
    }
    return NULL;
}

static void test_SetApplicationName_GetApplicationName(void)
{
53
    ITask *test_task;
54 55 56 57 58 59 60 61 62
    HRESULT hres;
    LPWSTR stored_name;
    LPCWSTR full_name;
    const WCHAR non_application_name[] = {'N','o','S','u','c','h',
            'A','p','p','l','i','c','a','t','i','o','n', 0};
    const WCHAR notepad_exe[] = {
            'n','o','t','e','p','a','d','.','e','x','e', 0};
    const WCHAR notepad[] = {'n','o','t','e','p','a','d', 0};

63 64 65
    hres = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                      &IID_ITask, (IUnknown **)&test_task);
    ok(hres == S_OK, "Failed to setup test_task\n");
66 67 68

    /* Attempt getting before setting application name */
    hres = ITask_GetApplicationName(test_task, &stored_name);
69
    ok(hres == S_OK, "GetApplicationName failed: %08lx\n", hres);
70 71
    if (hres == S_OK)
    {
72
        ok(!lstrcmpiW(stored_name, empty),
73
                "Got %s, expected empty string\n", wine_dbgstr_w(stored_name));
74 75 76
        CoTaskMemFree(stored_name);
    }

77
    /* Set application name to a nonexistent application and then get
78 79
     * the application name that is actually stored */
    hres = ITask_SetApplicationName(test_task, non_application_name);
80
    ok(hres == S_OK, "Failed setting name %s: %08lx\n",
81
            wine_dbgstr_w(non_application_name), hres);
82
    hres = ITask_GetApplicationName(test_task, &stored_name);
83
    ok(hres == S_OK, "GetApplicationName failed: %08lx\n", hres);
84 85 86
    if (hres == S_OK)
    {
        full_name = path_resolve_name(non_application_name);
87
        ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
88
                wine_dbgstr_w(stored_name), wine_dbgstr_w(full_name));
89 90 91 92 93 94
        CoTaskMemFree(stored_name);
    }

    /* Set a valid application name with program type extension and then
     * get the stored name */
    hres = ITask_SetApplicationName(test_task, notepad_exe);
95
    ok(hres == S_OK, "Failed setting name %s: %08lx\n",
96
            wine_dbgstr_w(notepad_exe), hres);
97
    hres = ITask_GetApplicationName(test_task, &stored_name);
98
    ok(hres == S_OK, "GetApplicationName failed: %08lx\n", hres);
99 100 101
    if (hres == S_OK)
    {
        full_name = path_resolve_name(notepad_exe);
102
        ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
103
                wine_dbgstr_w(stored_name), wine_dbgstr_w(full_name));
104 105 106 107 108 109
        CoTaskMemFree(stored_name);
    }

    /* Set a valid application name without program type extension and
     * then get the stored name */
    hres = ITask_SetApplicationName(test_task, notepad);
110
    ok(hres == S_OK, "Failed setting name %s: %08lx\n", wine_dbgstr_w(notepad), hres);
111
    hres = ITask_GetApplicationName(test_task, &stored_name);
112
    ok(hres == S_OK, "GetApplicationName failed: %08lx\n", hres);
113 114
    if (hres == S_OK)
    {
115 116 117 118 119
        full_name = path_resolve_name(notepad_exe);  /* XP SP1 appends .exe */
        if (lstrcmpiW(stored_name, full_name) != 0)
        {
            full_name = path_resolve_name(notepad);
            ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
120
               wine_dbgstr_w(stored_name), wine_dbgstr_w(full_name));
121
        }
122 123 124
        CoTaskMemFree(stored_name);
    }

125 126
    /* After having a valid application name set, set application the name
     * to a nonexistent application and then get the name that is
127 128
     * actually stored */
    hres = ITask_SetApplicationName(test_task, non_application_name);
129
    ok(hres == S_OK, "Failed setting name %s: %08lx\n",
130
            wine_dbgstr_w(non_application_name), hres);
131
    hres = ITask_GetApplicationName(test_task, &stored_name);
132
    ok(hres == S_OK, "GetApplicationName failed: %08lx\n", hres);
133 134 135
    if (hres == S_OK)
    {
        full_name = path_resolve_name(non_application_name);
136
        ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
137
                wine_dbgstr_w(stored_name), wine_dbgstr_w(full_name));
138 139 140 141 142
        CoTaskMemFree(stored_name);
    }

    /* Clear application name */
    hres = ITask_SetApplicationName(test_task, empty);
143
    ok(hres == S_OK, "Failed setting name %s: %08lx\n", wine_dbgstr_w(empty), hres);
144
    hres = ITask_GetApplicationName(test_task, &stored_name);
145
    ok(hres == S_OK, "GetApplicationName failed: %08lx\n", hres);
146 147
    if (hres == S_OK)
    {
148
        ok(!lstrcmpiW(stored_name, empty),
149
                "Got %s, expected empty string\n", wine_dbgstr_w(stored_name));
150 151 152
        CoTaskMemFree(stored_name);
    }

153
    ITask_Release(test_task);
154 155
}

156 157
static void test_CreateTrigger(void)
{
158
    ITask *test_task;
159 160 161 162
    HRESULT hres;
    WORD trigger_index;
    ITaskTrigger *test_trigger;

163 164 165
    hres = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                      &IID_ITask, (IUnknown **)&test_task);
    ok(hres == S_OK, "Failed to setup test_task\n");
166 167

    hres = ITask_CreateTrigger(test_task, &trigger_index, &test_trigger);
168
    ok(hres == S_OK, "Failed to create trigger: 0x%08lx\n", hres);
169 170

    ITaskTrigger_Release(test_trigger);
171
    ITask_Release(test_task);
172 173
}

174 175
static void test_SetParameters_GetParameters(void)
{
176
    ITask *test_task;
177 178 179 180 181 182
    HRESULT hres;
    LPWSTR parameters;
    const WCHAR parameters_a[] = {'f','o','o','.','t','x','t', 0};
    const WCHAR parameters_b[] = {'f','o','o','.','t','x','t',' ',
        'b','a','r','.','t','x','t', 0};

183 184 185
    hres = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                      &IID_ITask, (IUnknown **)&test_task);
    ok(hres == S_OK, "Failed to setup test_task\n");
186 187 188

    /* Get parameters before setting them */
    hres = ITask_GetParameters(test_task, &parameters);
189
    ok(hres == S_OK, "GetParameters failed: %08lx\n", hres);
190 191
    if (hres == S_OK)
    {
192
        ok(!lstrcmpW(parameters, empty),
193
                "Got %s, expected empty string\n", wine_dbgstr_w(parameters));
194 195 196 197 198
        CoTaskMemFree(parameters);
    }

    /* Set parameters to a simple string */
    hres = ITask_SetParameters(test_task, parameters_a);
199
    ok(hres == S_OK, "Failed setting parameters %s: %08lx\n",
200
            wine_dbgstr_w(parameters_a), hres);
201
    hres = ITask_GetParameters(test_task, &parameters);
202
    ok(hres == S_OK, "GetParameters failed: %08lx\n", hres);
203 204
    if (hres == S_OK)
    {
205
        ok(!lstrcmpW(parameters, parameters_a), "Got %s, expected %s\n",
206
                wine_dbgstr_w(parameters), wine_dbgstr_w(parameters_a));
207 208 209 210 211
        CoTaskMemFree(parameters);
    }

    /* Update parameters to a different simple string */
    hres = ITask_SetParameters(test_task, parameters_b);
212
    ok(hres == S_OK, "Failed setting parameters %s: %08lx\n",
213
            wine_dbgstr_w(parameters_b), hres);
214
    hres = ITask_GetParameters(test_task, &parameters);
215
    ok(hres == S_OK, "GetParameters failed: %08lx\n", hres);
216 217
    if (hres == S_OK)
    {
218
        ok(!lstrcmpW(parameters, parameters_b), "Got %s, expected %s\n",
219
                wine_dbgstr_w(parameters), wine_dbgstr_w(parameters_b));
220 221 222 223 224
        CoTaskMemFree(parameters);
    }

    /* Clear parameters */
    hres = ITask_SetParameters(test_task, empty);
225
    ok(hres == S_OK, "Failed setting parameters %s: %08lx\n",
226
            wine_dbgstr_w(empty), hres);
227
    hres = ITask_GetParameters(test_task, &parameters);
228
    ok(hres == S_OK, "GetParameters failed: %08lx\n", hres);
229 230
    if (hres == S_OK)
    {
231
        ok(!lstrcmpW(parameters, empty),
232
                "Got %s, expected empty string\n", wine_dbgstr_w(parameters));
233 234 235
        CoTaskMemFree(parameters);
    }

236
    ITask_Release(test_task);
237 238
}

239 240
static void test_SetComment_GetComment(void)
{
241
    ITask *test_task;
242 243 244 245 246 247
    HRESULT hres;
    LPWSTR comment;
    const WCHAR comment_a[] = {'C','o','m','m','e','n','t','.', 0};
    const WCHAR comment_b[] = {'L','o','n','g','e','r',' ',
            'c','o','m','m','e','n','t','.', 0};

248 249 250
    hres = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                      &IID_ITask, (IUnknown **)&test_task);
    ok(hres == S_OK, "Failed to setup test_task\n");
251 252 253

    /* Get comment before setting it*/
    hres = ITask_GetComment(test_task, &comment);
254
    ok(hres == S_OK, "GetComment failed: %08lx\n", hres);
255 256
    if (hres == S_OK)
    {
257
        ok(!lstrcmpW(comment, empty),
258
                "Got %s, expected empty string\n", wine_dbgstr_w(comment));
259 260 261 262 263
        CoTaskMemFree(comment);
    }

    /* Set comment to a simple string */
    hres = ITask_SetComment(test_task, comment_a);
264
    ok(hres == S_OK, "Failed setting comment %s: %08lx\n",
265
            wine_dbgstr_w(comment_a), hres);
266
    hres = ITask_GetComment(test_task, &comment);
267
    ok(hres == S_OK, "GetComment failed: %08lx\n", hres);
268 269
    if (hres == S_OK)
    {
270
        ok(!lstrcmpW(comment, comment_a), "Got %s, expected %s\n",
271
                wine_dbgstr_w(comment), wine_dbgstr_w(comment_a));
272 273 274 275 276
        CoTaskMemFree(comment);
    }

    /* Update comment to a different simple string */
    hres = ITask_SetComment(test_task, comment_b);
277
    ok(hres == S_OK, "Failed setting comment %s: %08lx\n",
278
            wine_dbgstr_w(comment_b), hres);
279
    hres = ITask_GetComment(test_task, &comment);
280
    ok(hres == S_OK, "GetComment failed: %08lx\n", hres);
281 282
    if (hres == S_OK)
    {
283
        ok(!lstrcmpW(comment, comment_b), "Got %s, expected %s\n",
284
                wine_dbgstr_w(comment), wine_dbgstr_w(comment_b));
285 286 287 288 289
        CoTaskMemFree(comment);
    }

    /* Clear comment */
    hres = ITask_SetComment(test_task, empty);
290
    ok(hres == S_OK, "Failed setting comment %s: %08lx\n",
291
            wine_dbgstr_w(empty), hres);
292
    hres = ITask_GetComment(test_task, &comment);
293
    ok(hres == S_OK, "GetComment failed: %08lx\n", hres);
294 295
    if (hres == S_OK)
    {
296
        ok(!lstrcmpW(comment, empty),
297
                "Got %s, expected empty string\n", wine_dbgstr_w(comment));
298 299 300
        CoTaskMemFree(comment);
    }

301
    ITask_Release(test_task);
302 303
}

304 305
static void test_SetMaxRunTime_GetMaxRunTime(void)
{
306
    ITask *test_task;
307 308 309
    HRESULT hres;
    DWORD max_run_time;

310 311 312
    hres = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                      &IID_ITask, (IUnknown **)&test_task);
    ok(hres == S_OK, "Failed to setup test_task\n");
313 314 315 316 317

    /* Default time is 3 days:
     * 3 days * 24 hours * 60 minutes * 60 seconds * 1000 ms = 259200000 */
    max_run_time = 0;
    hres = ITask_GetMaxRunTime(test_task, &max_run_time);
318 319
    ok(hres == S_OK, "Failed to get max runtime: 0x%08lx\n", hres);
    ok(max_run_time == 259200000, "Expected 259200000: %ld\n", max_run_time);
320 321 322 323

    /* Basic set test */
    max_run_time = 0;
    hres = ITask_SetMaxRunTime(test_task, 1234);
324
    ok(hres == S_OK, "Failed to set max runtime: 0x%08lx\n", hres);
325
    hres = ITask_GetMaxRunTime(test_task, &max_run_time);
326 327
    ok(hres == S_OK, "Failed to get max runtime: 0x%08lx\n", hres);
    ok(max_run_time == 1234, "Expected 1234: %ld\n", max_run_time);
328 329 330 331

    /* Verify that time can be set to zero */
    max_run_time = 1;
    hres = ITask_SetMaxRunTime(test_task, 0);
332
    ok(hres == S_OK, "Failed to set max runtime: 0x%08lx\n", hres);
333
    hres = ITask_GetMaxRunTime(test_task, &max_run_time);
334 335
    ok(hres == S_OK, "Failed to get max runtime: 0x%08lx\n", hres);
    ok(max_run_time == 0, "Expected 0: %ld\n", max_run_time);
336 337 338 339

    /* Check resolution by setting time to one */
    max_run_time = 0;
    hres = ITask_SetMaxRunTime(test_task, 1);
340
    ok(hres == S_OK, "Failed to set max runtime: 0x%08lx\n", hres);
341
    hres = ITask_GetMaxRunTime(test_task, &max_run_time);
342 343
    ok(hres == S_OK, "Failed to get max runtime: 0x%08lx\n", hres);
    ok(max_run_time == 1, "Expected 1: %ld\n", max_run_time);
344 345 346 347

    /* Verify that time can be set to INFINITE */
    max_run_time = 0;
    hres = ITask_SetMaxRunTime(test_task, INFINITE);
348
    ok(hres == S_OK, "Failed to set max runtime: 0x%08lx\n", hres);
349
    hres = ITask_GetMaxRunTime(test_task, &max_run_time);
350 351
    ok(hres == S_OK, "Failed to get max runtime: 0x%08lx\n", hres);
    ok(max_run_time == INFINITE, "Expected INFINITE: %ld\n", max_run_time);
352

353
    ITask_Release(test_task);
354 355
}

356 357
static void test_SetAccountInformation_GetAccountInformation(void)
{
358
    ITask *test_task;
359 360 361 362
    HRESULT hres;
    LPWSTR account_name;
    const WCHAR dummy_account_name[] = {'N', 'o', 'S', 'u', 'c', 'h',
            'A', 'c', 'c', 'o', 'u', 'n', 't', 0};
363 364
    const WCHAR dummy_account_name_b[] = {'N', 'o', 'S', 'u', 'c', 'h',
            'A', 'c', 'c', 'o', 'u', 'n', 't', 'B', 0};
365

366 367 368
    hres = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                      &IID_ITask, (IUnknown **)&test_task);
    ok(hres == S_OK, "Failed to setup test_task\n");
369 370 371 372

    /* Get account information before it is set */
    hres = ITask_GetAccountInformation(test_task, &account_name);
    /* WinXP returns HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND): 0x80070002 but
373 374
     * Win2K returns SCHED_E_CANNOT_OPEN_TASK: 0x8004130d
     * Win9x doesn't support security services */
375
    if (hres == SCHED_E_NO_SECURITY_SERVICES || hres == SCHED_E_SERVICE_NOT_RUNNING)
376 377
    {
        win_skip("Security services are not supported\n");
378
        ITask_Release(test_task);
379
    }
380
    ok(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
381
            hres == SCHED_E_CANNOT_OPEN_TASK,
382
            "Unset account name generated: 0x%08lx\n", hres);
383 384 385 386

    /* Attempt to set to a dummy account without a password */
    /* This test passes on WinXP but fails on Win2K */
    hres = ITask_SetAccountInformation(test_task, dummy_account_name, NULL);
387
    ok(hres == S_OK,
388
            "Failed setting dummy account with no password: %08lx\n", hres);
389
    hres = ITask_GetAccountInformation(test_task, &account_name);
390
    ok(hres == S_OK ||
391
       broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
392 393
              hres == SCHED_E_CANNOT_OPEN_TASK ||
              hres == 0x200),  /* win2k */
394
       "GetAccountInformation failed: %08lx\n", hres);
395 396
    if (hres == S_OK)
    {
397
        ok(!lstrcmpW(account_name, dummy_account_name),
398 399
                "Got %s, expected %s\n", wine_dbgstr_w(account_name),
                wine_dbgstr_w(dummy_account_name));
400 401 402 403 404
        CoTaskMemFree(account_name);
    }

    /* Attempt to set to a dummy account with a (invalid) password */
    /* This test passes on WinXP but fails on Win2K */
405 406 407
    hres = ITask_SetAccountInformation(test_task, dummy_account_name_b,
            dummy_account_name_b);
    ok(hres == S_OK,
408
            "Failed setting dummy account with password: %08lx\n", hres);
409
    hres = ITask_GetAccountInformation(test_task, &account_name);
410
    ok(hres == S_OK ||
411
       broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
412 413
              hres == SCHED_E_CANNOT_OPEN_TASK ||
              hres == 0x200),  /* win2k */
414
       "GetAccountInformation failed: %08lx\n", hres);
415 416
    if (hres == S_OK)
    {
417
        ok(!lstrcmpW(account_name, dummy_account_name_b),
418 419
                "Got %s, expected %s\n", wine_dbgstr_w(account_name),
                wine_dbgstr_w(dummy_account_name_b));
420 421 422 423 424
        CoTaskMemFree(account_name);
    }

    /* Attempt to set to the local system account */
    hres = ITask_SetAccountInformation(test_task, empty, NULL);
425
    ok(hres == S_OK, "Failed setting system account: %08lx\n", hres);
426
    hres = ITask_GetAccountInformation(test_task, &account_name);
427
    ok(hres == S_OK ||
428
       broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
429 430
              hres == SCHED_E_CANNOT_OPEN_TASK ||
              hres == 0x200),  /* win2k */
431
       "GetAccountInformation failed: %08lx\n", hres);
432 433
    if (hres == S_OK)
    {
434
        ok(!lstrcmpW(account_name, empty),
435
                "Got %s, expected empty string\n", wine_dbgstr_w(account_name));
436 437 438
        CoTaskMemFree(account_name);
    }

439
    ITask_Release(test_task);
440 441
}

442
static void test_task_state(void)
443
{
444
    ITask *test_task;
445
    HRESULT hr, status;
446
    DWORD flags, val;
447
    WORD val1, val2;
448
    SYSTEMTIME st;
449

450 451 452
    hr = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                    &IID_ITask, (IUnknown **)&test_task);
    ok(hr == S_OK, "Failed to setup test_task\n");
453 454 455 456 457 458

    if (0) /* crashes under Windows */
        hr = ITask_GetFlags(test_task, NULL);

    flags = 0xdeadbeef;
    hr = ITask_GetFlags(test_task, &flags);
459 460
    ok(hr == S_OK, "GetFlags error %#lx\n", hr);
    ok(flags == 0, "got %#lx\n", flags);
461 462 463 464 465 466

    if (0) /* crashes under Windows */
        hr = ITask_GetTaskFlags(test_task, NULL);

    flags = 0xdeadbeef;
    hr = ITask_GetTaskFlags(test_task, &flags);
467 468
    ok(hr == S_OK, "GetTaskFlags error %#lx\n", hr);
    ok(flags == 0, "got %#lx\n", flags);
469

470 471 472 473 474
    if (0) /* crashes under Windows */
        hr = ITask_GetStatus(test_task, NULL);

    status = 0xdeadbeef;
    hr = ITask_GetStatus(test_task, &status);
475 476
    ok(hr == S_OK, "GetStatus error %#lx\n", hr);
    ok(status == SCHED_S_TASK_NOT_SCHEDULED, "got %#lx\n", status);
477

478 479 480 481
    if (0) /* crashes under Windows */
        hr = ITask_GetErrorRetryCount(test_task, NULL);

    hr = ITask_GetErrorRetryCount(test_task, &val1);
482
    ok(hr == E_NOTIMPL, "got %#lx\n", hr);
483

484 485 486 487
    if (0) /* crashes under Windows */
        hr = ITask_GetErrorRetryInterval(test_task, NULL);

    hr = ITask_GetErrorRetryInterval(test_task, &val1);
488
    ok(hr == E_NOTIMPL, "got %#lx\n", hr);
489

490 491 492 493 494 495
    if (0) /* crashes under Windows */
        hr = ITask_GetIdleWait(test_task, NULL, NULL);

    val1 = 0xdead;
    val2 = 0xbeef;
    hr = ITask_GetIdleWait(test_task, &val1, &val2);
496
    ok(hr == S_OK, "got %#lx\n", hr);
497 498 499
    ok(val1 == 10, "got %u\n", val1);
    ok(val2 == 60, "got %u\n", val2);

500 501 502 503 504
    if (0) /* crashes under Windows */
        hr = ITask_GetPriority(test_task, NULL);

    val = 0xdeadbeef;
    hr = ITask_GetPriority(test_task, &val);
505 506
    ok(hr == S_OK, "got %#lx\n", hr);
    ok(val == NORMAL_PRIORITY_CLASS, "got %#lx\n", val);
507

508 509 510 511 512
    if (0) /* crashes under Windows */
        hr = ITask_GetExitCode(test_task, NULL);

    val = 0xdeadbeef;
    hr = ITask_GetExitCode(test_task, &val);
513 514
    ok(hr == SCHED_S_TASK_HAS_NOT_RUN, "got %#lx\n", hr);
    ok(val == 0, "got %#lx\n", val);
515

516 517 518 519 520
    if (0) /* crashes under Windows */
        hr = ITask_GetMostRecentRunTime(test_task, NULL);

    memset(&st, 0xff, sizeof(st));
    hr = ITask_GetMostRecentRunTime(test_task, &st);
521
    ok(hr == SCHED_S_TASK_HAS_NOT_RUN, "got %#lx\n", hr);
522 523 524 525 526 527 528
    ok(st.wYear == 0, "got %u\n", st.wYear);
    ok(st.wMonth == 0, "got %u\n", st.wMonth);
    ok(st.wDay == 0, "got %u\n", st.wDay);
    ok(st.wHour == 0, "got %u\n", st.wHour);
    ok(st.wMinute == 0, "got %u\n", st.wMinute);
    ok(st.wSecond == 0, "got %u\n", st.wSecond);

529
    ITask_Release(test_task);
530 531
}

532 533 534 535 536 537
static void save_job(ITask *task)
{
    HRESULT hr;
    IPersistFile *pfile;

    hr = ITask_QueryInterface(task, &IID_IPersistFile, (void **)&pfile);
538
    ok(hr == S_OK, "QueryInterface error %#lx\n", hr);
539 540

    hr = IPersistFile_Save(pfile, NULL, FALSE);
541
    ok(hr == S_OK, "got %#lx\n", hr);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

    IPersistFile_Release(pfile);
}

static void test_Run(void)
{
    static const WCHAR wine_test_runW[] = { 'w','i','n','e','_','t','e','s','t','_','r','u','n',0 };
    static const WCHAR cmdW[] = { 'c','m','d','.','e','x','e',0 };
    ITask *task;
    ITaskTrigger *trigger;
    WORD idx, i;
    TASK_TRIGGER trigger_data;
    SYSTEMTIME st;
    HRESULT hr, status;

    /* cleanup after previous runs */
558
    taskscheduler_delete(scheduler, wine_test_runW);
559 560 561

    hr = ITaskScheduler_NewWorkItem(scheduler, wine_test_runW, &CLSID_CTask,
                                    &IID_ITask, (IUnknown **)&task);
562
    ok(hr == S_OK, "got %#lx\n", hr);
563 564

    hr = ITask_Run(task);
565
    ok(hr == SCHED_E_TASK_NOT_READY, "got %#lx\n", hr);
566 567

    hr = ITask_Terminate(task);
568
    ok(hr == SCHED_E_TASK_NOT_RUNNING, "got %#lx\n", hr);
569 570

    hr = ITask_GetStatus(task, &status);
571 572
    ok(hr == S_OK, "got %#lx\n", hr);
    ok(status == SCHED_S_TASK_NOT_SCHEDULED, "got %#lx\n", status);
573 574 575 576

    save_job(task);

    hr = ITask_GetStatus(task, &status);
577 578
    ok(hr == S_OK, "got %#lx\n", hr);
    ok(status == SCHED_S_TASK_NOT_SCHEDULED, "got %#lx\n", status);
579 580

    hr = ITask_CreateTrigger(task, &idx, &trigger);
581
    ok(hr == S_OK, "got %#lx\n", hr);
582 583 584 585 586 587 588 589 590 591 592 593 594 595

    memset(&trigger_data, 0, sizeof(trigger_data));
    trigger_data.cbTriggerSize = sizeof(trigger_data);
    trigger_data.Reserved1 = 0;
    GetLocalTime(&st);
    trigger_data.wBeginYear = st.wYear;
    trigger_data.wBeginMonth = st.wMonth;
    trigger_data.wBeginDay = st.wDay;
    trigger_data.wStartHour = st.wHour;
    trigger_data.wStartMinute = st.wMinute;
    trigger_data.TriggerType = TASK_TIME_TRIGGER_WEEKLY;
    trigger_data.Type.Weekly.WeeksInterval = 1;
    trigger_data.Type.Weekly.rgfDaysOfTheWeek = 0x7f; /* every day */
    hr = ITaskTrigger_SetTrigger(trigger, &trigger_data);
596
    ok(hr == S_OK, "got %#lx\n", hr);
597 598 599
    ITaskTrigger_Release(trigger);

    hr = ITask_SetApplicationName(task, cmdW);
600
    ok(hr == S_OK, "got %#lx\n", hr);
601 602

    hr = ITask_SetParameters(task, empty);
603
    ok(hr == S_OK, "got %#lx\n", hr);
604 605

    hr = ITask_SetWorkingDirectory(task, empty);
606
    ok(hr == S_OK, "got %#lx\n", hr);
607 608 609 610 611

    /* Save the task so that the Scheduler service would notice the changes */
    save_job(task);

    hr = ITask_GetStatus(task, &status);
612 613
    ok(hr == S_OK, "got %#lx\n", hr);
    ok(status == SCHED_S_TASK_HAS_NOT_RUN, "got %#lx\n", status);
614 615

    hr = ITask_Run(task);
616
    ok(hr == S_OK, "got %#lx\n", hr);
617 618 619

    /* According to MSDN the task status doesn't update dynamically */
    hr = ITask_GetStatus(task, &status);
620 621
    ok(hr == S_OK, "got %#lx\n", hr);
    ok(status == SCHED_S_TASK_HAS_NOT_RUN, "got %#lx\n", status);
622 623 624 625 626 627 628 629 630 631

    ITask_Release(task);

    /* Running the process associated with the task to start up may
     * take quite a bit a of time, and waiting for it during the test
     * may be not the best idea.
     *
     * This is how it's supposed to look like in the application
     * (the loop should be infinite):
     */
632
    for (i = 0; i < 5; i++)
633 634
    {
        hr = ITaskScheduler_Activate(scheduler, wine_test_runW, &IID_ITask, (IUnknown **)&task);
635
        ok(hr == S_OK, "Activate error %#lx\n", hr);
636 637

        hr = ITask_GetStatus(task, &status);
638
        ok(hr == S_OK, "got %#lx\n", hr);
639 640 641 642 643 644 645 646 647

        ITask_Release(task);

        if (status == SCHED_S_TASK_RUNNING) break;

        Sleep(100);
    }

    hr = ITaskScheduler_Activate(scheduler, wine_test_runW, &IID_ITask, (IUnknown **)&task);
648
    ok(hr == S_OK, "Activate error %#lx\n", hr);
649 650

    hr = ITask_GetStatus(task, &status);
651
    ok(hr == S_OK, "got %#lx\n", hr);
652 653 654 655

    if (status == SCHED_S_TASK_RUNNING)
    {
        hr = ITask_Terminate(task);
656
        ok(hr == S_OK, "got %#lx\n", hr);
657 658 659 660 661 662 663 664 665 666

        ITask_Release(task);

        /* Waiting for the process associated with the task to terminate
         * may take quite a bit a of time, and waiting for it during the
         * test is not practical.
         *
         * This is how it's supposed to look like in the application
         * (the loop should be infinite):
         */
667
        for (i = 0; i < 5; i++)
668 669
        {
            hr = ITaskScheduler_Activate(scheduler, wine_test_runW, &IID_ITask, (IUnknown **)&task);
670
            ok(hr == S_OK, "Activate error %#lx\n", hr);
671 672

            hr = ITask_GetStatus(task, &status);
673
            ok(hr == S_OK, "got %#lx\n", hr);
674 675 676 677 678 679 680 681 682 683 684

            ITask_Release(task);

            if (status != SCHED_S_TASK_RUNNING) break;

            Sleep(100);
        }
    }
    else
        ITask_Release(task);

685
    hr = taskscheduler_delete(scheduler, wine_test_runW);
686
    ok(hr == S_OK, "got %#lx\n", hr);
687 688
}

689 690 691 692 693 694 695 696
static void test_SetFlags(void)
{
    HRESULT hr;
    ITask *task;
    DWORD flags;

    hr = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                    &IID_ITask, (IUnknown **)&task);
697
    ok(hr == S_OK, "got %#lx\n", hr);
698 699

    hr = ITask_SetFlags(task, 0);
700
    ok(hr == S_OK, "got %#lx\n", hr);
701 702 703

    flags = 0xdeadbeef;
    hr = ITask_GetFlags(task, &flags);
704 705
    ok(hr == S_OK, "got %#lx\n", hr);
    ok(flags == 0, "got %#lx\n", flags);
706 707

    hr = ITask_SetFlags(task, 0xffffffff);
708
    ok(hr == S_OK, "got %#lx\n", hr);
709 710 711

    flags = 0xdeadbeef;
    hr = ITask_GetFlags(task, &flags);
712 713
    ok(hr == S_OK, "got %#lx\n", hr);
    ok(flags == 0x7fff, "got %#lx\n", flags);
714 715

    hr = ITask_SetFlags(task, 0x9234);
716
    ok(hr == S_OK, "got %#lx\n", hr);
717 718 719

    flags = 0xdeadbeef;
    hr = ITask_GetFlags(task, &flags);
720 721
    ok(hr == S_OK, "got %#lx\n", hr);
    ok(flags == 0x1234, "got %#lx\n", flags);
722 723 724 725

    ITask_Release(task);
}

726 727 728 729 730 731 732 733 734 735
static void test_workitem_data(void)
{
    static BYTE hello[] = "Hello World!";
    HRESULT hr;
    ITask *task;
    WORD count;
    BYTE *data;

    hr = ITaskScheduler_NewWorkItem(scheduler, task_name, &CLSID_CTask,
                                    &IID_ITask, (IUnknown **)&task);
736
    ok(hr == S_OK, "got %#lx\n", hr);
737 738 739 740 741 742 743 744 745

    if (0) /* crashes under Windows */
        hr = ITask_GetWorkItemData(task, &count, NULL);
    if (0) /* crashes under Windows */
        hr = ITask_GetWorkItemData(task, NULL, &data);

    count = 0xdead;
    data = (BYTE *)0xdeadbeef;
    hr = ITask_GetWorkItemData(task, &count, &data);
746
    ok(hr == S_OK, "got %#lx\n", hr);
747 748 749 750
    ok(count == 0, "got %u\n", count);
    ok(data == NULL, "got %p\n", data);

    hr = ITask_SetWorkItemData(task, sizeof(hello), NULL);
751
    ok(hr == E_INVALIDARG, "got %#lx\n", hr);
752 753

    hr = ITask_SetWorkItemData(task, 0, hello);
754
    ok(hr == E_INVALIDARG, "got %#lx\n", hr);
755 756

    hr = ITask_SetWorkItemData(task, sizeof(hello), hello);
757
    ok(hr == S_OK, "got %#lx\n", hr);
758 759 760 761

    count = 0xdead;
    data = NULL;
    hr = ITask_GetWorkItemData(task, &count, &data);
762
    ok(hr == S_OK, "got %#lx\n", hr);
763 764 765
    ok(count == sizeof(hello), "got %u\n", count);
    ok(data != NULL, "got NULL\n");
    ok(!memcmp(data, hello, sizeof(hello)), "data mismatch\n");
766
    CoTaskMemFree(data);
767 768

    hr = ITask_SetWorkItemData(task, 0, NULL);
769
    ok(hr == S_OK, "got %#lx\n", hr);
770 771 772 773

    count = 0xdead;
    data = (BYTE *)0xdeadbeef;
    hr = ITask_GetWorkItemData(task, &count, &data);
774
    ok(hr == S_OK, "got %#lx\n", hr);
775 776 777 778 779 780
    ok(count == 0, "got %u\n", count);
    ok(data == NULL, "got %p\n", data);

    ITask_Release(task);
}

781 782
START_TEST(task)
{
783 784
    HRESULT hr;

785
    CoInitialize(NULL);
786 787
    hr = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
                          &IID_ITaskScheduler, (void **)&scheduler);
788
    ok(hr == S_OK, "failed to create task scheduler: %#lx\n", hr);
789

790
    test_SetApplicationName_GetApplicationName();
791
    test_CreateTrigger();
792
    test_SetParameters_GetParameters();
793
    test_SetComment_GetComment();
794
    test_SetMaxRunTime_GetMaxRunTime();
795
    test_SetAccountInformation_GetAccountInformation();
796
    test_task_state();
797
    test_Run();
798
    test_SetFlags();
799
    test_workitem_data();
800 801

    ITaskScheduler_Release(scheduler);
802 803
    CoUninitialize();
}