Release 1.5.29.
[wine] / dlls / qmgr / tests / enum_jobs.c
1 /*
2  * Unit test suite for Enum Background Copy Jobs Interface
3  *
4  * Copyright 2007 Google (Roy Shea, Dan Hipschman)
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 #include <stdio.h>
22
23 #define COBJMACROS
24
25 #include "wine/test.h"
26 #include "bits.h"
27
28 /* Globals used by many tests */
29 static const WCHAR test_displayNameA[] = {'T','e','s','t','A', 0};
30 static const WCHAR test_displayNameB[] = {'T','e','s','t','B', 0};
31 static IBackgroundCopyManager *test_manager;
32 static IBackgroundCopyJob *test_jobA;
33 static IBackgroundCopyJob *test_jobB;
34 static ULONG test_jobCountB;
35 static IEnumBackgroundCopyJobs *test_enumJobsA;
36 static IEnumBackgroundCopyJobs *test_enumJobsB;
37 static GUID test_jobIdA;
38 static GUID test_jobIdB;
39
40 /* Generic test setup */
41 static BOOL setup(void)
42 {
43     HRESULT hres;
44
45     test_manager = NULL;
46     test_jobA = NULL;
47     test_jobB = NULL;
48     memset(&test_jobIdA, 0, sizeof test_jobIdA);
49     memset(&test_jobIdB, 0, sizeof test_jobIdB);
50
51     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
52                             CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
53                             (void **) &test_manager);
54     if(hres != S_OK)
55         return FALSE;
56
57     hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayNameA,
58                                             BG_JOB_TYPE_DOWNLOAD, &test_jobIdA,
59                                             &test_jobA);
60     if(hres != S_OK)
61         return FALSE;
62
63     hres = IBackgroundCopyManager_EnumJobs(test_manager, 0, &test_enumJobsA);
64     if(hres != S_OK)
65         return FALSE;
66
67     hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayNameB,
68                                             BG_JOB_TYPE_DOWNLOAD, &test_jobIdB,
69                                             &test_jobB);
70     if(hres != S_OK)
71         return FALSE;
72
73     hres = IBackgroundCopyManager_EnumJobs(test_manager, 0, &test_enumJobsB);
74     if(hres != S_OK)
75         return FALSE;
76
77     hres = IEnumBackgroundCopyJobs_GetCount(test_enumJobsB, &test_jobCountB);
78     if (hres != S_OK)
79         return FALSE;
80
81     return TRUE;
82 }
83
84 /* Generic test cleanup */
85 static void teardown(void)
86 {
87     if (test_enumJobsB)
88         IEnumBackgroundCopyJobs_Release(test_enumJobsB);
89     test_enumJobsB = NULL;
90     if (test_jobB)
91         IBackgroundCopyJob_Release(test_jobB);
92     test_jobB = NULL;
93     if (test_enumJobsA)
94         IEnumBackgroundCopyJobs_Release(test_enumJobsA);
95     test_enumJobsA = NULL;
96     if (test_jobA)
97         IBackgroundCopyJob_Release(test_jobA);
98     test_jobA = NULL;
99     if (test_manager)
100         IBackgroundCopyManager_Release(test_manager);
101     test_manager = NULL;
102 }
103
104 /* We can't assume the job count will start at any fixed number since esp
105    when testing on Windows there may be other jobs created by other
106    processes.  Even this approach of creating two jobs and checking the
107    difference in counts could fail if a job was created in between, but
108    it's probably not worth worrying about in sane test environments.  */
109 static void test_GetCount(void)
110 {
111     HRESULT hres;
112     ULONG jobCountA, jobCountB;
113
114     hres = IEnumBackgroundCopyJobs_GetCount(test_enumJobsA, &jobCountA);
115     ok(hres == S_OK, "GetCount failed: %08x\n", hres);
116     if(hres != S_OK)
117     {
118         skip("Couldn't get job count\n");
119         return;
120     }
121
122     hres = IEnumBackgroundCopyJobs_GetCount(test_enumJobsB, &jobCountB);
123     ok(hres == S_OK, "GetCount failed: %08x\n", hres);
124     if(hres != S_OK)
125     {
126         skip("Couldn't get job count\n");
127         return;
128     }
129
130     ok(jobCountB == jobCountA + 1, "Got incorrect count\n");
131 }
132
133 /* Test Next with a NULL pceltFetched*/
134 static void test_Next_walkListNull(void)
135 {
136     HRESULT hres;
137     IBackgroundCopyJob *job;
138     ULONG i;
139
140     /* Fetch the available jobs */
141     for (i = 0; i < test_jobCountB; i++)
142     {
143         hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 1, &job, NULL);
144         ok(hres == S_OK, "Next failed: %08x\n", hres);
145         if(hres != S_OK)
146         {
147             skip("Unable to get job from Next\n");
148             return;
149         }
150         IBackgroundCopyJob_Release(job);
151     }
152
153     /* Attempt to fetch one more than the number of available jobs */
154     hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 1, &job, NULL);
155     ok(hres == S_FALSE, "Next off end of available jobs failed: %08x\n", hres);
156 }
157
158 /* Test Next */
159 static void test_Next_walkList_1(void)
160 {
161     HRESULT hres;
162     IBackgroundCopyJob *job;
163     ULONG fetched;
164     ULONG i;
165
166     /* Fetch the available jobs */
167     for (i = 0; i < test_jobCountB; i++)
168     {
169         fetched = 0;
170         hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 1, &job, &fetched);
171         ok(hres == S_OK, "Next failed: %08x\n", hres);
172         if(hres != S_OK)
173         {
174             skip("Unable to get job from Next\n");
175             return;
176         }
177         ok(fetched == 1, "Next returned the incorrect number of jobs: %08x\n", hres);
178         IBackgroundCopyJob_Release(job);
179     }
180
181     /* Attempt to fetch one more than the number of available jobs */
182     fetched = 0;
183     hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 1, &job, &fetched);
184     ok(hres == S_FALSE, "Next off end of available jobs failed: %08x\n", hres);
185     ok(fetched == 0, "Next returned the incorrect number of jobs: %08x\n", hres);
186 }
187
188 /* Test Next by requesting multiple files at a time */
189 static void test_Next_walkList_2(void)
190 {
191     HRESULT hres;
192     IBackgroundCopyJob **jobs;
193     ULONG fetched;
194     ULONG i;
195
196     jobs = HeapAlloc(GetProcessHeap(), 0, test_jobCountB * sizeof *jobs);
197     if (!jobs)
198     {
199         skip("Couldn't allocate memory\n");
200         return;
201     }
202
203     for (i = 0; i < test_jobCountB; i++)
204         jobs[i] = NULL;
205
206     fetched = 0;
207     hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, test_jobCountB, jobs, &fetched);
208     ok(hres == S_OK, "Next failed: %08x\n", hres);
209     if(hres != S_OK)
210     {
211         skip("Unable to get file from test_enumJobs\n");
212         HeapFree(GetProcessHeap(), 0, jobs);
213         return;
214     }
215     ok(fetched == test_jobCountB, "Next returned the incorrect number of jobs: %08x\n", hres);
216
217     for (i = 0; i < test_jobCountB; i++)
218     {
219         ok(jobs[i] != NULL, "Next returned NULL\n");
220         if (jobs[i])
221             IBackgroundCopyJob_Release(jobs[i]);
222     }
223
224     HeapFree(GetProcessHeap(), 0, jobs);
225 }
226
227 /* Test Next Error conditions */
228 static void test_Next_errors(void)
229 {
230     HRESULT hres;
231     IBackgroundCopyJob *jobs[2];
232
233     /* E_INVALIDARG: pceltFetched can ONLY be NULL if celt is 1 */
234     hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 2, jobs, NULL);
235     ok(hres != S_OK, "Invalid call to Next succeeded: %08x\n", hres);
236 }
237
238 /* Test skipping through the jobs in a list */
239 static void test_Skip_walkList(void)
240 {
241     HRESULT hres;
242     ULONG i;
243
244     for (i = 0; i < test_jobCountB; i++)
245     {
246         hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, 1);
247         ok(hres == S_OK, "Skip failed: %08x\n", hres);
248         if(hres != S_OK)
249         {
250             skip("Unable to properly Skip jobs\n");
251             return;
252         }
253     }
254
255     hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, 1);
256     ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
257 }
258
259 /* Test skipping off the end of the list */
260 static void test_Skip_offEnd(void)
261 {
262     HRESULT hres;
263
264     hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, test_jobCountB + 1);
265     ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
266 }
267
268 /* Test reset */
269 static void test_Reset(void)
270 {
271     HRESULT hres;
272
273     hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, test_jobCountB);
274     ok(hres == S_OK, "Skip failed: %08x\n", hres);
275     if (hres != S_OK)
276     {
277         skip("Skip failed\n");
278         return;
279     }
280
281     hres = IEnumBackgroundCopyJobs_Reset(test_enumJobsB);
282     ok(hres == S_OK, "Reset failed: %08x\n", hres);
283     if(hres != S_OK)
284     {
285         skip("Unable to Reset enumerator\n");
286         return;
287     }
288
289     hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, test_jobCountB);
290     ok(hres == S_OK, "Reset failed: %08x\n", hres);
291 }
292
293 typedef void (*test_t)(void);
294
295 START_TEST(enum_jobs)
296 {
297     static const test_t tests[] = {
298         test_GetCount,
299         test_Next_walkListNull,
300         test_Next_walkList_1,
301         test_Next_walkList_2,
302         test_Next_errors,
303         test_Skip_walkList,
304         test_Skip_offEnd,
305         test_Reset,
306         0
307     };
308     const test_t *test;
309
310     CoInitialize(NULL);
311     for (test = tests; *test; ++test)
312     {
313         /* Keep state separate between tests */
314         if (!setup())
315         {
316             teardown();
317             skip("Unable to setup test\n");
318             break;
319         }
320         (*test)();
321         teardown();
322     }
323     CoUninitialize();
324 }