Release 1.5.29.
[wine] / dlls / qmgr / tests / 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 #define NUM_FILES 1
32 static const WCHAR test_remoteName[] = {'r','e','m','o','t','e', 0};
33 static const WCHAR test_localName[] = {'l','o','c','a','l', 0};
34 static WCHAR test_localFile[MAX_PATH];
35 static  WCHAR test_remoteUrl[MAX_PATH];
36 static const ULONG test_fileCount = NUM_FILES;
37 static const WCHAR test_displayName[] = {'T','e','s','t', 0};
38 static IBackgroundCopyJob *test_job;
39 static IBackgroundCopyManager *test_manager;
40 static IEnumBackgroundCopyFiles *test_enumFiles;
41 static IBackgroundCopyFile *test_file;
42
43 /* Helper function to add a file to a job.  The helper function takes base
44    file name and creates properly formed path and URL strings for creation of
45    the file. */
46 static HRESULT addFileHelper(IBackgroundCopyJob* job,
47         const WCHAR *localName, const WCHAR *remoteName)
48 {
49     DWORD urlSize;
50
51     GetCurrentDirectoryW(MAX_PATH, test_localFile);
52     PathAppendW(test_localFile, localName);
53     GetCurrentDirectoryW(MAX_PATH, test_remoteUrl);
54     PathAppendW(test_remoteUrl, remoteName);
55     urlSize = MAX_PATH;
56     UrlCreateFromPathW(test_remoteUrl, test_remoteUrl, &urlSize, 0);
57     UrlUnescapeW(test_remoteUrl, NULL, &urlSize, URL_UNESCAPE_INPLACE);
58     return IBackgroundCopyJob_AddFile(test_job, test_remoteUrl, test_localFile);
59 }
60
61 /* Generic test setup */
62 static BOOL setup(void)
63 {
64     HRESULT hres;
65     GUID test_jobId;
66
67     test_manager = NULL;
68     test_job = NULL;
69     memset(&test_jobId, 0, sizeof test_jobId);
70
71     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
72             CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
73             (void **) &test_manager);
74     if(hres != S_OK)
75         return FALSE;
76
77     hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
78             BG_JOB_TYPE_DOWNLOAD, &test_jobId, &test_job);
79     if(hres != S_OK)
80     {
81         IBackgroundCopyManager_Release(test_manager);
82         return FALSE;
83     }
84
85     if (addFileHelper(test_job, test_localName, test_remoteName) != S_OK
86         || IBackgroundCopyJob_EnumFiles(test_job, &test_enumFiles) != S_OK)
87     {
88         IBackgroundCopyJob_Release(test_job);
89         IBackgroundCopyManager_Release(test_manager);
90         return FALSE;
91     }
92
93     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &test_file, NULL);
94     if(hres != S_OK)
95     {
96         IEnumBackgroundCopyFiles_Release(test_enumFiles);
97         IBackgroundCopyJob_Release(test_job);
98         IBackgroundCopyManager_Release(test_manager);
99         return FALSE;
100     }
101
102     return TRUE;
103 }
104
105 /* Generic test cleanup */
106 static void teardown(void)
107 {
108     IBackgroundCopyFile_Release(test_file);
109     IEnumBackgroundCopyFiles_Release(test_enumFiles);
110     IBackgroundCopyJob_Release(test_job);
111     IBackgroundCopyManager_Release(test_manager);
112 }
113
114 /* Test that the remote name is properly set */
115 static void test_GetRemoteName(void)
116 {
117     HRESULT hres;
118     LPWSTR name;
119
120     hres = IBackgroundCopyFile_GetRemoteName(test_file, &name);
121     ok(hres == S_OK, "GetRemoteName failed: %08x\n", hres);
122     if(hres != S_OK)
123     {
124         skip("Unable to get remote name of test_file.\n");
125         return;
126     }
127     ok(lstrcmpW(name, test_remoteUrl) == 0, "Got incorrect remote name\n");
128     CoTaskMemFree(name);
129 }
130
131 /* Test that the local name is properly set */
132 static void test_GetLocalName(void)
133 {
134     HRESULT hres;
135     LPWSTR name;
136
137     hres = IBackgroundCopyFile_GetLocalName(test_file, &name);
138     ok(hres == S_OK, "GetLocalName failed: %08x\n", hres);
139     if(hres != S_OK)
140     {
141         skip("Unable to get local name of test_file.\n");
142         return;
143     }
144     ok(lstrcmpW(name, test_localFile) == 0, "Got incorrect local name\n");
145     CoTaskMemFree(name);
146 }
147
148 /* Test getting the progress of a file*/
149 static void test_GetProgress_PreTransfer(void)
150 {
151     HRESULT hres;
152     BG_FILE_PROGRESS progress;
153
154     hres = IBackgroundCopyFile_GetProgress(test_file, &progress);
155     ok(hres == S_OK, "GetProgress failed: %08x\n", hres);
156     if(hres != S_OK)
157     {
158         skip("Unable to get progress of test_file.\n");
159         return;
160     }
161     ok(progress.BytesTotal == BG_SIZE_UNKNOWN, "Got incorrect total size: %x%08x\n",
162        (DWORD)(progress.BytesTotal >> 32), (DWORD)progress.BytesTotal);
163     ok(progress.BytesTransferred == 0, "Got incorrect number of transferred bytes: %x%08x\n",
164        (DWORD)(progress.BytesTransferred >> 32), (DWORD)progress.BytesTransferred);
165     ok(progress.Completed == FALSE, "Got incorrect completion status\n");
166 }
167
168 typedef void (*test_t)(void);
169
170 START_TEST(file)
171 {
172     static const test_t tests[] = {
173         test_GetRemoteName,
174         test_GetLocalName,
175         test_GetProgress_PreTransfer,
176         0
177     };
178     const test_t *test;
179
180     CoInitialize(NULL);
181     for (test = tests; *test; ++test)
182     {
183         /* Keep state separate between tests. */
184         if (!setup())
185         {
186             skip("Unable to setup test\n");
187             break;
188         }
189         (*test)();
190         teardown();
191     }
192     CoUninitialize();
193 }