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

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