Release 1.5.29.
[wine] / dlls / qmgr / tests / enum_files.c
1 /*
2  * Unit test suite for Enum Background Copy Files Interface
3  *
4  * Copyright 2007, 2008 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 <shlwapi.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "wine/test.h"
27 #include "bits.h"
28
29 /* Globals used by many tests */
30 #define NUM_FILES 2             /* At least two.  */
31 static const WCHAR test_remoteNameA[] = {'r','e','m','o','t','e','A', 0};
32 static const WCHAR test_localNameA[] = {'l','o','c','a','l','A', 0};
33 static const WCHAR test_remoteNameB[] = {'r','e','m','o','t','e','B', 0};
34 static const WCHAR test_localNameB[] = {'l','o','c','a','l','B', 0};
35 static const WCHAR test_displayName[] = {'T', 'e', 's', 't', 0};
36 static const ULONG test_fileCount = NUM_FILES;
37 static IBackgroundCopyJob *test_job;
38 static IBackgroundCopyManager *test_manager;
39 static IEnumBackgroundCopyFiles *test_enumFiles;
40
41 /* Helper function to add a file to a job.  The helper function takes base
42    file name and creates properly formed path and URL strings for creation of
43    the file. */
44 static HRESULT addFileHelper(IBackgroundCopyJob* job,
45                              const WCHAR *localName, const WCHAR *remoteName)
46 {
47     DWORD urlSize;
48     WCHAR localFile[MAX_PATH];
49     WCHAR remoteUrl[MAX_PATH];
50     WCHAR remoteFile[MAX_PATH];
51
52     GetCurrentDirectoryW(MAX_PATH, localFile);
53     PathAppendW(localFile, localName);
54     GetCurrentDirectoryW(MAX_PATH, remoteFile);
55     PathAppendW(remoteFile, remoteName);
56     urlSize = MAX_PATH;
57     UrlCreateFromPathW(remoteFile, remoteUrl, &urlSize, 0);
58     UrlUnescapeW(remoteUrl, NULL, &urlSize, URL_UNESCAPE_INPLACE);
59     return IBackgroundCopyJob_AddFile(test_job, remoteUrl, localFile);
60 }
61
62 /* Generic test setup */
63 static BOOL setup(void)
64 {
65     HRESULT hres;
66     GUID test_jobId;
67
68     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
69                             CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
70                             (void **) &test_manager);
71     if(hres != S_OK)
72         return FALSE;
73
74     hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
75                                             BG_JOB_TYPE_DOWNLOAD, &test_jobId,
76                                             &test_job);
77     if(hres != S_OK)
78     {
79         IBackgroundCopyManager_Release(test_manager);
80         return FALSE;
81     }
82
83     if (addFileHelper(test_job, test_localNameA, test_remoteNameA) != S_OK
84         || addFileHelper(test_job, test_localNameB, test_remoteNameB) != S_OK
85         || IBackgroundCopyJob_EnumFiles(test_job, &test_enumFiles) != S_OK)
86     {
87         IBackgroundCopyJob_Release(test_job);
88         IBackgroundCopyManager_Release(test_manager);
89         return FALSE;
90     }
91
92     return TRUE;
93 }
94
95 /* Generic test cleanup */
96 static void teardown(void)
97 {
98     IEnumBackgroundCopyFiles_Release(test_enumFiles);
99     IBackgroundCopyJob_Release(test_job);
100     IBackgroundCopyManager_Release(test_manager);
101 }
102
103 /* Test GetCount */
104 static void test_GetCount(void)
105 {
106     HRESULT hres;
107     ULONG fileCount;
108
109     hres = IEnumBackgroundCopyFiles_GetCount(test_enumFiles, &fileCount);
110     ok(hres == S_OK, "GetCount failed: %08x\n", hres);
111     if(hres != S_OK)
112     {
113         skip("Unable to get count from test_enumFiles.\n");
114         return;
115     }
116     ok(fileCount == test_fileCount, "Got incorrect count\n");
117 }
118
119 /* Test Next with a NULL pceltFetched*/
120 static void test_Next_walkListNull(void)
121 {
122     HRESULT hres;
123     IBackgroundCopyFile *file;
124     ULONG i;
125
126     /* Fetch the available files */
127     for (i = 0; i < test_fileCount; i++)
128     {
129         hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
130         ok(hres == S_OK, "Next failed: %08x\n", hres);
131         if(hres != S_OK)
132         {
133             skip("Unable to get file from test_enumFiles\n");
134             return;
135         }
136         IBackgroundCopyFile_Release(file);
137     }
138
139     /* Attempt to fetch one more than the number of available files */
140     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
141     ok(hres == S_FALSE, "Next off end of available files failed: %08x\n", hres);
142 }
143
144 /* Test Next by requesting one file at a time */
145 static void test_Next_walkList_1(void)
146 {
147     HRESULT hres;
148     IBackgroundCopyFile *file;
149     ULONG fetched;
150     ULONG i;
151
152     /* Fetch the available files */
153     for (i = 0; i < test_fileCount; i++)
154     {
155         file = NULL;
156         fetched = 0;
157         hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, &fetched);
158         ok(hres == S_OK, "Next failed: %08x\n", hres);
159         if(hres != S_OK)
160         {
161             skip("Unable to get file from test_enumFiles\n");
162             return;
163         }
164         ok(fetched == 1, "Next returned the incorrect number of files: %08x\n", hres);
165         ok(file != NULL, "Next returned NULL\n");
166         if (file)
167             IBackgroundCopyFile_Release(file);
168     }
169
170     /* Attempt to fetch one more than the number of available files */
171     fetched = 0;
172     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, &fetched);
173     ok(hres == S_FALSE, "Next off end of available files failed: %08x\n", hres);
174     ok(fetched == 0, "Next returned the incorrect number of files: %08x\n", hres);
175 }
176
177 /* Test Next by requesting multiple files at a time */
178 static void test_Next_walkList_2(void)
179 {
180     HRESULT hres;
181     IBackgroundCopyFile *files[NUM_FILES];
182     ULONG fetched;
183     ULONG i;
184
185     for (i = 0; i < test_fileCount; i++)
186         files[i] = NULL;
187
188     fetched = 0;
189     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, test_fileCount, files, &fetched);
190     ok(hres == S_OK, "Next failed: %08x\n", hres);
191     if(hres != S_OK)
192     {
193         skip("Unable to get file from test_enumFiles\n");
194         return;
195     }
196     ok(fetched == test_fileCount, "Next returned the incorrect number of files: %08x\n", hres);
197
198     for (i = 0; i < test_fileCount; i++)
199     {
200         ok(files[i] != NULL, "Next returned NULL\n");
201         if (files[i])
202             IBackgroundCopyFile_Release(files[i]);
203     }
204 }
205
206 /* Test Next Error conditions */
207 static void test_Next_errors(void)
208 {
209     HRESULT hres;
210     IBackgroundCopyFile *files[NUM_FILES];
211
212     /* E_INVALIDARG: pceltFetched can ONLY be NULL if celt is 1 */
213     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 2, files, NULL);
214     ok(hres == E_INVALIDARG, "Invalid call to Next succeeded: %08x\n", hres);
215 }
216
217 /* Test skipping through the files in a list */
218 static void test_Skip_walkList(void)
219 {
220     HRESULT hres;
221     ULONG i;
222
223     for (i = 0; i < test_fileCount; i++)
224     {
225         hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
226         ok(hres == S_OK, "Skip failed: %08x\n", hres);
227         if(hres != S_OK)
228         {
229             skip("Unable to properly Skip files\n");
230             return;
231         }
232     }
233
234     hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
235     ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
236 }
237
238 /* Test skipping off the end of the list */
239 static void test_Skip_offEnd(void)
240 {
241     HRESULT hres;
242
243     hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount + 1);
244     ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
245 }
246
247 /* Test resetting the file enumerator */
248 static void test_Reset(void)
249 {
250     HRESULT hres;
251
252     hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
253     ok(hres == S_OK, "Skip failed: %08x\n", hres);
254     hres = IEnumBackgroundCopyFiles_Reset(test_enumFiles);
255     ok(hres == S_OK, "Reset failed: %08x\n", hres);
256     if(hres != S_OK)
257     {
258         skip("Unable to Reset enumerator\n");
259         return;
260     }
261     hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
262     ok(hres == S_OK, "Reset failed: %08x\n", hres);
263 }
264
265 typedef void (*test_t)(void);
266
267 START_TEST(enum_files)
268 {
269     static const test_t tests[] = {
270         test_GetCount,
271         test_Next_walkListNull,
272         test_Next_walkList_1,
273         test_Next_walkList_2,
274         test_Next_errors,
275         test_Skip_walkList,
276         test_Skip_offEnd,
277         test_Reset,
278         0
279     };
280     const test_t *test;
281
282     CoInitialize(NULL);
283     for (test = tests; *test; ++test)
284     {
285         /* Keep state separate between tests. */
286         if (!setup())
287         {
288             skip("Unable to setup test\n");
289             break;
290         }
291         (*test)();
292         teardown();
293     }
294     CoUninitialize();
295 }