qmgr/tests: Add tests.
[wine/multimedia.git] / dlls / qmgr / tests / enum_files.c
blob9564d7c1adb01998bdc920eea69508fa7fff3cbe
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 "initguid.h"
28 #include "bits.h"
30 /* Globals used by many tests */
31 #define NUM_FILES 2 /* At least two. */
32 static const WCHAR test_remoteNameA[] = {'r','e','m','o','t','e','A', 0};
33 static const WCHAR test_localNameA[] = {'l','o','c','a','l','A', 0};
34 static const WCHAR test_remoteNameB[] = {'r','e','m','o','t','e','B', 0};
35 static const WCHAR test_localNameB[] = {'l','o','c','a','l','B', 0};
36 static const WCHAR test_displayName[] = {'T', 'e', 's', 't', 0};
37 static const ULONG test_fileCount = NUM_FILES;
38 static IBackgroundCopyJob *test_job;
39 static IBackgroundCopyManager *test_manager;
40 static IEnumBackgroundCopyFiles *test_enumFiles;
42 /* Helper function to add a file to a job. The helper function takes base
43 file name and creates properly formed path and URL strings for creation of
44 the file. */
45 static HRESULT addFileHelper(IBackgroundCopyJob* job,
46 const WCHAR *localName, const WCHAR *remoteName)
48 DWORD urlSize;
49 WCHAR localFile[MAX_PATH];
50 WCHAR remoteUrl[MAX_PATH];
51 WCHAR remoteFile[MAX_PATH];
53 GetCurrentDirectoryW(MAX_PATH, localFile);
54 PathAppendW(localFile, localName);
55 GetCurrentDirectoryW(MAX_PATH, remoteFile);
56 PathAppendW(remoteFile, remoteName);
57 urlSize = MAX_PATH;
58 UrlCreateFromPathW(remoteFile, remoteUrl, &urlSize, 0);
59 UrlUnescapeW(remoteUrl, NULL, &urlSize, URL_UNESCAPE_INPLACE);
60 return IBackgroundCopyJob_AddFile(job, remoteUrl, localFile);
63 static HRESULT test_create_manager(void)
65 HRESULT hres;
66 IBackgroundCopyManager *manager = NULL;
68 /* Creating BITS instance */
69 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL, CLSCTX_LOCAL_SERVER,
70 &IID_IBackgroundCopyManager, (void **) &manager);
72 if(hres == HRESULT_FROM_WIN32(ERROR_SERVICE_DISABLED)) {
73 win_skip("Needed Service is disabled\n");
74 return hres;
77 if (hres == S_OK)
79 IBackgroundCopyJob *job;
80 GUID jobId;
82 hres = IBackgroundCopyManager_CreateJob(manager, test_displayName, BG_JOB_TYPE_DOWNLOAD, &jobId, &job);
83 if (hres == S_OK)
85 hres = addFileHelper(job, test_localNameA, test_remoteNameA);
86 if (hres != S_OK)
87 win_skip("AddFile() with file:// protocol failed. Tests will be skipped.\n");
88 IBackgroundCopyJob_Release(job);
90 IBackgroundCopyManager_Release(manager);
93 return hres;
96 /* Generic test setup */
97 static BOOL setup(void)
99 HRESULT hres;
100 GUID test_jobId;
102 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
103 CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
104 (void **) &test_manager);
105 if(hres != S_OK)
106 return FALSE;
108 hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
109 BG_JOB_TYPE_DOWNLOAD, &test_jobId,
110 &test_job);
111 if(hres != S_OK)
113 IBackgroundCopyManager_Release(test_manager);
114 return FALSE;
117 if (addFileHelper(test_job, test_localNameA, test_remoteNameA) != S_OK
118 || addFileHelper(test_job, test_localNameB, test_remoteNameB) != S_OK
119 || IBackgroundCopyJob_EnumFiles(test_job, &test_enumFiles) != S_OK)
121 IBackgroundCopyJob_Release(test_job);
122 IBackgroundCopyManager_Release(test_manager);
123 return FALSE;
126 return TRUE;
129 /* Generic test cleanup */
130 static void teardown(void)
132 IEnumBackgroundCopyFiles_Release(test_enumFiles);
133 IBackgroundCopyJob_Release(test_job);
134 IBackgroundCopyManager_Release(test_manager);
137 /* Test GetCount */
138 static void test_GetCount(void)
140 HRESULT hres;
141 ULONG fileCount;
143 hres = IEnumBackgroundCopyFiles_GetCount(test_enumFiles, &fileCount);
144 ok(hres == S_OK, "GetCount failed: %08x\n", hres);
145 ok(fileCount == test_fileCount, "Got incorrect count\n");
148 /* Test Next with a NULL pceltFetched*/
149 static void test_Next_walkListNull(void)
151 HRESULT hres;
152 IBackgroundCopyFile *file;
153 ULONG i;
155 /* Fetch the available files */
156 for (i = 0; i < test_fileCount; i++)
158 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
159 ok(hres == S_OK, "Next failed: %08x\n", hres);
160 IBackgroundCopyFile_Release(file);
163 /* Attempt to fetch one more than the number of available files */
164 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
165 ok(hres == S_FALSE, "Next off end of available files failed: %08x\n", hres);
168 /* Test Next by requesting one file at a time */
169 static void test_Next_walkList_1(void)
171 HRESULT hres;
172 IBackgroundCopyFile *file;
173 ULONG fetched;
174 ULONG i;
176 /* Fetch the available files */
177 for (i = 0; i < test_fileCount; i++)
179 file = NULL;
180 fetched = 0;
181 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, &fetched);
182 ok(hres == S_OK, "Next failed: %08x\n", hres);
183 ok(fetched == 1, "Next returned the incorrect number of files: %08x\n", hres);
184 ok(file != NULL, "Next returned NULL\n");
185 if (file)
186 IBackgroundCopyFile_Release(file);
189 /* Attempt to fetch one more than the number of available files */
190 fetched = 0;
191 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, &fetched);
192 ok(hres == S_FALSE, "Next off end of available files failed: %08x\n", hres);
193 ok(fetched == 0, "Next returned the incorrect number of files: %08x\n", hres);
196 /* Test Next by requesting multiple files at a time */
197 static void test_Next_walkList_2(void)
199 HRESULT hres;
200 IBackgroundCopyFile *files[NUM_FILES];
201 ULONG fetched;
202 ULONG i;
204 for (i = 0; i < test_fileCount; i++)
205 files[i] = NULL;
207 fetched = 0;
208 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, test_fileCount, files, &fetched);
209 ok(hres == S_OK, "Next failed: %08x\n", hres);
210 ok(fetched == test_fileCount, "Next returned the incorrect number of files: %08x\n", hres);
212 for (i = 0; i < test_fileCount; i++)
214 ok(files[i] != NULL, "Next returned NULL\n");
215 if (files[i])
216 IBackgroundCopyFile_Release(files[i]);
220 /* Test Next Error conditions */
221 static void test_Next_errors(void)
223 HRESULT hres;
224 IBackgroundCopyFile *files[NUM_FILES];
226 /* E_INVALIDARG: pceltFetched can ONLY be NULL if celt is 1 */
227 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 2, files, NULL);
228 ok(hres == E_INVALIDARG, "Invalid call to Next succeeded: %08x\n", hres);
231 /* Test skipping through the files in a list */
232 static void test_Skip_walkList(void)
234 HRESULT hres;
235 ULONG i;
237 for (i = 0; i < test_fileCount; i++)
239 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
240 ok(hres == S_OK, "Skip failed: %08x\n", hres);
243 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
244 ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
247 /* Test skipping off the end of the list */
248 static void test_Skip_offEnd(void)
250 HRESULT hres;
252 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount + 1);
253 ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
256 /* Test resetting the file enumerator */
257 static void test_Reset(void)
259 HRESULT hres;
261 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
262 ok(hres == S_OK, "Skip failed: %08x\n", hres);
263 hres = IEnumBackgroundCopyFiles_Reset(test_enumFiles);
264 ok(hres == S_OK, "Reset failed: %08x\n", hres);
265 hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
266 ok(hres == S_OK, "Reset failed: %08x\n", hres);
269 typedef void (*test_t)(void);
271 START_TEST(enum_files)
273 static const test_t tests[] = {
274 test_GetCount,
275 test_Next_walkListNull,
276 test_Next_walkList_1,
277 test_Next_walkList_2,
278 test_Next_errors,
279 test_Skip_walkList,
280 test_Skip_offEnd,
281 test_Reset,
284 const test_t *test;
285 int i;
287 CoInitialize(NULL);
289 if (FAILED(test_create_manager()))
291 CoUninitialize();
292 win_skip("Failed to create Manager instance, skipping tests\n");
293 return;
296 for (test = tests, i = 0; *test; ++test, ++i)
298 /* Keep state separate between tests. */
299 if (!setup())
301 ok(0, "tests:%d: Unable to setup test\n", i);
302 break;
304 (*test)();
305 teardown();
307 CoUninitialize();