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

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

Issue 6793020: Move FilePathWatcher to base/files. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move OWNERS file and rebase to pick up latest changes Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "content/common/file_path_watcher/file_path_watcher.h" 5 #include "base/files/file_path_watcher.h"
dmac 2011/04/05 19:55:51 this was a browsertest because it locks up in fail
Craig 2011/04/06 15:27:30 OK. Should I wait for your fix or just turn this b
dmac 2011/04/06 16:19:54 For right now I would turn it back into a browsert
6 6
7 #include <set> 7 #include <set>
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <windows.h> 10 #include <windows.h>
11 #include <aclapi.h> 11 #include <aclapi.h>
12 #elif defined(OS_POSIX) 12 #elif defined(OS_POSIX)
13 #include <sys/stat.h> 13 #include <sys/stat.h>
14 #endif 14 #endif
dmac 2011/04/05 19:55:51 instead of prefixes everywhere, how about using :
Craig 2011/04/06 15:27:30 Yes, sorry about that. I've used "using ::base::fi
15 15
16 #include "base/basictypes.h" 16 #include "base/basictypes.h"
17 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
18 #include "base/file_path.h" 18 #include "base/file_path.h"
19 #include "base/file_util.h" 19 #include "base/file_util.h"
20 #include "base/memory/scoped_temp_dir.h" 20 #include "base/memory/scoped_temp_dir.h"
21 #include "base/message_loop.h" 21 #include "base/message_loop.h"
22 #include "base/message_loop_proxy.h" 22 #include "base/message_loop_proxy.h"
23 #include "base/path_service.h" 23 #include "base/path_service.h"
24 #include "base/string_util.h" 24 #include "base/string_util.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 std::set<TestDelegate*> signaled_; 78 std::set<TestDelegate*> signaled_;
79 79
80 // The loop we should break after all delegates signaled. 80 // The loop we should break after all delegates signaled.
81 scoped_refptr<base::MessageLoopProxy> loop_; 81 scoped_refptr<base::MessageLoopProxy> loop_;
82 }; 82 };
83 83
84 // A mock FilePathWatcher::Delegate for testing. I'd rather use gmock, but it's 84 // A mock FilePathWatcher::Delegate for testing. I'd rather use gmock, but it's
85 // not thread safe for setting expectations, so the test code couldn't safely 85 // not thread safe for setting expectations, so the test code couldn't safely
86 // reset expectations while the file watcher is running. In order to allow this, 86 // reset expectations while the file watcher is running. In order to allow this,
87 // we keep simple thread safe status flags in TestDelegate. 87 // we keep simple thread safe status flags in TestDelegate.
88 class TestDelegate : public FilePathWatcher::Delegate { 88 class TestDelegate : public base::files::FilePathWatcher::Delegate {
89 public: 89 public:
90 // The message loop specified by |loop| will be quit if a notification is 90 // The message loop specified by |loop| will be quit if a notification is
91 // received while the delegate is |armed_|. Note that the testing code must 91 // received while the delegate is |armed_|. Note that the testing code must
92 // guarantee |loop| outlives the file thread on which OnFilePathChanged runs. 92 // guarantee |loop| outlives the file thread on which OnFilePathChanged runs.
93 explicit TestDelegate(NotificationCollector* collector) 93 explicit TestDelegate(NotificationCollector* collector)
94 : collector_(collector) { 94 : collector_(collector) {
95 collector_->Register(this); 95 collector_->Register(this);
96 } 96 }
97 97
98 virtual void OnFilePathChanged(const FilePath&) { 98 virtual void OnFilePathChanged(const FilePath&) {
99 collector_->OnChange(this); 99 collector_->OnChange(this);
100 } 100 }
101 101
102 virtual void OnFilePathError(const FilePath& path) { 102 virtual void OnFilePathError(const FilePath& path) {
103 ADD_FAILURE() << "Error " << path.value(); 103 ADD_FAILURE() << "Error " << path.value();
104 } 104 }
105 105
106 private: 106 private:
107 scoped_refptr<NotificationCollector> collector_; 107 scoped_refptr<NotificationCollector> collector_;
108 108
109 DISALLOW_COPY_AND_ASSIGN(TestDelegate); 109 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
110 }; 110 };
111 111
112 // A helper class for setting up watches on the file thread. 112 // A helper class for setting up watches on the file thread.
113 class SetupWatchTask : public Task { 113 class SetupWatchTask : public Task {
114 public: 114 public:
115 SetupWatchTask(const FilePath& target, 115 SetupWatchTask(const FilePath& target,
116 FilePathWatcher* watcher, 116 base::files::FilePathWatcher* watcher,
117 FilePathWatcher::Delegate* delegate, 117 base::files::FilePathWatcher::Delegate* delegate,
118 bool* result, 118 bool* result,
119 base::WaitableEvent* completion) 119 base::WaitableEvent* completion)
120 : target_(target), 120 : target_(target),
121 watcher_(watcher), 121 watcher_(watcher),
122 delegate_(delegate), 122 delegate_(delegate),
123 result_(result), 123 result_(result),
124 completion_(completion) {} 124 completion_(completion) {}
125 125
126 void Run() { 126 void Run() {
127 *result_ = watcher_->Watch(target_, delegate_); 127 *result_ = watcher_->Watch(target_, delegate_);
128 completion_->Signal(); 128 completion_->Signal();
129 } 129 }
130 130
131 private: 131 private:
132 const FilePath target_; 132 const FilePath target_;
133 FilePathWatcher* watcher_; 133 base::files::FilePathWatcher* watcher_;
134 FilePathWatcher::Delegate* delegate_; 134 base::files::FilePathWatcher::Delegate* delegate_;
135 bool* result_; 135 bool* result_;
136 base::WaitableEvent* completion_; 136 base::WaitableEvent* completion_;
137 137
138 DISALLOW_COPY_AND_ASSIGN(SetupWatchTask); 138 DISALLOW_COPY_AND_ASSIGN(SetupWatchTask);
139 }; 139 };
140 140
141 class FilePathWatcherTest : public testing::Test { 141 class FilePathWatcherTest : public testing::Test {
142 public: 142 public:
143 FilePathWatcherTest() 143 FilePathWatcherTest()
144 : file_thread_("FilePathWatcherTest") {} 144 : file_thread_("FilePathWatcherTest") {}
(...skipping 17 matching lines...) Expand all
162 return temp_dir_.path().AppendASCII("FilePathWatcherTest"); 162 return temp_dir_.path().AppendASCII("FilePathWatcherTest");
163 } 163 }
164 164
165 // Write |content| to |file|. Returns true on success. 165 // Write |content| to |file|. Returns true on success.
166 bool WriteFile(const FilePath& file, const std::string& content) { 166 bool WriteFile(const FilePath& file, const std::string& content) {
167 int write_size = file_util::WriteFile(file, content.c_str(), 167 int write_size = file_util::WriteFile(file, content.c_str(),
168 content.length()); 168 content.length());
169 return write_size == static_cast<int>(content.length()); 169 return write_size == static_cast<int>(content.length());
170 } 170 }
171 171
172 bool SetupWatch(const FilePath& target, 172 bool SetupWatch(
173 FilePathWatcher* watcher, 173 const FilePath& target,
174 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { 174 base::files::FilePathWatcher* watcher,
175 base::files::FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT {
175 base::WaitableEvent completion(false, false); 176 base::WaitableEvent completion(false, false);
176 bool result; 177 bool result;
177 file_thread_.message_loop_proxy()->PostTask(FROM_HERE, 178 file_thread_.message_loop_proxy()->PostTask(FROM_HERE,
178 new SetupWatchTask(target, 179 new SetupWatchTask(target,
179 watcher, 180 watcher,
180 delegate, 181 delegate,
181 &result, 182 &result,
182 &completion)); 183 &completion));
183 completion.Wait(); 184 completion.Wait();
184 return result; 185 return result;
185 } 186 }
186 187
187 bool WaitForEvents() WARN_UNUSED_RESULT { 188 bool WaitForEvents() WARN_UNUSED_RESULT {
188 collector_->Reset(); 189 collector_->Reset();
189 loop_.Run(); 190 loop_.Run();
190 return collector_->Success(); 191 return collector_->Success();
191 } 192 }
192 193
193 NotificationCollector* collector() { return collector_.get(); } 194 NotificationCollector* collector() { return collector_.get(); }
194 195
195 MessageLoop loop_; 196 MessageLoop loop_;
196 base::Thread file_thread_; 197 base::Thread file_thread_;
197 ScopedTempDir temp_dir_; 198 ScopedTempDir temp_dir_;
198 scoped_refptr<NotificationCollector> collector_; 199 scoped_refptr<NotificationCollector> collector_;
199 }; 200 };
200 201
201 // Basic test: Create the file and verify that we notice. 202 // Basic test: Create the file and verify that we notice.
202 TEST_F(FilePathWatcherTest, NewFile) { 203 TEST_F(FilePathWatcherTest, NewFile) {
203 FilePathWatcher watcher; 204 base::files::FilePathWatcher watcher;
204 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 205 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
205 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 206 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
206 207
207 ASSERT_TRUE(WriteFile(test_file(), "content")); 208 ASSERT_TRUE(WriteFile(test_file(), "content"));
208 ASSERT_TRUE(WaitForEvents()); 209 ASSERT_TRUE(WaitForEvents());
209 } 210 }
210 211
211 // Verify that modifying the file is caught. 212 // Verify that modifying the file is caught.
212 TEST_F(FilePathWatcherTest, ModifiedFile) { 213 TEST_F(FilePathWatcherTest, ModifiedFile) {
213 ASSERT_TRUE(WriteFile(test_file(), "content")); 214 ASSERT_TRUE(WriteFile(test_file(), "content"));
214 215
215 FilePathWatcher watcher; 216 base::files::FilePathWatcher watcher;
216 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 217 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
217 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 218 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
218 219
219 // Now make sure we get notified if the file is modified. 220 // Now make sure we get notified if the file is modified.
220 ASSERT_TRUE(WriteFile(test_file(), "new content")); 221 ASSERT_TRUE(WriteFile(test_file(), "new content"));
221 ASSERT_TRUE(WaitForEvents()); 222 ASSERT_TRUE(WaitForEvents());
222 } 223 }
223 224
224 // Verify that moving the file into place is caught. 225 // Verify that moving the file into place is caught.
225 TEST_F(FilePathWatcherTest, MovedFile) { 226 TEST_F(FilePathWatcherTest, MovedFile) {
226 FilePath source_file(temp_dir_.path().AppendASCII("source")); 227 FilePath source_file(temp_dir_.path().AppendASCII("source"));
227 ASSERT_TRUE(WriteFile(source_file, "content")); 228 ASSERT_TRUE(WriteFile(source_file, "content"));
228 229
229 FilePathWatcher watcher; 230 base::files::FilePathWatcher watcher;
230 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 231 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
231 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 232 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
232 233
233 // Now make sure we get notified if the file is modified. 234 // Now make sure we get notified if the file is modified.
234 ASSERT_TRUE(file_util::Move(source_file, test_file())); 235 ASSERT_TRUE(file_util::Move(source_file, test_file()));
235 ASSERT_TRUE(WaitForEvents()); 236 ASSERT_TRUE(WaitForEvents());
236 } 237 }
237 238
238 TEST_F(FilePathWatcherTest, DeletedFile) { 239 TEST_F(FilePathWatcherTest, DeletedFile) {
239 ASSERT_TRUE(WriteFile(test_file(), "content")); 240 ASSERT_TRUE(WriteFile(test_file(), "content"));
240 241
241 FilePathWatcher watcher; 242 base::files::FilePathWatcher watcher;
242 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 243 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
243 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 244 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
244 245
245 // Now make sure we get notified if the file is deleted. 246 // Now make sure we get notified if the file is deleted.
246 file_util::Delete(test_file(), false); 247 file_util::Delete(test_file(), false);
247 ASSERT_TRUE(WaitForEvents()); 248 ASSERT_TRUE(WaitForEvents());
248 } 249 }
249 250
250 // Used by the DeleteDuringNotify test below. 251 // Used by the DeleteDuringNotify test below.
251 // Deletes the FilePathWatcher when it's notified. 252 // Deletes the FilePathWatcher when it's notified.
252 class Deleter : public FilePathWatcher::Delegate { 253 class Deleter : public base::files::FilePathWatcher::Delegate {
253 public: 254 public:
254 Deleter(FilePathWatcher* watcher, MessageLoop* loop) 255 Deleter(base::files::FilePathWatcher* watcher, MessageLoop* loop)
255 : watcher_(watcher), 256 : watcher_(watcher),
256 loop_(loop) { 257 loop_(loop) {
257 } 258 }
258 259
259 virtual void OnFilePathChanged(const FilePath& path) { 260 virtual void OnFilePathChanged(const FilePath& path) {
260 watcher_.reset(); 261 watcher_.reset();
261 loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 262 loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
262 } 263 }
263 264
264 scoped_ptr<FilePathWatcher> watcher_; 265 scoped_ptr<base::files::FilePathWatcher> watcher_;
265 MessageLoop* loop_; 266 MessageLoop* loop_;
266 }; 267 };
267 268
268 // Verify that deleting a watcher during the callback doesn't crash. 269 // Verify that deleting a watcher during the callback doesn't crash.
269 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { 270 TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
270 FilePathWatcher* watcher = new FilePathWatcher; 271 base::files::FilePathWatcher* watcher = new base::files::FilePathWatcher;
271 // Takes ownership of watcher. 272 // Takes ownership of watcher.
272 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_)); 273 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_));
273 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get())); 274 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get()));
274 275
275 ASSERT_TRUE(WriteFile(test_file(), "content")); 276 ASSERT_TRUE(WriteFile(test_file(), "content"));
276 ASSERT_TRUE(WaitForEvents()); 277 ASSERT_TRUE(WaitForEvents());
277 278
278 // We win if we haven't crashed yet. 279 // We win if we haven't crashed yet.
279 // Might as well double-check it got deleted, too. 280 // Might as well double-check it got deleted, too.
280 ASSERT_TRUE(deleter->watcher_.get() == NULL); 281 ASSERT_TRUE(deleter->watcher_.get() == NULL);
281 } 282 }
282 283
283 // Verify that deleting the watcher works even if there is a pending 284 // Verify that deleting the watcher works even if there is a pending
284 // notification. 285 // notification.
285 TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) { 286 TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) {
286 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 287 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
287 FilePathWatcher* watcher = new FilePathWatcher; 288 base::files::FilePathWatcher* watcher = new base::files::FilePathWatcher;
288 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get())); 289 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get()));
289 ASSERT_TRUE(WriteFile(test_file(), "content")); 290 ASSERT_TRUE(WriteFile(test_file(), "content"));
290 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); 291 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
291 } 292 }
292 293
293 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) { 294 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) {
294 FilePathWatcher watcher1, watcher2; 295 base::files::FilePathWatcher watcher1, watcher2;
295 scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector())); 296 scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector()));
296 scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector())); 297 scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector()));
297 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get())); 298 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get()));
298 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get())); 299 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get()));
299 300
300 ASSERT_TRUE(WriteFile(test_file(), "content")); 301 ASSERT_TRUE(WriteFile(test_file(), "content"));
301 ASSERT_TRUE(WaitForEvents()); 302 ASSERT_TRUE(WaitForEvents());
302 } 303 }
303 304
304 // Verify that watching a file whose parent directory doesn't exist yet works if 305 // Verify that watching a file whose parent directory doesn't exist yet works if
305 // the directory and file are created eventually. 306 // the directory and file are created eventually.
306 TEST_F(FilePathWatcherTest, NonExistentDirectory) { 307 TEST_F(FilePathWatcherTest, NonExistentDirectory) {
307 FilePathWatcher watcher; 308 base::files::FilePathWatcher watcher;
308 FilePath dir(temp_dir_.path().AppendASCII("dir")); 309 FilePath dir(temp_dir_.path().AppendASCII("dir"));
309 FilePath file(dir.AppendASCII("file")); 310 FilePath file(dir.AppendASCII("file"));
310 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 311 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
311 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); 312 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
312 313
313 ASSERT_TRUE(file_util::CreateDirectory(dir)); 314 ASSERT_TRUE(file_util::CreateDirectory(dir));
314 315
315 ASSERT_TRUE(WriteFile(file, "content")); 316 ASSERT_TRUE(WriteFile(file, "content"));
316 317
317 VLOG(1) << "Waiting for file creation"; 318 VLOG(1) << "Waiting for file creation";
(...skipping 12 matching lines...) Expand all
330 // are rapidly created. 331 // are rapidly created.
331 TEST_F(FilePathWatcherTest, DirectoryChain) { 332 TEST_F(FilePathWatcherTest, DirectoryChain) {
332 FilePath path(temp_dir_.path()); 333 FilePath path(temp_dir_.path());
333 std::vector<std::string> dir_names; 334 std::vector<std::string> dir_names;
334 for (int i = 0; i < 20; i++) { 335 for (int i = 0; i < 20; i++) {
335 std::string dir(StringPrintf("d%d", i)); 336 std::string dir(StringPrintf("d%d", i));
336 dir_names.push_back(dir); 337 dir_names.push_back(dir);
337 path = path.AppendASCII(dir); 338 path = path.AppendASCII(dir);
338 } 339 }
339 340
340 FilePathWatcher watcher; 341 base::files::FilePathWatcher watcher;
341 FilePath file(path.AppendASCII("file")); 342 FilePath file(path.AppendASCII("file"));
342 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 343 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
343 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); 344 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
344 345
345 FilePath sub_path(temp_dir_.path()); 346 FilePath sub_path(temp_dir_.path());
346 for (std::vector<std::string>::const_iterator d(dir_names.begin()); 347 for (std::vector<std::string>::const_iterator d(dir_names.begin());
347 d != dir_names.end(); ++d) { 348 d != dir_names.end(); ++d) {
348 sub_path = sub_path.AppendASCII(*d); 349 sub_path = sub_path.AppendASCII(*d);
349 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); 350 ASSERT_TRUE(file_util::CreateDirectory(sub_path));
350 } 351 }
351 VLOG(1) << "Create File"; 352 VLOG(1) << "Create File";
352 ASSERT_TRUE(WriteFile(file, "content")); 353 ASSERT_TRUE(WriteFile(file, "content"));
353 VLOG(1) << "Waiting for file creation"; 354 VLOG(1) << "Waiting for file creation";
354 ASSERT_TRUE(WaitForEvents()); 355 ASSERT_TRUE(WaitForEvents());
355 356
356 ASSERT_TRUE(WriteFile(file, "content v2")); 357 ASSERT_TRUE(WriteFile(file, "content v2"));
357 VLOG(1) << "Waiting for file modification"; 358 VLOG(1) << "Waiting for file modification";
358 ASSERT_TRUE(WaitForEvents()); 359 ASSERT_TRUE(WaitForEvents());
359 } 360 }
360 361
361 TEST_F(FilePathWatcherTest, DisappearingDirectory) { 362 TEST_F(FilePathWatcherTest, DisappearingDirectory) {
362 FilePathWatcher watcher; 363 base::files::FilePathWatcher watcher;
363 FilePath dir(temp_dir_.path().AppendASCII("dir")); 364 FilePath dir(temp_dir_.path().AppendASCII("dir"));
364 FilePath file(dir.AppendASCII("file")); 365 FilePath file(dir.AppendASCII("file"));
365 ASSERT_TRUE(file_util::CreateDirectory(dir)); 366 ASSERT_TRUE(file_util::CreateDirectory(dir));
366 ASSERT_TRUE(WriteFile(file, "content")); 367 ASSERT_TRUE(WriteFile(file, "content"));
367 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 368 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
368 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); 369 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
369 370
370 ASSERT_TRUE(file_util::Delete(dir, true)); 371 ASSERT_TRUE(file_util::Delete(dir, true));
371 ASSERT_TRUE(WaitForEvents()); 372 ASSERT_TRUE(WaitForEvents());
372 } 373 }
373 374
374 // Tests that a file that is deleted and reappears is tracked correctly. 375 // Tests that a file that is deleted and reappears is tracked correctly.
375 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { 376 TEST_F(FilePathWatcherTest, DeleteAndRecreate) {
376 ASSERT_TRUE(WriteFile(test_file(), "content")); 377 ASSERT_TRUE(WriteFile(test_file(), "content"));
377 FilePathWatcher watcher; 378 base::files::FilePathWatcher watcher;
378 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 379 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
379 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 380 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
380 381
381 ASSERT_TRUE(file_util::Delete(test_file(), false)); 382 ASSERT_TRUE(file_util::Delete(test_file(), false));
382 VLOG(1) << "Waiting for file deletion"; 383 VLOG(1) << "Waiting for file deletion";
383 ASSERT_TRUE(WaitForEvents()); 384 ASSERT_TRUE(WaitForEvents());
384 385
385 ASSERT_TRUE(WriteFile(test_file(), "content")); 386 ASSERT_TRUE(WriteFile(test_file(), "content"));
386 VLOG(1) << "Waiting for file creation"; 387 VLOG(1) << "Waiting for file creation";
387 ASSERT_TRUE(WaitForEvents()); 388 ASSERT_TRUE(WaitForEvents());
388 } 389 }
389 390
390 TEST_F(FilePathWatcherTest, WatchDirectory) { 391 TEST_F(FilePathWatcherTest, WatchDirectory) {
391 FilePathWatcher watcher; 392 base::files::FilePathWatcher watcher;
392 FilePath dir(temp_dir_.path().AppendASCII("dir")); 393 FilePath dir(temp_dir_.path().AppendASCII("dir"));
393 FilePath file1(dir.AppendASCII("file1")); 394 FilePath file1(dir.AppendASCII("file1"));
394 FilePath file2(dir.AppendASCII("file2")); 395 FilePath file2(dir.AppendASCII("file2"));
395 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 396 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
396 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get())); 397 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get()));
397 398
398 ASSERT_TRUE(file_util::CreateDirectory(dir)); 399 ASSERT_TRUE(file_util::CreateDirectory(dir));
399 VLOG(1) << "Waiting for directory creation"; 400 VLOG(1) << "Waiting for directory creation";
400 ASSERT_TRUE(WaitForEvents()); 401 ASSERT_TRUE(WaitForEvents());
401 402
(...skipping 11 matching lines...) Expand all
413 ASSERT_TRUE(file_util::Delete(file1, false)); 414 ASSERT_TRUE(file_util::Delete(file1, false));
414 VLOG(1) << "Waiting for file1 deletion"; 415 VLOG(1) << "Waiting for file1 deletion";
415 ASSERT_TRUE(WaitForEvents()); 416 ASSERT_TRUE(WaitForEvents());
416 417
417 ASSERT_TRUE(WriteFile(file2, "content")); 418 ASSERT_TRUE(WriteFile(file2, "content"));
418 VLOG(1) << "Waiting for file2 creation"; 419 VLOG(1) << "Waiting for file2 creation";
419 ASSERT_TRUE(WaitForEvents()); 420 ASSERT_TRUE(WaitForEvents());
420 } 421 }
421 422
422 TEST_F(FilePathWatcherTest, MoveParent) { 423 TEST_F(FilePathWatcherTest, MoveParent) {
423 FilePathWatcher file_watcher; 424 base::files::FilePathWatcher file_watcher;
424 FilePathWatcher subdir_watcher; 425 base::files::FilePathWatcher subdir_watcher;
425 FilePath dir(temp_dir_.path().AppendASCII("dir")); 426 FilePath dir(temp_dir_.path().AppendASCII("dir"));
426 FilePath dest(temp_dir_.path().AppendASCII("dest")); 427 FilePath dest(temp_dir_.path().AppendASCII("dest"));
427 FilePath subdir(dir.AppendASCII("subdir")); 428 FilePath subdir(dir.AppendASCII("subdir"));
428 FilePath file(subdir.AppendASCII("file")); 429 FilePath file(subdir.AppendASCII("file"));
429 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); 430 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
430 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get())); 431 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get()));
431 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); 432 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
432 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get())); 433 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()));
433 434
434 // Setup a directory hierarchy. 435 // Setup a directory hierarchy.
435 ASSERT_TRUE(file_util::CreateDirectory(subdir)); 436 ASSERT_TRUE(file_util::CreateDirectory(subdir));
436 ASSERT_TRUE(WriteFile(file, "content")); 437 ASSERT_TRUE(WriteFile(file, "content"));
437 VLOG(1) << "Waiting for file creation"; 438 VLOG(1) << "Waiting for file creation";
438 ASSERT_TRUE(WaitForEvents()); 439 ASSERT_TRUE(WaitForEvents());
439 440
440 // Move the parent directory. 441 // Move the parent directory.
441 file_util::Move(dir, dest); 442 file_util::Move(dir, dest);
442 VLOG(1) << "Waiting for directory move"; 443 VLOG(1) << "Waiting for directory move";
443 ASSERT_TRUE(WaitForEvents()); 444 ASSERT_TRUE(WaitForEvents());
444 } 445 }
445 446
446 TEST_F(FilePathWatcherTest, MoveChild) { 447 TEST_F(FilePathWatcherTest, MoveChild) {
447 FilePathWatcher file_watcher; 448 base::files::FilePathWatcher file_watcher;
448 FilePathWatcher subdir_watcher; 449 base::files::FilePathWatcher subdir_watcher;
449 FilePath source_dir(temp_dir_.path().AppendASCII("source")); 450 FilePath source_dir(temp_dir_.path().AppendASCII("source"));
450 FilePath source_subdir(source_dir.AppendASCII("subdir")); 451 FilePath source_subdir(source_dir.AppendASCII("subdir"));
451 FilePath source_file(source_subdir.AppendASCII("file")); 452 FilePath source_file(source_subdir.AppendASCII("file"));
452 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); 453 FilePath dest_dir(temp_dir_.path().AppendASCII("dest"));
453 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); 454 FilePath dest_subdir(dest_dir.AppendASCII("subdir"));
454 FilePath dest_file(dest_subdir.AppendASCII("file")); 455 FilePath dest_file(dest_subdir.AppendASCII("file"));
455 456
456 // Setup a directory hierarchy. 457 // Setup a directory hierarchy.
457 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); 458 ASSERT_TRUE(file_util::CreateDirectory(source_subdir));
458 ASSERT_TRUE(WriteFile(source_file, "content")); 459 ASSERT_TRUE(WriteFile(source_file, "content"));
459 460
460 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); 461 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
461 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get())); 462 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get()));
462 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); 463 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
463 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get())); 464 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()));
464 465
465 // Move the directory into place, s.t. the watched file appears. 466 // Move the directory into place, s.t. the watched file appears.
466 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); 467 ASSERT_TRUE(file_util::Move(source_dir, dest_dir));
467 ASSERT_TRUE(WaitForEvents()); 468 ASSERT_TRUE(WaitForEvents());
468 } 469 }
469 470
470 #if !defined(OS_LINUX) 471 #if !defined(OS_LINUX)
471 // Linux implementation of FilePathWatcher doesn't catch attribute changes. 472 // Linux implementation of FilePathWatcher doesn't catch attribute changes.
472 // http://crbug.com/78043 473 // http://crbug.com/78043
473 474
474 // Verify that changing attributes on a file is caught 475 // Verify that changing attributes on a file is caught
475 TEST_F(FilePathWatcherTest, FileAttributesChanged) { 476 TEST_F(FilePathWatcherTest, FileAttributesChanged) {
476 ASSERT_TRUE(WriteFile(test_file(), "content")); 477 ASSERT_TRUE(WriteFile(test_file(), "content"));
477 FilePathWatcher watcher; 478 base::files::FilePathWatcher watcher;
478 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 479 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
479 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); 480 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
480 481
481 // Now make sure we get notified if the file is modified. 482 // Now make sure we get notified if the file is modified.
482 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); 483 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file()));
483 ASSERT_TRUE(WaitForEvents()); 484 ASSERT_TRUE(WaitForEvents());
484 } 485 }
485 486
486 #endif // !OS_LINUX 487 #endif // !OS_LINUX
487 488
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 // Verify that changing attributes on a directory works. 586 // Verify that changing attributes on a directory works.
586 TEST_F(FilePathWatcherTest, DirAttributesChanged) { 587 TEST_F(FilePathWatcherTest, DirAttributesChanged) {
587 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1")); 588 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1"));
588 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); 589 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2"));
589 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); 590 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile"));
590 // Setup a directory hierarchy. 591 // Setup a directory hierarchy.
591 ASSERT_TRUE(file_util::CreateDirectory(test_dir1)); 592 ASSERT_TRUE(file_util::CreateDirectory(test_dir1));
592 ASSERT_TRUE(file_util::CreateDirectory(test_dir2)); 593 ASSERT_TRUE(file_util::CreateDirectory(test_dir2));
593 ASSERT_TRUE(WriteFile(test_file, "content")); 594 ASSERT_TRUE(WriteFile(test_file, "content"));
594 595
595 FilePathWatcher watcher; 596 base::files::FilePathWatcher watcher;
596 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 597 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
597 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get())); 598 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get()));
598 599
599 // We should not get notified in this case as it hasn't affected our ability 600 // We should not get notified in this case as it hasn't affected our ability
600 // to access the file. 601 // to access the file.
601 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false)); 602 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false));
602 loop_.PostDelayedTask(FROM_HERE, 603 loop_.PostDelayedTask(FROM_HERE,
603 new MessageLoop::QuitTask, 604 new MessageLoop::QuitTask,
604 TestTimeouts::tiny_timeout_ms()); 605 TestTimeouts::tiny_timeout_ms());
605 ASSERT_FALSE(WaitForEvents()); 606 ASSERT_FALSE(WaitForEvents());
606 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); 607 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true));
607 608
608 // We should get notified in this case because filepathwatcher can no 609 // We should get notified in this case because filepathwatcher can no
609 // longer access the file 610 // longer access the file
610 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); 611 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
611 ASSERT_TRUE(WaitForEvents()); 612 ASSERT_TRUE(WaitForEvents());
612 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); 613 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
613 } 614 }
614 615
615 #endif // OS_MACOSX 616 #endif // OS_MACOSX
616 } // namespace 617 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698