Index: base/file_path_component_watcher_unittest.cc |
diff --git a/base/file_path_component_watcher_unittest.cc b/base/file_path_component_watcher_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c22a4f274ba5cd1b77193bfc9c4bcf605d7f4225 |
--- /dev/null |
+++ b/base/file_path_component_watcher_unittest.cc |
@@ -0,0 +1,269 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/file_path_component_watcher.h" |
+ |
+#include <gtest/gtest.h> |
+ |
+#include "base/basictypes.h" |
+#include "base/file_util.h" |
+#include "base/message_loop.h" |
+#include "base/scoped_ptr.h" |
+#include "base/scoped_temp_dir.h" |
+#include "base/test/test_timeouts.h" |
+ |
+#if defined(OS_MACOSX) |
+ |
+namespace { |
+ |
+class TestDelegate : public base::FilePathComponentWatcher::Delegate { |
+ public: |
+ TestDelegate() |
+ : components_changed_(false), watch_again_(false), error_(false) { } |
+ virtual ~TestDelegate() { } |
+ virtual void OnFilePathComponentsChanged( |
+ base::FilePathComponentWatcher* watcher, |
+ const FilePath& old_path, |
+ const FilePath& new_path) { |
+ components_changed_ = true; |
+ old_path_ = old_path; |
+ new_path_ = new_path; |
+ if (watch_again_) { |
+ if (!watcher->Watch(new_path_, this)) { |
+ error_ = true; |
+ } |
+ watch_again_ = false; |
+ } |
+ MessageLoop::current()->Quit(); |
+ } |
+ |
+ bool components_changed_; |
+ bool watch_again_; |
+ bool error_; |
+ FilePath old_path_; |
+ FilePath new_path_; |
+}; |
+ |
+class FilePathComponentWatcherTest : public testing::Test { |
+ public: |
+ // Implementation of FilePathWatcher on Mac requires UI loop. |
+ FilePathComponentWatcherTest() |
+ : loop_(MessageLoop::TYPE_UI), delegate_(new TestDelegate) { |
+ } |
+ |
+ protected: |
+ virtual void SetUp() { |
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ } |
+ |
+ virtual void TearDown() { |
+ loop_.RunAllPending(); |
+ } |
+ |
+ FilePath test_file() { |
+ return temp_dir_.path().AppendASCII("FilePathWatcherTest"); |
+ } |
+ |
+ // Write |content| to |file|. Returns true on success. |
+ bool WriteFile(const FilePath& file, const std::string& content) { |
+ int write_size = file_util::WriteFile(file, content.c_str(), |
+ content.length()); |
+ return write_size == static_cast<int>(content.length()); |
+ } |
+ |
+ void WaitForEvents() { |
+ loop_.Run(); |
+ } |
+ |
+ MessageLoop loop_; |
+ ScopedTempDir temp_dir_; |
+ base::FilePathComponentWatcher file_watcher_; |
+ scoped_refptr<TestDelegate> delegate_; |
+}; |
+ |
+// RunnableFunctions for tests below |
+void ChangeAttr(bool *success, const FilePath& path, mode_t mode) { |
+ *success = chmod(path.value().c_str(), mode) == 0; |
+} |
+ |
+void Move(bool *success, const FilePath& old_path, const FilePath& new_path) { |
+ *success = file_util::Move(old_path, new_path); |
+} |
+ |
+void Delete(bool *success, const FilePath& path) { |
+ *success = file_util::Delete(path, true); |
+} |
+ |
+TEST_F(FilePathComponentWatcherTest, AttrChange) { |
+ FilePath source_dir(temp_dir_.path().AppendASCII("source")); |
+ FilePath source_file(source_dir.AppendASCII("file")); |
+ |
+ // Setup a directory hierarchy. |
+ ASSERT_TRUE(file_util::CreateDirectory(source_dir)); |
+ ASSERT_TRUE(WriteFile(source_file, "content")); |
+ |
+ ASSERT_TRUE(file_watcher_.Watch(source_file, delegate_)); |
+ bool success; |
+ loop_.PostTask(FROM_HERE, NewRunnableFunction(ChangeAttr, |
+ &success, |
+ source_file, |
+ S_IRWXU | S_IRWXG | S_IRWXO)); |
+ loop_.PostDelayedTask(FROM_HERE, |
+ new MessageLoop::QuitTask, |
+ TestTimeouts::action_timeout_ms()); |
+ WaitForEvents(); |
+ |
+ ASSERT_TRUE(success); |
+ ASSERT_TRUE(delegate_->components_changed_); |
+ ASSERT_TRUE(file_util::AreReferringToSameObject(delegate_->old_path_, |
+ source_file)); |
+ ASSERT_TRUE(file_util::AreReferringToSameObject(delegate_->new_path_, |
+ source_file)); |
+} |
+ |
+TEST_F(FilePathComponentWatcherTest, InheritedAttrChange) { |
+ FilePath source_dir1(temp_dir_.path().AppendASCII("source1")); |
+ FilePath source_dir2(source_dir1.AppendASCII("source2")); |
+ FilePath source_file(source_dir2.AppendASCII("file")); |
+ |
+ // Setup a directory hierarchy. |
+ ASSERT_TRUE(file_util::CreateDirectory(source_dir2)); |
+ ASSERT_TRUE(WriteFile(source_file, "content")); |
+ |
+ ASSERT_TRUE(file_watcher_.Watch(source_file, delegate_)); |
+ |
+ bool success; |
+ loop_.PostTask(FROM_HERE, NewRunnableFunction(ChangeAttr, |
+ &success, |
+ source_dir1, |
+ S_IRWXU | S_IRWXG | S_IRWXO)); |
+ loop_.PostDelayedTask(FROM_HERE, |
+ new MessageLoop::QuitTask, |
+ TestTimeouts::action_timeout_ms()); |
+ WaitForEvents(); |
+ |
+ ASSERT_TRUE(success); |
+ ASSERT_TRUE(delegate_->components_changed_); |
+ ASSERT_TRUE(file_util::AreReferringToSameObject(delegate_->old_path_, |
+ source_file)); |
+ ASSERT_TRUE(file_util::AreReferringToSameObject(delegate_->new_path_, |
+ source_file)); |
+} |
+ |
+TEST_F(FilePathComponentWatcherTest, InheritedMove) { |
+ FilePath source_dir1(temp_dir_.path().AppendASCII("source1")); |
+ FilePath source_dir2(source_dir1.AppendASCII("source2")); |
+ FilePath source_file(source_dir2.AppendASCII("file")); |
+ FilePath new_sourcedir1(temp_dir_.path().AppendASCII("new_source1")); |
+ FilePath new_sourcedir2(new_sourcedir1.AppendASCII("source2")); |
+ FilePath new_source_file(new_sourcedir2.AppendASCII("file")); |
+ |
+ // Setup a directory hierarchy. |
+ ASSERT_TRUE(file_util::CreateDirectory(source_dir2)); |
+ ASSERT_TRUE(WriteFile(source_file, "content")); |
+ |
+ ASSERT_TRUE(file_watcher_.Watch(source_file, delegate_)); |
+ |
+ bool success; |
+ loop_.PostTask(FROM_HERE, NewRunnableFunction(Move, |
+ &success, |
+ source_dir1, |
+ new_sourcedir1)); |
+ loop_.PostDelayedTask(FROM_HERE, |
+ new MessageLoop::QuitTask, |
+ TestTimeouts::action_timeout_ms()); |
+ WaitForEvents(); |
+ |
+ ASSERT_TRUE(success); |
+ ASSERT_TRUE(delegate_->components_changed_); |
+ |
+ ASSERT_TRUE(file_util::AreReferringToSameObject(delegate_->new_path_, |
+ new_source_file)); |
+ |
+ // File has been moved, so these paths will no longer exist |
+ ASSERT_FALSE(file_util::PathExists(delegate_->old_path_)); |
+ ASSERT_FALSE(file_util::PathExists(source_file)); |
+} |
+ |
+TEST_F(FilePathComponentWatcherTest, Delete) { |
+ FilePath source_dir1(temp_dir_.path().AppendASCII("source1")); |
+ FilePath source_dir2(source_dir1.AppendASCII("source2")); |
+ FilePath source_file(source_dir2.AppendASCII("file")); |
+ |
+ // Setup a directory hierarchy. |
+ ASSERT_TRUE(file_util::CreateDirectory(source_dir2)); |
+ ASSERT_TRUE(WriteFile(source_file, "content")); |
+ |
+ ASSERT_TRUE(file_watcher_.Watch(source_file, delegate_)); |
+ |
+ bool success; |
+ loop_.PostTask(FROM_HERE, NewRunnableFunction(Delete, &success, source_file)); |
+ loop_.PostDelayedTask(FROM_HERE, |
+ new MessageLoop::QuitTask, |
+ TestTimeouts::action_timeout_ms()); |
+ WaitForEvents(); |
+ |
+ ASSERT_TRUE(success); |
+ ASSERT_TRUE(delegate_->components_changed_); |
+ |
+ // File has been deleted, so these paths will no longer exist |
+ ASSERT_FALSE(file_util::PathExists(delegate_->old_path_)); |
+ ASSERT_FALSE(file_util::PathExists(source_file)); |
+} |
+ |
+TEST_F(FilePathComponentWatcherTest, WatchAgain) { |
+ FilePath source_dir1(temp_dir_.path().AppendASCII("source1")); |
+ FilePath source_dir2(source_dir1.AppendASCII("source2")); |
+ FilePath source_file(source_dir2.AppendASCII("file")); |
+ |
+ // Setup a directory hierarchy. |
+ ASSERT_TRUE(file_util::CreateDirectory(source_dir2)); |
+ ASSERT_TRUE(WriteFile(source_file, "content")); |
+ |
+ ASSERT_TRUE(file_watcher_.Watch(source_file, delegate_)); |
+ |
+ bool success; |
+ delegate_->watch_again_ = true; |
+ loop_.PostTask(FROM_HERE, NewRunnableFunction(ChangeAttr, |
+ &success, |
+ source_dir1, |
+ S_IRWXU | S_IRWXG | S_IRWXO)); |
+ loop_.PostDelayedTask(FROM_HERE, |
+ new MessageLoop::QuitTask, |
+ TestTimeouts::action_timeout_ms()); |
+ WaitForEvents(); |
+ |
+ ASSERT_TRUE(success); |
+ ASSERT_TRUE(delegate_->components_changed_); |
+ ASSERT_FALSE(delegate_->error_); |
+ ASSERT_FALSE(delegate_->watch_again_); |
+ delegate_->components_changed_ = false; |
+ |
+ loop_.PostTask(FROM_HERE, NewRunnableFunction(ChangeAttr, |
+ &success, |
+ source_dir1, |
+ S_IRWXU | S_IRWXG | S_IROTH)); |
+ loop_.PostDelayedTask(FROM_HERE, |
+ new MessageLoop::QuitTask, |
+ TestTimeouts::action_timeout_ms()); |
+ WaitForEvents(); |
+ ASSERT_TRUE(success); |
+ ASSERT_TRUE(delegate_->components_changed_); |
+ ASSERT_FALSE(delegate_->error_); |
+ ASSERT_FALSE(delegate_->watch_again_); |
+} |
+ |
+} |
+ |
+#else // OS_MACOSX |
+ |
+// Test that our stub is there for now |
+TEST(FilePathComponentWatcherTest, StubTest) { |
+ base::FilePathComponentWatcher file_watcher_; |
+ ScopedTempDir temp_dir; |
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
+ ASSERT_FALSE(file_watcher_.Watch(temp_dir.path(), NULL)); |
+} |
+ |
+#endif // OS_MACOSX |