| 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 13 matching lines...) Expand all Loading... |
| 24 #include "base/message_loop/message_loop_proxy.h" | 24 #include "base/message_loop/message_loop_proxy.h" |
| 25 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
| 26 #include "base/stl_util.h" | 26 #include "base/stl_util.h" |
| 27 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
| 28 #include "base/synchronization/waitable_event.h" | 28 #include "base/synchronization/waitable_event.h" |
| 29 #include "base/test/test_file_util.h" | 29 #include "base/test/test_file_util.h" |
| 30 #include "base/test/test_timeouts.h" | 30 #include "base/test/test_timeouts.h" |
| 31 #include "base/threading/thread.h" | 31 #include "base/threading/thread.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 33 |
| 34 #if defined(OS_ANDROID) |
| 35 #include "base/android/path_utils.h" |
| 36 #endif // defined(OS_ANDROID) |
| 37 |
| 34 namespace base { | 38 namespace base { |
| 35 | 39 |
| 36 namespace { | 40 namespace { |
| 37 | 41 |
| 38 class TestDelegate; | 42 class TestDelegate; |
| 39 | 43 |
| 40 // Aggregates notifications from the test delegates and breaks the message loop | 44 // Aggregates notifications from the test delegates and breaks the message loop |
| 41 // the test thread is waiting on once they all came in. | 45 // the test thread is waiting on once they all came in. |
| 42 class NotificationCollector | 46 class NotificationCollector |
| 43 : public base::RefCountedThreadSafe<NotificationCollector> { | 47 : public base::RefCountedThreadSafe<NotificationCollector> { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 FilePathWatcherTest() | 147 FilePathWatcherTest() |
| 144 : file_thread_("FilePathWatcherTest") {} | 148 : file_thread_("FilePathWatcherTest") {} |
| 145 | 149 |
| 146 ~FilePathWatcherTest() override {} | 150 ~FilePathWatcherTest() override {} |
| 147 | 151 |
| 148 protected: | 152 protected: |
| 149 void SetUp() override { | 153 void SetUp() override { |
| 150 // Create a separate file thread in order to test proper thread usage. | 154 // Create a separate file thread in order to test proper thread usage. |
| 151 base::Thread::Options options(MessageLoop::TYPE_IO, 0); | 155 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 152 ASSERT_TRUE(file_thread_.StartWithOptions(options)); | 156 ASSERT_TRUE(file_thread_.StartWithOptions(options)); |
| 157 #if defined(OS_ANDROID) |
| 158 // Watching files is only permitted when all parent directories are |
| 159 // accessible, which is not the case for the default temp directory |
| 160 // on Android which is under /data/data. Use /sdcard instead. |
| 161 // TODO(pauljensen): Remove this when crbug.com/475568 is fixed. |
| 162 FilePath parent_dir; |
| 163 ASSERT_TRUE(android::GetExternalStorageDirectory(&parent_dir)); |
| 164 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(parent_dir)); |
| 165 #else // defined(OS_ANDROID) |
| 153 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 166 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 167 #endif // defined(OS_ANDROID) |
| 154 collector_ = new NotificationCollector(); | 168 collector_ = new NotificationCollector(); |
| 155 } | 169 } |
| 156 | 170 |
| 157 void TearDown() override { RunLoop().RunUntilIdle(); } | 171 void TearDown() override { RunLoop().RunUntilIdle(); } |
| 158 | 172 |
| 159 void DeleteDelegateOnFileThread(TestDelegate* delegate) { | 173 void DeleteDelegateOnFileThread(TestDelegate* delegate) { |
| 160 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, delegate); | 174 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, delegate); |
| 161 } | 175 } |
| 162 | 176 |
| 163 FilePath test_file() { | 177 FilePath test_file() { |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 | 533 |
| 520 // Create "$dir/subdir/subdir_child_dir/child_dir_file1". | 534 // Create "$dir/subdir/subdir_child_dir/child_dir_file1". |
| 521 FilePath child_dir_file1(subdir_child_dir.AppendASCII("child_dir_file1")); | 535 FilePath child_dir_file1(subdir_child_dir.AppendASCII("child_dir_file1")); |
| 522 ASSERT_TRUE(WriteFile(child_dir_file1, "content v2")); | 536 ASSERT_TRUE(WriteFile(child_dir_file1, "content v2")); |
| 523 ASSERT_TRUE(WaitForEvents()); | 537 ASSERT_TRUE(WaitForEvents()); |
| 524 | 538 |
| 525 // Write into "$dir/subdir/subdir_child_dir/child_dir_file1". | 539 // Write into "$dir/subdir/subdir_child_dir/child_dir_file1". |
| 526 ASSERT_TRUE(WriteFile(child_dir_file1, "content")); | 540 ASSERT_TRUE(WriteFile(child_dir_file1, "content")); |
| 527 ASSERT_TRUE(WaitForEvents()); | 541 ASSERT_TRUE(WaitForEvents()); |
| 528 | 542 |
| 543 // Apps cannot change file attributes on Android in /sdcard as /sdcard uses the |
| 544 // "fuse" file system, while /data uses "ext4". Running these tests in /data |
| 545 // would be preferable and allow testing file attributes and symlinks. |
| 546 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places |
| 547 // the |temp_dir_| in /data. |
| 548 #if !defined(OS_ANDROID) |
| 529 // Modify "$dir/subdir/subdir_child_dir/child_dir_file1" attributes. | 549 // Modify "$dir/subdir/subdir_child_dir/child_dir_file1" attributes. |
| 530 ASSERT_TRUE(base::MakeFileUnreadable(child_dir_file1)); | 550 ASSERT_TRUE(base::MakeFileUnreadable(child_dir_file1)); |
| 531 ASSERT_TRUE(WaitForEvents()); | 551 ASSERT_TRUE(WaitForEvents()); |
| 552 #endif |
| 532 | 553 |
| 533 // Delete "$dir/subdir/subdir_file1". | 554 // Delete "$dir/subdir/subdir_file1". |
| 534 ASSERT_TRUE(base::DeleteFile(subdir_file1, false)); | 555 ASSERT_TRUE(base::DeleteFile(subdir_file1, false)); |
| 535 ASSERT_TRUE(WaitForEvents()); | 556 ASSERT_TRUE(WaitForEvents()); |
| 536 | 557 |
| 537 // Delete "$dir/subdir/subdir_child_dir/child_dir_file1". | 558 // Delete "$dir/subdir/subdir_child_dir/child_dir_file1". |
| 538 ASSERT_TRUE(base::DeleteFile(child_dir_file1, false)); | 559 ASSERT_TRUE(base::DeleteFile(child_dir_file1, false)); |
| 539 ASSERT_TRUE(WaitForEvents()); | 560 ASSERT_TRUE(WaitForEvents()); |
| 540 DeleteDelegateOnFileThread(delegate.release()); | 561 DeleteDelegateOnFileThread(delegate.release()); |
| 541 } | 562 } |
| 542 | 563 |
| 543 #if defined(OS_POSIX) | 564 #if defined(OS_POSIX) |
| 565 #if defined(OS_ANDROID) |
| 566 // Apps cannot create symlinks on Android in /sdcard as /sdcard uses the |
| 567 // "fuse" file system, while /data uses "ext4". Running these tests in /data |
| 568 // would be preferable and allow testing file attributes and symlinks. |
| 569 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places |
| 570 // the |temp_dir_| in /data. |
| 571 #define RecursiveWithSymLink DISABLED_RecursiveWithSymLink |
| 572 #endif // defined(OS_ANDROID) |
| 544 TEST_F(FilePathWatcherTest, RecursiveWithSymLink) { | 573 TEST_F(FilePathWatcherTest, RecursiveWithSymLink) { |
| 545 if (!FilePathWatcher::RecursiveWatchAvailable()) | 574 if (!FilePathWatcher::RecursiveWatchAvailable()) |
| 546 return; | 575 return; |
| 547 | 576 |
| 548 FilePathWatcher watcher; | 577 FilePathWatcher watcher; |
| 549 FilePath test_dir(temp_dir_.path().AppendASCII("test_dir")); | 578 FilePath test_dir(temp_dir_.path().AppendASCII("test_dir")); |
| 550 ASSERT_TRUE(base::CreateDirectory(test_dir)); | 579 ASSERT_TRUE(base::CreateDirectory(test_dir)); |
| 551 FilePath symlink(test_dir.AppendASCII("symlink")); | 580 FilePath symlink(test_dir.AppendASCII("symlink")); |
| 552 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 581 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 553 ASSERT_TRUE(SetupWatch(symlink, &watcher, delegate.get(), true)); | 582 ASSERT_TRUE(SetupWatch(symlink, &watcher, delegate.get(), true)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 false)); | 632 false)); |
| 604 | 633 |
| 605 // Move the directory into place, s.t. the watched file appears. | 634 // Move the directory into place, s.t. the watched file appears. |
| 606 ASSERT_TRUE(base::Move(source_dir, dest_dir)); | 635 ASSERT_TRUE(base::Move(source_dir, dest_dir)); |
| 607 ASSERT_TRUE(WaitForEvents()); | 636 ASSERT_TRUE(WaitForEvents()); |
| 608 DeleteDelegateOnFileThread(file_delegate.release()); | 637 DeleteDelegateOnFileThread(file_delegate.release()); |
| 609 DeleteDelegateOnFileThread(subdir_delegate.release()); | 638 DeleteDelegateOnFileThread(subdir_delegate.release()); |
| 610 } | 639 } |
| 611 | 640 |
| 612 // Verify that changing attributes on a file is caught | 641 // Verify that changing attributes on a file is caught |
| 642 #if defined(OS_ANDROID) |
| 643 // Apps cannot change file attributes on Android in /sdcard as /sdcard uses the |
| 644 // "fuse" file system, while /data uses "ext4". Running these tests in /data |
| 645 // would be preferable and allow testing file attributes and symlinks. |
| 646 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places |
| 647 // the |temp_dir_| in /data. |
| 648 #define FileAttributesChanged DISABLED_FileAttributesChanged |
| 649 #endif // defined(OS_ANDROID |
| 613 TEST_F(FilePathWatcherTest, FileAttributesChanged) { | 650 TEST_F(FilePathWatcherTest, FileAttributesChanged) { |
| 614 ASSERT_TRUE(WriteFile(test_file(), "content")); | 651 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 615 FilePathWatcher watcher; | 652 FilePathWatcher watcher; |
| 616 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 653 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 617 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 654 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 618 | 655 |
| 619 // Now make sure we get notified if the file is modified. | 656 // Now make sure we get notified if the file is modified. |
| 620 ASSERT_TRUE(base::MakeFileUnreadable(test_file())); | 657 ASSERT_TRUE(base::MakeFileUnreadable(test_file())); |
| 621 ASSERT_TRUE(WaitForEvents()); | 658 ASSERT_TRUE(WaitForEvents()); |
| 622 DeleteDelegateOnFileThread(delegate.release()); | 659 DeleteDelegateOnFileThread(delegate.release()); |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 899 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
| 863 ASSERT_TRUE(WaitForEvents()); | 900 ASSERT_TRUE(WaitForEvents()); |
| 864 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 901 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
| 865 DeleteDelegateOnFileThread(delegate.release()); | 902 DeleteDelegateOnFileThread(delegate.release()); |
| 866 } | 903 } |
| 867 | 904 |
| 868 #endif // OS_MACOSX | 905 #endif // OS_MACOSX |
| 869 } // namespace | 906 } // namespace |
| 870 | 907 |
| 871 } // namespace base | 908 } // namespace base |
| OLD | NEW |