OLD | NEW |
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> | 7 #include <set> |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 112 |
113 protected: | 113 protected: |
114 virtual ~TestDelegate() {} | 114 virtual ~TestDelegate() {} |
115 | 115 |
116 private: | 116 private: |
117 scoped_refptr<NotificationCollector> collector_; | 117 scoped_refptr<NotificationCollector> collector_; |
118 | 118 |
119 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 119 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
120 }; | 120 }; |
121 | 121 |
122 void SetupWatchCallback(const FilePath& target, | 122 void SetupWatchDelegate(const FilePath& target, |
123 FilePathWatcher* watcher, | 123 FilePathWatcher* watcher, |
124 FilePathWatcher::Delegate* delegate, | 124 FilePathWatcher::Delegate* delegate, |
125 bool* result, | 125 bool* result, |
126 base::WaitableEvent* completion) { | 126 base::WaitableEvent* completion) { |
127 *result = watcher->Watch(target, delegate); | 127 *result = watcher->Watch(target, delegate); |
128 completion->Signal(); | 128 completion->Signal(); |
129 } | 129 } |
130 | 130 |
| 131 void SetupWatchCallback(const FilePath& target, |
| 132 FilePathWatcher* watcher, |
| 133 const FilePathWatcher::Callback& callback) { |
| 134 ASSERT_TRUE(watcher->Watch(target, callback)); |
| 135 } |
| 136 |
| 137 void QuitLoopWatchCallback(MessageLoop* loop, |
| 138 const FilePath& expected_path, |
| 139 bool expected_error, |
| 140 bool* flag, |
| 141 const FilePath& path, |
| 142 bool error) { |
| 143 ASSERT_TRUE(flag); |
| 144 *flag = true; |
| 145 EXPECT_EQ(expected_path, path); |
| 146 EXPECT_EQ(expected_error, error); |
| 147 loop->PostTask(FROM_HERE, loop->QuitClosure()); |
| 148 } |
| 149 |
131 class FilePathWatcherTest : public testing::Test { | 150 class FilePathWatcherTest : public testing::Test { |
132 public: | 151 public: |
133 FilePathWatcherTest() | 152 FilePathWatcherTest() |
134 : file_thread_("FilePathWatcherTest") {} | 153 : file_thread_("FilePathWatcherTest") {} |
135 | 154 |
136 virtual ~FilePathWatcherTest() {} | 155 virtual ~FilePathWatcherTest() {} |
137 | 156 |
138 protected: | 157 protected: |
139 virtual void SetUp() { | 158 virtual void SetUp() { |
140 // Create a separate file thread in order to test proper thread usage. | 159 // Create a separate file thread in order to test proper thread usage. |
(...skipping 22 matching lines...) Expand all Loading... |
163 return write_size == static_cast<int>(content.length()); | 182 return write_size == static_cast<int>(content.length()); |
164 } | 183 } |
165 | 184 |
166 bool SetupWatch(const FilePath& target, | 185 bool SetupWatch(const FilePath& target, |
167 FilePathWatcher* watcher, | 186 FilePathWatcher* watcher, |
168 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { | 187 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { |
169 base::WaitableEvent completion(false, false); | 188 base::WaitableEvent completion(false, false); |
170 bool result; | 189 bool result; |
171 file_thread_.message_loop_proxy()->PostTask( | 190 file_thread_.message_loop_proxy()->PostTask( |
172 FROM_HERE, | 191 FROM_HERE, |
173 base::Bind(SetupWatchCallback, target, watcher, | 192 base::Bind(SetupWatchDelegate, target, watcher, |
174 make_scoped_refptr(delegate), &result, &completion)); | 193 make_scoped_refptr(delegate), &result, &completion)); |
175 completion.Wait(); | 194 completion.Wait(); |
176 return result; | 195 return result; |
177 } | 196 } |
178 | 197 |
179 bool WaitForEvents() WARN_UNUSED_RESULT { | 198 bool WaitForEvents() WARN_UNUSED_RESULT { |
180 collector_->Reset(); | 199 collector_->Reset(); |
181 loop_.Run(); | 200 loop_.Run(); |
182 return collector_->Success(); | 201 return collector_->Success(); |
183 } | 202 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 251 |
233 FilePathWatcher watcher; | 252 FilePathWatcher watcher; |
234 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 253 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
235 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 254 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); |
236 | 255 |
237 // Now make sure we get notified if the file is deleted. | 256 // Now make sure we get notified if the file is deleted. |
238 file_util::Delete(test_file(), false); | 257 file_util::Delete(test_file(), false); |
239 ASSERT_TRUE(WaitForEvents()); | 258 ASSERT_TRUE(WaitForEvents()); |
240 } | 259 } |
241 | 260 |
| 261 TEST_F(FilePathWatcherTest, Callback) { |
| 262 FilePathWatcher watcher; |
| 263 bool called_back = false; |
| 264 |
| 265 MessageLoop* file_loop = file_thread_.message_loop(); |
| 266 ASSERT_TRUE(file_loop); |
| 267 // The callback makes |loop_| quit on file events, and flips |called_back| |
| 268 // to true. |
| 269 FilePathWatcher::Callback callback = base::Bind( |
| 270 QuitLoopWatchCallback, &loop_, test_file(), false, &called_back); |
| 271 |
| 272 // Start watching on the file thread, and unblock the loop once the callback |
| 273 // has been installed. |
| 274 file_thread_.message_loop_proxy()->PostTaskAndReply( |
| 275 FROM_HERE, |
| 276 base::Bind(SetupWatchCallback, test_file(), &watcher, callback), |
| 277 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_))); |
| 278 loop_.Run(); |
| 279 |
| 280 // The watch has been installed. Trigger a file event now, which will unblock |
| 281 // the loop again. |
| 282 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 283 loop_.Run(); |
| 284 EXPECT_TRUE(called_back); |
| 285 } |
| 286 |
242 // Used by the DeleteDuringNotify test below. | 287 // Used by the DeleteDuringNotify test below. |
243 // Deletes the FilePathWatcher when it's notified. | 288 // Deletes the FilePathWatcher when it's notified. |
244 class Deleter : public FilePathWatcher::Delegate { | 289 class Deleter : public FilePathWatcher::Delegate { |
245 public: | 290 public: |
246 Deleter(FilePathWatcher* watcher, MessageLoop* loop) | 291 Deleter(FilePathWatcher* watcher, MessageLoop* loop) |
247 : watcher_(watcher), | 292 : watcher_(watcher), |
248 loop_(loop) { | 293 loop_(loop) { |
249 } | 294 } |
250 | 295 |
251 virtual void OnFilePathChanged(const FilePath& path) { | 296 virtual void OnFilePathChanged(const FilePath& path) { |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 820 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
776 ASSERT_TRUE(WaitForEvents()); | 821 ASSERT_TRUE(WaitForEvents()); |
777 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 822 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
778 } | 823 } |
779 | 824 |
780 #endif // OS_MACOSX | 825 #endif // OS_MACOSX |
781 } // namespace | 826 } // namespace |
782 | 827 |
783 } // namespace files | 828 } // namespace files |
784 } // namespace base | 829 } // namespace base |
OLD | NEW |