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

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

Issue 11348263: Change FilePathWatcher tests to use the new callback style FilePathWatcher::Watch(). (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: move deleter back where it was 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 | « no previous file | no next file » | 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 #include <set>
8
9 #if defined(OS_WIN) 7 #if defined(OS_WIN)
10 #include <windows.h> 8 #include <windows.h>
11 #include <aclapi.h> 9 #include <aclapi.h>
12 #elif defined(OS_POSIX) 10 #elif defined(OS_POSIX)
13 #include <sys/stat.h> 11 #include <sys/stat.h>
14 #endif 12 #endif
15 13
14 #include <set>
15
16 #include "base/basictypes.h" 16 #include "base/basictypes.h"
17 #include "base/bind.h" 17 #include "base/bind.h"
18 #include "base/bind_helpers.h" 18 #include "base/bind_helpers.h"
19 #include "base/compiler_specific.h" 19 #include "base/compiler_specific.h"
20 #include "base/file_path.h" 20 #include "base/file_path.h"
21 #include "base/file_util.h" 21 #include "base/file_util.h"
22 #include "base/files/scoped_temp_dir.h" 22 #include "base/files/scoped_temp_dir.h"
23 #include "base/message_loop.h" 23 #include "base/message_loop.h"
24 #include "base/message_loop_proxy.h" 24 #include "base/message_loop_proxy.h"
25 #include "base/path_service.h" 25 #include "base/path_service.h"
(...skipping 17 matching lines...) Expand all
43 class NotificationCollector 43 class NotificationCollector
44 : public base::RefCountedThreadSafe<NotificationCollector> { 44 : public base::RefCountedThreadSafe<NotificationCollector> {
45 public: 45 public:
46 NotificationCollector() 46 NotificationCollector()
47 : loop_(base::MessageLoopProxy::current()) {} 47 : loop_(base::MessageLoopProxy::current()) {}
48 48
49 // Called from the file thread by the delegates. 49 // Called from the file thread by the delegates.
50 void OnChange(TestDelegate* delegate) { 50 void OnChange(TestDelegate* delegate) {
51 loop_->PostTask(FROM_HERE, 51 loop_->PostTask(FROM_HERE,
52 base::Bind(&NotificationCollector::RecordChange, this, 52 base::Bind(&NotificationCollector::RecordChange, this,
53 make_scoped_refptr(delegate))); 53 base::Unretained(delegate)));
54 } 54 }
55 55
56 void Register(TestDelegate* delegate) { 56 void Register(TestDelegate* delegate) {
57 delegates_.insert(delegate); 57 delegates_.insert(delegate);
58 } 58 }
59 59
60 void Reset() { 60 void Reset() {
61 signaled_.clear(); 61 signaled_.clear();
62 } 62 }
63 63
64 bool Success() { 64 bool Success() {
65 return signaled_ == delegates_; 65 return signaled_ == delegates_;
66 } 66 }
67 67
68 private: 68 private:
69 friend class base::RefCountedThreadSafe<NotificationCollector>; 69 friend class base::RefCountedThreadSafe<NotificationCollector>;
70 ~NotificationCollector() {} 70 ~NotificationCollector() {}
71 71
72 void RecordChange(TestDelegate* delegate) { 72 void RecordChange(TestDelegate* delegate) {
73 // Warning: |delegate| is Unretained. Do not dereference.
73 ASSERT_TRUE(loop_->BelongsToCurrentThread()); 74 ASSERT_TRUE(loop_->BelongsToCurrentThread());
74 ASSERT_TRUE(delegates_.count(delegate)); 75 ASSERT_TRUE(delegates_.count(delegate));
75 signaled_.insert(delegate); 76 signaled_.insert(delegate);
76 77
77 // Check whether all delegates have been signaled. 78 // Check whether all delegates have been signaled.
78 if (signaled_ == delegates_) 79 if (signaled_ == delegates_)
79 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 80 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
80 } 81 }
81 82
82 // Set of registered delegates. 83 // Set of registered delegates.
83 std::set<TestDelegate*> delegates_; 84 std::set<TestDelegate*> delegates_;
84 85
85 // Set of signaled delegates. 86 // Set of signaled delegates.
86 std::set<TestDelegate*> signaled_; 87 std::set<TestDelegate*> signaled_;
87 88
88 // The loop we should break after all delegates signaled. 89 // The loop we should break after all delegates signaled.
89 scoped_refptr<base::MessageLoopProxy> loop_; 90 scoped_refptr<base::MessageLoopProxy> loop_;
90 }; 91 };
91 92
92 // A mock FilePathWatcher::Delegate for testing. I'd rather use gmock, but it's 93 // A mock class for testing. Gmock is not appropriate because it is not
93 // not thread safe for setting expectations, so the test code couldn't safely 94 // thread-safe for setting expectations. Thus the test code cannot safely
94 // reset expectations while the file watcher is running. In order to allow this, 95 // reset expectations while the file watcher is running.
95 // we keep simple thread safe status flags in TestDelegate. 96 // Instead, TestDelegate gets the notifications from FilePathWatcher and uses
96 class TestDelegate : public FilePathWatcher::Delegate { 97 // NotificationCollector to aggregate the results.
98 class TestDelegate : public SupportsWeakPtr<TestDelegate> {
97 public: 99 public:
98 // The message loop specified by |loop| will be quit if a notification is
99 // received while the delegate is |armed_|. Note that the testing code must
100 // guarantee |loop| outlives the file thread on which OnFilePathChanged runs.
101 explicit TestDelegate(NotificationCollector* collector) 100 explicit TestDelegate(NotificationCollector* collector)
102 : collector_(collector) { 101 : collector_(collector) {
103 collector_->Register(this); 102 collector_->Register(this);
104 } 103 }
104 ~TestDelegate() {}
105 105
106 virtual void OnFilePathChanged(const FilePath&) { 106 void OnFileChanged(const FilePath& path, bool error) {
107 collector_->OnChange(this); 107 if (error)
108 ADD_FAILURE() << "Error " << path.value();
109 else
110 collector_->OnChange(this);
108 } 111 }
109 112
110 virtual void OnFilePathError(const FilePath& path) {
111 ADD_FAILURE() << "Error " << path.value();
112 }
113
114 protected:
115 virtual ~TestDelegate() {}
116
117 private: 113 private:
118 scoped_refptr<NotificationCollector> collector_; 114 scoped_refptr<NotificationCollector> collector_;
119 115
120 DISALLOW_COPY_AND_ASSIGN(TestDelegate); 116 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
121 }; 117 };
122 118
123 void SetupWatchDelegate(const FilePath& target, 119 template <class T>
Mattias Nissler (ping if slow) 2012/11/29 10:45:34 Note: Instead of templates, you could use a Delega
Lei Zhang 2012/11/29 23:47:04 Done.
120 void SetupWatchCallback(const FilePath& target,
124 FilePathWatcher* watcher, 121 FilePathWatcher* watcher,
125 FilePathWatcher::Delegate* delegate, 122 T* delegate,
126 bool* result, 123 bool* result,
127 base::WaitableEvent* completion) { 124 base::WaitableEvent* completion) {
128 *result = watcher->Watch(target, delegate); 125 *result = watcher->Watch(target,
126 base::Bind(&T::OnFileChanged,
127 delegate->AsWeakPtr()));
129 completion->Signal(); 128 completion->Signal();
130 } 129 }
131 130
132 void SetupWatchCallback(const FilePath& target, 131 void SetupWatchCallbackForQuitCallback(
133 FilePathWatcher* watcher, 132 const FilePath& target,
134 const FilePathWatcher::Callback& callback) { 133 FilePathWatcher* watcher,
134 const FilePathWatcher::Callback& callback) {
135 ASSERT_TRUE(watcher->Watch(target, callback)); 135 ASSERT_TRUE(watcher->Watch(target, callback));
136 } 136 }
137 137
138 void QuitLoopWatchCallback(MessageLoop* loop, 138 void QuitLoopWatchCallback(MessageLoop* loop,
139 const FilePath& expected_path, 139 const FilePath& expected_path,
140 bool expected_error, 140 bool expected_error,
141 bool* flag, 141 bool* flag,
142 const FilePath& path, 142 const FilePath& path,
143 bool error) { 143 bool error) {
144 ASSERT_TRUE(flag); 144 ASSERT_TRUE(flag);
(...skipping 16 matching lines...) Expand all
161 base::Thread::Options options(MessageLoop::TYPE_IO, 0); 161 base::Thread::Options options(MessageLoop::TYPE_IO, 0);
162 ASSERT_TRUE(file_thread_.StartWithOptions(options)); 162 ASSERT_TRUE(file_thread_.StartWithOptions(options));
163 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 163 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
164 collector_ = new NotificationCollector(); 164 collector_ = new NotificationCollector();
165 } 165 }
166 166
167 virtual void TearDown() OVERRIDE { 167 virtual void TearDown() OVERRIDE {
168 loop_.RunUntilIdle(); 168 loop_.RunUntilIdle();
169 } 169 }
170 170
171 void DeleteDelegateOnFileThread(TestDelegate* delegate) {
172 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, delegate);
173 }
174
171 FilePath test_file() { 175 FilePath test_file() {
172 return temp_dir_.path().AppendASCII("FilePathWatcherTest"); 176 return temp_dir_.path().AppendASCII("FilePathWatcherTest");
173 } 177 }
174 178
175 FilePath test_link() { 179 FilePath test_link() {
176 return temp_dir_.path().AppendASCII("FilePathWatcherTest.lnk"); 180 return temp_dir_.path().AppendASCII("FilePathWatcherTest.lnk");
177 } 181 }
178 182
179 // Write |content| to |file|. Returns true on success. 183 // Write |content| to |file|. Returns true on success.
180 bool WriteFile(const FilePath& file, const std::string& content) { 184 bool WriteFile(const FilePath& file, const std::string& content) {
181 int write_size = file_util::WriteFile(file, content.c_str(), 185 int write_size = file_util::WriteFile(file, content.c_str(),
182 content.length()); 186 content.length());
183 return write_size == static_cast<int>(content.length()); 187 return write_size == static_cast<int>(content.length());
184 } 188 }
185 189
190 template <class T>
186 bool SetupWatch(const FilePath& target, 191 bool SetupWatch(const FilePath& target,
187 FilePathWatcher* watcher, 192 FilePathWatcher* watcher,
188 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { 193 T* delegate) WARN_UNUSED_RESULT;
189 base::WaitableEvent completion(false, false);
190 bool result;
191 file_thread_.message_loop_proxy()->PostTask(
192 FROM_HERE,
193 base::Bind(SetupWatchDelegate, target, watcher,
194 make_scoped_refptr(delegate), &result, &completion));
195 completion.Wait();
196 return result;
197 }
198 194
199 bool WaitForEvents() WARN_UNUSED_RESULT { 195 bool WaitForEvents() WARN_UNUSED_RESULT {
200 collector_->Reset(); 196 collector_->Reset();
201 loop_.Run(); 197 loop_.Run();
202 return collector_->Success(); 198 return collector_->Success();
203 } 199 }
204 200
205 NotificationCollector* collector() { return collector_.get(); } 201 NotificationCollector* collector() { return collector_.get(); }
206 202
207 MessageLoop loop_; 203 MessageLoop loop_;
208 base::Thread file_thread_; 204 base::Thread file_thread_;
209 ScopedTempDir temp_dir_; 205 ScopedTempDir temp_dir_;
210 scoped_refptr<NotificationCollector> collector_; 206 scoped_refptr<NotificationCollector> collector_;
207
208 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherTest);
211 }; 209 };
212 210
211 template <class T>
212 bool FilePathWatcherTest::SetupWatch(const FilePath& target,
213 FilePathWatcher* watcher,
214 T* delegate) {
215 base::WaitableEvent completion(false, false);
216 bool result;
217 file_thread_.message_loop_proxy()->PostTask(
218 FROM_HERE,
219 base::Bind(SetupWatchCallback<T>,
220 target, watcher, delegate, &result, &completion));
221 completion.Wait();
222 return result;
223 }
224
213 // Basic test: Create the file and verify that we notice. 225 // Basic test: Create the file and verify that we notice.
214 TEST_F(FilePathWatcherTest, NewFile) { 226 TEST_F(FilePathWatcherTest, NewFile) {
215 FilePathWatcher watcher; 227 FilePathWatcher watcher;
216 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 228 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
217 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 229 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
218 230
219 ASSERT_TRUE(WriteFile(test_file(), "content")); 231 ASSERT_TRUE(WriteFile(test_file(), "content"));
220 ASSERT_TRUE(WaitForEvents()); 232 ASSERT_TRUE(WaitForEvents());
233 DeleteDelegateOnFileThread(delegate.release());
221 } 234 }
222 235
223 // Verify that modifying the file is caught. 236 // Verify that modifying the file is caught.
224 TEST_F(FilePathWatcherTest, ModifiedFile) { 237 TEST_F(FilePathWatcherTest, ModifiedFile) {
225 ASSERT_TRUE(WriteFile(test_file(), "content")); 238 ASSERT_TRUE(WriteFile(test_file(), "content"));
226 239
227 FilePathWatcher watcher; 240 FilePathWatcher watcher;
228 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 241 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
229 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 242 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
230 243
231 // Now make sure we get notified if the file is modified. 244 // Now make sure we get notified if the file is modified.
232 ASSERT_TRUE(WriteFile(test_file(), "new content")); 245 ASSERT_TRUE(WriteFile(test_file(), "new content"));
233 ASSERT_TRUE(WaitForEvents()); 246 ASSERT_TRUE(WaitForEvents());
247 DeleteDelegateOnFileThread(delegate.release());
234 } 248 }
235 249
236 // Verify that moving the file into place is caught. 250 // Verify that moving the file into place is caught.
237 TEST_F(FilePathWatcherTest, MovedFile) { 251 TEST_F(FilePathWatcherTest, MovedFile) {
238 FilePath source_file(temp_dir_.path().AppendASCII("source")); 252 FilePath source_file(temp_dir_.path().AppendASCII("source"));
239 ASSERT_TRUE(WriteFile(source_file, "content")); 253 ASSERT_TRUE(WriteFile(source_file, "content"));
240 254
241 FilePathWatcher watcher; 255 FilePathWatcher watcher;
242 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 256 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
243 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 257 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
244 258
245 // Now make sure we get notified if the file is modified. 259 // Now make sure we get notified if the file is modified.
246 ASSERT_TRUE(file_util::Move(source_file, test_file())); 260 ASSERT_TRUE(file_util::Move(source_file, test_file()));
247 ASSERT_TRUE(WaitForEvents()); 261 ASSERT_TRUE(WaitForEvents());
262 DeleteDelegateOnFileThread(delegate.release());
248 } 263 }
249 264
250 TEST_F(FilePathWatcherTest, DeletedFile) { 265 TEST_F(FilePathWatcherTest, DeletedFile) {
251 ASSERT_TRUE(WriteFile(test_file(), "content")); 266 ASSERT_TRUE(WriteFile(test_file(), "content"));
252 267
253 FilePathWatcher watcher; 268 FilePathWatcher watcher;
254 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 269 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
255 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 270 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
256 271
257 // Now make sure we get notified if the file is deleted. 272 // Now make sure we get notified if the file is deleted.
258 file_util::Delete(test_file(), false); 273 file_util::Delete(test_file(), false);
259 ASSERT_TRUE(WaitForEvents()); 274 ASSERT_TRUE(WaitForEvents());
275 DeleteDelegateOnFileThread(delegate.release());
260 } 276 }
261 277
262 TEST_F(FilePathWatcherTest, Callback) { 278 TEST_F(FilePathWatcherTest, Callback) {
263 FilePathWatcher* watcher = new FilePathWatcher(); 279 FilePathWatcher* watcher = new FilePathWatcher();
264 bool called_back = false; 280 bool called_back = false;
265 281
266 MessageLoop* file_loop = file_thread_.message_loop(); 282 MessageLoop* file_loop = file_thread_.message_loop();
267 ASSERT_TRUE(file_loop); 283 ASSERT_TRUE(file_loop);
268 // The callback makes |loop_| quit on file events, and flips |called_back| 284 // The callback makes |loop_| quit on file events, and flips |called_back|
269 // to true. 285 // to true.
270 FilePathWatcher::Callback callback = base::Bind( 286 FilePathWatcher::Callback callback = base::Bind(
271 QuitLoopWatchCallback, &loop_, test_file(), false, &called_back); 287 QuitLoopWatchCallback, &loop_, test_file(), false, &called_back);
272 288
273 // Start watching on the file thread, and unblock the loop once the callback 289 // Start watching on the file thread, and unblock the loop once the callback
274 // has been installed. 290 // has been installed.
275 file_thread_.message_loop_proxy()->PostTaskAndReply( 291 file_thread_.message_loop_proxy()->PostTaskAndReply(
276 FROM_HERE, 292 FROM_HERE,
277 base::Bind(SetupWatchCallback, test_file(), watcher, callback), 293 base::Bind(SetupWatchCallbackForQuitCallback,
294 test_file(), watcher, callback),
278 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_))); 295 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_)));
279 loop_.Run(); 296 loop_.Run();
Mattias Nissler (ping if slow) 2012/11/29 10:45:34 There are now essentially two mechanisms for setti
Lei Zhang 2012/11/29 23:47:04 I deleted this test. All the tests have been switc
280 297
281 // The watch has been installed. Trigger a file event now, which will unblock 298 // The watch has been installed. Trigger a file event now, which will unblock
282 // the loop again. 299 // the loop again.
283 ASSERT_TRUE(WriteFile(test_file(), "content")); 300 ASSERT_TRUE(WriteFile(test_file(), "content"));
284 loop_.Run(); 301 loop_.Run();
285 EXPECT_TRUE(called_back); 302 EXPECT_TRUE(called_back);
286 303
287 // Multiple events might have triggered, meaning that multiple 304 // Multiple events might have triggered, meaning that multiple
288 // QuitLoopWatchCallback have been posted. The FilePathWatcher can only cancel 305 // QuitLoopWatchCallback have been posted. The FilePathWatcher can only cancel
289 // on the FILE thread, and thus that callback might still trigger after this 306 // on the FILE thread, and thus that callback might still trigger after this
290 // function returns. Make sure the |watcher| is deleted before returning and 307 // function returns. Make sure the |watcher| is deleted before returning and
291 // destroying |called_back|. 308 // destroying |called_back|.
292 // A better fix requires significant changes to the FilePathWatcher. 309 // A better fix requires significant changes to the FilePathWatcher.
293 // TODO(joaodasilva): fix the FPW interface. http://crbug.com/145653 310 // TODO(joaodasilva): fix the FPW interface. http://crbug.com/145653
294 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); 311 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
295 file_thread_.message_loop_proxy()->PostTaskAndReply( 312 file_thread_.message_loop_proxy()->PostTaskAndReply(
296 FROM_HERE, 313 FROM_HERE,
297 base::Bind(base::DoNothing), 314 base::Bind(base::DoNothing),
298 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_))); 315 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_)));
299 loop_.Run(); 316 loop_.Run();
300 } 317 }
301 318
302 // Used by the DeleteDuringNotify test below. 319 // Used by the DeleteDuringNotify test below.
303 // Deletes the FilePathWatcher when it's notified. 320 // Deletes the FilePathWatcher when it's notified.
304 class Deleter : public FilePathWatcher::Delegate { 321 class Deleter : public SupportsWeakPtr<Deleter> {
305 public: 322 public:
306 Deleter(FilePathWatcher* watcher, MessageLoop* loop) 323 Deleter(FilePathWatcher* watcher, MessageLoop* loop)
307 : watcher_(watcher), 324 : watcher_(watcher),
308 loop_(loop) { 325 loop_(loop) {
309 } 326 }
327 ~Deleter() {}
310 328
311 virtual void OnFilePathChanged(const FilePath& path) { 329 void OnFileChanged(const FilePath&, bool) {
312 watcher_.reset(); 330 watcher_.reset();
313 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 331 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
314 } 332 }
315 333
334 FilePathWatcher* watcher() const { return watcher_.get(); }
335
336 private:
316 scoped_ptr<FilePathWatcher> watcher_; 337 scoped_ptr<FilePathWatcher> watcher_;
317 MessageLoop* loop_; 338 MessageLoop* loop_;
318 339
319 private: 340 DISALLOW_COPY_AND_ASSIGN(Deleter);
320 virtual ~Deleter() {}
321 }; 341 };
322 342
323 // Verify that deleting a watcher during the callback doesn't crash. 343 // Verify that deleting a watcher during the callback doesn't crash.
324 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { 344 TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
325 FilePathWatcher* watcher = new FilePathWatcher; 345 FilePathWatcher* watcher = new FilePathWatcher;
326 // Takes ownership of watcher. 346 // Takes ownership of watcher.
327 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_)); 347 scoped_ptr<Deleter> deleter(new Deleter(watcher, &loop_));
328 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get())); 348 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get()));
329 349
330 ASSERT_TRUE(WriteFile(test_file(), "content")); 350 ASSERT_TRUE(WriteFile(test_file(), "content"));
331 ASSERT_TRUE(WaitForEvents()); 351 ASSERT_TRUE(WaitForEvents());
332 352
333 // We win if we haven't crashed yet. 353 // We win if we haven't crashed yet.
334 // Might as well double-check it got deleted, too. 354 // Might as well double-check it got deleted, too.
335 ASSERT_TRUE(deleter->watcher_.get() == NULL); 355 ASSERT_TRUE(deleter->watcher() == NULL);
336 } 356 }
337 357
338 // Verify that deleting the watcher works even if there is a pending 358 // Verify that deleting the watcher works even if there is a pending
339 // notification. 359 // notification.
340 // Flaky on MacOS. http://crbug.com/85930 360 // Flaky on MacOS. http://crbug.com/85930
341 #if defined(OS_MACOSX) 361 #if defined(OS_MACOSX)
342 #define MAYBE_DestroyWithPendingNotification DISABLED_DestroyWithPendingNotifica tion 362 #define MAYBE_DestroyWithPendingNotification \
363 DISABLED_DestroyWithPendingNotification
343 #else 364 #else
344 #define MAYBE_DestroyWithPendingNotification DestroyWithPendingNotification 365 #define MAYBE_DestroyWithPendingNotification DestroyWithPendingNotification
345 #endif 366 #endif
346 TEST_F(FilePathWatcherTest, MAYBE_DestroyWithPendingNotification) { 367 TEST_F(FilePathWatcherTest, MAYBE_DestroyWithPendingNotification) {
347 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 368 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
348 FilePathWatcher* watcher = new FilePathWatcher; 369 FilePathWatcher* watcher = new FilePathWatcher;
349 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get())); 370 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get()));
350 ASSERT_TRUE(WriteFile(test_file(), "content")); 371 ASSERT_TRUE(WriteFile(test_file(), "content"));
351 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); 372 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
373 DeleteDelegateOnFileThread(delegate.release());
352 } 374 }
353 375
354 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) { 376 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) {
355 FilePathWatcher watcher1, watcher2; 377 FilePathWatcher watcher1, watcher2;
356 scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector())); 378 scoped_ptr<TestDelegate> delegate1(new TestDelegate(collector()));
357 scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector())); 379 scoped_ptr<TestDelegate> delegate2(new TestDelegate(collector()));
358 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get())); 380 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get()));
359 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get())); 381 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get()));
360 382
361 ASSERT_TRUE(WriteFile(test_file(), "content")); 383 ASSERT_TRUE(WriteFile(test_file(), "content"));
362 ASSERT_TRUE(WaitForEvents()); 384 ASSERT_TRUE(WaitForEvents());
385 DeleteDelegateOnFileThread(delegate1.release());
386 DeleteDelegateOnFileThread(delegate2.release());
363 } 387 }
364 388
365 // Verify that watching a file whose parent directory doesn't exist yet works if 389 // Verify that watching a file whose parent directory doesn't exist yet works if
366 // the directory and file are created eventually. 390 // the directory and file are created eventually.
367 TEST_F(FilePathWatcherTest, NonExistentDirectory) { 391 TEST_F(FilePathWatcherTest, NonExistentDirectory) {
368 FilePathWatcher watcher; 392 FilePathWatcher watcher;
369 FilePath dir(temp_dir_.path().AppendASCII("dir")); 393 FilePath dir(temp_dir_.path().AppendASCII("dir"));
370 FilePath file(dir.AppendASCII("file")); 394 FilePath file(dir.AppendASCII("file"));
371 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 395 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
372 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); 396 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
373 397
374 ASSERT_TRUE(file_util::CreateDirectory(dir)); 398 ASSERT_TRUE(file_util::CreateDirectory(dir));
375 399
376 ASSERT_TRUE(WriteFile(file, "content")); 400 ASSERT_TRUE(WriteFile(file, "content"));
377 401
378 VLOG(1) << "Waiting for file creation"; 402 VLOG(1) << "Waiting for file creation";
379 ASSERT_TRUE(WaitForEvents()); 403 ASSERT_TRUE(WaitForEvents());
380 404
381 ASSERT_TRUE(WriteFile(file, "content v2")); 405 ASSERT_TRUE(WriteFile(file, "content v2"));
382 VLOG(1) << "Waiting for file change"; 406 VLOG(1) << "Waiting for file change";
383 ASSERT_TRUE(WaitForEvents()); 407 ASSERT_TRUE(WaitForEvents());
384 408
385 ASSERT_TRUE(file_util::Delete(file, false)); 409 ASSERT_TRUE(file_util::Delete(file, false));
386 VLOG(1) << "Waiting for file deletion"; 410 VLOG(1) << "Waiting for file deletion";
387 ASSERT_TRUE(WaitForEvents()); 411 ASSERT_TRUE(WaitForEvents());
412 DeleteDelegateOnFileThread(delegate.release());
388 } 413 }
389 414
390 // Exercises watch reconfiguration for the case that directories on the path 415 // Exercises watch reconfiguration for the case that directories on the path
391 // are rapidly created. 416 // are rapidly created.
392 TEST_F(FilePathWatcherTest, DirectoryChain) { 417 TEST_F(FilePathWatcherTest, DirectoryChain) {
393 FilePath path(temp_dir_.path()); 418 FilePath path(temp_dir_.path());
394 std::vector<std::string> dir_names; 419 std::vector<std::string> dir_names;
395 for (int i = 0; i < 20; i++) { 420 for (int i = 0; i < 20; i++) {
396 std::string dir(base::StringPrintf("d%d", i)); 421 std::string dir(base::StringPrintf("d%d", i));
397 dir_names.push_back(dir); 422 dir_names.push_back(dir);
398 path = path.AppendASCII(dir); 423 path = path.AppendASCII(dir);
399 } 424 }
400 425
401 FilePathWatcher watcher; 426 FilePathWatcher watcher;
402 FilePath file(path.AppendASCII("file")); 427 FilePath file(path.AppendASCII("file"));
403 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 428 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
404 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); 429 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
405 430
406 FilePath sub_path(temp_dir_.path()); 431 FilePath sub_path(temp_dir_.path());
407 for (std::vector<std::string>::const_iterator d(dir_names.begin()); 432 for (std::vector<std::string>::const_iterator d(dir_names.begin());
408 d != dir_names.end(); ++d) { 433 d != dir_names.end(); ++d) {
409 sub_path = sub_path.AppendASCII(*d); 434 sub_path = sub_path.AppendASCII(*d);
410 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); 435 ASSERT_TRUE(file_util::CreateDirectory(sub_path));
411 } 436 }
412 VLOG(1) << "Create File"; 437 VLOG(1) << "Create File";
413 ASSERT_TRUE(WriteFile(file, "content")); 438 ASSERT_TRUE(WriteFile(file, "content"));
414 VLOG(1) << "Waiting for file creation"; 439 VLOG(1) << "Waiting for file creation";
415 ASSERT_TRUE(WaitForEvents()); 440 ASSERT_TRUE(WaitForEvents());
416 441
417 ASSERT_TRUE(WriteFile(file, "content v2")); 442 ASSERT_TRUE(WriteFile(file, "content v2"));
418 VLOG(1) << "Waiting for file modification"; 443 VLOG(1) << "Waiting for file modification";
419 ASSERT_TRUE(WaitForEvents()); 444 ASSERT_TRUE(WaitForEvents());
445 DeleteDelegateOnFileThread(delegate.release());
420 } 446 }
421 447
422 #if defined(OS_MACOSX) 448 #if defined(OS_MACOSX)
423 // http://crbug.com/85930 449 // http://crbug.com/85930
424 #define DisappearingDirectory DISABLED_DisappearingDirectory 450 #define DisappearingDirectory DISABLED_DisappearingDirectory
425 #endif 451 #endif
426 TEST_F(FilePathWatcherTest, DisappearingDirectory) { 452 TEST_F(FilePathWatcherTest, DisappearingDirectory) {
427 FilePathWatcher watcher; 453 FilePathWatcher watcher;
428 FilePath dir(temp_dir_.path().AppendASCII("dir")); 454 FilePath dir(temp_dir_.path().AppendASCII("dir"));
429 FilePath file(dir.AppendASCII("file")); 455 FilePath file(dir.AppendASCII("file"));
430 ASSERT_TRUE(file_util::CreateDirectory(dir)); 456 ASSERT_TRUE(file_util::CreateDirectory(dir));
431 ASSERT_TRUE(WriteFile(file, "content")); 457 ASSERT_TRUE(WriteFile(file, "content"));
432 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 458 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
433 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); 459 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
434 460
435 ASSERT_TRUE(file_util::Delete(dir, true)); 461 ASSERT_TRUE(file_util::Delete(dir, true));
436 ASSERT_TRUE(WaitForEvents()); 462 ASSERT_TRUE(WaitForEvents());
463 DeleteDelegateOnFileThread(delegate.release());
437 } 464 }
438 465
439 // Tests that a file that is deleted and reappears is tracked correctly. 466 // Tests that a file that is deleted and reappears is tracked correctly.
440 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { 467 TEST_F(FilePathWatcherTest, DeleteAndRecreate) {
441 ASSERT_TRUE(WriteFile(test_file(), "content")); 468 ASSERT_TRUE(WriteFile(test_file(), "content"));
442 FilePathWatcher watcher; 469 FilePathWatcher watcher;
443 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 470 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
444 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 471 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
445 472
446 ASSERT_TRUE(file_util::Delete(test_file(), false)); 473 ASSERT_TRUE(file_util::Delete(test_file(), false));
447 VLOG(1) << "Waiting for file deletion"; 474 VLOG(1) << "Waiting for file deletion";
448 ASSERT_TRUE(WaitForEvents()); 475 ASSERT_TRUE(WaitForEvents());
449 476
450 ASSERT_TRUE(WriteFile(test_file(), "content")); 477 ASSERT_TRUE(WriteFile(test_file(), "content"));
451 VLOG(1) << "Waiting for file creation"; 478 VLOG(1) << "Waiting for file creation";
452 ASSERT_TRUE(WaitForEvents()); 479 ASSERT_TRUE(WaitForEvents());
480 DeleteDelegateOnFileThread(delegate.release());
453 } 481 }
454 482
455 TEST_F(FilePathWatcherTest, WatchDirectory) { 483 TEST_F(FilePathWatcherTest, WatchDirectory) {
456 FilePathWatcher watcher; 484 FilePathWatcher watcher;
457 FilePath dir(temp_dir_.path().AppendASCII("dir")); 485 FilePath dir(temp_dir_.path().AppendASCII("dir"));
458 FilePath file1(dir.AppendASCII("file1")); 486 FilePath file1(dir.AppendASCII("file1"));
459 FilePath file2(dir.AppendASCII("file2")); 487 FilePath file2(dir.AppendASCII("file2"));
460 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 488 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
461 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get())); 489 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get()));
462 490
463 ASSERT_TRUE(file_util::CreateDirectory(dir)); 491 ASSERT_TRUE(file_util::CreateDirectory(dir));
464 VLOG(1) << "Waiting for directory creation"; 492 VLOG(1) << "Waiting for directory creation";
465 ASSERT_TRUE(WaitForEvents()); 493 ASSERT_TRUE(WaitForEvents());
466 494
467 ASSERT_TRUE(WriteFile(file1, "content")); 495 ASSERT_TRUE(WriteFile(file1, "content"));
468 VLOG(1) << "Waiting for file1 creation"; 496 VLOG(1) << "Waiting for file1 creation";
469 ASSERT_TRUE(WaitForEvents()); 497 ASSERT_TRUE(WaitForEvents());
470 498
471 #if !defined(OS_MACOSX) 499 #if !defined(OS_MACOSX)
472 // Mac implementation does not detect files modified in a directory. 500 // Mac implementation does not detect files modified in a directory.
473 ASSERT_TRUE(WriteFile(file1, "content v2")); 501 ASSERT_TRUE(WriteFile(file1, "content v2"));
474 VLOG(1) << "Waiting for file1 modification"; 502 VLOG(1) << "Waiting for file1 modification";
475 ASSERT_TRUE(WaitForEvents()); 503 ASSERT_TRUE(WaitForEvents());
476 #endif // !OS_MACOSX 504 #endif // !OS_MACOSX
477 505
478 ASSERT_TRUE(file_util::Delete(file1, false)); 506 ASSERT_TRUE(file_util::Delete(file1, false));
479 VLOG(1) << "Waiting for file1 deletion"; 507 VLOG(1) << "Waiting for file1 deletion";
480 ASSERT_TRUE(WaitForEvents()); 508 ASSERT_TRUE(WaitForEvents());
481 509
482 ASSERT_TRUE(WriteFile(file2, "content")); 510 ASSERT_TRUE(WriteFile(file2, "content"));
483 VLOG(1) << "Waiting for file2 creation"; 511 VLOG(1) << "Waiting for file2 creation";
484 ASSERT_TRUE(WaitForEvents()); 512 ASSERT_TRUE(WaitForEvents());
513 DeleteDelegateOnFileThread(delegate.release());
485 } 514 }
486 515
487 TEST_F(FilePathWatcherTest, MoveParent) { 516 TEST_F(FilePathWatcherTest, MoveParent) {
488 FilePathWatcher file_watcher; 517 FilePathWatcher file_watcher;
489 FilePathWatcher subdir_watcher; 518 FilePathWatcher subdir_watcher;
490 FilePath dir(temp_dir_.path().AppendASCII("dir")); 519 FilePath dir(temp_dir_.path().AppendASCII("dir"));
491 FilePath dest(temp_dir_.path().AppendASCII("dest")); 520 FilePath dest(temp_dir_.path().AppendASCII("dest"));
492 FilePath subdir(dir.AppendASCII("subdir")); 521 FilePath subdir(dir.AppendASCII("subdir"));
493 FilePath file(subdir.AppendASCII("file")); 522 FilePath file(subdir.AppendASCII("file"));
494 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); 523 scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector()));
495 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get())); 524 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get()));
496 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); 525 scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
497 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get())); 526 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()));
498 527
499 // Setup a directory hierarchy. 528 // Setup a directory hierarchy.
500 ASSERT_TRUE(file_util::CreateDirectory(subdir)); 529 ASSERT_TRUE(file_util::CreateDirectory(subdir));
501 ASSERT_TRUE(WriteFile(file, "content")); 530 ASSERT_TRUE(WriteFile(file, "content"));
502 VLOG(1) << "Waiting for file creation"; 531 VLOG(1) << "Waiting for file creation";
503 ASSERT_TRUE(WaitForEvents()); 532 ASSERT_TRUE(WaitForEvents());
504 533
505 // Move the parent directory. 534 // Move the parent directory.
506 file_util::Move(dir, dest); 535 file_util::Move(dir, dest);
507 VLOG(1) << "Waiting for directory move"; 536 VLOG(1) << "Waiting for directory move";
508 ASSERT_TRUE(WaitForEvents()); 537 ASSERT_TRUE(WaitForEvents());
538 DeleteDelegateOnFileThread(file_delegate.release());
539 DeleteDelegateOnFileThread(subdir_delegate.release());
509 } 540 }
510 541
511 TEST_F(FilePathWatcherTest, MoveChild) { 542 TEST_F(FilePathWatcherTest, MoveChild) {
512 FilePathWatcher file_watcher; 543 FilePathWatcher file_watcher;
513 FilePathWatcher subdir_watcher; 544 FilePathWatcher subdir_watcher;
514 FilePath source_dir(temp_dir_.path().AppendASCII("source")); 545 FilePath source_dir(temp_dir_.path().AppendASCII("source"));
515 FilePath source_subdir(source_dir.AppendASCII("subdir")); 546 FilePath source_subdir(source_dir.AppendASCII("subdir"));
516 FilePath source_file(source_subdir.AppendASCII("file")); 547 FilePath source_file(source_subdir.AppendASCII("file"));
517 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); 548 FilePath dest_dir(temp_dir_.path().AppendASCII("dest"));
518 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); 549 FilePath dest_subdir(dest_dir.AppendASCII("subdir"));
519 FilePath dest_file(dest_subdir.AppendASCII("file")); 550 FilePath dest_file(dest_subdir.AppendASCII("file"));
520 551
521 // Setup a directory hierarchy. 552 // Setup a directory hierarchy.
522 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); 553 ASSERT_TRUE(file_util::CreateDirectory(source_subdir));
523 ASSERT_TRUE(WriteFile(source_file, "content")); 554 ASSERT_TRUE(WriteFile(source_file, "content"));
524 555
525 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); 556 scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector()));
526 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get())); 557 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get()));
527 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); 558 scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
528 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get())); 559 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()));
529 560
530 // Move the directory into place, s.t. the watched file appears. 561 // Move the directory into place, s.t. the watched file appears.
531 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); 562 ASSERT_TRUE(file_util::Move(source_dir, dest_dir));
532 ASSERT_TRUE(WaitForEvents()); 563 ASSERT_TRUE(WaitForEvents());
564 DeleteDelegateOnFileThread(file_delegate.release());
565 DeleteDelegateOnFileThread(subdir_delegate.release());
533 } 566 }
534 567
535 #if !defined(OS_LINUX) 568 #if !defined(OS_LINUX)
536 // Linux implementation of FilePathWatcher doesn't catch attribute changes. 569 // Linux implementation of FilePathWatcher doesn't catch attribute changes.
537 // http://crbug.com/78043 570 // http://crbug.com/78043
538 571
539 // Verify that changing attributes on a file is caught 572 // Verify that changing attributes on a file is caught
540 TEST_F(FilePathWatcherTest, FileAttributesChanged) { 573 TEST_F(FilePathWatcherTest, FileAttributesChanged) {
541 ASSERT_TRUE(WriteFile(test_file(), "content")); 574 ASSERT_TRUE(WriteFile(test_file(), "content"));
542 FilePathWatcher watcher; 575 FilePathWatcher watcher;
543 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 576 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
544 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 577 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
545 578
546 // Now make sure we get notified if the file is modified. 579 // Now make sure we get notified if the file is modified.
547 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); 580 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file()));
548 ASSERT_TRUE(WaitForEvents()); 581 ASSERT_TRUE(WaitForEvents());
582 DeleteDelegateOnFileThread(delegate.release());
549 } 583 }
550 584
551 #endif // !OS_LINUX 585 #endif // !OS_LINUX
552 586
553 #if defined(OS_LINUX) 587 #if defined(OS_LINUX)
554 588
555 // Verify that creating a symlink is caught. 589 // Verify that creating a symlink is caught.
556 TEST_F(FilePathWatcherTest, CreateLink) { 590 TEST_F(FilePathWatcherTest, CreateLink) {
557 FilePathWatcher watcher; 591 FilePathWatcher watcher;
558 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 592 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
559 // Note that we are watching the symlink 593 // Note that we are watching the symlink
560 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 594 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
561 595
562 // Now make sure we get notified if the link is created. 596 // Now make sure we get notified if the link is created.
563 // Note that test_file() doesn't have to exist. 597 // Note that test_file() doesn't have to exist.
564 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 598 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
565 ASSERT_TRUE(WaitForEvents()); 599 ASSERT_TRUE(WaitForEvents());
600 DeleteDelegateOnFileThread(delegate.release());
566 } 601 }
567 602
568 // Verify that deleting a symlink is caught. 603 // Verify that deleting a symlink is caught.
569 TEST_F(FilePathWatcherTest, DeleteLink) { 604 TEST_F(FilePathWatcherTest, DeleteLink) {
570 // Unfortunately this test case only works if the link target exists. 605 // Unfortunately this test case only works if the link target exists.
571 // TODO(craig) fix this as part of crbug.com/91561. 606 // TODO(craig) fix this as part of crbug.com/91561.
572 ASSERT_TRUE(WriteFile(test_file(), "content")); 607 ASSERT_TRUE(WriteFile(test_file(), "content"));
573 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 608 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
574 FilePathWatcher watcher; 609 FilePathWatcher watcher;
575 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 610 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
576 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 611 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
577 612
578 // Now make sure we get notified if the link is deleted. 613 // Now make sure we get notified if the link is deleted.
579 ASSERT_TRUE(file_util::Delete(test_link(), false)); 614 ASSERT_TRUE(file_util::Delete(test_link(), false));
580 ASSERT_TRUE(WaitForEvents()); 615 ASSERT_TRUE(WaitForEvents());
616 DeleteDelegateOnFileThread(delegate.release());
581 } 617 }
582 618
583 // Verify that modifying a target file that a link is pointing to 619 // Verify that modifying a target file that a link is pointing to
584 // when we are watching the link is caught. 620 // when we are watching the link is caught.
585 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { 621 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) {
586 ASSERT_TRUE(WriteFile(test_file(), "content")); 622 ASSERT_TRUE(WriteFile(test_file(), "content"));
587 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 623 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
588 FilePathWatcher watcher; 624 FilePathWatcher watcher;
589 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 625 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
590 // Note that we are watching the symlink. 626 // Note that we are watching the symlink.
591 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 627 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
592 628
593 // Now make sure we get notified if the file is modified. 629 // Now make sure we get notified if the file is modified.
594 ASSERT_TRUE(WriteFile(test_file(), "new content")); 630 ASSERT_TRUE(WriteFile(test_file(), "new content"));
595 ASSERT_TRUE(WaitForEvents()); 631 ASSERT_TRUE(WaitForEvents());
632 DeleteDelegateOnFileThread(delegate.release());
596 } 633 }
597 634
598 // Verify that creating a target file that a link is pointing to 635 // Verify that creating a target file that a link is pointing to
599 // when we are watching the link is caught. 636 // when we are watching the link is caught.
600 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { 637 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) {
601 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 638 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
602 FilePathWatcher watcher; 639 FilePathWatcher watcher;
603 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 640 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
604 // Note that we are watching the symlink. 641 // Note that we are watching the symlink.
605 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 642 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
606 643
607 // Now make sure we get notified if the target file is created. 644 // Now make sure we get notified if the target file is created.
608 ASSERT_TRUE(WriteFile(test_file(), "content")); 645 ASSERT_TRUE(WriteFile(test_file(), "content"));
609 ASSERT_TRUE(WaitForEvents()); 646 ASSERT_TRUE(WaitForEvents());
647 DeleteDelegateOnFileThread(delegate.release());
610 } 648 }
611 649
612 // Verify that deleting a target file that a link is pointing to 650 // Verify that deleting a target file that a link is pointing to
613 // when we are watching the link is caught. 651 // when we are watching the link is caught.
614 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { 652 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) {
615 ASSERT_TRUE(WriteFile(test_file(), "content")); 653 ASSERT_TRUE(WriteFile(test_file(), "content"));
616 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); 654 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link()));
617 FilePathWatcher watcher; 655 FilePathWatcher watcher;
618 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 656 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
619 // Note that we are watching the symlink. 657 // Note that we are watching the symlink.
620 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); 658 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get()));
621 659
622 // Now make sure we get notified if the target file is deleted. 660 // Now make sure we get notified if the target file is deleted.
623 ASSERT_TRUE(file_util::Delete(test_file(), false)); 661 ASSERT_TRUE(file_util::Delete(test_file(), false));
624 ASSERT_TRUE(WaitForEvents()); 662 ASSERT_TRUE(WaitForEvents());
663 DeleteDelegateOnFileThread(delegate.release());
625 } 664 }
626 665
627 // Verify that watching a file whose parent directory is a link that 666 // Verify that watching a file whose parent directory is a link that
628 // doesn't exist yet works if the symlink is created eventually. 667 // doesn't exist yet works if the symlink is created eventually.
629 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { 668 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) {
630 FilePathWatcher watcher; 669 FilePathWatcher watcher;
631 FilePath dir(temp_dir_.path().AppendASCII("dir")); 670 FilePath dir(temp_dir_.path().AppendASCII("dir"));
632 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); 671 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
633 FilePath file(dir.AppendASCII("file")); 672 FilePath file(dir.AppendASCII("file"));
634 FilePath linkfile(link_dir.AppendASCII("file")); 673 FilePath linkfile(link_dir.AppendASCII("file"));
635 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 674 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
636 // dir/file should exist. 675 // dir/file should exist.
637 ASSERT_TRUE(file_util::CreateDirectory(dir)); 676 ASSERT_TRUE(file_util::CreateDirectory(dir));
638 ASSERT_TRUE(WriteFile(file, "content")); 677 ASSERT_TRUE(WriteFile(file, "content"));
639 // Note that we are watching dir.lnk/file which doesn't exist yet. 678 // Note that we are watching dir.lnk/file which doesn't exist yet.
640 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); 679 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get()));
641 680
642 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); 681 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
643 VLOG(1) << "Waiting for link creation"; 682 VLOG(1) << "Waiting for link creation";
644 ASSERT_TRUE(WaitForEvents()); 683 ASSERT_TRUE(WaitForEvents());
645 684
646 ASSERT_TRUE(WriteFile(file, "content v2")); 685 ASSERT_TRUE(WriteFile(file, "content v2"));
647 VLOG(1) << "Waiting for file change"; 686 VLOG(1) << "Waiting for file change";
648 ASSERT_TRUE(WaitForEvents()); 687 ASSERT_TRUE(WaitForEvents());
649 688
650 ASSERT_TRUE(file_util::Delete(file, false)); 689 ASSERT_TRUE(file_util::Delete(file, false));
651 VLOG(1) << "Waiting for file deletion"; 690 VLOG(1) << "Waiting for file deletion";
652 ASSERT_TRUE(WaitForEvents()); 691 ASSERT_TRUE(WaitForEvents());
692 DeleteDelegateOnFileThread(delegate.release());
653 } 693 }
654 694
655 // Verify that watching a file whose parent directory is a 695 // Verify that watching a file whose parent directory is a
656 // dangling symlink works if the directory is created eventually. 696 // dangling symlink works if the directory is created eventually.
657 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { 697 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) {
658 FilePathWatcher watcher; 698 FilePathWatcher watcher;
659 FilePath dir(temp_dir_.path().AppendASCII("dir")); 699 FilePath dir(temp_dir_.path().AppendASCII("dir"));
660 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); 700 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
661 FilePath file(dir.AppendASCII("file")); 701 FilePath file(dir.AppendASCII("file"));
662 FilePath linkfile(link_dir.AppendASCII("file")); 702 FilePath linkfile(link_dir.AppendASCII("file"));
663 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 703 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
664 // Now create the link from dir.lnk pointing to dir but 704 // Now create the link from dir.lnk pointing to dir but
665 // neither dir nor dir/file exist yet. 705 // neither dir nor dir/file exist yet.
666 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); 706 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
667 // Note that we are watching dir.lnk/file. 707 // Note that we are watching dir.lnk/file.
668 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); 708 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get()));
669 709
670 ASSERT_TRUE(file_util::CreateDirectory(dir)); 710 ASSERT_TRUE(file_util::CreateDirectory(dir));
671 ASSERT_TRUE(WriteFile(file, "content")); 711 ASSERT_TRUE(WriteFile(file, "content"));
672 VLOG(1) << "Waiting for dir/file creation"; 712 VLOG(1) << "Waiting for dir/file creation";
673 ASSERT_TRUE(WaitForEvents()); 713 ASSERT_TRUE(WaitForEvents());
674 714
675 ASSERT_TRUE(WriteFile(file, "content v2")); 715 ASSERT_TRUE(WriteFile(file, "content v2"));
676 VLOG(1) << "Waiting for file change"; 716 VLOG(1) << "Waiting for file change";
677 ASSERT_TRUE(WaitForEvents()); 717 ASSERT_TRUE(WaitForEvents());
678 718
679 ASSERT_TRUE(file_util::Delete(file, false)); 719 ASSERT_TRUE(file_util::Delete(file, false));
680 VLOG(1) << "Waiting for file deletion"; 720 VLOG(1) << "Waiting for file deletion";
681 ASSERT_TRUE(WaitForEvents()); 721 ASSERT_TRUE(WaitForEvents());
722 DeleteDelegateOnFileThread(delegate.release());
682 } 723 }
683 724
684 // Verify that watching a file with a symlink on the path 725 // Verify that watching a file with a symlink on the path
685 // to the file works. 726 // to the file works.
686 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { 727 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) {
687 FilePathWatcher watcher; 728 FilePathWatcher watcher;
688 FilePath dir(temp_dir_.path().AppendASCII("dir")); 729 FilePath dir(temp_dir_.path().AppendASCII("dir"));
689 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); 730 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk"));
690 FilePath file(dir.AppendASCII("file")); 731 FilePath file(dir.AppendASCII("file"));
691 FilePath linkfile(link_dir.AppendASCII("file")); 732 FilePath linkfile(link_dir.AppendASCII("file"));
692 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 733 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
693 ASSERT_TRUE(file_util::CreateDirectory(dir)); 734 ASSERT_TRUE(file_util::CreateDirectory(dir));
694 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); 735 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir));
695 // Note that we are watching dir.lnk/file but the file doesn't exist yet. 736 // Note that we are watching dir.lnk/file but the file doesn't exist yet.
696 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); 737 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get()));
697 738
698 ASSERT_TRUE(WriteFile(file, "content")); 739 ASSERT_TRUE(WriteFile(file, "content"));
699 VLOG(1) << "Waiting for file creation"; 740 VLOG(1) << "Waiting for file creation";
700 ASSERT_TRUE(WaitForEvents()); 741 ASSERT_TRUE(WaitForEvents());
701 742
702 ASSERT_TRUE(WriteFile(file, "content v2")); 743 ASSERT_TRUE(WriteFile(file, "content v2"));
703 VLOG(1) << "Waiting for file change"; 744 VLOG(1) << "Waiting for file change";
704 ASSERT_TRUE(WaitForEvents()); 745 ASSERT_TRUE(WaitForEvents());
705 746
706 ASSERT_TRUE(file_util::Delete(file, false)); 747 ASSERT_TRUE(file_util::Delete(file, false));
707 VLOG(1) << "Waiting for file deletion"; 748 VLOG(1) << "Waiting for file deletion";
708 ASSERT_TRUE(WaitForEvents()); 749 ASSERT_TRUE(WaitForEvents());
750 DeleteDelegateOnFileThread(delegate.release());
709 } 751 }
710 752
711 #endif // OS_LINUX 753 #endif // OS_LINUX
712 754
713 enum Permission { 755 enum Permission {
714 Read, 756 Read,
715 Write, 757 Write,
716 Execute 758 Execute
717 }; 759 };
718 760
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 TEST_F(FilePathWatcherTest, DirAttributesChanged) { 853 TEST_F(FilePathWatcherTest, DirAttributesChanged) {
812 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1")); 854 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1"));
813 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); 855 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2"));
814 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); 856 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile"));
815 // Setup a directory hierarchy. 857 // Setup a directory hierarchy.
816 ASSERT_TRUE(file_util::CreateDirectory(test_dir1)); 858 ASSERT_TRUE(file_util::CreateDirectory(test_dir1));
817 ASSERT_TRUE(file_util::CreateDirectory(test_dir2)); 859 ASSERT_TRUE(file_util::CreateDirectory(test_dir2));
818 ASSERT_TRUE(WriteFile(test_file, "content")); 860 ASSERT_TRUE(WriteFile(test_file, "content"));
819 861
820 FilePathWatcher watcher; 862 FilePathWatcher watcher;
821 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 863 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector()));
822 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get())); 864 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get()));
823 865
824 // We should not get notified in this case as it hasn't affected our ability 866 // We should not get notified in this case as it hasn't affected our ability
825 // to access the file. 867 // to access the file.
826 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false)); 868 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
827 loop_.PostDelayedTask(FROM_HERE, 869 loop_.PostDelayedTask(FROM_HERE,
828 MessageLoop::QuitClosure(), 870 MessageLoop::QuitClosure(),
829 TestTimeouts::tiny_timeout()); 871 TestTimeouts::tiny_timeout());
830 ASSERT_FALSE(WaitForEvents()); 872 ASSERT_FALSE(WaitForEvents());
831 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); 873 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
832 874
833 // We should get notified in this case because filepathwatcher can no 875 // We should get notified in this case because filepathwatcher can no
834 // longer access the file 876 // longer access the file
835 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); 877 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
836 ASSERT_TRUE(WaitForEvents()); 878 ASSERT_TRUE(WaitForEvents());
837 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); 879 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
880 DeleteDelegateOnFileThread(delegate.release());
838 } 881 }
839 882
840 #endif // OS_MACOSX 883 #endif // OS_MACOSX
841 } // namespace 884 } // namespace
842 885
843 } // namespace files 886 } // namespace files
844 } // namespace base 887 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698