| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/files/file_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <aclapi.h> | 9 #include <aclapi.h> |
| 10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 collector_ = new NotificationCollector(); | 169 collector_ = new NotificationCollector(); |
| 170 } | 170 } |
| 171 | 171 |
| 172 void TearDown() override { RunLoop().RunUntilIdle(); } | 172 void TearDown() override { RunLoop().RunUntilIdle(); } |
| 173 | 173 |
| 174 void DeleteDelegateOnFileThread(TestDelegate* delegate) { | 174 void DeleteDelegateOnFileThread(TestDelegate* delegate) { |
| 175 file_thread_.task_runner()->DeleteSoon(FROM_HERE, delegate); | 175 file_thread_.task_runner()->DeleteSoon(FROM_HERE, delegate); |
| 176 } | 176 } |
| 177 | 177 |
| 178 FilePath test_file() { | 178 FilePath test_file() { |
| 179 return temp_dir_.path().AppendASCII("FilePathWatcherTest"); | 179 return temp_dir_.GetPath().AppendASCII("FilePathWatcherTest"); |
| 180 } | 180 } |
| 181 | 181 |
| 182 FilePath test_link() { | 182 FilePath test_link() { |
| 183 return temp_dir_.path().AppendASCII("FilePathWatcherTest.lnk"); | 183 return temp_dir_.GetPath().AppendASCII("FilePathWatcherTest.lnk"); |
| 184 } | 184 } |
| 185 | 185 |
| 186 // Write |content| to |file|. Returns true on success. | 186 // Write |content| to |file|. Returns true on success. |
| 187 bool WriteFile(const FilePath& file, const std::string& content) { | 187 bool WriteFile(const FilePath& file, const std::string& content) { |
| 188 int write_size = ::base::WriteFile(file, content.c_str(), content.length()); | 188 int write_size = ::base::WriteFile(file, content.c_str(), content.length()); |
| 189 return write_size == static_cast<int>(content.length()); | 189 return write_size == static_cast<int>(content.length()); |
| 190 } | 190 } |
| 191 | 191 |
| 192 bool SetupWatch(const FilePath& target, | 192 bool SetupWatch(const FilePath& target, |
| 193 FilePathWatcher* watcher, | 193 FilePathWatcher* watcher, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 249 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 250 | 250 |
| 251 // Now make sure we get notified if the file is modified. | 251 // Now make sure we get notified if the file is modified. |
| 252 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 252 ASSERT_TRUE(WriteFile(test_file(), "new content")); |
| 253 ASSERT_TRUE(WaitForEvents()); | 253 ASSERT_TRUE(WaitForEvents()); |
| 254 DeleteDelegateOnFileThread(delegate.release()); | 254 DeleteDelegateOnFileThread(delegate.release()); |
| 255 } | 255 } |
| 256 | 256 |
| 257 // Verify that moving the file into place is caught. | 257 // Verify that moving the file into place is caught. |
| 258 TEST_F(FilePathWatcherTest, MovedFile) { | 258 TEST_F(FilePathWatcherTest, MovedFile) { |
| 259 FilePath source_file(temp_dir_.path().AppendASCII("source")); | 259 FilePath source_file(temp_dir_.GetPath().AppendASCII("source")); |
| 260 ASSERT_TRUE(WriteFile(source_file, "content")); | 260 ASSERT_TRUE(WriteFile(source_file, "content")); |
| 261 | 261 |
| 262 FilePathWatcher watcher; | 262 FilePathWatcher watcher; |
| 263 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 263 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 264 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 264 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 265 | 265 |
| 266 // Now make sure we get notified if the file is modified. | 266 // Now make sure we get notified if the file is modified. |
| 267 ASSERT_TRUE(base::Move(source_file, test_file())); | 267 ASSERT_TRUE(base::Move(source_file, test_file())); |
| 268 ASSERT_TRUE(WaitForEvents()); | 268 ASSERT_TRUE(WaitForEvents()); |
| 269 DeleteDelegateOnFileThread(delegate.release()); | 269 DeleteDelegateOnFileThread(delegate.release()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 ASSERT_TRUE(WriteFile(test_file(), "content")); | 344 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 345 ASSERT_TRUE(WaitForEvents()); | 345 ASSERT_TRUE(WaitForEvents()); |
| 346 DeleteDelegateOnFileThread(delegate1.release()); | 346 DeleteDelegateOnFileThread(delegate1.release()); |
| 347 DeleteDelegateOnFileThread(delegate2.release()); | 347 DeleteDelegateOnFileThread(delegate2.release()); |
| 348 } | 348 } |
| 349 | 349 |
| 350 // Verify that watching a file whose parent directory doesn't exist yet works if | 350 // Verify that watching a file whose parent directory doesn't exist yet works if |
| 351 // the directory and file are created eventually. | 351 // the directory and file are created eventually. |
| 352 TEST_F(FilePathWatcherTest, NonExistentDirectory) { | 352 TEST_F(FilePathWatcherTest, NonExistentDirectory) { |
| 353 FilePathWatcher watcher; | 353 FilePathWatcher watcher; |
| 354 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 354 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
| 355 FilePath file(dir.AppendASCII("file")); | 355 FilePath file(dir.AppendASCII("file")); |
| 356 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 356 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 357 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); | 357 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); |
| 358 | 358 |
| 359 ASSERT_TRUE(base::CreateDirectory(dir)); | 359 ASSERT_TRUE(base::CreateDirectory(dir)); |
| 360 | 360 |
| 361 ASSERT_TRUE(WriteFile(file, "content")); | 361 ASSERT_TRUE(WriteFile(file, "content")); |
| 362 | 362 |
| 363 VLOG(1) << "Waiting for file creation"; | 363 VLOG(1) << "Waiting for file creation"; |
| 364 ASSERT_TRUE(WaitForEvents()); | 364 ASSERT_TRUE(WaitForEvents()); |
| 365 | 365 |
| 366 ASSERT_TRUE(WriteFile(file, "content v2")); | 366 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 367 VLOG(1) << "Waiting for file change"; | 367 VLOG(1) << "Waiting for file change"; |
| 368 ASSERT_TRUE(WaitForEvents()); | 368 ASSERT_TRUE(WaitForEvents()); |
| 369 | 369 |
| 370 ASSERT_TRUE(base::DeleteFile(file, false)); | 370 ASSERT_TRUE(base::DeleteFile(file, false)); |
| 371 VLOG(1) << "Waiting for file deletion"; | 371 VLOG(1) << "Waiting for file deletion"; |
| 372 ASSERT_TRUE(WaitForEvents()); | 372 ASSERT_TRUE(WaitForEvents()); |
| 373 DeleteDelegateOnFileThread(delegate.release()); | 373 DeleteDelegateOnFileThread(delegate.release()); |
| 374 } | 374 } |
| 375 | 375 |
| 376 // Exercises watch reconfiguration for the case that directories on the path | 376 // Exercises watch reconfiguration for the case that directories on the path |
| 377 // are rapidly created. | 377 // are rapidly created. |
| 378 TEST_F(FilePathWatcherTest, DirectoryChain) { | 378 TEST_F(FilePathWatcherTest, DirectoryChain) { |
| 379 FilePath path(temp_dir_.path()); | 379 FilePath path(temp_dir_.GetPath()); |
| 380 std::vector<std::string> dir_names; | 380 std::vector<std::string> dir_names; |
| 381 for (int i = 0; i < 20; i++) { | 381 for (int i = 0; i < 20; i++) { |
| 382 std::string dir(base::StringPrintf("d%d", i)); | 382 std::string dir(base::StringPrintf("d%d", i)); |
| 383 dir_names.push_back(dir); | 383 dir_names.push_back(dir); |
| 384 path = path.AppendASCII(dir); | 384 path = path.AppendASCII(dir); |
| 385 } | 385 } |
| 386 | 386 |
| 387 FilePathWatcher watcher; | 387 FilePathWatcher watcher; |
| 388 FilePath file(path.AppendASCII("file")); | 388 FilePath file(path.AppendASCII("file")); |
| 389 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 389 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 390 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); | 390 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); |
| 391 | 391 |
| 392 FilePath sub_path(temp_dir_.path()); | 392 FilePath sub_path(temp_dir_.GetPath()); |
| 393 for (std::vector<std::string>::const_iterator d(dir_names.begin()); | 393 for (std::vector<std::string>::const_iterator d(dir_names.begin()); |
| 394 d != dir_names.end(); ++d) { | 394 d != dir_names.end(); ++d) { |
| 395 sub_path = sub_path.AppendASCII(*d); | 395 sub_path = sub_path.AppendASCII(*d); |
| 396 ASSERT_TRUE(base::CreateDirectory(sub_path)); | 396 ASSERT_TRUE(base::CreateDirectory(sub_path)); |
| 397 } | 397 } |
| 398 VLOG(1) << "Create File"; | 398 VLOG(1) << "Create File"; |
| 399 ASSERT_TRUE(WriteFile(file, "content")); | 399 ASSERT_TRUE(WriteFile(file, "content")); |
| 400 VLOG(1) << "Waiting for file creation"; | 400 VLOG(1) << "Waiting for file creation"; |
| 401 ASSERT_TRUE(WaitForEvents()); | 401 ASSERT_TRUE(WaitForEvents()); |
| 402 | 402 |
| 403 ASSERT_TRUE(WriteFile(file, "content v2")); | 403 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 404 VLOG(1) << "Waiting for file modification"; | 404 VLOG(1) << "Waiting for file modification"; |
| 405 ASSERT_TRUE(WaitForEvents()); | 405 ASSERT_TRUE(WaitForEvents()); |
| 406 DeleteDelegateOnFileThread(delegate.release()); | 406 DeleteDelegateOnFileThread(delegate.release()); |
| 407 } | 407 } |
| 408 | 408 |
| 409 #if defined(OS_MACOSX) | 409 #if defined(OS_MACOSX) |
| 410 // http://crbug.com/85930 | 410 // http://crbug.com/85930 |
| 411 #define DisappearingDirectory DISABLED_DisappearingDirectory | 411 #define DisappearingDirectory DISABLED_DisappearingDirectory |
| 412 #endif | 412 #endif |
| 413 TEST_F(FilePathWatcherTest, DisappearingDirectory) { | 413 TEST_F(FilePathWatcherTest, DisappearingDirectory) { |
| 414 FilePathWatcher watcher; | 414 FilePathWatcher watcher; |
| 415 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 415 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
| 416 FilePath file(dir.AppendASCII("file")); | 416 FilePath file(dir.AppendASCII("file")); |
| 417 ASSERT_TRUE(base::CreateDirectory(dir)); | 417 ASSERT_TRUE(base::CreateDirectory(dir)); |
| 418 ASSERT_TRUE(WriteFile(file, "content")); | 418 ASSERT_TRUE(WriteFile(file, "content")); |
| 419 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 419 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 420 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); | 420 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); |
| 421 | 421 |
| 422 ASSERT_TRUE(base::DeleteFile(dir, true)); | 422 ASSERT_TRUE(base::DeleteFile(dir, true)); |
| 423 ASSERT_TRUE(WaitForEvents()); | 423 ASSERT_TRUE(WaitForEvents()); |
| 424 DeleteDelegateOnFileThread(delegate.release()); | 424 DeleteDelegateOnFileThread(delegate.release()); |
| 425 } | 425 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 436 ASSERT_TRUE(WaitForEvents()); | 436 ASSERT_TRUE(WaitForEvents()); |
| 437 | 437 |
| 438 ASSERT_TRUE(WriteFile(test_file(), "content")); | 438 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 439 VLOG(1) << "Waiting for file creation"; | 439 VLOG(1) << "Waiting for file creation"; |
| 440 ASSERT_TRUE(WaitForEvents()); | 440 ASSERT_TRUE(WaitForEvents()); |
| 441 DeleteDelegateOnFileThread(delegate.release()); | 441 DeleteDelegateOnFileThread(delegate.release()); |
| 442 } | 442 } |
| 443 | 443 |
| 444 TEST_F(FilePathWatcherTest, WatchDirectory) { | 444 TEST_F(FilePathWatcherTest, WatchDirectory) { |
| 445 FilePathWatcher watcher; | 445 FilePathWatcher watcher; |
| 446 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 446 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
| 447 FilePath file1(dir.AppendASCII("file1")); | 447 FilePath file1(dir.AppendASCII("file1")); |
| 448 FilePath file2(dir.AppendASCII("file2")); | 448 FilePath file2(dir.AppendASCII("file2")); |
| 449 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 449 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 450 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), false)); | 450 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), false)); |
| 451 | 451 |
| 452 ASSERT_TRUE(base::CreateDirectory(dir)); | 452 ASSERT_TRUE(base::CreateDirectory(dir)); |
| 453 VLOG(1) << "Waiting for directory creation"; | 453 VLOG(1) << "Waiting for directory creation"; |
| 454 ASSERT_TRUE(WaitForEvents()); | 454 ASSERT_TRUE(WaitForEvents()); |
| 455 | 455 |
| 456 ASSERT_TRUE(WriteFile(file1, "content")); | 456 ASSERT_TRUE(WriteFile(file1, "content")); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 470 | 470 |
| 471 ASSERT_TRUE(WriteFile(file2, "content")); | 471 ASSERT_TRUE(WriteFile(file2, "content")); |
| 472 VLOG(1) << "Waiting for file2 creation"; | 472 VLOG(1) << "Waiting for file2 creation"; |
| 473 ASSERT_TRUE(WaitForEvents()); | 473 ASSERT_TRUE(WaitForEvents()); |
| 474 DeleteDelegateOnFileThread(delegate.release()); | 474 DeleteDelegateOnFileThread(delegate.release()); |
| 475 } | 475 } |
| 476 | 476 |
| 477 TEST_F(FilePathWatcherTest, MoveParent) { | 477 TEST_F(FilePathWatcherTest, MoveParent) { |
| 478 FilePathWatcher file_watcher; | 478 FilePathWatcher file_watcher; |
| 479 FilePathWatcher subdir_watcher; | 479 FilePathWatcher subdir_watcher; |
| 480 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 480 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
| 481 FilePath dest(temp_dir_.path().AppendASCII("dest")); | 481 FilePath dest(temp_dir_.GetPath().AppendASCII("dest")); |
| 482 FilePath subdir(dir.AppendASCII("subdir")); | 482 FilePath subdir(dir.AppendASCII("subdir")); |
| 483 FilePath file(subdir.AppendASCII("file")); | 483 FilePath file(subdir.AppendASCII("file")); |
| 484 std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 484 std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
| 485 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get(), false)); | 485 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get(), false)); |
| 486 std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 486 std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
| 487 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get(), | 487 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get(), |
| 488 false)); | 488 false)); |
| 489 | 489 |
| 490 // Setup a directory hierarchy. | 490 // Setup a directory hierarchy. |
| 491 ASSERT_TRUE(base::CreateDirectory(subdir)); | 491 ASSERT_TRUE(base::CreateDirectory(subdir)); |
| 492 ASSERT_TRUE(WriteFile(file, "content")); | 492 ASSERT_TRUE(WriteFile(file, "content")); |
| 493 VLOG(1) << "Waiting for file creation"; | 493 VLOG(1) << "Waiting for file creation"; |
| 494 ASSERT_TRUE(WaitForEvents()); | 494 ASSERT_TRUE(WaitForEvents()); |
| 495 | 495 |
| 496 // Move the parent directory. | 496 // Move the parent directory. |
| 497 base::Move(dir, dest); | 497 base::Move(dir, dest); |
| 498 VLOG(1) << "Waiting for directory move"; | 498 VLOG(1) << "Waiting for directory move"; |
| 499 ASSERT_TRUE(WaitForEvents()); | 499 ASSERT_TRUE(WaitForEvents()); |
| 500 DeleteDelegateOnFileThread(file_delegate.release()); | 500 DeleteDelegateOnFileThread(file_delegate.release()); |
| 501 DeleteDelegateOnFileThread(subdir_delegate.release()); | 501 DeleteDelegateOnFileThread(subdir_delegate.release()); |
| 502 } | 502 } |
| 503 | 503 |
| 504 TEST_F(FilePathWatcherTest, RecursiveWatch) { | 504 TEST_F(FilePathWatcherTest, RecursiveWatch) { |
| 505 FilePathWatcher watcher; | 505 FilePathWatcher watcher; |
| 506 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 506 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
| 507 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 507 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 508 bool setup_result = SetupWatch(dir, &watcher, delegate.get(), true); | 508 bool setup_result = SetupWatch(dir, &watcher, delegate.get(), true); |
| 509 if (!FilePathWatcher::RecursiveWatchAvailable()) { | 509 if (!FilePathWatcher::RecursiveWatchAvailable()) { |
| 510 ASSERT_FALSE(setup_result); | 510 ASSERT_FALSE(setup_result); |
| 511 DeleteDelegateOnFileThread(delegate.release()); | 511 DeleteDelegateOnFileThread(delegate.release()); |
| 512 return; | 512 return; |
| 513 } | 513 } |
| 514 ASSERT_TRUE(setup_result); | 514 ASSERT_TRUE(setup_result); |
| 515 | 515 |
| 516 // Main directory("dir") creation. | 516 // Main directory("dir") creation. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 // would be preferable and allow testing file attributes and symlinks. | 574 // would be preferable and allow testing file attributes and symlinks. |
| 575 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places | 575 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places |
| 576 // the |temp_dir_| in /data. | 576 // the |temp_dir_| in /data. |
| 577 #define RecursiveWithSymLink DISABLED_RecursiveWithSymLink | 577 #define RecursiveWithSymLink DISABLED_RecursiveWithSymLink |
| 578 #endif // defined(OS_ANDROID) | 578 #endif // defined(OS_ANDROID) |
| 579 TEST_F(FilePathWatcherTest, RecursiveWithSymLink) { | 579 TEST_F(FilePathWatcherTest, RecursiveWithSymLink) { |
| 580 if (!FilePathWatcher::RecursiveWatchAvailable()) | 580 if (!FilePathWatcher::RecursiveWatchAvailable()) |
| 581 return; | 581 return; |
| 582 | 582 |
| 583 FilePathWatcher watcher; | 583 FilePathWatcher watcher; |
| 584 FilePath test_dir(temp_dir_.path().AppendASCII("test_dir")); | 584 FilePath test_dir(temp_dir_.GetPath().AppendASCII("test_dir")); |
| 585 ASSERT_TRUE(base::CreateDirectory(test_dir)); | 585 ASSERT_TRUE(base::CreateDirectory(test_dir)); |
| 586 FilePath symlink(test_dir.AppendASCII("symlink")); | 586 FilePath symlink(test_dir.AppendASCII("symlink")); |
| 587 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 587 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 588 ASSERT_TRUE(SetupWatch(symlink, &watcher, delegate.get(), true)); | 588 ASSERT_TRUE(SetupWatch(symlink, &watcher, delegate.get(), true)); |
| 589 | 589 |
| 590 // Link creation. | 590 // Link creation. |
| 591 FilePath target1(temp_dir_.path().AppendASCII("target1")); | 591 FilePath target1(temp_dir_.GetPath().AppendASCII("target1")); |
| 592 ASSERT_TRUE(base::CreateSymbolicLink(target1, symlink)); | 592 ASSERT_TRUE(base::CreateSymbolicLink(target1, symlink)); |
| 593 ASSERT_TRUE(WaitForEvents()); | 593 ASSERT_TRUE(WaitForEvents()); |
| 594 | 594 |
| 595 // Target1 creation. | 595 // Target1 creation. |
| 596 ASSERT_TRUE(base::CreateDirectory(target1)); | 596 ASSERT_TRUE(base::CreateDirectory(target1)); |
| 597 ASSERT_TRUE(WaitForEvents()); | 597 ASSERT_TRUE(WaitForEvents()); |
| 598 | 598 |
| 599 // Create a file in target1. | 599 // Create a file in target1. |
| 600 FilePath target1_file(target1.AppendASCII("file")); | 600 FilePath target1_file(target1.AppendASCII("file")); |
| 601 ASSERT_TRUE(WriteFile(target1_file, "content")); | 601 ASSERT_TRUE(WriteFile(target1_file, "content")); |
| 602 ASSERT_TRUE(WaitForEvents()); | 602 ASSERT_TRUE(WaitForEvents()); |
| 603 | 603 |
| 604 // Link change. | 604 // Link change. |
| 605 FilePath target2(temp_dir_.path().AppendASCII("target2")); | 605 FilePath target2(temp_dir_.GetPath().AppendASCII("target2")); |
| 606 ASSERT_TRUE(base::CreateDirectory(target2)); | 606 ASSERT_TRUE(base::CreateDirectory(target2)); |
| 607 ASSERT_TRUE(base::DeleteFile(symlink, false)); | 607 ASSERT_TRUE(base::DeleteFile(symlink, false)); |
| 608 ASSERT_TRUE(base::CreateSymbolicLink(target2, symlink)); | 608 ASSERT_TRUE(base::CreateSymbolicLink(target2, symlink)); |
| 609 ASSERT_TRUE(WaitForEvents()); | 609 ASSERT_TRUE(WaitForEvents()); |
| 610 | 610 |
| 611 // Create a file in target2. | 611 // Create a file in target2. |
| 612 FilePath target2_file(target2.AppendASCII("file")); | 612 FilePath target2_file(target2.AppendASCII("file")); |
| 613 ASSERT_TRUE(WriteFile(target2_file, "content")); | 613 ASSERT_TRUE(WriteFile(target2_file, "content")); |
| 614 ASSERT_TRUE(WaitForEvents()); | 614 ASSERT_TRUE(WaitForEvents()); |
| 615 | 615 |
| 616 DeleteDelegateOnFileThread(delegate.release()); | 616 DeleteDelegateOnFileThread(delegate.release()); |
| 617 } | 617 } |
| 618 #endif // OS_POSIX | 618 #endif // OS_POSIX |
| 619 | 619 |
| 620 TEST_F(FilePathWatcherTest, MoveChild) { | 620 TEST_F(FilePathWatcherTest, MoveChild) { |
| 621 FilePathWatcher file_watcher; | 621 FilePathWatcher file_watcher; |
| 622 FilePathWatcher subdir_watcher; | 622 FilePathWatcher subdir_watcher; |
| 623 FilePath source_dir(temp_dir_.path().AppendASCII("source")); | 623 FilePath source_dir(temp_dir_.GetPath().AppendASCII("source")); |
| 624 FilePath source_subdir(source_dir.AppendASCII("subdir")); | 624 FilePath source_subdir(source_dir.AppendASCII("subdir")); |
| 625 FilePath source_file(source_subdir.AppendASCII("file")); | 625 FilePath source_file(source_subdir.AppendASCII("file")); |
| 626 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); | 626 FilePath dest_dir(temp_dir_.GetPath().AppendASCII("dest")); |
| 627 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); | 627 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); |
| 628 FilePath dest_file(dest_subdir.AppendASCII("file")); | 628 FilePath dest_file(dest_subdir.AppendASCII("file")); |
| 629 | 629 |
| 630 // Setup a directory hierarchy. | 630 // Setup a directory hierarchy. |
| 631 ASSERT_TRUE(base::CreateDirectory(source_subdir)); | 631 ASSERT_TRUE(base::CreateDirectory(source_subdir)); |
| 632 ASSERT_TRUE(WriteFile(source_file, "content")); | 632 ASSERT_TRUE(WriteFile(source_file, "content")); |
| 633 | 633 |
| 634 std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 634 std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
| 635 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get(), false)); | 635 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get(), false)); |
| 636 std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 636 std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 // Now make sure we get notified if the target file is deleted. | 741 // Now make sure we get notified if the target file is deleted. |
| 742 ASSERT_TRUE(base::DeleteFile(test_file(), false)); | 742 ASSERT_TRUE(base::DeleteFile(test_file(), false)); |
| 743 ASSERT_TRUE(WaitForEvents()); | 743 ASSERT_TRUE(WaitForEvents()); |
| 744 DeleteDelegateOnFileThread(delegate.release()); | 744 DeleteDelegateOnFileThread(delegate.release()); |
| 745 } | 745 } |
| 746 | 746 |
| 747 // Verify that watching a file whose parent directory is a link that | 747 // Verify that watching a file whose parent directory is a link that |
| 748 // doesn't exist yet works if the symlink is created eventually. | 748 // doesn't exist yet works if the symlink is created eventually. |
| 749 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { | 749 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { |
| 750 FilePathWatcher watcher; | 750 FilePathWatcher watcher; |
| 751 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 751 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
| 752 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 752 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); |
| 753 FilePath file(dir.AppendASCII("file")); | 753 FilePath file(dir.AppendASCII("file")); |
| 754 FilePath linkfile(link_dir.AppendASCII("file")); | 754 FilePath linkfile(link_dir.AppendASCII("file")); |
| 755 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 755 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 756 // dir/file should exist. | 756 // dir/file should exist. |
| 757 ASSERT_TRUE(base::CreateDirectory(dir)); | 757 ASSERT_TRUE(base::CreateDirectory(dir)); |
| 758 ASSERT_TRUE(WriteFile(file, "content")); | 758 ASSERT_TRUE(WriteFile(file, "content")); |
| 759 // Note that we are watching dir.lnk/file which doesn't exist yet. | 759 // Note that we are watching dir.lnk/file which doesn't exist yet. |
| 760 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 760 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
| 761 | 761 |
| 762 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); | 762 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
| 763 VLOG(1) << "Waiting for link creation"; | 763 VLOG(1) << "Waiting for link creation"; |
| 764 ASSERT_TRUE(WaitForEvents()); | 764 ASSERT_TRUE(WaitForEvents()); |
| 765 | 765 |
| 766 ASSERT_TRUE(WriteFile(file, "content v2")); | 766 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 767 VLOG(1) << "Waiting for file change"; | 767 VLOG(1) << "Waiting for file change"; |
| 768 ASSERT_TRUE(WaitForEvents()); | 768 ASSERT_TRUE(WaitForEvents()); |
| 769 | 769 |
| 770 ASSERT_TRUE(base::DeleteFile(file, false)); | 770 ASSERT_TRUE(base::DeleteFile(file, false)); |
| 771 VLOG(1) << "Waiting for file deletion"; | 771 VLOG(1) << "Waiting for file deletion"; |
| 772 ASSERT_TRUE(WaitForEvents()); | 772 ASSERT_TRUE(WaitForEvents()); |
| 773 DeleteDelegateOnFileThread(delegate.release()); | 773 DeleteDelegateOnFileThread(delegate.release()); |
| 774 } | 774 } |
| 775 | 775 |
| 776 // Verify that watching a file whose parent directory is a | 776 // Verify that watching a file whose parent directory is a |
| 777 // dangling symlink works if the directory is created eventually. | 777 // dangling symlink works if the directory is created eventually. |
| 778 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { | 778 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { |
| 779 FilePathWatcher watcher; | 779 FilePathWatcher watcher; |
| 780 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 780 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
| 781 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 781 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); |
| 782 FilePath file(dir.AppendASCII("file")); | 782 FilePath file(dir.AppendASCII("file")); |
| 783 FilePath linkfile(link_dir.AppendASCII("file")); | 783 FilePath linkfile(link_dir.AppendASCII("file")); |
| 784 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 784 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 785 // Now create the link from dir.lnk pointing to dir but | 785 // Now create the link from dir.lnk pointing to dir but |
| 786 // neither dir nor dir/file exist yet. | 786 // neither dir nor dir/file exist yet. |
| 787 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); | 787 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
| 788 // Note that we are watching dir.lnk/file. | 788 // Note that we are watching dir.lnk/file. |
| 789 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 789 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
| 790 | 790 |
| 791 ASSERT_TRUE(base::CreateDirectory(dir)); | 791 ASSERT_TRUE(base::CreateDirectory(dir)); |
| 792 ASSERT_TRUE(WriteFile(file, "content")); | 792 ASSERT_TRUE(WriteFile(file, "content")); |
| 793 VLOG(1) << "Waiting for dir/file creation"; | 793 VLOG(1) << "Waiting for dir/file creation"; |
| 794 ASSERT_TRUE(WaitForEvents()); | 794 ASSERT_TRUE(WaitForEvents()); |
| 795 | 795 |
| 796 ASSERT_TRUE(WriteFile(file, "content v2")); | 796 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 797 VLOG(1) << "Waiting for file change"; | 797 VLOG(1) << "Waiting for file change"; |
| 798 ASSERT_TRUE(WaitForEvents()); | 798 ASSERT_TRUE(WaitForEvents()); |
| 799 | 799 |
| 800 ASSERT_TRUE(base::DeleteFile(file, false)); | 800 ASSERT_TRUE(base::DeleteFile(file, false)); |
| 801 VLOG(1) << "Waiting for file deletion"; | 801 VLOG(1) << "Waiting for file deletion"; |
| 802 ASSERT_TRUE(WaitForEvents()); | 802 ASSERT_TRUE(WaitForEvents()); |
| 803 DeleteDelegateOnFileThread(delegate.release()); | 803 DeleteDelegateOnFileThread(delegate.release()); |
| 804 } | 804 } |
| 805 | 805 |
| 806 // Verify that watching a file with a symlink on the path | 806 // Verify that watching a file with a symlink on the path |
| 807 // to the file works. | 807 // to the file works. |
| 808 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { | 808 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { |
| 809 FilePathWatcher watcher; | 809 FilePathWatcher watcher; |
| 810 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 810 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
| 811 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 811 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); |
| 812 FilePath file(dir.AppendASCII("file")); | 812 FilePath file(dir.AppendASCII("file")); |
| 813 FilePath linkfile(link_dir.AppendASCII("file")); | 813 FilePath linkfile(link_dir.AppendASCII("file")); |
| 814 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 814 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 815 ASSERT_TRUE(base::CreateDirectory(dir)); | 815 ASSERT_TRUE(base::CreateDirectory(dir)); |
| 816 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); | 816 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
| 817 // Note that we are watching dir.lnk/file but the file doesn't exist yet. | 817 // Note that we are watching dir.lnk/file but the file doesn't exist yet. |
| 818 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 818 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
| 819 | 819 |
| 820 ASSERT_TRUE(WriteFile(file, "content")); | 820 ASSERT_TRUE(WriteFile(file, "content")); |
| 821 VLOG(1) << "Waiting for file creation"; | 821 VLOG(1) << "Waiting for file creation"; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 872 | 872 |
| 873 #if defined(OS_MACOSX) | 873 #if defined(OS_MACOSX) |
| 874 // Linux implementation of FilePathWatcher doesn't catch attribute changes. | 874 // Linux implementation of FilePathWatcher doesn't catch attribute changes. |
| 875 // http://crbug.com/78043 | 875 // http://crbug.com/78043 |
| 876 // Windows implementation of FilePathWatcher catches attribute changes that | 876 // Windows implementation of FilePathWatcher catches attribute changes that |
| 877 // don't affect the path being watched. | 877 // don't affect the path being watched. |
| 878 // http://crbug.com/78045 | 878 // http://crbug.com/78045 |
| 879 | 879 |
| 880 // Verify that changing attributes on a directory works. | 880 // Verify that changing attributes on a directory works. |
| 881 TEST_F(FilePathWatcherTest, DirAttributesChanged) { | 881 TEST_F(FilePathWatcherTest, DirAttributesChanged) { |
| 882 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1")); | 882 FilePath test_dir1( |
| 883 temp_dir_.GetPath().AppendASCII("DirAttributesChangedDir1")); |
| 883 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); | 884 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); |
| 884 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); | 885 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); |
| 885 // Setup a directory hierarchy. | 886 // Setup a directory hierarchy. |
| 886 ASSERT_TRUE(base::CreateDirectory(test_dir1)); | 887 ASSERT_TRUE(base::CreateDirectory(test_dir1)); |
| 887 ASSERT_TRUE(base::CreateDirectory(test_dir2)); | 888 ASSERT_TRUE(base::CreateDirectory(test_dir2)); |
| 888 ASSERT_TRUE(WriteFile(test_file, "content")); | 889 ASSERT_TRUE(WriteFile(test_file, "content")); |
| 889 | 890 |
| 890 FilePathWatcher watcher; | 891 FilePathWatcher watcher; |
| 891 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 892 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 892 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get(), false)); | 893 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get(), false)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 905 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 906 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
| 906 ASSERT_TRUE(WaitForEvents()); | 907 ASSERT_TRUE(WaitForEvents()); |
| 907 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 908 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
| 908 DeleteDelegateOnFileThread(delegate.release()); | 909 DeleteDelegateOnFileThread(delegate.release()); |
| 909 } | 910 } |
| 910 | 911 |
| 911 #endif // OS_MACOSX | 912 #endif // OS_MACOSX |
| 912 } // namespace | 913 } // namespace |
| 913 | 914 |
| 914 } // namespace base | 915 } // namespace base |
| OLD | NEW |