| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/directory_watcher.h" | 5 #include "base/directory_watcher.h" |
| 6 | 6 |
| 7 #include <fstream> | 7 #include <fstream> |
| 8 | 8 |
| 9 #include "build/build_config.h" | |
| 10 | |
| 11 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 11 #include "base/logging.h" |
| 14 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 15 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/platform_thread.h" |
| 16 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 17 #if defined(OS_WIN) | |
| 18 #include "base/win_util.h" | |
| 19 #endif | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 17 |
| 22 // For tests where we wait a bit to verify nothing happened | 18 // For tests where we wait a bit to verify nothing happened |
| 23 namespace { | 19 namespace { |
| 24 const int kWaitForEventTime = 500; | 20 const int kWaitForEventTime = 500; |
| 25 } | 21 } |
| 26 | 22 |
| 27 class DirectoryWatcherTest : public testing::Test, | 23 class DirectoryWatcherTest : public testing::Test, |
| 28 public DirectoryWatcher::Delegate { | 24 public DirectoryWatcher::Delegate { |
| 29 protected: | 25 protected: |
| 30 virtual void SetUp() { | 26 virtual void SetUp() { |
| 31 // Name a subdirectory of the temp directory. | 27 // Name a subdirectory of the temp directory. |
| 32 std::wstring path_str; | 28 std::wstring path_str; |
| 33 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_str)); | 29 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_str)); |
| 34 test_dir_ = FilePath(path_str).Append( | 30 test_dir_ = FilePath(path_str).Append( |
| 35 FILE_PATH_LITERAL("DirectoryWatcherTest")); | 31 FILE_PATH_LITERAL("DirectoryWatcherTest")); |
| 36 | 32 |
| 37 // Create a fresh, empty copy of this directory. | 33 // Create a fresh, empty copy of this directory. |
| 38 file_util::Delete(test_dir_.value(), true); | 34 file_util::Delete(test_dir_.value(), true); |
| 39 file_util::CreateDirectory(test_dir_.value()); | 35 file_util::CreateDirectory(test_dir_.value()); |
| 40 | 36 |
| 41 directory_mods_ = 0; | 37 directory_mods_ = 0; |
| 42 quit_mod_count_ = 0; | 38 quit_mod_count_ = 0; |
| 43 } | 39 } |
| 44 | 40 |
| 45 virtual void OnDirectoryChanged(const FilePath& path) { | 41 virtual void OnDirectoryChanged(const FilePath& path) { |
| 42 EXPECT_EQ(path.value(), test_dir_.value()); |
| 46 ++directory_mods_; | 43 ++directory_mods_; |
| 47 if (directory_mods_ == quit_mod_count_) | 44 if (directory_mods_ == quit_mod_count_) |
| 48 MessageLoop::current()->Quit(); | 45 MessageLoop::current()->Quit(); |
| 49 } | 46 } |
| 50 | 47 |
| 51 virtual void TearDown() { | 48 virtual void TearDown() { |
| 52 // Clean up test directory. | 49 // Clean up test directory. |
| 53 ASSERT_TRUE(file_util::Delete(test_dir_.value(), true)); | 50 ASSERT_TRUE(file_util::Delete(test_dir_.value(), true)); |
| 54 ASSERT_FALSE(file_util::PathExists(test_dir_.value())); | 51 ASSERT_FALSE(file_util::PathExists(test_dir_.value())); |
| 55 } | 52 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 86 | 83 |
| 87 // Basic test: add a file and verify we notice it. | 84 // Basic test: add a file and verify we notice it. |
| 88 TEST_F(DirectoryWatcherTest, NewFile) { | 85 TEST_F(DirectoryWatcherTest, NewFile) { |
| 89 DirectoryWatcher watcher; | 86 DirectoryWatcher watcher; |
| 90 ASSERT_TRUE(watcher.Watch(test_dir_, this)); | 87 ASSERT_TRUE(watcher.Watch(test_dir_, this)); |
| 91 | 88 |
| 92 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); | 89 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); |
| 93 LoopUntilModsEqual(2); | 90 LoopUntilModsEqual(2); |
| 94 } | 91 } |
| 95 | 92 |
| 93 TEST_F(DirectoryWatcherTest, NewDir) { |
| 94 DirectoryWatcher watcher; |
| 95 ASSERT_TRUE(watcher.Watch(test_dir_, this)); |
| 96 |
| 97 FilePath subdir(FILE_PATH_LITERAL("SubDir")); |
| 98 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); |
| 99 |
| 100 LoopUntilModsEqual(1); |
| 101 } |
| 102 |
| 96 // Verify that modifying a file is caught. | 103 // Verify that modifying a file is caught. |
| 97 TEST_F(DirectoryWatcherTest, ModifiedFile) { | 104 TEST_F(DirectoryWatcherTest, ModifiedFile) { |
| 98 DirectoryWatcher watcher; | 105 DirectoryWatcher watcher; |
| 99 ASSERT_TRUE(watcher.Watch(test_dir_, this)); | 106 ASSERT_TRUE(watcher.Watch(test_dir_, this)); |
| 100 | 107 |
| 101 // Write a file to the test dir. | 108 // Write a file to the test dir. |
| 102 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); | 109 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); |
| 103 LoopUntilModsEqual(2); | 110 LoopUntilModsEqual(2); |
| 104 | 111 |
| 105 // Now make sure we get notified if the file is modified. | 112 // Now make sure we get notified if the file is modified. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 123 // that notification doesn't come. | 130 // that notification doesn't come. |
| 124 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, | 131 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, |
| 125 kWaitForEventTime); | 132 kWaitForEventTime); |
| 126 loop_.Run(); | 133 loop_.Run(); |
| 127 | 134 |
| 128 ASSERT_EQ(directory_mods_, 0); | 135 ASSERT_EQ(directory_mods_, 0); |
| 129 } | 136 } |
| 130 | 137 |
| 131 // Verify that modifications to a subdirectory isn't noticed. | 138 // Verify that modifications to a subdirectory isn't noticed. |
| 132 TEST_F(DirectoryWatcherTest, SubDir) { | 139 TEST_F(DirectoryWatcherTest, SubDir) { |
| 133 #if defined(OS_WIN) | 140 DirectoryWatcher watcher; |
| 134 // Temporarily disabling test on Vista, see | 141 ASSERT_TRUE(watcher.Watch(test_dir_, this)); |
| 135 // http://code.google.com/p/chromium/issues/detail?id=5072 | 142 |
| 136 // TODO: Enable this test, quickly. | |
| 137 if (win_util::GetWinVersion() == win_util::WINVERSION_VISTA) | |
| 138 return; | |
| 139 #endif | |
| 140 FilePath subdir(FILE_PATH_LITERAL("SubDir")); | 143 FilePath subdir(FILE_PATH_LITERAL("SubDir")); |
| 141 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); | 144 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); |
| 142 | 145 |
| 143 DirectoryWatcher watcher; | 146 LoopUntilModsEqual(1); |
| 144 ASSERT_TRUE(watcher.Watch(test_dir_, this)); | 147 |
| 148 // Wait to make sure we also clear some late notifications. |
| 149 PlatformThread::Sleep(2 * kWaitForEventTime); |
| 150 directory_mods_ = 0; |
| 151 |
| 145 // Write a file to the subdir. | 152 // Write a file to the subdir. |
| 146 FilePath test_path = subdir.Append(FILE_PATH_LITERAL("test_file")); | 153 FilePath test_path = subdir.Append(FILE_PATH_LITERAL("test_file")); |
| 147 WriteTestDirFile(test_path.value(), "some content"); | 154 WriteTestDirFile(test_path.value(), "some content"); |
| 148 | 155 |
| 149 // We won't get a notification, so we just wait around a bit to verify | 156 // We won't get a notification, so we just wait around a bit to verify |
| 150 // that notification doesn't come. | 157 // that notification doesn't come. |
| 151 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, | 158 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, |
| 152 kWaitForEventTime); | 159 kWaitForEventTime); |
| 153 loop_.Run(); | 160 loop_.Run(); |
| 154 | 161 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 177 Deleter deleter(watcher); // Takes ownership of watcher. | 184 Deleter deleter(watcher); // Takes ownership of watcher. |
| 178 ASSERT_TRUE(watcher->Watch(test_dir_, &deleter)); | 185 ASSERT_TRUE(watcher->Watch(test_dir_, &deleter)); |
| 179 | 186 |
| 180 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); | 187 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); |
| 181 LoopUntilModsEqual(2); | 188 LoopUntilModsEqual(2); |
| 182 | 189 |
| 183 // We win if we haven't crashed yet. | 190 // We win if we haven't crashed yet. |
| 184 // Might as well double-check it got deleted, too. | 191 // Might as well double-check it got deleted, too. |
| 185 ASSERT_TRUE(deleter.watcher_.get() == NULL); | 192 ASSERT_TRUE(deleter.watcher_.get() == NULL); |
| 186 } | 193 } |
| OLD | NEW |