notepad: Rename the SIZEOF macro to the more descriptive ARRAY_SIZE.
[wine.git] / dlls / qmgr / tests / enum_files.c
blobeeb800958299a65411e609b0133554f0f6b615d3
1 /*
2 * Unit test suite for Enum Background Copy Files Interface
4 * Copyright 2007, 2008 Google (Roy Shea, Dan Hipschman)
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.
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.
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
21 #include <shlwapi.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "wine/test.h"
27 #include "bits.h"
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;
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)
47 DWORD urlSize;
48 WCHAR localFile[MAX_PATH];
49 WCHAR remoteUrl[MAX_PATH];
50 WCHAR remoteFile[MAX_PATH];
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);
62 /* Generic test setup */
63 static BOOL setup(void)
65 HRESULT hres;
66 GUID test_jobId;
68 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
69 CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
70 (void **) &test_manager);
71 if(hres != S_OK)
72 return FALSE;
74 hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
75 BG_JOB_TYPE_DOWNLOAD, &test_jobId,
76 &test_job);
77 if(hres != S_OK)
79 IBackgroundCopyManager_Release(test_manager);
80 return FALSE;
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)
87 IBackgroundCopyJob_Release(test_job);
88 IBackgroundCopyManager_Release(test_manager);
89 return FALSE;
92 return TRUE;
95 /* Generic test cleanup */
96 static void teardown(void)
98 IEnumBackgroundCopyFiles_Release(test_enumFiles);
99 IBackgroundCopyJob_Release(test_job);
100 IBackgroundCopyManager_Release(test_manager);
103 /* Test GetCount */
104 static void test_GetCount(void)
106 HRESULT hres;
107 ULONG fileCount;
109 hres = IEnumBackgroundCopyFiles_GetCount(test_enumFiles, &fileCount);
110 ok(hres == S_OK, "GetCount failed: %08x\n", hres);
111 if(hres != S_OK)
113 skip("Unable to get count from test_enumFiles.\n");
114 return;
116 ok(fileCount == test_fileCount, "Got incorrect count\n");
119 /* Test Next with a NULL pceltFetched*/
120 static void test_Next_walkListNull(void)
122 HRESULT hres;
123 IBackgroundCopyFile *file;
124 ULONG i;
126 /* Fetch the available files */
127 for (i = 0; i < test_fileCount; i++)
129 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
130 ok(hres == S_OK, "Next failed: %08x\n", hres);
131 if(hres != S_OK)
133 skip("Unable to get file from test_enumFiles\n");
134 return;
136 IBackgroundCopyFile_Release(file);
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);
144 /* Test Next by requesting one file at a time */
145 static void test_Next_walkList_1(void)
147 HRESULT hres;
148 IBackgroundCopyFile *file;
149 ULONG fetched;
150 ULONG i;
152 /* Fetch the available files */
153 for (i = 0; i < test_fileCount; i++)
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)
161 skip("Unable to get file from test_enumFiles\n");
162 return;
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);
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);
177 /* Test Next by requesting multiple files at a time */
178 static void test_Next_walkList_2(void)
180 HRESULT hres;
181 IBackgroundCopyFile *files[NUM_FILES];
182 ULONG fetched;
183 ULONG i;
185 for (i = 0; i < test_fileCount; i++)
186 files[i] = NULL;
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)
193 skip("Unable to get file from test_enumFiles\n");
194 return;
196 ok(fetched == test_fileCount, "Next returned the incorrect number of files: %08x\n", hres);
198 for (i = 0; i < test_fileCount; i++)
200 ok(files[i] != NULL, "Next returned NULL\n");
201 if (files[i])
202 IBackgroundCopyFile_Release(files[i]);
206 /* Test Next Error conditions */
207 static void test_Next_errors(void)
209 HRESULT hres;
210 IBackgroundCopyFile *files[NUM_FILES];
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);
217 /* Test skipping through the files in a list */
218 static void test_Skip_walkList(void)
220 HRESULT hres;
221 ULONG i;
223 for (i = 0; i < test_fileCount; i++)
225 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
226 ok(hres == S_OK, "Skip failed: %08x\n", hres);
227 if(hres != S_OK)
229 skip("Unable to propely Skip files\n");
230 return;
234 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
235 ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
238 /* Test skipping off the end of the list */
239 static void test_Skip_offEnd(void)
241 HRESULT hres;
243 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount + 1);
244 ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
247 /* Test resetting the file enumerator */
248 static void test_Reset(void)
250 HRESULT hres;
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)
258 skip("Unable to Reset enumerator\n");
259 return;
261 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
262 ok(hres == S_OK, "Reset failed: %08x\n", hres);
265 typedef void (*test_t)(void);
267 START_TEST(enum_files)
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,
280 const test_t *test;
282 CoInitialize(NULL);
283 for (test = tests; *test; ++test)
285 /* Keep state separate between tests. */
286 if (!setup())
288 skip("Unable to setup test\n");
289 break;
291 (*test)();
292 teardown();
294 CoUninitialize();