Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: base/files/file_path_watcher_browsertest.cc

Issue 11415066: FilePathWatcher::Watch() - Listen for sub directory changes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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;
Mattias Nissler (ping if slow) 2012/11/21 09:28:48 Can't you put the entire test into include guards?
kmadhusu 2012/11/22 01:39:41 Browser test is extensively using the deprecated F
501 #endif
502 // Main directory("dir") creation.
503 ASSERT_TRUE(file_util::CreateDirectory(dir));
504 ASSERT_TRUE(WaitForEvents());
505
506 // Create "$dir/file1".
507 FilePath file1(dir.AppendASCII("file1"));
508 ASSERT_TRUE(WriteFile(file1, "content"));
509 ASSERT_TRUE(WaitForEvents());
510
511 // Create "$dir/subdir".
512 FilePath subdir(dir.AppendASCII("subdir"));
513 ASSERT_TRUE(file_util::CreateDirectory(subdir));
514 ASSERT_TRUE(WaitForEvents());
515
516 // Create "$dir/subdir/subdir_file1".
517 FilePath subdir_file1(subdir.AppendASCII("subdir_file1"));
518 ASSERT_TRUE(WriteFile(subdir_file1, "content"));
519 ASSERT_TRUE(WaitForEvents());
520
521 // Create "$dir/subdir/subdir_child_dir".
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 "$dir/subdir/subdir_child_dir/child_dir_file1".
527 FilePath child_dir_file1(subdir_child_dir.AppendASCII("child_dir_file1"));
528 ASSERT_TRUE(WriteFile(child_dir_file1, "content v2"));
529 ASSERT_TRUE(WaitForEvents());
530
531 // Delete "$dir/subdir/subdir_file1".
532 ASSERT_TRUE(file_util::Delete(subdir_file1, false));
533 ASSERT_TRUE(WaitForEvents());
534
535 // Delete "$dir/subdir/subdir_child_dir/child_dir_file1".
536 ASSERT_TRUE(file_util::Delete(child_dir_file1, false));
537 ASSERT_TRUE(WaitForEvents());
538 }
539
487 TEST_F(FilePathWatcherTest, MoveParent) { 540 TEST_F(FilePathWatcherTest, MoveParent) {
488 FilePathWatcher file_watcher; 541 FilePathWatcher file_watcher;
489 FilePathWatcher subdir_watcher; 542 FilePathWatcher subdir_watcher;
490 FilePath dir(temp_dir_.path().AppendASCII("dir")); 543 FilePath dir(temp_dir_.path().AppendASCII("dir"));
491 FilePath dest(temp_dir_.path().AppendASCII("dest")); 544 FilePath dest(temp_dir_.path().AppendASCII("dest"));
492 FilePath subdir(dir.AppendASCII("subdir")); 545 FilePath subdir(dir.AppendASCII("subdir"));
493 FilePath file(subdir.AppendASCII("file")); 546 FilePath file(subdir.AppendASCII("file"));
494 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); 547 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
495 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get())); 548 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get(), false));
496 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); 549 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
497 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get())); 550 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get(),
551 false));
498 552
499 // Setup a directory hierarchy. 553 // Setup a directory hierarchy.
500 ASSERT_TRUE(file_util::CreateDirectory(subdir)); 554 ASSERT_TRUE(file_util::CreateDirectory(subdir));
501 ASSERT_TRUE(WriteFile(file, "content")); 555 ASSERT_TRUE(WriteFile(file, "content"));
502 VLOG(1) << "Waiting for file creation"; 556 VLOG(1) << "Waiting for file creation";
503 ASSERT_TRUE(WaitForEvents()); 557 ASSERT_TRUE(WaitForEvents());
504 558
505 // Move the parent directory. 559 // Move the parent directory.
506 file_util::Move(dir, dest); 560 file_util::Move(dir, dest);
507 VLOG(1) << "Waiting for directory move"; 561 VLOG(1) << "Waiting for directory move";
508 ASSERT_TRUE(WaitForEvents()); 562 ASSERT_TRUE(WaitForEvents());
509 } 563 }
510 564
511 TEST_F(FilePathWatcherTest, MoveChild) { 565 TEST_F(FilePathWatcherTest, MoveChild) {
512 FilePathWatcher file_watcher; 566 FilePathWatcher file_watcher;
513 FilePathWatcher subdir_watcher; 567 FilePathWatcher subdir_watcher;
514 FilePath source_dir(temp_dir_.path().AppendASCII("source")); 568 FilePath source_dir(temp_dir_.path().AppendASCII("source"));
515 FilePath source_subdir(source_dir.AppendASCII("subdir")); 569 FilePath source_subdir(source_dir.AppendASCII("subdir"));
516 FilePath source_file(source_subdir.AppendASCII("file")); 570 FilePath source_file(source_subdir.AppendASCII("file"));
517 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); 571 FilePath dest_dir(temp_dir_.path().AppendASCII("dest"));
518 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); 572 FilePath dest_subdir(dest_dir.AppendASCII("subdir"));
519 FilePath dest_file(dest_subdir.AppendASCII("file")); 573 FilePath dest_file(dest_subdir.AppendASCII("file"));
520 574
521 // Setup a directory hierarchy. 575 // Setup a directory hierarchy.
522 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); 576 ASSERT_TRUE(file_util::CreateDirectory(source_subdir));
523 ASSERT_TRUE(WriteFile(source_file, "content")); 577 ASSERT_TRUE(WriteFile(source_file, "content"));
524 578
525 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); 579 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
526 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get())); 580 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get(), false));
527 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); 581 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
528 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get())); 582 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get(),
583 false));
529 584
530 // Move the directory into place, s.t. the watched file appears. 585 // Move the directory into place, s.t. the watched file appears.
531 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); 586 ASSERT_TRUE(file_util::Move(source_dir, dest_dir));
532 ASSERT_TRUE(WaitForEvents()); 587 ASSERT_TRUE(WaitForEvents());
533 } 588 }
534 589
535 #if !defined(OS_LINUX) 590 #if !defined(OS_LINUX)
536 // Linux implementation of FilePathWatcher doesn't catch attribute changes. 591 // Linux implementation of FilePathWatcher doesn't catch attribute changes.
537 // http://crbug.com/78043 592 // http://crbug.com/78043
538 593
539 // Verify that changing attributes on a file is caught 594 // Verify that changing attributes on a file is caught
540 TEST_F(FilePathWatcherTest, FileAttributesChanged) { 595 TEST_F(FilePathWatcherTest, FileAttributesChanged) {
541 ASSERT_TRUE(WriteFile(test_file(), "content")); 596 ASSERT_TRUE(WriteFile(test_file(), "content"));
542 FilePathWatcher watcher; 597 FilePathWatcher watcher;
543 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 598 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
544 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 599 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false));
545 600
546 // Now make sure we get notified if the file is modified. 601 // Now make sure we get notified if the file is modified.
547 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); 602 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file()));
548 ASSERT_TRUE(WaitForEvents()); 603 ASSERT_TRUE(WaitForEvents());
549 } 604 }
550 605
551 #endif // !OS_LINUX 606 #endif // !OS_LINUX
552 607
553 #if defined(OS_LINUX) 608 #if defined(OS_LINUX)
554 609
555 // Verify that creating a symlink is caught. 610 // Verify that creating a symlink is caught.
556 TEST_F(FilePathWatcherTest, CreateLink) { 611 TEST_F(FilePathWatcherTest, CreateLink) {
557 FilePathWatcher watcher; 612 FilePathWatcher watcher;
558 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 613 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
559 // Note that we are watching the symlink 614 // Note that we are watching the symlink
560 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 615 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
561 616
562 // Now make sure we get notified if the link is created. 617 // Now make sure we get notified if the link is created.
563 // Note that test_file() doesn't have to exist. 618 // Note that test_file() doesn't have to exist.
564 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 619 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
565 ASSERT_TRUE(WaitForEvents()); 620 ASSERT_TRUE(WaitForEvents());
566 } 621 }
567 622
568 // Verify that deleting a symlink is caught. 623 // Verify that deleting a symlink is caught.
569 TEST_F(FilePathWatcherTest, DeleteLink) { 624 TEST_F(FilePathWatcherTest, DeleteLink) {
570 // Unfortunately this test case only works if the link target exists. 625 // Unfortunately this test case only works if the link target exists.
571 // TODO(craig) fix this as part of crbug.com/91561. 626 // TODO(craig) fix this as part of crbug.com/91561.
572 ASSERT_TRUE(WriteFile(test_file(), "content")); 627 ASSERT_TRUE(WriteFile(test_file(), "content"));
573 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 628 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
574 FilePathWatcher watcher; 629 FilePathWatcher watcher;
575 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 630 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
576 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 631 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
577 632
578 // Now make sure we get notified if the link is deleted. 633 // Now make sure we get notified if the link is deleted.
579 ASSERT_TRUE(file_util::Delete(test_link(), false)); 634 ASSERT_TRUE(file_util::Delete(test_link(), false));
580 ASSERT_TRUE(WaitForEvents()); 635 ASSERT_TRUE(WaitForEvents());
581 } 636 }
582 637
583 // Verify that modifying a target file that a link is pointing to 638 // Verify that modifying a target file that a link is pointing to
584 // when we are watching the link is caught. 639 // when we are watching the link is caught.
585 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { 640 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) {
586 ASSERT_TRUE(WriteFile(test_file(), "content")); 641 ASSERT_TRUE(WriteFile(test_file(), "content"));
587 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 642 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
588 FilePathWatcher watcher; 643 FilePathWatcher watcher;
589 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 644 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
590 // Note that we are watching the symlink. 645 // Note that we are watching the symlink.
591 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 646 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
592 647
593 // Now make sure we get notified if the file is modified. 648 // Now make sure we get notified if the file is modified.
594 ASSERT_TRUE(WriteFile(test_file(), "new content")); 649 ASSERT_TRUE(WriteFile(test_file(), "new content"));
595 ASSERT_TRUE(WaitForEvents()); 650 ASSERT_TRUE(WaitForEvents());
596 } 651 }
597 652
598 // Verify that creating a target file that a link is pointing to 653 // Verify that creating a target file that a link is pointing to
599 // when we are watching the link is caught. 654 // when we are watching the link is caught.
600 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { 655 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) {
601 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 656 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
602 FilePathWatcher watcher; 657 FilePathWatcher watcher;
603 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 658 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
604 // Note that we are watching the symlink. 659 // Note that we are watching the symlink.
605 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 660 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
606 661
607 // Now make sure we get notified if the target file is created. 662 // Now make sure we get notified if the target file is created.
608 ASSERT_TRUE(WriteFile(test_file(), "content")); 663 ASSERT_TRUE(WriteFile(test_file(), "content"));
609 ASSERT_TRUE(WaitForEvents()); 664 ASSERT_TRUE(WaitForEvents());
610 } 665 }
611 666
612 // Verify that deleting a target file that a link is pointing to 667 // Verify that deleting a target file that a link is pointing to
613 // when we are watching the link is caught. 668 // when we are watching the link is caught.
614 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { 669 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) {
615 ASSERT_TRUE(WriteFile(test_file(), "content")); 670 ASSERT_TRUE(WriteFile(test_file(), "content"));
616 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 671 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
617 FilePathWatcher watcher; 672 FilePathWatcher watcher;
618 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 673 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
619 // Note that we are watching the symlink. 674 // Note that we are watching the symlink.
620 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 675 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false));
621 676
622 // Now make sure we get notified if the target file is deleted. 677 // Now make sure we get notified if the target file is deleted.
623 ASSERT_TRUE(file_util::Delete(test_file(), false)); 678 ASSERT_TRUE(file_util::Delete(test_file(), false));
624 ASSERT_TRUE(WaitForEvents()); 679 ASSERT_TRUE(WaitForEvents());
625 } 680 }
626 681
627 // Verify that watching a file whose parent directory is a link that 682 // Verify that watching a file whose parent directory is a link that
628 // doesn't exist yet works if the symlink is created eventually. 683 // doesn't exist yet works if the symlink is created eventually.
629 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { 684 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) {
630 FilePathWatcher watcher; 685 FilePathWatcher watcher;
631 FilePath dir(temp_dir_.path().AppendASCII("dir")); 686 FilePath dir(temp_dir_.path().AppendASCII("dir"));
632 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); 687 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
633 FilePath file(dir.AppendASCII("file")); 688 FilePath file(dir.AppendASCII("file"));
634 FilePath linkfile(link_dir.AppendASCII("file")); 689 FilePath linkfile(link_dir.AppendASCII("file"));
635 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 690 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
636 // dir/file should exist. 691 // dir/file should exist.
637 ASSERT_TRUE(file_util::CreateDirectory(dir)); 692 ASSERT_TRUE(file_util::CreateDirectory(dir));
638 ASSERT_TRUE(WriteFile(file, "content")); 693 ASSERT_TRUE(WriteFile(file, "content"));
639 // Note that we are watching dir.lnk/file which doesn't exist yet. 694 // Note that we are watching dir.lnk/file which doesn't exist yet.
640 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); 695 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false));
641 696
642 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); 697 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
643 VLOG(1) << "Waiting for link creation"; 698 VLOG(1) << "Waiting for link creation";
644 ASSERT_TRUE(WaitForEvents()); 699 ASSERT_TRUE(WaitForEvents());
645 700
646 ASSERT_TRUE(WriteFile(file, "content v2")); 701 ASSERT_TRUE(WriteFile(file, "content v2"));
647 VLOG(1) << "Waiting for file change"; 702 VLOG(1) << "Waiting for file change";
648 ASSERT_TRUE(WaitForEvents()); 703 ASSERT_TRUE(WaitForEvents());
649 704
650 ASSERT_TRUE(file_util::Delete(file, false)); 705 ASSERT_TRUE(file_util::Delete(file, false));
651 VLOG(1) << "Waiting for file deletion"; 706 VLOG(1) << "Waiting for file deletion";
652 ASSERT_TRUE(WaitForEvents()); 707 ASSERT_TRUE(WaitForEvents());
653 } 708 }
654 709
655 // Verify that watching a file whose parent directory is a 710 // Verify that watching a file whose parent directory is a
656 // dangling symlink works if the directory is created eventually. 711 // dangling symlink works if the directory is created eventually.
657 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { 712 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) {
658 FilePathWatcher watcher; 713 FilePathWatcher watcher;
659 FilePath dir(temp_dir_.path().AppendASCII("dir")); 714 FilePath dir(temp_dir_.path().AppendASCII("dir"));
660 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); 715 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
661 FilePath file(dir.AppendASCII("file")); 716 FilePath file(dir.AppendASCII("file"));
662 FilePath linkfile(link_dir.AppendASCII("file")); 717 FilePath linkfile(link_dir.AppendASCII("file"));
663 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 718 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
664 // Now create the link from dir.lnk pointing to dir but 719 // Now create the link from dir.lnk pointing to dir but
665 // neither dir nor dir/file exist yet. 720 // neither dir nor dir/file exist yet.
666 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); 721 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
667 // Note that we are watching dir.lnk/file. 722 // Note that we are watching dir.lnk/file.
668 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); 723 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false));
669 724
670 ASSERT_TRUE(file_util::CreateDirectory(dir)); 725 ASSERT_TRUE(file_util::CreateDirectory(dir));
671 ASSERT_TRUE(WriteFile(file, "content")); 726 ASSERT_TRUE(WriteFile(file, "content"));
672 VLOG(1) << "Waiting for dir/file creation"; 727 VLOG(1) << "Waiting for dir/file creation";
673 ASSERT_TRUE(WaitForEvents()); 728 ASSERT_TRUE(WaitForEvents());
674 729
675 ASSERT_TRUE(WriteFile(file, "content v2")); 730 ASSERT_TRUE(WriteFile(file, "content v2"));
676 VLOG(1) << "Waiting for file change"; 731 VLOG(1) << "Waiting for file change";
677 ASSERT_TRUE(WaitForEvents()); 732 ASSERT_TRUE(WaitForEvents());
678 733
679 ASSERT_TRUE(file_util::Delete(file, false)); 734 ASSERT_TRUE(file_util::Delete(file, false));
680 VLOG(1) << "Waiting for file deletion"; 735 VLOG(1) << "Waiting for file deletion";
681 ASSERT_TRUE(WaitForEvents()); 736 ASSERT_TRUE(WaitForEvents());
682 } 737 }
683 738
684 // Verify that watching a file with a symlink on the path 739 // Verify that watching a file with a symlink on the path
685 // to the file works. 740 // to the file works.
686 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { 741 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) {
687 FilePathWatcher watcher; 742 FilePathWatcher watcher;
688 FilePath dir(temp_dir_.path().AppendASCII("dir")); 743 FilePath dir(temp_dir_.path().AppendASCII("dir"));
689 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); 744 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
690 FilePath file(dir.AppendASCII("file")); 745 FilePath file(dir.AppendASCII("file"));
691 FilePath linkfile(link_dir.AppendASCII("file")); 746 FilePath linkfile(link_dir.AppendASCII("file"));
692 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 747 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
693 ASSERT_TRUE(file_util::CreateDirectory(dir)); 748 ASSERT_TRUE(file_util::CreateDirectory(dir));
694 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); 749 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
695 // Note that we are watching dir.lnk/file but the file doesn't exist yet. 750 // Note that we are watching dir.lnk/file but the file doesn't exist yet.
696 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); 751 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false));
697 752
698 ASSERT_TRUE(WriteFile(file, "content")); 753 ASSERT_TRUE(WriteFile(file, "content"));
699 VLOG(1) << "Waiting for file creation"; 754 VLOG(1) << "Waiting for file creation";
700 ASSERT_TRUE(WaitForEvents()); 755 ASSERT_TRUE(WaitForEvents());
701 756
702 ASSERT_TRUE(WriteFile(file, "content v2")); 757 ASSERT_TRUE(WriteFile(file, "content v2"));
703 VLOG(1) << "Waiting for file change"; 758 VLOG(1) << "Waiting for file change";
704 ASSERT_TRUE(WaitForEvents()); 759 ASSERT_TRUE(WaitForEvents());
705 760
706 ASSERT_TRUE(file_util::Delete(file, false)); 761 ASSERT_TRUE(file_util::Delete(file, false));
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1")); 867 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1"));
813 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); 868 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2"));
814 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); 869 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile"));
815 // Setup a directory hierarchy. 870 // Setup a directory hierarchy.
816 ASSERT_TRUE(file_util::CreateDirectory(test_dir1)); 871 ASSERT_TRUE(file_util::CreateDirectory(test_dir1));
817 ASSERT_TRUE(file_util::CreateDirectory(test_dir2)); 872 ASSERT_TRUE(file_util::CreateDirectory(test_dir2));
818 ASSERT_TRUE(WriteFile(test_file, "content")); 873 ASSERT_TRUE(WriteFile(test_file, "content"));
819 874
820 FilePathWatcher watcher; 875 FilePathWatcher watcher;
821 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 876 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
822 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get())); 877 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get(), false));
823 878
824 // We should not get notified in this case as it hasn't affected our ability 879 // We should not get notified in this case as it hasn't affected our ability
825 // to access the file. 880 // to access the file.
826 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false)); 881 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
827 loop_.PostDelayedTask(FROM_HERE, 882 loop_.PostDelayedTask(FROM_HERE,
828 MessageLoop::QuitClosure(), 883 MessageLoop::QuitClosure(),
829 TestTimeouts::tiny_timeout()); 884 TestTimeouts::tiny_timeout());
830 ASSERT_FALSE(WaitForEvents()); 885 ASSERT_FALSE(WaitForEvents());
831 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); 886 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
832 887
833 // We should get notified in this case because filepathwatcher can no 888 // We should get notified in this case because filepathwatcher can no
834 // longer access the file 889 // longer access the file
835 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); 890 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
836 ASSERT_TRUE(WaitForEvents()); 891 ASSERT_TRUE(WaitForEvents());
837 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); 892 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
838 } 893 }
839 894
840 #endif // OS_MACOSX 895 #endif // OS_MACOSX
841 } // namespace 896 } // namespace
842 897
843 } // namespace files 898 } // namespace files
844 } // namespace base 899 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698