Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <aclapi.h> | 9 #include <aclapi.h> |
| 10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 | 122 |
| 123 private: | 123 private: |
| 124 scoped_refptr<NotificationCollector> collector_; | 124 scoped_refptr<NotificationCollector> collector_; |
| 125 | 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 126 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 127 }; | 127 }; |
| 128 | 128 |
| 129 void SetupWatchCallback(const FilePath& target, | 129 void SetupWatchCallback(const FilePath& target, |
| 130 FilePathWatcher* watcher, | 130 FilePathWatcher* watcher, |
| 131 TestDelegateBase* delegate, | 131 TestDelegateBase* delegate, |
| 132 bool recursive_watch, | |
| 132 bool* result, | 133 bool* result, |
| 133 base::WaitableEvent* completion) { | 134 base::WaitableEvent* completion) { |
| 134 *result = watcher->Watch(target, | 135 *result = watcher->Watch(target, recursive_watch, |
| 135 base::Bind(&TestDelegateBase::OnFileChanged, | 136 base::Bind(&TestDelegateBase::OnFileChanged, |
| 136 delegate->AsWeakPtr())); | 137 delegate->AsWeakPtr())); |
| 137 completion->Signal(); | 138 completion->Signal(); |
| 138 } | 139 } |
| 139 | 140 |
| 140 void QuitLoopWatchCallback(MessageLoop* loop, | 141 void QuitLoopWatchCallback(MessageLoop* loop, |
| 141 const FilePath& expected_path, | 142 const FilePath& expected_path, |
| 142 bool expected_error, | 143 bool expected_error, |
| 143 bool* flag, | 144 bool* flag, |
| 144 const FilePath& path, | 145 const FilePath& path, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 | 185 |
| 185 // Write |content| to |file|. Returns true on success. | 186 // Write |content| to |file|. Returns true on success. |
| 186 bool WriteFile(const FilePath& file, const std::string& content) { | 187 bool WriteFile(const FilePath& file, const std::string& content) { |
| 187 int write_size = file_util::WriteFile(file, content.c_str(), | 188 int write_size = file_util::WriteFile(file, content.c_str(), |
| 188 content.length()); | 189 content.length()); |
| 189 return write_size == static_cast<int>(content.length()); | 190 return write_size == static_cast<int>(content.length()); |
| 190 } | 191 } |
| 191 | 192 |
| 192 bool SetupWatch(const FilePath& target, | 193 bool SetupWatch(const FilePath& target, |
| 193 FilePathWatcher* watcher, | 194 FilePathWatcher* watcher, |
| 194 TestDelegateBase* delegate) WARN_UNUSED_RESULT; | 195 TestDelegateBase* delegate, |
| 196 bool recursive_watch) WARN_UNUSED_RESULT; | |
| 195 | 197 |
| 196 bool WaitForEvents() WARN_UNUSED_RESULT { | 198 bool WaitForEvents() WARN_UNUSED_RESULT { |
| 197 collector_->Reset(); | 199 collector_->Reset(); |
| 198 loop_.Run(); | 200 loop_.Run(); |
| 199 return collector_->Success(); | 201 return collector_->Success(); |
| 200 } | 202 } |
| 201 | 203 |
| 202 NotificationCollector* collector() { return collector_.get(); } | 204 NotificationCollector* collector() { return collector_.get(); } |
| 203 | 205 |
| 204 MessageLoop loop_; | 206 MessageLoop loop_; |
| 205 base::Thread file_thread_; | 207 base::Thread file_thread_; |
| 206 ScopedTempDir temp_dir_; | 208 ScopedTempDir temp_dir_; |
| 207 scoped_refptr<NotificationCollector> collector_; | 209 scoped_refptr<NotificationCollector> collector_; |
| 208 | 210 |
| 209 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherTest); | 211 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherTest); |
| 210 }; | 212 }; |
| 211 | 213 |
| 212 bool FilePathWatcherTest::SetupWatch(const FilePath& target, | 214 bool FilePathWatcherTest::SetupWatch(const FilePath& target, |
| 213 FilePathWatcher* watcher, | 215 FilePathWatcher* watcher, |
| 214 TestDelegateBase* delegate) { | 216 TestDelegateBase* delegate, |
| 217 bool recursive_watch) { | |
| 215 base::WaitableEvent completion(false, false); | 218 base::WaitableEvent completion(false, false); |
| 216 bool result; | 219 bool result; |
| 217 file_thread_.message_loop_proxy()->PostTask( | 220 file_thread_.message_loop_proxy()->PostTask( |
| 218 FROM_HERE, | 221 FROM_HERE, |
| 219 base::Bind(SetupWatchCallback, | 222 base::Bind(SetupWatchCallback, |
| 220 target, watcher, delegate, &result, &completion)); | 223 target, watcher, delegate, recursive_watch, &result, |
| 224 &completion)); | |
| 221 completion.Wait(); | 225 completion.Wait(); |
| 222 return result; | 226 return result; |
| 223 } | 227 } |
| 224 | 228 |
| 225 // Basic test: Create the file and verify that we notice. | 229 // Basic test: Create the file and verify that we notice. |
| 226 TEST_F(FilePathWatcherTest, NewFile) { | 230 TEST_F(FilePathWatcherTest, NewFile) { |
| 227 FilePathWatcher watcher; | 231 FilePathWatcher watcher; |
| 228 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 232 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 229 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 233 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 230 | 234 |
| 231 ASSERT_TRUE(WriteFile(test_file(), "content")); | 235 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 232 ASSERT_TRUE(WaitForEvents()); | 236 ASSERT_TRUE(WaitForEvents()); |
| 233 DeleteDelegateOnFileThread(delegate.release()); | 237 DeleteDelegateOnFileThread(delegate.release()); |
| 234 } | 238 } |
| 235 | 239 |
| 236 // Verify that modifying the file is caught. | 240 // Verify that modifying the file is caught. |
| 237 TEST_F(FilePathWatcherTest, ModifiedFile) { | 241 TEST_F(FilePathWatcherTest, ModifiedFile) { |
| 238 ASSERT_TRUE(WriteFile(test_file(), "content")); | 242 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 239 | 243 |
| 240 FilePathWatcher watcher; | 244 FilePathWatcher watcher; |
| 241 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 245 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 242 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 246 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 243 | 247 |
| 244 // Now make sure we get notified if the file is modified. | 248 // Now make sure we get notified if the file is modified. |
| 245 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 249 ASSERT_TRUE(WriteFile(test_file(), "new content")); |
| 246 ASSERT_TRUE(WaitForEvents()); | 250 ASSERT_TRUE(WaitForEvents()); |
| 247 DeleteDelegateOnFileThread(delegate.release()); | 251 DeleteDelegateOnFileThread(delegate.release()); |
| 248 } | 252 } |
| 249 | 253 |
| 250 // Verify that moving the file into place is caught. | 254 // Verify that moving the file into place is caught. |
| 251 TEST_F(FilePathWatcherTest, MovedFile) { | 255 TEST_F(FilePathWatcherTest, MovedFile) { |
| 252 FilePath source_file(temp_dir_.path().AppendASCII("source")); | 256 FilePath source_file(temp_dir_.path().AppendASCII("source")); |
| 253 ASSERT_TRUE(WriteFile(source_file, "content")); | 257 ASSERT_TRUE(WriteFile(source_file, "content")); |
| 254 | 258 |
| 255 FilePathWatcher watcher; | 259 FilePathWatcher watcher; |
| 256 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 260 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 257 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 261 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 258 | 262 |
| 259 // Now make sure we get notified if the file is modified. | 263 // Now make sure we get notified if the file is modified. |
| 260 ASSERT_TRUE(file_util::Move(source_file, test_file())); | 264 ASSERT_TRUE(file_util::Move(source_file, test_file())); |
| 261 ASSERT_TRUE(WaitForEvents()); | 265 ASSERT_TRUE(WaitForEvents()); |
| 262 DeleteDelegateOnFileThread(delegate.release()); | 266 DeleteDelegateOnFileThread(delegate.release()); |
| 263 } | 267 } |
| 264 | 268 |
| 265 TEST_F(FilePathWatcherTest, DeletedFile) { | 269 TEST_F(FilePathWatcherTest, DeletedFile) { |
| 266 ASSERT_TRUE(WriteFile(test_file(), "content")); | 270 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 267 | 271 |
| 268 FilePathWatcher watcher; | 272 FilePathWatcher watcher; |
| 269 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 273 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 270 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 274 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 271 | 275 |
| 272 // Now make sure we get notified if the file is deleted. | 276 // Now make sure we get notified if the file is deleted. |
| 273 file_util::Delete(test_file(), false); | 277 file_util::Delete(test_file(), false); |
| 274 ASSERT_TRUE(WaitForEvents()); | 278 ASSERT_TRUE(WaitForEvents()); |
| 275 DeleteDelegateOnFileThread(delegate.release()); | 279 DeleteDelegateOnFileThread(delegate.release()); |
| 276 } | 280 } |
| 277 | 281 |
| 278 // Used by the DeleteDuringNotify test below. | 282 // Used by the DeleteDuringNotify test below. |
| 279 // Deletes the FilePathWatcher when it's notified. | 283 // Deletes the FilePathWatcher when it's notified. |
| 280 class Deleter : public TestDelegateBase { | 284 class Deleter : public TestDelegateBase { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 297 MessageLoop* loop_; | 301 MessageLoop* loop_; |
| 298 | 302 |
| 299 DISALLOW_COPY_AND_ASSIGN(Deleter); | 303 DISALLOW_COPY_AND_ASSIGN(Deleter); |
| 300 }; | 304 }; |
| 301 | 305 |
| 302 // Verify that deleting a watcher during the callback doesn't crash. | 306 // Verify that deleting a watcher during the callback doesn't crash. |
| 303 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { | 307 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { |
| 304 FilePathWatcher* watcher = new FilePathWatcher; | 308 FilePathWatcher* watcher = new FilePathWatcher; |
| 305 // Takes ownership of watcher. | 309 // Takes ownership of watcher. |
| 306 scoped_ptr<Deleter> deleter(new Deleter(watcher, &loop_)); | 310 scoped_ptr<Deleter> deleter(new Deleter(watcher, &loop_)); |
| 307 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get())); | 311 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get(), false)); |
| 308 | 312 |
| 309 ASSERT_TRUE(WriteFile(test_file(), "content")); | 313 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 310 ASSERT_TRUE(WaitForEvents()); | 314 ASSERT_TRUE(WaitForEvents()); |
| 311 | 315 |
| 312 // We win if we haven't crashed yet. | 316 // We win if we haven't crashed yet. |
| 313 // Might as well double-check it got deleted, too. | 317 // Might as well double-check it got deleted, too. |
| 314 ASSERT_TRUE(deleter->watcher() == NULL); | 318 ASSERT_TRUE(deleter->watcher() == NULL); |
| 315 } | 319 } |
| 316 | 320 |
| 317 // Verify that deleting the watcher works even if there is a pending | 321 // Verify that deleting the watcher works even if there is a pending |
| 318 // notification. | 322 // notification. |
| 319 // Flaky on MacOS. http://crbug.com/85930 | 323 // Flaky on MacOS. http://crbug.com/85930 |
| 320 #if defined(OS_MACOSX) | 324 #if defined(OS_MACOSX) |
| 321 #define MAYBE_DestroyWithPendingNotification \ | 325 #define MAYBE_DestroyWithPendingNotification \ |
| 322 DISABLED_DestroyWithPendingNotification | 326 DISABLED_DestroyWithPendingNotification |
| 323 #else | 327 #else |
| 324 #define MAYBE_DestroyWithPendingNotification DestroyWithPendingNotification | 328 #define MAYBE_DestroyWithPendingNotification DestroyWithPendingNotification |
| 325 #endif | 329 #endif |
| 326 TEST_F(FilePathWatcherTest, MAYBE_DestroyWithPendingNotification) { | 330 TEST_F(FilePathWatcherTest, MAYBE_DestroyWithPendingNotification) { |
| 327 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 331 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 328 FilePathWatcher* watcher = new FilePathWatcher; | 332 FilePathWatcher* watcher = new FilePathWatcher; |
| 329 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get())); | 333 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get(), false)); |
| 330 ASSERT_TRUE(WriteFile(test_file(), "content")); | 334 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 331 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); | 335 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); |
| 332 DeleteDelegateOnFileThread(delegate.release()); | 336 DeleteDelegateOnFileThread(delegate.release()); |
| 333 } | 337 } |
| 334 | 338 |
| 335 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) { | 339 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) { |
| 336 FilePathWatcher watcher1, watcher2; | 340 FilePathWatcher watcher1, watcher2; |
| 337 scoped_ptr<TestDelegate> delegate1(new TestDelegate(collector())); | 341 scoped_ptr<TestDelegate> delegate1(new TestDelegate(collector())); |
| 338 scoped_ptr<TestDelegate> delegate2(new TestDelegate(collector())); | 342 scoped_ptr<TestDelegate> delegate2(new TestDelegate(collector())); |
| 339 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get())); | 343 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get(), false)); |
| 340 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get())); | 344 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get(), false)); |
| 341 | 345 |
| 342 ASSERT_TRUE(WriteFile(test_file(), "content")); | 346 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 343 ASSERT_TRUE(WaitForEvents()); | 347 ASSERT_TRUE(WaitForEvents()); |
| 344 DeleteDelegateOnFileThread(delegate1.release()); | 348 DeleteDelegateOnFileThread(delegate1.release()); |
| 345 DeleteDelegateOnFileThread(delegate2.release()); | 349 DeleteDelegateOnFileThread(delegate2.release()); |
| 346 } | 350 } |
| 347 | 351 |
| 348 // Verify that watching a file whose parent directory doesn't exist yet works if | 352 // Verify that watching a file whose parent directory doesn't exist yet works if |
| 349 // the directory and file are created eventually. | 353 // the directory and file are created eventually. |
| 350 TEST_F(FilePathWatcherTest, NonExistentDirectory) { | 354 TEST_F(FilePathWatcherTest, NonExistentDirectory) { |
| 351 FilePathWatcher watcher; | 355 FilePathWatcher watcher; |
| 352 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 356 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 353 FilePath file(dir.AppendASCII("file")); | 357 FilePath file(dir.AppendASCII("file")); |
| 354 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 358 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 355 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); | 359 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); |
| 356 | 360 |
| 357 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 361 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 358 | 362 |
| 359 ASSERT_TRUE(WriteFile(file, "content")); | 363 ASSERT_TRUE(WriteFile(file, "content")); |
| 360 | 364 |
| 361 VLOG(1) << "Waiting for file creation"; | 365 VLOG(1) << "Waiting for file creation"; |
| 362 ASSERT_TRUE(WaitForEvents()); | 366 ASSERT_TRUE(WaitForEvents()); |
| 363 | 367 |
| 364 ASSERT_TRUE(WriteFile(file, "content v2")); | 368 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 365 VLOG(1) << "Waiting for file change"; | 369 VLOG(1) << "Waiting for file change"; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 378 std::vector<std::string> dir_names; | 382 std::vector<std::string> dir_names; |
| 379 for (int i = 0; i < 20; i++) { | 383 for (int i = 0; i < 20; i++) { |
| 380 std::string dir(base::StringPrintf("d%d", i)); | 384 std::string dir(base::StringPrintf("d%d", i)); |
| 381 dir_names.push_back(dir); | 385 dir_names.push_back(dir); |
| 382 path = path.AppendASCII(dir); | 386 path = path.AppendASCII(dir); |
| 383 } | 387 } |
| 384 | 388 |
| 385 FilePathWatcher watcher; | 389 FilePathWatcher watcher; |
| 386 FilePath file(path.AppendASCII("file")); | 390 FilePath file(path.AppendASCII("file")); |
| 387 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 391 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 388 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); | 392 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); |
| 389 | 393 |
| 390 FilePath sub_path(temp_dir_.path()); | 394 FilePath sub_path(temp_dir_.path()); |
| 391 for (std::vector<std::string>::const_iterator d(dir_names.begin()); | 395 for (std::vector<std::string>::const_iterator d(dir_names.begin()); |
| 392 d != dir_names.end(); ++d) { | 396 d != dir_names.end(); ++d) { |
| 393 sub_path = sub_path.AppendASCII(*d); | 397 sub_path = sub_path.AppendASCII(*d); |
| 394 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); | 398 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); |
| 395 } | 399 } |
| 396 VLOG(1) << "Create File"; | 400 VLOG(1) << "Create File"; |
| 397 ASSERT_TRUE(WriteFile(file, "content")); | 401 ASSERT_TRUE(WriteFile(file, "content")); |
| 398 VLOG(1) << "Waiting for file creation"; | 402 VLOG(1) << "Waiting for file creation"; |
| 399 ASSERT_TRUE(WaitForEvents()); | 403 ASSERT_TRUE(WaitForEvents()); |
| 400 | 404 |
| 401 ASSERT_TRUE(WriteFile(file, "content v2")); | 405 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 402 VLOG(1) << "Waiting for file modification"; | 406 VLOG(1) << "Waiting for file modification"; |
| 403 ASSERT_TRUE(WaitForEvents()); | 407 ASSERT_TRUE(WaitForEvents()); |
| 404 DeleteDelegateOnFileThread(delegate.release()); | 408 DeleteDelegateOnFileThread(delegate.release()); |
| 405 } | 409 } |
| 406 | 410 |
| 407 #if defined(OS_MACOSX) | 411 #if defined(OS_MACOSX) |
| 408 // http://crbug.com/85930 | 412 // http://crbug.com/85930 |
| 409 #define DisappearingDirectory DISABLED_DisappearingDirectory | 413 #define DisappearingDirectory DISABLED_DisappearingDirectory |
| 410 #endif | 414 #endif |
| 411 TEST_F(FilePathWatcherTest, DisappearingDirectory) { | 415 TEST_F(FilePathWatcherTest, DisappearingDirectory) { |
| 412 FilePathWatcher watcher; | 416 FilePathWatcher watcher; |
| 413 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 417 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 414 FilePath file(dir.AppendASCII("file")); | 418 FilePath file(dir.AppendASCII("file")); |
| 415 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 419 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 416 ASSERT_TRUE(WriteFile(file, "content")); | 420 ASSERT_TRUE(WriteFile(file, "content")); |
| 417 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 421 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 418 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); | 422 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); |
| 419 | 423 |
| 420 ASSERT_TRUE(file_util::Delete(dir, true)); | 424 ASSERT_TRUE(file_util::Delete(dir, true)); |
| 421 ASSERT_TRUE(WaitForEvents()); | 425 ASSERT_TRUE(WaitForEvents()); |
| 422 DeleteDelegateOnFileThread(delegate.release()); | 426 DeleteDelegateOnFileThread(delegate.release()); |
| 423 } | 427 } |
| 424 | 428 |
| 425 // Tests that a file that is deleted and reappears is tracked correctly. | 429 // Tests that a file that is deleted and reappears is tracked correctly. |
| 426 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { | 430 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { |
| 427 ASSERT_TRUE(WriteFile(test_file(), "content")); | 431 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 428 FilePathWatcher watcher; | 432 FilePathWatcher watcher; |
| 429 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 433 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 430 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 434 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 431 | 435 |
| 432 ASSERT_TRUE(file_util::Delete(test_file(), false)); | 436 ASSERT_TRUE(file_util::Delete(test_file(), false)); |
| 433 VLOG(1) << "Waiting for file deletion"; | 437 VLOG(1) << "Waiting for file deletion"; |
| 434 ASSERT_TRUE(WaitForEvents()); | 438 ASSERT_TRUE(WaitForEvents()); |
| 435 | 439 |
| 436 ASSERT_TRUE(WriteFile(test_file(), "content")); | 440 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 437 VLOG(1) << "Waiting for file creation"; | 441 VLOG(1) << "Waiting for file creation"; |
| 438 ASSERT_TRUE(WaitForEvents()); | 442 ASSERT_TRUE(WaitForEvents()); |
| 439 DeleteDelegateOnFileThread(delegate.release()); | 443 DeleteDelegateOnFileThread(delegate.release()); |
| 440 } | 444 } |
| 441 | 445 |
| 442 TEST_F(FilePathWatcherTest, WatchDirectory) { | 446 TEST_F(FilePathWatcherTest, WatchDirectory) { |
| 443 FilePathWatcher watcher; | 447 FilePathWatcher watcher; |
| 444 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 448 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 445 FilePath file1(dir.AppendASCII("file1")); | 449 FilePath file1(dir.AppendASCII("file1")); |
| 446 FilePath file2(dir.AppendASCII("file2")); | 450 FilePath file2(dir.AppendASCII("file2")); |
| 447 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 451 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 448 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get())); | 452 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), false)); |
| 449 | 453 |
| 450 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 454 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 451 VLOG(1) << "Waiting for directory creation"; | 455 VLOG(1) << "Waiting for directory creation"; |
| 452 ASSERT_TRUE(WaitForEvents()); | 456 ASSERT_TRUE(WaitForEvents()); |
| 453 | 457 |
| 454 ASSERT_TRUE(WriteFile(file1, "content")); | 458 ASSERT_TRUE(WriteFile(file1, "content")); |
| 455 VLOG(1) << "Waiting for file1 creation"; | 459 VLOG(1) << "Waiting for file1 creation"; |
| 456 ASSERT_TRUE(WaitForEvents()); | 460 ASSERT_TRUE(WaitForEvents()); |
| 457 | 461 |
| 458 #if !defined(OS_MACOSX) | 462 #if !defined(OS_MACOSX) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 473 } | 477 } |
| 474 | 478 |
| 475 TEST_F(FilePathWatcherTest, MoveParent) { | 479 TEST_F(FilePathWatcherTest, MoveParent) { |
| 476 FilePathWatcher file_watcher; | 480 FilePathWatcher file_watcher; |
| 477 FilePathWatcher subdir_watcher; | 481 FilePathWatcher subdir_watcher; |
| 478 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 482 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 479 FilePath dest(temp_dir_.path().AppendASCII("dest")); | 483 FilePath dest(temp_dir_.path().AppendASCII("dest")); |
| 480 FilePath subdir(dir.AppendASCII("subdir")); | 484 FilePath subdir(dir.AppendASCII("subdir")); |
| 481 FilePath file(subdir.AppendASCII("file")); | 485 FilePath file(subdir.AppendASCII("file")); |
| 482 scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 486 scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
| 483 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get())); | 487 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get(), false)); |
| 484 scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 488 scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
| 485 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get())); | 489 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get(), |
| 490 false)); | |
| 486 | 491 |
| 487 // Setup a directory hierarchy. | 492 // Setup a directory hierarchy. |
| 488 ASSERT_TRUE(file_util::CreateDirectory(subdir)); | 493 ASSERT_TRUE(file_util::CreateDirectory(subdir)); |
| 489 ASSERT_TRUE(WriteFile(file, "content")); | 494 ASSERT_TRUE(WriteFile(file, "content")); |
| 490 VLOG(1) << "Waiting for file creation"; | 495 VLOG(1) << "Waiting for file creation"; |
| 491 ASSERT_TRUE(WaitForEvents()); | 496 ASSERT_TRUE(WaitForEvents()); |
| 492 | 497 |
| 493 // Move the parent directory. | 498 // Move the parent directory. |
| 494 file_util::Move(dir, dest); | 499 file_util::Move(dir, dest); |
| 495 VLOG(1) << "Waiting for directory move"; | 500 VLOG(1) << "Waiting for directory move"; |
| 496 ASSERT_TRUE(WaitForEvents()); | 501 ASSERT_TRUE(WaitForEvents()); |
| 497 DeleteDelegateOnFileThread(file_delegate.release()); | 502 DeleteDelegateOnFileThread(file_delegate.release()); |
| 498 DeleteDelegateOnFileThread(subdir_delegate.release()); | 503 DeleteDelegateOnFileThread(subdir_delegate.release()); |
| 499 } | 504 } |
| 500 | 505 |
| 506 #if defined(OS_WIN) | |
| 507 TEST_F(FilePathWatcherTest, RecursiveWatch) { | |
| 508 FilePathWatcher watcher; | |
| 509 FilePath dir(temp_dir_.path().AppendASCII("dir")); | |
| 510 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | |
| 511 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), true)); | |
| 512 | |
| 513 // Main directory("dir") creation. | |
| 514 ASSERT_TRUE(file_util::CreateDirectory(dir)); | |
| 515 ASSERT_TRUE(WaitForEvents()); | |
| 516 | |
| 517 // Create "$dir/file1". | |
| 518 FilePath file1(dir.AppendASCII("file1")); | |
| 519 ASSERT_TRUE(WriteFile(file1, "content")); | |
| 520 ASSERT_TRUE(WaitForEvents()); | |
| 521 | |
| 522 // Create "$dir/subdir". | |
| 523 FilePath subdir(dir.AppendASCII("subdir")); | |
| 524 ASSERT_TRUE(file_util::CreateDirectory(subdir)); | |
| 525 ASSERT_TRUE(WaitForEvents()); | |
| 526 | |
| 527 // Create "$dir/subdir/subdir_file1". | |
| 528 FilePath subdir_file1(subdir.AppendASCII("subdir_file1")); | |
| 529 ASSERT_TRUE(WriteFile(subdir_file1, "content")); | |
| 530 ASSERT_TRUE(WaitForEvents()); | |
| 531 | |
| 532 // Create "$dir/subdir/subdir_child_dir". | |
| 533 FilePath subdir_child_dir(subdir.AppendASCII("subdir_child_dir")); | |
| 534 ASSERT_TRUE(file_util::CreateDirectory(subdir_child_dir)); | |
| 535 ASSERT_TRUE(WaitForEvents()); | |
| 536 | |
| 537 // Create "$dir/subdir/subdir_child_dir/child_dir_file1". | |
| 538 FilePath child_dir_file1(subdir_child_dir.AppendASCII("child_dir_file1")); | |
| 539 ASSERT_TRUE(WriteFile(child_dir_file1, "content v2")); | |
| 540 ASSERT_TRUE(WaitForEvents()); | |
|
Mattias Nissler (ping if slow)
2012/12/04 09:27:35
Can you also test whether file modifications get d
kmadhusu
2012/12/04 18:47:05
Done. Added a block to write into child_dir_file1
| |
| 541 | |
| 542 // Delete "$dir/subdir/subdir_file1". | |
| 543 ASSERT_TRUE(file_util::Delete(subdir_file1, false)); | |
| 544 ASSERT_TRUE(WaitForEvents()); | |
| 545 | |
| 546 // Delete "$dir/subdir/subdir_child_dir/child_dir_file1". | |
| 547 ASSERT_TRUE(file_util::Delete(child_dir_file1, false)); | |
| 548 ASSERT_TRUE(WaitForEvents()); | |
| 549 DeleteDelegateOnFileThread(delegate.release()); | |
| 550 } | |
| 551 #else | |
| 552 TEST_F(FilePathWatcherTest, RecursiveWatch) { | |
| 553 FilePathWatcher watcher; | |
| 554 FilePath dir(temp_dir_.path().AppendASCII("dir")); | |
| 555 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | |
| 556 // Non-Windows implementaion does not support recursive watching. | |
| 557 ASSERT_FALSE(SetupWatch(dir, &watcher, delegate.get(), true)); | |
| 558 DeleteDelegateOnFileThread(delegate.release()); | |
| 559 } | |
| 560 #endif | |
| 561 | |
| 501 TEST_F(FilePathWatcherTest, MoveChild) { | 562 TEST_F(FilePathWatcherTest, MoveChild) { |
| 502 FilePathWatcher file_watcher; | 563 FilePathWatcher file_watcher; |
| 503 FilePathWatcher subdir_watcher; | 564 FilePathWatcher subdir_watcher; |
| 504 FilePath source_dir(temp_dir_.path().AppendASCII("source")); | 565 FilePath source_dir(temp_dir_.path().AppendASCII("source")); |
| 505 FilePath source_subdir(source_dir.AppendASCII("subdir")); | 566 FilePath source_subdir(source_dir.AppendASCII("subdir")); |
| 506 FilePath source_file(source_subdir.AppendASCII("file")); | 567 FilePath source_file(source_subdir.AppendASCII("file")); |
| 507 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); | 568 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); |
| 508 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); | 569 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); |
| 509 FilePath dest_file(dest_subdir.AppendASCII("file")); | 570 FilePath dest_file(dest_subdir.AppendASCII("file")); |
| 510 | 571 |
| 511 // Setup a directory hierarchy. | 572 // Setup a directory hierarchy. |
| 512 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); | 573 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); |
| 513 ASSERT_TRUE(WriteFile(source_file, "content")); | 574 ASSERT_TRUE(WriteFile(source_file, "content")); |
| 514 | 575 |
| 515 scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 576 scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
| 516 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get())); | 577 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get(), false)); |
| 517 scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 578 scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
| 518 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get())); | 579 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get(), |
| 580 false)); | |
| 519 | 581 |
| 520 // Move the directory into place, s.t. the watched file appears. | 582 // Move the directory into place, s.t. the watched file appears. |
| 521 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); | 583 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); |
| 522 ASSERT_TRUE(WaitForEvents()); | 584 ASSERT_TRUE(WaitForEvents()); |
| 523 DeleteDelegateOnFileThread(file_delegate.release()); | 585 DeleteDelegateOnFileThread(file_delegate.release()); |
| 524 DeleteDelegateOnFileThread(subdir_delegate.release()); | 586 DeleteDelegateOnFileThread(subdir_delegate.release()); |
| 525 } | 587 } |
| 526 | 588 |
| 527 #if !defined(OS_LINUX) | 589 #if !defined(OS_LINUX) |
| 528 // Linux implementation of FilePathWatcher doesn't catch attribute changes. | 590 // Linux implementation of FilePathWatcher doesn't catch attribute changes. |
| 529 // http://crbug.com/78043 | 591 // http://crbug.com/78043 |
| 530 | 592 |
| 531 // Verify that changing attributes on a file is caught | 593 // Verify that changing attributes on a file is caught |
| 532 TEST_F(FilePathWatcherTest, FileAttributesChanged) { | 594 TEST_F(FilePathWatcherTest, FileAttributesChanged) { |
| 533 ASSERT_TRUE(WriteFile(test_file(), "content")); | 595 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 534 FilePathWatcher watcher; | 596 FilePathWatcher watcher; |
| 535 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 597 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 536 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 598 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
| 537 | 599 |
| 538 // Now make sure we get notified if the file is modified. | 600 // Now make sure we get notified if the file is modified. |
| 539 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); | 601 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); |
| 540 ASSERT_TRUE(WaitForEvents()); | 602 ASSERT_TRUE(WaitForEvents()); |
| 541 DeleteDelegateOnFileThread(delegate.release()); | 603 DeleteDelegateOnFileThread(delegate.release()); |
| 542 } | 604 } |
| 543 | 605 |
| 544 #endif // !OS_LINUX | 606 #endif // !OS_LINUX |
| 545 | 607 |
| 546 #if defined(OS_LINUX) | 608 #if defined(OS_LINUX) |
| 547 | 609 |
| 548 // Verify that creating a symlink is caught. | 610 // Verify that creating a symlink is caught. |
| 549 TEST_F(FilePathWatcherTest, CreateLink) { | 611 TEST_F(FilePathWatcherTest, CreateLink) { |
| 550 FilePathWatcher watcher; | 612 FilePathWatcher watcher; |
| 551 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 613 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 552 // Note that we are watching the symlink | 614 // Note that we are watching the symlink |
| 553 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 615 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
| 554 | 616 |
| 555 // Now make sure we get notified if the link is created. | 617 // Now make sure we get notified if the link is created. |
| 556 // Note that test_file() doesn't have to exist. | 618 // Note that test_file() doesn't have to exist. |
| 557 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 619 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 558 ASSERT_TRUE(WaitForEvents()); | 620 ASSERT_TRUE(WaitForEvents()); |
| 559 DeleteDelegateOnFileThread(delegate.release()); | 621 DeleteDelegateOnFileThread(delegate.release()); |
| 560 } | 622 } |
| 561 | 623 |
| 562 // Verify that deleting a symlink is caught. | 624 // Verify that deleting a symlink is caught. |
| 563 TEST_F(FilePathWatcherTest, DeleteLink) { | 625 TEST_F(FilePathWatcherTest, DeleteLink) { |
| 564 // Unfortunately this test case only works if the link target exists. | 626 // Unfortunately this test case only works if the link target exists. |
| 565 // TODO(craig) fix this as part of crbug.com/91561. | 627 // TODO(craig) fix this as part of crbug.com/91561. |
| 566 ASSERT_TRUE(WriteFile(test_file(), "content")); | 628 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 567 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 629 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 568 FilePathWatcher watcher; | 630 FilePathWatcher watcher; |
| 569 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 631 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 570 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 632 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
| 571 | 633 |
| 572 // Now make sure we get notified if the link is deleted. | 634 // Now make sure we get notified if the link is deleted. |
| 573 ASSERT_TRUE(file_util::Delete(test_link(), false)); | 635 ASSERT_TRUE(file_util::Delete(test_link(), false)); |
| 574 ASSERT_TRUE(WaitForEvents()); | 636 ASSERT_TRUE(WaitForEvents()); |
| 575 DeleteDelegateOnFileThread(delegate.release()); | 637 DeleteDelegateOnFileThread(delegate.release()); |
| 576 } | 638 } |
| 577 | 639 |
| 578 // Verify that modifying a target file that a link is pointing to | 640 // Verify that modifying a target file that a link is pointing to |
| 579 // when we are watching the link is caught. | 641 // when we are watching the link is caught. |
| 580 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { | 642 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { |
| 581 ASSERT_TRUE(WriteFile(test_file(), "content")); | 643 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 582 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 644 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 583 FilePathWatcher watcher; | 645 FilePathWatcher watcher; |
| 584 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 646 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 585 // Note that we are watching the symlink. | 647 // Note that we are watching the symlink. |
| 586 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 648 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
| 587 | 649 |
| 588 // Now make sure we get notified if the file is modified. | 650 // Now make sure we get notified if the file is modified. |
| 589 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 651 ASSERT_TRUE(WriteFile(test_file(), "new content")); |
| 590 ASSERT_TRUE(WaitForEvents()); | 652 ASSERT_TRUE(WaitForEvents()); |
| 591 DeleteDelegateOnFileThread(delegate.release()); | 653 DeleteDelegateOnFileThread(delegate.release()); |
| 592 } | 654 } |
| 593 | 655 |
| 594 // Verify that creating a target file that a link is pointing to | 656 // Verify that creating a target file that a link is pointing to |
| 595 // when we are watching the link is caught. | 657 // when we are watching the link is caught. |
| 596 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { | 658 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { |
| 597 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 659 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 598 FilePathWatcher watcher; | 660 FilePathWatcher watcher; |
| 599 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 661 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 600 // Note that we are watching the symlink. | 662 // Note that we are watching the symlink. |
| 601 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 663 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
| 602 | 664 |
| 603 // Now make sure we get notified if the target file is created. | 665 // Now make sure we get notified if the target file is created. |
| 604 ASSERT_TRUE(WriteFile(test_file(), "content")); | 666 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 605 ASSERT_TRUE(WaitForEvents()); | 667 ASSERT_TRUE(WaitForEvents()); |
| 606 DeleteDelegateOnFileThread(delegate.release()); | 668 DeleteDelegateOnFileThread(delegate.release()); |
| 607 } | 669 } |
| 608 | 670 |
| 609 // Verify that deleting a target file that a link is pointing to | 671 // Verify that deleting a target file that a link is pointing to |
| 610 // when we are watching the link is caught. | 672 // when we are watching the link is caught. |
| 611 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { | 673 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { |
| 612 ASSERT_TRUE(WriteFile(test_file(), "content")); | 674 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 613 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 675 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 614 FilePathWatcher watcher; | 676 FilePathWatcher watcher; |
| 615 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 677 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 616 // Note that we are watching the symlink. | 678 // Note that we are watching the symlink. |
| 617 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 679 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
| 618 | 680 |
| 619 // Now make sure we get notified if the target file is deleted. | 681 // Now make sure we get notified if the target file is deleted. |
| 620 ASSERT_TRUE(file_util::Delete(test_file(), false)); | 682 ASSERT_TRUE(file_util::Delete(test_file(), false)); |
| 621 ASSERT_TRUE(WaitForEvents()); | 683 ASSERT_TRUE(WaitForEvents()); |
| 622 DeleteDelegateOnFileThread(delegate.release()); | 684 DeleteDelegateOnFileThread(delegate.release()); |
| 623 } | 685 } |
| 624 | 686 |
| 625 // Verify that watching a file whose parent directory is a link that | 687 // Verify that watching a file whose parent directory is a link that |
| 626 // doesn't exist yet works if the symlink is created eventually. | 688 // doesn't exist yet works if the symlink is created eventually. |
| 627 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { | 689 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { |
| 628 FilePathWatcher watcher; | 690 FilePathWatcher watcher; |
| 629 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 691 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 630 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 692 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
| 631 FilePath file(dir.AppendASCII("file")); | 693 FilePath file(dir.AppendASCII("file")); |
| 632 FilePath linkfile(link_dir.AppendASCII("file")); | 694 FilePath linkfile(link_dir.AppendASCII("file")); |
| 633 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 695 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 634 // dir/file should exist. | 696 // dir/file should exist. |
| 635 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 697 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 636 ASSERT_TRUE(WriteFile(file, "content")); | 698 ASSERT_TRUE(WriteFile(file, "content")); |
| 637 // Note that we are watching dir.lnk/file which doesn't exist yet. | 699 // Note that we are watching dir.lnk/file which doesn't exist yet. |
| 638 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); | 700 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
| 639 | 701 |
| 640 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 702 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); |
| 641 VLOG(1) << "Waiting for link creation"; | 703 VLOG(1) << "Waiting for link creation"; |
| 642 ASSERT_TRUE(WaitForEvents()); | 704 ASSERT_TRUE(WaitForEvents()); |
| 643 | 705 |
| 644 ASSERT_TRUE(WriteFile(file, "content v2")); | 706 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 645 VLOG(1) << "Waiting for file change"; | 707 VLOG(1) << "Waiting for file change"; |
| 646 ASSERT_TRUE(WaitForEvents()); | 708 ASSERT_TRUE(WaitForEvents()); |
| 647 | 709 |
| 648 ASSERT_TRUE(file_util::Delete(file, false)); | 710 ASSERT_TRUE(file_util::Delete(file, false)); |
| 649 VLOG(1) << "Waiting for file deletion"; | 711 VLOG(1) << "Waiting for file deletion"; |
| 650 ASSERT_TRUE(WaitForEvents()); | 712 ASSERT_TRUE(WaitForEvents()); |
| 651 DeleteDelegateOnFileThread(delegate.release()); | 713 DeleteDelegateOnFileThread(delegate.release()); |
| 652 } | 714 } |
| 653 | 715 |
| 654 // Verify that watching a file whose parent directory is a | 716 // Verify that watching a file whose parent directory is a |
| 655 // dangling symlink works if the directory is created eventually. | 717 // dangling symlink works if the directory is created eventually. |
| 656 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { | 718 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { |
| 657 FilePathWatcher watcher; | 719 FilePathWatcher watcher; |
| 658 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 720 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 659 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 721 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
| 660 FilePath file(dir.AppendASCII("file")); | 722 FilePath file(dir.AppendASCII("file")); |
| 661 FilePath linkfile(link_dir.AppendASCII("file")); | 723 FilePath linkfile(link_dir.AppendASCII("file")); |
| 662 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 724 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 663 // Now create the link from dir.lnk pointing to dir but | 725 // Now create the link from dir.lnk pointing to dir but |
| 664 // neither dir nor dir/file exist yet. | 726 // neither dir nor dir/file exist yet. |
| 665 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 727 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); |
| 666 // Note that we are watching dir.lnk/file. | 728 // Note that we are watching dir.lnk/file. |
| 667 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); | 729 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
| 668 | 730 |
| 669 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 731 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 670 ASSERT_TRUE(WriteFile(file, "content")); | 732 ASSERT_TRUE(WriteFile(file, "content")); |
| 671 VLOG(1) << "Waiting for dir/file creation"; | 733 VLOG(1) << "Waiting for dir/file creation"; |
| 672 ASSERT_TRUE(WaitForEvents()); | 734 ASSERT_TRUE(WaitForEvents()); |
| 673 | 735 |
| 674 ASSERT_TRUE(WriteFile(file, "content v2")); | 736 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 675 VLOG(1) << "Waiting for file change"; | 737 VLOG(1) << "Waiting for file change"; |
| 676 ASSERT_TRUE(WaitForEvents()); | 738 ASSERT_TRUE(WaitForEvents()); |
| 677 | 739 |
| 678 ASSERT_TRUE(file_util::Delete(file, false)); | 740 ASSERT_TRUE(file_util::Delete(file, false)); |
| 679 VLOG(1) << "Waiting for file deletion"; | 741 VLOG(1) << "Waiting for file deletion"; |
| 680 ASSERT_TRUE(WaitForEvents()); | 742 ASSERT_TRUE(WaitForEvents()); |
| 681 DeleteDelegateOnFileThread(delegate.release()); | 743 DeleteDelegateOnFileThread(delegate.release()); |
| 682 } | 744 } |
| 683 | 745 |
| 684 // Verify that watching a file with a symlink on the path | 746 // Verify that watching a file with a symlink on the path |
| 685 // to the file works. | 747 // to the file works. |
| 686 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { | 748 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { |
| 687 FilePathWatcher watcher; | 749 FilePathWatcher watcher; |
| 688 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 750 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 689 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 751 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
| 690 FilePath file(dir.AppendASCII("file")); | 752 FilePath file(dir.AppendASCII("file")); |
| 691 FilePath linkfile(link_dir.AppendASCII("file")); | 753 FilePath linkfile(link_dir.AppendASCII("file")); |
| 692 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 754 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 693 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 755 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 694 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 756 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); |
| 695 // Note that we are watching dir.lnk/file but the file doesn't exist yet. | 757 // Note that we are watching dir.lnk/file but the file doesn't exist yet. |
| 696 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); | 758 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
| 697 | 759 |
| 698 ASSERT_TRUE(WriteFile(file, "content")); | 760 ASSERT_TRUE(WriteFile(file, "content")); |
| 699 VLOG(1) << "Waiting for file creation"; | 761 VLOG(1) << "Waiting for file creation"; |
| 700 ASSERT_TRUE(WaitForEvents()); | 762 ASSERT_TRUE(WaitForEvents()); |
| 701 | 763 |
| 702 ASSERT_TRUE(WriteFile(file, "content v2")); | 764 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 703 VLOG(1) << "Waiting for file change"; | 765 VLOG(1) << "Waiting for file change"; |
| 704 ASSERT_TRUE(WaitForEvents()); | 766 ASSERT_TRUE(WaitForEvents()); |
| 705 | 767 |
| 706 ASSERT_TRUE(file_util::Delete(file, false)); | 768 ASSERT_TRUE(file_util::Delete(file, false)); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1")); | 875 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1")); |
| 814 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); | 876 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); |
| 815 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); | 877 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); |
| 816 // Setup a directory hierarchy. | 878 // Setup a directory hierarchy. |
| 817 ASSERT_TRUE(file_util::CreateDirectory(test_dir1)); | 879 ASSERT_TRUE(file_util::CreateDirectory(test_dir1)); |
| 818 ASSERT_TRUE(file_util::CreateDirectory(test_dir2)); | 880 ASSERT_TRUE(file_util::CreateDirectory(test_dir2)); |
| 819 ASSERT_TRUE(WriteFile(test_file, "content")); | 881 ASSERT_TRUE(WriteFile(test_file, "content")); |
| 820 | 882 |
| 821 FilePathWatcher watcher; | 883 FilePathWatcher watcher; |
| 822 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 884 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 823 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get())); | 885 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get(), false)); |
| 824 | 886 |
| 825 // We should not get notified in this case as it hasn't affected our ability | 887 // We should not get notified in this case as it hasn't affected our ability |
| 826 // to access the file. | 888 // to access the file. |
| 827 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false)); | 889 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false)); |
| 828 loop_.PostDelayedTask(FROM_HERE, | 890 loop_.PostDelayedTask(FROM_HERE, |
| 829 MessageLoop::QuitClosure(), | 891 MessageLoop::QuitClosure(), |
| 830 TestTimeouts::tiny_timeout()); | 892 TestTimeouts::tiny_timeout()); |
| 831 ASSERT_FALSE(WaitForEvents()); | 893 ASSERT_FALSE(WaitForEvents()); |
| 832 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); | 894 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); |
| 833 | 895 |
| 834 // We should get notified in this case because filepathwatcher can no | 896 // We should get notified in this case because filepathwatcher can no |
| 835 // longer access the file | 897 // longer access the file |
| 836 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 898 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
| 837 ASSERT_TRUE(WaitForEvents()); | 899 ASSERT_TRUE(WaitForEvents()); |
| 838 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 900 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
| 839 DeleteDelegateOnFileThread(delegate.release()); | 901 DeleteDelegateOnFileThread(delegate.release()); |
| 840 } | 902 } |
| 841 | 903 |
| 842 #endif // OS_MACOSX | 904 #endif // OS_MACOSX |
| 843 } // namespace | 905 } // namespace |
| 844 | 906 |
| 845 } // namespace files | 907 } // namespace files |
| 846 } // namespace base | 908 } // namespace base |
| OLD | NEW |