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 */
setup(void)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 */
teardown(void)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.  */
test_GetCount(void)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 
117     hres = IEnumBackgroundCopyJobs_GetCount(test_enumJobsB, &jobCountB);
118     ok(hres == S_OK, "GetCount failed: %08x\n", hres);
119 
120     ok(jobCountB == jobCountA + 1, "Got incorrect count\n");
121 }
122 
123 /* Test Next with a NULL pceltFetched*/
test_Next_walkListNull(void)124 static void test_Next_walkListNull(void)
125 {
126     HRESULT hres;
127     IBackgroundCopyJob *job;
128     ULONG i;
129 
130     /* Fetch the available jobs */
131     for (i = 0; i < test_jobCountB; i++)
132     {
133         hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 1, &job, NULL);
134         ok(hres == S_OK, "Next failed: %08x\n", hres);
135         IBackgroundCopyJob_Release(job);
136     }
137 
138     /* Attempt to fetch one more than the number of available jobs */
139     hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 1, &job, NULL);
140     ok(hres == S_FALSE, "Next off end of available jobs failed: %08x\n", hres);
141 }
142 
143 /* Test Next */
test_Next_walkList_1(void)144 static void test_Next_walkList_1(void)
145 {
146     HRESULT hres;
147     IBackgroundCopyJob *job;
148     ULONG fetched;
149     ULONG i;
150 
151     /* Fetch the available jobs */
152     for (i = 0; i < test_jobCountB; i++)
153     {
154         fetched = 0;
155         hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 1, &job, &fetched);
156         ok(hres == S_OK, "Next failed: %08x\n", hres);
157         ok(fetched == 1, "Next returned the incorrect number of jobs: %08x\n", hres);
158         IBackgroundCopyJob_Release(job);
159     }
160 
161     /* Attempt to fetch one more than the number of available jobs */
162     fetched = 0;
163     hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 1, &job, &fetched);
164     ok(hres == S_FALSE, "Next off end of available jobs failed: %08x\n", hres);
165     ok(fetched == 0, "Next returned the incorrect number of jobs: %08x\n", hres);
166 }
167 
168 /* Test Next by requesting multiple files at a time */
test_Next_walkList_2(void)169 static void test_Next_walkList_2(void)
170 {
171     HRESULT hres;
172     IBackgroundCopyJob **jobs;
173     ULONG fetched;
174     ULONG i;
175 
176     jobs = HeapAlloc(GetProcessHeap(), 0, test_jobCountB * sizeof *jobs);
177     for (i = 0; i < test_jobCountB; i++)
178         jobs[i] = NULL;
179 
180     fetched = 0;
181     hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, test_jobCountB, jobs, &fetched);
182     ok(hres == S_OK, "Next failed: %08x\n", hres);
183     ok(fetched == test_jobCountB, "Next returned the incorrect number of jobs: %08x\n", hres);
184 
185     for (i = 0; i < test_jobCountB; i++)
186     {
187         ok(jobs[i] != NULL, "Next returned NULL\n");
188         if (jobs[i])
189             IBackgroundCopyJob_Release(jobs[i]);
190     }
191 
192     HeapFree(GetProcessHeap(), 0, jobs);
193 }
194 
195 /* Test Next Error conditions */
test_Next_errors(void)196 static void test_Next_errors(void)
197 {
198     HRESULT hres;
199     IBackgroundCopyJob *jobs[2];
200 
201     /* E_INVALIDARG: pceltFetched can ONLY be NULL if celt is 1 */
202     hres = IEnumBackgroundCopyJobs_Next(test_enumJobsB, 2, jobs, NULL);
203     ok(hres != S_OK, "Invalid call to Next succeeded: %08x\n", hres);
204 }
205 
206 /* Test skipping through the jobs in a list */
test_Skip_walkList(void)207 static void test_Skip_walkList(void)
208 {
209     HRESULT hres;
210     ULONG i;
211 
212     for (i = 0; i < test_jobCountB; i++)
213     {
214         hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, 1);
215         ok(hres == S_OK, "Skip failed: %08x\n", hres);
216     }
217 
218     hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, 1);
219     ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
220 }
221 
222 /* Test skipping off the end of the list */
test_Skip_offEnd(void)223 static void test_Skip_offEnd(void)
224 {
225     HRESULT hres;
226 
227     hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, test_jobCountB + 1);
228     ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
229 }
230 
231 /* Test reset */
test_Reset(void)232 static void test_Reset(void)
233 {
234     HRESULT hres;
235 
236     hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, test_jobCountB);
237     ok(hres == S_OK, "Skip failed: %08x\n", hres);
238 
239     hres = IEnumBackgroundCopyJobs_Reset(test_enumJobsB);
240     ok(hres == S_OK, "Reset failed: %08x\n", hres);
241 
242     hres = IEnumBackgroundCopyJobs_Skip(test_enumJobsB, test_jobCountB);
243     ok(hres == S_OK, "Reset failed: %08x\n", hres);
244 }
245 
246 typedef void (*test_t)(void);
247 
START_TEST(enum_jobs)248 START_TEST(enum_jobs)
249 {
250     static const test_t tests[] = {
251         test_GetCount,
252         test_Next_walkListNull,
253         test_Next_walkList_1,
254         test_Next_walkList_2,
255         test_Next_errors,
256         test_Skip_walkList,
257         test_Skip_offEnd,
258         test_Reset,
259         0
260     };
261     const test_t *test;
262     int i;
263 
264     CoInitialize(NULL);
265     for (test = tests, i = 0; *test; ++test, ++i)
266     {
267         /* Keep state separate between tests */
268         if (!setup())
269         {
270             teardown();
271             ok(0, "tests:%d: Unable to setup test\n", i);
272             break;
273         }
274         (*test)();
275         teardown();
276     }
277     CoUninitialize();
278 }
279