Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/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 Loading... | |
| 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 Loading... | |
| 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 scoped_refptr<base::MessageLoopProxy> mac_run_loop) | |
|
Mattias Nissler (ping if slow)
2011/03/17 10:37:56
I think most of our code passes plain pointers in
dmac
2011/03/17 17:16:48
Done.
| |
| 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. | 143 FilePathWatcherTest() : loop_(MessageLoop::TYPE_UI), |
| 134 FilePathWatcherTest() | 144 file_thread_("FilePathWatcherTest") { } |
| 135 : loop_(MessageLoop::TYPE_UI), | |
| 136 ui_thread_(BrowserThread::UI, &loop_) { | |
| 137 } | |
| 138 | |
| 139 protected: | 145 protected: |
| 140 virtual void SetUp() { | 146 virtual void SetUp() { |
| 141 // Create a separate file thread in order to test proper thread usage. | 147 // Create a separate file thread in order to test proper thread usage. |
| 142 file_thread_.reset(new BrowserThread(BrowserThread::FILE)); | 148 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 143 file_thread_->Start(); | 149 ASSERT_TRUE(file_thread_.StartWithOptions(options)); |
| 144 temp_dir_.reset(new ScopedTempDir); | 150 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 145 ASSERT_TRUE(temp_dir_->CreateUniqueTempDir()); | |
| 146 collector_ = new NotificationCollector(); | 151 collector_ = new NotificationCollector(); |
| 147 } | 152 } |
| 148 | 153 |
| 149 virtual void TearDown() { | 154 virtual void TearDown() { |
| 150 loop_.RunAllPending(); | 155 loop_.RunAllPending(); |
| 151 file_thread_.reset(); | |
| 152 } | 156 } |
| 153 | 157 |
| 154 FilePath test_file() { | 158 FilePath test_file() { |
| 155 return temp_dir_->path().AppendASCII("FilePathWatcherTest"); | 159 return temp_dir_.path().AppendASCII("FilePathWatcherTest"); |
| 156 } | 160 } |
| 157 | 161 |
| 158 // Write |content| to |file|. Returns true on success. | 162 // Write |content| to |file|. Returns true on success. |
| 159 bool WriteFile(const FilePath& file, const std::string& content) { | 163 bool WriteFile(const FilePath& file, const std::string& content) { |
| 160 int write_size = file_util::WriteFile(file, content.c_str(), | 164 int write_size = file_util::WriteFile(file, content.c_str(), |
| 161 content.length()); | 165 content.length()); |
| 162 return write_size == static_cast<int>(content.length()); | 166 return write_size == static_cast<int>(content.length()); |
| 163 } | 167 } |
| 164 | 168 |
| 165 void SetupWatch(const FilePath& target, | 169 bool SetupWatch(const FilePath& target, |
| 166 FilePathWatcher* watcher, | 170 FilePathWatcher* watcher, |
| 167 FilePathWatcher::Delegate* delegate) { | 171 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { |
| 168 base::WaitableEvent completion(false, false); | 172 base::WaitableEvent completion(false, false); |
| 169 bool result; | 173 bool result; |
| 170 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 174 file_thread_.message_loop_proxy()->PostTask(FROM_HERE, |
| 171 new SetupWatchTask(target, watcher, delegate, &result, &completion)); | 175 new SetupWatchTask(target, |
| 176 watcher, | |
| 177 delegate, | |
| 178 &result, | |
| 179 &completion, | |
| 180 base::MessageLoopProxy::CreateForCurrentThread())); | |
| 172 completion.Wait(); | 181 completion.Wait(); |
| 173 ASSERT_TRUE(result); | 182 return result; |
| 174 } | 183 } |
| 175 | 184 |
| 176 void WaitForEvents() { | 185 bool WaitForEvents() WARN_UNUSED_RESULT { |
| 177 collector_->Reset(); | 186 collector_->Reset(); |
| 187 loop_.PostDelayedTask(FROM_HERE, | |
| 188 new MessageLoop::QuitTask(), | |
| 189 TestTimeouts::action_timeout_ms()); | |
|
Mattias Nissler (ping if slow)
2011/03/17 10:37:56
This is exactly the hang we saw on the Mac test bo
dmac
2011/03/17 17:16:48
Fixed by going back to browsertest
| |
| 178 loop_.Run(); | 190 loop_.Run(); |
| 191 return collector_->Success(); | |
| 179 } | 192 } |
| 180 | 193 |
| 181 NotificationCollector* collector() { return collector_.get(); } | 194 NotificationCollector* collector() { return collector_.get(); } |
| 182 | 195 |
| 183 MessageLoop loop_; | 196 MessageLoop loop_; |
| 184 BrowserThread ui_thread_; | 197 base::Thread file_thread_; |
| 185 scoped_ptr<BrowserThread> file_thread_; | 198 ScopedTempDir temp_dir_; |
| 186 scoped_ptr<ScopedTempDir> temp_dir_; | |
| 187 scoped_refptr<NotificationCollector> collector_; | 199 scoped_refptr<NotificationCollector> collector_; |
| 188 }; | 200 }; |
| 189 | 201 |
| 190 // Basic test: Create the file and verify that we notice. | 202 // Basic test: Create the file and verify that we notice. |
| 191 TEST_F(FilePathWatcherTest, MAYBE(NewFile)) { | 203 TEST_F(FilePathWatcherTest, MAYBE(NewFile)) { |
| 192 FilePathWatcher watcher; | 204 FilePathWatcher watcher; |
| 193 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 205 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 194 SetupWatch(test_file(), &watcher, delegate.get()); | 206 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); |
| 195 | 207 |
| 196 ASSERT_TRUE(WriteFile(test_file(), "content")); | 208 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 197 WaitForEvents(); | 209 ASSERT_TRUE(WaitForEvents()); |
| 198 } | 210 } |
| 199 | 211 |
| 200 // Verify that modifying the file is caught. | 212 // Verify that modifying the file is caught. |
| 201 TEST_F(FilePathWatcherTest, MAYBE(ModifiedFile)) { | 213 TEST_F(FilePathWatcherTest, MAYBE(ModifiedFile)) { |
| 202 ASSERT_TRUE(WriteFile(test_file(), "content")); | 214 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 203 | 215 |
| 204 FilePathWatcher watcher; | 216 FilePathWatcher watcher; |
| 205 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 217 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 206 SetupWatch(test_file(), &watcher, delegate.get()); | 218 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); |
| 207 | 219 |
| 208 // Now make sure we get notified if the file is modified. | 220 // Now make sure we get notified if the file is modified. |
| 209 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 221 ASSERT_TRUE(WriteFile(test_file(), "new content")); |
| 210 WaitForEvents(); | 222 ASSERT_TRUE(WaitForEvents()); |
| 211 } | 223 } |
| 212 | 224 |
| 213 // Verify that moving the file into place is caught. | 225 // Verify that moving the file into place is caught. |
| 214 TEST_F(FilePathWatcherTest, MAYBE(MovedFile)) { | 226 TEST_F(FilePathWatcherTest, MAYBE(MovedFile)) { |
| 215 FilePath source_file(temp_dir_->path().AppendASCII("source")); | 227 FilePath source_file(temp_dir_.path().AppendASCII("source")); |
| 216 ASSERT_TRUE(WriteFile(source_file, "content")); | 228 ASSERT_TRUE(WriteFile(source_file, "content")); |
| 217 | 229 |
| 218 FilePathWatcher watcher; | 230 FilePathWatcher watcher; |
| 219 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 231 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 220 SetupWatch(test_file(), &watcher, delegate.get()); | 232 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); |
| 221 | 233 |
| 222 // Now make sure we get notified if the file is modified. | 234 // Now make sure we get notified if the file is modified. |
| 223 ASSERT_TRUE(file_util::Move(source_file, test_file())); | 235 ASSERT_TRUE(file_util::Move(source_file, test_file())); |
| 224 WaitForEvents(); | 236 ASSERT_TRUE(WaitForEvents()); |
| 225 } | 237 } |
| 226 | 238 |
| 227 TEST_F(FilePathWatcherTest, MAYBE(DeletedFile)) { | 239 TEST_F(FilePathWatcherTest, MAYBE(DeletedFile)) { |
| 228 ASSERT_TRUE(WriteFile(test_file(), "content")); | 240 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 229 | 241 |
| 230 FilePathWatcher watcher; | 242 FilePathWatcher watcher; |
| 231 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 243 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 232 SetupWatch(test_file(), &watcher, delegate.get()); | 244 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); |
| 233 | 245 |
| 234 // Now make sure we get notified if the file is deleted. | 246 // Now make sure we get notified if the file is deleted. |
| 235 file_util::Delete(test_file(), false); | 247 file_util::Delete(test_file(), false); |
| 236 WaitForEvents(); | 248 ASSERT_TRUE(WaitForEvents()); |
| 237 } | 249 } |
| 238 | 250 |
| 239 // Used by the DeleteDuringNotify test below. | 251 // Used by the DeleteDuringNotify test below. |
| 240 // Deletes the FilePathWatcher when it's notified. | 252 // Deletes the FilePathWatcher when it's notified. |
| 241 class Deleter : public FilePathWatcher::Delegate { | 253 class Deleter : public FilePathWatcher::Delegate { |
| 242 public: | 254 public: |
| 243 Deleter(FilePathWatcher* watcher, MessageLoop* loop) | 255 Deleter(FilePathWatcher* watcher, MessageLoop* loop) |
| 244 : watcher_(watcher), | 256 : watcher_(watcher), |
| 245 loop_(loop) { | 257 loop_(loop) { |
| 246 } | 258 } |
| 247 | 259 |
| 248 virtual void OnFilePathChanged(const FilePath& path) { | 260 virtual void OnFilePathChanged(const FilePath& path) { |
| 249 watcher_.reset(NULL); | 261 watcher_.reset(NULL); |
| 250 loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 262 loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 251 } | 263 } |
| 252 | 264 |
| 253 scoped_ptr<FilePathWatcher> watcher_; | 265 scoped_ptr<FilePathWatcher> watcher_; |
| 254 MessageLoop* loop_; | 266 MessageLoop* loop_; |
| 255 }; | 267 }; |
| 256 | 268 |
| 257 // Verify that deleting a watcher during the callback doesn't crash. | 269 // Verify that deleting a watcher during the callback doesn't crash. |
| 258 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { | 270 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { |
| 259 FilePathWatcher* watcher = new FilePathWatcher; | 271 FilePathWatcher* watcher = new FilePathWatcher; |
| 260 // Takes ownership of watcher. | 272 // Takes ownership of watcher. |
| 261 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_)); | 273 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_)); |
| 262 SetupWatch(test_file(), watcher, deleter.get()); | 274 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get())); |
| 263 | 275 |
| 264 ASSERT_TRUE(WriteFile(test_file(), "content")); | 276 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 265 WaitForEvents(); | 277 ASSERT_TRUE(WaitForEvents()); |
| 266 | 278 |
| 267 // We win if we haven't crashed yet. | 279 // We win if we haven't crashed yet. |
| 268 // Might as well double-check it got deleted, too. | 280 // Might as well double-check it got deleted, too. |
| 269 ASSERT_TRUE(deleter->watcher_.get() == NULL); | 281 ASSERT_TRUE(deleter->watcher_.get() == NULL); |
| 270 } | 282 } |
| 271 | 283 |
| 272 // 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 |
| 273 // notification. | 285 // notification. |
| 274 TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) { | 286 TEST_F(FilePathWatcherTest, DestroyWithPendingNotification) { |
| 275 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 287 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 276 FilePathWatcher* watcher = new FilePathWatcher; | 288 FilePathWatcher* watcher = new FilePathWatcher; |
| 277 SetupWatch(test_file(), watcher, delegate.get()); | 289 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get())); |
| 278 ASSERT_TRUE(WriteFile(test_file(), "content")); | 290 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 279 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, watcher); | 291 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); |
| 280 } | 292 } |
| 281 | 293 |
| 282 TEST_F(FilePathWatcherTest, MAYBE(MultipleWatchersSingleFile)) { | 294 TEST_F(FilePathWatcherTest, MAYBE(MultipleWatchersSingleFile)) { |
| 283 FilePathWatcher watcher1, watcher2; | 295 FilePathWatcher watcher1, watcher2; |
| 284 scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector())); | 296 scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector())); |
| 285 scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector())); | 297 scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector())); |
| 286 SetupWatch(test_file(), &watcher1, delegate1.get()); | 298 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get())); |
| 287 SetupWatch(test_file(), &watcher2, delegate2.get()); | 299 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get())); |
| 288 | 300 |
| 289 ASSERT_TRUE(WriteFile(test_file(), "content")); | 301 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 290 WaitForEvents(); | 302 ASSERT_TRUE(WaitForEvents()); |
| 291 } | 303 } |
| 292 | 304 |
| 293 // 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 |
| 294 // the directory and file are created eventually. | 306 // the directory and file are created eventually. |
| 295 TEST_F(FilePathWatcherTest, NonExistentDirectory) { | 307 TEST_F(FilePathWatcherTest, MAYBE(NonExistentDirectory)) { |
| 296 FilePathWatcher watcher; | 308 FilePathWatcher watcher; |
| 297 FilePath dir(temp_dir_->path().AppendASCII("dir")); | 309 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 298 FilePath file(dir.AppendASCII("file")); | 310 FilePath file(dir.AppendASCII("file")); |
| 299 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 311 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 300 SetupWatch(file, &watcher, delegate.get()); | 312 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); |
| 301 | 313 |
| 302 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 314 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 303 | 315 |
| 304 ASSERT_TRUE(WriteFile(file, "content")); | 316 ASSERT_TRUE(WriteFile(file, "content")); |
| 317 | |
| 305 VLOG(1) << "Waiting for file creation"; | 318 VLOG(1) << "Waiting for file creation"; |
| 306 WaitForEvents(); | 319 ASSERT_TRUE(WaitForEvents()); |
| 307 | 320 |
| 308 ASSERT_TRUE(WriteFile(file, "content v2")); | 321 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 309 VLOG(1) << "Waiting for file change"; | 322 VLOG(1) << "Waiting for file change"; |
| 310 WaitForEvents(); | 323 ASSERT_TRUE(WaitForEvents()); |
| 311 | 324 |
| 312 ASSERT_TRUE(file_util::Delete(file, false)); | 325 ASSERT_TRUE(file_util::Delete(file, false)); |
| 313 VLOG(1) << "Waiting for file deletion"; | 326 VLOG(1) << "Waiting for file deletion"; |
| 314 WaitForEvents(); | 327 ASSERT_TRUE(WaitForEvents()); |
| 315 } | 328 } |
| 316 | 329 |
| 317 // Exercises watch reconfiguration for the case that directories on the path | 330 // Exercises watch reconfiguration for the case that directories on the path |
| 318 // are rapidly created. | 331 // are rapidly created. |
| 319 TEST_F(FilePathWatcherTest, DirectoryChain) { | 332 TEST_F(FilePathWatcherTest, DirectoryChain) { |
| 320 FilePath path(temp_dir_->path()); | 333 FilePath path(temp_dir_.path()); |
| 321 std::vector<std::string> dir_names; | 334 std::vector<std::string> dir_names; |
| 322 for (int i = 0; i < 20; i++) { | 335 for (int i = 0; i < 20; i++) { |
| 323 std::string dir(StringPrintf("d%d", i)); | 336 std::string dir(StringPrintf("d%d", i)); |
| 324 dir_names.push_back(dir); | 337 dir_names.push_back(dir); |
| 325 path = path.AppendASCII(dir); | 338 path = path.AppendASCII(dir); |
| 326 } | 339 } |
| 327 | 340 |
| 328 FilePathWatcher watcher; | 341 FilePathWatcher watcher; |
| 329 FilePath file(path.AppendASCII("file")); | 342 FilePath file(path.AppendASCII("file")); |
| 330 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 343 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 331 SetupWatch(file, &watcher, delegate.get()); | 344 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); |
| 332 | 345 |
| 333 FilePath sub_path(temp_dir_->path()); | 346 FilePath sub_path(temp_dir_.path()); |
| 334 for (std::vector<std::string>::const_iterator d(dir_names.begin()); | 347 for (std::vector<std::string>::const_iterator d(dir_names.begin()); |
| 335 d != dir_names.end(); ++d) { | 348 d != dir_names.end(); ++d) { |
| 336 sub_path = sub_path.AppendASCII(*d); | 349 sub_path = sub_path.AppendASCII(*d); |
| 337 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); | 350 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); |
| 338 } | 351 } |
| 352 VLOG(1) << "Create File"; | |
| 339 ASSERT_TRUE(WriteFile(file, "content")); | 353 ASSERT_TRUE(WriteFile(file, "content")); |
| 340 VLOG(1) << "Waiting for file creation"; | 354 VLOG(1) << "Waiting for file creation"; |
| 341 WaitForEvents(); | 355 ASSERT_TRUE(WaitForEvents()); |
| 342 | 356 |
| 343 ASSERT_TRUE(WriteFile(file, "content v2")); | 357 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 344 VLOG(1) << "Waiting for file modification"; | 358 VLOG(1) << "Waiting for file modification"; |
| 345 WaitForEvents(); | 359 ASSERT_TRUE(WaitForEvents()); |
| 346 } | 360 } |
| 347 | 361 |
| 348 TEST_F(FilePathWatcherTest, DisappearingDirectory) { | 362 TEST_F(FilePathWatcherTest, DisappearingDirectory) { |
| 349 FilePathWatcher watcher; | 363 FilePathWatcher watcher; |
| 350 FilePath dir(temp_dir_->path().AppendASCII("dir")); | 364 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 351 FilePath file(dir.AppendASCII("file")); | 365 FilePath file(dir.AppendASCII("file")); |
| 352 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 366 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 353 ASSERT_TRUE(WriteFile(file, "content")); | 367 ASSERT_TRUE(WriteFile(file, "content")); |
| 354 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 368 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 355 SetupWatch(file, &watcher, delegate.get()); | 369 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); |
| 356 | 370 |
| 357 ASSERT_TRUE(file_util::Delete(dir, true)); | 371 ASSERT_TRUE(file_util::Delete(dir, true)); |
| 358 WaitForEvents(); | 372 ASSERT_TRUE(WaitForEvents()); |
| 359 } | 373 } |
| 360 | 374 |
| 361 // 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. |
| 362 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { | 376 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { |
| 363 ASSERT_TRUE(WriteFile(test_file(), "content")); | 377 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 364 FilePathWatcher watcher; | 378 FilePathWatcher watcher; |
| 365 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 379 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 366 SetupWatch(test_file(), &watcher, delegate.get()); | 380 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); |
| 367 | 381 |
| 368 ASSERT_TRUE(file_util::Delete(test_file(), false)); | 382 ASSERT_TRUE(file_util::Delete(test_file(), false)); |
| 369 VLOG(1) << "Waiting for file deletion"; | 383 VLOG(1) << "Waiting for file deletion"; |
| 370 WaitForEvents(); | 384 ASSERT_TRUE(WaitForEvents()); |
| 371 | 385 |
| 372 ASSERT_TRUE(WriteFile(test_file(), "content")); | 386 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 373 VLOG(1) << "Waiting for file creation"; | 387 VLOG(1) << "Waiting for file creation"; |
| 374 WaitForEvents(); | 388 ASSERT_TRUE(WaitForEvents()); |
| 375 } | 389 } |
| 376 | 390 |
| 377 TEST_F(FilePathWatcherTest, WatchDirectory) { | 391 TEST_F(FilePathWatcherTest, WatchDirectory) { |
| 378 FilePathWatcher watcher; | 392 FilePathWatcher watcher; |
| 379 FilePath dir(temp_dir_->path().AppendASCII("dir")); | 393 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 380 FilePath file1(dir.AppendASCII("file1")); | 394 FilePath file1(dir.AppendASCII("file1")); |
| 381 FilePath file2(dir.AppendASCII("file2")); | 395 FilePath file2(dir.AppendASCII("file2")); |
| 382 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 396 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 383 SetupWatch(dir, &watcher, delegate.get()); | 397 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get())); |
| 384 | 398 |
| 385 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 399 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 386 VLOG(1) << "Waiting for directory creation"; | 400 VLOG(1) << "Waiting for directory creation"; |
| 387 WaitForEvents(); | 401 ASSERT_TRUE(WaitForEvents()); |
| 388 | 402 |
| 389 ASSERT_TRUE(WriteFile(file1, "content")); | 403 ASSERT_TRUE(WriteFile(file1, "content")); |
| 390 VLOG(1) << "Waiting for file1 creation"; | 404 VLOG(1) << "Waiting for file1 creation"; |
| 391 WaitForEvents(); | 405 ASSERT_TRUE(WaitForEvents()); |
| 392 | 406 |
| 393 ASSERT_TRUE(WriteFile(file1, "content v2")); | 407 ASSERT_TRUE(WriteFile(file1, "content v2")); |
| 394 VLOG(1) << "Waiting for file1 modification"; | 408 VLOG(1) << "Waiting for file1 modification"; |
| 395 WaitForEvents(); | 409 ASSERT_TRUE(WaitForEvents()); |
| 396 | 410 |
| 397 ASSERT_TRUE(file_util::Delete(file1, false)); | 411 ASSERT_TRUE(file_util::Delete(file1, false)); |
| 398 VLOG(1) << "Waiting for file1 deletion"; | 412 VLOG(1) << "Waiting for file1 deletion"; |
| 399 WaitForEvents(); | 413 ASSERT_TRUE(WaitForEvents()); |
| 400 | 414 |
| 401 ASSERT_TRUE(WriteFile(file2, "content")); | 415 ASSERT_TRUE(WriteFile(file2, "content")); |
| 402 VLOG(1) << "Waiting for file2 creation"; | 416 VLOG(1) << "Waiting for file2 creation"; |
| 403 WaitForEvents(); | 417 ASSERT_TRUE(WaitForEvents()); |
| 404 } | 418 } |
| 405 | 419 |
| 406 TEST_F(FilePathWatcherTest, MoveParent) { | 420 TEST_F(FilePathWatcherTest, MoveParent) { |
| 407 FilePathWatcher file_watcher; | 421 FilePathWatcher file_watcher; |
| 408 FilePathWatcher subdir_watcher; | 422 FilePathWatcher subdir_watcher; |
| 409 FilePath dir(temp_dir_->path().AppendASCII("dir")); | 423 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 410 FilePath dest(temp_dir_->path().AppendASCII("dest")); | 424 FilePath dest(temp_dir_.path().AppendASCII("dest")); |
| 411 FilePath subdir(dir.AppendASCII("subdir")); | 425 FilePath subdir(dir.AppendASCII("subdir")); |
| 412 FilePath file(subdir.AppendASCII("file")); | 426 FilePath file(subdir.AppendASCII("file")); |
| 413 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 427 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
| 414 SetupWatch(file, &file_watcher, file_delegate.get()); | 428 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get())); |
| 415 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 429 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
| 416 SetupWatch(subdir, &subdir_watcher, subdir_delegate.get()); | 430 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get())); |
| 417 | 431 |
| 418 // Setup a directory hierarchy. | 432 // Setup a directory hierarchy. |
| 419 ASSERT_TRUE(file_util::CreateDirectory(subdir)); | 433 ASSERT_TRUE(file_util::CreateDirectory(subdir)); |
| 420 ASSERT_TRUE(WriteFile(file, "content")); | 434 ASSERT_TRUE(WriteFile(file, "content")); |
| 421 VLOG(1) << "Waiting for file creation"; | 435 VLOG(1) << "Waiting for file creation"; |
| 422 WaitForEvents(); | 436 ASSERT_TRUE(WaitForEvents()); |
| 423 | 437 |
| 424 // Move the parent directory. | 438 // Move the parent directory. |
| 425 file_util::Move(dir, dest); | 439 file_util::Move(dir, dest); |
| 426 VLOG(1) << "Waiting for directory move"; | 440 VLOG(1) << "Waiting for directory move"; |
| 427 WaitForEvents(); | 441 ASSERT_TRUE(WaitForEvents()); |
| 428 } | 442 } |
| 429 | 443 |
| 430 TEST_F(FilePathWatcherTest, MoveChild) { | 444 TEST_F(FilePathWatcherTest, MoveChild) { |
| 431 FilePathWatcher file_watcher; | 445 FilePathWatcher file_watcher; |
| 432 FilePathWatcher subdir_watcher; | 446 FilePathWatcher subdir_watcher; |
| 433 FilePath source_dir(temp_dir_->path().AppendASCII("source")); | 447 FilePath source_dir(temp_dir_.path().AppendASCII("source")); |
| 434 FilePath source_subdir(source_dir.AppendASCII("subdir")); | 448 FilePath source_subdir(source_dir.AppendASCII("subdir")); |
| 435 FilePath source_file(source_subdir.AppendASCII("file")); | 449 FilePath source_file(source_subdir.AppendASCII("file")); |
| 436 FilePath dest_dir(temp_dir_->path().AppendASCII("dest")); | 450 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); |
| 437 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); | 451 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); |
| 438 FilePath dest_file(dest_subdir.AppendASCII("file")); | 452 FilePath dest_file(dest_subdir.AppendASCII("file")); |
| 439 | 453 |
| 440 // Setup a directory hierarchy. | 454 // Setup a directory hierarchy. |
| 441 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); | 455 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); |
| 442 ASSERT_TRUE(WriteFile(source_file, "content")); | 456 ASSERT_TRUE(WriteFile(source_file, "content")); |
| 443 | 457 |
| 444 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 458 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
| 445 SetupWatch(dest_file, &file_watcher, file_delegate.get()); | 459 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get())); |
| 446 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 460 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
| 447 SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get()); | 461 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get())); |
| 448 | 462 |
| 449 // Move the directory into place, s.t. the watched file appears. | 463 // Move the directory into place, s.t. the watched file appears. |
| 450 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); | 464 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); |
| 451 WaitForEvents(); | 465 ASSERT_TRUE(WaitForEvents()); |
| 452 } | 466 } |
| 453 | 467 |
| 454 } // namespace | 468 } // namespace |
| OLD | NEW |