| 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 <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 150 |
| 151 // Write a file to the test dir. | 151 // Write a file to the test dir. |
| 152 SetExpectedNumberOfModifications(0); | 152 SetExpectedNumberOfModifications(0); |
| 153 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); | 153 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); |
| 154 VerifyExpectedNumberOfModifications(); | 154 VerifyExpectedNumberOfModifications(); |
| 155 } | 155 } |
| 156 | 156 |
| 157 TEST_F(DirectoryWatcherTest, SubDirRecursive) { | 157 TEST_F(DirectoryWatcherTest, SubDirRecursive) { |
| 158 FilePath subdir(FILE_PATH_LITERAL("SubDir")); | 158 FilePath subdir(FILE_PATH_LITERAL("SubDir")); |
| 159 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); | 159 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); |
| 160 | |
| 161 #if !defined(OS_WIN) | |
| 162 // TODO(port): Recursive watches are not implemented on Linux. | |
| 163 return; | |
| 164 #endif // !defined(OS_WIN) | |
| 165 | |
| 166 // Verify that modifications to a subdirectory are noticed by recursive watch. | 160 // Verify that modifications to a subdirectory are noticed by recursive watch. |
| 167 DirectoryWatcher watcher; | 161 DirectoryWatcher watcher; |
| 162 // Write a file to the subdir. |
| 163 FilePath test_path = subdir.AppendASCII("test_file"); |
| 168 ASSERT_TRUE(watcher.Watch(test_dir_, this, true)); | 164 ASSERT_TRUE(watcher.Watch(test_dir_, this, true)); |
| 169 // Write a file to the subdir. | |
| 170 SetExpectedNumberOfModifications(2); | 165 SetExpectedNumberOfModifications(2); |
| 171 FilePath test_path = subdir.AppendASCII("test_file"); | 166 // Sleep needs to go. |
| 167 // Since watcher.Watch recursive will watch subdirectory on another thread, |
| 168 // Sleep is needed to guarantee that we write content after we watch test_dir, |
| 169 // otherwise we will not see any inotify event |
| 170 // as the diretory is modified before watcher watch it. |
| 171 sleep(1); |
| 172 WriteTestDirFile(test_path.value(), "some content"); | 172 WriteTestDirFile(test_path.value(), "some content"); |
| 173 VerifyExpectedNumberOfModifications(); | 173 VerifyExpectedNumberOfModifications(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 TEST_F(DirectoryWatcherTest, SubDirNonRecursive) { | 176 TEST_F(DirectoryWatcherTest, SubDirNonRecursive) { |
| 177 #if defined(OS_WIN) | 177 #if defined(OS_WIN) |
| 178 // Disable this test for earlier version of Windows. It turned out to be | 178 // Disable this test for earlier version of Windows. It turned out to be |
| 179 // very difficult to create a reliable test for them. | 179 // very difficult to create a reliable test for them. |
| 180 if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) | 180 if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) |
| 181 return; | 181 return; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 loop_.Run(); | 230 loop_.Run(); |
| 231 | 231 |
| 232 // We win if we haven't crashed yet. | 232 // We win if we haven't crashed yet. |
| 233 // Might as well double-check it got deleted, too. | 233 // Might as well double-check it got deleted, too. |
| 234 ASSERT_TRUE(deleter.watcher_.get() == NULL); | 234 ASSERT_TRUE(deleter.watcher_.get() == NULL); |
| 235 } | 235 } |
| 236 | 236 |
| 237 TEST_F(DirectoryWatcherTest, MultipleWatchersSingleFile) { | 237 TEST_F(DirectoryWatcherTest, MultipleWatchersSingleFile) { |
| 238 DirectoryWatcher watcher1, watcher2; | 238 DirectoryWatcher watcher1, watcher2; |
| 239 ASSERT_TRUE(watcher1.Watch(test_dir_, this, false)); | 239 ASSERT_TRUE(watcher1.Watch(test_dir_, this, false)); |
| 240 ASSERT_TRUE(watcher2.Watch(test_dir_, this, false)); | 240 ASSERT_TRUE(watcher2.Watch(test_dir_, this, true)); |
| 241 | 241 |
| 242 SetExpectedNumberOfModifications(4); // Each watcher should fire twice. | 242 SetExpectedNumberOfModifications(4); // Each watcher should fire twice. |
| 243 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); | 243 WriteTestDirFile(FILE_PATH_LITERAL("test_file"), "some content"); |
| 244 VerifyExpectedNumberOfModifications(); | 244 VerifyExpectedNumberOfModifications(); |
| 245 } | 245 } |
| 246 | 246 |
| 247 TEST_F(DirectoryWatcherTest, MultipleWatchersDifferentFiles) { | 247 TEST_F(DirectoryWatcherTest, MultipleWatchersDifferentFiles) { |
| 248 const int kNumberOfWatchers = 5; | 248 const int kNumberOfWatchers = 5; |
| 249 DirectoryWatcher watchers[kNumberOfWatchers]; | 249 DirectoryWatcher watchers[kNumberOfWatchers]; |
| 250 FilePath subdirs[kNumberOfWatchers]; | 250 FilePath subdirs[kNumberOfWatchers]; |
| 251 bool recursive; |
| 251 for (int i = 0; i < kNumberOfWatchers; i++) { | 252 for (int i = 0; i < kNumberOfWatchers; i++) { |
| 252 subdirs[i] = FilePath(FILE_PATH_LITERAL("Dir")).AppendASCII(IntToString(i)); | 253 subdirs[i] = FilePath(FILE_PATH_LITERAL("Dir")).AppendASCII(IntToString(i)); |
| 253 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdirs[i]))); | 254 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdirs[i]))); |
| 254 | 255 |
| 255 ASSERT_TRUE(watchers[i].Watch(test_dir_.Append(subdirs[i]), this, false)); | 256 recursive = (i%2)?true:false; |
| 257 ASSERT_TRUE(watchers[i].Watch(test_dir_.Append(subdirs[i]), |
| 258 this, recursive)); |
| 256 } | 259 } |
| 257 for (int i = 0; i < kNumberOfWatchers; i++) { | 260 for (int i = 0; i < kNumberOfWatchers; i++) { |
| 258 // Verify that we only get modifications from one watcher (each watcher has | 261 // Verify that we only get modifications from one watcher (each watcher has |
| 259 // different directory). | 262 // different directory). |
| 260 | 263 |
| 261 ASSERT_EQ(0, directory_mods_); | 264 ASSERT_EQ(0, directory_mods_); |
| 262 | 265 |
| 263 // Write a file to the subdir. | 266 // Write a file to the subdir. |
| 264 FilePath test_path = subdirs[i].AppendASCII("test_file"); | 267 FilePath test_path = subdirs[i].AppendASCII("test_file"); |
| 265 SetExpectedNumberOfModifications(2); | 268 SetExpectedNumberOfModifications(2); |
| 266 WriteTestDirFile(test_path.value(), "some content"); | 269 WriteTestDirFile(test_path.value(), "some content"); |
| 267 VerifyExpectedNumberOfModifications(); | 270 VerifyExpectedNumberOfModifications(); |
| 268 | 271 |
| 269 directory_mods_ = 0; | 272 directory_mods_ = 0; |
| 270 } | 273 } |
| 271 } | 274 } |
| 272 | 275 |
| 276 // testdir |
| 277 // SubDir |
| 278 // SubDir2 |
| 279 // Move Subdir2 to be a child of SubDir, while |
| 280 // Both Subdir and SubDir2 are being watched recursively. |
| 281 // testdir |
| 282 // SubDir |
| 283 // SubDir2 |
| 284 TEST_F(DirectoryWatcherTest, MoveDirectoryAcrossWatchRecursive) { |
| 285 FilePath subdir(FILE_PATH_LITERAL("SubDir")); |
| 286 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir))); |
| 287 |
| 288 // Verify that modifications to a subdirectory are noticed by recursive watch. |
| 289 DirectoryWatcher watcher; |
| 290 ASSERT_TRUE(watcher.Watch(test_dir_.Append(subdir), this, true)); |
| 291 |
| 292 FilePath subdir2(FILE_PATH_LITERAL("SubDir2")); |
| 293 ASSERT_TRUE(file_util::CreateDirectory(test_dir_.Append(subdir2))); |
| 294 DirectoryWatcher watcher2; |
| 295 ASSERT_TRUE(watcher2.Watch(test_dir_.Append(subdir2), this, true)); |
| 296 |
| 297 SetExpectedNumberOfModifications(7); |
| 298 |
| 299 // Write a file to the subdirectories. |
| 300 FilePath test_path = subdir.AppendASCII("test_file"); |
| 301 WriteTestDirFile(test_path.value(), "some content"); |
| 302 FilePath test_path2 = subdir2.AppendASCII("test_file2"); |
| 303 WriteTestDirFile(test_path2.value(), "some content2"); |
| 304 ASSERT_TRUE((file_util::Move(test_dir_.Append(subdir2), |
| 305 test_dir_.Append(subdir)))); |
| 306 VerifyExpectedNumberOfModifications(); |
| 307 } |
| 308 |
| 273 // Verify that watching a directory that doesn't exist fails, but doesn't | 309 // Verify that watching a directory that doesn't exist fails, but doesn't |
| 274 // asssert. | 310 // asssert. |
| 275 // Basic test: add a file and verify we notice it. | 311 // Basic test: add a file and verify we notice it. |
| 276 TEST_F(DirectoryWatcherTest, NonExistentDirectory) { | 312 TEST_F(DirectoryWatcherTest, NonExistentDirectory) { |
| 277 DirectoryWatcher watcher; | 313 DirectoryWatcher watcher; |
| 278 ASSERT_FALSE(watcher.Watch(test_dir_.AppendASCII("does-not-exist"), this, | 314 ASSERT_FALSE(watcher.Watch(test_dir_.AppendASCII("does-not-exist"), this, |
| 279 false)); | 315 false)); |
| 280 } | 316 } |
| OLD | NEW |