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 <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" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/platform_thread.h" | 14 #include "base/platform_thread.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
| 17 #include "base/win_util.h" | 17 #include "base/win_util.h" |
| 18 #endif // defined(OS_WIN) | 18 #endif // defined(OS_WIN) |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // For tests where we wait a bit to verify nothing happened | 23 // For tests where we wait a bit to verify nothing happened |
| 24 const int kWaitForEventTime = 1000; | 24 const int kWaitForEventTime = 1000; |
| 25 | 25 |
| 26 } // namespace | |
| 27 | |
| 26 class DirectoryWatcherTest : public testing::Test { | 28 class DirectoryWatcherTest : public testing::Test { |
| 27 public: | 29 public: |
| 28 // Implementation of DirectoryWatcher on Mac requires UI loop. | 30 // Implementation of DirectoryWatcher on Mac requires UI loop. |
| 29 DirectoryWatcherTest() : loop_(MessageLoop::TYPE_UI) { | 31 DirectoryWatcherTest() : loop_(MessageLoop::TYPE_UI) { |
| 30 } | 32 } |
| 31 | 33 |
| 32 void OnTestDelegateFirstNotification(const FilePath& path) { | 34 void OnTestDelegateFirstNotification(const FilePath& path) { |
| 33 notified_delegates_++; | 35 notified_delegates_++; |
| 34 if (notified_delegates_ >= expected_notified_delegates_) | 36 if (notified_delegates_ >= expected_notified_delegates_) |
| 35 MessageLoop::current()->Quit(); | 37 MessageLoop::current()->Quit(); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 | 204 |
| 203 // Write a file to the test dir. | 205 // Write a file to the test dir. |
| 204 SetExpectedNumberOfNotifiedDelegates(0); | 206 SetExpectedNumberOfNotifiedDelegates(0); |
| 205 ASSERT_TRUE(WriteTestFile(test_dir_.AppendASCII("test_file"), "content")); | 207 ASSERT_TRUE(WriteTestFile(test_dir_.AppendASCII("test_file"), "content")); |
| 206 VerifyExpectedNumberOfNotifiedDelegates(); | 208 VerifyExpectedNumberOfNotifiedDelegates(); |
| 207 } | 209 } |
| 208 | 210 |
| 209 TEST_F(DirectoryWatcherTest, SubDirRecursive) { | 211 TEST_F(DirectoryWatcherTest, SubDirRecursive) { |
| 210 FilePath subdir(CreateTestDirDirectoryASCII("SubDir", true)); | 212 FilePath subdir(CreateTestDirDirectoryASCII("SubDir", true)); |
| 211 | 213 |
| 212 #if defined(OS_LINUX) | |
| 213 // TODO(port): Recursive watches are not implemented on Linux. | |
| 214 return; | |
| 215 #endif // !defined(OS_WIN) | |
| 216 | |
| 217 // Verify that modifications to a subdirectory are noticed by recursive watch. | 214 // Verify that modifications to a subdirectory are noticed by recursive watch. |
| 218 TestDelegate delegate(this); | 215 TestDelegate delegate(this); |
| 219 DirectoryWatcher watcher; | 216 DirectoryWatcher watcher; |
| 220 ASSERT_TRUE(watcher.Watch(test_dir_, &delegate, true)); | 217 ASSERT_TRUE(watcher.Watch(test_dir_, &delegate, true)); |
| 221 // Write a file to the subdir. | 218 // Write a file to the subdir. |
| 222 SetExpectedNumberOfNotifiedDelegates(1); | 219 SetExpectedNumberOfNotifiedDelegates(1); |
| 220 watcher.EnsureSetupFinished(); | |
| 223 ASSERT_TRUE(WriteTestFile(subdir.AppendASCII("test_file"), "some content")); | 221 ASSERT_TRUE(WriteTestFile(subdir.AppendASCII("test_file"), "some content")); |
| 224 VerifyExpectedNumberOfNotifiedDelegates(); | 222 VerifyExpectedNumberOfNotifiedDelegates(); |
| 225 } | 223 } |
| 226 | 224 |
| 227 TEST_F(DirectoryWatcherTest, SubDirNonRecursive) { | 225 TEST_F(DirectoryWatcherTest, SubDirNonRecursive) { |
| 228 #if defined(OS_WIN) | 226 #if defined(OS_WIN) |
| 229 // Disable this test for earlier version of Windows. It turned out to be | 227 // Disable this test for earlier version of Windows. It turned out to be |
| 230 // very difficult to create a reliable test for them. | 228 // very difficult to create a reliable test for them. |
| 231 if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) | 229 if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) |
| 232 return; | 230 return; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 VerifyExpectedNumberOfNotifiedDelegates(); | 295 VerifyExpectedNumberOfNotifiedDelegates(); |
| 298 } | 296 } |
| 299 | 297 |
| 300 TEST_F(DirectoryWatcherTest, MultipleWatchersDifferentFiles) { | 298 TEST_F(DirectoryWatcherTest, MultipleWatchersDifferentFiles) { |
| 301 const int kNumberOfWatchers = 5; | 299 const int kNumberOfWatchers = 5; |
| 302 DirectoryWatcher watchers[kNumberOfWatchers]; | 300 DirectoryWatcher watchers[kNumberOfWatchers]; |
| 303 TestDelegate delegates[kNumberOfWatchers] = {this, this, this, this, this}; | 301 TestDelegate delegates[kNumberOfWatchers] = {this, this, this, this, this}; |
| 304 FilePath subdirs[kNumberOfWatchers]; | 302 FilePath subdirs[kNumberOfWatchers]; |
| 305 for (int i = 0; i < kNumberOfWatchers; i++) { | 303 for (int i = 0; i < kNumberOfWatchers; i++) { |
| 306 subdirs[i] = CreateTestDirDirectoryASCII("Dir" + IntToString(i), false); | 304 subdirs[i] = CreateTestDirDirectoryASCII("Dir" + IntToString(i), false); |
| 307 ASSERT_TRUE(watchers[i].Watch(subdirs[i], &delegates[i], false)); | 305 ASSERT_TRUE(watchers[i].Watch(subdirs[i], &delegates[i], ((i % 2) == 0))); |
| 308 } | 306 } |
| 309 for (int i = 0; i < kNumberOfWatchers; i++) { | 307 for (int i = 0; i < kNumberOfWatchers; i++) { |
| 310 // Verify that we only get modifications from one watcher (each watcher has | 308 // Verify that we only get modifications from one watcher (each watcher has |
| 311 // different directory). | 309 // different directory). |
| 312 | 310 |
| 313 for (int j = 0; j < kNumberOfWatchers; j++) | 311 for (int j = 0; j < kNumberOfWatchers; j++) |
| 314 delegates[j].reset(); | 312 delegates[j].reset(); |
| 315 | 313 |
| 316 // Write a file to the subdir. | 314 // Write a file to the subdir. |
| 317 SetExpectedNumberOfNotifiedDelegates(1); | 315 SetExpectedNumberOfNotifiedDelegates(1); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 336 ASSERT_TRUE(WriteTestFile(subdir.AppendASCII("test_file"), "some content")); | 334 ASSERT_TRUE(WriteTestFile(subdir.AppendASCII("test_file"), "some content")); |
| 337 VerifyExpectedNumberOfNotifiedDelegates(); | 335 VerifyExpectedNumberOfNotifiedDelegates(); |
| 338 | 336 |
| 339 delegate.reset(); | 337 delegate.reset(); |
| 340 | 338 |
| 341 // Verify that changes inside the subdir are noticed. | 339 // Verify that changes inside the subdir are noticed. |
| 342 SetExpectedNumberOfNotifiedDelegates(1); | 340 SetExpectedNumberOfNotifiedDelegates(1); |
| 343 ASSERT_TRUE(WriteTestFile(subdir.AppendASCII("test_file"), "other content")); | 341 ASSERT_TRUE(WriteTestFile(subdir.AppendASCII("test_file"), "other content")); |
| 344 VerifyExpectedNumberOfNotifiedDelegates(); | 342 VerifyExpectedNumberOfNotifiedDelegates(); |
| 345 } | 343 } |
| 344 #endif // defined(OS_WIN) || defined(OS_MACOSX) | |
| 346 | 345 |
| 347 TEST_F(DirectoryWatcherTest, RecursiveWatchDeletedSubdirectory) { | 346 TEST_F(DirectoryWatcherTest, RecursiveWatchDeletedSubdirectory) { |
| 348 FilePath subdir(CreateTestDirDirectoryASCII("SubDir", true)); | 347 FilePath subdir(CreateTestDirDirectoryASCII("SubDir", true)); |
| 349 | 348 |
| 350 TestDelegate delegate(this); | 349 TestDelegate delegate(this); |
| 351 DirectoryWatcher watcher; | 350 DirectoryWatcher watcher; |
| 352 ASSERT_TRUE(watcher.Watch(test_dir_, &delegate, true)); | 351 ASSERT_TRUE(watcher.Watch(test_dir_, &delegate, true)); |
| 353 | 352 |
| 354 // Write a file to the subdir. | 353 // Write a file to the subdir. |
| 355 SetExpectedNumberOfNotifiedDelegates(1); | 354 SetExpectedNumberOfNotifiedDelegates(1); |
| 355 watcher.EnsureSetupFinished(); | |
| 356 ASSERT_TRUE(WriteTestFile(subdir.AppendASCII("test_file"), "some content")); | 356 ASSERT_TRUE(WriteTestFile(subdir.AppendASCII("test_file"), "some content")); |
| 357 VerifyExpectedNumberOfNotifiedDelegates(); | 357 VerifyExpectedNumberOfNotifiedDelegates(); |
| 358 | 358 |
| 359 delegate.reset(); | 359 delegate.reset(); |
| 360 | 360 |
| 361 SetExpectedNumberOfNotifiedDelegates(1); | 361 SetExpectedNumberOfNotifiedDelegates(1); |
| 362 ASSERT_TRUE(file_util::Delete(subdir, true)); | 362 ASSERT_TRUE(file_util::Delete(subdir, true)); |
| 363 VerifyExpectedNumberOfNotifiedDelegates(); | 363 VerifyExpectedNumberOfNotifiedDelegates(); |
| 364 } | 364 } |
| 365 | 365 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 385 subdir2.AppendASCII("file"))); | 385 subdir2.AppendASCII("file"))); |
| 386 VerifyExpectedNumberOfNotifiedDelegates(); | 386 VerifyExpectedNumberOfNotifiedDelegates(); |
| 387 | 387 |
| 388 delegate1.reset(); | 388 delegate1.reset(); |
| 389 delegate2.reset(); | 389 delegate2.reset(); |
| 390 | 390 |
| 391 SetExpectedNumberOfNotifiedDelegates(1); | 391 SetExpectedNumberOfNotifiedDelegates(1); |
| 392 ASSERT_TRUE(WriteTestFile(subdir2.AppendASCII("file"), "other content")); | 392 ASSERT_TRUE(WriteTestFile(subdir2.AppendASCII("file"), "other content")); |
| 393 VerifyExpectedNumberOfNotifiedDelegates(); | 393 VerifyExpectedNumberOfNotifiedDelegates(); |
| 394 } | 394 } |
| 395 #endif // defined(OS_WIN) || defined(OS_MACOSX) | |
|
Paweł Hajdan Jr.
2009/05/12 07:26:37
Let's not move this line at this point. I will fix
| |
| 396 | 395 |
| 397 // Verify that watching a directory that doesn't exist fails, but doesn't | 396 // Verify that watching a directory that doesn't exist fails, but doesn't |
| 398 // asssert. | 397 // asssert. |
| 399 // Basic test: add a file and verify we notice it. | 398 // Basic test: add a file and verify we notice it. |
| 400 TEST_F(DirectoryWatcherTest, NonExistentDirectory) { | 399 TEST_F(DirectoryWatcherTest, NonExistentDirectory) { |
| 401 DirectoryWatcher watcher; | 400 DirectoryWatcher watcher; |
| 402 ASSERT_FALSE(watcher.Watch(test_dir_.AppendASCII("does-not-exist"), NULL, | 401 ASSERT_FALSE(watcher.Watch(test_dir_.AppendASCII("does-not-exist"), NULL, |
| 403 false)); | 402 false)); |
| 404 } | 403 } |
| 405 | 404 |
| 406 } // namespace | |
| OLD | NEW |