blob: e925e274e2e45a77e60c15cea8022ff0107487e8 (
plain)
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
|
// Copyright (c) 2012 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 "net/dns/file_path_watcher_wrapper.h"
#include "base/bind.h"
#include "base/files/file_path_watcher.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class FilePathWatcherWrapperTest : public testing::Test {
protected:
class MockFilePathWatcher : public base::files::FilePathWatcher {
public:
virtual bool Watch(const FilePath& path, Delegate* delegate) OVERRIDE {
DCHECK(!delegate_);
DCHECK(delegate);
path_ = path;
delegate_ = delegate;
return true;
}
void OnFilePathChanged() {
delegate_->OnFilePathChanged(path_);
}
void OnFilePathError() {
delegate_->OnFilePathError(path_);
}
private:
FilePath path_;
scoped_refptr<Delegate> delegate_;
};
class FailingFilePathWatcher : public MockFilePathWatcher {
public:
virtual bool Watch(const FilePath& path, Delegate* delegate) OVERRIDE {
MockFilePathWatcher::Watch(path, delegate);
return false;
}
};
// Class under test.
class TestFilePathWatcherWrapper : public FilePathWatcherWrapper {
public:
TestFilePathWatcherWrapper() : fail_on_watch_(false) {}
MockFilePathWatcher* file_path_watcher() {
return test_watcher_;
}
void set_fail_on_watch(bool value) {
fail_on_watch_ = value;
}
private:
virtual scoped_ptr<base::files::FilePathWatcher>
CreateFilePathWatcher() OVERRIDE {
test_watcher_ = fail_on_watch_ ? new FailingFilePathWatcher()
: new MockFilePathWatcher();
return scoped_ptr<base::files::FilePathWatcher>(test_watcher_);
}
MockFilePathWatcher* test_watcher_;
bool fail_on_watch_;
};
virtual void SetUp() OVERRIDE {
watcher_wrapper_.reset(new TestFilePathWatcherWrapper());
got_change_ = false;
}
bool Watch() {
return watcher_wrapper_->Watch(
FilePath(FILE_PATH_LITERAL("some_file_name")),
base::Bind(&FilePathWatcherWrapperTest::OnChange,
base::Unretained(this)));
}
void OnChange(bool succeeded) {
got_change_ = true;
last_result_ = succeeded;
}
scoped_ptr<TestFilePathWatcherWrapper> watcher_wrapper_;
bool got_change_;
bool last_result_;
};
TEST_F(FilePathWatcherWrapperTest, Basic) {
watcher_wrapper_->set_fail_on_watch(true);
EXPECT_FALSE(Watch());
EXPECT_FALSE(watcher_wrapper_->IsWatching());
EXPECT_FALSE(got_change_);
watcher_wrapper_->set_fail_on_watch(false);
EXPECT_TRUE(Watch());
EXPECT_TRUE(watcher_wrapper_->IsWatching());
EXPECT_FALSE(got_change_);
EXPECT_TRUE(Watch());
EXPECT_TRUE(watcher_wrapper_->IsWatching());
EXPECT_FALSE(got_change_);
watcher_wrapper_->file_path_watcher()->OnFilePathChanged();
EXPECT_TRUE(watcher_wrapper_->IsWatching());
EXPECT_TRUE(got_change_);
EXPECT_TRUE(last_result_);
got_change_ = false;
watcher_wrapper_->file_path_watcher()->OnFilePathError();
EXPECT_FALSE(watcher_wrapper_->IsWatching());
EXPECT_TRUE(got_change_);
EXPECT_FALSE(last_result_);
got_change_ = false;
EXPECT_TRUE(Watch());
EXPECT_TRUE(watcher_wrapper_->IsWatching());
EXPECT_FALSE(got_change_);
}
TEST_F(FilePathWatcherWrapperTest, Cancel) {
EXPECT_TRUE(Watch());
EXPECT_TRUE(watcher_wrapper_->IsWatching());
watcher_wrapper_->Cancel();
EXPECT_FALSE(watcher_wrapper_->IsWatching());
EXPECT_FALSE(got_change_);
EXPECT_TRUE(Watch());
EXPECT_TRUE(watcher_wrapper_->IsWatching());
}
} // namespace
} // namespace net
|