wined3d: Only use a single allocation for each struct private_data.
[wine.git] / dlls / qmgr / tests / enum_files.c
blob0b059209e637b944b48e60d83465f134e7bea9d0
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(job, remoteUrl, localFile);
62 static HRESULT test_create_manager(void)
64 HRESULT hres;
65 IBackgroundCopyManager *manager = NULL;
67 /* Creating BITS instance */
68 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL, CLSCTX_LOCAL_SERVER,
69 &IID_IBackgroundCopyManager, (void **) &manager);
71 if(hres == HRESULT_FROM_WIN32(ERROR_SERVICE_DISABLED)) {
72 win_skip("Needed Service is disabled\n");
73 return hres;
76 if (hres == S_OK)
78 IBackgroundCopyJob *job;
79 GUID jobId;
81 hres = IBackgroundCopyManager_CreateJob(manager, test_displayName, BG_JOB_TYPE_DOWNLOAD, &jobId, &job);
82 if (hres == S_OK)
84 hres = addFileHelper(job, test_localNameA, test_remoteNameA);
85 if (hres != S_OK)
86 win_skip("AddFile() with file:// protocol failed. Tests will be skipped.\n");
87 IBackgroundCopyJob_Release(job);
89 IBackgroundCopyManager_Release(manager);
92 return hres;
95 /* Generic test setup */
96 static BOOL setup(void)
98 HRESULT hres;
99 GUID test_jobId;
101 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
102 CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
103 (void **) &test_manager);
104 if(hres != S_OK)
105 return FALSE;
107 hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
108 BG_JOB_TYPE_DOWNLOAD, &test_jobId,
109 &test_job);
110 if(hres != S_OK)
112 IBackgroundCopyManager_Release(test_manager);
113 return FALSE;
116 if (addFileHelper(test_job, test_localNameA, test_remoteNameA) != S_OK
117 || addFileHelper(test_job, test_localNameB, test_remoteNameB) != S_OK
118 || IBackgroundCopyJob_EnumFiles(test_job, &test_enumFiles) != S_OK)
120 IBackgroundCopyJob_Release(test_job);
121 IBackgroundCopyManager_Release(test_manager);
122 return FALSE;
125 return TRUE;
128 /* Generic test cleanup */
129 static void teardown(void)
131 IEnumBackgroundCopyFiles_Release(test_enumFiles);
132 IBackgroundCopyJob_Release(test_job);
133 IBackgroundCopyManager_Release(test_manager);
136 /* Test GetCount */
137 static void test_GetCount(void)
139 HRESULT hres;
140 ULONG fileCount;
142 hres = IEnumBackgroundCopyFiles_GetCount(test_enumFiles, &fileCount);
143 ok(hres == S_OK, "GetCount failed: %08x\n", hres);
144 ok(fileCount == test_fileCount, "Got incorrect count\n");
147 /* Test Next with a NULL pceltFetched*/
148 static void test_Next_walkListNull(void)
150 HRESULT hres;
151 IBackgroundCopyFile *file;
152 ULONG i;
154 /* Fetch the available files */
155 for (i = 0; i < test_fileCount; i++)
157 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
158 ok(hres == S_OK, "Next failed: %08x\n", hres);
159 IBackgroundCopyFile_Release(file);
162 /* Attempt to fetch one more than the number of available files */
163 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
164 ok(hres == S_FALSE, "Next off end of available files failed: %08x\n", hres);
167 /* Test Next by requesting one file at a time */
168 static void test_Next_walkList_1(void)
170 HRESULT hres;
171 IBackgroundCopyFile *file;
172 ULONG fetched;
173 ULONG i;
175 /* Fetch the available files */
176 for (i = 0; i < test_fileCount; i++)
178 file = NULL;
179 fetched = 0;
180 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, &fetched);
181 ok(hres == S_OK, "Next failed: %08x\n", hres);
182 ok(fetched == 1, "Next returned the incorrect number of files: %08x\n", hres);
183 ok(file != NULL, "Next returned NULL\n");
184 if (file)
185 IBackgroundCopyFile_Release(file);
188 /* Attempt to fetch one more than the number of available files */
189 fetched = 0;
190 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, &fetched);
191 ok(hres == S_FALSE, "Next off end of available files failed: %08x\n", hres);
192 ok(fetched == 0, "Next returned the incorrect number of files: %08x\n", hres);
195 /* Test Next by requesting multiple files at a time */
196 static void test_Next_walkList_2(void)
198 HRESULT hres;
199 IBackgroundCopyFile *files[NUM_FILES];
200 ULONG fetched;
201 ULONG i;
203 for (i = 0; i < test_fileCount; i++)
204 files[i] = NULL;
206 fetched = 0;
207 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, test_fileCount, files, &fetched);
208 ok(hres == S_OK, "Next failed: %08x\n", hres);
209 ok(fetched == test_fileCount, "Next returned the incorrect number of files: %08x\n", hres);
211 for (i = 0; i < test_fileCount; i++)
213 ok(files[i] != NULL, "Next returned NULL\n");
214 if (files[i])
215 IBackgroundCopyFile_Release(files[i]);
219 /* Test Next Error conditions */
220 static void test_Next_errors(void)
222 HRESULT hres;
223 IBackgroundCopyFile *files[NUM_FILES];
225 /* E_INVALIDARG: pceltFetched can ONLY be NULL if celt is 1 */
226 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 2, files, NULL);
227 ok(hres == E_INVALIDARG, "Invalid call to Next succeeded: %08x\n", hres);
230 /* Test skipping through the files in a list */
231 static void test_Skip_walkList(void)
233 HRESULT hres;
234 ULONG i;
236 for (i = 0; i < test_fileCount; i++)
238 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
239 ok(hres == S_OK, "Skip failed: %08x\n", hres);
242 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
243 ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
246 /* Test skipping off the end of the list */
247 static void test_Skip_offEnd(void)
249 HRESULT hres;
251 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount + 1);
252 ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
255 /* Test resetting the file enumerator */
256 static void test_Reset(void)
258 HRESULT hres;
260 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
261 ok(hres == S_OK, "Skip failed: %08x\n", hres);
262 hres = IEnumBackgroundCopyFiles_Reset(test_enumFiles);
263 ok(hres == S_OK, "Reset failed: %08x\n", hres);
264 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
265 ok(hres == S_OK, "Reset failed: %08x\n", hres);
268 typedef void (*test_t)(void);
270 START_TEST(enum_files)
272 static const test_t tests[] = {
273 test_GetCount,
274 test_Next_walkListNull,
275 test_Next_walkList_1,
276 test_Next_walkList_2,
277 test_Next_errors,
278 test_Skip_walkList,
279 test_Skip_offEnd,
280 test_Reset,
283 const test_t *test;
284 int i;
286 CoInitialize(NULL);
288 if (FAILED(test_create_manager()))
290 CoUninitialize();
291 win_skip("Failed to create Manager instance, skipping tests\n");
292 return;
295 for (test = tests, i = 0; *test; ++test, ++i)
297 /* Keep state separate between tests. */
298 if (!setup())
300 ok(0, "tests:%d: Unable to setup test\n", i);
301 break;
303 (*test)();
304 teardown();
306 CoUninitialize();