Chromium Code Reviews| 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> | |
| 8 | |
| 9 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 10 #include <windows.h> | 8 #include <windows.h> |
| 11 #include <aclapi.h> | 9 #include <aclapi.h> |
| 12 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
| 13 #include <sys/stat.h> | 11 #include <sys/stat.h> |
| 14 #endif | 12 #endif |
| 15 | 13 |
| 14 #include <set> | |
| 15 | |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/bind_helpers.h" | 18 #include "base/bind_helpers.h" |
| 19 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
| 20 #include "base/file_path.h" | 20 #include "base/file_path.h" |
| 21 #include "base/file_util.h" | 21 #include "base/file_util.h" |
| 22 #include "base/files/scoped_temp_dir.h" | 22 #include "base/files/scoped_temp_dir.h" |
| 23 #include "base/message_loop.h" | 23 #include "base/message_loop.h" |
| 24 #include "base/message_loop_proxy.h" | 24 #include "base/message_loop_proxy.h" |
| 25 #include "base/path_service.h" | 25 #include "base/path_service.h" |
| 26 #include "base/stl_util.h" | 26 #include "base/stl_util.h" |
| 27 #include "base/stringprintf.h" | 27 #include "base/stringprintf.h" |
| 28 #include "base/synchronization/waitable_event.h" | 28 #include "base/synchronization/waitable_event.h" |
| 29 #include "base/test/test_file_util.h" | 29 #include "base/test/test_file_util.h" |
| 30 #include "base/test/test_timeouts.h" | 30 #include "base/test/test_timeouts.h" |
| 31 #include "base/threading/thread.h" | 31 #include "base/threading/thread.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 33 |
| 34 namespace base { | 34 namespace base { |
| 35 namespace files { | 35 namespace files { |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 class TestDelegate; | 39 class TestDelegate; |
| 40 | 40 |
| 41 const char kTestContent[] = "content"; | |
| 42 const char kNewTestContent[] = "new content"; | |
| 43 | |
| 41 // Aggregates notifications from the test delegates and breaks the message loop | 44 // Aggregates notifications from the test delegates and breaks the message loop |
| 42 // the test thread is waiting on once they all came in. | 45 // the test thread is waiting on once they all came in. |
| 43 class NotificationCollector | 46 class NotificationCollector |
| 44 : public base::RefCountedThreadSafe<NotificationCollector> { | 47 : public base::RefCountedThreadSafe<NotificationCollector> { |
| 45 public: | 48 public: |
| 46 NotificationCollector() | 49 NotificationCollector() |
| 47 : loop_(base::MessageLoopProxy::current()) {} | 50 : loop_(base::MessageLoopProxy::current()) {} |
| 48 | 51 |
| 49 // Called from the file thread by the delegates. | 52 // Called from the file thread by the delegates. |
| 50 void OnChange(TestDelegate* delegate) { | 53 void OnChange(TestDelegate* delegate) { |
| 51 loop_->PostTask(FROM_HERE, | 54 loop_->PostTask(FROM_HERE, |
| 52 base::Bind(&NotificationCollector::RecordChange, this, | 55 base::Bind(&NotificationCollector::RecordChange, this, |
| 53 make_scoped_refptr(delegate))); | 56 base::Unretained(delegate))); |
| 54 } | 57 } |
| 55 | 58 |
| 56 void Register(TestDelegate* delegate) { | 59 void Register(TestDelegate* delegate) { |
| 57 delegates_.insert(delegate); | 60 delegates_.insert(delegate); |
| 58 } | 61 } |
| 59 | 62 |
| 60 void Reset() { | 63 void Reset() { |
| 61 signaled_.clear(); | 64 signaled_.clear(); |
| 62 } | 65 } |
| 63 | 66 |
| 64 bool Success() { | 67 bool Success() { |
| 65 return signaled_ == delegates_; | 68 return signaled_ == delegates_; |
| 66 } | 69 } |
| 67 | 70 |
| 68 private: | 71 private: |
| 69 friend class base::RefCountedThreadSafe<NotificationCollector>; | 72 friend class base::RefCountedThreadSafe<NotificationCollector>; |
| 70 ~NotificationCollector() {} | 73 ~NotificationCollector() {} |
| 71 | 74 |
| 72 void RecordChange(TestDelegate* delegate) { | 75 void RecordChange(TestDelegate* delegate) { |
| 76 // Warning: |delegate| is Unretained. Do not dereference. | |
| 73 ASSERT_TRUE(loop_->BelongsToCurrentThread()); | 77 ASSERT_TRUE(loop_->BelongsToCurrentThread()); |
| 74 ASSERT_TRUE(delegates_.count(delegate)); | 78 ASSERT_TRUE(delegates_.count(delegate)); |
| 75 signaled_.insert(delegate); | 79 signaled_.insert(delegate); |
| 76 | 80 |
| 77 // Check whether all delegates have been signaled. | 81 // Check whether all delegates have been signaled. |
| 78 if (signaled_ == delegates_) | 82 if (signaled_ == delegates_) |
| 79 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 83 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 80 } | 84 } |
| 81 | 85 |
| 82 // Set of registered delegates. | 86 // Set of registered delegates. |
| 83 std::set<TestDelegate*> delegates_; | 87 std::set<TestDelegate*> delegates_; |
| 84 | 88 |
| 85 // Set of signaled delegates. | 89 // Set of signaled delegates. |
| 86 std::set<TestDelegate*> signaled_; | 90 std::set<TestDelegate*> signaled_; |
| 87 | 91 |
| 88 // The loop we should break after all delegates signaled. | 92 // The loop we should break after all delegates signaled. |
| 89 scoped_refptr<base::MessageLoopProxy> loop_; | 93 scoped_refptr<base::MessageLoopProxy> loop_; |
| 90 }; | 94 }; |
| 91 | 95 |
| 92 // A mock FilePathWatcher::Delegate for testing. I'd rather use gmock, but it's | 96 // A mock class for testing. I'd rather use gmock, but it's |
| 93 // not thread safe for setting expectations, so the test code couldn't safely | 97 // not thread safe for setting expectations, so the test code couldn't safely |
| 94 // reset expectations while the file watcher is running. In order to allow this, | 98 // reset expectations while the file watcher is running. In order to allow this, |
| 95 // we keep simple thread safe status flags in TestDelegate. | 99 // we keep simple thread safe status flags in TestDelegate. |
| 96 class TestDelegate : public FilePathWatcher::Delegate { | 100 class TestDelegate : public SupportsWeakPtr<TestDelegate> { |
|
kmadhusu
2012/11/28 20:13:06
Do we need to update the comment?
Can we rename T
Lei Zhang
2012/11/28 23:27:19
Done.
| |
| 97 public: | 101 public: |
| 98 // The message loop specified by |loop| will be quit if a notification is | 102 // The message loop specified by |loop| will be quit if a notification is |
| 99 // received while the delegate is |armed_|. Note that the testing code must | 103 // received while the delegate is |armed_|. Note that the testing code must |
| 100 // guarantee |loop| outlives the file thread on which OnFilePathChanged runs. | 104 // guarantee |loop| outlives the file thread on which OnFileChanged runs. |
|
kmadhusu
2012/11/28 20:13:06
Do we need to update this comment?
Lei Zhang
2012/11/28 23:27:19
The comment is obsolete.
| |
| 101 explicit TestDelegate(NotificationCollector* collector) | 105 explicit TestDelegate(NotificationCollector* collector) |
| 102 : collector_(collector) { | 106 : collector_(collector) { |
| 103 collector_->Register(this); | 107 collector_->Register(this); |
| 104 } | 108 } |
| 109 ~TestDelegate() {} | |
| 105 | 110 |
| 106 virtual void OnFilePathChanged(const FilePath&) { | 111 void OnFileChanged(const FilePath& path, bool error) { |
| 107 collector_->OnChange(this); | 112 if (error) |
| 113 ADD_FAILURE() << "Error " << path.value(); | |
| 114 else | |
| 115 collector_->OnChange(this); | |
| 108 } | 116 } |
| 109 | 117 |
| 110 virtual void OnFilePathError(const FilePath& path) { | |
| 111 ADD_FAILURE() << "Error " << path.value(); | |
| 112 } | |
| 113 | |
| 114 protected: | |
| 115 virtual ~TestDelegate() {} | |
| 116 | |
| 117 private: | 118 private: |
| 118 scoped_refptr<NotificationCollector> collector_; | 119 scoped_refptr<NotificationCollector> collector_; |
| 119 | 120 |
| 120 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 121 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 121 }; | 122 }; |
| 122 | 123 |
| 123 void SetupWatchDelegate(const FilePath& target, | 124 // Used by the DeleteDuringNotify test below. |
| 124 FilePathWatcher* watcher, | 125 // Deletes the FilePathWatcher when it's notified. |
| 125 FilePathWatcher::Delegate* delegate, | 126 class Deleter : public SupportsWeakPtr<Deleter> { |
| 126 bool* result, | 127 public: |
| 127 base::WaitableEvent* completion) { | 128 Deleter(FilePathWatcher* watcher, MessageLoop* loop) |
| 128 *result = watcher->Watch(target, delegate); | 129 : watcher_(watcher), |
| 130 loop_(loop) { | |
| 131 } | |
| 132 ~Deleter() {} | |
| 133 | |
| 134 void OnFileChanged(const FilePath&, bool) { | |
| 135 watcher_.reset(); | |
| 136 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 137 } | |
| 138 | |
| 139 FilePathWatcher* watcher() const { return watcher_.get(); } | |
| 140 | |
| 141 private: | |
| 142 scoped_ptr<FilePathWatcher> watcher_; | |
| 143 MessageLoop* loop_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(Deleter); | |
| 146 }; | |
| 147 | |
| 148 void SetupWatchCallbackForTestDelegate(const FilePath& target, | |
|
kmadhusu
2012/11/28 20:19:44
SetupWatchCallbackForTestDelegate and SetupWatchCa
Lei Zhang
2012/11/28 23:27:19
Done.
| |
| 149 FilePathWatcher* watcher, | |
| 150 TestDelegate* delegate, | |
| 151 bool* result, | |
| 152 base::WaitableEvent* completion) { | |
| 153 *result = watcher->Watch(target, | |
| 154 base::Bind(&TestDelegate::OnFileChanged, | |
| 155 delegate->AsWeakPtr())); | |
| 156 completion->Signal(); | |
| 157 } | |
| 158 | |
| 159 void SetupWatchCallbackForDeleter(const FilePath& target, | |
| 160 FilePathWatcher* watcher, | |
| 161 Deleter* deleter, | |
| 162 bool* result, | |
| 163 base::WaitableEvent* completion) { | |
|
kmadhusu
2012/11/28 20:13:06
nit:
DCHECK(watcher)
DCHECK(deleter)
DCHECK(resul
Lei Zhang
2012/11/28 23:27:19
Given how straight-forward this 4 line function is
| |
| 164 *result = watcher->Watch(target, | |
| 165 base::Bind(&Deleter::OnFileChanged, | |
| 166 deleter->AsWeakPtr())); | |
| 129 completion->Signal(); | 167 completion->Signal(); |
| 130 } | 168 } |
| 131 | 169 |
| 132 void SetupWatchCallback(const FilePath& target, | 170 void SetupWatchCallback(const FilePath& target, |
| 133 FilePathWatcher* watcher, | 171 FilePathWatcher* watcher, |
| 134 const FilePathWatcher::Callback& callback) { | 172 const FilePathWatcher::Callback& callback) { |
| 135 ASSERT_TRUE(watcher->Watch(target, callback)); | 173 ASSERT_TRUE(watcher->Watch(target, callback)); |
| 136 } | 174 } |
| 137 | 175 |
| 138 void QuitLoopWatchCallback(MessageLoop* loop, | 176 void QuitLoopWatchCallback(MessageLoop* loop, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 161 base::Thread::Options options(MessageLoop::TYPE_IO, 0); | 199 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 162 ASSERT_TRUE(file_thread_.StartWithOptions(options)); | 200 ASSERT_TRUE(file_thread_.StartWithOptions(options)); |
| 163 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 201 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 164 collector_ = new NotificationCollector(); | 202 collector_ = new NotificationCollector(); |
| 165 } | 203 } |
| 166 | 204 |
| 167 virtual void TearDown() OVERRIDE { | 205 virtual void TearDown() OVERRIDE { |
| 168 loop_.RunUntilIdle(); | 206 loop_.RunUntilIdle(); |
| 169 } | 207 } |
| 170 | 208 |
| 209 void DeleteDelegateOnFileThread(TestDelegate* delegate) { | |
| 210 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, delegate); | |
| 211 } | |
| 212 | |
| 171 FilePath test_file() { | 213 FilePath test_file() { |
| 172 return temp_dir_.path().AppendASCII("FilePathWatcherTest"); | 214 return temp_dir_.path().AppendASCII("FilePathWatcherTest"); |
| 173 } | 215 } |
| 174 | 216 |
| 175 FilePath test_link() { | 217 FilePath test_link() { |
| 176 return temp_dir_.path().AppendASCII("FilePathWatcherTest.lnk"); | 218 return temp_dir_.path().AppendASCII("FilePathWatcherTest.lnk"); |
| 177 } | 219 } |
| 178 | 220 |
| 179 // Write |content| to |file|. Returns true on success. | 221 // Write |content| to |file|. Returns true on success. |
| 180 bool WriteFile(const FilePath& file, const std::string& content) { | 222 bool WriteFile(const FilePath& file, const std::string& content) { |
| 181 int write_size = file_util::WriteFile(file, content.c_str(), | 223 int write_size = file_util::WriteFile(file, content.c_str(), |
| 182 content.length()); | 224 content.length()); |
| 183 return write_size == static_cast<int>(content.length()); | 225 return write_size == static_cast<int>(content.length()); |
| 184 } | 226 } |
| 185 | 227 |
| 186 bool SetupWatch(const FilePath& target, | 228 bool SetupWatchForTestDelegate(const FilePath& target, |
| 187 FilePathWatcher* watcher, | 229 FilePathWatcher* watcher, |
| 188 FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { | 230 TestDelegate* delegate) WARN_UNUSED_RESULT { |
|
kmadhusu
2012/11/28 20:13:06
WARN_UNUSED_RESULT is not required any more.
Lei Zhang
2012/11/28 23:27:19
Why not? The caller should check and make sure Set
| |
| 189 base::WaitableEvent completion(false, false); | 231 base::WaitableEvent completion(false, false); |
| 190 bool result; | 232 bool result; |
| 191 file_thread_.message_loop_proxy()->PostTask( | 233 file_thread_.message_loop_proxy()->PostTask( |
| 192 FROM_HERE, | 234 FROM_HERE, |
| 193 base::Bind(SetupWatchDelegate, target, watcher, | 235 base::Bind(SetupWatchCallbackForTestDelegate, target, watcher, delegate, |
| 194 make_scoped_refptr(delegate), &result, &completion)); | 236 &result, &completion)); |
| 237 completion.Wait(); | |
| 238 return result; | |
| 239 } | |
| 240 | |
| 241 bool SetupWatchForDeleter(const FilePath& target, | |
| 242 FilePathWatcher* watcher, | |
| 243 Deleter* deleter) WARN_UNUSED_RESULT { | |
|
kmadhusu
2012/11/28 20:13:06
WARN_UNUSED_RESULT is not required any more.
| |
| 244 base::WaitableEvent completion(false, false); | |
| 245 bool result; | |
| 246 file_thread_.message_loop_proxy()->PostTask( | |
| 247 FROM_HERE, | |
| 248 base::Bind(SetupWatchCallbackForDeleter, target, watcher, deleter, | |
| 249 &result, &completion)); | |
| 195 completion.Wait(); | 250 completion.Wait(); |
| 196 return result; | 251 return result; |
| 197 } | 252 } |
| 198 | 253 |
| 199 bool WaitForEvents() WARN_UNUSED_RESULT { | 254 bool WaitForEvents() WARN_UNUSED_RESULT { |
| 200 collector_->Reset(); | 255 collector_->Reset(); |
| 201 loop_.Run(); | 256 loop_.Run(); |
| 202 return collector_->Success(); | 257 return collector_->Success(); |
| 203 } | 258 } |
| 204 | 259 |
| 205 NotificationCollector* collector() { return collector_.get(); } | 260 NotificationCollector* collector() { return collector_.get(); } |
| 206 | 261 |
| 207 MessageLoop loop_; | 262 MessageLoop loop_; |
| 208 base::Thread file_thread_; | 263 base::Thread file_thread_; |
| 209 ScopedTempDir temp_dir_; | 264 ScopedTempDir temp_dir_; |
| 210 scoped_refptr<NotificationCollector> collector_; | 265 scoped_refptr<NotificationCollector> collector_; |
| 266 | |
| 267 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherTest); | |
| 211 }; | 268 }; |
| 212 | 269 |
| 213 // Basic test: Create the file and verify that we notice. | 270 // Basic test: Create the file and verify that we notice. |
| 214 TEST_F(FilePathWatcherTest, NewFile) { | 271 TEST_F(FilePathWatcherTest, NewFile) { |
| 215 FilePathWatcher watcher; | 272 FilePathWatcher watcher; |
| 216 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 273 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 217 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 274 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), &watcher, delegate.get())); |
| 218 | 275 |
| 219 ASSERT_TRUE(WriteFile(test_file(), "content")); | 276 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 220 ASSERT_TRUE(WaitForEvents()); | 277 ASSERT_TRUE(WaitForEvents()); |
| 278 DeleteDelegateOnFileThread(delegate.release()); | |
| 221 } | 279 } |
| 222 | 280 |
| 223 // Verify that modifying the file is caught. | 281 // Verify that modifying the file is caught. |
| 224 TEST_F(FilePathWatcherTest, ModifiedFile) { | 282 TEST_F(FilePathWatcherTest, ModifiedFile) { |
| 225 ASSERT_TRUE(WriteFile(test_file(), "content")); | 283 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 226 | 284 |
| 227 FilePathWatcher watcher; | 285 FilePathWatcher watcher; |
| 228 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 286 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 229 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 287 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), &watcher, delegate.get())); |
| 230 | 288 |
| 231 // Now make sure we get notified if the file is modified. | 289 // Now make sure we get notified if the file is modified. |
| 232 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 290 ASSERT_TRUE(WriteFile(test_file(), kNewTestContent)); |
| 233 ASSERT_TRUE(WaitForEvents()); | 291 ASSERT_TRUE(WaitForEvents()); |
| 292 DeleteDelegateOnFileThread(delegate.release()); | |
| 234 } | 293 } |
| 235 | 294 |
| 236 // Verify that moving the file into place is caught. | 295 // Verify that moving the file into place is caught. |
| 237 TEST_F(FilePathWatcherTest, MovedFile) { | 296 TEST_F(FilePathWatcherTest, MovedFile) { |
| 238 FilePath source_file(temp_dir_.path().AppendASCII("source")); | 297 FilePath source_file(temp_dir_.path().AppendASCII("source")); |
| 239 ASSERT_TRUE(WriteFile(source_file, "content")); | 298 ASSERT_TRUE(WriteFile(source_file, kTestContent)); |
| 240 | 299 |
| 241 FilePathWatcher watcher; | 300 FilePathWatcher watcher; |
| 242 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 301 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 243 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 302 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), &watcher, delegate.get())); |
| 244 | 303 |
| 245 // Now make sure we get notified if the file is modified. | 304 // Now make sure we get notified if the file is modified. |
| 246 ASSERT_TRUE(file_util::Move(source_file, test_file())); | 305 ASSERT_TRUE(file_util::Move(source_file, test_file())); |
| 247 ASSERT_TRUE(WaitForEvents()); | 306 ASSERT_TRUE(WaitForEvents()); |
| 307 DeleteDelegateOnFileThread(delegate.release()); | |
| 248 } | 308 } |
| 249 | 309 |
| 250 TEST_F(FilePathWatcherTest, DeletedFile) { | 310 TEST_F(FilePathWatcherTest, DeletedFile) { |
| 251 ASSERT_TRUE(WriteFile(test_file(), "content")); | 311 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 252 | 312 |
| 253 FilePathWatcher watcher; | 313 FilePathWatcher watcher; |
| 254 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 314 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 255 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 315 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), &watcher, delegate.get())); |
| 256 | 316 |
| 257 // Now make sure we get notified if the file is deleted. | 317 // Now make sure we get notified if the file is deleted. |
| 258 file_util::Delete(test_file(), false); | 318 file_util::Delete(test_file(), false); |
| 259 ASSERT_TRUE(WaitForEvents()); | 319 ASSERT_TRUE(WaitForEvents()); |
| 320 DeleteDelegateOnFileThread(delegate.release()); | |
| 260 } | 321 } |
| 261 | 322 |
| 262 TEST_F(FilePathWatcherTest, Callback) { | 323 TEST_F(FilePathWatcherTest, Callback) { |
| 263 FilePathWatcher* watcher = new FilePathWatcher(); | 324 FilePathWatcher* watcher = new FilePathWatcher(); |
| 264 bool called_back = false; | 325 bool called_back = false; |
| 265 | 326 |
| 266 MessageLoop* file_loop = file_thread_.message_loop(); | 327 MessageLoop* file_loop = file_thread_.message_loop(); |
| 267 ASSERT_TRUE(file_loop); | 328 ASSERT_TRUE(file_loop); |
| 268 // The callback makes |loop_| quit on file events, and flips |called_back| | 329 // The callback makes |loop_| quit on file events, and flips |called_back| |
| 269 // to true. | 330 // to true. |
| 270 FilePathWatcher::Callback callback = base::Bind( | 331 FilePathWatcher::Callback callback = base::Bind( |
| 271 QuitLoopWatchCallback, &loop_, test_file(), false, &called_back); | 332 QuitLoopWatchCallback, &loop_, test_file(), false, &called_back); |
| 272 | 333 |
| 273 // Start watching on the file thread, and unblock the loop once the callback | 334 // Start watching on the file thread, and unblock the loop once the callback |
| 274 // has been installed. | 335 // has been installed. |
| 275 file_thread_.message_loop_proxy()->PostTaskAndReply( | 336 file_thread_.message_loop_proxy()->PostTaskAndReply( |
| 276 FROM_HERE, | 337 FROM_HERE, |
| 277 base::Bind(SetupWatchCallback, test_file(), watcher, callback), | 338 base::Bind(SetupWatchCallback, test_file(), watcher, callback), |
| 278 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_))); | 339 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_))); |
| 279 loop_.Run(); | 340 loop_.Run(); |
| 280 | 341 |
| 281 // The watch has been installed. Trigger a file event now, which will unblock | 342 // The watch has been installed. Trigger a file event now, which will unblock |
| 282 // the loop again. | 343 // the loop again. |
| 283 ASSERT_TRUE(WriteFile(test_file(), "content")); | 344 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 284 loop_.Run(); | 345 loop_.Run(); |
| 285 EXPECT_TRUE(called_back); | 346 EXPECT_TRUE(called_back); |
| 286 | 347 |
| 287 // Multiple events might have triggered, meaning that multiple | 348 // Multiple events might have triggered, meaning that multiple |
| 288 // QuitLoopWatchCallback have been posted. The FilePathWatcher can only cancel | 349 // QuitLoopWatchCallback have been posted. The FilePathWatcher can only cancel |
| 289 // on the FILE thread, and thus that callback might still trigger after this | 350 // on the FILE thread, and thus that callback might still trigger after this |
| 290 // function returns. Make sure the |watcher| is deleted before returning and | 351 // function returns. Make sure the |watcher| is deleted before returning and |
| 291 // destroying |called_back|. | 352 // destroying |called_back|. |
| 292 // A better fix requires significant changes to the FilePathWatcher. | 353 // A better fix requires significant changes to the FilePathWatcher. |
| 293 // TODO(joaodasilva): fix the FPW interface. http://crbug.com/145653 | 354 // TODO(joaodasilva): fix the FPW interface. http://crbug.com/145653 |
| 294 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); | 355 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); |
| 295 file_thread_.message_loop_proxy()->PostTaskAndReply( | 356 file_thread_.message_loop_proxy()->PostTaskAndReply( |
| 296 FROM_HERE, | 357 FROM_HERE, |
| 297 base::Bind(base::DoNothing), | 358 base::Bind(base::DoNothing), |
| 298 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_))); | 359 base::Bind(&MessageLoop::Quit, base::Unretained(&loop_))); |
| 299 loop_.Run(); | 360 loop_.Run(); |
| 300 } | 361 } |
| 301 | 362 |
| 302 // Used by the DeleteDuringNotify test below. | |
| 303 // Deletes the FilePathWatcher when it's notified. | |
| 304 class Deleter : public FilePathWatcher::Delegate { | |
| 305 public: | |
| 306 Deleter(FilePathWatcher* watcher, MessageLoop* loop) | |
| 307 : watcher_(watcher), | |
| 308 loop_(loop) { | |
| 309 } | |
| 310 | |
| 311 virtual void OnFilePathChanged(const FilePath& path) { | |
| 312 watcher_.reset(); | |
| 313 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 314 } | |
| 315 | |
| 316 scoped_ptr<FilePathWatcher> watcher_; | |
| 317 MessageLoop* loop_; | |
| 318 | |
| 319 private: | |
| 320 virtual ~Deleter() {} | |
| 321 }; | |
| 322 | |
| 323 // Verify that deleting a watcher during the callback doesn't crash. | 363 // Verify that deleting a watcher during the callback doesn't crash. |
| 324 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { | 364 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { |
| 325 FilePathWatcher* watcher = new FilePathWatcher; | 365 FilePathWatcher* watcher = new FilePathWatcher; |
| 326 // Takes ownership of watcher. | 366 // Takes ownership of watcher. |
| 327 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_)); | 367 scoped_ptr<Deleter> deleter(new Deleter(watcher, &loop_)); |
| 328 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get())); | 368 ASSERT_TRUE(SetupWatchForDeleter(test_file(), watcher, deleter.get())); |
| 329 | 369 |
| 330 ASSERT_TRUE(WriteFile(test_file(), "content")); | 370 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 331 ASSERT_TRUE(WaitForEvents()); | 371 ASSERT_TRUE(WaitForEvents()); |
| 332 | 372 |
| 333 // We win if we haven't crashed yet. | 373 // We win if we haven't crashed yet. |
| 334 // Might as well double-check it got deleted, too. | 374 // Might as well double-check it got deleted, too. |
| 335 ASSERT_TRUE(deleter->watcher_.get() == NULL); | 375 ASSERT_TRUE(deleter->watcher() == NULL); |
|
kmadhusu
2012/11/28 20:13:06
Do we need to Delete the |deleter| on file thread?
Lei Zhang
2012/11/28 23:27:19
No, once the watcher is gone, the callback that co
| |
| 336 } | 376 } |
| 337 | 377 |
| 338 // Verify that deleting the watcher works even if there is a pending | 378 // Verify that deleting the watcher works even if there is a pending |
| 339 // notification. | 379 // notification. |
| 340 // Flaky on MacOS. http://crbug.com/85930 | 380 // Flaky on MacOS. http://crbug.com/85930 |
| 341 #if defined(OS_MACOSX) | 381 #if defined(OS_MACOSX) |
| 342 #define MAYBE_DestroyWithPendingNotification DISABLED_DestroyWithPendingNotifica tion | 382 #define MAYBE_DestroyWithPendingNotification \ |
| 383 DISABLED_DestroyWithPendingNotification | |
| 343 #else | 384 #else |
| 344 #define MAYBE_DestroyWithPendingNotification DestroyWithPendingNotification | 385 #define MAYBE_DestroyWithPendingNotification DestroyWithPendingNotification |
| 345 #endif | 386 #endif |
| 346 TEST_F(FilePathWatcherTest, MAYBE_DestroyWithPendingNotification) { | 387 TEST_F(FilePathWatcherTest, MAYBE_DestroyWithPendingNotification) { |
| 347 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 388 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 348 FilePathWatcher* watcher = new FilePathWatcher; | 389 FilePathWatcher* watcher = new FilePathWatcher; |
| 349 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get())); | 390 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), watcher, delegate.get())); |
| 350 ASSERT_TRUE(WriteFile(test_file(), "content")); | 391 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 351 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); | 392 file_thread_.message_loop_proxy()->DeleteSoon(FROM_HERE, watcher); |
| 393 DeleteDelegateOnFileThread(delegate.release()); | |
| 352 } | 394 } |
| 353 | 395 |
| 354 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) { | 396 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) { |
| 355 FilePathWatcher watcher1, watcher2; | 397 FilePathWatcher watcher1, watcher2; |
| 356 scoped_refptr<TestDelegate> delegate1(new TestDelegate(collector())); | 398 scoped_ptr<TestDelegate> delegate1(new TestDelegate(collector())); |
| 357 scoped_refptr<TestDelegate> delegate2(new TestDelegate(collector())); | 399 scoped_ptr<TestDelegate> delegate2(new TestDelegate(collector())); |
| 358 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get())); | 400 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), &watcher1, |
| 359 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get())); | 401 delegate1.get())); |
| 402 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), &watcher2, | |
| 403 delegate2.get())); | |
| 360 | 404 |
| 361 ASSERT_TRUE(WriteFile(test_file(), "content")); | 405 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 362 ASSERT_TRUE(WaitForEvents()); | 406 ASSERT_TRUE(WaitForEvents()); |
| 407 DeleteDelegateOnFileThread(delegate1.release()); | |
| 408 DeleteDelegateOnFileThread(delegate2.release()); | |
| 363 } | 409 } |
| 364 | 410 |
| 365 // Verify that watching a file whose parent directory doesn't exist yet works if | 411 // Verify that watching a file whose parent directory doesn't exist yet works if |
| 366 // the directory and file are created eventually. | 412 // the directory and file are created eventually. |
| 367 TEST_F(FilePathWatcherTest, NonExistentDirectory) { | 413 TEST_F(FilePathWatcherTest, NonExistentDirectory) { |
| 368 FilePathWatcher watcher; | 414 FilePathWatcher watcher; |
| 369 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 415 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 370 FilePath file(dir.AppendASCII("file")); | 416 FilePath file(dir.AppendASCII("file")); |
| 371 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 417 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 372 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); | 418 ASSERT_TRUE(SetupWatchForTestDelegate(file, &watcher, delegate.get())); |
| 373 | 419 |
| 374 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 420 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 375 | 421 |
| 376 ASSERT_TRUE(WriteFile(file, "content")); | 422 ASSERT_TRUE(WriteFile(file, kTestContent)); |
| 377 | 423 |
| 378 VLOG(1) << "Waiting for file creation"; | 424 VLOG(1) << "Waiting for file creation"; |
| 379 ASSERT_TRUE(WaitForEvents()); | 425 ASSERT_TRUE(WaitForEvents()); |
| 380 | 426 |
| 381 ASSERT_TRUE(WriteFile(file, "content v2")); | 427 ASSERT_TRUE(WriteFile(file, "content v2")); |
|
kmadhusu
2012/11/28 20:13:06
nit: Can we use kNewContent instead of "content v2
Lei Zhang
2012/11/28 23:27:19
I reverted this cleanup, since it's also distracti
| |
| 382 VLOG(1) << "Waiting for file change"; | 428 VLOG(1) << "Waiting for file change"; |
| 383 ASSERT_TRUE(WaitForEvents()); | 429 ASSERT_TRUE(WaitForEvents()); |
| 384 | 430 |
| 385 ASSERT_TRUE(file_util::Delete(file, false)); | 431 ASSERT_TRUE(file_util::Delete(file, false)); |
| 386 VLOG(1) << "Waiting for file deletion"; | 432 VLOG(1) << "Waiting for file deletion"; |
| 387 ASSERT_TRUE(WaitForEvents()); | 433 ASSERT_TRUE(WaitForEvents()); |
| 434 DeleteDelegateOnFileThread(delegate.release()); | |
| 388 } | 435 } |
| 389 | 436 |
| 390 // Exercises watch reconfiguration for the case that directories on the path | 437 // Exercises watch reconfiguration for the case that directories on the path |
| 391 // are rapidly created. | 438 // are rapidly created. |
| 392 TEST_F(FilePathWatcherTest, DirectoryChain) { | 439 TEST_F(FilePathWatcherTest, DirectoryChain) { |
| 393 FilePath path(temp_dir_.path()); | 440 FilePath path(temp_dir_.path()); |
| 394 std::vector<std::string> dir_names; | 441 std::vector<std::string> dir_names; |
| 395 for (int i = 0; i < 20; i++) { | 442 for (int i = 0; i < 20; i++) { |
| 396 std::string dir(base::StringPrintf("d%d", i)); | 443 std::string dir(base::StringPrintf("d%d", i)); |
| 397 dir_names.push_back(dir); | 444 dir_names.push_back(dir); |
| 398 path = path.AppendASCII(dir); | 445 path = path.AppendASCII(dir); |
| 399 } | 446 } |
| 400 | 447 |
| 401 FilePathWatcher watcher; | 448 FilePathWatcher watcher; |
| 402 FilePath file(path.AppendASCII("file")); | 449 FilePath file(path.AppendASCII("file")); |
| 403 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 450 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 404 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); | 451 ASSERT_TRUE(SetupWatchForTestDelegate(file, &watcher, delegate.get())); |
| 405 | 452 |
| 406 FilePath sub_path(temp_dir_.path()); | 453 FilePath sub_path(temp_dir_.path()); |
| 407 for (std::vector<std::string>::const_iterator d(dir_names.begin()); | 454 for (std::vector<std::string>::const_iterator d(dir_names.begin()); |
| 408 d != dir_names.end(); ++d) { | 455 d != dir_names.end(); ++d) { |
| 409 sub_path = sub_path.AppendASCII(*d); | 456 sub_path = sub_path.AppendASCII(*d); |
| 410 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); | 457 ASSERT_TRUE(file_util::CreateDirectory(sub_path)); |
| 411 } | 458 } |
| 412 VLOG(1) << "Create File"; | 459 VLOG(1) << "Create File"; |
| 413 ASSERT_TRUE(WriteFile(file, "content")); | 460 ASSERT_TRUE(WriteFile(file, kTestContent)); |
| 414 VLOG(1) << "Waiting for file creation"; | 461 VLOG(1) << "Waiting for file creation"; |
| 415 ASSERT_TRUE(WaitForEvents()); | 462 ASSERT_TRUE(WaitForEvents()); |
| 416 | 463 |
| 417 ASSERT_TRUE(WriteFile(file, "content v2")); | 464 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 418 VLOG(1) << "Waiting for file modification"; | 465 VLOG(1) << "Waiting for file modification"; |
| 419 ASSERT_TRUE(WaitForEvents()); | 466 ASSERT_TRUE(WaitForEvents()); |
| 467 DeleteDelegateOnFileThread(delegate.release()); | |
| 420 } | 468 } |
| 421 | 469 |
| 422 #if defined(OS_MACOSX) | 470 #if defined(OS_MACOSX) |
| 423 // http://crbug.com/85930 | 471 // http://crbug.com/85930 |
| 424 #define DisappearingDirectory DISABLED_DisappearingDirectory | 472 #define DisappearingDirectory DISABLED_DisappearingDirectory |
| 425 #endif | 473 #endif |
| 426 TEST_F(FilePathWatcherTest, DisappearingDirectory) { | 474 TEST_F(FilePathWatcherTest, DisappearingDirectory) { |
| 427 FilePathWatcher watcher; | 475 FilePathWatcher watcher; |
| 428 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 476 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 429 FilePath file(dir.AppendASCII("file")); | 477 FilePath file(dir.AppendASCII("file")); |
| 430 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 478 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 431 ASSERT_TRUE(WriteFile(file, "content")); | 479 ASSERT_TRUE(WriteFile(file, kTestContent)); |
| 432 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 480 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 433 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get())); | 481 ASSERT_TRUE(SetupWatchForTestDelegate(file, &watcher, delegate.get())); |
| 434 | 482 |
| 435 ASSERT_TRUE(file_util::Delete(dir, true)); | 483 ASSERT_TRUE(file_util::Delete(dir, true)); |
| 436 ASSERT_TRUE(WaitForEvents()); | 484 ASSERT_TRUE(WaitForEvents()); |
| 485 DeleteDelegateOnFileThread(delegate.release()); | |
| 437 } | 486 } |
| 438 | 487 |
| 439 // Tests that a file that is deleted and reappears is tracked correctly. | 488 // Tests that a file that is deleted and reappears is tracked correctly. |
| 440 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { | 489 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { |
| 441 ASSERT_TRUE(WriteFile(test_file(), "content")); | 490 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 442 FilePathWatcher watcher; | 491 FilePathWatcher watcher; |
| 443 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 492 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 444 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 493 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), &watcher, delegate.get())); |
| 445 | 494 |
| 446 ASSERT_TRUE(file_util::Delete(test_file(), false)); | 495 ASSERT_TRUE(file_util::Delete(test_file(), false)); |
| 447 VLOG(1) << "Waiting for file deletion"; | 496 VLOG(1) << "Waiting for file deletion"; |
| 448 ASSERT_TRUE(WaitForEvents()); | 497 ASSERT_TRUE(WaitForEvents()); |
| 449 | 498 |
| 450 ASSERT_TRUE(WriteFile(test_file(), "content")); | 499 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 451 VLOG(1) << "Waiting for file creation"; | 500 VLOG(1) << "Waiting for file creation"; |
| 452 ASSERT_TRUE(WaitForEvents()); | 501 ASSERT_TRUE(WaitForEvents()); |
| 502 DeleteDelegateOnFileThread(delegate.release()); | |
| 453 } | 503 } |
| 454 | 504 |
| 455 TEST_F(FilePathWatcherTest, WatchDirectory) { | 505 TEST_F(FilePathWatcherTest, WatchDirectory) { |
| 456 FilePathWatcher watcher; | 506 FilePathWatcher watcher; |
| 457 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 507 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 458 FilePath file1(dir.AppendASCII("file1")); | 508 FilePath file1(dir.AppendASCII("file1")); |
| 459 FilePath file2(dir.AppendASCII("file2")); | 509 FilePath file2(dir.AppendASCII("file2")); |
| 460 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 510 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 461 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get())); | 511 ASSERT_TRUE(SetupWatchForTestDelegate(dir, &watcher, delegate.get())); |
| 462 | 512 |
| 463 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 513 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 464 VLOG(1) << "Waiting for directory creation"; | 514 VLOG(1) << "Waiting for directory creation"; |
| 465 ASSERT_TRUE(WaitForEvents()); | 515 ASSERT_TRUE(WaitForEvents()); |
| 466 | 516 |
| 467 ASSERT_TRUE(WriteFile(file1, "content")); | 517 ASSERT_TRUE(WriteFile(file1, kTestContent)); |
| 468 VLOG(1) << "Waiting for file1 creation"; | 518 VLOG(1) << "Waiting for file1 creation"; |
| 469 ASSERT_TRUE(WaitForEvents()); | 519 ASSERT_TRUE(WaitForEvents()); |
| 470 | 520 |
| 471 #if !defined(OS_MACOSX) | 521 #if !defined(OS_MACOSX) |
| 472 // Mac implementation does not detect files modified in a directory. | 522 // Mac implementation does not detect files modified in a directory. |
| 473 ASSERT_TRUE(WriteFile(file1, "content v2")); | 523 ASSERT_TRUE(WriteFile(file1, "content v2")); |
| 474 VLOG(1) << "Waiting for file1 modification"; | 524 VLOG(1) << "Waiting for file1 modification"; |
| 475 ASSERT_TRUE(WaitForEvents()); | 525 ASSERT_TRUE(WaitForEvents()); |
| 476 #endif // !OS_MACOSX | 526 #endif // !OS_MACOSX |
| 477 | 527 |
| 478 ASSERT_TRUE(file_util::Delete(file1, false)); | 528 ASSERT_TRUE(file_util::Delete(file1, false)); |
| 479 VLOG(1) << "Waiting for file1 deletion"; | 529 VLOG(1) << "Waiting for file1 deletion"; |
| 480 ASSERT_TRUE(WaitForEvents()); | 530 ASSERT_TRUE(WaitForEvents()); |
| 481 | 531 |
| 482 ASSERT_TRUE(WriteFile(file2, "content")); | 532 ASSERT_TRUE(WriteFile(file2, kTestContent)); |
| 483 VLOG(1) << "Waiting for file2 creation"; | 533 VLOG(1) << "Waiting for file2 creation"; |
| 484 ASSERT_TRUE(WaitForEvents()); | 534 ASSERT_TRUE(WaitForEvents()); |
| 535 DeleteDelegateOnFileThread(delegate.release()); | |
| 485 } | 536 } |
| 486 | 537 |
| 487 TEST_F(FilePathWatcherTest, MoveParent) { | 538 TEST_F(FilePathWatcherTest, MoveParent) { |
| 488 FilePathWatcher file_watcher; | 539 FilePathWatcher file_watcher; |
| 489 FilePathWatcher subdir_watcher; | 540 FilePathWatcher subdir_watcher; |
| 490 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 541 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 491 FilePath dest(temp_dir_.path().AppendASCII("dest")); | 542 FilePath dest(temp_dir_.path().AppendASCII("dest")); |
| 492 FilePath subdir(dir.AppendASCII("subdir")); | 543 FilePath subdir(dir.AppendASCII("subdir")); |
| 493 FilePath file(subdir.AppendASCII("file")); | 544 FilePath file(subdir.AppendASCII("file")); |
| 494 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 545 scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
| 495 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get())); | 546 ASSERT_TRUE(SetupWatchForTestDelegate(file, &file_watcher, |
| 496 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 547 file_delegate.get())); |
| 497 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get())); | 548 scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
| 549 ASSERT_TRUE(SetupWatchForTestDelegate(subdir, &subdir_watcher, | |
| 550 subdir_delegate.get())); | |
| 498 | 551 |
| 499 // Setup a directory hierarchy. | 552 // Setup a directory hierarchy. |
| 500 ASSERT_TRUE(file_util::CreateDirectory(subdir)); | 553 ASSERT_TRUE(file_util::CreateDirectory(subdir)); |
| 501 ASSERT_TRUE(WriteFile(file, "content")); | 554 ASSERT_TRUE(WriteFile(file, kTestContent)); |
| 502 VLOG(1) << "Waiting for file creation"; | 555 VLOG(1) << "Waiting for file creation"; |
| 503 ASSERT_TRUE(WaitForEvents()); | 556 ASSERT_TRUE(WaitForEvents()); |
| 504 | 557 |
| 505 // Move the parent directory. | 558 // Move the parent directory. |
| 506 file_util::Move(dir, dest); | 559 file_util::Move(dir, dest); |
| 507 VLOG(1) << "Waiting for directory move"; | 560 VLOG(1) << "Waiting for directory move"; |
| 508 ASSERT_TRUE(WaitForEvents()); | 561 ASSERT_TRUE(WaitForEvents()); |
| 562 DeleteDelegateOnFileThread(file_delegate.release()); | |
| 563 DeleteDelegateOnFileThread(subdir_delegate.release()); | |
| 509 } | 564 } |
| 510 | 565 |
| 511 TEST_F(FilePathWatcherTest, MoveChild) { | 566 TEST_F(FilePathWatcherTest, MoveChild) { |
| 512 FilePathWatcher file_watcher; | 567 FilePathWatcher file_watcher; |
| 513 FilePathWatcher subdir_watcher; | 568 FilePathWatcher subdir_watcher; |
| 514 FilePath source_dir(temp_dir_.path().AppendASCII("source")); | 569 FilePath source_dir(temp_dir_.path().AppendASCII("source")); |
| 515 FilePath source_subdir(source_dir.AppendASCII("subdir")); | 570 FilePath source_subdir(source_dir.AppendASCII("subdir")); |
| 516 FilePath source_file(source_subdir.AppendASCII("file")); | 571 FilePath source_file(source_subdir.AppendASCII("file")); |
| 517 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); | 572 FilePath dest_dir(temp_dir_.path().AppendASCII("dest")); |
| 518 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); | 573 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); |
| 519 FilePath dest_file(dest_subdir.AppendASCII("file")); | 574 FilePath dest_file(dest_subdir.AppendASCII("file")); |
| 520 | 575 |
| 521 // Setup a directory hierarchy. | 576 // Setup a directory hierarchy. |
| 522 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); | 577 ASSERT_TRUE(file_util::CreateDirectory(source_subdir)); |
| 523 ASSERT_TRUE(WriteFile(source_file, "content")); | 578 ASSERT_TRUE(WriteFile(source_file, kTestContent)); |
| 524 | 579 |
| 525 scoped_refptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 580 scoped_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
| 526 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get())); | 581 ASSERT_TRUE(SetupWatchForTestDelegate(dest_file, &file_watcher, |
| 527 scoped_refptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 582 file_delegate.get())); |
| 528 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get())); | 583 scoped_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
| 584 ASSERT_TRUE(SetupWatchForTestDelegate(dest_subdir, &subdir_watcher, | |
| 585 subdir_delegate.get())); | |
| 529 | 586 |
| 530 // Move the directory into place, s.t. the watched file appears. | 587 // Move the directory into place, s.t. the watched file appears. |
| 531 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); | 588 ASSERT_TRUE(file_util::Move(source_dir, dest_dir)); |
| 532 ASSERT_TRUE(WaitForEvents()); | 589 ASSERT_TRUE(WaitForEvents()); |
| 590 DeleteDelegateOnFileThread(file_delegate.release()); | |
| 591 DeleteDelegateOnFileThread(subdir_delegate.release()); | |
| 533 } | 592 } |
| 534 | 593 |
| 535 #if !defined(OS_LINUX) | 594 #if !defined(OS_LINUX) |
| 536 // Linux implementation of FilePathWatcher doesn't catch attribute changes. | 595 // Linux implementation of FilePathWatcher doesn't catch attribute changes. |
| 537 // http://crbug.com/78043 | 596 // http://crbug.com/78043 |
| 538 | 597 |
| 539 // Verify that changing attributes on a file is caught | 598 // Verify that changing attributes on a file is caught |
| 540 TEST_F(FilePathWatcherTest, FileAttributesChanged) { | 599 TEST_F(FilePathWatcherTest, FileAttributesChanged) { |
| 541 ASSERT_TRUE(WriteFile(test_file(), "content")); | 600 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 542 FilePathWatcher watcher; | 601 FilePathWatcher watcher; |
| 543 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 602 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 544 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get())); | 603 ASSERT_TRUE(SetupWatchForTestDelegate(test_file(), &watcher, delegate.get())); |
| 545 | 604 |
| 546 // Now make sure we get notified if the file is modified. | 605 // Now make sure we get notified if the file is modified. |
| 547 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); | 606 ASSERT_TRUE(file_util::MakeFileUnreadable(test_file())); |
| 548 ASSERT_TRUE(WaitForEvents()); | 607 ASSERT_TRUE(WaitForEvents()); |
| 608 DeleteDelegateOnFileThread(delegate.release()); | |
| 549 } | 609 } |
| 550 | 610 |
| 551 #endif // !OS_LINUX | 611 #endif // !OS_LINUX |
| 552 | 612 |
| 553 #if defined(OS_LINUX) | 613 #if defined(OS_LINUX) |
| 554 | 614 |
| 555 // Verify that creating a symlink is caught. | 615 // Verify that creating a symlink is caught. |
| 556 TEST_F(FilePathWatcherTest, CreateLink) { | 616 TEST_F(FilePathWatcherTest, CreateLink) { |
| 557 FilePathWatcher watcher; | 617 FilePathWatcher watcher; |
| 558 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 618 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 559 // Note that we are watching the symlink | 619 // Note that we are watching the symlink |
| 560 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 620 ASSERT_TRUE(SetupWatchForTestDelegate(test_link(), &watcher, delegate.get())); |
| 561 | 621 |
| 562 // Now make sure we get notified if the link is created. | 622 // Now make sure we get notified if the link is created. |
| 563 // Note that test_file() doesn't have to exist. | 623 // Note that test_file() doesn't have to exist. |
| 564 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 624 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 565 ASSERT_TRUE(WaitForEvents()); | 625 ASSERT_TRUE(WaitForEvents()); |
| 626 DeleteDelegateOnFileThread(delegate.release()); | |
| 566 } | 627 } |
| 567 | 628 |
| 568 // Verify that deleting a symlink is caught. | 629 // Verify that deleting a symlink is caught. |
| 569 TEST_F(FilePathWatcherTest, DeleteLink) { | 630 TEST_F(FilePathWatcherTest, DeleteLink) { |
| 570 // Unfortunately this test case only works if the link target exists. | 631 // Unfortunately this test case only works if the link target exists. |
| 571 // TODO(craig) fix this as part of crbug.com/91561. | 632 // TODO(craig) fix this as part of crbug.com/91561. |
| 572 ASSERT_TRUE(WriteFile(test_file(), "content")); | 633 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 573 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 634 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 574 FilePathWatcher watcher; | 635 FilePathWatcher watcher; |
| 575 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 636 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 576 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 637 ASSERT_TRUE(SetupWatchForTestDelegate(test_link(), &watcher, delegate.get())); |
| 577 | 638 |
| 578 // Now make sure we get notified if the link is deleted. | 639 // Now make sure we get notified if the link is deleted. |
| 579 ASSERT_TRUE(file_util::Delete(test_link(), false)); | 640 ASSERT_TRUE(file_util::Delete(test_link(), false)); |
| 580 ASSERT_TRUE(WaitForEvents()); | 641 ASSERT_TRUE(WaitForEvents()); |
| 642 DeleteDelegateOnFileThread(delegate.release()); | |
| 581 } | 643 } |
| 582 | 644 |
| 583 // Verify that modifying a target file that a link is pointing to | 645 // Verify that modifying a target file that a link is pointing to |
| 584 // when we are watching the link is caught. | 646 // when we are watching the link is caught. |
| 585 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { | 647 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { |
| 586 ASSERT_TRUE(WriteFile(test_file(), "content")); | 648 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 587 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 649 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 588 FilePathWatcher watcher; | 650 FilePathWatcher watcher; |
| 589 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 651 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 590 // Note that we are watching the symlink. | 652 // Note that we are watching the symlink. |
| 591 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 653 ASSERT_TRUE(SetupWatchForTestDelegate(test_link(), &watcher, delegate.get())); |
| 592 | 654 |
| 593 // Now make sure we get notified if the file is modified. | 655 // Now make sure we get notified if the file is modified. |
| 594 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 656 ASSERT_TRUE(WriteFile(test_file(), kNewTestContent)); |
| 595 ASSERT_TRUE(WaitForEvents()); | 657 ASSERT_TRUE(WaitForEvents()); |
| 658 DeleteDelegateOnFileThread(delegate.release()); | |
| 596 } | 659 } |
| 597 | 660 |
| 598 // Verify that creating a target file that a link is pointing to | 661 // Verify that creating a target file that a link is pointing to |
| 599 // when we are watching the link is caught. | 662 // when we are watching the link is caught. |
| 600 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { | 663 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { |
| 601 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 664 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 602 FilePathWatcher watcher; | 665 FilePathWatcher watcher; |
| 603 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 666 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 604 // Note that we are watching the symlink. | 667 // Note that we are watching the symlink. |
| 605 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 668 ASSERT_TRUE(SetupWatchForTestDelegate(test_link(), &watcher, delegate.get())); |
| 606 | 669 |
| 607 // Now make sure we get notified if the target file is created. | 670 // Now make sure we get notified if the target file is created. |
| 608 ASSERT_TRUE(WriteFile(test_file(), "content")); | 671 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 609 ASSERT_TRUE(WaitForEvents()); | 672 ASSERT_TRUE(WaitForEvents()); |
| 673 DeleteDelegateOnFileThread(delegate.release()); | |
| 610 } | 674 } |
| 611 | 675 |
| 612 // Verify that deleting a target file that a link is pointing to | 676 // Verify that deleting a target file that a link is pointing to |
| 613 // when we are watching the link is caught. | 677 // when we are watching the link is caught. |
| 614 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { | 678 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { |
| 615 ASSERT_TRUE(WriteFile(test_file(), "content")); | 679 ASSERT_TRUE(WriteFile(test_file(), kTestContent)); |
| 616 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); | 680 ASSERT_TRUE(file_util::CreateSymbolicLink(test_file(), test_link())); |
| 617 FilePathWatcher watcher; | 681 FilePathWatcher watcher; |
| 618 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 682 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 619 // Note that we are watching the symlink. | 683 // Note that we are watching the symlink. |
| 620 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get())); | 684 ASSERT_TRUE(SetupWatchForTestDelegate(test_link(), &watcher, delegate.get())); |
| 621 | 685 |
| 622 // Now make sure we get notified if the target file is deleted. | 686 // Now make sure we get notified if the target file is deleted. |
| 623 ASSERT_TRUE(file_util::Delete(test_file(), false)); | 687 ASSERT_TRUE(file_util::Delete(test_file(), false)); |
| 624 ASSERT_TRUE(WaitForEvents()); | 688 ASSERT_TRUE(WaitForEvents()); |
| 689 DeleteDelegateOnFileThread(delegate.release()); | |
| 625 } | 690 } |
| 626 | 691 |
| 627 // Verify that watching a file whose parent directory is a link that | 692 // Verify that watching a file whose parent directory is a link that |
| 628 // doesn't exist yet works if the symlink is created eventually. | 693 // doesn't exist yet works if the symlink is created eventually. |
| 629 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { | 694 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { |
| 630 FilePathWatcher watcher; | 695 FilePathWatcher watcher; |
| 631 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 696 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 632 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 697 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
| 633 FilePath file(dir.AppendASCII("file")); | 698 FilePath file(dir.AppendASCII("file")); |
| 634 FilePath linkfile(link_dir.AppendASCII("file")); | 699 FilePath linkfile(link_dir.AppendASCII("file")); |
| 635 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 700 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 636 // dir/file should exist. | 701 // dir/file should exist. |
| 637 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 702 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 638 ASSERT_TRUE(WriteFile(file, "content")); | 703 ASSERT_TRUE(WriteFile(file, kTestContent)); |
| 639 // Note that we are watching dir.lnk/file which doesn't exist yet. | 704 // Note that we are watching dir.lnk/file which doesn't exist yet. |
| 640 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); | 705 ASSERT_TRUE(SetupWatchForTestDelegate(linkfile, &watcher, delegate.get())); |
| 641 | 706 |
| 642 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 707 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); |
| 643 VLOG(1) << "Waiting for link creation"; | 708 VLOG(1) << "Waiting for link creation"; |
| 644 ASSERT_TRUE(WaitForEvents()); | 709 ASSERT_TRUE(WaitForEvents()); |
| 645 | 710 |
| 646 ASSERT_TRUE(WriteFile(file, "content v2")); | 711 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 647 VLOG(1) << "Waiting for file change"; | 712 VLOG(1) << "Waiting for file change"; |
| 648 ASSERT_TRUE(WaitForEvents()); | 713 ASSERT_TRUE(WaitForEvents()); |
| 649 | 714 |
| 650 ASSERT_TRUE(file_util::Delete(file, false)); | 715 ASSERT_TRUE(file_util::Delete(file, false)); |
| 651 VLOG(1) << "Waiting for file deletion"; | 716 VLOG(1) << "Waiting for file deletion"; |
| 652 ASSERT_TRUE(WaitForEvents()); | 717 ASSERT_TRUE(WaitForEvents()); |
| 718 DeleteDelegateOnFileThread(delegate.release()); | |
| 653 } | 719 } |
| 654 | 720 |
| 655 // Verify that watching a file whose parent directory is a | 721 // Verify that watching a file whose parent directory is a |
| 656 // dangling symlink works if the directory is created eventually. | 722 // dangling symlink works if the directory is created eventually. |
| 657 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { | 723 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { |
| 658 FilePathWatcher watcher; | 724 FilePathWatcher watcher; |
| 659 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 725 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 660 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 726 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
| 661 FilePath file(dir.AppendASCII("file")); | 727 FilePath file(dir.AppendASCII("file")); |
| 662 FilePath linkfile(link_dir.AppendASCII("file")); | 728 FilePath linkfile(link_dir.AppendASCII("file")); |
| 663 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 729 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 664 // Now create the link from dir.lnk pointing to dir but | 730 // Now create the link from dir.lnk pointing to dir but |
| 665 // neither dir nor dir/file exist yet. | 731 // neither dir nor dir/file exist yet. |
| 666 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 732 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); |
| 667 // Note that we are watching dir.lnk/file. | 733 // Note that we are watching dir.lnk/file. |
| 668 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); | 734 ASSERT_TRUE(SetupWatchForTestDelegate(linkfile, &watcher, delegate.get())); |
| 669 | 735 |
| 670 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 736 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 671 ASSERT_TRUE(WriteFile(file, "content")); | 737 ASSERT_TRUE(WriteFile(file, kTestContent)); |
| 672 VLOG(1) << "Waiting for dir/file creation"; | 738 VLOG(1) << "Waiting for dir/file creation"; |
| 673 ASSERT_TRUE(WaitForEvents()); | 739 ASSERT_TRUE(WaitForEvents()); |
| 674 | 740 |
| 675 ASSERT_TRUE(WriteFile(file, "content v2")); | 741 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 676 VLOG(1) << "Waiting for file change"; | 742 VLOG(1) << "Waiting for file change"; |
| 677 ASSERT_TRUE(WaitForEvents()); | 743 ASSERT_TRUE(WaitForEvents()); |
| 678 | 744 |
| 679 ASSERT_TRUE(file_util::Delete(file, false)); | 745 ASSERT_TRUE(file_util::Delete(file, false)); |
| 680 VLOG(1) << "Waiting for file deletion"; | 746 VLOG(1) << "Waiting for file deletion"; |
| 681 ASSERT_TRUE(WaitForEvents()); | 747 ASSERT_TRUE(WaitForEvents()); |
| 748 DeleteDelegateOnFileThread(delegate.release()); | |
| 682 } | 749 } |
| 683 | 750 |
| 684 // Verify that watching a file with a symlink on the path | 751 // Verify that watching a file with a symlink on the path |
| 685 // to the file works. | 752 // to the file works. |
| 686 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { | 753 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { |
| 687 FilePathWatcher watcher; | 754 FilePathWatcher watcher; |
| 688 FilePath dir(temp_dir_.path().AppendASCII("dir")); | 755 FilePath dir(temp_dir_.path().AppendASCII("dir")); |
| 689 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); | 756 FilePath link_dir(temp_dir_.path().AppendASCII("dir.lnk")); |
| 690 FilePath file(dir.AppendASCII("file")); | 757 FilePath file(dir.AppendASCII("file")); |
| 691 FilePath linkfile(link_dir.AppendASCII("file")); | 758 FilePath linkfile(link_dir.AppendASCII("file")); |
| 692 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 759 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 693 ASSERT_TRUE(file_util::CreateDirectory(dir)); | 760 ASSERT_TRUE(file_util::CreateDirectory(dir)); |
| 694 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); | 761 ASSERT_TRUE(file_util::CreateSymbolicLink(dir, link_dir)); |
| 695 // Note that we are watching dir.lnk/file but the file doesn't exist yet. | 762 // Note that we are watching dir.lnk/file but the file doesn't exist yet. |
| 696 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get())); | 763 ASSERT_TRUE(SetupWatchForTestDelegate(linkfile, &watcher, delegate.get())); |
| 697 | 764 |
| 698 ASSERT_TRUE(WriteFile(file, "content")); | 765 ASSERT_TRUE(WriteFile(file, kTestContent)); |
| 699 VLOG(1) << "Waiting for file creation"; | 766 VLOG(1) << "Waiting for file creation"; |
| 700 ASSERT_TRUE(WaitForEvents()); | 767 ASSERT_TRUE(WaitForEvents()); |
| 701 | 768 |
| 702 ASSERT_TRUE(WriteFile(file, "content v2")); | 769 ASSERT_TRUE(WriteFile(file, "content v2")); |
| 703 VLOG(1) << "Waiting for file change"; | 770 VLOG(1) << "Waiting for file change"; |
| 704 ASSERT_TRUE(WaitForEvents()); | 771 ASSERT_TRUE(WaitForEvents()); |
| 705 | 772 |
| 706 ASSERT_TRUE(file_util::Delete(file, false)); | 773 ASSERT_TRUE(file_util::Delete(file, false)); |
| 707 VLOG(1) << "Waiting for file deletion"; | 774 VLOG(1) << "Waiting for file deletion"; |
| 708 ASSERT_TRUE(WaitForEvents()); | 775 ASSERT_TRUE(WaitForEvents()); |
| 776 DeleteDelegateOnFileThread(delegate.release()); | |
| 709 } | 777 } |
| 710 | 778 |
| 711 #endif // OS_LINUX | 779 #endif // OS_LINUX |
| 712 | 780 |
| 713 enum Permission { | 781 enum Permission { |
| 714 Read, | 782 Read, |
| 715 Write, | 783 Write, |
| 716 Execute | 784 Execute |
| 717 }; | 785 }; |
| 718 | 786 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 808 // http://crbug.com/78045 | 876 // http://crbug.com/78045 |
| 809 | 877 |
| 810 // Verify that changing attributes on a directory works. | 878 // Verify that changing attributes on a directory works. |
| 811 TEST_F(FilePathWatcherTest, DirAttributesChanged) { | 879 TEST_F(FilePathWatcherTest, DirAttributesChanged) { |
| 812 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1")); | 880 FilePath test_dir1(temp_dir_.path().AppendASCII("DirAttributesChangedDir1")); |
| 813 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); | 881 FilePath test_dir2(test_dir1.AppendASCII("DirAttributesChangedDir2")); |
| 814 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); | 882 FilePath test_file(test_dir2.AppendASCII("DirAttributesChangedFile")); |
| 815 // Setup a directory hierarchy. | 883 // Setup a directory hierarchy. |
| 816 ASSERT_TRUE(file_util::CreateDirectory(test_dir1)); | 884 ASSERT_TRUE(file_util::CreateDirectory(test_dir1)); |
| 817 ASSERT_TRUE(file_util::CreateDirectory(test_dir2)); | 885 ASSERT_TRUE(file_util::CreateDirectory(test_dir2)); |
| 818 ASSERT_TRUE(WriteFile(test_file, "content")); | 886 ASSERT_TRUE(WriteFile(test_file, kTestContent)); |
| 819 | 887 |
| 820 FilePathWatcher watcher; | 888 FilePathWatcher watcher; |
| 821 scoped_refptr<TestDelegate> delegate(new TestDelegate(collector())); | 889 scoped_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
| 822 ASSERT_TRUE(SetupWatch(test_file, &watcher, delegate.get())); | 890 ASSERT_TRUE(SetupWatchForTestDelegate(test_file, &watcher, delegate.get())); |
| 823 | 891 |
| 824 // We should not get notified in this case as it hasn't affected our ability | 892 // We should not get notified in this case as it hasn't affected our ability |
| 825 // to access the file. | 893 // to access the file. |
| 826 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false)); | 894 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, false)); |
| 827 loop_.PostDelayedTask(FROM_HERE, | 895 loop_.PostDelayedTask(FROM_HERE, |
| 828 MessageLoop::QuitClosure(), | 896 MessageLoop::QuitClosure(), |
| 829 TestTimeouts::tiny_timeout()); | 897 TestTimeouts::tiny_timeout()); |
| 830 ASSERT_FALSE(WaitForEvents()); | 898 ASSERT_FALSE(WaitForEvents()); |
| 831 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); | 899 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); |
| 832 | 900 |
| 833 // We should get notified in this case because filepathwatcher can no | 901 // We should get notified in this case because filepathwatcher can no |
| 834 // longer access the file | 902 // longer access the file |
| 835 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 903 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
| 836 ASSERT_TRUE(WaitForEvents()); | 904 ASSERT_TRUE(WaitForEvents()); |
| 837 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 905 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
| 906 DeleteDelegateOnFileThread(delegate.release()); | |
| 838 } | 907 } |
| 839 | 908 |
| 840 #endif // OS_MACOSX | 909 #endif // OS_MACOSX |
| 841 } // namespace | 910 } // namespace |
| 842 | 911 |
| 843 } // namespace files | 912 } // namespace files |
| 844 } // namespace base | 913 } // namespace base |
| OLD | NEW |