Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: chrome/browser/chromeos/file_manager/file_watcher_unittest.cc

Issue 2970653002: Reland of "file_manager: Migrate FILE thread to TaskScheduler" (Closed)
Patch Set: fix Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 109
109 TEST_F(FileManagerFileWatcherTest, WatchLocalFile) { 110 TEST_F(FileManagerFileWatcherTest, WatchLocalFile) {
110 const base::FilePath kVirtualPath = 111 const base::FilePath kVirtualPath =
111 base::FilePath::FromUTF8Unsafe("foo/bar.txt"); 112 base::FilePath::FromUTF8Unsafe("foo/bar.txt");
112 const char kExtensionId[] = "extension-id"; 113 const char kExtensionId[] = "extension-id";
113 114
114 // Create a temporary directory. 115 // Create a temporary directory.
115 base::ScopedTempDir temp_dir; 116 base::ScopedTempDir temp_dir;
116 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 117 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
117 118
118 // See the comment at the end of this function for why scoped_ptr is used. 119 // Create a callback that will run when a change is detected.
119 std::unique_ptr<FileWatcher> file_watcher(new FileWatcher(kVirtualPath)); 120 bool on_change_error = false;
120 file_watcher->AddExtension(kExtensionId); 121 base::FilePath changed_path;
122 base::RunLoop change_run_loop;
123 base::FilePathWatcher::Callback change_callback = CreateQuitCallback(
124 &change_run_loop,
125 CreateCopyResultCallback(&changed_path, &on_change_error));
126
127 // Create a callback that will run when the watcher is started.
128 bool watcher_created = false;
129 base::RunLoop start_run_loop;
130 FileWatcher::BoolCallback start_callback = CreateQuitCallback(
131 &start_run_loop, CreateCopyResultCallback(&watcher_created));
121 132
122 // Start watching changes in the temporary directory. 133 // Start watching changes in the temporary directory.
123 base::FilePath changed_path; 134 FileWatcher file_watcher(kVirtualPath);
124 bool watcher_created = false; 135 file_watcher.AddExtension(kExtensionId);
125 bool on_change_error = false; 136 file_watcher.WatchLocalFile(temp_dir.GetPath(), change_callback,
126 base::RunLoop run_loop; 137 start_callback);
127 file_watcher->WatchLocalFile( 138 start_run_loop.Run();
128 temp_dir.GetPath(),
129 CreateQuitCallback(
130 &run_loop, CreateCopyResultCallback(&changed_path, &on_change_error)),
131 CreateCopyResultCallback(&watcher_created));
132 // Spin the message loop so the base::FilePathWatcher is created.
133 base::RunLoop().RunUntilIdle();
134 ASSERT_TRUE(watcher_created); 139 ASSERT_TRUE(watcher_created);
135 140
136 // Create a temporary file in the temporary directory. The file watcher 141 // Create a temporary file in the temporary directory. The file watcher
137 // should detect the change in the directory. 142 // should detect the change in the directory.
138 base::FilePath temp_file_path; 143 base::FilePath temp_file_path;
139 ASSERT_TRUE( 144 ASSERT_TRUE(
140 base::CreateTemporaryFileInDir(temp_dir.GetPath(), &temp_file_path)); 145 base::CreateTemporaryFileInDir(temp_dir.GetPath(), &temp_file_path));
141 // Wait until the directory change is notified. 146 // Wait until the directory change is notified, and also flush the tasks in
142 run_loop.Run(); 147 // the message loop since |change_callback| can be called multiple times.
148 change_run_loop.Run();
149 base::RunLoop().RunUntilIdle();
150
143 ASSERT_FALSE(on_change_error); 151 ASSERT_FALSE(on_change_error);
144 ASSERT_EQ(temp_dir.GetPath().value(), changed_path.value()); 152 ASSERT_EQ(temp_dir.GetPath().value(), changed_path.value());
145
146 // This is ugly, but FileWatcher should be deleted explicitly here, and
147 // spin the message loop so the base::FilePathWatcher is deleted.
148 // Otherwise, base::FilePathWatcher may detect a change when the temporary
149 // directory is deleted, which may result in crash.
150 file_watcher.reset();
151 base::RunLoop().RunUntilIdle();
152 } 153 }
153 154
154 } // namespace 155 } // namespace
155 } // namespace file_manager. 156 } // namespace file_manager.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698