[QMGR_WINETEST]
[reactos.git] / rostests / winetests / qmgr / file.c
1 /*
2 * Unit test suite for Background Copy File Interface
3 *
4 * Copyright 2007 Google (Roy Shea)
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 #include <stdarg.h>
24
25 #define WIN32_NO_STATUS
26 #define _INC_WINDOWS
27 #define COM_NO_WINDOWS_H
28
29 #include <windef.h>
30 #include <winbase.h>
31 #include <winreg.h>
32
33 #include <shlwapi.h>
34
35 #define COBJMACROS
36
37 #include <wine/test.h>
38 #include <bits.h>
39
40 /* Globals used by many tests */
41 #define NUM_FILES 1
42 static const WCHAR test_remoteName[] = {'r','e','m','o','t','e', 0};
43 static const WCHAR test_localName[] = {'l','o','c','a','l', 0};
44 static WCHAR test_localFile[MAX_PATH];
45 static WCHAR test_remoteUrl[MAX_PATH];
46 static const ULONG test_fileCount = NUM_FILES;
47 static const WCHAR test_displayName[] = {'T','e','s','t', 0};
48 static IBackgroundCopyJob *test_job;
49 static IBackgroundCopyManager *test_manager;
50 static IEnumBackgroundCopyFiles *test_enumFiles;
51 static IBackgroundCopyFile *test_file;
52
53 /* Helper function to add a file to a job. The helper function takes base
54 file name and creates properly formed path and URL strings for creation of
55 the file. */
56 static HRESULT addFileHelper(IBackgroundCopyJob* job,
57 const WCHAR *localName, const WCHAR *remoteName)
58 {
59 DWORD urlSize;
60
61 GetCurrentDirectoryW(MAX_PATH, test_localFile);
62 PathAppendW(test_localFile, localName);
63 GetCurrentDirectoryW(MAX_PATH, test_remoteUrl);
64 PathAppendW(test_remoteUrl, remoteName);
65 urlSize = MAX_PATH;
66 UrlCreateFromPathW(test_remoteUrl, test_remoteUrl, &urlSize, 0);
67 UrlUnescapeW(test_remoteUrl, NULL, &urlSize, URL_UNESCAPE_INPLACE);
68
69 return IBackgroundCopyJob_AddFile(job, test_remoteUrl, test_localFile);
70 }
71
72 static HRESULT test_create_manager(void)
73 {
74 HRESULT hres;
75 IBackgroundCopyManager *manager = NULL;
76
77 /* Creating BITS instance */
78 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL, CLSCTX_LOCAL_SERVER,
79 &IID_IBackgroundCopyManager, (void **) &manager);
80
81 if(hres == HRESULT_FROM_WIN32(ERROR_SERVICE_DISABLED)) {
82 win_skip("Needed Service is disabled\n");
83 return hres;
84 }
85
86 if (hres == S_OK)
87 {
88 IBackgroundCopyJob *job;
89 GUID jobId;
90
91 hres = IBackgroundCopyManager_CreateJob(manager, test_displayName, BG_JOB_TYPE_DOWNLOAD, &jobId, &job);
92 if (hres == S_OK)
93 {
94 hres = addFileHelper(job, test_localName, test_remoteName);
95 if (hres != S_OK)
96 win_skip("AddFile() with file:// protocol failed. Tests will be skipped.\n");
97 IBackgroundCopyJob_Release(job);
98 }
99 IBackgroundCopyManager_Release(manager);
100 }
101
102 return hres;
103 }
104
105 /* Generic test setup */
106 static BOOL setup(void)
107 {
108 HRESULT hres;
109 GUID test_jobId;
110
111 test_manager = NULL;
112 test_job = NULL;
113 memset(&test_jobId, 0, sizeof test_jobId);
114
115 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
116 CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
117 (void **) &test_manager);
118 if(hres != S_OK)
119 return FALSE;
120
121 hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
122 BG_JOB_TYPE_DOWNLOAD, &test_jobId, &test_job);
123 if(hres != S_OK)
124 {
125 IBackgroundCopyManager_Release(test_manager);
126 return FALSE;
127 }
128
129 if (addFileHelper(test_job, test_localName, test_remoteName) != S_OK
130 || IBackgroundCopyJob_EnumFiles(test_job, &test_enumFiles) != S_OK)
131 {
132 IBackgroundCopyJob_Release(test_job);
133 IBackgroundCopyManager_Release(test_manager);
134 return FALSE;
135 }
136
137 hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &test_file, NULL);
138 if(hres != S_OK)
139 {
140 IEnumBackgroundCopyFiles_Release(test_enumFiles);
141 IBackgroundCopyJob_Release(test_job);
142 IBackgroundCopyManager_Release(test_manager);
143 return FALSE;
144 }
145
146 return TRUE;
147 }
148
149 /* Generic test cleanup */
150 static void teardown(void)
151 {
152 IBackgroundCopyFile_Release(test_file);
153 IEnumBackgroundCopyFiles_Release(test_enumFiles);
154 IBackgroundCopyJob_Release(test_job);
155 IBackgroundCopyManager_Release(test_manager);
156 }
157
158 /* Test that the remote name is properly set */
159 static void test_GetRemoteName(void)
160 {
161 HRESULT hres;
162 LPWSTR name;
163
164 hres = IBackgroundCopyFile_GetRemoteName(test_file, &name);
165 ok(hres == S_OK, "GetRemoteName failed: %08x\n", hres);
166 ok(lstrcmpW(name, test_remoteUrl) == 0, "Got incorrect remote name\n");
167 CoTaskMemFree(name);
168 }
169
170 /* Test that the local name is properly set */
171 static void test_GetLocalName(void)
172 {
173 HRESULT hres;
174 LPWSTR name;
175
176 hres = IBackgroundCopyFile_GetLocalName(test_file, &name);
177 ok(hres == S_OK, "GetLocalName failed: %08x\n", hres);
178 ok(lstrcmpW(name, test_localFile) == 0, "Got incorrect local name\n");
179 CoTaskMemFree(name);
180 }
181
182 /* Test getting the progress of a file*/
183 static void test_GetProgress_PreTransfer(void)
184 {
185 HRESULT hres;
186 BG_FILE_PROGRESS progress;
187
188 hres = IBackgroundCopyFile_GetProgress(test_file, &progress);
189 ok(hres == S_OK, "GetProgress failed: %08x\n", hres);
190 ok(progress.BytesTotal == BG_SIZE_UNKNOWN, "Got incorrect total size: %x%08x\n",
191 (DWORD)(progress.BytesTotal >> 32), (DWORD)progress.BytesTotal);
192 ok(progress.BytesTransferred == 0, "Got incorrect number of transferred bytes: %x%08x\n",
193 (DWORD)(progress.BytesTransferred >> 32), (DWORD)progress.BytesTransferred);
194 ok(progress.Completed == FALSE, "Got incorrect completion status\n");
195 }
196
197 typedef void (*test_t)(void);
198
199 START_TEST(file)
200 {
201 static const test_t tests[] = {
202 test_GetRemoteName,
203 test_GetLocalName,
204 test_GetProgress_PreTransfer,
205 0
206 };
207 const test_t *test;
208 int i;
209
210 CoInitialize(NULL);
211
212 if (FAILED(test_create_manager()))
213 {
214 CoUninitialize();
215 win_skip("Failed to create Manager instance, skipping tests\n");
216 return;
217 }
218
219 for (test = tests, i = 0; *test; ++test, ++i)
220 {
221 /* Keep state separate between tests. */
222 if (!setup())
223 {
224 ok(0, "tests:%d: Unable to setup test\n", i);
225 break;
226 }
227 (*test)();
228 teardown();
229 }
230 CoUninitialize();
231 }