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..68112f5068a721c5680fdd5444a35581fd2589e2 |
--- /dev/null |
+++ b/base/file_path_component_watcher_unittest.cc |
@@ -0,0 +1,256 @@ |
+// 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) { } |
+ virtual ~TestDelegate() { } |
+ virtual void OnFilePathComponentsChanged(const FilePath& old_path, |
+ const FilePath& new_path) { |
+ components_changed_ = true; |
+ old_path_ = old_path; |
+ new_path_ = new_path; |
+ MessageLoop::current()->Quit(); |
+ } |
+ |
+ bool components_changed_; |
+ 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() { |
+ temp_dir_.reset(new ScopedTempDir); |
+ 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_; |
+ scoped_ptr<ScopedTempDir> temp_dir_; |
Paweł Hajdan Jr.
2011/03/10 16:02:04
nit: No need for scoped_ptr, really. The entire te
|
+ base::FilePathComponentWatcher file_watcher_; |
+ scoped_refptr<TestDelegate> delegate_; |
+}; |
+ |
+class TestTask : public Task { |
+ public: |
+ void set_success() { *success_ = true; } |
+ |
+ protected: |
+ bool* success_; |
+ TestTask(bool *success) : success_(success) { *success_ = false; } |
+ virtual ~TestTask() { } |
+}; |
+ |
+class ChangeAttr : public TestTask { |
Paweł Hajdan Jr.
2011/03/10 16:02:04
nit: It's going to be simpler to use NewRunnableFu
|
+ public: |
+ ChangeAttr(bool *success, const FilePath& path) |
+ : TestTask(success), path_(path) { } |
+ virtual ~ChangeAttr() { } |
+ virtual void Run() { |
+ if (chmod(path_.value().c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0) { |
+ set_success(); |
+ } |
+ } |
+ |
+ private: |
+ FilePath path_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ChangeAttr); |
+}; |
+ |
+class Move : public TestTask { |
+ public: |
+ Move(bool *success, const FilePath& old_path, const FilePath& new_path) |
+ : TestTask(success), old_path_(old_path), new_path_(new_path) { } |
+ virtual ~Move() { } |
+ virtual void Run() { |
+ if (file_util::Move(old_path_, new_path_)) { |
+ set_success(); |
+ } |
+ } |
+ |
+ private: |
+ FilePath old_path_, new_path_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(Move); |
+}; |
+ |
+class Delete : public TestTask { |
+ public: |
+ Delete(bool *success, const FilePath& path) |
+ : TestTask(success), path_(path) { } |
+ virtual ~Delete() { } |
+ virtual void Run() { |
+ if (file_util::Delete(path_, true)) { |
+ set_success(); |
+ } |
+ } |
+ |
+ private: |
+ FilePath path_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(Delete); |
+}; |
+ |
+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, new ChangeAttr(&success, source_file)); |
+ 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, new ChangeAttr(&success, source_dir1)); |
+ 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, new 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, new 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)); |
+} |
+ |
+} |
+ |
+#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 |