1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/file_path_watcher.h"
#include <limits>
#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/lock.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/platform_thread.h"
#include "base/scoped_temp_dir.h"
#include "base/string_util.h"
#include "base/thread.h"
#include "base/waitable_event.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_MACOSX)
// TODO(mnissler): There are flakes on Mac (http://crbug.com/54822) at least for
// FilePathWatcherTest.MultipleWatchersSingleFile.
#define MAYBE(name) FLAKY_ ## name
#else
#define MAYBE(name) name
#endif
namespace {
// The time we wait for events to happen. It should be large enough to be
// reasonably sure all notifications sent in response to file system
// modifications made by the test are processed. This must also accomodate for
// the latency interval we pass to the Mac FSEvents API.
const int kWaitForEventTime = 1000;
// A mock FilePathWatcher::Delegate for testing. I'd rather use gmock, but it's
// not thread safe for setting expectations, so the test code couldn't safely
// reset expectations while the file watcher is running. In order to allow this,
// we implement simple thread safe call counters in TestDelegate.
class TestDelegate : public FilePathWatcher::Delegate {
public:
TestDelegate()
: change_count_(0),
error_count_(0) {}
virtual void OnFilePathChanged(const FilePath&) {
AutoLock lock(lock_);
++change_count_;
}
virtual void OnError() {
AutoLock lock(lock_);
++error_count_;
}
void GetCountsAndReset(int* changes, int* errors) {
AutoLock lock(lock_);
*errors = error_count_;
*changes = change_count_;
error_count_ = 0;
change_count_ = 0;
}
private:
Lock lock_;
int change_count_;
int error_count_;
DISALLOW_COPY_AND_ASSIGN(TestDelegate);
};
// A helper class for setting up watches on the file thread.
class SetupWatchTask : public Task {
public:
SetupWatchTask(const FilePath& target,
FilePathWatcher* watcher,
FilePathWatcher::Delegate* delegate,
bool* result,
base::WaitableEvent* completion)
: target_(target),
watcher_(watcher),
delegate_(delegate),
result_(result),
completion_(completion) {}
void Run() {
*result_ = watcher_->Watch(target_, delegate_);
completion_->Signal();
}
private:
const FilePath target_;
FilePathWatcher* watcher_;
FilePathWatcher::Delegate* delegate_;
bool* result_;
base::WaitableEvent* completion_;
DISALLOW_COPY_AND_ASSIGN(SetupWatchTask);
};
class FilePathWatcherTest : public testing::Test {
public:
// Implementation of FilePathWatcher on Mac requires UI loop.
FilePathWatcherTest()
: loop_(MessageLoop::TYPE_UI),
ui_thread_(ChromeThread::UI, &loop_) {
}
protected:
virtual void SetUp() {
// Create a separate file thread in order to test proper thread usage.
file_thread_.reset(new ChromeThread(ChromeThread::FILE));
file_thread_->Start();
temp_dir_.reset(new ScopedTempDir);
ASSERT_TRUE(temp_dir_->CreateUniqueTempDir());
}
virtual void TearDown() {
loop_.RunAllPending();
file_thread_.reset();
}
FilePath test_file() {
return temp_dir_->path().AppendASCII("FilePathWatcherTest");
}
// Write |content| to |file|. Returns true on success.
bool WriteFile(const FilePath& file, const std::string& content) {
int write_size = file_util::WriteFile(file, content.c_str(),
content.length());
SyncIfPOSIX();
return write_size == static_cast<int>(content.length());
}
void SetupWatch(const FilePath& target,
FilePathWatcher* watcher,
FilePathWatcher::Delegate* delegate) {
base::WaitableEvent completion(false, false);
bool result;
ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
new SetupWatchTask(target, watcher, delegate, &result, &completion));
completion.Wait();
ASSERT_TRUE(result);
}
int WaitForEvents(TestDelegate* delegate) {
// Wait for events and check the expectations of the delegate.
loop_.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask,
kWaitForEventTime);
loop_.Run();
int errors;
int changes;
delegate->GetCountsAndReset(&changes, &errors);
EXPECT_EQ(0, errors);
return changes;
}
// We need this function for reliable tests on Mac OS X. FSEvents API
// has a latency interval and can merge multiple events into one,
// and we need a clear distinction between events triggered by test setup code
// and test code.
void SyncIfPOSIX() {
#if defined(OS_POSIX)
sync();
#endif // defined(OS_POSIX)
}
MessageLoop loop_;
ChromeThread ui_thread_;
scoped_ptr<ChromeThread> file_thread_;
scoped_ptr<ScopedTempDir> temp_dir_;
};
// Basic test: Create the file and verify that we notice.
TEST_F(FilePathWatcherTest, MAYBE(NewFile)) {
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(test_file(), &watcher, delegate.get());
ASSERT_TRUE(WriteFile(test_file(), "content"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
// Verify that modifying the file is caught.
TEST_F(FilePathWatcherTest, MAYBE(ModifiedFile)) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(test_file(), &watcher, delegate.get());
// Now make sure we get notified if the file is modified.
ASSERT_TRUE(WriteFile(test_file(), "new content"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
// Verify that moving the file into place is caught.
TEST_F(FilePathWatcherTest, MAYBE(MovedFile)) {
FilePath source_file(temp_dir_->path().AppendASCII("source"));
ASSERT_TRUE(WriteFile(source_file, "content"));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(test_file(), &watcher, delegate.get());
// Now make sure we get notified if the file is modified.
ASSERT_TRUE(file_util::Move(source_file, test_file()));
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
TEST_F(FilePathWatcherTest, MAYBE(DeletedFile)) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(test_file(), &watcher, delegate.get());
// Now make sure we get notified if the file is deleted.
file_util::Delete(test_file(), false);
SyncIfPOSIX();
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
// Verify that letting the watcher go out of scope stops notifications.
TEST_F(FilePathWatcherTest, Unregister) {
scoped_refptr<TestDelegate> delegate(new TestDelegate);
{
FilePathWatcher watcher;
SetupWatch(test_file(), &watcher, delegate.get());
// And then let it fall out of scope, clearing its watch.
}
// Write a file to the test dir.
ASSERT_TRUE(WriteFile(test_file(), "content"));
EXPECT_EQ(0, WaitForEvents(delegate.get()));
}
namespace {
// Used by the DeleteDuringNotify test below.
// Deletes the FilePathWatcher when it's notified.
class Deleter : public FilePathWatcher::Delegate {
public:
Deleter(FilePathWatcher* watcher, MessageLoop* loop)
: watcher_(watcher),
loop_(loop) {
}
virtual void OnFilePathChanged(const FilePath& path) {
watcher_.reset(NULL);
loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
}
scoped_ptr<FilePathWatcher> watcher_;
MessageLoop* loop_;
};
} // anonymous namespace
// Verify that deleting a watcher during the callback doesn't crash.
TEST_F(FilePathWatcherTest, DeleteDuringNotify) {
FilePathWatcher* watcher = new FilePathWatcher;
// Takes ownership of watcher.
scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_));
SetupWatch(test_file(), watcher, deleter.get());
ASSERT_TRUE(WriteFile(test_file(), "content"));
loop_.Run();
// We win if we haven't crashed yet.
// Might as well double-check it got deleted, too.
ASSERT_TRUE(deleter->watcher_.get() == NULL);
}
// Verify that deleting the watcher works even if there is a pending
// notification.
TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) {
scoped_refptr<TestDelegate> delegate(new TestDelegate);
FilePathWatcher* watcher = new FilePathWatcher;
SetupWatch(test_file(), watcher, delegate.get());
ASSERT_TRUE(WriteFile(test_file(), "content"));
ChromeThread::DeleteSoon(ChromeThread::FILE, FROM_HERE, watcher);
// Success if there is no crash or DCHECK when running the callback.
WaitForEvents(delegate.get());
}
TEST_F(FilePathWatcherTest, MAYBE(MultipleWatchersSingleFile)) {
FilePathWatcher watcher1, watcher2;
scoped_refptr<TestDelegate> delegate1(new TestDelegate);
scoped_refptr<TestDelegate> delegate2(new TestDelegate);
SetupWatch(test_file(), &watcher1, delegate1.get());
SetupWatch(test_file(), &watcher2, delegate2.get());
ASSERT_TRUE(WriteFile(test_file(), "content"));
EXPECT_LE(1, WaitForEvents(delegate1.get()));
EXPECT_LE(1, WaitForEvents(delegate2.get()));
}
// Verify that watching a file whose parent directory doesn't exist yet works if
// the directory and file are created eventually.
TEST_F(FilePathWatcherTest, MAYBE(NonExistentDirectory)) {
FilePathWatcher watcher;
FilePath dir(temp_dir_->path().AppendASCII("dir"));
FilePath file(dir.AppendASCII("file"));
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(file, &watcher, delegate.get());
ASSERT_TRUE(file_util::CreateDirectory(dir));
EXPECT_EQ(0, WaitForEvents(delegate.get()));
ASSERT_TRUE(WriteFile(file, "content"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(WriteFile(file, "content v2"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(file_util::Delete(file, false));
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
// Exercises watch reconfiguration for the case that directories on the path
// are rapidly created.
TEST_F(FilePathWatcherTest, MAYBE(DirectoryChain)) {
FilePath path(temp_dir_->path());
std::vector<std::string> dir_names;
for (int i = 0; i < 20; i++) {
std::string dir(StringPrintf("d%d", i));
dir_names.push_back(dir);
path = path.AppendASCII(dir);
}
FilePathWatcher watcher;
FilePath file(path.AppendASCII("file"));
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(file, &watcher, delegate.get());
FilePath sub_path(temp_dir_->path());
for (std::vector<std::string>::const_iterator d(dir_names.begin());
d != dir_names.end(); ++d) {
sub_path = sub_path.AppendASCII(*d);
ASSERT_TRUE(file_util::CreateDirectory(sub_path));
}
ASSERT_TRUE(WriteFile(file, "content"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(WriteFile(file, "content v2"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
TEST_F(FilePathWatcherTest, DisappearingDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_->path().AppendASCII("dir"));
FilePath file(dir.AppendASCII("file"));
ASSERT_TRUE(file_util::CreateDirectory(dir));
ASSERT_TRUE(WriteFile(file, "content"));
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(file, &watcher, delegate.get());
ASSERT_TRUE(file_util::Delete(dir, true));
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(file_util::CreateDirectory(dir));
EXPECT_EQ(0, WaitForEvents(delegate.get()));
ASSERT_TRUE(WriteFile(file, "content v2"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
// Tests that a file that is deleted and reappears is tracked correctly.
TEST_F(FilePathWatcherTest, MAYBE(DeleteAndRecreate)) {
ASSERT_TRUE(WriteFile(test_file(), "content"));
FilePathWatcher watcher;
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(test_file(), &watcher, delegate.get());
ASSERT_TRUE(file_util::Delete(test_file(), false));
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(WriteFile(test_file(), "content"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
TEST_F(FilePathWatcherTest, WatchDirectory) {
FilePathWatcher watcher;
FilePath dir(temp_dir_->path().AppendASCII("dir"));
FilePath file1(dir.AppendASCII("file1"));
FilePath file2(dir.AppendASCII("file2"));
scoped_refptr<TestDelegate> delegate(new TestDelegate);
SetupWatch(dir, &watcher, delegate.get());
ASSERT_TRUE(file_util::CreateDirectory(dir));
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(WriteFile(file1, "content"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(WriteFile(file1, "content v2"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(file_util::Delete(file1, false));
SyncIfPOSIX();
EXPECT_LE(1, WaitForEvents(delegate.get()));
ASSERT_TRUE(WriteFile(file2, "content"));
EXPECT_LE(1, WaitForEvents(delegate.get()));
}
TEST_F(FilePathWatcherTest, MAYBE(MoveParent)) {
FilePathWatcher file_watcher;
FilePathWatcher subdir_watcher;
FilePath dir(temp_dir_->path().AppendASCII("dir"));
FilePath dest(temp_dir_->path().AppendASCII("dest"));
FilePath subdir(dir.AppendASCII("subdir"));
FilePath file(subdir.AppendASCII("file"));
scoped_refptr<TestDelegate> file_delegate(new TestDelegate);
SetupWatch(file, &file_watcher, file_delegate.get());
scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate);
SetupWatch(subdir, &subdir_watcher, subdir_delegate.get());
// Setup a directory hierarchy.
ASSERT_TRUE(file_util::CreateDirectory(subdir));
ASSERT_TRUE(WriteFile(file, "content"));
EXPECT_LE(1, WaitForEvents(file_delegate.get()));
EXPECT_LE(1, WaitForEvents(subdir_delegate.get()));
// Move the parent directory.
file_util::Move(dir, dest);
EXPECT_LE(1, WaitForEvents(file_delegate.get()));
EXPECT_LE(1, WaitForEvents(subdir_delegate.get()));
// Recreate the hierarchy.
ASSERT_TRUE(file_util::CreateDirectory(subdir));
ASSERT_TRUE(WriteFile(file, "content"));
EXPECT_LE(1, WaitForEvents(file_delegate.get()));
EXPECT_LE(1, WaitForEvents(subdir_delegate.get()));
}
TEST_F(FilePathWatcherTest, MAYBE(MoveChild)) {
FilePathWatcher file_watcher;
FilePathWatcher subdir_watcher;
FilePath source_dir(temp_dir_->path().AppendASCII("source"));
FilePath source_subdir(source_dir.AppendASCII("subdir"));
FilePath source_file(source_subdir.AppendASCII("file"));
FilePath dest_dir(temp_dir_->path().AppendASCII("dest"));
FilePath dest_subdir(dest_dir.AppendASCII("subdir"));
FilePath dest_file(dest_subdir.AppendASCII("file"));
scoped_refptr<TestDelegate> file_delegate(new TestDelegate);
SetupWatch(dest_file, &file_watcher, file_delegate.get());
scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate);
SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get());
// Setup a directory hierarchy.
ASSERT_TRUE(file_util::CreateDirectory(source_subdir));
ASSERT_TRUE(WriteFile(source_file, "content"));
EXPECT_EQ(0, WaitForEvents(file_delegate.get()));
EXPECT_EQ(0, WaitForEvents(subdir_delegate.get()));
// Move the directory into place, s.t. the watched file appears.
ASSERT_TRUE(file_util::Move(source_dir, dest_dir));
EXPECT_LE(1, WaitForEvents(file_delegate.get()));
EXPECT_LE(1, WaitForEvents(subdir_delegate.get()));
}
} // namespace
|