blob: 0a71276ce80846b3f9bce0db1b234a46a3202960 [file] [log] [blame]
initial.commitd7cae122008-07-26 21:49:381// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
[email protected]8541bb82008-08-15 17:45:1330#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3831#include <windows.h>
initial.commitd7cae122008-07-26 21:49:3832#include <shellapi.h>
33#include <shlobj.h>
[email protected]37088fef2008-08-15 17:32:1034#endif
initial.commitd7cae122008-07-26 21:49:3835
36#include <fstream>
37#include <iostream>
[email protected]37088fef2008-08-15 17:32:1038#include <set>
initial.commitd7cae122008-07-26 21:49:3839
40#include "base/base_paths.h"
41#include "base/file_util.h"
42#include "base/logging.h"
43#include "base/path_service.h"
44#include "base/string_util.h"
45#include "testing/gtest/include/gtest/gtest.h"
46
47namespace {
48
49class FileUtilTest : public testing::Test {
50 protected:
51 virtual void SetUp() {
52 // Name a subdirectory of the temp directory.
53 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
54 file_util::AppendToPath(&test_dir_, L"FileUtilTest");
55
56 // Create a fresh, empty copy of this directory.
57 file_util::Delete(test_dir_, true);
[email protected]37088fef2008-08-15 17:32:1058 file_util::CreateDirectory(test_dir_.c_str());
initial.commitd7cae122008-07-26 21:49:3859 }
60 virtual void TearDown() {
61 // Clean up test directory
[email protected]37088fef2008-08-15 17:32:1062 ASSERT_TRUE(file_util::Delete(test_dir_, true));
initial.commitd7cae122008-07-26 21:49:3863 ASSERT_FALSE(file_util::PathExists(test_dir_));
64 }
65
66 // the path to temporary directory used to contain the test operations
67 std::wstring test_dir_;
68};
69
70// Collects all the results from the given file enumerator, and provides an
71// interface to query whether a given file is present.
72class FindResultCollector {
73 public:
74 FindResultCollector(file_util::FileEnumerator& enumerator) {
75 std::wstring cur_file;
76 while (!(cur_file = enumerator.Next()).empty()) {
77 // The file should not be returned twice.
78 EXPECT_TRUE(files_.end() == files_.find(cur_file))
79 << "Same file returned twice";
80
81 // Save for later.
82 files_.insert(cur_file);
83 }
84 }
85
86 // Returns true if the enumerator found the file.
87 bool HasFile(const std::wstring& file) const {
88 return files_.find(file) != files_.end();
89 }
[email protected]37088fef2008-08-15 17:32:1090
91 int size() {
92 return files_.size();
93 }
initial.commitd7cae122008-07-26 21:49:3894
95 private:
96 std::set<std::wstring> files_;
97};
98
99// Simple function to dump some text into a new file.
100void CreateTextFile(const std::wstring& filename,
101 const std::wstring& contents) {
102 std::ofstream file;
[email protected]37088fef2008-08-15 17:32:10103 file.open(WideToUTF8(filename).c_str());
initial.commitd7cae122008-07-26 21:49:38104 ASSERT_TRUE(file.is_open());
105 file << contents;
106 file.close();
107}
108
109// Simple function to take out some text from a file.
110std::wstring ReadTextFile(const std::wstring& filename) {
[email protected]37088fef2008-08-15 17:32:10111 wchar_t contents[64];
initial.commitd7cae122008-07-26 21:49:38112 std::wifstream file;
[email protected]37088fef2008-08-15 17:32:10113 file.open(WideToUTF8(filename).c_str());
initial.commitd7cae122008-07-26 21:49:38114 EXPECT_TRUE(file.is_open());
115 file.getline(contents, 64);
116 file.close();
117 return std::wstring(contents);
118}
119
[email protected]8541bb82008-08-15 17:45:13120#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38121uint64 FileTimeAsUint64(const FILETIME& ft) {
122 ULARGE_INTEGER u;
123 u.LowPart = ft.dwLowDateTime;
124 u.HighPart = ft.dwHighDateTime;
125 return u.QuadPart;
126}
[email protected]37088fef2008-08-15 17:32:10127#endif
initial.commitd7cae122008-07-26 21:49:38128
129const struct append_case {
130 const wchar_t* path;
131 const wchar_t* ending;
132 const wchar_t* result;
133} append_cases[] = {
[email protected]8541bb82008-08-15 17:45:13134#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38135 {L"c:\\colon\\backslash", L"path", L"c:\\colon\\backslash\\path"},
136 {L"c:\\colon\\backslash\\", L"path", L"c:\\colon\\backslash\\path"},
137 {L"c:\\colon\\backslash\\\\", L"path", L"c:\\colon\\backslash\\\\path"},
138 {L"c:\\colon\\backslash\\", L"", L"c:\\colon\\backslash\\"},
139 {L"c:\\colon\\backslash", L"", L"c:\\colon\\backslash\\"},
140 {L"", L"path", L"\\path"},
141 {L"", L"", L"\\"},
[email protected]37088fef2008-08-15 17:32:10142#elif defined(OS_POSIX)
143 {L"/foo/bar", L"path", L"/foo/bar/path"},
144 {L"/foo/bar/", L"path", L"/foo/bar/path"},
145 {L"/foo/bar//", L"path", L"/foo/bar//path"},
146 {L"/foo/bar/", L"", L"/foo/bar/"},
147 {L"/foo/bar", L"", L"/foo/bar/"},
148 {L"", L"path", L"/path"},
149 {L"", L"", L"/"},
150#endif
initial.commitd7cae122008-07-26 21:49:38151};
152
153} // namespace
154
155TEST_F(FileUtilTest, AppendToPath) {
[email protected]37088fef2008-08-15 17:32:10156 for (unsigned int i = 0; i < arraysize(append_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38157 const append_case& value = append_cases[i];
158 std::wstring result = value.path;
159 file_util::AppendToPath(&result, value.ending);
160 EXPECT_EQ(value.result, result);
161 }
162
163#ifdef NDEBUG
164 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
165#endif
166}
167
168static const struct InsertBeforeExtensionCase {
169 std::wstring path;
170 std::wstring suffix;
171 std::wstring result;
172} kInsertBeforeExtension[] = {
173 {L"", L"", L""},
174 {L"", L"txt", L"txt"},
175 {L".", L"txt", L"txt."},
176 {L".", L"", L"."},
177 {L"foo.dll", L"txt", L"footxt.dll"},
178 {L"foo.dll", L".txt", L"foo.txt.dll"},
179 {L"foo", L"txt", L"footxt"},
180 {L"foo", L".txt", L"foo.txt"},
181 {L"foo.baz.dll", L"txt", L"foo.baztxt.dll"},
182 {L"foo.baz.dll", L".txt", L"foo.baz.txt.dll"},
183 {L"foo.dll", L"", L"foo.dll"},
184 {L"foo.dll", L".", L"foo..dll"},
185 {L"foo", L"", L"foo"},
186 {L"foo", L".", L"foo."},
187 {L"foo.baz.dll", L"", L"foo.baz.dll"},
188 {L"foo.baz.dll", L".", L"foo.baz..dll"},
[email protected]8541bb82008-08-15 17:45:13189#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38190 {L"\\", L"", L"\\"},
191 {L"\\", L"txt", L"\\txt"},
192 {L"\\.", L"txt", L"\\txt."},
193 {L"\\.", L"", L"\\."},
194 {L"C:\\bar\\foo.dll", L"txt", L"C:\\bar\\footxt.dll"},
195 {L"C:\\bar.baz\\foodll", L"txt", L"C:\\bar.baz\\foodlltxt"},
196 {L"C:\\bar.baz\\foo.dll", L"txt", L"C:\\bar.baz\\footxt.dll"},
197 {L"C:\\bar.baz\\foo.dll.exe", L"txt", L"C:\\bar.baz\\foo.dlltxt.exe"},
198 {L"C:\\bar.baz\\foo", L"", L"C:\\bar.baz\\foo"},
199 {L"C:\\bar.baz\\foo.exe", L"", L"C:\\bar.baz\\foo.exe"},
200 {L"C:\\bar.baz\\foo.dll.exe", L"", L"C:\\bar.baz\\foo.dll.exe"},
201 {L"C:\\bar\\baz\\foo.exe", L" (1)", L"C:\\bar\\baz\\foo (1).exe"},
[email protected]37088fef2008-08-15 17:32:10202#elif defined(OS_POSIX)
203 {L"/", L"", L"/"},
204 {L"/", L"txt", L"/txt"},
205 {L"/.", L"txt", L"/txt."},
206 {L"/.", L"", L"/."},
207 {L"/bar/foo.dll", L"txt", L"/bar/footxt.dll"},
208 {L"/bar.baz/foodll", L"txt", L"/bar.baz/foodlltxt"},
209 {L"/bar.baz/foo.dll", L"txt", L"/bar.baz/footxt.dll"},
210 {L"/bar.baz/foo.dll.exe", L"txt", L"/bar.baz/foo.dlltxt.exe"},
211 {L"/bar.baz/foo", L"", L"/bar.baz/foo"},
212 {L"/bar.baz/foo.exe", L"", L"/bar.baz/foo.exe"},
213 {L"/bar.baz/foo.dll.exe", L"", L"/bar.baz/foo.dll.exe"},
214 {L"/bar/baz/foo.exe", L" (1)", L"/bar/baz/foo (1).exe"},
215#endif
initial.commitd7cae122008-07-26 21:49:38216};
217
218TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
[email protected]37088fef2008-08-15 17:32:10219 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
initial.commitd7cae122008-07-26 21:49:38220 std::wstring path(kInsertBeforeExtension[i].path);
221 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
222 EXPECT_EQ(path, kInsertBeforeExtension[i].result);
223 }
224}
225
226static const struct filename_case {
227 const wchar_t* path;
228 const wchar_t* filename;
229} filename_cases[] = {
[email protected]8541bb82008-08-15 17:45:13230#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38231 {L"c:\\colon\\backslash", L"backslash"},
232 {L"c:\\colon\\backslash\\", L""},
233 {L"\\\\filename.exe", L"filename.exe"},
234 {L"filename.exe", L"filename.exe"},
235 {L"", L""},
236 {L"\\\\\\", L""},
237 {L"c:/colon/backslash", L"backslash"},
238 {L"c:/colon/backslash/", L""},
239 {L"//////", L""},
240 {L"///filename.exe", L"filename.exe"},
[email protected]37088fef2008-08-15 17:32:10241#elif defined(OS_POSIX)
242 {L"/foo/bar", L"bar"},
243 {L"/foo/bar/", L""},
244 {L"/filename.exe", L"filename.exe"},
245 {L"filename.exe", L"filename.exe"},
246 {L"", L""},
247 {L"/", L""},
248#endif
initial.commitd7cae122008-07-26 21:49:38249};
250
251TEST_F(FileUtilTest, GetFilenameFromPath) {
[email protected]37088fef2008-08-15 17:32:10252 for (unsigned int i = 0; i < arraysize(filename_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38253 const filename_case& value = filename_cases[i];
254 std::wstring result = file_util::GetFilenameFromPath(value.path);
255 EXPECT_EQ(value.filename, result);
256 }
257}
258
259// Test finding the file type from a path name
260static const struct extension_case {
261 const wchar_t* path;
262 const wchar_t* extension;
263} extension_cases[] = {
[email protected]8541bb82008-08-15 17:45:13264#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38265 {L"C:\\colon\\backslash\\filename.extension", L"extension"},
266 {L"C:\\colon\\backslash\\filename.", L""},
267 {L"C:\\colon\\backslash\\filename", L""},
268 {L"C:\\colon\\backslash\\", L""},
269 {L"C:\\colon\\backslash.\\", L""},
270 {L"C:\\colon\\backslash\filename.extension.extension2", L"extension2"},
[email protected]37088fef2008-08-15 17:32:10271#elif defined(OS_POSIX)
272 {L"/foo/bar/filename.extension", L"extension"},
273 {L"/foo/bar/filename.", L""},
274 {L"/foo/bar/filename", L""},
275 {L"/foo/bar/", L""},
276 {L"/foo/bar./", L""},
277 {L"/foo/bar/filename.extension.extension2", L"extension2"},
278 {L".", L""},
279 {L"..", L""},
280 {L"./foo", L""},
281 {L"./foo.extension", L"extension"},
282 {L"/foo.extension1/bar.extension2", L"extension2"},
283#endif
initial.commitd7cae122008-07-26 21:49:38284};
285
286TEST_F(FileUtilTest, GetFileExtensionFromPath) {
[email protected]37088fef2008-08-15 17:32:10287 for (unsigned int i = 0; i < arraysize(extension_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38288 const extension_case& ext = extension_cases[i];
289 const std::wstring fext = file_util::GetFileExtensionFromPath(ext.path);
290 EXPECT_EQ(ext.extension, fext);
291 }
292}
293
294// Test finding the directory component of a path
295static const struct dir_case {
296 const wchar_t* full_path;
297 const wchar_t* directory;
298} dir_cases[] = {
[email protected]8541bb82008-08-15 17:45:13299#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38300 {L"C:\\WINDOWS\\system32\\gdi32.dll", L"C:\\WINDOWS\\system32"},
301 {L"C:\\WINDOWS\\system32\\not_exist_thx_1138", L"C:\\WINDOWS\\system32"},
302 {L"C:\\WINDOWS\\system32\\", L"C:\\WINDOWS\\system32"},
303 {L"C:\\WINDOWS\\system32\\\\", L"C:\\WINDOWS\\system32"},
304 {L"C:\\WINDOWS\\system32", L"C:\\WINDOWS"},
305 {L"C:\\WINDOWS\\system32.\\", L"C:\\WINDOWS\\system32."},
306 {L"C:\\", L"C:"},
[email protected]37088fef2008-08-15 17:32:10307#elif defined(OS_POSIX)
308 {L"/foo/bar/gdi32.dll", L"/foo/bar"},
309 {L"/foo/bar/not_exist_thx_1138", L"/foo/bar"},
310 {L"/foo/bar/", L"/foo/bar"},
311 {L"/foo/bar//", L"/foo/bar"},
312 {L"/foo/bar", L"/foo"},
313 {L"/foo/bar./", L"/foo/bar."},
314 {L"/", L"/"},
315 {L".", L"."},
316 {L"..", L"."}, // yes, ".." technically lives in "."
317#endif
initial.commitd7cae122008-07-26 21:49:38318};
319
320TEST_F(FileUtilTest, GetDirectoryFromPath) {
[email protected]37088fef2008-08-15 17:32:10321 for (unsigned int i = 0; i < arraysize(dir_cases); ++i) {
initial.commitd7cae122008-07-26 21:49:38322 const dir_case& dir = dir_cases[i];
323 const std::wstring parent =
324 file_util::GetDirectoryFromPath(dir.full_path);
325 EXPECT_EQ(dir.directory, parent);
326 }
327}
328
[email protected]37088fef2008-08-15 17:32:10329// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:13330#if defined OS_WIN
initial.commitd7cae122008-07-26 21:49:38331TEST_F(FileUtilTest, CountFilesCreatedAfter) {
332 // Create old file (that we don't want to count)
333 std::wstring old_file_name = test_dir_;
334 file_util::AppendToPath(&old_file_name, L"Old File.txt");
335 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
336
337 // Age to perfection
338 Sleep(100);
339
340 // Establish our cutoff time
341 FILETIME test_start_time;
342 GetSystemTimeAsFileTime(&test_start_time);
343 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, test_start_time));
344
345 // Create a new file (that we do want to count)
346 std::wstring new_file_name = test_dir_;
347 file_util::AppendToPath(&new_file_name, L"New File.txt");
348 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
349
350 // We should see only the new file.
351 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, test_start_time));
352
353 // Delete new file, we should see no files after cutoff now
354 EXPECT_TRUE(file_util::Delete(new_file_name, false));
355 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, test_start_time));
356}
[email protected]37088fef2008-08-15 17:32:10357#endif
initial.commitd7cae122008-07-26 21:49:38358
359// Tests that the Delete function works as expected, especially
360// the recursion flag. Also coincidentally tests PathExists.
361TEST_F(FileUtilTest, Delete) {
362 // Create a file
363 std::wstring file_name = test_dir_;
364 file_util::AppendToPath(&file_name, L"Test File.txt");
365 CreateTextFile(file_name, L"I'm cannon fodder.");
366
367 ASSERT_TRUE(file_util::PathExists(file_name));
368
369 std::wstring subdir_path = test_dir_;
370 file_util::AppendToPath(&subdir_path, L"Subdirectory");
[email protected]37088fef2008-08-15 17:32:10371 file_util::CreateDirectory(subdir_path.c_str());
initial.commitd7cae122008-07-26 21:49:38372
373 ASSERT_TRUE(file_util::PathExists(subdir_path));
374
375 std::wstring directory_contents = test_dir_;
[email protected]8541bb82008-08-15 17:45:13376#if defined(OS_WIN)
[email protected]37088fef2008-08-15 17:32:10377 // TODO(erikkay): see if anyone's actually using this feature of the API
initial.commitd7cae122008-07-26 21:49:38378 file_util::AppendToPath(&directory_contents, L"*");
379
380 // Delete non-recursively and check that only the file is deleted
381 ASSERT_TRUE(file_util::Delete(directory_contents, false));
[email protected]37088fef2008-08-15 17:32:10382 EXPECT_FALSE(file_util::PathExists(file_name));
383 EXPECT_TRUE(file_util::PathExists(subdir_path));
384#endif
initial.commitd7cae122008-07-26 21:49:38385
386 // Delete recursively and make sure all contents are deleted
387 ASSERT_TRUE(file_util::Delete(directory_contents, true));
[email protected]37088fef2008-08-15 17:32:10388 EXPECT_FALSE(file_util::PathExists(file_name));
389 EXPECT_FALSE(file_util::PathExists(subdir_path));
initial.commitd7cae122008-07-26 21:49:38390}
391
392TEST_F(FileUtilTest, Move) {
393 // Create a directory
394 std::wstring dir_name_from(test_dir_);
395 file_util::AppendToPath(&dir_name_from, L"Move_From_Subdir");
[email protected]37088fef2008-08-15 17:32:10396 file_util::CreateDirectory(dir_name_from.c_str());
initial.commitd7cae122008-07-26 21:49:38397 ASSERT_TRUE(file_util::PathExists(dir_name_from));
398
399 // Create a file under the directory
400 std::wstring file_name_from(dir_name_from);
401 file_util::AppendToPath(&file_name_from, L"Move_Test_File.txt");
402 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
403 ASSERT_TRUE(file_util::PathExists(file_name_from));
404
405 // Move the directory
406 std::wstring dir_name_to(test_dir_);
407 file_util::AppendToPath(&dir_name_to, L"Move_To_Subdir");
408 std::wstring file_name_to(dir_name_to);
409 file_util::AppendToPath(&file_name_to, L"Move_Test_File.txt");
410
411 ASSERT_FALSE(file_util::PathExists(dir_name_to));
412
413 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
414
415 // Check everything has been moved.
416 EXPECT_FALSE(file_util::PathExists(dir_name_from));
417 EXPECT_FALSE(file_util::PathExists(file_name_from));
418 EXPECT_TRUE(file_util::PathExists(dir_name_to));
419 EXPECT_TRUE(file_util::PathExists(file_name_to));
420}
421
[email protected]37088fef2008-08-15 17:32:10422// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:13423#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38424TEST_F(FileUtilTest, CopyDirectoryRecursively) {
425 // Create a directory.
426 std::wstring dir_name_from(test_dir_);
427 file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir");
[email protected]37088fef2008-08-15 17:32:10428 file_util::CreateDirectory(dir_name_from.c_str());
initial.commitd7cae122008-07-26 21:49:38429 ASSERT_TRUE(file_util::PathExists(dir_name_from));
430
431 // Create a file under the directory.
432 std::wstring file_name_from(dir_name_from);
433 file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt");
434 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
435 ASSERT_TRUE(file_util::PathExists(file_name_from));
436
437 // Create a subdirectory.
438 std::wstring subdir_name_from(dir_name_from);
439 file_util::AppendToPath(&subdir_name_from, L"Subdir");
[email protected]37088fef2008-08-15 17:32:10440 file_util::CreateDirectory(subdir_name_from.c_str());
initial.commitd7cae122008-07-26 21:49:38441 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
442
443 // Create a file under the subdirectory.
444 std::wstring file_name2_from(subdir_name_from);
445 file_util::AppendToPath(&file_name2_from, L"Copy_Test_File.txt");
446 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
447 ASSERT_TRUE(file_util::PathExists(file_name2_from));
448
449 // Copy the directory recursively.
450 std::wstring dir_name_to(test_dir_);
451 file_util::AppendToPath(&dir_name_to, L"Copy_To_Subdir");
452 std::wstring file_name_to(dir_name_to);
453 file_util::AppendToPath(&file_name_to, L"Copy_Test_File.txt");
454 std::wstring subdir_name_to(dir_name_to);
455 file_util::AppendToPath(&subdir_name_to, L"Subdir");
456 std::wstring file_name2_to(subdir_name_to);
457 file_util::AppendToPath(&file_name2_to, L"Copy_Test_File.txt");
458
459 ASSERT_FALSE(file_util::PathExists(dir_name_to));
460
461 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
462
463 // Check everything has been copied.
464 EXPECT_TRUE(file_util::PathExists(dir_name_from));
465 EXPECT_TRUE(file_util::PathExists(file_name_from));
466 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
467 EXPECT_TRUE(file_util::PathExists(file_name2_from));
468 EXPECT_TRUE(file_util::PathExists(dir_name_to));
469 EXPECT_TRUE(file_util::PathExists(file_name_to));
470 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
471 EXPECT_TRUE(file_util::PathExists(file_name2_to));
472}
473
474TEST_F(FileUtilTest, CopyDirectory) {
475 // Create a directory.
476 std::wstring dir_name_from(test_dir_);
477 file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir");
[email protected]37088fef2008-08-15 17:32:10478 file_util::CreateDirectory(dir_name_from.c_str());
initial.commitd7cae122008-07-26 21:49:38479 ASSERT_TRUE(file_util::PathExists(dir_name_from));
480
481 // Create a file under the directory.
482 std::wstring file_name_from(dir_name_from);
483 file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt");
484 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
485 ASSERT_TRUE(file_util::PathExists(file_name_from));
486
487 // Create a subdirectory.
488 std::wstring subdir_name_from(dir_name_from);
489 file_util::AppendToPath(&subdir_name_from, L"Subdir");
[email protected]37088fef2008-08-15 17:32:10490 file_util::CreateDirectory(subdir_name_from.c_str());
initial.commitd7cae122008-07-26 21:49:38491 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
492
493 // Create a file under the subdirectory.
494 std::wstring file_name2_from(subdir_name_from);
495 file_util::AppendToPath(&file_name2_from, L"Copy_Test_File.txt");
496 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
497 ASSERT_TRUE(file_util::PathExists(file_name2_from));
498
499 // Copy the directory not recursively.
500 std::wstring dir_name_to(test_dir_);
501 file_util::AppendToPath(&dir_name_to, L"Copy_To_Subdir");
502 std::wstring file_name_to(dir_name_to);
503 file_util::AppendToPath(&file_name_to, L"Copy_Test_File.txt");
504 std::wstring subdir_name_to(dir_name_to);
505 file_util::AppendToPath(&subdir_name_to, L"Subdir");
506
507 ASSERT_FALSE(file_util::PathExists(dir_name_to));
508
509 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
510
511 // Check everything has been copied.
512 EXPECT_TRUE(file_util::PathExists(dir_name_from));
513 EXPECT_TRUE(file_util::PathExists(file_name_from));
514 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
515 EXPECT_TRUE(file_util::PathExists(file_name2_from));
516 EXPECT_TRUE(file_util::PathExists(dir_name_to));
517 EXPECT_TRUE(file_util::PathExists(file_name_to));
518 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
519}
[email protected]37088fef2008-08-15 17:32:10520#endif
initial.commitd7cae122008-07-26 21:49:38521
522TEST_F(FileUtilTest, CopyFile) {
523 // Create a directory
524 std::wstring dir_name_from(test_dir_);
525 file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir");
[email protected]37088fef2008-08-15 17:32:10526 file_util::CreateDirectory(dir_name_from.c_str());
initial.commitd7cae122008-07-26 21:49:38527 ASSERT_TRUE(file_util::PathExists(dir_name_from));
528
529 // Create a file under the directory
530 std::wstring file_name_from(dir_name_from);
531 file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt");
532 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
533 CreateTextFile(file_name_from, file_contents);
534 ASSERT_TRUE(file_util::PathExists(file_name_from));
535
536 // Copy the file.
537 std::wstring dest_file(dir_name_from);
538 file_util::AppendToPath(&dest_file, L"DestFile.txt");
539 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
[email protected]37088fef2008-08-15 17:32:10540
541 // Copy the file to another location using '..' in the path.
542 std::wstring dest_file2(dir_name_from);
543 file_util::AppendToPath(&dest_file2, L"..");
544 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
545 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2));
546 std::wstring dest_file2_test(dir_name_from);
547 file_util::UpOneDirectory(&dest_file2_test);
548 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
initial.commitd7cae122008-07-26 21:49:38549
550 // Check everything has been copied.
551 EXPECT_TRUE(file_util::PathExists(file_name_from));
552 EXPECT_TRUE(file_util::PathExists(dest_file));
553 const std::wstring read_contents = ReadTextFile(dest_file);
554 EXPECT_EQ(file_contents, read_contents);
[email protected]37088fef2008-08-15 17:32:10555 EXPECT_TRUE(file_util::PathExists(dest_file2_test));
556 EXPECT_TRUE(file_util::PathExists(dest_file2));
initial.commitd7cae122008-07-26 21:49:38557}
558
[email protected]37088fef2008-08-15 17:32:10559// TODO(erikkay): implement
[email protected]8541bb82008-08-15 17:45:13560#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38561TEST_F(FileUtilTest, GetFileCreationLocalTime) {
562 std::wstring file_name = test_dir_;
563 file_util::AppendToPath(&file_name, L"Test File.txt");
564
565 SYSTEMTIME start_time;
566 GetLocalTime(&start_time);
567 Sleep(100);
568 CreateTextFile(file_name, L"New file!");
569 Sleep(100);
570 SYSTEMTIME end_time;
571 GetLocalTime(&end_time);
572
573 SYSTEMTIME file_creation_time;
574 file_util::GetFileCreationLocalTime(file_name, &file_creation_time);
575
576 FILETIME start_filetime;
577 SystemTimeToFileTime(&start_time, &start_filetime);
578 FILETIME end_filetime;
579 SystemTimeToFileTime(&end_time, &end_filetime);
580 FILETIME file_creation_filetime;
581 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
582
583 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
584 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
585 "creation time: " << FileTimeAsUint64(file_creation_filetime);
586
587 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
588 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
589 "end time: " << FileTimeAsUint64(end_filetime);
590
591 ASSERT_TRUE(DeleteFile(file_name.c_str()));
592}
[email protected]37088fef2008-08-15 17:32:10593#endif
initial.commitd7cae122008-07-26 21:49:38594
595typedef testing::Test ReadOnlyFileUtilTest;
596
597TEST(ReadOnlyFileUtilTest, ContentsEqual) {
598 std::wstring data_dir;
599 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
600 file_util::AppendToPath(&data_dir, L"base");
601 file_util::AppendToPath(&data_dir, L"data");
602 file_util::AppendToPath(&data_dir, L"file_util_unittest");
603 ASSERT_TRUE(file_util::PathExists(data_dir));
604
605 std::wstring original_file = data_dir;
606 file_util::AppendToPath(&original_file, L"original.txt");
607 std::wstring same_file = data_dir;
608 file_util::AppendToPath(&same_file, L"same.txt");
609 std::wstring same_length_file = data_dir;
610 file_util::AppendToPath(&same_length_file, L"same_length.txt");
611 std::wstring different_file = data_dir;
612 file_util::AppendToPath(&different_file, L"different.txt");
613 std::wstring different_first_file = data_dir;
614 file_util::AppendToPath(&different_first_file, L"different_first.txt");
615 std::wstring different_last_file = data_dir;
616 file_util::AppendToPath(&different_last_file, L"different_last.txt");
617 std::wstring empty1_file = data_dir;
618 file_util::AppendToPath(&empty1_file, L"empty1.txt");
619 std::wstring empty2_file = data_dir;
620 file_util::AppendToPath(&empty2_file, L"empty2.txt");
621 std::wstring shortened_file = data_dir;
622 file_util::AppendToPath(&shortened_file, L"shortened.txt");
623 std::wstring binary_file = data_dir;
624 file_util::AppendToPath(&binary_file, L"binary_file.bin");
625 std::wstring binary_file_same = data_dir;
626 file_util::AppendToPath(&binary_file_same, L"binary_file_same.bin");
627 std::wstring binary_file_diff = data_dir;
628 file_util::AppendToPath(&binary_file_diff, L"binary_file_diff.bin");
629
630 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
631 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
632 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
633 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
634 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname"));
635 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
636 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
637 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
638 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
639 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
640 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
641 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
642}
643
[email protected]37088fef2008-08-15 17:32:10644// We don't need equivalent functionality outside of Windows.
[email protected]8541bb82008-08-15 17:45:13645#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38646TEST_F(FileUtilTest, ResolveShortcutTest) {
647 std::wstring target_file = test_dir_;
648 file_util::AppendToPath(&target_file, L"Target.txt");
649 CreateTextFile(target_file, L"This is the target.");
650
651 std::wstring link_file = test_dir_;
652 file_util::AppendToPath(&link_file, L"Link.lnk");
653
654 HRESULT result;
655 IShellLink *shell = NULL;
656 IPersistFile *persist = NULL;
657
658 CoInitialize(NULL);
659 // Temporarily create a shortcut for test
660 result = CoCreateInstance(CLSID_ShellLink, NULL,
661 CLSCTX_INPROC_SERVER, IID_IShellLink,
662 reinterpret_cast<LPVOID*>(&shell));
663 EXPECT_TRUE(SUCCEEDED(result));
664 result = shell->QueryInterface(IID_IPersistFile,
665 reinterpret_cast<LPVOID*>(&persist));
666 EXPECT_TRUE(SUCCEEDED(result));
667 result = shell->SetPath(target_file.c_str());
668 EXPECT_TRUE(SUCCEEDED(result));
669 result = shell->SetDescription(L"ResolveShortcutTest");
670 EXPECT_TRUE(SUCCEEDED(result));
671 result = persist->Save(link_file.c_str(), TRUE);
672 EXPECT_TRUE(SUCCEEDED(result));
673 if (persist)
674 persist->Release();
675 if (shell)
676 shell->Release();
677
678 bool is_solved;
679 is_solved = file_util::ResolveShortcut(&link_file);
680 EXPECT_TRUE(is_solved);
681 std::wstring contents;
682 contents = ReadTextFile(link_file);
683 EXPECT_EQ(L"This is the target.", contents);
684
685 // Cleanning
686 DeleteFile(target_file.c_str());
687 DeleteFile(link_file.c_str());
688 CoUninitialize();
689}
690
691TEST_F(FileUtilTest, CreateShortcutTest) {
692 const wchar_t file_contents[] = L"This is another target.";
693 std::wstring target_file = test_dir_;
694 file_util::AppendToPath(&target_file, L"Target1.txt");
695 CreateTextFile(target_file, file_contents);
696
697 std::wstring link_file = test_dir_;
698 file_util::AppendToPath(&link_file, L"Link1.lnk");
699
700 CoInitialize(NULL);
701 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.c_str(),
702 link_file.c_str(),
703 NULL, NULL, NULL, NULL, 0));
704 std::wstring resolved_name = link_file;
705 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
706 std::wstring read_contents = ReadTextFile(resolved_name);
707 EXPECT_EQ(file_contents, read_contents);
708
709 DeleteFile(target_file.c_str());
710 DeleteFile(link_file.c_str());
711 CoUninitialize();
712}
[email protected]37088fef2008-08-15 17:32:10713#endif
initial.commitd7cae122008-07-26 21:49:38714
715TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
716 std::wstring temp_file;
717 file_util::CreateTemporaryFileName(&temp_file);
718 EXPECT_EQ(file_util::PathExists(temp_file), true);
[email protected]37088fef2008-08-15 17:32:10719 EXPECT_EQ(file_util::Delete(temp_file, false), true);
initial.commitd7cae122008-07-26 21:49:38720}
721
722TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
723 std::wstring temp_dir;
724 file_util::CreateNewTempDirectory(std::wstring(), &temp_dir);
725 EXPECT_EQ(file_util::PathExists(temp_dir), true);
[email protected]37088fef2008-08-15 17:32:10726 EXPECT_EQ(file_util::Delete(temp_dir, false), true);
initial.commitd7cae122008-07-26 21:49:38727}
728
729TEST_F(FileUtilTest, CreateDirectoryTest) {
730 std::wstring test_root = test_dir_;
731 file_util::AppendToPath(&test_root, L"create_directory_test");
732 std::wstring test_path(test_root);
[email protected]8541bb82008-08-15 17:45:13733#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38734 file_util::AppendToPath(&test_path, L"dir\\tree\\likely\\doesnt\\exist\\");
[email protected]37088fef2008-08-15 17:32:10735#elif defined(OS_POSIX)
736 file_util::AppendToPath(&test_path, L"dir/tree/likely/doesnt/exist/");
737#endif
738
initial.commitd7cae122008-07-26 21:49:38739 EXPECT_EQ(file_util::PathExists(test_path), false);
740 EXPECT_EQ(file_util::CreateDirectory(test_path), true);
741 EXPECT_EQ(file_util::PathExists(test_path), true);
742 EXPECT_EQ(file_util::Delete(test_root, true), true);
743 EXPECT_EQ(file_util::PathExists(test_root), false);
744 EXPECT_EQ(file_util::PathExists(test_path), false);
745}
746
[email protected]37088fef2008-08-15 17:32:10747static const struct goodbad_pair {
initial.commitd7cae122008-07-26 21:49:38748 std::wstring bad_name;
749 std::wstring good_name;
750} kIllegalCharacterCases[] = {
751 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
752 {L"**********::::.txt", L"--------------.txt"},
initial.commitd7cae122008-07-26 21:49:38753 // We can't use UCNs (universal character names) for C0/C1 characters and
754 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
755 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
[email protected]8541bb82008-08-15 17:45:13756#if defined(OS_WIN)
[email protected]37088fef2008-08-15 17:32:10757 {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
initial.commitd7cae122008-07-26 21:49:38758 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
759 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
[email protected]37088fef2008-08-15 17:32:10760#elif defined(OS_POSIX)
761 {L"bad*file?name.jpg", L"bad-file-name.jpg"},
762 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"},
763 {L"bad\uFFFFfile-name.jpg ", L"bad-file-name.jpg"},
764#endif
initial.commitd7cae122008-07-26 21:49:38765 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
766 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
767 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
768 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
769 // Unassigned codepoints are ok.
770 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
771};
772
773TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) {
[email protected]37088fef2008-08-15 17:32:10774 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
initial.commitd7cae122008-07-26 21:49:38775 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
776 file_util::ReplaceIllegalCharacters(&bad_name, L'-');
777 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
778 }
779}
780
781static const struct ReplaceExtensionCase {
782 std::wstring file_name;
783 std::wstring extension;
784 std::wstring result;
785} kReplaceExtension[] = {
786 {L"", L"", L""},
787 {L"", L"txt", L".txt"},
788 {L".", L"txt", L".txt"},
789 {L".", L"", L""},
790 {L"foo.dll", L"txt", L"foo.txt"},
791 {L"foo.dll", L".txt", L"foo.txt"},
792 {L"foo", L"txt", L"foo.txt"},
793 {L"foo", L".txt", L"foo.txt"},
794 {L"foo.baz.dll", L"txt", L"foo.baz.txt"},
795 {L"foo.baz.dll", L".txt", L"foo.baz.txt"},
796 {L"foo.dll", L"", L"foo"},
797 {L"foo.dll", L".", L"foo"},
798 {L"foo", L"", L"foo"},
799 {L"foo", L".", L"foo"},
800 {L"foo.baz.dll", L"", L"foo.baz"},
801 {L"foo.baz.dll", L".", L"foo.baz"},
802};
803
804TEST_F(FileUtilTest, ReplaceExtensionTest) {
[email protected]37088fef2008-08-15 17:32:10805 for (unsigned int i = 0; i < arraysize(kReplaceExtension); ++i) {
initial.commitd7cae122008-07-26 21:49:38806 std::wstring file_name(kReplaceExtension[i].file_name);
807 file_util::ReplaceExtension(&file_name, kReplaceExtension[i].extension);
808 EXPECT_EQ(file_name, kReplaceExtension[i].result);
809 }
810}
811
812TEST_F(FileUtilTest, FileEnumeratorTest) {
813 // Test an empty directory.
814 file_util::FileEnumerator f0(test_dir_, true,
815 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
816 EXPECT_EQ(f0.Next(), L"");
817 EXPECT_EQ(f0.Next(), L"");
818
[email protected]37088fef2008-08-15 17:32:10819 // create the directories
820 std::wstring dir1 = test_dir_;
821 file_util::AppendToPath(&dir1, L"dir1");
822 EXPECT_TRUE(file_util::CreateDirectory(dir1));
823 std::wstring dir2 = test_dir_;
824 file_util::AppendToPath(&dir2, L"dir2");
825 EXPECT_TRUE(file_util::CreateDirectory(dir2));
826 std::wstring dir2inner = dir2;
827 file_util::AppendToPath(&dir2inner, L"inner");
828 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
829
830 // create the files
831 std::wstring dir2file = dir2;
832 file_util::AppendToPath(&dir2file, L"dir2file.txt");
833 CreateTextFile(dir2file, L"");
834 std::wstring dir2innerfile = dir2inner;
835 file_util::AppendToPath(&dir2innerfile, L"innerfile.txt");
836 CreateTextFile(dir2innerfile, L"");
837 std::wstring file1 = test_dir_;
838 file_util::AppendToPath(&file1, L"file1.txt");
839 CreateTextFile(file1, L"");
840 std::wstring file2_rel = dir2;
841 file_util::AppendToPath(&file2_rel, L"..");
842 file_util::AppendToPath(&file2_rel, L"file2.txt");
843 CreateTextFile(file2_rel, L"");
844 std::wstring file2_abs = test_dir_;
845 file_util::AppendToPath(&file2_abs, L"file2.txt");
initial.commitd7cae122008-07-26 21:49:38846
847 // Only enumerate files.
848 file_util::FileEnumerator f1(test_dir_, true,
849 file_util::FileEnumerator::FILES);
850 FindResultCollector c1(f1);
[email protected]37088fef2008-08-15 17:32:10851 EXPECT_TRUE(c1.HasFile(file1));
852 EXPECT_TRUE(c1.HasFile(file2_abs));
853 EXPECT_TRUE(c1.HasFile(dir2file));
854 EXPECT_TRUE(c1.HasFile(dir2innerfile));
855 EXPECT_EQ(c1.size(), 4);
initial.commitd7cae122008-07-26 21:49:38856
857 // Only enumerate directories.
858 file_util::FileEnumerator f2(test_dir_, true,
859 file_util::FileEnumerator::DIRECTORIES);
860 FindResultCollector c2(f2);
[email protected]37088fef2008-08-15 17:32:10861 EXPECT_TRUE(c2.HasFile(dir1));
862 EXPECT_TRUE(c2.HasFile(dir2));
863 EXPECT_TRUE(c2.HasFile(dir2inner));
864 EXPECT_EQ(c2.size(), 3);
initial.commitd7cae122008-07-26 21:49:38865
866 // Enumerate files and directories.
867 file_util::FileEnumerator f3(test_dir_, true,
868 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
869 FindResultCollector c3(f3);
[email protected]37088fef2008-08-15 17:32:10870 EXPECT_TRUE(c3.HasFile(dir1));
871 EXPECT_TRUE(c3.HasFile(dir2));
872 EXPECT_TRUE(c3.HasFile(file1));
873 EXPECT_TRUE(c3.HasFile(file2_abs));
874 EXPECT_TRUE(c3.HasFile(dir2file));
875 EXPECT_TRUE(c3.HasFile(dir2inner));
876 EXPECT_TRUE(c3.HasFile(dir2innerfile));
877 EXPECT_EQ(c3.size(), 7);
initial.commitd7cae122008-07-26 21:49:38878
879 // Non-recursive operation.
880 file_util::FileEnumerator f4(test_dir_, false,
881 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
882 FindResultCollector c4(f4);
[email protected]37088fef2008-08-15 17:32:10883 EXPECT_TRUE(c4.HasFile(dir2));
884 EXPECT_TRUE(c4.HasFile(dir2));
885 EXPECT_TRUE(c4.HasFile(file1));
886 EXPECT_TRUE(c4.HasFile(file2_abs));
887 EXPECT_EQ(c4.size(), 4);
initial.commitd7cae122008-07-26 21:49:38888
889 // Enumerate with a pattern.
890 file_util::FileEnumerator f5(test_dir_, true,
891 file_util::FileEnumerator::FILES_AND_DIRECTORIES, L"dir*");
892 FindResultCollector c5(f5);
[email protected]37088fef2008-08-15 17:32:10893 EXPECT_TRUE(c5.HasFile(dir1));
894 EXPECT_TRUE(c5.HasFile(dir2));
895 EXPECT_TRUE(c5.HasFile(dir2file));
896 EXPECT_TRUE(c5.HasFile(dir2inner));
897 EXPECT_TRUE(c5.HasFile(dir2innerfile));
898 EXPECT_EQ(c5.size(), 5);
initial.commitd7cae122008-07-26 21:49:38899
900 // Make sure the destructor closes the find handle while in the middle of a
901 // query to allow TearDown to delete the directory.
902 file_util::FileEnumerator f6(test_dir_, true,
903 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
904 EXPECT_FALSE(f6.Next().empty()); // Should have found something
905 // (we don't care what).
906}