1c2c66affSColin Finck /*
2c2c66affSColin Finck * Unit test suite for Background Copy File Interface
3c2c66affSColin Finck *
4c2c66affSColin Finck * Copyright 2007 Google (Roy Shea)
5c2c66affSColin Finck *
6c2c66affSColin Finck * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck * License as published by the Free Software Foundation; either
9c2c66affSColin Finck * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck *
11c2c66affSColin Finck * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14c2c66affSColin Finck * Lesser General Public License for more details.
15c2c66affSColin Finck *
16c2c66affSColin Finck * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck */
20c2c66affSColin Finck
21*c851a1bfSAmine Khaldi #include <stdio.h>
22c2c66affSColin Finck
23c2c66affSColin Finck #include <shlwapi.h>
24c2c66affSColin Finck
25c2c66affSColin Finck #define COBJMACROS
26c2c66affSColin Finck
27*c851a1bfSAmine Khaldi #include "wine/test.h"
28*c851a1bfSAmine Khaldi #include "bits.h"
29c2c66affSColin Finck
30c2c66affSColin Finck /* Globals used by many tests */
31c2c66affSColin Finck static const WCHAR test_remoteName[] = {'r','e','m','o','t','e', 0};
32c2c66affSColin Finck static const WCHAR test_localName[] = {'l','o','c','a','l', 0};
33c2c66affSColin Finck static WCHAR test_localFile[MAX_PATH];
34c2c66affSColin Finck static WCHAR test_remoteUrl[MAX_PATH];
35c2c66affSColin Finck static const WCHAR test_displayName[] = {'T','e','s','t', 0};
36c2c66affSColin Finck static IBackgroundCopyJob *test_job;
37c2c66affSColin Finck static IBackgroundCopyManager *test_manager;
38c2c66affSColin Finck static IEnumBackgroundCopyFiles *test_enumFiles;
39c2c66affSColin Finck static IBackgroundCopyFile *test_file;
40c2c66affSColin Finck
41c2c66affSColin Finck /* Helper function to add a file to a job. The helper function takes base
42c2c66affSColin Finck file name and creates properly formed path and URL strings for creation of
43c2c66affSColin Finck the file. */
addFileHelper(IBackgroundCopyJob * job,const WCHAR * localName,const WCHAR * remoteName)44c2c66affSColin Finck static HRESULT addFileHelper(IBackgroundCopyJob* job,
45c2c66affSColin Finck const WCHAR *localName, const WCHAR *remoteName)
46c2c66affSColin Finck {
47c2c66affSColin Finck DWORD urlSize;
48c2c66affSColin Finck
49c2c66affSColin Finck GetCurrentDirectoryW(MAX_PATH, test_localFile);
50c2c66affSColin Finck PathAppendW(test_localFile, localName);
51c2c66affSColin Finck GetCurrentDirectoryW(MAX_PATH, test_remoteUrl);
52c2c66affSColin Finck PathAppendW(test_remoteUrl, remoteName);
53c2c66affSColin Finck urlSize = MAX_PATH;
54c2c66affSColin Finck UrlCreateFromPathW(test_remoteUrl, test_remoteUrl, &urlSize, 0);
55c2c66affSColin Finck UrlUnescapeW(test_remoteUrl, NULL, &urlSize, URL_UNESCAPE_INPLACE);
56c2c66affSColin Finck
57c2c66affSColin Finck return IBackgroundCopyJob_AddFile(job, test_remoteUrl, test_localFile);
58c2c66affSColin Finck }
59c2c66affSColin Finck
test_create_manager(void)60c2c66affSColin Finck static HRESULT test_create_manager(void)
61c2c66affSColin Finck {
62c2c66affSColin Finck HRESULT hres;
63c2c66affSColin Finck IBackgroundCopyManager *manager = NULL;
64c2c66affSColin Finck
65c2c66affSColin Finck /* Creating BITS instance */
66c2c66affSColin Finck hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL, CLSCTX_LOCAL_SERVER,
67c2c66affSColin Finck &IID_IBackgroundCopyManager, (void **) &manager);
68c2c66affSColin Finck
69c2c66affSColin Finck if(hres == HRESULT_FROM_WIN32(ERROR_SERVICE_DISABLED)) {
70c2c66affSColin Finck win_skip("Needed Service is disabled\n");
71c2c66affSColin Finck return hres;
72c2c66affSColin Finck }
73c2c66affSColin Finck
74c2c66affSColin Finck if (hres == S_OK)
75c2c66affSColin Finck {
76c2c66affSColin Finck IBackgroundCopyJob *job;
77c2c66affSColin Finck GUID jobId;
78c2c66affSColin Finck
79c2c66affSColin Finck hres = IBackgroundCopyManager_CreateJob(manager, test_displayName, BG_JOB_TYPE_DOWNLOAD, &jobId, &job);
80c2c66affSColin Finck if (hres == S_OK)
81c2c66affSColin Finck {
82c2c66affSColin Finck hres = addFileHelper(job, test_localName, test_remoteName);
83c2c66affSColin Finck if (hres != S_OK)
84c2c66affSColin Finck win_skip("AddFile() with file:// protocol failed. Tests will be skipped.\n");
85c2c66affSColin Finck IBackgroundCopyJob_Release(job);
86c2c66affSColin Finck }
87c2c66affSColin Finck IBackgroundCopyManager_Release(manager);
88c2c66affSColin Finck }
89c2c66affSColin Finck
90c2c66affSColin Finck return hres;
91c2c66affSColin Finck }
92c2c66affSColin Finck
93c2c66affSColin Finck /* Generic test setup */
setup(void)94c2c66affSColin Finck static BOOL setup(void)
95c2c66affSColin Finck {
96c2c66affSColin Finck HRESULT hres;
97c2c66affSColin Finck GUID test_jobId;
98c2c66affSColin Finck
99c2c66affSColin Finck test_manager = NULL;
100c2c66affSColin Finck test_job = NULL;
101c2c66affSColin Finck memset(&test_jobId, 0, sizeof test_jobId);
102c2c66affSColin Finck
103c2c66affSColin Finck hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
104c2c66affSColin Finck CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
105c2c66affSColin Finck (void **) &test_manager);
106c2c66affSColin Finck if(hres != S_OK)
107c2c66affSColin Finck return FALSE;
108c2c66affSColin Finck
109c2c66affSColin Finck hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
110c2c66affSColin Finck BG_JOB_TYPE_DOWNLOAD, &test_jobId, &test_job);
111c2c66affSColin Finck if(hres != S_OK)
112c2c66affSColin Finck {
113c2c66affSColin Finck IBackgroundCopyManager_Release(test_manager);
114c2c66affSColin Finck return FALSE;
115c2c66affSColin Finck }
116c2c66affSColin Finck
117c2c66affSColin Finck if (addFileHelper(test_job, test_localName, test_remoteName) != S_OK
118c2c66affSColin Finck || IBackgroundCopyJob_EnumFiles(test_job, &test_enumFiles) != S_OK)
119c2c66affSColin Finck {
120c2c66affSColin Finck IBackgroundCopyJob_Release(test_job);
121c2c66affSColin Finck IBackgroundCopyManager_Release(test_manager);
122c2c66affSColin Finck return FALSE;
123c2c66affSColin Finck }
124c2c66affSColin Finck
125c2c66affSColin Finck hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &test_file, NULL);
126c2c66affSColin Finck if(hres != S_OK)
127c2c66affSColin Finck {
128c2c66affSColin Finck IEnumBackgroundCopyFiles_Release(test_enumFiles);
129c2c66affSColin Finck IBackgroundCopyJob_Release(test_job);
130c2c66affSColin Finck IBackgroundCopyManager_Release(test_manager);
131c2c66affSColin Finck return FALSE;
132c2c66affSColin Finck }
133c2c66affSColin Finck
134c2c66affSColin Finck return TRUE;
135c2c66affSColin Finck }
136c2c66affSColin Finck
137c2c66affSColin Finck /* Generic test cleanup */
teardown(void)138c2c66affSColin Finck static void teardown(void)
139c2c66affSColin Finck {
140c2c66affSColin Finck IBackgroundCopyFile_Release(test_file);
141c2c66affSColin Finck IEnumBackgroundCopyFiles_Release(test_enumFiles);
142c2c66affSColin Finck IBackgroundCopyJob_Release(test_job);
143c2c66affSColin Finck IBackgroundCopyManager_Release(test_manager);
144c2c66affSColin Finck }
145c2c66affSColin Finck
146c2c66affSColin Finck /* Test that the remote name is properly set */
test_GetRemoteName(void)147c2c66affSColin Finck static void test_GetRemoteName(void)
148c2c66affSColin Finck {
149c2c66affSColin Finck HRESULT hres;
150c2c66affSColin Finck LPWSTR name;
151c2c66affSColin Finck
152c2c66affSColin Finck hres = IBackgroundCopyFile_GetRemoteName(test_file, &name);
153c2c66affSColin Finck ok(hres == S_OK, "GetRemoteName failed: %08x\n", hres);
154c2c66affSColin Finck ok(lstrcmpW(name, test_remoteUrl) == 0, "Got incorrect remote name\n");
155c2c66affSColin Finck CoTaskMemFree(name);
156c2c66affSColin Finck }
157c2c66affSColin Finck
158c2c66affSColin Finck /* Test that the local name is properly set */
test_GetLocalName(void)159c2c66affSColin Finck static void test_GetLocalName(void)
160c2c66affSColin Finck {
161c2c66affSColin Finck HRESULT hres;
162c2c66affSColin Finck LPWSTR name;
163c2c66affSColin Finck
164c2c66affSColin Finck hres = IBackgroundCopyFile_GetLocalName(test_file, &name);
165c2c66affSColin Finck ok(hres == S_OK, "GetLocalName failed: %08x\n", hres);
166c2c66affSColin Finck ok(lstrcmpW(name, test_localFile) == 0, "Got incorrect local name\n");
167c2c66affSColin Finck CoTaskMemFree(name);
168c2c66affSColin Finck }
169c2c66affSColin Finck
170c2c66affSColin Finck /* Test getting the progress of a file*/
test_GetProgress_PreTransfer(void)171c2c66affSColin Finck static void test_GetProgress_PreTransfer(void)
172c2c66affSColin Finck {
173c2c66affSColin Finck HRESULT hres;
174c2c66affSColin Finck BG_FILE_PROGRESS progress;
175c2c66affSColin Finck
176c2c66affSColin Finck hres = IBackgroundCopyFile_GetProgress(test_file, &progress);
177c2c66affSColin Finck ok(hres == S_OK, "GetProgress failed: %08x\n", hres);
178684625e4SAmine Khaldi ok(progress.BytesTotal == BG_SIZE_UNKNOWN, "Got incorrect total size: %s\n",
179684625e4SAmine Khaldi wine_dbgstr_longlong(progress.BytesTotal));
180684625e4SAmine Khaldi ok(progress.BytesTransferred == 0, "Got incorrect number of transferred bytes: %s\n",
181684625e4SAmine Khaldi wine_dbgstr_longlong(progress.BytesTransferred));
182c2c66affSColin Finck ok(progress.Completed == FALSE, "Got incorrect completion status\n");
183c2c66affSColin Finck }
184c2c66affSColin Finck
185c2c66affSColin Finck typedef void (*test_t)(void);
186c2c66affSColin Finck
START_TEST(file)187c2c66affSColin Finck START_TEST(file)
188c2c66affSColin Finck {
189c2c66affSColin Finck static const test_t tests[] = {
190c2c66affSColin Finck test_GetRemoteName,
191c2c66affSColin Finck test_GetLocalName,
192c2c66affSColin Finck test_GetProgress_PreTransfer,
193c2c66affSColin Finck 0
194c2c66affSColin Finck };
195c2c66affSColin Finck const test_t *test;
196c2c66affSColin Finck int i;
197c2c66affSColin Finck
198c2c66affSColin Finck CoInitialize(NULL);
199c2c66affSColin Finck
200c2c66affSColin Finck if (FAILED(test_create_manager()))
201c2c66affSColin Finck {
202c2c66affSColin Finck CoUninitialize();
203c2c66affSColin Finck win_skip("Failed to create Manager instance, skipping tests\n");
204c2c66affSColin Finck return;
205c2c66affSColin Finck }
206c2c66affSColin Finck
207c2c66affSColin Finck for (test = tests, i = 0; *test; ++test, ++i)
208c2c66affSColin Finck {
209c2c66affSColin Finck /* Keep state separate between tests. */
210c2c66affSColin Finck if (!setup())
211c2c66affSColin Finck {
212c2c66affSColin Finck ok(0, "tests:%d: Unable to setup test\n", i);
213c2c66affSColin Finck break;
214c2c66affSColin Finck }
215c2c66affSColin Finck (*test)();
216c2c66affSColin Finck teardown();
217c2c66affSColin Finck }
218c2c66affSColin Finck CoUninitialize();
219c2c66affSColin Finck }
220