Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: base/file_util_unittest.cc

Issue 8825: Begin the first small step towards using FilePath everywhere: (Closed)
Patch Set: works on windows Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
11 #endif 11 #endif
12 12
13 #include <fstream> 13 #include <fstream>
14 #include <iostream> 14 #include <iostream>
15 #include <set> 15 #include <set>
16 16
17 #include "base/base_paths.h" 17 #include "base/base_paths.h"
18 #include "base/file_path.h"
18 #include "base/file_util.h" 19 #include "base/file_util.h"
19 #include "base/logging.h" 20 #include "base/logging.h"
20 #include "base/path_service.h" 21 #include "base/path_service.h"
21 #include "base/platform_test.h" 22 #include "base/platform_test.h"
22 #include "base/string_util.h" 23 #include "base/string_util.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 25
25 namespace { 26 namespace {
26 27
27 // file_util winds up using autoreleased objects on the Mac, so this needs 28 // file_util winds up using autoreleased objects on the Mac, so this needs
28 // to be a PlatformTest 29 // to be a PlatformTest
29 class FileUtilTest : public PlatformTest { 30 class FileUtilTest : public PlatformTest {
30 protected: 31 protected:
31 virtual void SetUp() { 32 virtual void SetUp() {
32 PlatformTest::SetUp(); 33 PlatformTest::SetUp();
33 // Name a subdirectory of the temp directory. 34 // Name a subdirectory of the temp directory.
34 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); 35 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
35 file_util::AppendToPath(&test_dir_, L"FileUtilTest"); 36 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
36 37
37 // Create a fresh, empty copy of this directory. 38 // Create a fresh, empty copy of this directory.
38 file_util::Delete(test_dir_, true); 39 file_util::Delete(test_dir_, true);
39 file_util::CreateDirectory(test_dir_.c_str()); 40 file_util::CreateDirectory(test_dir_);
40 } 41 }
41 virtual void TearDown() { 42 virtual void TearDown() {
42 PlatformTest::TearDown(); 43 PlatformTest::TearDown();
43 // Clean up test directory 44 // Clean up test directory
44 ASSERT_TRUE(file_util::Delete(test_dir_, true)); 45 ASSERT_TRUE(file_util::Delete(test_dir_, true));
45 ASSERT_FALSE(file_util::PathExists(test_dir_)); 46 ASSERT_FALSE(file_util::PathExists(test_dir_));
46 } 47 }
47 48
48 // the path to temporary directory used to contain the test operations 49 // the path to temporary directory used to contain the test operations
49 std::wstring test_dir_; 50 FilePath test_dir_;
50 }; 51 };
51 52
52 // Collects all the results from the given file enumerator, and provides an 53 // Collects all the results from the given file enumerator, and provides an
53 // interface to query whether a given file is present. 54 // interface to query whether a given file is present.
54 class FindResultCollector { 55 class FindResultCollector {
55 public: 56 public:
56 FindResultCollector(file_util::FileEnumerator& enumerator) { 57 FindResultCollector(file_util::FileEnumerator& enumerator) {
57 std::wstring cur_file; 58 std::wstring cur_file;
58 while (!(cur_file = enumerator.Next()).empty()) { 59 while (!(cur_file = enumerator.Next()).empty()) {
60 FilePath::StringType path = FilePath::FromWStringHack(cur_file).value();
59 // The file should not be returned twice. 61 // The file should not be returned twice.
60 EXPECT_TRUE(files_.end() == files_.find(cur_file)) 62 EXPECT_TRUE(files_.end() == files_.find(path))
61 << "Same file returned twice"; 63 << "Same file returned twice";
62 64
63 // Save for later. 65 // Save for later.
64 files_.insert(cur_file); 66 files_.insert(path);
65 } 67 }
66 } 68 }
67 69
68 // Returns true if the enumerator found the file. 70 // Returns true if the enumerator found the file.
69 bool HasFile(const std::wstring& file) const { 71 bool HasFile(const FilePath& file) const {
70 return files_.find(file) != files_.end(); 72 return files_.find(file.value()) != files_.end();
71 } 73 }
72 74
73 int size() { 75 int size() {
74 return static_cast<int>(files_.size()); 76 return static_cast<int>(files_.size());
75 } 77 }
76 78
77 private: 79 private:
78 std::set<std::wstring> files_; 80 std::set<FilePath::StringType> files_;
79 }; 81 };
80 82
81 // Simple function to dump some text into a new file. 83 // Simple function to dump some text into a new file.
82 void CreateTextFile(const std::wstring& filename, 84 void CreateTextFile(const FilePath& filename,
83 const std::wstring& contents) { 85 const std::wstring& contents) {
84 std::ofstream file; 86 std::ofstream file;
85 file.open(WideToUTF8(filename).c_str()); 87 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
86 ASSERT_TRUE(file.is_open()); 88 ASSERT_TRUE(file.is_open());
87 file << contents; 89 file << contents;
88 file.close(); 90 file.close();
89 } 91 }
90 92
91 // Simple function to take out some text from a file. 93 // Simple function to take out some text from a file.
92 std::wstring ReadTextFile(const std::wstring& filename) { 94 std::wstring ReadTextFile(const FilePath& filename) {
93 wchar_t contents[64]; 95 wchar_t contents[64];
94 std::wifstream file; 96 std::wifstream file;
95 file.open(WideToUTF8(filename).c_str()); 97 file.open(WideToUTF8(filename.ToWStringHack()).c_str());
96 EXPECT_TRUE(file.is_open()); 98 EXPECT_TRUE(file.is_open());
97 file.getline(contents, 64); 99 file.getline(contents, 64);
98 file.close(); 100 file.close();
99 return std::wstring(contents); 101 return std::wstring(contents);
100 } 102 }
101 103
102 #if defined(OS_WIN) 104 #if defined(OS_WIN)
103 uint64 FileTimeAsUint64(const FILETIME& ft) { 105 uint64 FileTimeAsUint64(const FILETIME& ft) {
104 ULARGE_INTEGER u; 106 ULARGE_INTEGER u;
105 u.LowPart = ft.dwLowDateTime; 107 u.LowPart = ft.dwLowDateTime;
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 const std::wstring parent = 305 const std::wstring parent =
304 file_util::GetDirectoryFromPath(dir.full_path); 306 file_util::GetDirectoryFromPath(dir.full_path);
305 EXPECT_EQ(dir.directory, parent); 307 EXPECT_EQ(dir.directory, parent);
306 } 308 }
307 } 309 }
308 310
309 // TODO(erikkay): implement 311 // TODO(erikkay): implement
310 #if defined OS_WIN 312 #if defined OS_WIN
311 TEST_F(FileUtilTest, CountFilesCreatedAfter) { 313 TEST_F(FileUtilTest, CountFilesCreatedAfter) {
312 // Create old file (that we don't want to count) 314 // Create old file (that we don't want to count)
313 std::wstring old_file_name = test_dir_; 315 FilePath old_file_name = test_dir_.Append(L"Old File.txt");
314 file_util::AppendToPath(&old_file_name, L"Old File.txt");
315 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits"); 316 CreateTextFile(old_file_name, L"Just call me Mr. Creakybits");
316 317
317 // Age to perfection 318 // Age to perfection
318 Sleep(100); 319 Sleep(100);
319 320
320 // Establish our cutoff time 321 // Establish our cutoff time
321 FILETIME test_start_time; 322 FILETIME test_start_time;
322 GetSystemTimeAsFileTime(&test_start_time); 323 GetSystemTimeAsFileTime(&test_start_time);
323 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, test_start_time)); 324 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
325 test_start_time));
324 326
325 // Create a new file (that we do want to count) 327 // Create a new file (that we do want to count)
326 std::wstring new_file_name = test_dir_; 328 FilePath new_file_name = test_dir_.Append(L"New File.txt");
327 file_util::AppendToPath(&new_file_name, L"New File.txt");
328 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah."); 329 CreateTextFile(new_file_name, L"Waaaaaaaaaaaaaah.");
329 330
330 // We should see only the new file. 331 // We should see only the new file.
331 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_, test_start_time)); 332 EXPECT_EQ(1, file_util::CountFilesCreatedAfter(test_dir_.value(),
333 test_start_time));
332 334
333 // Delete new file, we should see no files after cutoff now 335 // Delete new file, we should see no files after cutoff now
334 EXPECT_TRUE(file_util::Delete(new_file_name, false)); 336 EXPECT_TRUE(file_util::Delete(new_file_name, false));
335 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_, test_start_time)); 337 EXPECT_EQ(0, file_util::CountFilesCreatedAfter(test_dir_.value(),
338 test_start_time));
336 } 339 }
337 #endif 340 #endif
338 341
339 // Tests that the Delete function works as expected, especially 342 // Tests that the Delete function works as expected, especially
340 // the recursion flag. Also coincidentally tests PathExists. 343 // the recursion flag. Also coincidentally tests PathExists.
341 TEST_F(FileUtilTest, Delete) { 344 TEST_F(FileUtilTest, Delete) {
342 // Create a file 345 // Create a file
343 std::wstring file_name = test_dir_; 346 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt"));
344 file_util::AppendToPath(&file_name, L"Test File.txt");
345 CreateTextFile(file_name, L"I'm cannon fodder."); 347 CreateTextFile(file_name, L"I'm cannon fodder.");
346 348
347 ASSERT_TRUE(file_util::PathExists(file_name)); 349 ASSERT_TRUE(file_util::PathExists(file_name));
348 350
349 std::wstring subdir_path = test_dir_; 351 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory"));
350 file_util::AppendToPath(&subdir_path, L"Subdirectory"); 352 file_util::CreateDirectory(subdir_path);
351 file_util::CreateDirectory(subdir_path.c_str());
352 353
353 ASSERT_TRUE(file_util::PathExists(subdir_path)); 354 ASSERT_TRUE(file_util::PathExists(subdir_path));
354 355
355 std::wstring directory_contents = test_dir_; 356 FilePath directory_contents = test_dir_;
356 #if defined(OS_WIN) 357 #if defined(OS_WIN)
357 // TODO(erikkay): see if anyone's actually using this feature of the API 358 // TODO(erikkay): see if anyone's actually using this feature of the API
358 file_util::AppendToPath(&directory_contents, L"*"); 359 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*"));
359
360 // Delete non-recursively and check that only the file is deleted 360 // Delete non-recursively and check that only the file is deleted
361 ASSERT_TRUE(file_util::Delete(directory_contents, false)); 361 ASSERT_TRUE(file_util::Delete(directory_contents, false));
362 EXPECT_FALSE(file_util::PathExists(file_name)); 362 EXPECT_FALSE(file_util::PathExists(file_name));
363 EXPECT_TRUE(file_util::PathExists(subdir_path)); 363 EXPECT_TRUE(file_util::PathExists(subdir_path));
364 #endif 364 #endif
365 365
366 // Delete recursively and make sure all contents are deleted 366 // Delete recursively and make sure all contents are deleted
367 ASSERT_TRUE(file_util::Delete(directory_contents, true)); 367 ASSERT_TRUE(file_util::Delete(directory_contents, true));
368 EXPECT_FALSE(file_util::PathExists(file_name)); 368 EXPECT_FALSE(file_util::PathExists(file_name));
369 EXPECT_FALSE(file_util::PathExists(subdir_path)); 369 EXPECT_FALSE(file_util::PathExists(subdir_path));
370 } 370 }
371 371
372 TEST_F(FileUtilTest, Move) { 372 TEST_F(FileUtilTest, Move) {
373 // Create a directory 373 // Create a directory
374 std::wstring dir_name_from(test_dir_); 374 FilePath dir_name_from =
375 file_util::AppendToPath(&dir_name_from, L"Move_From_Subdir"); 375 test_dir_.Append(FILE_PATH_LITERAL("Move_From_Subdir"));
376 file_util::CreateDirectory(dir_name_from.c_str()); 376 file_util::CreateDirectory(dir_name_from);
377 ASSERT_TRUE(file_util::PathExists(dir_name_from)); 377 ASSERT_TRUE(file_util::PathExists(dir_name_from));
378 378
379 // Create a file under the directory 379 // Create a file under the directory
380 std::wstring file_name_from(dir_name_from); 380 FilePath file_name_from =
381 file_util::AppendToPath(&file_name_from, L"Move_Test_File.txt"); 381 dir_name_from.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
382 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 382 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
383 ASSERT_TRUE(file_util::PathExists(file_name_from)); 383 ASSERT_TRUE(file_util::PathExists(file_name_from));
384 384
385 // Move the directory 385 // Move the directory
386 std::wstring dir_name_to(test_dir_); 386 FilePath dir_name_to = test_dir_.Append(FILE_PATH_LITERAL("Move_To_Subdir"));
387 file_util::AppendToPath(&dir_name_to, L"Move_To_Subdir"); 387 FilePath file_name_to =
388 std::wstring file_name_to(dir_name_to); 388 dir_name_to.Append(FILE_PATH_LITERAL("Move_Test_File.txt"));
389 file_util::AppendToPath(&file_name_to, L"Move_Test_File.txt");
390 389
391 ASSERT_FALSE(file_util::PathExists(dir_name_to)); 390 ASSERT_FALSE(file_util::PathExists(dir_name_to));
392 391
393 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to)); 392 EXPECT_TRUE(file_util::Move(dir_name_from, dir_name_to));
394 393
395 // Check everything has been moved. 394 // Check everything has been moved.
396 EXPECT_FALSE(file_util::PathExists(dir_name_from)); 395 EXPECT_FALSE(file_util::PathExists(dir_name_from));
397 EXPECT_FALSE(file_util::PathExists(file_name_from)); 396 EXPECT_FALSE(file_util::PathExists(file_name_from));
398 EXPECT_TRUE(file_util::PathExists(dir_name_to)); 397 EXPECT_TRUE(file_util::PathExists(dir_name_to));
399 EXPECT_TRUE(file_util::PathExists(file_name_to)); 398 EXPECT_TRUE(file_util::PathExists(file_name_to));
400 } 399 }
401 400
402 TEST_F(FileUtilTest, CopyDirectoryRecursively) { 401 TEST_F(FileUtilTest, CopyDirectoryRecursively) {
403 // Create a directory. 402 // Create a directory.
404 std::wstring dir_name_from(test_dir_); 403 FilePath dir_name_from =
405 file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir"); 404 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
406 file_util::CreateDirectory(dir_name_from.c_str()); 405 file_util::CreateDirectory(dir_name_from);
407 ASSERT_TRUE(file_util::PathExists(dir_name_from)); 406 ASSERT_TRUE(file_util::PathExists(dir_name_from));
408 407
409 // Create a file under the directory. 408 // Create a file under the directory.
410 std::wstring file_name_from(dir_name_from); 409 FilePath file_name_from =
411 file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt"); 410 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
412 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 411 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
413 ASSERT_TRUE(file_util::PathExists(file_name_from)); 412 ASSERT_TRUE(file_util::PathExists(file_name_from));
414 413
415 // Create a subdirectory. 414 // Create a subdirectory.
416 std::wstring subdir_name_from(dir_name_from); 415 FilePath subdir_name_from =
417 file_util::AppendToPath(&subdir_name_from, L"Subdir"); 416 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
418 file_util::CreateDirectory(subdir_name_from.c_str()); 417 file_util::CreateDirectory(subdir_name_from);
419 ASSERT_TRUE(file_util::PathExists(subdir_name_from)); 418 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
420 419
421 // Create a file under the subdirectory. 420 // Create a file under the subdirectory.
422 std::wstring file_name2_from(subdir_name_from); 421 FilePath file_name2_from =
423 file_util::AppendToPath(&file_name2_from, L"Copy_Test_File.txt"); 422 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
424 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); 423 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
425 ASSERT_TRUE(file_util::PathExists(file_name2_from)); 424 ASSERT_TRUE(file_util::PathExists(file_name2_from));
426 425
427 // Copy the directory recursively. 426 // Copy the directory recursively.
428 std::wstring dir_name_to(test_dir_); 427 FilePath dir_name_to =
429 file_util::AppendToPath(&dir_name_to, L"Copy_To_Subdir"); 428 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
430 std::wstring file_name_to(dir_name_to); 429 FilePath file_name_to =
431 file_util::AppendToPath(&file_name_to, L"Copy_Test_File.txt"); 430 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
432 std::wstring subdir_name_to(dir_name_to); 431 FilePath subdir_name_to =
433 file_util::AppendToPath(&subdir_name_to, L"Subdir"); 432 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
434 std::wstring file_name2_to(subdir_name_to); 433 FilePath file_name2_to =
435 file_util::AppendToPath(&file_name2_to, L"Copy_Test_File.txt"); 434 subdir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
436 435
437 ASSERT_FALSE(file_util::PathExists(dir_name_to)); 436 ASSERT_FALSE(file_util::PathExists(dir_name_to));
438 437
439 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true)); 438 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, true));
440 439
441 // Check everything has been copied. 440 // Check everything has been copied.
442 EXPECT_TRUE(file_util::PathExists(dir_name_from)); 441 EXPECT_TRUE(file_util::PathExists(dir_name_from));
443 EXPECT_TRUE(file_util::PathExists(file_name_from)); 442 EXPECT_TRUE(file_util::PathExists(file_name_from));
444 EXPECT_TRUE(file_util::PathExists(subdir_name_from)); 443 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
445 EXPECT_TRUE(file_util::PathExists(file_name2_from)); 444 EXPECT_TRUE(file_util::PathExists(file_name2_from));
446 EXPECT_TRUE(file_util::PathExists(dir_name_to)); 445 EXPECT_TRUE(file_util::PathExists(dir_name_to));
447 EXPECT_TRUE(file_util::PathExists(file_name_to)); 446 EXPECT_TRUE(file_util::PathExists(file_name_to));
448 EXPECT_TRUE(file_util::PathExists(subdir_name_to)); 447 EXPECT_TRUE(file_util::PathExists(subdir_name_to));
449 EXPECT_TRUE(file_util::PathExists(file_name2_to)); 448 EXPECT_TRUE(file_util::PathExists(file_name2_to));
450 } 449 }
451 450
452 TEST_F(FileUtilTest, CopyDirectory) { 451 TEST_F(FileUtilTest, CopyDirectory) {
453 // Create a directory. 452 // Create a directory.
454 std::wstring dir_name_from(test_dir_); 453 FilePath dir_name_from =
455 file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir"); 454 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
456 file_util::CreateDirectory(dir_name_from.c_str()); 455 file_util::CreateDirectory(dir_name_from);
457 ASSERT_TRUE(file_util::PathExists(dir_name_from)); 456 ASSERT_TRUE(file_util::PathExists(dir_name_from));
458 457
459 // Create a file under the directory. 458 // Create a file under the directory.
460 std::wstring file_name_from(dir_name_from); 459 FilePath file_name_from =
461 file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt"); 460 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
462 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); 461 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle");
463 ASSERT_TRUE(file_util::PathExists(file_name_from)); 462 ASSERT_TRUE(file_util::PathExists(file_name_from));
464 463
465 // Create a subdirectory. 464 // Create a subdirectory.
466 std::wstring subdir_name_from(dir_name_from); 465 FilePath subdir_name_from =
467 file_util::AppendToPath(&subdir_name_from, L"Subdir"); 466 dir_name_from.Append(FILE_PATH_LITERAL("Subdir"));
468 file_util::CreateDirectory(subdir_name_from.c_str()); 467 file_util::CreateDirectory(subdir_name_from);
469 ASSERT_TRUE(file_util::PathExists(subdir_name_from)); 468 ASSERT_TRUE(file_util::PathExists(subdir_name_from));
470 469
471 // Create a file under the subdirectory. 470 // Create a file under the subdirectory.
472 std::wstring file_name2_from(subdir_name_from); 471 FilePath file_name2_from =
473 file_util::AppendToPath(&file_name2_from, L"Copy_Test_File.txt"); 472 subdir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
474 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle"); 473 CreateTextFile(file_name2_from, L"Gooooooooooooooooooooogle");
475 ASSERT_TRUE(file_util::PathExists(file_name2_from)); 474 ASSERT_TRUE(file_util::PathExists(file_name2_from));
476 475
477 // Copy the directory not recursively. 476 // Copy the directory not recursively.
478 std::wstring dir_name_to(test_dir_); 477 FilePath dir_name_to =
479 file_util::AppendToPath(&dir_name_to, L"Copy_To_Subdir"); 478 test_dir_.Append(FILE_PATH_LITERAL("Copy_To_Subdir"));
480 std::wstring file_name_to(dir_name_to); 479 FilePath file_name_to =
481 file_util::AppendToPath(&file_name_to, L"Copy_Test_File.txt"); 480 dir_name_to.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
482 std::wstring subdir_name_to(dir_name_to); 481 FilePath subdir_name_to =
483 file_util::AppendToPath(&subdir_name_to, L"Subdir"); 482 dir_name_to.Append(FILE_PATH_LITERAL("Subdir"));
484 483
485 ASSERT_FALSE(file_util::PathExists(dir_name_to)); 484 ASSERT_FALSE(file_util::PathExists(dir_name_to));
486 485
487 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false)); 486 EXPECT_TRUE(file_util::CopyDirectory(dir_name_from, dir_name_to, false));
488 487
489 // Check everything has been copied. 488 // Check everything has been copied.
490 EXPECT_TRUE(file_util::PathExists(dir_name_from)); 489 EXPECT_TRUE(file_util::PathExists(dir_name_from));
491 EXPECT_TRUE(file_util::PathExists(file_name_from)); 490 EXPECT_TRUE(file_util::PathExists(file_name_from));
492 EXPECT_TRUE(file_util::PathExists(subdir_name_from)); 491 EXPECT_TRUE(file_util::PathExists(subdir_name_from));
493 EXPECT_TRUE(file_util::PathExists(file_name2_from)); 492 EXPECT_TRUE(file_util::PathExists(file_name2_from));
494 EXPECT_TRUE(file_util::PathExists(dir_name_to)); 493 EXPECT_TRUE(file_util::PathExists(dir_name_to));
495 EXPECT_TRUE(file_util::PathExists(file_name_to)); 494 EXPECT_TRUE(file_util::PathExists(file_name_to));
496 EXPECT_FALSE(file_util::PathExists(subdir_name_to)); 495 EXPECT_FALSE(file_util::PathExists(subdir_name_to));
497 } 496 }
498 497
499 TEST_F(FileUtilTest, CopyFile) { 498 TEST_F(FileUtilTest, CopyFile) {
500 // Create a directory 499 // Create a directory
501 std::wstring dir_name_from(test_dir_); 500 FilePath dir_name_from =
502 file_util::AppendToPath(&dir_name_from, L"Copy_From_Subdir"); 501 test_dir_.Append(FILE_PATH_LITERAL("Copy_From_Subdir"));
503 file_util::CreateDirectory(dir_name_from.c_str()); 502 file_util::CreateDirectory(dir_name_from);
504 ASSERT_TRUE(file_util::PathExists(dir_name_from)); 503 ASSERT_TRUE(file_util::PathExists(dir_name_from));
505 504
506 // Create a file under the directory 505 // Create a file under the directory
507 std::wstring file_name_from(dir_name_from); 506 FilePath file_name_from =
508 file_util::AppendToPath(&file_name_from, L"Copy_Test_File.txt"); 507 dir_name_from.Append(FILE_PATH_LITERAL("Copy_Test_File.txt"));
509 const std::wstring file_contents(L"Gooooooooooooooooooooogle"); 508 const std::wstring file_contents(L"Gooooooooooooooooooooogle");
510 CreateTextFile(file_name_from, file_contents); 509 CreateTextFile(file_name_from, file_contents);
511 ASSERT_TRUE(file_util::PathExists(file_name_from)); 510 ASSERT_TRUE(file_util::PathExists(file_name_from));
512 511
513 // Copy the file. 512 // Copy the file.
514 std::wstring dest_file(dir_name_from); 513 FilePath dest_file = dir_name_from.Append(FILE_PATH_LITERAL("DestFile.txt"));
515 file_util::AppendToPath(&dest_file, L"DestFile.txt");
516 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file)); 514 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file));
517 515
518 // Copy the file to another location using '..' in the path. 516 // Copy the file to another location using '..' in the path.
519 std::wstring dest_file2(dir_name_from); 517 std::wstring dest_file2(dir_name_from.ToWStringHack());
520 file_util::AppendToPath(&dest_file2, L".."); 518 file_util::AppendToPath(&dest_file2, L"..");
521 file_util::AppendToPath(&dest_file2, L"DestFile.txt"); 519 file_util::AppendToPath(&dest_file2, L"DestFile.txt");
522 ASSERT_TRUE(file_util::CopyFile(file_name_from, dest_file2)); 520 ASSERT_TRUE(file_util::CopyFile(file_name_from,
523 std::wstring dest_file2_test(dir_name_from); 521 FilePath::FromWStringHack(dest_file2)));
522 std::wstring dest_file2_test(dir_name_from.ToWStringHack());
524 file_util::UpOneDirectory(&dest_file2_test); 523 file_util::UpOneDirectory(&dest_file2_test);
525 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt"); 524 file_util::AppendToPath(&dest_file2_test, L"DestFile.txt");
526 525
527 // Check everything has been copied. 526 // Check everything has been copied.
528 EXPECT_TRUE(file_util::PathExists(file_name_from)); 527 EXPECT_TRUE(file_util::PathExists(file_name_from));
529 EXPECT_TRUE(file_util::PathExists(dest_file)); 528 EXPECT_TRUE(file_util::PathExists(dest_file));
530 const std::wstring read_contents = ReadTextFile(dest_file); 529 const std::wstring read_contents = ReadTextFile(dest_file);
531 EXPECT_EQ(file_contents, read_contents); 530 EXPECT_EQ(file_contents, read_contents);
532 EXPECT_TRUE(file_util::PathExists(dest_file2_test)); 531 EXPECT_TRUE(file_util::PathExists(
533 EXPECT_TRUE(file_util::PathExists(dest_file2)); 532 FilePath::FromWStringHack(dest_file2_test)));
533 EXPECT_TRUE(file_util::PathExists(FilePath::FromWStringHack(dest_file2)));
534 } 534 }
535 535
536 // TODO(erikkay): implement 536 // TODO(erikkay): implement
537 #if defined(OS_WIN) 537 #if defined(OS_WIN)
538 TEST_F(FileUtilTest, GetFileCreationLocalTime) { 538 TEST_F(FileUtilTest, GetFileCreationLocalTime) {
539 std::wstring file_name = test_dir_; 539 FilePath file_name = test_dir_.Append(L"Test File.txt");
540 file_util::AppendToPath(&file_name, L"Test File.txt");
541 540
542 SYSTEMTIME start_time; 541 SYSTEMTIME start_time;
543 GetLocalTime(&start_time); 542 GetLocalTime(&start_time);
544 Sleep(100); 543 Sleep(100);
545 CreateTextFile(file_name, L"New file!"); 544 CreateTextFile(file_name, L"New file!");
546 Sleep(100); 545 Sleep(100);
547 SYSTEMTIME end_time; 546 SYSTEMTIME end_time;
548 GetLocalTime(&end_time); 547 GetLocalTime(&end_time);
549 548
550 SYSTEMTIME file_creation_time; 549 SYSTEMTIME file_creation_time;
551 file_util::GetFileCreationLocalTime(file_name, &file_creation_time); 550 file_util::GetFileCreationLocalTime(file_name.value(), &file_creation_time);
552 551
553 FILETIME start_filetime; 552 FILETIME start_filetime;
554 SystemTimeToFileTime(&start_time, &start_filetime); 553 SystemTimeToFileTime(&start_time, &start_filetime);
555 FILETIME end_filetime; 554 FILETIME end_filetime;
556 SystemTimeToFileTime(&end_time, &end_filetime); 555 SystemTimeToFileTime(&end_time, &end_filetime);
557 FILETIME file_creation_filetime; 556 FILETIME file_creation_filetime;
558 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime); 557 SystemTimeToFileTime(&file_creation_time, &file_creation_filetime);
559 558
560 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) << 559 EXPECT_EQ(-1, CompareFileTime(&start_filetime, &file_creation_filetime)) <<
561 "start time: " << FileTimeAsUint64(start_filetime) << ", " << 560 "start time: " << FileTimeAsUint64(start_filetime) << ", " <<
562 "creation time: " << FileTimeAsUint64(file_creation_filetime); 561 "creation time: " << FileTimeAsUint64(file_creation_filetime);
563 562
564 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) << 563 EXPECT_EQ(-1, CompareFileTime(&file_creation_filetime, &end_filetime)) <<
565 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " << 564 "creation time: " << FileTimeAsUint64(file_creation_filetime) << ", " <<
566 "end time: " << FileTimeAsUint64(end_filetime); 565 "end time: " << FileTimeAsUint64(end_filetime);
567 566
568 ASSERT_TRUE(DeleteFile(file_name.c_str())); 567 ASSERT_TRUE(DeleteFile(file_name.value().c_str()));
569 } 568 }
570 #endif 569 #endif
571 570
572 // file_util winds up using autoreleased objects on the Mac, so this needs 571 // file_util winds up using autoreleased objects on the Mac, so this needs
573 // to be a PlatformTest 572 // to be a PlatformTest.
574 typedef PlatformTest ReadOnlyFileUtilTest; 573 typedef PlatformTest ReadOnlyFileUtilTest;
575 574
576 TEST_F(ReadOnlyFileUtilTest, ContentsEqual) { 575 TEST_F(ReadOnlyFileUtilTest, ContentsEqual) {
577 std::wstring data_dir; 576 FilePath data_dir;
578 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); 577 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
579 file_util::AppendToPath(&data_dir, L"base"); 578 data_dir = data_dir.Append(FILE_PATH_LITERAL("base"))
580 file_util::AppendToPath(&data_dir, L"data"); 579 .Append(FILE_PATH_LITERAL("data"))
581 file_util::AppendToPath(&data_dir, L"file_util_unittest"); 580 .Append(FILE_PATH_LITERAL("file_util_unittest"));
582 ASSERT_TRUE(file_util::PathExists(data_dir)); 581 ASSERT_TRUE(file_util::PathExists(data_dir));
583 582
584 std::wstring original_file = data_dir; 583 FilePath original_file =
585 file_util::AppendToPath(&original_file, L"original.txt"); 584 data_dir.Append(FILE_PATH_LITERAL("original.txt"));
586 std::wstring same_file = data_dir; 585 FilePath same_file =
587 file_util::AppendToPath(&same_file, L"same.txt"); 586 data_dir.Append(FILE_PATH_LITERAL("same.txt"));
588 std::wstring same_length_file = data_dir; 587 FilePath same_length_file =
589 file_util::AppendToPath(&same_length_file, L"same_length.txt"); 588 data_dir.Append(FILE_PATH_LITERAL("same_length.txt"));
590 std::wstring different_file = data_dir; 589 FilePath different_file =
591 file_util::AppendToPath(&different_file, L"different.txt"); 590 data_dir.Append(FILE_PATH_LITERAL("different.txt"));
592 std::wstring different_first_file = data_dir; 591 FilePath different_first_file =
593 file_util::AppendToPath(&different_first_file, L"different_first.txt"); 592 data_dir.Append(FILE_PATH_LITERAL("different_first.txt"));
594 std::wstring different_last_file = data_dir; 593 FilePath different_last_file =
595 file_util::AppendToPath(&different_last_file, L"different_last.txt"); 594 data_dir.Append(FILE_PATH_LITERAL("different_last.txt"));
596 std::wstring empty1_file = data_dir; 595 FilePath empty1_file =
597 file_util::AppendToPath(&empty1_file, L"empty1.txt"); 596 data_dir.Append(FILE_PATH_LITERAL("empty1.txt"));
598 std::wstring empty2_file = data_dir; 597 FilePath empty2_file =
599 file_util::AppendToPath(&empty2_file, L"empty2.txt"); 598 data_dir.Append(FILE_PATH_LITERAL("empty2.txt"));
600 std::wstring shortened_file = data_dir; 599 FilePath shortened_file =
601 file_util::AppendToPath(&shortened_file, L"shortened.txt"); 600 data_dir.Append(FILE_PATH_LITERAL("shortened.txt"));
602 std::wstring binary_file = data_dir; 601 FilePath binary_file =
603 file_util::AppendToPath(&binary_file, L"binary_file.bin"); 602 data_dir.Append(FILE_PATH_LITERAL("binary_file.bin"));
604 std::wstring binary_file_same = data_dir; 603 FilePath binary_file_same =
605 file_util::AppendToPath(&binary_file_same, L"binary_file_same.bin"); 604 data_dir.Append(FILE_PATH_LITERAL("binary_file_same.bin"));
606 std::wstring binary_file_diff = data_dir; 605 FilePath binary_file_diff =
607 file_util::AppendToPath(&binary_file_diff, L"binary_file_diff.bin"); 606 data_dir.Append(FILE_PATH_LITERAL("binary_file_diff.bin"));
608 607
609 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file)); 608 EXPECT_TRUE(file_util::ContentsEqual(original_file, original_file));
610 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file)); 609 EXPECT_TRUE(file_util::ContentsEqual(original_file, same_file));
611 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file)); 610 EXPECT_FALSE(file_util::ContentsEqual(original_file, same_length_file));
612 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file)); 611 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_file));
613 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname")); 612 EXPECT_FALSE(file_util::ContentsEqual(L"bogusname", L"bogusname"));
614 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file)); 613 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_first_file));
615 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file)); 614 EXPECT_FALSE(file_util::ContentsEqual(original_file, different_last_file));
616 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file)); 615 EXPECT_TRUE(file_util::ContentsEqual(empty1_file, empty2_file));
617 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file)); 616 EXPECT_FALSE(file_util::ContentsEqual(original_file, shortened_file));
618 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file)); 617 EXPECT_FALSE(file_util::ContentsEqual(shortened_file, original_file));
619 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same)); 618 EXPECT_TRUE(file_util::ContentsEqual(binary_file, binary_file_same));
620 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff)); 619 EXPECT_FALSE(file_util::ContentsEqual(binary_file, binary_file_diff));
621 } 620 }
622 621
623 // We don't need equivalent functionality outside of Windows. 622 // We don't need equivalent functionality outside of Windows.
624 #if defined(OS_WIN) 623 #if defined(OS_WIN)
625 TEST_F(FileUtilTest, ResolveShortcutTest) { 624 TEST_F(FileUtilTest, ResolveShortcutTest) {
626 std::wstring target_file = test_dir_; 625 FilePath target_file = test_dir_.Append(L"Target.txt");
627 file_util::AppendToPath(&target_file, L"Target.txt");
628 CreateTextFile(target_file, L"This is the target."); 626 CreateTextFile(target_file, L"This is the target.");
629 627
630 std::wstring link_file = test_dir_; 628 FilePath link_file = test_dir_.Append(L"Link.lnk");
631 file_util::AppendToPath(&link_file, L"Link.lnk");
632 629
633 HRESULT result; 630 HRESULT result;
634 IShellLink *shell = NULL; 631 IShellLink *shell = NULL;
635 IPersistFile *persist = NULL; 632 IPersistFile *persist = NULL;
636 633
637 CoInitialize(NULL); 634 CoInitialize(NULL);
638 // Temporarily create a shortcut for test 635 // Temporarily create a shortcut for test
639 result = CoCreateInstance(CLSID_ShellLink, NULL, 636 result = CoCreateInstance(CLSID_ShellLink, NULL,
640 CLSCTX_INPROC_SERVER, IID_IShellLink, 637 CLSCTX_INPROC_SERVER, IID_IShellLink,
641 reinterpret_cast<LPVOID*>(&shell)); 638 reinterpret_cast<LPVOID*>(&shell));
642 EXPECT_TRUE(SUCCEEDED(result)); 639 EXPECT_TRUE(SUCCEEDED(result));
643 result = shell->QueryInterface(IID_IPersistFile, 640 result = shell->QueryInterface(IID_IPersistFile,
644 reinterpret_cast<LPVOID*>(&persist)); 641 reinterpret_cast<LPVOID*>(&persist));
645 EXPECT_TRUE(SUCCEEDED(result)); 642 EXPECT_TRUE(SUCCEEDED(result));
646 result = shell->SetPath(target_file.c_str()); 643 result = shell->SetPath(target_file.value().c_str());
647 EXPECT_TRUE(SUCCEEDED(result)); 644 EXPECT_TRUE(SUCCEEDED(result));
648 result = shell->SetDescription(L"ResolveShortcutTest"); 645 result = shell->SetDescription(L"ResolveShortcutTest");
649 EXPECT_TRUE(SUCCEEDED(result)); 646 EXPECT_TRUE(SUCCEEDED(result));
650 result = persist->Save(link_file.c_str(), TRUE); 647 result = persist->Save(link_file.value().c_str(), TRUE);
651 EXPECT_TRUE(SUCCEEDED(result)); 648 EXPECT_TRUE(SUCCEEDED(result));
652 if (persist) 649 if (persist)
653 persist->Release(); 650 persist->Release();
654 if (shell) 651 if (shell)
655 shell->Release(); 652 shell->Release();
656 653
657 bool is_solved; 654 bool is_solved;
658 is_solved = file_util::ResolveShortcut(&link_file); 655 std::wstring link_file_str = link_file.value();
656 is_solved = file_util::ResolveShortcut(&link_file_str);
659 EXPECT_TRUE(is_solved); 657 EXPECT_TRUE(is_solved);
660 std::wstring contents; 658 std::wstring contents;
661 contents = ReadTextFile(link_file); 659 contents = ReadTextFile(FilePath(link_file_str));
662 EXPECT_EQ(L"This is the target.", contents); 660 EXPECT_EQ(L"This is the target.", contents);
663 661
664 // Cleaning 662 // Cleaning
665 DeleteFile(target_file.c_str()); 663 DeleteFile(target_file.value().c_str());
666 DeleteFile(link_file.c_str()); 664 DeleteFile(link_file_str.c_str());
667 CoUninitialize(); 665 CoUninitialize();
668 } 666 }
669 667
670 TEST_F(FileUtilTest, CreateShortcutTest) { 668 TEST_F(FileUtilTest, CreateShortcutTest) {
671 const wchar_t file_contents[] = L"This is another target."; 669 const wchar_t file_contents[] = L"This is another target.";
672 std::wstring target_file = test_dir_; 670 FilePath target_file = test_dir_.Append(L"Target1.txt");
673 file_util::AppendToPath(&target_file, L"Target1.txt");
674 CreateTextFile(target_file, file_contents); 671 CreateTextFile(target_file, file_contents);
675 672
676 std::wstring link_file = test_dir_; 673 FilePath link_file = test_dir_.Append(L"Link1.lnk");
677 file_util::AppendToPath(&link_file, L"Link1.lnk");
678 674
679 CoInitialize(NULL); 675 CoInitialize(NULL);
680 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.c_str(), 676 EXPECT_TRUE(file_util::CreateShortcutLink(target_file.value().c_str(),
681 link_file.c_str(), 677 link_file.value().c_str(),
682 NULL, NULL, NULL, NULL, 0)); 678 NULL, NULL, NULL, NULL, 0));
683 std::wstring resolved_name = link_file; 679 std::wstring resolved_name = link_file.value();
684 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name)); 680 EXPECT_TRUE(file_util::ResolveShortcut(&resolved_name));
685 std::wstring read_contents = ReadTextFile(resolved_name); 681 std::wstring read_contents = ReadTextFile(FilePath(resolved_name));
686 EXPECT_EQ(file_contents, read_contents); 682 EXPECT_EQ(file_contents, read_contents);
687 683
688 DeleteFile(target_file.c_str()); 684 DeleteFile(target_file.value().c_str());
689 DeleteFile(link_file.c_str()); 685 DeleteFile(link_file.value().c_str());
690 CoUninitialize(); 686 CoUninitialize();
691 } 687 }
692 #endif 688 #endif
693 689
694 TEST_F(FileUtilTest, CreateTemporaryFileNameTest) { 690 TEST_F(FileUtilTest, CreateTemporaryFileNameTest) {
695 std::wstring temp_file; 691 std::wstring temp_file;
696 file_util::CreateTemporaryFileName(&temp_file); 692 file_util::CreateTemporaryFileName(&temp_file);
697 EXPECT_TRUE(file_util::PathExists(temp_file)); 693 EXPECT_TRUE(file_util::PathExists(temp_file));
698 EXPECT_TRUE(file_util::Delete(temp_file, false)); 694 EXPECT_TRUE(file_util::Delete(temp_file, false));
699 } 695 }
700 696
701 TEST_F(FileUtilTest, CreateNewTempDirectoryTest) { 697 TEST_F(FileUtilTest, CreateNewTempDirectoryTest) {
702 std::wstring temp_dir; 698 std::wstring temp_dir;
703 file_util::CreateNewTempDirectory(std::wstring(), &temp_dir); 699 file_util::CreateNewTempDirectory(std::wstring(), &temp_dir);
704 EXPECT_TRUE(file_util::PathExists(temp_dir)); 700 EXPECT_TRUE(file_util::PathExists(temp_dir));
705 EXPECT_TRUE(file_util::Delete(temp_dir, false)); 701 EXPECT_TRUE(file_util::Delete(temp_dir, false));
706 } 702 }
707 703
708 TEST_F(FileUtilTest, CreateDirectoryTest) { 704 TEST_F(FileUtilTest, CreateDirectoryTest) {
709 std::wstring test_root = test_dir_; 705 FilePath test_root =
710 file_util::AppendToPath(&test_root, L"create_directory_test"); 706 test_dir_.Append(FILE_PATH_LITERAL("create_directory_test"));
711 std::wstring test_path(test_root);
712 #if defined(OS_WIN) 707 #if defined(OS_WIN)
713 file_util::AppendToPath(&test_path, L"dir\\tree\\likely\\doesnt\\exist\\"); 708 FilePath test_path =
709 test_root.Append(FILE_PATH_LITERAL("dir\\tree\\likely\\doesnt\\exist\\"));
714 #elif defined(OS_POSIX) 710 #elif defined(OS_POSIX)
715 file_util::AppendToPath(&test_path, L"dir/tree/likely/doesnt/exist/"); 711 FilePath test_path =
712 test_root.Append(FILE_PATH_LITERAL("dir/tree/likely/doesnt/exist/"));
716 #endif 713 #endif
717 714
718 EXPECT_FALSE(file_util::PathExists(test_path)); 715 EXPECT_FALSE(file_util::PathExists(test_path));
719 EXPECT_TRUE(file_util::CreateDirectory(test_path)); 716 EXPECT_TRUE(file_util::CreateDirectory(test_path));
720 EXPECT_TRUE(file_util::PathExists(test_path)); 717 EXPECT_TRUE(file_util::PathExists(test_path));
721 // CreateDirectory returns true if the DirectoryExists returns true. 718 // CreateDirectory returns true if the DirectoryExists returns true.
722 EXPECT_TRUE(file_util::CreateDirectory(test_path)); 719 EXPECT_TRUE(file_util::CreateDirectory(test_path));
723 720
724 // Doesn't work to create it on top of a non-dir 721 // Doesn't work to create it on top of a non-dir
725 file_util::AppendToPath(&test_path, L"foobar.txt"); 722 test_path = test_path.Append(FILE_PATH_LITERAL("foobar.txt"));
726 EXPECT_FALSE(file_util::PathExists(test_path)); 723 EXPECT_FALSE(file_util::PathExists(test_path));
727 CreateTextFile(test_path, L"test file"); 724 CreateTextFile(test_path, L"test file");
728 EXPECT_TRUE(file_util::PathExists(test_path)); 725 EXPECT_TRUE(file_util::PathExists(test_path));
729 EXPECT_FALSE(file_util::CreateDirectory(test_path)); 726 EXPECT_FALSE(file_util::CreateDirectory(test_path));
730 727
731 EXPECT_TRUE(file_util::Delete(test_root, true)); 728 EXPECT_TRUE(file_util::Delete(test_root, true));
732 EXPECT_FALSE(file_util::PathExists(test_root)); 729 EXPECT_FALSE(file_util::PathExists(test_root));
733 EXPECT_FALSE(file_util::PathExists(test_path)); 730 EXPECT_FALSE(file_util::PathExists(test_path));
734 } 731 }
735 732
736 TEST_F(FileUtilTest, DetectDirectoryTest) { 733 TEST_F(FileUtilTest, DetectDirectoryTest) {
737 // Check a directory 734 // Check a directory
738 std::wstring test_root = test_dir_; 735 FilePath test_root =
739 file_util::AppendToPath(&test_root, L"detect_directory_test"); 736 test_dir_.Append(FILE_PATH_LITERAL("detect_directory_test"));
740 EXPECT_FALSE(file_util::PathExists(test_root)); 737 EXPECT_FALSE(file_util::PathExists(test_root));
741 EXPECT_TRUE(file_util::CreateDirectory(test_root)); 738 EXPECT_TRUE(file_util::CreateDirectory(test_root));
742 EXPECT_TRUE(file_util::PathExists(test_root)); 739 EXPECT_TRUE(file_util::PathExists(test_root));
743 EXPECT_TRUE(file_util::DirectoryExists(test_root)); 740 EXPECT_TRUE(file_util::DirectoryExists(test_root));
744 741
745 // Check a file 742 // Check a file
746 std::wstring test_path(test_root); 743 FilePath test_path =
747 file_util::AppendToPath(&test_path, L"foobar.txt"); 744 test_root.Append(FILE_PATH_LITERAL("foobar.txt"));
748 EXPECT_FALSE(file_util::PathExists(test_path)); 745 EXPECT_FALSE(file_util::PathExists(test_path));
749 CreateTextFile(test_path, L"test file"); 746 CreateTextFile(test_path, L"test file");
750 EXPECT_TRUE(file_util::PathExists(test_path)); 747 EXPECT_TRUE(file_util::PathExists(test_path));
751 EXPECT_FALSE(file_util::DirectoryExists(test_path)); 748 EXPECT_FALSE(file_util::DirectoryExists(test_path));
752 EXPECT_TRUE(file_util::Delete(test_path, false)); 749 EXPECT_TRUE(file_util::Delete(test_path, false));
753 750
754 EXPECT_TRUE(file_util::Delete(test_root, true)); 751 EXPECT_TRUE(file_util::Delete(test_root, true));
755 } 752 }
756 753
757 static const struct goodbad_pair { 754 static const struct goodbad_pair {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 file_util::AppendToPath(&path, L"foo.bar"); 823 file_util::AppendToPath(&path, L"foo.bar");
827 file_util::AppendToPath(&path, L"foo"); 824 file_util::AppendToPath(&path, L"foo");
828 // '/foo.bar/foo' with extension '.baz' 825 // '/foo.bar/foo' with extension '.baz'
829 std::wstring result_path = path; 826 std::wstring result_path = path;
830 file_util::ReplaceExtension(&result_path, L".baz"); 827 file_util::ReplaceExtension(&result_path, L".baz");
831 EXPECT_EQ(path + L".baz", result_path); 828 EXPECT_EQ(path + L".baz", result_path);
832 } 829 }
833 830
834 TEST_F(FileUtilTest, FileEnumeratorTest) { 831 TEST_F(FileUtilTest, FileEnumeratorTest) {
835 // Test an empty directory. 832 // Test an empty directory.
836 file_util::FileEnumerator f0(test_dir_, true, 833 file_util::FileEnumerator f0(test_dir_.ToWStringHack(), true,
837 file_util::FileEnumerator::FILES_AND_DIRECTORIES); 834 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
838 EXPECT_EQ(f0.Next(), L""); 835 EXPECT_EQ(f0.Next(), L"");
839 EXPECT_EQ(f0.Next(), L""); 836 EXPECT_EQ(f0.Next(), L"");
840 837
841 // create the directories 838 // create the directories
842 std::wstring dir1 = test_dir_; 839 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1"));
843 file_util::AppendToPath(&dir1, L"dir1");
844 EXPECT_TRUE(file_util::CreateDirectory(dir1)); 840 EXPECT_TRUE(file_util::CreateDirectory(dir1));
845 std::wstring dir2 = test_dir_; 841 FilePath dir2 = test_dir_.Append(FILE_PATH_LITERAL("dir2"));
846 file_util::AppendToPath(&dir2, L"dir2");
847 EXPECT_TRUE(file_util::CreateDirectory(dir2)); 842 EXPECT_TRUE(file_util::CreateDirectory(dir2));
848 std::wstring dir2inner = dir2; 843 FilePath dir2inner = dir2.Append(FILE_PATH_LITERAL("inner"));
849 file_util::AppendToPath(&dir2inner, L"inner");
850 EXPECT_TRUE(file_util::CreateDirectory(dir2inner)); 844 EXPECT_TRUE(file_util::CreateDirectory(dir2inner));
851 845
852 // create the files 846 // create the files
853 std::wstring dir2file = dir2; 847 FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
854 file_util::AppendToPath(&dir2file, L"dir2file.txt");
855 CreateTextFile(dir2file, L""); 848 CreateTextFile(dir2file, L"");
856 std::wstring dir2innerfile = dir2inner; 849 FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
857 file_util::AppendToPath(&dir2innerfile, L"innerfile.txt");
858 CreateTextFile(dir2innerfile, L""); 850 CreateTextFile(dir2innerfile, L"");
859 std::wstring file1 = test_dir_; 851 FilePath file1 = test_dir_.Append(FILE_PATH_LITERAL("file1.txt"));
860 file_util::AppendToPath(&file1, L"file1.txt");
861 CreateTextFile(file1, L""); 852 CreateTextFile(file1, L"");
862 std::wstring file2_rel = dir2; 853 FilePath file2_rel =
863 file_util::AppendToPath(&file2_rel, L".."); 854 dir2.Append(FilePath::kParentDirectory)
864 file_util::AppendToPath(&file2_rel, L"file2.txt"); 855 .Append(FILE_PATH_LITERAL("file2.txt"));
865 CreateTextFile(file2_rel, L""); 856 CreateTextFile(file2_rel, L"");
866 std::wstring file2_abs = test_dir_; 857 FilePath file2_abs = test_dir_.Append(FILE_PATH_LITERAL("file2.txt"));
867 file_util::AppendToPath(&file2_abs, L"file2.txt");
868 858
869 // Only enumerate files. 859 // Only enumerate files.
870 file_util::FileEnumerator f1(test_dir_, true, 860 file_util::FileEnumerator f1(test_dir_.ToWStringHack(), true,
871 file_util::FileEnumerator::FILES); 861 file_util::FileEnumerator::FILES);
872 FindResultCollector c1(f1); 862 FindResultCollector c1(f1);
873 EXPECT_TRUE(c1.HasFile(file1)); 863 EXPECT_TRUE(c1.HasFile(file1));
874 EXPECT_TRUE(c1.HasFile(file2_abs)); 864 EXPECT_TRUE(c1.HasFile(file2_abs));
875 EXPECT_TRUE(c1.HasFile(dir2file)); 865 EXPECT_TRUE(c1.HasFile(dir2file));
876 EXPECT_TRUE(c1.HasFile(dir2innerfile)); 866 EXPECT_TRUE(c1.HasFile(dir2innerfile));
877 EXPECT_EQ(c1.size(), 4); 867 EXPECT_EQ(c1.size(), 4);
878 868
879 // Only enumerate directories. 869 // Only enumerate directories.
880 file_util::FileEnumerator f2(test_dir_, true, 870 file_util::FileEnumerator f2(test_dir_.ToWStringHack(), true,
881 file_util::FileEnumerator::DIRECTORIES); 871 file_util::FileEnumerator::DIRECTORIES);
882 FindResultCollector c2(f2); 872 FindResultCollector c2(f2);
883 EXPECT_TRUE(c2.HasFile(dir1)); 873 EXPECT_TRUE(c2.HasFile(dir1));
884 EXPECT_TRUE(c2.HasFile(dir2)); 874 EXPECT_TRUE(c2.HasFile(dir2));
885 EXPECT_TRUE(c2.HasFile(dir2inner)); 875 EXPECT_TRUE(c2.HasFile(dir2inner));
886 EXPECT_EQ(c2.size(), 3); 876 EXPECT_EQ(c2.size(), 3);
887 877
888 // Only enumerate directories non-recursively. 878 // Only enumerate directories non-recursively.
889 file_util::FileEnumerator f2_non_recursive( 879 file_util::FileEnumerator f2_non_recursive(
890 test_dir_, false, file_util::FileEnumerator::DIRECTORIES); 880 test_dir_.ToWStringHack(), false, file_util::FileEnumerator::DIRECTORIES);
891 FindResultCollector c2_non_recursive(f2_non_recursive); 881 FindResultCollector c2_non_recursive(f2_non_recursive);
892 EXPECT_TRUE(c2_non_recursive.HasFile(dir1)); 882 EXPECT_TRUE(c2_non_recursive.HasFile(dir1));
893 EXPECT_TRUE(c2_non_recursive.HasFile(dir2)); 883 EXPECT_TRUE(c2_non_recursive.HasFile(dir2));
894 EXPECT_EQ(c2_non_recursive.size(), 2); 884 EXPECT_EQ(c2_non_recursive.size(), 2);
895 885
896 // Enumerate files and directories. 886 // Enumerate files and directories.
897 file_util::FileEnumerator f3(test_dir_, true, 887 file_util::FileEnumerator f3(test_dir_.ToWStringHack(), true,
898 file_util::FileEnumerator::FILES_AND_DIRECTORIES); 888 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
899 FindResultCollector c3(f3); 889 FindResultCollector c3(f3);
900 EXPECT_TRUE(c3.HasFile(dir1)); 890 EXPECT_TRUE(c3.HasFile(dir1));
901 EXPECT_TRUE(c3.HasFile(dir2)); 891 EXPECT_TRUE(c3.HasFile(dir2));
902 EXPECT_TRUE(c3.HasFile(file1)); 892 EXPECT_TRUE(c3.HasFile(file1));
903 EXPECT_TRUE(c3.HasFile(file2_abs)); 893 EXPECT_TRUE(c3.HasFile(file2_abs));
904 EXPECT_TRUE(c3.HasFile(dir2file)); 894 EXPECT_TRUE(c3.HasFile(dir2file));
905 EXPECT_TRUE(c3.HasFile(dir2inner)); 895 EXPECT_TRUE(c3.HasFile(dir2inner));
906 EXPECT_TRUE(c3.HasFile(dir2innerfile)); 896 EXPECT_TRUE(c3.HasFile(dir2innerfile));
907 EXPECT_EQ(c3.size(), 7); 897 EXPECT_EQ(c3.size(), 7);
908 898
909 // Non-recursive operation. 899 // Non-recursive operation.
910 file_util::FileEnumerator f4(test_dir_, false, 900 file_util::FileEnumerator f4(test_dir_.ToWStringHack(), false,
911 file_util::FileEnumerator::FILES_AND_DIRECTORIES); 901 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
912 FindResultCollector c4(f4); 902 FindResultCollector c4(f4);
913 EXPECT_TRUE(c4.HasFile(dir2)); 903 EXPECT_TRUE(c4.HasFile(dir2));
914 EXPECT_TRUE(c4.HasFile(dir2)); 904 EXPECT_TRUE(c4.HasFile(dir2));
915 EXPECT_TRUE(c4.HasFile(file1)); 905 EXPECT_TRUE(c4.HasFile(file1));
916 EXPECT_TRUE(c4.HasFile(file2_abs)); 906 EXPECT_TRUE(c4.HasFile(file2_abs));
917 EXPECT_EQ(c4.size(), 4); 907 EXPECT_EQ(c4.size(), 4);
918 908
919 // Enumerate with a pattern. 909 // Enumerate with a pattern.
920 file_util::FileEnumerator f5(test_dir_, true, 910 file_util::FileEnumerator f5(test_dir_.ToWStringHack(), true,
921 file_util::FileEnumerator::FILES_AND_DIRECTORIES, L"dir*"); 911 file_util::FileEnumerator::FILES_AND_DIRECTORIES, L"dir*");
922 FindResultCollector c5(f5); 912 FindResultCollector c5(f5);
923 EXPECT_TRUE(c5.HasFile(dir1)); 913 EXPECT_TRUE(c5.HasFile(dir1));
924 EXPECT_TRUE(c5.HasFile(dir2)); 914 EXPECT_TRUE(c5.HasFile(dir2));
925 EXPECT_TRUE(c5.HasFile(dir2file)); 915 EXPECT_TRUE(c5.HasFile(dir2file));
926 EXPECT_TRUE(c5.HasFile(dir2inner)); 916 EXPECT_TRUE(c5.HasFile(dir2inner));
927 EXPECT_TRUE(c5.HasFile(dir2innerfile)); 917 EXPECT_TRUE(c5.HasFile(dir2innerfile));
928 EXPECT_EQ(c5.size(), 5); 918 EXPECT_EQ(c5.size(), 5);
929 919
930 // Make sure the destructor closes the find handle while in the middle of a 920 // Make sure the destructor closes the find handle while in the middle of a
931 // query to allow TearDown to delete the directory. 921 // query to allow TearDown to delete the directory.
932 file_util::FileEnumerator f6(test_dir_, true, 922 file_util::FileEnumerator f6(test_dir_.ToWStringHack(), true,
933 file_util::FileEnumerator::FILES_AND_DIRECTORIES); 923 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
934 EXPECT_FALSE(f6.Next().empty()); // Should have found something 924 EXPECT_FALSE(f6.Next().empty()); // Should have found something
935 // (we don't care what). 925 // (we don't care what).
936 } 926 }
937 927
938 } // namespace 928 } // namespace
OLDNEW
« no previous file with comments | « base/file_util_posix.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698