2 * Unit test of the SHFileOperation function.
4 * Copyright 2002 Andriy Palamarchuk
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wine/test.h"
26 CHAR CURR_DIR[MAX_PATH];
28 /* creates a file with the specified name for tests */
29 void createTestFile(CHAR *name)
35 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, (HANDLE)NULL);
36 sprintf(msg, "Failure to open file %s", name);
37 ok(file != INVALID_HANDLE_VALUE, msg);
38 WriteFile(file, name, strlen(name), &written, NULL);
39 WriteFile(file, "\n", strlen("\n"), &written, NULL);
43 BOOL file_exists(CHAR *name)
45 return GetFileAttributesA(name) != 0xFFFFFFFF;
48 /* initializes the tests */
49 void init_shfo_tests(void)
51 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
52 createTestFile(".\\test1.txt");
53 createTestFile(".\\test2.txt");
54 createTestFile(".\\test3.txt");
55 CreateDirectoryA(".\\test4.txt", NULL);
56 CreateDirectoryA(".\\testdir2", NULL);
59 /* cleans after tests */
60 void clean_after_shfo_tests(void)
62 DeleteFileA(".\\test1.txt");
63 DeleteFileA(".\\test2.txt");
64 DeleteFileA(".\\test3.txt");
65 DeleteFileA(".\\test4.txt\\test1.txt");
66 DeleteFileA(".\\test4.txt\\test2.txt");
67 DeleteFileA(".\\test4.txt\\test3.txt");
68 RemoveDirectoryA(".\\test4.txt");
69 DeleteFileA(".\\testdir2\\test1.txt");
70 DeleteFileA(".\\testdir2\\test2.txt");
71 DeleteFileA(".\\testdir2\\test3.txt");
72 DeleteFileA(".\\testdir2\\test4.txt\\test1.txt");
73 RemoveDirectoryA(".\\testdir2\\test4.txt");
74 RemoveDirectoryA(".\\testdir2");
78 puts into the specified buffer file names with current directory.
79 files - string with file names, separated by null characters. Ends on a double
82 void set_curr_dir_path(CHAR *buf, CHAR* files)
87 strcpy(buf, CURR_DIR);
92 buf += strlen(buf) + 1;
93 files += strlen(files) + 1;
99 /* tests the FO_DELETE action */
100 void test_delete(void)
102 SHFILEOPSTRUCTA shfo;
105 sprintf(buf, "%s\\%s", CURR_DIR, "test?.txt");
106 buf[strlen(buf) + 1] = '\0';
108 shfo.hwnd = (HANDLE)NULL;
109 shfo.wFunc = FO_DELETE;
112 shfo.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
113 shfo.hNameMappings = NULL;
114 shfo.lpszProgressTitle = NULL;
116 ok(!SHFileOperationA(&shfo), "Deletion was successful");
117 ok(file_exists(".\\test4.txt"), "Directory should not be removed");
118 ok(!file_exists(".\\test1.txt"), "File should be removed");
120 ok(!SHFileOperationA(&shfo), "Directory exists, but is not removed");
121 ok(file_exists(".\\test4.txt"), "Directory should not be removed");
123 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
125 ok(!SHFileOperationA(&shfo), "Directory removed");
126 ok(!file_exists(".\\test4.txt"), "Directory should be removed");
128 ok(!SHFileOperationA(&shfo), "The requested file does not exist");
131 sprintf(buf, "%s\\%s", CURR_DIR, "test4.txt");
132 buf[strlen(buf) + 1] = '\0';
133 ok(MoveFileA(".\\test1.txt", ".\\test4.txt\\test1.txt"), "Fill the subdirectory");
134 ok(!SHFileOperationA(&shfo), "Directory removed");
135 ok(!file_exists(".\\test4.txt"), "Directory is removed");
138 shfo.pFrom = ".\\test1.txt\0.\\test4.txt\0";
139 ok(!SHFileOperationA(&shfo), "Directory and a file removed");
140 ok(!file_exists(".\\test1.txt"), "The file should be removed");
141 ok(!file_exists(".\\test4.txt"), "Directory should be removed");
142 ok(file_exists(".\\test2.txt"), "This file should not be removed");
145 /* tests the FO_RENAME action */
148 SHFILEOPSTRUCTA shfo, shfo2;
152 shfo.hwnd = (HANDLE)NULL;
153 shfo.wFunc = FO_RENAME;
156 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
157 shfo.hNameMappings = NULL;
158 shfo.lpszProgressTitle = NULL;
160 set_curr_dir_path(from, "test1.txt\0");
161 set_curr_dir_path(to, "test4.txt\0");
162 ok(SHFileOperationA(&shfo), "File is not renamed moving to other directory "
163 "when specifying directory name only");
164 ok(file_exists(".\\test1.txt"), "The file is not removed");
166 set_curr_dir_path(from, "test3.txt\0");
167 set_curr_dir_path(to, "test4.txt\\test1.txt\0");
168 ok(!SHFileOperationA(&shfo), "File is renamed moving to other directory");
169 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is renamed");
171 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
172 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
173 ok(SHFileOperationA(&shfo), "Can't rename many files");
174 ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified ");
176 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
177 shfo2.fFlags |= FOF_MULTIDESTFILES;
179 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
180 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
181 ok(SHFileOperationA(&shfo2), "Can't rename many files");
182 ok(file_exists(".\\test1.txt"), "The file is not renamed - many files are specified ");
184 set_curr_dir_path(from, "test1.txt\0");
185 set_curr_dir_path(to, "test6.txt\0");
186 ok(!SHFileOperationA(&shfo), "Rename file");
187 ok(!file_exists(".\\test1.txt"), "The file is renamed");
188 ok(file_exists(".\\test6.txt"), "The file is renamed ");
189 set_curr_dir_path(from, "test6.txt\0");
190 set_curr_dir_path(to, "test1.txt\0");
191 ok(!SHFileOperationA(&shfo), "Rename file back");
193 set_curr_dir_path(from, "test4.txt\0");
194 set_curr_dir_path(to, "test6.txt\0");
195 ok(!SHFileOperationA(&shfo), "Rename dir");
196 ok(!file_exists(".\\test4.txt"), "The dir is renamed");
197 ok(file_exists(".\\test6.txt"), "The dir is renamed ");
198 set_curr_dir_path(from, "test6.txt\0");
199 set_curr_dir_path(to, "test4.txt\0");
200 ok(!SHFileOperationA(&shfo), "Rename dir back");
203 /* tests the FO_COPY action */
206 SHFILEOPSTRUCTA shfo, shfo2;
209 FILEOP_FLAGS tmp_flags;
211 shfo.hwnd = (HANDLE)NULL;
212 shfo.wFunc = FO_COPY;
215 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
216 shfo.hNameMappings = NULL;
217 shfo.lpszProgressTitle = NULL;
219 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
220 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
221 ok(SHFileOperationA(&shfo), "Can't copy many files");
222 ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
223 "specified as a target");
225 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
226 shfo2.fFlags |= FOF_MULTIDESTFILES;
228 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
229 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
230 ok(!SHFileOperationA(&shfo2), "Can't copy many files");
231 ok(file_exists(".\\test6.txt"), "The file is copied - many files are "
232 "specified as a target");
233 DeleteFileA(".\\test6.txt");
234 DeleteFileA(".\\test7.txt");
235 RemoveDirectoryA(".\\test8.txt");
237 /* number of sources do not correspond to number of targets */
238 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
239 set_curr_dir_path(to, "test6.txt\0test7.txt\0");
240 ok(SHFileOperationA(&shfo2), "Can't copy many files");
241 ok(!file_exists(".\\test6.txt"), "The file is not copied - many files are "
242 "specified as a target");
244 set_curr_dir_path(from, "test1.txt\0");
245 set_curr_dir_path(to, "test4.txt\0");
246 ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are copied recursively");
247 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is copied");
249 set_curr_dir_path(from, "test?.txt\0");
250 set_curr_dir_path(to, "testdir2\0");
251 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
252 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet");
253 ok(!SHFileOperationA(&shfo), "Files and directories are copied to directory ");
254 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
255 ok(file_exists(".\\testdir2\\test4.txt"), "The directory is copied");
256 ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is copied");
257 clean_after_shfo_tests();
260 shfo.fFlags |= FOF_FILESONLY;
261 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
262 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not copied yet");
263 ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
264 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
265 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is copied");
266 clean_after_shfo_tests();
269 set_curr_dir_path(from, "test1.txt\0test2.txt\0");
270 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
271 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet");
272 ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
273 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
274 ok(file_exists(".\\testdir2\\test2.txt"), "The file is copied");
275 clean_after_shfo_tests();
278 tmp_flags = shfo.fFlags;
279 set_curr_dir_path(from, "test1.txt\0test10.txt\0test2.txt\0");
280 ok(!file_exists(".\\testdir2\\test1.txt"), "The file is not copied yet");
281 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not copied yet");
282 ok(!SHFileOperationA(&shfo), "Files are copied to other directory ");
283 ok(file_exists(".\\testdir2\\test1.txt"), "The file is copied");
284 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is copied");
285 shfo.fFlags = tmp_flags;
288 /* tests the FO_MOVE action */
291 SHFILEOPSTRUCTA shfo, shfo2;
295 shfo.hwnd = (HANDLE)NULL;
296 shfo.wFunc = FO_MOVE;
299 shfo.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
300 shfo.hNameMappings = NULL;
301 shfo.lpszProgressTitle = NULL;
303 set_curr_dir_path(from, "test1.txt\0");
304 set_curr_dir_path(to, "test4.txt\0");
305 ok(!SHFileOperationA(&shfo), "Prepare test to check how directories are moved recursively");
306 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved");
308 set_curr_dir_path(from, "test?.txt\0");
309 set_curr_dir_path(to, "testdir2\0");
310 ok(!file_exists(".\\testdir2\\test2.txt"), "The file is not moved yet");
311 ok(!file_exists(".\\testdir2\\test4.txt"), "The directory is not moved yet");
312 ok(!SHFileOperationA(&shfo), "Files and directories are moved to directory ");
313 ok(file_exists(".\\testdir2\\test2.txt"), "The file is moved");
314 ok(file_exists(".\\testdir2\\test4.txt"), "The directory is moved");
315 ok(file_exists(".\\testdir2\\test4.txt\\test1.txt"), "The file in subdirectory is moved");
317 clean_after_shfo_tests();
320 memcpy(&shfo2, &shfo, sizeof(SHFILEOPSTRUCTA));
321 shfo2.fFlags |= FOF_MULTIDESTFILES;
323 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
324 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
325 ok(!SHFileOperationA(&shfo2), "Move many files");
326 ok(file_exists(".\\test6.txt"), "The file is moved - many files are "
327 "specified as a target");
328 DeleteFileA(".\\test6.txt");
329 DeleteFileA(".\\test7.txt");
330 RemoveDirectoryA(".\\test8.txt");
334 /* number of sources do not correspond to number of targets */
335 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
336 set_curr_dir_path(to, "test6.txt\0test7.txt\0");
337 ok(SHFileOperationA(&shfo2), "Can't move many files");
338 ok(!file_exists(".\\test6.txt"), "The file is not moved - many files are "
339 "specified as a target");
343 set_curr_dir_path(from, "test3.txt\0");
344 set_curr_dir_path(to, "test4.txt\\test1.txt\0");
345 ok(!SHFileOperationA(&shfo), "File is moved moving to other directory");
346 ok(file_exists(".\\test4.txt\\test1.txt"), "The file is moved");
348 set_curr_dir_path(from, "test1.txt\0test2.txt\0test4.txt\0");
349 set_curr_dir_path(to, "test6.txt\0test7.txt\0test8.txt\0");
350 ok(SHFileOperationA(&shfo), "Can not move many files");
351 ok(file_exists(".\\test1.txt"), "The file is not moved. Many files are specified ");
352 ok(file_exists(".\\test4.txt"), "The directory not is moved. Many files are specified ");
354 set_curr_dir_path(from, "test1.txt\0");
355 set_curr_dir_path(to, "test6.txt\0");
356 ok(!SHFileOperationA(&shfo), "Move file");
357 ok(!file_exists(".\\test1.txt"), "The file is moved");
358 ok(file_exists(".\\test6.txt"), "The file is moved ");
359 set_curr_dir_path(from, "test6.txt\0");
360 set_curr_dir_path(to, "test1.txt\0");
361 ok(!SHFileOperationA(&shfo), "Move file back");
363 set_curr_dir_path(from, "test4.txt\0");
364 set_curr_dir_path(to, "test6.txt\0");
365 ok(!SHFileOperationA(&shfo), "Move dir");
366 ok(!file_exists(".\\test4.txt"), "The dir is moved");
367 ok(file_exists(".\\test6.txt"), "The dir is moved ");
368 set_curr_dir_path(from, "test6.txt\0");
369 set_curr_dir_path(to, "test4.txt\0");
370 ok(!SHFileOperationA(&shfo), "Move dir back");
373 START_TEST(shlfileop)
375 clean_after_shfo_tests();
379 clean_after_shfo_tests();
383 clean_after_shfo_tests();
387 clean_after_shfo_tests();
391 clean_after_shfo_tests();