| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/file_manager/file_watcher.h" | 5 #include "chrome/browser/chromeos/file_manager/file_watcher.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/task_scheduler/task_scheduler.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "google_apis/drive/test_util.h" | 13 #include "google_apis/drive/test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace file_manager { | 16 namespace file_manager { |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 using google_apis::test_util::CreateQuitCallback; | 19 using google_apis::test_util::CreateQuitCallback; |
| 19 using google_apis::test_util::CreateCopyResultCallback; | 20 using google_apis::test_util::CreateCopyResultCallback; |
| 20 | 21 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // Start watching changes in the temporary directory. | 123 // Start watching changes in the temporary directory. |
| 123 base::FilePath changed_path; | 124 base::FilePath changed_path; |
| 124 bool watcher_created = false; | 125 bool watcher_created = false; |
| 125 bool on_change_error = false; | 126 bool on_change_error = false; |
| 126 base::RunLoop run_loop; | 127 base::RunLoop run_loop; |
| 127 file_watcher->WatchLocalFile( | 128 file_watcher->WatchLocalFile( |
| 128 temp_dir.GetPath(), | 129 temp_dir.GetPath(), |
| 129 CreateQuitCallback( | 130 CreateQuitCallback( |
| 130 &run_loop, CreateCopyResultCallback(&changed_path, &on_change_error)), | 131 &run_loop, CreateCopyResultCallback(&changed_path, &on_change_error)), |
| 131 CreateCopyResultCallback(&watcher_created)); | 132 CreateCopyResultCallback(&watcher_created)); |
| 132 // Spin the message loop so the base::FilePathWatcher is created. | 133 // Flush the task scheduler and spin the message loop and so the |
| 134 // base::FilePathWatcher is created. |
| 135 base::TaskScheduler::GetInstance()->FlushForTesting(); |
| 133 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
| 134 ASSERT_TRUE(watcher_created); | 137 ASSERT_TRUE(watcher_created); |
| 135 | 138 |
| 136 // Create a temporary file in the temporary directory. The file watcher | 139 // Create a temporary file in the temporary directory. The file watcher |
| 137 // should detect the change in the directory. | 140 // should detect the change in the directory. |
| 138 base::FilePath temp_file_path; | 141 base::FilePath temp_file_path; |
| 139 ASSERT_TRUE( | 142 ASSERT_TRUE( |
| 140 base::CreateTemporaryFileInDir(temp_dir.GetPath(), &temp_file_path)); | 143 base::CreateTemporaryFileInDir(temp_dir.GetPath(), &temp_file_path)); |
| 141 // Wait until the directory change is notified. | 144 // Wait until the directory change is notified. |
| 142 run_loop.Run(); | 145 run_loop.Run(); |
| 143 ASSERT_FALSE(on_change_error); | 146 ASSERT_FALSE(on_change_error); |
| 144 ASSERT_EQ(temp_dir.GetPath().value(), changed_path.value()); | 147 ASSERT_EQ(temp_dir.GetPath().value(), changed_path.value()); |
| 145 | 148 |
| 146 // This is ugly, but FileWatcher should be deleted explicitly here, and | 149 // This is ugly, but FileWatcher should be deleted explicitly here, and |
| 147 // spin the message loop so the base::FilePathWatcher is deleted. | 150 // flush the task scheduler, so the base::FilePathWatcher is deleted. |
| 148 // Otherwise, base::FilePathWatcher may detect a change when the temporary | 151 // Otherwise, base::FilePathWatcher may detect a change when the temporary |
| 149 // directory is deleted, which may result in crash. | 152 // directory is deleted, which may result in crash. |
| 150 file_watcher.reset(); | 153 file_watcher.reset(); |
| 151 base::RunLoop().RunUntilIdle(); | 154 base::TaskScheduler::GetInstance()->FlushForTesting(); |
| 152 } | 155 } |
| 153 | 156 |
| 154 } // namespace | 157 } // namespace |
| 155 } // namespace file_manager. | 158 } // namespace file_manager. |
| OLD | NEW |