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