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