Chromium Code Reviews| 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 } |
| 56 | 53 |
| 57 // Write |content| to a file under the test directory. | 54 // Write |content| to a file under the test directory. |
| 58 void WriteTestDirFile(const FilePath::StringType& filename, | 55 void WriteTestDirFile(const FilePath::StringType& filename, |
| 59 const std::string& content) { | 56 const std::string& content) { |
| 57 EXPECT_FALSE(FilePath(filename).IsAbsolute()); | |
| 60 FilePath path = test_dir_.Append(filename); | 58 FilePath path = test_dir_.Append(filename); |
| 61 | 59 |
| 62 std::ofstream file; | 60 std::ofstream file; |
| 63 file.open(WideToUTF8(path.value()).c_str()); | 61 file.open(WideToUTF8(path.value()).c_str()); |
| 64 file << content; | 62 file << content; |
| 65 file.close(); | 63 file.close(); |
| 66 } | 64 } |
| 67 | 65 |
| 68 // Run the message loop until we've seen |n| directory modifications. | 66 // Run the message loop until we've seen |n| directory modifications. |
| 69 void LoopUntilModsEqual(int n) { | 67 void LoopUntilModsEqual(int n) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 // that notification doesn't come. | 121 // that notification doesn't come. |
| 124 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, | 122 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, |
| 125 kWaitForEventTime); | 123 kWaitForEventTime); |
| 126 loop_.Run(); | 124 loop_.Run(); |
| 127 | 125 |
| 128 ASSERT_EQ(directory_mods_, 0); | 126 ASSERT_EQ(directory_mods_, 0); |
| 129 } | 127 } |
| 130 | 128 |
| 131 // Verify that modifications to a subdirectory isn't noticed. | 129 // Verify that modifications to a subdirectory isn't noticed. |
| 132 TEST_F(DirectoryWatcherTest, SubDir) { | 130 TEST_F(DirectoryWatcherTest, SubDir) { |
| 133 #if defined(OS_WIN) | |
| 134 // Temporarily disabling test on Vista, see | |
| 135 // http://code.google.com/p/chromium/issues/detail?id=5072 | |
| 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")); | 131 FilePath subdir(FILE_PATH_LITERAL("SubDir")); |
| 141 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); | 132 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); |
| 142 | 133 |
| 134 // Create a file in the subdir. | |
| 135 FilePath test_path = subdir.Append(FILE_PATH_LITERAL("test_file")); | |
| 136 WriteTestDirFile(test_path.value(), "some content"); | |
| 137 | |
| 138 #if defined(OS_WIN) | |
|
M-A Ruel
2009/01/02 20:47:21
If the flush was done in WriteTestDirFile(), you c
| |
| 139 // We will flush the write buffer now to ensure it won't interfere | |
| 140 // with the notifications. | |
| 141 FilePath full_path(test_dir_.Append(test_path)); | |
| 142 HANDLE file_handle = CreateFile(full_path.value().c_str(), | |
| 143 GENERIC_READ | GENERIC_WRITE, | |
| 144 0, NULL, OPEN_EXISTING, | |
| 145 FILE_ATTRIBUTE_NORMAL, NULL); | |
| 146 ASSERT_TRUE(file_handle != INVALID_HANDLE_VALUE); | |
| 147 ASSERT_TRUE(FlushFileBuffers(file_handle)); | |
| 148 CloseHandle(file_handle); | |
| 149 #endif | |
| 150 | |
| 143 DirectoryWatcher watcher; | 151 DirectoryWatcher watcher; |
| 144 ASSERT_TRUE(watcher.Watch(test_dir_, this)); | 152 ASSERT_TRUE(watcher.Watch(test_dir_, this)); |
| 145 // Write a file to the subdir. | 153 |
| 146 FilePath test_path = subdir.Append(FILE_PATH_LITERAL("test_file")); | 154 WriteTestDirFile(test_path.value(), "changed content"); |
| 147 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 |
| 155 // We shouldn't have been notified and shouldn't have crashed. | 162 // We shouldn't have been notified and shouldn't have crashed. |
| 156 ASSERT_EQ(0, directory_mods_); | 163 ASSERT_EQ(0, directory_mods_); |
| 157 } | 164 } |
| (...skipping 19 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 |