OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <set> | 7 #include <set> |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 } | 158 } |
159 | 159 |
160 virtual void TearDown() { | 160 virtual void TearDown() { |
161 loop_.RunAllPending(); | 161 loop_.RunAllPending(); |
162 } | 162 } |
163 | 163 |
164 FilePath test_file() { | 164 FilePath test_file() { |
165 return temp_dir_.path().AppendASCII("FilePathWatcherTest"); | 165 return temp_dir_.path().AppendASCII("FilePathWatcherTest"); |
166 } | 166 } |
167 | 167 |
168 FilePath test_link() { | |
169 return temp_dir_.path().AppendASCII("FilePathWatcherTest.lnk"); | |
170 } | |
171 | |
168 // Write |content| to |file|. Returns true on success. | 172 // Write |content| to |file|. Returns true on success. |
169 bool WriteFile(const FilePath& file, const std::string& content) { | 173 bool WriteFile(const FilePath& file, const std::string& content) { |
170 int write_size = file_util::WriteFile(file, content.c_str(), | 174 int write_size = file_util::WriteFile(file, content.c_str(), |
171 content.length()); | 175 content.length()); |
172 return write_size == static_cast<int>(content.length()); | 176 return write_size == static_cast<int>(content.length()); |
173 } | 177 } |
174 | 178 |
175 bool SetupWatch(const FilePath& target, | 179 bool SetupWatch(const FilePath& target, |
176 FilePathWatcher* watcher, | 180 FilePathWatcher* watcher, |
177 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { | 181 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 491 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
488 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 492 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); |
489 | 493 |
490 // Now make sure we get notified if the file is modified. | 494 // Now make sure we get notified if the file is modified. |
491 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); | 495 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); |
492 ASSERT_TRUE(WaitForEvents()); | 496 ASSERT_TRUE(WaitForEvents()); |
493 } | 497 } |
494 | 498 |
495 #endif // !OS_LINUX | 499 #endif // !OS_LINUX |
496 | 500 |
501 #if defined(OS_LINUX) | |
502 | |
503 // Verify that creating a symlink is caught. | |
504 TEST_F(FilePathWatcherTest, CreateLink) { | |
505 FilePathWatcher watcher; | |
506 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | |
507 // Note that we are watching the symlink | |
508 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | |
509 | |
510 // Now make sure we get notified if the link is created. | |
511 // Note that test_file() doesn't have to exist. | |
512 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | |
513 ASSERT_TRUE(WaitForEvents()); | |
514 } | |
515 | |
516 // Verify that deleting a symlink is caught. | |
517 TEST_F(FilePathWatcherTest, DeleteLink) { | |
518 ASSERT_TRUE(WriteFile(test_file(), "content")); | |
519 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | |
520 FilePathWatcher watcher; | |
521 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | |
522 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | |
523 | |
524 // Now make sure we get notified if the link is deleted. | |
525 ASSERT_TRUE(file_util::Delete(test_link(), false)); | |
526 ASSERT_TRUE(WaitForEvents()); | |
527 } | |
528 | |
529 // Verify that modifying a target file that a link is pointing to | |
530 // when we are watching the link is caught. | |
531 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { | |
532 ASSERT_TRUE(WriteFile(test_file(), "content")); | |
533 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | |
534 FilePathWatcher watcher; | |
535 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | |
536 // Note that we are watching the symlink. | |
537 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | |
538 | |
539 // Now make sure we get notified if the file is modified. | |
540 ASSERT_TRUE(WriteFile(test_file(), "new content")); | |
541 ASSERT_TRUE(WaitForEvents()); | |
542 } | |
543 | |
544 // Verify that creating a target file that a link is pointing to | |
545 // when we are watching the link is caught. | |
546 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { | |
547 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | |
548 FilePathWatcher watcher; | |
549 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | |
550 // Note that we are watching the symlink. | |
551 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | |
552 | |
553 // Now make sure we get notified if the target file is created. | |
554 ASSERT_TRUE(WriteFile(test_file(), "content")); | |
555 ASSERT_TRUE(WaitForEvents()); | |
556 } | |
Mattias Nissler (ping if slow)
2011/08/30 09:53:06
You may want to add another test that deletes the
Craig
2011/08/30 16:00:16
Will do.
| |
557 | |
558 #endif // OS_LINUX | |
559 | |
497 enum Permission { | 560 enum Permission { |
498 Read, | 561 Read, |
499 Write, | 562 Write, |
500 Execute | 563 Execute |
501 }; | 564 }; |
502 | 565 |
503 bool ChangeFilePermissions(const FilePath& path, Permission perm, bool allow) { | 566 bool ChangeFilePermissions(const FilePath& path, Permission perm, bool allow) { |
504 #if defined(OS_POSIX) | 567 #if defined(OS_POSIX) |
505 struct stat stat_buf; | 568 struct stat stat_buf; |
506 | 569 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
619 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 682 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
620 ASSERT_TRUE(WaitForEvents()); | 683 ASSERT_TRUE(WaitForEvents()); |
621 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 684 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
622 } | 685 } |
623 | 686 |
624 #endif // OS_MACOSX | 687 #endif // OS_MACOSX |
625 } // namespace | 688 } // namespace |
626 | 689 |
627 } // namespace files | 690 } // namespace files |
628 } // namespace base | 691 } // namespace base |
OLD | NEW |