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 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <aclapi.h> | 9 #include <aclapi.h> |
10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "base/files/scoped_temp_dir.h" | 21 #include "base/files/scoped_temp_dir.h" |
22 #include "base/location.h" | 22 #include "base/location.h" |
23 #include "base/macros.h" | 23 #include "base/macros.h" |
24 #include "base/run_loop.h" | 24 #include "base/run_loop.h" |
25 #include "base/single_thread_task_runner.h" | 25 #include "base/single_thread_task_runner.h" |
26 #include "base/stl_util.h" | 26 #include "base/stl_util.h" |
27 #include "base/strings/stringprintf.h" | 27 #include "base/strings/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_task_runner_handle.h" | 32 #include "base/threading/thread_task_runner_handle.h" |
32 #include "build/build_config.h" | 33 #include "build/build_config.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
34 | 35 |
35 #if defined(OS_ANDROID) | 36 #if defined(OS_ANDROID) |
36 #include "base/android/path_utils.h" | 37 #include "base/android/path_utils.h" |
37 #endif // defined(OS_ANDROID) | 38 #endif // defined(OS_ANDROID) |
38 | 39 |
39 #if defined(OS_POSIX) | |
40 #include "base/files/file_descriptor_watcher_posix.h" | |
41 #endif // defined(OS_POSIX) | |
42 | |
43 namespace base { | 40 namespace base { |
44 | 41 |
45 namespace { | 42 namespace { |
46 | 43 |
47 class TestDelegate; | 44 class TestDelegate; |
48 | 45 |
49 // Aggregates notifications from the test delegates and breaks the message loop | 46 // Aggregates notifications from the test delegates and breaks the message loop |
50 // the test thread is waiting on once they all came in. | 47 // the test thread is waiting on once they all came in. |
51 class NotificationCollector | 48 class NotificationCollector |
52 : public base::RefCountedThreadSafe<NotificationCollector> { | 49 : public base::RefCountedThreadSafe<NotificationCollector> { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 else | 124 else |
128 collector_->OnChange(this); | 125 collector_->OnChange(this); |
129 } | 126 } |
130 | 127 |
131 private: | 128 private: |
132 scoped_refptr<NotificationCollector> collector_; | 129 scoped_refptr<NotificationCollector> collector_; |
133 | 130 |
134 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 131 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
135 }; | 132 }; |
136 | 133 |
| 134 void SetupWatchCallback(const FilePath& target, |
| 135 FilePathWatcher* watcher, |
| 136 TestDelegateBase* delegate, |
| 137 bool recursive_watch, |
| 138 bool* result, |
| 139 base::WaitableEvent* completion) { |
| 140 *result = watcher->Watch(target, recursive_watch, |
| 141 base::Bind(&TestDelegateBase::OnFileChanged, |
| 142 delegate->AsWeakPtr())); |
| 143 completion->Signal(); |
| 144 } |
| 145 |
137 class FilePathWatcherTest : public testing::Test { | 146 class FilePathWatcherTest : public testing::Test { |
138 public: | 147 public: |
139 FilePathWatcherTest() | 148 FilePathWatcherTest() |
140 #if defined(OS_POSIX) | 149 : file_thread_("FilePathWatcherTest") {} |
141 : file_descriptor_watcher_(&loop_) | |
142 #endif | |
143 { | |
144 } | |
145 | 150 |
146 ~FilePathWatcherTest() override {} | 151 ~FilePathWatcherTest() override {} |
147 | 152 |
148 protected: | 153 protected: |
149 void SetUp() override { | 154 void SetUp() override { |
| 155 // Create a separate file thread in order to test proper thread usage. |
| 156 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
| 157 ASSERT_TRUE(file_thread_.StartWithOptions(options)); |
150 #if defined(OS_ANDROID) | 158 #if defined(OS_ANDROID) |
151 // Watching files is only permitted when all parent directories are | 159 // Watching files is only permitted when all parent directories are |
152 // accessible, which is not the case for the default temp directory | 160 // accessible, which is not the case for the default temp directory |
153 // on Android which is under /data/data. Use /sdcard instead. | 161 // on Android which is under /data/data. Use /sdcard instead. |
154 // TODO(pauljensen): Remove this when crbug.com/475568 is fixed. | 162 // TODO(pauljensen): Remove this when crbug.com/475568 is fixed. |
155 FilePath parent_dir; | 163 FilePath parent_dir; |
156 ASSERT_TRUE(android::GetExternalStorageDirectory(&parent_dir)); | 164 ASSERT_TRUE(android::GetExternalStorageDirectory(&parent_dir)); |
157 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(parent_dir)); | 165 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(parent_dir)); |
158 #else // defined(OS_ANDROID) | 166 #else // defined(OS_ANDROID) |
159 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 167 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
160 #endif // defined(OS_ANDROID) | 168 #endif // defined(OS_ANDROID) |
161 collector_ = new NotificationCollector(); | 169 collector_ = new NotificationCollector(); |
162 } | 170 } |
163 | 171 |
164 void TearDown() override { RunLoop().RunUntilIdle(); } | 172 void TearDown() override { RunLoop().RunUntilIdle(); } |
165 | 173 |
| 174 void DeleteDelegateOnFileThread(TestDelegate* delegate) { |
| 175 file_thread_.task_runner()->DeleteSoon(FROM_HERE, delegate); |
| 176 } |
| 177 |
166 FilePath test_file() { | 178 FilePath test_file() { |
167 return temp_dir_.GetPath().AppendASCII("FilePathWatcherTest"); | 179 return temp_dir_.GetPath().AppendASCII("FilePathWatcherTest"); |
168 } | 180 } |
169 | 181 |
170 FilePath test_link() { | 182 FilePath test_link() { |
171 return temp_dir_.GetPath().AppendASCII("FilePathWatcherTest.lnk"); | 183 return temp_dir_.GetPath().AppendASCII("FilePathWatcherTest.lnk"); |
172 } | 184 } |
173 | 185 |
174 // Write |content| to |file|. Returns true on success. | 186 // Write |content| to |file|. Returns true on success. |
175 bool WriteFile(const FilePath& file, const std::string& content) { | 187 bool WriteFile(const FilePath& file, const std::string& content) { |
176 int write_size = ::base::WriteFile(file, content.c_str(), content.length()); | 188 int write_size = ::base::WriteFile(file, content.c_str(), content.length()); |
177 return write_size == static_cast<int>(content.length()); | 189 return write_size == static_cast<int>(content.length()); |
178 } | 190 } |
179 | 191 |
180 bool SetupWatch(const FilePath& target, | 192 bool SetupWatch(const FilePath& target, |
181 FilePathWatcher* watcher, | 193 FilePathWatcher* watcher, |
182 TestDelegateBase* delegate, | 194 TestDelegateBase* delegate, |
183 bool recursive_watch) WARN_UNUSED_RESULT; | 195 bool recursive_watch) WARN_UNUSED_RESULT; |
184 | 196 |
185 bool WaitForEvents() WARN_UNUSED_RESULT { | 197 bool WaitForEvents() WARN_UNUSED_RESULT { |
186 collector_->Reset(); | 198 collector_->Reset(); |
187 | |
188 RunLoop run_loop; | |
189 // Make sure we timeout if we don't get notified. | 199 // Make sure we timeout if we don't get notified. |
190 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 200 loop_.task_runner()->PostDelayedTask(FROM_HERE, |
191 FROM_HERE, run_loop.QuitWhenIdleClosure(), | 201 MessageLoop::QuitWhenIdleClosure(), |
192 TestTimeouts::action_timeout()); | 202 TestTimeouts::action_timeout()); |
193 run_loop.Run(); | 203 RunLoop().Run(); |
194 return collector_->Success(); | 204 return collector_->Success(); |
195 } | 205 } |
196 | 206 |
197 NotificationCollector* collector() { return collector_.get(); } | 207 NotificationCollector* collector() { return collector_.get(); } |
198 | 208 |
199 MessageLoopForIO loop_; | 209 MessageLoop loop_; |
200 #if defined(OS_POSIX) | 210 base::Thread file_thread_; |
201 FileDescriptorWatcher file_descriptor_watcher_; | |
202 #endif | |
203 | |
204 ScopedTempDir temp_dir_; | 211 ScopedTempDir temp_dir_; |
205 scoped_refptr<NotificationCollector> collector_; | 212 scoped_refptr<NotificationCollector> collector_; |
206 | 213 |
207 private: | 214 private: |
208 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherTest); | 215 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherTest); |
209 }; | 216 }; |
210 | 217 |
211 bool FilePathWatcherTest::SetupWatch(const FilePath& target, | 218 bool FilePathWatcherTest::SetupWatch(const FilePath& target, |
212 FilePathWatcher* watcher, | 219 FilePathWatcher* watcher, |
213 TestDelegateBase* delegate, | 220 TestDelegateBase* delegate, |
214 bool recursive_watch) { | 221 bool recursive_watch) { |
215 return watcher->Watch( | 222 base::WaitableEvent completion(WaitableEvent::ResetPolicy::AUTOMATIC, |
216 target, recursive_watch, | 223 WaitableEvent::InitialState::NOT_SIGNALED); |
217 base::Bind(&TestDelegateBase::OnFileChanged, delegate->AsWeakPtr())); | 224 bool result; |
| 225 file_thread_.task_runner()->PostTask( |
| 226 FROM_HERE, base::Bind(SetupWatchCallback, target, watcher, delegate, |
| 227 recursive_watch, &result, &completion)); |
| 228 completion.Wait(); |
| 229 return result; |
218 } | 230 } |
219 | 231 |
220 // Basic test: Create the file and verify that we notice. | 232 // Basic test: Create the file and verify that we notice. |
221 TEST_F(FilePathWatcherTest, NewFile) { | 233 TEST_F(FilePathWatcherTest, NewFile) { |
222 FilePathWatcher watcher; | 234 FilePathWatcher watcher; |
223 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 235 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
224 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 236 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
225 | 237 |
226 ASSERT_TRUE(WriteFile(test_file(), "content")); | 238 ASSERT_TRUE(WriteFile(test_file(), "content")); |
227 ASSERT_TRUE(WaitForEvents()); | 239 ASSERT_TRUE(WaitForEvents()); |
| 240 DeleteDelegateOnFileThread(delegate.release()); |
228 } | 241 } |
229 | 242 |
230 // Verify that modifying the file is caught. | 243 // Verify that modifying the file is caught. |
231 TEST_F(FilePathWatcherTest, ModifiedFile) { | 244 TEST_F(FilePathWatcherTest, ModifiedFile) { |
232 ASSERT_TRUE(WriteFile(test_file(), "content")); | 245 ASSERT_TRUE(WriteFile(test_file(), "content")); |
233 | 246 |
234 FilePathWatcher watcher; | 247 FilePathWatcher watcher; |
235 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 248 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
236 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 249 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
237 | 250 |
238 // Now make sure we get notified if the file is modified. | 251 // Now make sure we get notified if the file is modified. |
239 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 252 ASSERT_TRUE(WriteFile(test_file(), "new content")); |
240 ASSERT_TRUE(WaitForEvents()); | 253 ASSERT_TRUE(WaitForEvents()); |
| 254 DeleteDelegateOnFileThread(delegate.release()); |
241 } | 255 } |
242 | 256 |
243 // Verify that moving the file into place is caught. | 257 // Verify that moving the file into place is caught. |
244 TEST_F(FilePathWatcherTest, MovedFile) { | 258 TEST_F(FilePathWatcherTest, MovedFile) { |
245 FilePath source_file(temp_dir_.GetPath().AppendASCII("source")); | 259 FilePath source_file(temp_dir_.GetPath().AppendASCII("source")); |
246 ASSERT_TRUE(WriteFile(source_file, "content")); | 260 ASSERT_TRUE(WriteFile(source_file, "content")); |
247 | 261 |
248 FilePathWatcher watcher; | 262 FilePathWatcher watcher; |
249 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 263 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
250 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 264 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
251 | 265 |
252 // Now make sure we get notified if the file is modified. | 266 // Now make sure we get notified if the file is modified. |
253 ASSERT_TRUE(base::Move(source_file, test_file())); | 267 ASSERT_TRUE(base::Move(source_file, test_file())); |
254 ASSERT_TRUE(WaitForEvents()); | 268 ASSERT_TRUE(WaitForEvents()); |
| 269 DeleteDelegateOnFileThread(delegate.release()); |
255 } | 270 } |
256 | 271 |
257 TEST_F(FilePathWatcherTest, DeletedFile) { | 272 TEST_F(FilePathWatcherTest, DeletedFile) { |
258 ASSERT_TRUE(WriteFile(test_file(), "content")); | 273 ASSERT_TRUE(WriteFile(test_file(), "content")); |
259 | 274 |
260 FilePathWatcher watcher; | 275 FilePathWatcher watcher; |
261 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 276 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
262 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 277 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
263 | 278 |
264 // Now make sure we get notified if the file is deleted. | 279 // Now make sure we get notified if the file is deleted. |
265 base::DeleteFile(test_file(), false); | 280 base::DeleteFile(test_file(), false); |
266 ASSERT_TRUE(WaitForEvents()); | 281 ASSERT_TRUE(WaitForEvents()); |
| 282 DeleteDelegateOnFileThread(delegate.release()); |
267 } | 283 } |
268 | 284 |
269 // Used by the DeleteDuringNotify test below. | 285 // Used by the DeleteDuringNotify test below. |
270 // Deletes the FilePathWatcher when it's notified. | 286 // Deletes the FilePathWatcher when it's notified. |
271 class Deleter : public TestDelegateBase { | 287 class Deleter : public TestDelegateBase { |
272 public: | 288 public: |
273 Deleter(FilePathWatcher* watcher, MessageLoop* loop) | 289 Deleter(FilePathWatcher* watcher, MessageLoop* loop) |
274 : watcher_(watcher), | 290 : watcher_(watcher), |
275 loop_(loop) { | 291 loop_(loop) { |
276 } | 292 } |
(...skipping 27 matching lines...) Expand all Loading... |
304 // We win if we haven't crashed yet. | 320 // We win if we haven't crashed yet. |
305 // Might as well double-check it got deleted, too. | 321 // Might as well double-check it got deleted, too. |
306 ASSERT_TRUE(deleter->watcher() == NULL); | 322 ASSERT_TRUE(deleter->watcher() == NULL); |
307 } | 323 } |
308 | 324 |
309 // Verify that deleting the watcher works even if there is a pending | 325 // Verify that deleting the watcher works even if there is a pending |
310 // notification. | 326 // notification. |
311 // Flaky on MacOS (and ARM linux): http://crbug.com/85930 | 327 // Flaky on MacOS (and ARM linux): http://crbug.com/85930 |
312 TEST_F(FilePathWatcherTest, DISABLED_DestroyWithPendingNotification) { | 328 TEST_F(FilePathWatcherTest, DISABLED_DestroyWithPendingNotification) { |
313 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 329 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
314 FilePathWatcher watcher; | 330 FilePathWatcher* watcher = new FilePathWatcher; |
315 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 331 ASSERT_TRUE(SetupWatch(test_file(), watcher, delegate.get(), false)); |
316 ASSERT_TRUE(WriteFile(test_file(), "content")); | 332 ASSERT_TRUE(WriteFile(test_file(), "content")); |
| 333 file_thread_.task_runner()->DeleteSoon(FROM_HERE, watcher); |
| 334 DeleteDelegateOnFileThread(delegate.release()); |
317 } | 335 } |
318 | 336 |
319 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) { | 337 TEST_F(FilePathWatcherTest, MultipleWatchersSingleFile) { |
320 FilePathWatcher watcher1, watcher2; | 338 FilePathWatcher watcher1, watcher2; |
321 std::unique_ptr<TestDelegate> delegate1(new TestDelegate(collector())); | 339 std::unique_ptr<TestDelegate> delegate1(new TestDelegate(collector())); |
322 std::unique_ptr<TestDelegate> delegate2(new TestDelegate(collector())); | 340 std::unique_ptr<TestDelegate> delegate2(new TestDelegate(collector())); |
323 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get(), false)); | 341 ASSERT_TRUE(SetupWatch(test_file(), &watcher1, delegate1.get(), false)); |
324 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get(), false)); | 342 ASSERT_TRUE(SetupWatch(test_file(), &watcher2, delegate2.get(), false)); |
325 | 343 |
326 ASSERT_TRUE(WriteFile(test_file(), "content")); | 344 ASSERT_TRUE(WriteFile(test_file(), "content")); |
327 ASSERT_TRUE(WaitForEvents()); | 345 ASSERT_TRUE(WaitForEvents()); |
| 346 DeleteDelegateOnFileThread(delegate1.release()); |
| 347 DeleteDelegateOnFileThread(delegate2.release()); |
328 } | 348 } |
329 | 349 |
330 // Verify that watching a file whose parent directory doesn't exist yet works if | 350 // Verify that watching a file whose parent directory doesn't exist yet works if |
331 // the directory and file are created eventually. | 351 // the directory and file are created eventually. |
332 TEST_F(FilePathWatcherTest, NonExistentDirectory) { | 352 TEST_F(FilePathWatcherTest, NonExistentDirectory) { |
333 FilePathWatcher watcher; | 353 FilePathWatcher watcher; |
334 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); | 354 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
335 FilePath file(dir.AppendASCII("file")); | 355 FilePath file(dir.AppendASCII("file")); |
336 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 356 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
337 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); | 357 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); |
338 | 358 |
339 ASSERT_TRUE(base::CreateDirectory(dir)); | 359 ASSERT_TRUE(base::CreateDirectory(dir)); |
340 | 360 |
341 ASSERT_TRUE(WriteFile(file, "content")); | 361 ASSERT_TRUE(WriteFile(file, "content")); |
342 | 362 |
343 VLOG(1) << "Waiting for file creation"; | 363 VLOG(1) << "Waiting for file creation"; |
344 ASSERT_TRUE(WaitForEvents()); | 364 ASSERT_TRUE(WaitForEvents()); |
345 | 365 |
346 ASSERT_TRUE(WriteFile(file, "content v2")); | 366 ASSERT_TRUE(WriteFile(file, "content v2")); |
347 VLOG(1) << "Waiting for file change"; | 367 VLOG(1) << "Waiting for file change"; |
348 ASSERT_TRUE(WaitForEvents()); | 368 ASSERT_TRUE(WaitForEvents()); |
349 | 369 |
350 ASSERT_TRUE(base::DeleteFile(file, false)); | 370 ASSERT_TRUE(base::DeleteFile(file, false)); |
351 VLOG(1) << "Waiting for file deletion"; | 371 VLOG(1) << "Waiting for file deletion"; |
352 ASSERT_TRUE(WaitForEvents()); | 372 ASSERT_TRUE(WaitForEvents()); |
| 373 DeleteDelegateOnFileThread(delegate.release()); |
353 } | 374 } |
354 | 375 |
355 // Exercises watch reconfiguration for the case that directories on the path | 376 // Exercises watch reconfiguration for the case that directories on the path |
356 // are rapidly created. | 377 // are rapidly created. |
357 TEST_F(FilePathWatcherTest, DirectoryChain) { | 378 TEST_F(FilePathWatcherTest, DirectoryChain) { |
358 FilePath path(temp_dir_.GetPath()); | 379 FilePath path(temp_dir_.GetPath()); |
359 std::vector<std::string> dir_names; | 380 std::vector<std::string> dir_names; |
360 for (int i = 0; i < 20; i++) { | 381 for (int i = 0; i < 20; i++) { |
361 std::string dir(base::StringPrintf("d%d", i)); | 382 std::string dir(base::StringPrintf("d%d", i)); |
362 dir_names.push_back(dir); | 383 dir_names.push_back(dir); |
(...skipping 12 matching lines...) Expand all Loading... |
375 ASSERT_TRUE(base::CreateDirectory(sub_path)); | 396 ASSERT_TRUE(base::CreateDirectory(sub_path)); |
376 } | 397 } |
377 VLOG(1) << "Create File"; | 398 VLOG(1) << "Create File"; |
378 ASSERT_TRUE(WriteFile(file, "content")); | 399 ASSERT_TRUE(WriteFile(file, "content")); |
379 VLOG(1) << "Waiting for file creation"; | 400 VLOG(1) << "Waiting for file creation"; |
380 ASSERT_TRUE(WaitForEvents()); | 401 ASSERT_TRUE(WaitForEvents()); |
381 | 402 |
382 ASSERT_TRUE(WriteFile(file, "content v2")); | 403 ASSERT_TRUE(WriteFile(file, "content v2")); |
383 VLOG(1) << "Waiting for file modification"; | 404 VLOG(1) << "Waiting for file modification"; |
384 ASSERT_TRUE(WaitForEvents()); | 405 ASSERT_TRUE(WaitForEvents()); |
| 406 DeleteDelegateOnFileThread(delegate.release()); |
385 } | 407 } |
386 | 408 |
387 #if defined(OS_MACOSX) | 409 #if defined(OS_MACOSX) |
388 // http://crbug.com/85930 | 410 // http://crbug.com/85930 |
389 #define DisappearingDirectory DISABLED_DisappearingDirectory | 411 #define DisappearingDirectory DISABLED_DisappearingDirectory |
390 #endif | 412 #endif |
391 TEST_F(FilePathWatcherTest, DisappearingDirectory) { | 413 TEST_F(FilePathWatcherTest, DisappearingDirectory) { |
392 FilePathWatcher watcher; | 414 FilePathWatcher watcher; |
393 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); | 415 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
394 FilePath file(dir.AppendASCII("file")); | 416 FilePath file(dir.AppendASCII("file")); |
395 ASSERT_TRUE(base::CreateDirectory(dir)); | 417 ASSERT_TRUE(base::CreateDirectory(dir)); |
396 ASSERT_TRUE(WriteFile(file, "content")); | 418 ASSERT_TRUE(WriteFile(file, "content")); |
397 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 419 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
398 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); | 420 ASSERT_TRUE(SetupWatch(file, &watcher, delegate.get(), false)); |
399 | 421 |
400 ASSERT_TRUE(base::DeleteFile(dir, true)); | 422 ASSERT_TRUE(base::DeleteFile(dir, true)); |
401 ASSERT_TRUE(WaitForEvents()); | 423 ASSERT_TRUE(WaitForEvents()); |
| 424 DeleteDelegateOnFileThread(delegate.release()); |
402 } | 425 } |
403 | 426 |
404 // Tests that a file that is deleted and reappears is tracked correctly. | 427 // Tests that a file that is deleted and reappears is tracked correctly. |
405 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { | 428 TEST_F(FilePathWatcherTest, DeleteAndRecreate) { |
406 ASSERT_TRUE(WriteFile(test_file(), "content")); | 429 ASSERT_TRUE(WriteFile(test_file(), "content")); |
407 FilePathWatcher watcher; | 430 FilePathWatcher watcher; |
408 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 431 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
409 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 432 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
410 | 433 |
411 ASSERT_TRUE(base::DeleteFile(test_file(), false)); | 434 ASSERT_TRUE(base::DeleteFile(test_file(), false)); |
412 VLOG(1) << "Waiting for file deletion"; | 435 VLOG(1) << "Waiting for file deletion"; |
413 ASSERT_TRUE(WaitForEvents()); | 436 ASSERT_TRUE(WaitForEvents()); |
414 | 437 |
415 ASSERT_TRUE(WriteFile(test_file(), "content")); | 438 ASSERT_TRUE(WriteFile(test_file(), "content")); |
416 VLOG(1) << "Waiting for file creation"; | 439 VLOG(1) << "Waiting for file creation"; |
417 ASSERT_TRUE(WaitForEvents()); | 440 ASSERT_TRUE(WaitForEvents()); |
| 441 DeleteDelegateOnFileThread(delegate.release()); |
418 } | 442 } |
419 | 443 |
420 TEST_F(FilePathWatcherTest, WatchDirectory) { | 444 TEST_F(FilePathWatcherTest, WatchDirectory) { |
421 FilePathWatcher watcher; | 445 FilePathWatcher watcher; |
422 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); | 446 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
423 FilePath file1(dir.AppendASCII("file1")); | 447 FilePath file1(dir.AppendASCII("file1")); |
424 FilePath file2(dir.AppendASCII("file2")); | 448 FilePath file2(dir.AppendASCII("file2")); |
425 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 449 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
426 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), false)); | 450 ASSERT_TRUE(SetupWatch(dir, &watcher, delegate.get(), false)); |
427 | 451 |
(...skipping 12 matching lines...) Expand all Loading... |
440 ASSERT_TRUE(WaitForEvents()); | 464 ASSERT_TRUE(WaitForEvents()); |
441 #endif // !OS_MACOSX | 465 #endif // !OS_MACOSX |
442 | 466 |
443 ASSERT_TRUE(base::DeleteFile(file1, false)); | 467 ASSERT_TRUE(base::DeleteFile(file1, false)); |
444 VLOG(1) << "Waiting for file1 deletion"; | 468 VLOG(1) << "Waiting for file1 deletion"; |
445 ASSERT_TRUE(WaitForEvents()); | 469 ASSERT_TRUE(WaitForEvents()); |
446 | 470 |
447 ASSERT_TRUE(WriteFile(file2, "content")); | 471 ASSERT_TRUE(WriteFile(file2, "content")); |
448 VLOG(1) << "Waiting for file2 creation"; | 472 VLOG(1) << "Waiting for file2 creation"; |
449 ASSERT_TRUE(WaitForEvents()); | 473 ASSERT_TRUE(WaitForEvents()); |
| 474 DeleteDelegateOnFileThread(delegate.release()); |
450 } | 475 } |
451 | 476 |
452 TEST_F(FilePathWatcherTest, MoveParent) { | 477 TEST_F(FilePathWatcherTest, MoveParent) { |
453 FilePathWatcher file_watcher; | 478 FilePathWatcher file_watcher; |
454 FilePathWatcher subdir_watcher; | 479 FilePathWatcher subdir_watcher; |
455 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); | 480 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
456 FilePath dest(temp_dir_.GetPath().AppendASCII("dest")); | 481 FilePath dest(temp_dir_.GetPath().AppendASCII("dest")); |
457 FilePath subdir(dir.AppendASCII("subdir")); | 482 FilePath subdir(dir.AppendASCII("subdir")); |
458 FilePath file(subdir.AppendASCII("file")); | 483 FilePath file(subdir.AppendASCII("file")); |
459 std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 484 std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
460 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get(), false)); | 485 ASSERT_TRUE(SetupWatch(file, &file_watcher, file_delegate.get(), false)); |
461 std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 486 std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
462 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get(), | 487 ASSERT_TRUE(SetupWatch(subdir, &subdir_watcher, subdir_delegate.get(), |
463 false)); | 488 false)); |
464 | 489 |
465 // Setup a directory hierarchy. | 490 // Setup a directory hierarchy. |
466 ASSERT_TRUE(base::CreateDirectory(subdir)); | 491 ASSERT_TRUE(base::CreateDirectory(subdir)); |
467 ASSERT_TRUE(WriteFile(file, "content")); | 492 ASSERT_TRUE(WriteFile(file, "content")); |
468 VLOG(1) << "Waiting for file creation"; | 493 VLOG(1) << "Waiting for file creation"; |
469 ASSERT_TRUE(WaitForEvents()); | 494 ASSERT_TRUE(WaitForEvents()); |
470 | 495 |
471 // Move the parent directory. | 496 // Move the parent directory. |
472 base::Move(dir, dest); | 497 base::Move(dir, dest); |
473 VLOG(1) << "Waiting for directory move"; | 498 VLOG(1) << "Waiting for directory move"; |
474 ASSERT_TRUE(WaitForEvents()); | 499 ASSERT_TRUE(WaitForEvents()); |
| 500 DeleteDelegateOnFileThread(file_delegate.release()); |
| 501 DeleteDelegateOnFileThread(subdir_delegate.release()); |
475 } | 502 } |
476 | 503 |
477 TEST_F(FilePathWatcherTest, RecursiveWatch) { | 504 TEST_F(FilePathWatcherTest, RecursiveWatch) { |
478 FilePathWatcher watcher; | 505 FilePathWatcher watcher; |
479 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); | 506 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
480 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 507 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
481 bool setup_result = SetupWatch(dir, &watcher, delegate.get(), true); | 508 bool setup_result = SetupWatch(dir, &watcher, delegate.get(), true); |
482 if (!FilePathWatcher::RecursiveWatchAvailable()) { | 509 if (!FilePathWatcher::RecursiveWatchAvailable()) { |
483 ASSERT_FALSE(setup_result); | 510 ASSERT_FALSE(setup_result); |
| 511 DeleteDelegateOnFileThread(delegate.release()); |
484 return; | 512 return; |
485 } | 513 } |
486 ASSERT_TRUE(setup_result); | 514 ASSERT_TRUE(setup_result); |
487 | 515 |
488 // Main directory("dir") creation. | 516 // Main directory("dir") creation. |
489 ASSERT_TRUE(base::CreateDirectory(dir)); | 517 ASSERT_TRUE(base::CreateDirectory(dir)); |
490 ASSERT_TRUE(WaitForEvents()); | 518 ASSERT_TRUE(WaitForEvents()); |
491 | 519 |
492 // Create "$dir/file1". | 520 // Create "$dir/file1". |
493 FilePath file1(dir.AppendASCII("file1")); | 521 FilePath file1(dir.AppendASCII("file1")); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 ASSERT_TRUE(WaitForEvents()); | 557 ASSERT_TRUE(WaitForEvents()); |
530 #endif | 558 #endif |
531 | 559 |
532 // Delete "$dir/subdir/subdir_file1". | 560 // Delete "$dir/subdir/subdir_file1". |
533 ASSERT_TRUE(base::DeleteFile(subdir_file1, false)); | 561 ASSERT_TRUE(base::DeleteFile(subdir_file1, false)); |
534 ASSERT_TRUE(WaitForEvents()); | 562 ASSERT_TRUE(WaitForEvents()); |
535 | 563 |
536 // Delete "$dir/subdir/subdir_child_dir/child_dir_file1". | 564 // Delete "$dir/subdir/subdir_child_dir/child_dir_file1". |
537 ASSERT_TRUE(base::DeleteFile(child_dir_file1, false)); | 565 ASSERT_TRUE(base::DeleteFile(child_dir_file1, false)); |
538 ASSERT_TRUE(WaitForEvents()); | 566 ASSERT_TRUE(WaitForEvents()); |
| 567 DeleteDelegateOnFileThread(delegate.release()); |
539 } | 568 } |
540 | 569 |
541 #if defined(OS_POSIX) | 570 #if defined(OS_POSIX) |
542 #if defined(OS_ANDROID) | 571 #if defined(OS_ANDROID) |
543 // Apps cannot create symlinks on Android in /sdcard as /sdcard uses the | 572 // Apps cannot create symlinks on Android in /sdcard as /sdcard uses the |
544 // "fuse" file system, while /data uses "ext4". Running these tests in /data | 573 // "fuse" file system, while /data uses "ext4". Running these tests in /data |
545 // would be preferable and allow testing file attributes and symlinks. | 574 // would be preferable and allow testing file attributes and symlinks. |
546 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places | 575 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places |
547 // the |temp_dir_| in /data. | 576 // the |temp_dir_| in /data. |
548 #define RecursiveWithSymLink DISABLED_RecursiveWithSymLink | 577 #define RecursiveWithSymLink DISABLED_RecursiveWithSymLink |
(...skipping 27 matching lines...) Expand all Loading... |
576 FilePath target2(temp_dir_.GetPath().AppendASCII("target2")); | 605 FilePath target2(temp_dir_.GetPath().AppendASCII("target2")); |
577 ASSERT_TRUE(base::CreateDirectory(target2)); | 606 ASSERT_TRUE(base::CreateDirectory(target2)); |
578 ASSERT_TRUE(base::DeleteFile(symlink, false)); | 607 ASSERT_TRUE(base::DeleteFile(symlink, false)); |
579 ASSERT_TRUE(base::CreateSymbolicLink(target2, symlink)); | 608 ASSERT_TRUE(base::CreateSymbolicLink(target2, symlink)); |
580 ASSERT_TRUE(WaitForEvents()); | 609 ASSERT_TRUE(WaitForEvents()); |
581 | 610 |
582 // Create a file in target2. | 611 // Create a file in target2. |
583 FilePath target2_file(target2.AppendASCII("file")); | 612 FilePath target2_file(target2.AppendASCII("file")); |
584 ASSERT_TRUE(WriteFile(target2_file, "content")); | 613 ASSERT_TRUE(WriteFile(target2_file, "content")); |
585 ASSERT_TRUE(WaitForEvents()); | 614 ASSERT_TRUE(WaitForEvents()); |
| 615 |
| 616 DeleteDelegateOnFileThread(delegate.release()); |
586 } | 617 } |
587 #endif // OS_POSIX | 618 #endif // OS_POSIX |
588 | 619 |
589 TEST_F(FilePathWatcherTest, MoveChild) { | 620 TEST_F(FilePathWatcherTest, MoveChild) { |
590 FilePathWatcher file_watcher; | 621 FilePathWatcher file_watcher; |
591 FilePathWatcher subdir_watcher; | 622 FilePathWatcher subdir_watcher; |
592 FilePath source_dir(temp_dir_.GetPath().AppendASCII("source")); | 623 FilePath source_dir(temp_dir_.GetPath().AppendASCII("source")); |
593 FilePath source_subdir(source_dir.AppendASCII("subdir")); | 624 FilePath source_subdir(source_dir.AppendASCII("subdir")); |
594 FilePath source_file(source_subdir.AppendASCII("file")); | 625 FilePath source_file(source_subdir.AppendASCII("file")); |
595 FilePath dest_dir(temp_dir_.GetPath().AppendASCII("dest")); | 626 FilePath dest_dir(temp_dir_.GetPath().AppendASCII("dest")); |
596 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); | 627 FilePath dest_subdir(dest_dir.AppendASCII("subdir")); |
597 FilePath dest_file(dest_subdir.AppendASCII("file")); | 628 FilePath dest_file(dest_subdir.AppendASCII("file")); |
598 | 629 |
599 // Setup a directory hierarchy. | 630 // Setup a directory hierarchy. |
600 ASSERT_TRUE(base::CreateDirectory(source_subdir)); | 631 ASSERT_TRUE(base::CreateDirectory(source_subdir)); |
601 ASSERT_TRUE(WriteFile(source_file, "content")); | 632 ASSERT_TRUE(WriteFile(source_file, "content")); |
602 | 633 |
603 std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); | 634 std::unique_ptr<TestDelegate> file_delegate(new TestDelegate(collector())); |
604 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get(), false)); | 635 ASSERT_TRUE(SetupWatch(dest_file, &file_watcher, file_delegate.get(), false)); |
605 std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); | 636 std::unique_ptr<TestDelegate> subdir_delegate(new TestDelegate(collector())); |
606 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get(), | 637 ASSERT_TRUE(SetupWatch(dest_subdir, &subdir_watcher, subdir_delegate.get(), |
607 false)); | 638 false)); |
608 | 639 |
609 // Move the directory into place, s.t. the watched file appears. | 640 // Move the directory into place, s.t. the watched file appears. |
610 ASSERT_TRUE(base::Move(source_dir, dest_dir)); | 641 ASSERT_TRUE(base::Move(source_dir, dest_dir)); |
611 ASSERT_TRUE(WaitForEvents()); | 642 ASSERT_TRUE(WaitForEvents()); |
| 643 DeleteDelegateOnFileThread(file_delegate.release()); |
| 644 DeleteDelegateOnFileThread(subdir_delegate.release()); |
612 } | 645 } |
613 | 646 |
614 // Verify that changing attributes on a file is caught | 647 // Verify that changing attributes on a file is caught |
615 #if defined(OS_ANDROID) | 648 #if defined(OS_ANDROID) |
616 // Apps cannot change file attributes on Android in /sdcard as /sdcard uses the | 649 // Apps cannot change file attributes on Android in /sdcard as /sdcard uses the |
617 // "fuse" file system, while /data uses "ext4". Running these tests in /data | 650 // "fuse" file system, while /data uses "ext4". Running these tests in /data |
618 // would be preferable and allow testing file attributes and symlinks. | 651 // would be preferable and allow testing file attributes and symlinks. |
619 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places | 652 // TODO(pauljensen): Re-enable when crbug.com/475568 is fixed and SetUp() places |
620 // the |temp_dir_| in /data. | 653 // the |temp_dir_| in /data. |
621 #define FileAttributesChanged DISABLED_FileAttributesChanged | 654 #define FileAttributesChanged DISABLED_FileAttributesChanged |
622 #endif // defined(OS_ANDROID | 655 #endif // defined(OS_ANDROID |
623 TEST_F(FilePathWatcherTest, FileAttributesChanged) { | 656 TEST_F(FilePathWatcherTest, FileAttributesChanged) { |
624 ASSERT_TRUE(WriteFile(test_file(), "content")); | 657 ASSERT_TRUE(WriteFile(test_file(), "content")); |
625 FilePathWatcher watcher; | 658 FilePathWatcher watcher; |
626 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 659 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
627 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); | 660 ASSERT_TRUE(SetupWatch(test_file(), &watcher, delegate.get(), false)); |
628 | 661 |
629 // Now make sure we get notified if the file is modified. | 662 // Now make sure we get notified if the file is modified. |
630 ASSERT_TRUE(base::MakeFileUnreadable(test_file())); | 663 ASSERT_TRUE(base::MakeFileUnreadable(test_file())); |
631 ASSERT_TRUE(WaitForEvents()); | 664 ASSERT_TRUE(WaitForEvents()); |
| 665 DeleteDelegateOnFileThread(delegate.release()); |
632 } | 666 } |
633 | 667 |
634 #if defined(OS_LINUX) | 668 #if defined(OS_LINUX) |
635 | 669 |
636 // Verify that creating a symlink is caught. | 670 // Verify that creating a symlink is caught. |
637 TEST_F(FilePathWatcherTest, CreateLink) { | 671 TEST_F(FilePathWatcherTest, CreateLink) { |
638 FilePathWatcher watcher; | 672 FilePathWatcher watcher; |
639 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 673 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
640 // Note that we are watching the symlink | 674 // Note that we are watching the symlink |
641 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 675 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
642 | 676 |
643 // Now make sure we get notified if the link is created. | 677 // Now make sure we get notified if the link is created. |
644 // Note that test_file() doesn't have to exist. | 678 // Note that test_file() doesn't have to exist. |
645 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); | 679 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
646 ASSERT_TRUE(WaitForEvents()); | 680 ASSERT_TRUE(WaitForEvents()); |
| 681 DeleteDelegateOnFileThread(delegate.release()); |
647 } | 682 } |
648 | 683 |
649 // Verify that deleting a symlink is caught. | 684 // Verify that deleting a symlink is caught. |
650 TEST_F(FilePathWatcherTest, DeleteLink) { | 685 TEST_F(FilePathWatcherTest, DeleteLink) { |
651 // Unfortunately this test case only works if the link target exists. | 686 // Unfortunately this test case only works if the link target exists. |
652 // TODO(craig) fix this as part of crbug.com/91561. | 687 // TODO(craig) fix this as part of crbug.com/91561. |
653 ASSERT_TRUE(WriteFile(test_file(), "content")); | 688 ASSERT_TRUE(WriteFile(test_file(), "content")); |
654 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); | 689 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
655 FilePathWatcher watcher; | 690 FilePathWatcher watcher; |
656 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 691 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
657 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 692 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
658 | 693 |
659 // Now make sure we get notified if the link is deleted. | 694 // Now make sure we get notified if the link is deleted. |
660 ASSERT_TRUE(base::DeleteFile(test_link(), false)); | 695 ASSERT_TRUE(base::DeleteFile(test_link(), false)); |
661 ASSERT_TRUE(WaitForEvents()); | 696 ASSERT_TRUE(WaitForEvents()); |
| 697 DeleteDelegateOnFileThread(delegate.release()); |
662 } | 698 } |
663 | 699 |
664 // Verify that modifying a target file that a link is pointing to | 700 // Verify that modifying a target file that a link is pointing to |
665 // when we are watching the link is caught. | 701 // when we are watching the link is caught. |
666 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { | 702 TEST_F(FilePathWatcherTest, ModifiedLinkedFile) { |
667 ASSERT_TRUE(WriteFile(test_file(), "content")); | 703 ASSERT_TRUE(WriteFile(test_file(), "content")); |
668 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); | 704 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
669 FilePathWatcher watcher; | 705 FilePathWatcher watcher; |
670 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 706 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
671 // Note that we are watching the symlink. | 707 // Note that we are watching the symlink. |
672 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 708 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
673 | 709 |
674 // Now make sure we get notified if the file is modified. | 710 // Now make sure we get notified if the file is modified. |
675 ASSERT_TRUE(WriteFile(test_file(), "new content")); | 711 ASSERT_TRUE(WriteFile(test_file(), "new content")); |
676 ASSERT_TRUE(WaitForEvents()); | 712 ASSERT_TRUE(WaitForEvents()); |
| 713 DeleteDelegateOnFileThread(delegate.release()); |
677 } | 714 } |
678 | 715 |
679 // Verify that creating a target file that a link is pointing to | 716 // Verify that creating a target file that a link is pointing to |
680 // when we are watching the link is caught. | 717 // when we are watching the link is caught. |
681 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { | 718 TEST_F(FilePathWatcherTest, CreateTargetLinkedFile) { |
682 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); | 719 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
683 FilePathWatcher watcher; | 720 FilePathWatcher watcher; |
684 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 721 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
685 // Note that we are watching the symlink. | 722 // Note that we are watching the symlink. |
686 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 723 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
687 | 724 |
688 // Now make sure we get notified if the target file is created. | 725 // Now make sure we get notified if the target file is created. |
689 ASSERT_TRUE(WriteFile(test_file(), "content")); | 726 ASSERT_TRUE(WriteFile(test_file(), "content")); |
690 ASSERT_TRUE(WaitForEvents()); | 727 ASSERT_TRUE(WaitForEvents()); |
| 728 DeleteDelegateOnFileThread(delegate.release()); |
691 } | 729 } |
692 | 730 |
693 // Verify that deleting a target file that a link is pointing to | 731 // Verify that deleting a target file that a link is pointing to |
694 // when we are watching the link is caught. | 732 // when we are watching the link is caught. |
695 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { | 733 TEST_F(FilePathWatcherTest, DeleteTargetLinkedFile) { |
696 ASSERT_TRUE(WriteFile(test_file(), "content")); | 734 ASSERT_TRUE(WriteFile(test_file(), "content")); |
697 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); | 735 ASSERT_TRUE(CreateSymbolicLink(test_file(), test_link())); |
698 FilePathWatcher watcher; | 736 FilePathWatcher watcher; |
699 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 737 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
700 // Note that we are watching the symlink. | 738 // Note that we are watching the symlink. |
701 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); | 739 ASSERT_TRUE(SetupWatch(test_link(), &watcher, delegate.get(), false)); |
702 | 740 |
703 // Now make sure we get notified if the target file is deleted. | 741 // Now make sure we get notified if the target file is deleted. |
704 ASSERT_TRUE(base::DeleteFile(test_file(), false)); | 742 ASSERT_TRUE(base::DeleteFile(test_file(), false)); |
705 ASSERT_TRUE(WaitForEvents()); | 743 ASSERT_TRUE(WaitForEvents()); |
| 744 DeleteDelegateOnFileThread(delegate.release()); |
706 } | 745 } |
707 | 746 |
708 // Verify that watching a file whose parent directory is a link that | 747 // Verify that watching a file whose parent directory is a link that |
709 // doesn't exist yet works if the symlink is created eventually. | 748 // doesn't exist yet works if the symlink is created eventually. |
710 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { | 749 TEST_F(FilePathWatcherTest, LinkedDirectoryPart1) { |
711 FilePathWatcher watcher; | 750 FilePathWatcher watcher; |
712 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); | 751 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
713 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); | 752 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); |
714 FilePath file(dir.AppendASCII("file")); | 753 FilePath file(dir.AppendASCII("file")); |
715 FilePath linkfile(link_dir.AppendASCII("file")); | 754 FilePath linkfile(link_dir.AppendASCII("file")); |
716 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 755 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
717 // dir/file should exist. | 756 // dir/file should exist. |
718 ASSERT_TRUE(base::CreateDirectory(dir)); | 757 ASSERT_TRUE(base::CreateDirectory(dir)); |
719 ASSERT_TRUE(WriteFile(file, "content")); | 758 ASSERT_TRUE(WriteFile(file, "content")); |
720 // Note that we are watching dir.lnk/file which doesn't exist yet. | 759 // Note that we are watching dir.lnk/file which doesn't exist yet. |
721 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 760 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
722 | 761 |
723 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); | 762 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
724 VLOG(1) << "Waiting for link creation"; | 763 VLOG(1) << "Waiting for link creation"; |
725 ASSERT_TRUE(WaitForEvents()); | 764 ASSERT_TRUE(WaitForEvents()); |
726 | 765 |
727 ASSERT_TRUE(WriteFile(file, "content v2")); | 766 ASSERT_TRUE(WriteFile(file, "content v2")); |
728 VLOG(1) << "Waiting for file change"; | 767 VLOG(1) << "Waiting for file change"; |
729 ASSERT_TRUE(WaitForEvents()); | 768 ASSERT_TRUE(WaitForEvents()); |
730 | 769 |
731 ASSERT_TRUE(base::DeleteFile(file, false)); | 770 ASSERT_TRUE(base::DeleteFile(file, false)); |
732 VLOG(1) << "Waiting for file deletion"; | 771 VLOG(1) << "Waiting for file deletion"; |
733 ASSERT_TRUE(WaitForEvents()); | 772 ASSERT_TRUE(WaitForEvents()); |
| 773 DeleteDelegateOnFileThread(delegate.release()); |
734 } | 774 } |
735 | 775 |
736 // Verify that watching a file whose parent directory is a | 776 // Verify that watching a file whose parent directory is a |
737 // dangling symlink works if the directory is created eventually. | 777 // dangling symlink works if the directory is created eventually. |
738 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { | 778 TEST_F(FilePathWatcherTest, LinkedDirectoryPart2) { |
739 FilePathWatcher watcher; | 779 FilePathWatcher watcher; |
740 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); | 780 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
741 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); | 781 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); |
742 FilePath file(dir.AppendASCII("file")); | 782 FilePath file(dir.AppendASCII("file")); |
743 FilePath linkfile(link_dir.AppendASCII("file")); | 783 FilePath linkfile(link_dir.AppendASCII("file")); |
744 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 784 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
745 // Now create the link from dir.lnk pointing to dir but | 785 // Now create the link from dir.lnk pointing to dir but |
746 // neither dir nor dir/file exist yet. | 786 // neither dir nor dir/file exist yet. |
747 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); | 787 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
748 // Note that we are watching dir.lnk/file. | 788 // Note that we are watching dir.lnk/file. |
749 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 789 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
750 | 790 |
751 ASSERT_TRUE(base::CreateDirectory(dir)); | 791 ASSERT_TRUE(base::CreateDirectory(dir)); |
752 ASSERT_TRUE(WriteFile(file, "content")); | 792 ASSERT_TRUE(WriteFile(file, "content")); |
753 VLOG(1) << "Waiting for dir/file creation"; | 793 VLOG(1) << "Waiting for dir/file creation"; |
754 ASSERT_TRUE(WaitForEvents()); | 794 ASSERT_TRUE(WaitForEvents()); |
755 | 795 |
756 ASSERT_TRUE(WriteFile(file, "content v2")); | 796 ASSERT_TRUE(WriteFile(file, "content v2")); |
757 VLOG(1) << "Waiting for file change"; | 797 VLOG(1) << "Waiting for file change"; |
758 ASSERT_TRUE(WaitForEvents()); | 798 ASSERT_TRUE(WaitForEvents()); |
759 | 799 |
760 ASSERT_TRUE(base::DeleteFile(file, false)); | 800 ASSERT_TRUE(base::DeleteFile(file, false)); |
761 VLOG(1) << "Waiting for file deletion"; | 801 VLOG(1) << "Waiting for file deletion"; |
762 ASSERT_TRUE(WaitForEvents()); | 802 ASSERT_TRUE(WaitForEvents()); |
| 803 DeleteDelegateOnFileThread(delegate.release()); |
763 } | 804 } |
764 | 805 |
765 // Verify that watching a file with a symlink on the path | 806 // Verify that watching a file with a symlink on the path |
766 // to the file works. | 807 // to the file works. |
767 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { | 808 TEST_F(FilePathWatcherTest, LinkedDirectoryPart3) { |
768 FilePathWatcher watcher; | 809 FilePathWatcher watcher; |
769 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); | 810 FilePath dir(temp_dir_.GetPath().AppendASCII("dir")); |
770 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); | 811 FilePath link_dir(temp_dir_.GetPath().AppendASCII("dir.lnk")); |
771 FilePath file(dir.AppendASCII("file")); | 812 FilePath file(dir.AppendASCII("file")); |
772 FilePath linkfile(link_dir.AppendASCII("file")); | 813 FilePath linkfile(link_dir.AppendASCII("file")); |
773 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); | 814 std::unique_ptr<TestDelegate> delegate(new TestDelegate(collector())); |
774 ASSERT_TRUE(base::CreateDirectory(dir)); | 815 ASSERT_TRUE(base::CreateDirectory(dir)); |
775 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); | 816 ASSERT_TRUE(CreateSymbolicLink(dir, link_dir)); |
776 // Note that we are watching dir.lnk/file but the file doesn't exist yet. | 817 // Note that we are watching dir.lnk/file but the file doesn't exist yet. |
777 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); | 818 ASSERT_TRUE(SetupWatch(linkfile, &watcher, delegate.get(), false)); |
778 | 819 |
779 ASSERT_TRUE(WriteFile(file, "content")); | 820 ASSERT_TRUE(WriteFile(file, "content")); |
780 VLOG(1) << "Waiting for file creation"; | 821 VLOG(1) << "Waiting for file creation"; |
781 ASSERT_TRUE(WaitForEvents()); | 822 ASSERT_TRUE(WaitForEvents()); |
782 | 823 |
783 ASSERT_TRUE(WriteFile(file, "content v2")); | 824 ASSERT_TRUE(WriteFile(file, "content v2")); |
784 VLOG(1) << "Waiting for file change"; | 825 VLOG(1) << "Waiting for file change"; |
785 ASSERT_TRUE(WaitForEvents()); | 826 ASSERT_TRUE(WaitForEvents()); |
786 | 827 |
787 ASSERT_TRUE(base::DeleteFile(file, false)); | 828 ASSERT_TRUE(base::DeleteFile(file, false)); |
788 VLOG(1) << "Waiting for file deletion"; | 829 VLOG(1) << "Waiting for file deletion"; |
789 ASSERT_TRUE(WaitForEvents()); | 830 ASSERT_TRUE(WaitForEvents()); |
| 831 DeleteDelegateOnFileThread(delegate.release()); |
790 } | 832 } |
791 | 833 |
792 #endif // OS_LINUX | 834 #endif // OS_LINUX |
793 | 835 |
794 enum Permission { | 836 enum Permission { |
795 Read, | 837 Read, |
796 Write, | 838 Write, |
797 Execute | 839 Execute |
798 }; | 840 }; |
799 | 841 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 MessageLoop::QuitWhenIdleClosure(), | 899 MessageLoop::QuitWhenIdleClosure(), |
858 TestTimeouts::tiny_timeout()); | 900 TestTimeouts::tiny_timeout()); |
859 ASSERT_FALSE(WaitForEvents()); | 901 ASSERT_FALSE(WaitForEvents()); |
860 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); | 902 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Read, true)); |
861 | 903 |
862 // We should get notified in this case because filepathwatcher can no | 904 // We should get notified in this case because filepathwatcher can no |
863 // longer access the file | 905 // longer access the file |
864 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 906 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
865 ASSERT_TRUE(WaitForEvents()); | 907 ASSERT_TRUE(WaitForEvents()); |
866 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 908 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
| 909 DeleteDelegateOnFileThread(delegate.release()); |
867 } | 910 } |
868 | 911 |
869 #endif // OS_MACOSX | 912 #endif // OS_MACOSX |
870 } // namespace | 913 } // namespace |
871 | 914 |
872 } // namespace base | 915 } // namespace base |
OLD | NEW |