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

Side by Side Diff: content/common/file_path_watcher/file_path_watcher_browsertest.cc

Issue 6670081: Move FilePathWatcher class from browser/... to common/... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix up linux clang issue, and clean up bad commented block Created 9 years, 9 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) 2010 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 "chrome/browser/file_path_watcher/file_path_watcher.h" 5 #include "content/common/file_path_watcher/file_path_watcher.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
10 #include "base/file_path.h" 11 #include "base/file_path.h"
11 #include "base/file_util.h" 12 #include "base/file_util.h"
12 #include "base/message_loop.h" 13 #include "base/message_loop.h"
13 #include "base/message_loop_proxy.h" 14 #include "base/message_loop_proxy.h"
14 #include "base/path_service.h" 15 #include "base/path_service.h"
15 #include "base/scoped_temp_dir.h" 16 #include "base/scoped_temp_dir.h"
16 #include "base/string_util.h" 17 #include "base/string_util.h"
17 #include "base/stl_util-inl.h" 18 #include "base/stl_util-inl.h"
18 #include "base/synchronization/waitable_event.h" 19 #include "base/synchronization/waitable_event.h"
20 #include "base/test/test_timeouts.h"
21 #include "base/threading/thread.h"
19 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
20 23
21 #if defined(OS_MACOSX) 24 #if defined(OS_MACOSX)
22 // TODO(mnissler): There are flakes on Mac (http://crbug.com/54822) at least for 25 // TODO(mnissler): There are flakes on Mac (http://crbug.com/54822) at least for
23 // FilePathWatcherTest.MultipleWatchersSingleFile. 26 // FilePathWatcherTest.MultipleWatchersSingleFile.
24 #define MAYBE(name) FLAKY_ ## name 27 #define MAYBE(name) FLAKY_ ## name
25 #else 28 #else
26 #define MAYBE(name) name 29 #define MAYBE(name) name
27 #endif 30 #endif
28 31
(...skipping 18 matching lines...) Expand all
47 } 50 }
48 51
49 void Register(TestDelegate* delegate) { 52 void Register(TestDelegate* delegate) {
50 delegates_.insert(delegate); 53 delegates_.insert(delegate);
51 } 54 }
52 55
53 void Reset() { 56 void Reset() {
54 signaled_.clear(); 57 signaled_.clear();
55 } 58 }
56 59
60 bool Success() {
61 return signaled_ == delegates_;
62 }
63
57 private: 64 private:
58 void RecordChange(TestDelegate* delegate) { 65 void RecordChange(TestDelegate* delegate) {
59 ASSERT_TRUE(loop_->BelongsToCurrentThread()); 66 ASSERT_TRUE(loop_->BelongsToCurrentThread());
60 ASSERT_TRUE(delegates_.count(delegate)); 67 ASSERT_TRUE(delegates_.count(delegate));
61 signaled_.insert(delegate); 68 signaled_.insert(delegate);
62 69
63 // Check whether all delegates have been signaled. 70 // Check whether all delegates have been signaled.
64 if (signaled_ == delegates_) 71 if (signaled_ == delegates_)
65 loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 72 loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
66 } 73 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 DISALLOW_COPY_AND_ASSIGN(TestDelegate); 106 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
100 }; 107 };
101 108
102 // A helper class for setting up watches on the file thread. 109 // A helper class for setting up watches on the file thread.
103 class SetupWatchTask : public Task { 110 class SetupWatchTask : public Task {
104 public: 111 public:
105 SetupWatchTask(const FilePath& target, 112 SetupWatchTask(const FilePath& target,
106 FilePathWatcher* watcher, 113 FilePathWatcher* watcher,
107 FilePathWatcher::Delegate* delegate, 114 FilePathWatcher::Delegate* delegate,
108 bool* result, 115 bool* result,
109 base::WaitableEvent* completion) 116 base::WaitableEvent* completion,
117 base::MessageLoopProxy* mac_run_loop)
110 : target_(target), 118 : target_(target),
111 watcher_(watcher), 119 watcher_(watcher),
112 delegate_(delegate), 120 delegate_(delegate),
113 result_(result), 121 result_(result),
114 completion_(completion) {} 122 completion_(completion),
123 mac_run_loop_(mac_run_loop) {}
115 124
116 void Run() { 125 void Run() {
117 *result_ = watcher_->Watch(target_, delegate_); 126 *result_ = watcher_->Watch(target_, delegate_, mac_run_loop_);
118 completion_->Signal(); 127 completion_->Signal();
119 } 128 }
120 129
121 private: 130 private:
122 const FilePath target_; 131 const FilePath target_;
123 FilePathWatcher* watcher_; 132 FilePathWatcher* watcher_;
124 FilePathWatcher::Delegate* delegate_; 133 FilePathWatcher::Delegate* delegate_;
125 bool* result_; 134 bool* result_;
126 base::WaitableEvent* completion_; 135 base::WaitableEvent* completion_;
136 scoped_refptr<base::MessageLoopProxy> mac_run_loop_;
127 137
128 DISALLOW_COPY_AND_ASSIGN(SetupWatchTask); 138 DISALLOW_COPY_AND_ASSIGN(SetupWatchTask);
129 }; 139 };
130 140
131 class FilePathWatcherTest : public testing::Test { 141 class FilePathWatcherTest : public testing::Test {
132 public: 142 public:
133 // Implementation of FilePathWatcher on Mac requires UI loop.
134 FilePathWatcherTest() 143 FilePathWatcherTest()
135 : loop_(MessageLoop::TYPE_UI), 144 : loop_(MessageLoop::TYPE_UI),
136 ui_thread_(BrowserThread::UI, &loop_) { 145 file_thread_("FilePathWatcherTest") { }
137 } 146
147 virtual ~FilePathWatcherTest() { }
138 148
139 protected: 149 protected:
140 virtual void SetUp() { 150 virtual void SetUp() {
141 // Create a separate file thread in order to test proper thread usage. 151 // Create a separate file thread in order to test proper thread usage.
142 file_thread_.reset(new BrowserThread(BrowserThread::FILE)); 152 base::Thread::Options options(MessageLoop::TYPE_IO, 0);
143 file_thread_->Start(); 153 ASSERT_TRUE(file_thread_.StartWithOptions(options));
144 temp_dir_.reset(new ScopedTempDir); 154 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
145 ASSERT_TRUE(temp_dir_->CreateUniqueTempDir());
146 collector_ = new NotificationCollector(); 155 collector_ = new NotificationCollector();
147 } 156 }
148 157
149 virtual void TearDown() { 158 virtual void TearDown() {
150 loop_.RunAllPending(); 159 loop_.RunAllPending();
151 file_thread_.reset();
152 } 160 }
153 161
154 FilePath test_file() { 162 FilePath test_file() {
155 return temp_dir_->path().AppendASCII("FilePathWatcherTest"); 163 return temp_dir_.path().AppendASCII("FilePathWatcherTest");
156 } 164 }
157 165
158 // Write |content| to |file|. Returns true on success. 166 // Write |content| to |file|. Returns true on success.
159 bool WriteFile(const FilePath& file, const std::string& content) { 167 bool WriteFile(const FilePath& file, const std::string& content) {
160 int write_size = file_util::WriteFile(file, content.c_str(), 168 int write_size = file_util::WriteFile(file, content.c_str(),
161 content.length()); 169 content.length());
162 return write_size == static_cast<int>(content.length()); 170 return write_size == static_cast<int>(content.length());
163 } 171 }
164 172
165 void SetupWatch(const FilePath& target, 173 bool SetupWatch(const FilePath& target,
166 FilePathWatcher* watcher, 174 FilePathWatcher* watcher,
167 FilePathWatcher::Delegate* delegate) { 175 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT {
168 base::WaitableEvent completion(false, false); 176 base::WaitableEvent completion(false, false);
169 bool result; 177 bool result;
170 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 178 file_thread_.message_loop_proxy()->PostTask(FROM_HERE,
171 new SetupWatchTask(target, watcher, delegate, &result, &completion)); 179 new SetupWatchTask(target,
180 watcher,
181 delegate,
182 &result,
183 &completion,
184 base::MessageLoopProxy::CreateForCurrentThread()));
172 completion.Wait(); 185 completion.Wait();
173 ASSERT_TRUE(result); 186 return result;
174 } 187 }
175 188
176 void WaitForEvents() { 189 bool WaitForEvents() WARN_UNUSED_RESULT {
177 collector_->Reset(); 190 collector_->Reset();
178 loop_.Run(); 191 loop_.Run();
192 return collector_->Success();
179 } 193 }
180 194
181 NotificationCollector* collector() { return collector_.get(); } 195 NotificationCollector* collector() { return collector_.get(); }
182 196
183 MessageLoop loop_; 197 MessageLoop loop_;
184 BrowserThread ui_thread_; 198 base::Thread file_thread_;
185 scoped_ptr<BrowserThread> file_thread_; 199 ScopedTempDir temp_dir_;
186 scoped_ptr<ScopedTempDir> temp_dir_;
187 scoped_refptr<NotificationCollector> collector_; 200 scoped_refptr<NotificationCollector> collector_;
188 }; 201 };
189 202
190 // Basic test: Create the file and verify that we notice. 203 // Basic test: Create the file and verify that we notice.
191 TEST_F(FilePathWatcherTest, MAYBE(NewFile)) { 204 TEST_F(FilePathWatcherTest, MAYBE(NewFile)) {
192 FilePathWatcher watcher; 205 FilePathWatcher watcher;
193 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 206 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
194 SetupWatch(test_file(), &watcher, delegate.get()); 207 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
195 208
196 ASSERT_TRUE(WriteFile(test_file(), "content")); 209 ASSERT_TRUE(WriteFile(test_file(), "content"));
197 WaitForEvents(); 210 ASSERT_TRUE(WaitForEvents());
198 } 211 }
199 212
200 // Verify that modifying the file is caught. 213 // Verify that modifying the file is caught.
201 TEST_F(FilePathWatcherTest, MAYBE(ModifiedFile)) { 214 TEST_F(FilePathWatcherTest, MAYBE(ModifiedFile)) {
202 ASSERT_TRUE(WriteFile(test_file(), "content")); 215 ASSERT_TRUE(WriteFile(test_file(), "content"));
203 216
204 FilePathWatcher watcher; 217 FilePathWatcher watcher;
205 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 218 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
206 SetupWatch(test_file(), &watcher, delegate.get()); 219 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
207 220
208 // Now make sure we get notified if the file is modified. 221 // Now make sure we get notified if the file is modified.
209 ASSERT_TRUE(WriteFile(test_file(), "new content")); 222 ASSERT_TRUE(WriteFile(test_file(), "new content"));
210 WaitForEvents(); 223 ASSERT_TRUE(WaitForEvents());
211 } 224 }
212 225
213 // Verify that moving the file into place is caught. 226 // Verify that moving the file into place is caught.
214 TEST_F(FilePathWatcherTest, MAYBE(MovedFile)) { 227 TEST_F(FilePathWatcherTest, MAYBE(MovedFile)) {
215 FilePath source_file(temp_dir_->path().AppendASCII("source")); 228 FilePath source_file(temp_dir_.path().AppendASCII("source"));
216 ASSERT_TRUE(WriteFile(source_file, "content")); 229 ASSERT_TRUE(WriteFile(source_file, "content"));
217 230
218 FilePathWatcher watcher; 231 FilePathWatcher watcher;
219 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 232 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
220 SetupWatch(test_file(), &watcher, delegate.get()); 233 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
221 234
222 // Now make sure we get notified if the file is modified. 235 // Now make sure we get notified if the file is modified.
223 ASSERT_TRUE(file_util::Move(source_file, test_file())); 236 ASSERT_TRUE(file_util::Move(source_file, test_file()));
224 WaitForEvents(); 237 ASSERT_TRUE(WaitForEvents());
225 } 238 }
226 239
227 TEST_F(FilePathWatcherTest, MAYBE(DeletedFile)) { 240 TEST_F(FilePathWatcherTest, MAYBE(DeletedFile)) {
228 ASSERT_TRUE(WriteFile(test_file(), "content")); 241 ASSERT_TRUE(WriteFile(test_file(), "content"));
229 242
230 FilePathWatcher watcher; 243 FilePathWatcher watcher;
231 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 244 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
232 SetupWatch(test_file(), &watcher, delegate.get()); 245 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
233 246
234 // Now make sure we get notified if the file is deleted. 247 // Now make sure we get notified if the file is deleted.
235 file_util::Delete(test_file(), false); 248 file_util::Delete(test_file(), false);
236 WaitForEvents(); 249 ASSERT_TRUE(WaitForEvents());
237 } 250 }
238 251
239 // Used by the DeleteDuringNotify test below. 252 // Used by the DeleteDuringNotify test below.
240 // Deletes the FilePathWatcher when it's notified. 253 // Deletes the FilePathWatcher when it's notified.
241 class Deleter : public FilePathWatcher::Delegate { 254 class Deleter : public FilePathWatcher::Delegate {
242 public: 255 public:
243 Deleter(FilePathWatcher* watcher, MessageLoop* loop) 256 Deleter(FilePathWatcher* watcher, MessageLoop* loop)
244 : watcher_(watcher), 257 : watcher_(watcher),
245 loop_(loop) { 258 loop_(loop) {
246 } 259 }
247 260
248 virtual void OnFilePathChanged(const FilePath& path) { 261 virtual void OnFilePathChanged(const FilePath& path) {
249 watcher_.reset(NULL); 262 watcher_.reset();
250 loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); 263 loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
251 } 264 }
252 265
253 scoped_ptr<FilePathWatcher> watcher_; 266 scoped_ptr<FilePathWatcher> watcher_;
254 MessageLoop* loop_; 267 MessageLoop* loop_;
255 }; 268 };
256 269
257 // Verify that deleting a watcher during the callback doesn't crash. 270 // Verify that deleting a watcher during the callback doesn't crash.
258 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { 271 TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
259 FilePathWatcher* watcher = new FilePathWatcher; 272 FilePathWatcher* watcher = new FilePathWatcher;
260 // Takes ownership of watcher. 273 // Takes ownership of watcher.
261 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_)); 274 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_));
262 SetupWatch(test_file(), watcher, deleter.get()); 275 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get()));
263 276
264 ASSERT_TRUE(WriteFile(test_file(), "content")); 277 ASSERT_TRUE(WriteFile(test_file(), "content"));
265 WaitForEvents(); 278 ASSERT_TRUE(WaitForEvents());
266 279
267 // We win if we haven't crashed yet. 280 // We win if we haven't crashed yet.
268 // Might as well double-check it got deleted, too. 281 // Might as well double-check it got deleted, too.
269 ASSERT_TRUE(deleter->watcher_.get() == NULL); 282 ASSERT_TRUE(deleter->watcher_.get() == NULL);
270 } 283 }
271 284
272 // Verify that deleting the watcher works even if there is a pending 285 // Verify that deleting the watcher works even if there is a pending
273 // notification. 286 // notification.
274 TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) { 287 TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) {
275 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 288 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
276 FilePathWatcher* watcher = new FilePathWatcher; 289 FilePathWatcher* watcher = new FilePathWatcher;
277 SetupWatch(test_file(), watcher, delegate.get()); 290 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get()));
278 ASSERT_TRUE(WriteFile(test_file(), "content")); 291 ASSERT_TRUE(WriteFile(test_file(), "content"));
279 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, watcher); 292 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher);
280 } 293 }
281 294
282 TEST_F(FilePathWatcherTest, MAYBE(MultipleWatchersSingleFile)) { 295 TEST_F(FilePathWatcherTest, MAYBE(MultipleWatchersSingleFile)) {
283 FilePathWatcher watcher1, watcher2; 296 FilePathWatcher watcher1, watcher2;
284 scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector())); 297 scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector()));
285 scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector())); 298 scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector()));
286 SetupWatch(test_file(), &watcher1, delegate1.get()); 299 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get()));
287 SetupWatch(test_file(), &watcher2, delegate2.get()); 300 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get()));
288 301
289 ASSERT_TRUE(WriteFile(test_file(), "content")); 302 ASSERT_TRUE(WriteFile(test_file(), "content"));
290 WaitForEvents(); 303 ASSERT_TRUE(WaitForEvents());
291 } 304 }
292 305
293 // Verify that watching a file whose parent directory doesn't exist yet works if 306 // Verify that watching a file whose parent directory doesn't exist yet works if
294 // the directory and file are created eventually. 307 // the directory and file are created eventually.
295 TEST_F(FilePathWatcherTest, NonExistentDirectory) { 308 TEST_F(FilePathWatcherTest, NonExistentDirectory) {
296 FilePathWatcher watcher; 309 FilePathWatcher watcher;
297 FilePath dir(temp_dir_->path().AppendASCII("dir")); 310 FilePath dir(temp_dir_.path().AppendASCII("dir"));
298 FilePath file(dir.AppendASCII("file")); 311 FilePath file(dir.AppendASCII("file"));
299 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 312 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
300 SetupWatch(file, &watcher, delegate.get()); 313 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
301 314
302 ASSERT_TRUE(file_util::CreateDirectory(dir)); 315 ASSERT_TRUE(file_util::CreateDirectory(dir));
303 316
304 ASSERT_TRUE(WriteFile(file, "content")); 317 ASSERT_TRUE(WriteFile(file, "content"));
318
305 VLOG(1) << "Waiting for file creation"; 319 VLOG(1) << "Waiting for file creation";
306 WaitForEvents(); 320 ASSERT_TRUE(WaitForEvents());
307 321
308 ASSERT_TRUE(WriteFile(file, "content v2")); 322 ASSERT_TRUE(WriteFile(file, "content v2"));
309 VLOG(1) << "Waiting for file change"; 323 VLOG(1) << "Waiting for file change";
310 WaitForEvents(); 324 ASSERT_TRUE(WaitForEvents());
311 325
312 ASSERT_TRUE(file_util::Delete(file, false)); 326 ASSERT_TRUE(file_util::Delete(file, false));
313 VLOG(1) << "Waiting for file deletion"; 327 VLOG(1) << "Waiting for file deletion";
314 WaitForEvents(); 328 ASSERT_TRUE(WaitForEvents());
315 } 329 }
316 330
317 // Exercises watch reconfiguration for the case that directories on the path 331 // Exercises watch reconfiguration for the case that directories on the path
318 // are rapidly created. 332 // are rapidly created.
319 TEST_F(FilePathWatcherTest, DirectoryChain) { 333 TEST_F(FilePathWatcherTest, DirectoryChain) {
320 FilePath path(temp_dir_->path()); 334 FilePath path(temp_dir_.path());
321 std::vector<std::string> dir_names; 335 std::vector<std::string> dir_names;
322 for (int i = 0; i < 20; i++) { 336 for (int i = 0; i < 20; i++) {
323 std::string dir(StringPrintf("d%d", i)); 337 std::string dir(StringPrintf("d%d", i));
324 dir_names.push_back(dir); 338 dir_names.push_back(dir);
325 path = path.AppendASCII(dir); 339 path = path.AppendASCII(dir);
326 } 340 }
327 341
328 FilePathWatcher watcher; 342 FilePathWatcher watcher;
329 FilePath file(path.AppendASCII("file")); 343 FilePath file(path.AppendASCII("file"));
330 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 344 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
331 SetupWatch(file, &watcher, delegate.get()); 345 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
332 346
333 FilePath sub_path(temp_dir_->path()); 347 FilePath sub_path(temp_dir_.path());
334 for (std::vector<std::string>::const_iterator d(dir_names.begin()); 348 for (std::vector<std::string>::const_iterator d(dir_names.begin());
335 d != dir_names.end(); ++d) { 349 d != dir_names.end(); ++d) {
336 sub_path = sub_path.AppendASCII(*d); 350 sub_path = sub_path.AppendASCII(*d);
337 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); 351 ASSERT_TRUE(file_util::CreateDirectory(sub_path));
338 } 352 }
353 VLOG(1) << "Create File";
339 ASSERT_TRUE(WriteFile(file, "content")); 354 ASSERT_TRUE(WriteFile(file, "content"));
340 VLOG(1) << "Waiting for file creation"; 355 VLOG(1) << "Waiting for file creation";
341 WaitForEvents(); 356 ASSERT_TRUE(WaitForEvents());
342 357
343 ASSERT_TRUE(WriteFile(file, "content v2")); 358 ASSERT_TRUE(WriteFile(file, "content v2"));
344 VLOG(1) << "Waiting for file modification"; 359 VLOG(1) << "Waiting for file modification";
345 WaitForEvents(); 360 ASSERT_TRUE(WaitForEvents());
346 } 361 }
347 362
348 TEST_F(FilePathWatcherTest, DisappearingDirectory) { 363 TEST_F(FilePathWatcherTest, DisappearingDirectory) {
349 FilePathWatcher watcher; 364 FilePathWatcher watcher;
350 FilePath dir(temp_dir_->path().AppendASCII("dir")); 365 FilePath dir(temp_dir_.path().AppendASCII("dir"));
351 FilePath file(dir.AppendASCII("file")); 366 FilePath file(dir.AppendASCII("file"));
352 ASSERT_TRUE(file_util::CreateDirectory(dir)); 367 ASSERT_TRUE(file_util::CreateDirectory(dir));
353 ASSERT_TRUE(WriteFile(file, "content")); 368 ASSERT_TRUE(WriteFile(file, "content"));
354 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 369 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
355 SetupWatch(file, &watcher, delegate.get()); 370 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get()));
356 371
357 ASSERT_TRUE(file_util::Delete(dir, true)); 372 ASSERT_TRUE(file_util::Delete(dir, true));
358 WaitForEvents(); 373 ASSERT_TRUE(WaitForEvents());
359 } 374 }
360 375
361 // Tests that a file that is deleted and reappears is tracked correctly. 376 // Tests that a file that is deleted and reappears is tracked correctly.
362 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { 377 TEST_F(FilePathWatcherTest, DeleteAndRecreate) {
363 ASSERT_TRUE(WriteFile(test_file(), "content")); 378 ASSERT_TRUE(WriteFile(test_file(), "content"));
364 FilePathWatcher watcher; 379 FilePathWatcher watcher;
365 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 380 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
366 SetupWatch(test_file(), &watcher, delegate.get()); 381 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get()));
367 382
368 ASSERT_TRUE(file_util::Delete(test_file(), false)); 383 ASSERT_TRUE(file_util::Delete(test_file(), false));
369 VLOG(1) << "Waiting for file deletion"; 384 VLOG(1) << "Waiting for file deletion";
370 WaitForEvents(); 385 ASSERT_TRUE(WaitForEvents());
371 386
372 ASSERT_TRUE(WriteFile(test_file(), "content")); 387 ASSERT_TRUE(WriteFile(test_file(), "content"));
373 VLOG(1) << "Waiting for file creation"; 388 VLOG(1) << "Waiting for file creation";
374 WaitForEvents(); 389 ASSERT_TRUE(WaitForEvents());
375 } 390 }
376 391
377 TEST_F(FilePathWatcherTest, WatchDirectory) { 392 TEST_F(FilePathWatcherTest, WatchDirectory) {
378 FilePathWatcher watcher; 393 FilePathWatcher watcher;
379 FilePath dir(temp_dir_->path().AppendASCII("dir")); 394 FilePath dir(temp_dir_.path().AppendASCII("dir"));
380 FilePath file1(dir.AppendASCII("file1")); 395 FilePath file1(dir.AppendASCII("file1"));
381 FilePath file2(dir.AppendASCII("file2")); 396 FilePath file2(dir.AppendASCII("file2"));
382 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); 397 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector()));
383 SetupWatch(dir, &watcher, delegate.get()); 398 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get()));
384 399
385 ASSERT_TRUE(file_util::CreateDirectory(dir)); 400 ASSERT_TRUE(file_util::CreateDirectory(dir));
386 VLOG(1) << "Waiting for directory creation"; 401 VLOG(1) << "Waiting for directory creation";
387 WaitForEvents(); 402 ASSERT_TRUE(WaitForEvents());
388 403
389 ASSERT_TRUE(WriteFile(file1, "content")); 404 ASSERT_TRUE(WriteFile(file1, "content"));
390 VLOG(1) << "Waiting for file1 creation"; 405 VLOG(1) << "Waiting for file1 creation";
391 WaitForEvents(); 406 ASSERT_TRUE(WaitForEvents());
392 407
393 ASSERT_TRUE(WriteFile(file1, "content v2")); 408 ASSERT_TRUE(WriteFile(file1, "content v2"));
394 VLOG(1) << "Waiting for file1 modification"; 409 VLOG(1) << "Waiting for file1 modification";
395 WaitForEvents(); 410 ASSERT_TRUE(WaitForEvents());
396 411
397 ASSERT_TRUE(file_util::Delete(file1, false)); 412 ASSERT_TRUE(file_util::Delete(file1, false));
398 VLOG(1) << "Waiting for file1 deletion"; 413 VLOG(1) << "Waiting for file1 deletion";
399 WaitForEvents(); 414 ASSERT_TRUE(WaitForEvents());
400 415
401 ASSERT_TRUE(WriteFile(file2, "content")); 416 ASSERT_TRUE(WriteFile(file2, "content"));
402 VLOG(1) << "Waiting for file2 creation"; 417 VLOG(1) << "Waiting for file2 creation";
403 WaitForEvents(); 418 ASSERT_TRUE(WaitForEvents());
404 } 419 }
405 420
406 TEST_F(FilePathWatcherTest, MoveParent) { 421 TEST_F(FilePathWatcherTest, MoveParent) {
407 FilePathWatcher file_watcher; 422 FilePathWatcher file_watcher;
408 FilePathWatcher subdir_watcher; 423 FilePathWatcher subdir_watcher;
409 FilePath dir(temp_dir_->path().AppendASCII("dir")); 424 FilePath dir(temp_dir_.path().AppendASCII("dir"));
410 FilePath dest(temp_dir_->path().AppendASCII("dest")); 425 FilePath dest(temp_dir_.path().AppendASCII("dest"));
411 FilePath subdir(dir.AppendASCII("subdir")); 426 FilePath subdir(dir.AppendASCII("subdir"));
412 FilePath file(subdir.AppendASCII("file")); 427 FilePath file(subdir.AppendASCII("file"));
413 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); 428 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
414 SetupWatch(file, &file_watcher, file_delegate.get()); 429 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get()));
415 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); 430 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
416 SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()); 431 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()));
417 432
418 // Setup a directory hierarchy. 433 // Setup a directory hierarchy.
419 ASSERT_TRUE(file_util::CreateDirectory(subdir)); 434 ASSERT_TRUE(file_util::CreateDirectory(subdir));
420 ASSERT_TRUE(WriteFile(file, "content")); 435 ASSERT_TRUE(WriteFile(file, "content"));
421 VLOG(1) << "Waiting for file creation"; 436 VLOG(1) << "Waiting for file creation";
422 WaitForEvents(); 437 ASSERT_TRUE(WaitForEvents());
423 438
424 // Move the parent directory. 439 // Move the parent directory.
425 file_util::Move(dir, dest); 440 file_util::Move(dir, dest);
426 VLOG(1) << "Waiting for directory move"; 441 VLOG(1) << "Waiting for directory move";
427 WaitForEvents(); 442 ASSERT_TRUE(WaitForEvents());
428 } 443 }
429 444
430 TEST_F(FilePathWatcherTest, MoveChild) { 445 TEST_F(FilePathWatcherTest, MoveChild) {
431 FilePathWatcher file_watcher; 446 FilePathWatcher file_watcher;
432 FilePathWatcher subdir_watcher; 447 FilePathWatcher subdir_watcher;
433 FilePath source_dir(temp_dir_->path().AppendASCII("source")); 448 FilePath source_dir(temp_dir_.path().AppendASCII("source"));
434 FilePath source_subdir(source_dir.AppendASCII("subdir")); 449 FilePath source_subdir(source_dir.AppendASCII("subdir"));
435 FilePath source_file(source_subdir.AppendASCII("file")); 450 FilePath source_file(source_subdir.AppendASCII("file"));
436 FilePath dest_dir(temp_dir_->path().AppendASCII("dest")); 451 FilePath dest_dir(temp_dir_.path().AppendASCII("dest"));
437 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); 452 FilePath dest_subdir(dest_dir.AppendASCII("subdir"));
438 FilePath dest_file(dest_subdir.AppendASCII("file")); 453 FilePath dest_file(dest_subdir.AppendASCII("file"));
439 454
440 // Setup a directory hierarchy. 455 // Setup a directory hierarchy.
441 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); 456 ASSERT_TRUE(file_util::CreateDirectory(source_subdir));
442 ASSERT_TRUE(WriteFile(source_file, "content")); 457 ASSERT_TRUE(WriteFile(source_file, "content"));
443 458
444 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); 459 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector()));
445 SetupWatch(dest_file, &file_watcher, file_delegate.get()); 460 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get()));
446 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); 461 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector()));
447 SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()); 462 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()));
448 463
449 // Move the directory into place, s.t. the watched file appears. 464 // Move the directory into place, s.t. the watched file appears.
450 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); 465 ASSERT_TRUE(file_util::Move(source_dir, dest_dir));
451 WaitForEvents(); 466 ASSERT_TRUE(WaitForEvents());
452 } 467 }
453 468
454 } // namespace 469 } // namespace
OLDNEW
« no previous file with comments | « content/common/file_path_watcher/file_path_watcher.cc ('k') | content/common/file_path_watcher/file_path_watcher_inotify.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698