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 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 }; | 198 }; |
199 | 199 |
200 bool FilePathWatcherTest::SetupWatch(const FilePath& target, | 200 bool FilePathWatcherTest::SetupWatch(const FilePath& target, |
201 FilePathWatcher* watcher, | 201 FilePathWatcher* watcher, |
202 TestDelegateBase* delegate, | 202 TestDelegateBase* delegate, |
203 bool recursive_watch) { | 203 bool recursive_watch) { |
204 base::WaitableEvent completion(false, false); | 204 base::WaitableEvent completion(false, false); |
205 bool result; | 205 bool result; |
206 file_thread_.message_loop_proxy()->PostTask( | 206 file_thread_.message_loop_proxy()->PostTask( |
207 FROM_HERE, | 207 FROM_HERE, |
208 base::Bind(SetupWatchCallback, target, watcher, delegate, recursive_watch, | 208 base::Bind(SetupWatchCallback, |
209 &result, &completion)); | 209 target, watcher, delegate, recursive_watch, &result, |
| 210 &completion)); |
210 completion.Wait(); | 211 completion.Wait(); |
211 return result; | 212 return result; |
212 } | 213 } |
213 | 214 |
214 // Basic test: Create the file and verify that we notice. | 215 // Basic test: Create the file and verify that we notice. |
215 TEST_F(FilePathWatcherTest, NewFile) { | 216 TEST_F(FilePathWatcherTest, NewFile) { |
216 FilePathWatcher watcher; | 217 FilePathWatcher watcher; |
217 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 218 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
218 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 219 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
219 | 220 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 ASSERT_TRUE(WaitForEvents()); | 476 ASSERT_TRUE(WaitForEvents()); |
476 | 477 |
477 // Move the parent directory. | 478 // Move the parent directory. |
478 base::Move(dir, dest); | 479 base::Move(dir, dest); |
479 VLOG(1) << "Waiting for directory move"; | 480 VLOG(1) << "Waiting for directory move"; |
480 ASSERT_TRUE(WaitForEvents()); | 481 ASSERT_TRUE(WaitForEvents()); |
481 DeleteDelegateOnFileThread(file_delegate.release()); | 482 DeleteDelegateOnFileThread(file_delegate.release()); |
482 DeleteDelegateOnFileThread(subdir_delegate.release()); | 483 DeleteDelegateOnFileThread(subdir_delegate.release()); |
483 } | 484 } |
484 | 485 |
485 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) | 486 #if defined(OS_WIN) || defined(OS_LINUX) |
486 TEST_F(FilePathWatcherTest, RecursiveWatch) { | 487 TEST_F(FilePathWatcherTest, RecursiveWatch) { |
487 FilePathWatcher watcher; | 488 FilePathWatcher watcher; |
488 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 489 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
489 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 490 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
490 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), true)); | 491 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), true)); |
491 | 492 |
492 // Main directory("dir") creation. | 493 // Main directory("dir") creation. |
493 ASSERT_TRUE(base::CreateDirectory(dir)); | 494 ASSERT_TRUE(base::CreateDirectory(dir)); |
494 ASSERT_TRUE(WaitForEvents()); | 495 ASSERT_TRUE(WaitForEvents()); |
495 | 496 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 FilePathWatcher watcher; | 541 FilePathWatcher watcher; |
541 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 542 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
542 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 543 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
543 // Only Windows/Linux support recursive watching. Other implementations | 544 // Only Windows/Linux support recursive watching. Other implementations |
544 // should simply fail. | 545 // should simply fail. |
545 ASSERT_FALSE(SetupWatch(dir, &watcher, delegate.get(), true)); | 546 ASSERT_FALSE(SetupWatch(dir, &watcher, delegate.get(), true)); |
546 DeleteDelegateOnFileThread(delegate.release()); | 547 DeleteDelegateOnFileThread(delegate.release()); |
547 } | 548 } |
548 #endif | 549 #endif |
549 | 550 |
550 #if defined(OS_LINUX) || defined(OS_MACOSX) | |
551 TEST_F(FilePathWatcherTest, RecursiveWithSymLink) { | |
552 FilePathWatcher watcher; | |
553 FilePath test_dir(temp_dir_.path().AppendASCII("test_dir")); | |
554 ASSERT_TRUE(base::CreateDirectory(test_dir)); | |
555 FilePath symlink(test_dir.AppendASCII("symlink")); | |
556 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | |
557 ASSERT_TRUE(SetupWatch(symlink, &watcher, delegate.get(), true)); | |
558 | |
559 // Link creation. | |
560 FilePath target1(temp_dir_.path().AppendASCII("target1")); | |
561 ASSERT_TRUE(base::CreateSymbolicLink(target1, symlink)); | |
562 ASSERT_TRUE(WaitForEvents()); | |
563 | |
564 // Target1 creation. | |
565 ASSERT_TRUE(base::CreateDirectory(target1)); | |
566 ASSERT_TRUE(WaitForEvents()); | |
567 | |
568 // Create a file in target1. | |
569 FilePath target1_file(target1.AppendASCII("file")); | |
570 ASSERT_TRUE(WriteFile(target1_file, "content")); | |
571 ASSERT_TRUE(WaitForEvents()); | |
572 | |
573 // Link change. | |
574 FilePath target2(temp_dir_.path().AppendASCII("target2")); | |
575 ASSERT_TRUE(base::CreateDirectory(target2)); | |
576 ASSERT_TRUE(base::DeleteFile(symlink, false)); | |
577 ASSERT_TRUE(base::CreateSymbolicLink(target2, symlink)); | |
578 ASSERT_TRUE(WaitForEvents()); | |
579 | |
580 // Create a file in target2. | |
581 FilePath target2_file(target2.AppendASCII("file")); | |
582 ASSERT_TRUE(WriteFile(target2_file, "content")); | |
583 ASSERT_TRUE(WaitForEvents()); | |
584 | |
585 DeleteDelegateOnFileThread(delegate.release()); | |
586 } | |
587 #endif | |
588 | |
589 TEST_F(FilePathWatcherTest, MoveChild) { | 551 TEST_F(FilePathWatcherTest, MoveChild) { |
590 FilePathWatcher file_watcher; | 552 FilePathWatcher file_watcher; |
591 FilePathWatcher subdir_watcher; | 553 FilePathWatcher subdir_watcher; |
592 FilePath source_dir(temp_dir_.path().AppendASCII("source")); | 554 FilePath source_dir(temp_dir_.path().AppendASCII("source")); |
593 FilePath source_subdir(source_dir.AppendASCII("subdir")); | 555 FilePath source_subdir(source_dir.AppendASCII("subdir")); |
594 FilePath source_file(source_subdir.AppendASCII("file")); | 556 FilePath source_file(source_subdir.AppendASCII("file")); |
595 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); | 557 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); |
596 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); | 558 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); |
597 FilePath dest_file(dest_subdir.AppendASCII("file")); | 559 FilePath dest_file(dest_subdir.AppendASCII("file")); |
598 | 560 |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
866 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 828 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
867 ASSERT_TRUE(WaitForEvents()); | 829 ASSERT_TRUE(WaitForEvents()); |
868 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 830 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
869 DeleteDelegateOnFileThread(delegate.release()); | 831 DeleteDelegateOnFileThread(delegate.release()); |
870 } | 832 } |
871 | 833 |
872 #endif // OS_MACOSX | 834 #endif // OS_MACOSX |
873 } // namespace | 835 } // namespace |
874 | 836 |
875 } // namespace base | 837 } // namespace base |
OLD | NEW |