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 |
9 #include "base/file_path.h" | 11 #include "base/file_path.h" |
10 #include "base/file_util.h" | 12 #include "base/file_util.h" |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
14 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #if defined(OS_WIN) |
| 18 #include "base/win_util.h" |
| 19 #endif |
15 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
16 | 21 |
17 // For tests where we wait a bit to verify nothing happened | 22 // For tests where we wait a bit to verify nothing happened |
18 namespace { | 23 namespace { |
19 const int kWaitForEventTime = 500; | 24 const int kWaitForEventTime = 500; |
20 } | 25 } |
21 | 26 |
22 class DirectoryWatcherTest : public testing::Test, | 27 class DirectoryWatcherTest : public testing::Test, |
23 public DirectoryWatcher::Delegate { | 28 public DirectoryWatcher::Delegate { |
24 protected: | 29 protected: |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // that notification doesn't come. | 123 // that notification doesn't come. |
119 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, | 124 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, |
120 kWaitForEventTime); | 125 kWaitForEventTime); |
121 loop_.Run(); | 126 loop_.Run(); |
122 | 127 |
123 ASSERT_EQ(directory_mods_, 0); | 128 ASSERT_EQ(directory_mods_, 0); |
124 } | 129 } |
125 | 130 |
126 // Verify that modifications to a subdirectory isn't noticed. | 131 // Verify that modifications to a subdirectory isn't noticed. |
127 TEST_F(DirectoryWatcherTest, SubDir) { | 132 TEST_F(DirectoryWatcherTest, SubDir) { |
128 FilePath subdir = test_dir_.Append(FILE_PATH_LITERAL("SubDir")); | 133 #if defined(OS_WIN) |
129 ASSERT_TRUE(file_util::CreateDirectory(subdir.value())); | 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")); |
| 141 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); |
130 | 142 |
131 DirectoryWatcher watcher; | 143 DirectoryWatcher watcher; |
132 ASSERT_TRUE(watcher.Watch(test_dir_, this)); | 144 ASSERT_TRUE(watcher.Watch(test_dir_, this)); |
133 // Write a file to the subdir. | 145 // Write a file to the subdir. |
134 FilePath test_path = subdir.Append(FILE_PATH_LITERAL("test_file")); | 146 FilePath test_path = subdir.Append(FILE_PATH_LITERAL("test_file")); |
135 WriteTestDirFile(test_path.value(), "some content"); | 147 WriteTestDirFile(test_path.value(), "some content"); |
136 | 148 |
137 // We won't get a notification, so we just wait around a bit to verify | 149 // We won't get a notification, so we just wait around a bit to verify |
138 // that notification doesn't come. | 150 // that notification doesn't come. |
139 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, | 151 loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask, |
140 kWaitForEventTime); | 152 kWaitForEventTime); |
141 loop_.Run(); | 153 loop_.Run(); |
142 | 154 |
143 // We shouldn't have been notified and shouldn't have crashed. | 155 // We shouldn't have been notified and shouldn't have crashed. |
144 ASSERT_EQ(directory_mods_, 0); | 156 ASSERT_EQ(0, directory_mods_); |
145 } | 157 } |
146 | 158 |
147 namespace { | 159 namespace { |
148 // Used by the DeleteDuringNotify test below. | 160 // Used by the DeleteDuringNotify test below. |
149 // Deletes the DirectoryWatcher when it's notified. | 161 // Deletes the DirectoryWatcher when it's notified. |
150 class Deleter : public DirectoryWatcher::Delegate { | 162 class Deleter : public DirectoryWatcher::Delegate { |
151 public: | 163 public: |
152 Deleter(DirectoryWatcher* watcher) : watcher_(watcher) {} | 164 Deleter(DirectoryWatcher* watcher) : watcher_(watcher) {} |
153 virtual void OnDirectoryChanged(const FilePath& path) { | 165 virtual void OnDirectoryChanged(const FilePath& path) { |
154 watcher_.reset(NULL); | 166 watcher_.reset(NULL); |
(...skipping 10 matching lines...) Expand all Loading... |
165 Deleter deleter(watcher); // Takes ownership of watcher. | 177 Deleter deleter(watcher); // Takes ownership of watcher. |
166 ASSERT_TRUE(watcher->Watch(test_dir_, &deleter)); | 178 ASSERT_TRUE(watcher->Watch(test_dir_, &deleter)); |
167 | 179 |
168 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); | 180 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); |
169 LoopUntilModsEqual(2); | 181 LoopUntilModsEqual(2); |
170 | 182 |
171 // We win if we haven't crashed yet. | 183 // We win if we haven't crashed yet. |
172 // Might as well double-check it got deleted, too. | 184 // Might as well double-check it got deleted, too. |
173 ASSERT_TRUE(deleter.watcher_.get() == NULL); | 185 ASSERT_TRUE(deleter.watcher_.get() == NULL); |
174 } | 186 } |
OLD | NEW |