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