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

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

Powered by Google App Engine
This is Rietveld 408576698