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