[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / fileapi / file_system_operation_runner_unittest.cc
blob075ba2130ff2e735f84f87ec35ced8089ae84045
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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/files/file_path.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/threading/thread_restrictions.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/mock_special_storage_policy.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/public/test/test_file_system_context.h"
16 #include "content/public/test/test_file_system_options.h"
17 #include "storage/browser/fileapi/external_mount_points.h"
18 #include "storage/browser/fileapi/file_system_backend.h"
19 #include "storage/browser/fileapi/file_system_context.h"
20 #include "storage/browser/fileapi/file_system_operation_runner.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using storage::FileSystemContext;
24 using storage::FileSystemOperationRunner;
25 using storage::FileSystemType;
26 using storage::FileSystemURL;
28 namespace content {
30 namespace {
32 void GetStatus(bool* done,
33 base::File::Error *status_out,
34 base::File::Error status) {
35 ASSERT_FALSE(*done);
36 *done = true;
37 *status_out = status;
40 void GetCancelStatus(bool* operation_done,
41 bool* cancel_done,
42 base::File::Error *status_out,
43 base::File::Error status) {
44 // Cancel callback must be always called after the operation's callback.
45 ASSERT_TRUE(*operation_done);
46 ASSERT_FALSE(*cancel_done);
47 *cancel_done = true;
48 *status_out = status;
51 void DidOpenFile(base::File file, const base::Closure& on_close_callback) {}
53 } // namespace
55 class FileSystemOperationRunnerTest : public testing::Test {
56 protected:
57 FileSystemOperationRunnerTest() {}
58 ~FileSystemOperationRunnerTest() override {}
60 void SetUp() override {
61 ASSERT_TRUE(base_.CreateUniqueTempDir());
62 base::FilePath base_dir = base_.path();
63 file_system_context_ = CreateFileSystemContextForTesting(nullptr, base_dir);
66 void TearDown() override {
67 file_system_context_ = nullptr;
68 base::RunLoop().RunUntilIdle();
71 FileSystemURL URL(const std::string& path) {
72 return file_system_context_->CreateCrackedFileSystemURL(
73 GURL("http://example.com"),
74 storage::kFileSystemTypeTemporary,
75 base::FilePath::FromUTF8Unsafe(path));
78 FileSystemOperationRunner* operation_runner() {
79 return file_system_context_->operation_runner();
82 private:
83 base::ScopedTempDir base_;
84 base::MessageLoop message_loop_;
85 scoped_refptr<FileSystemContext> file_system_context_;
87 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationRunnerTest);
90 TEST_F(FileSystemOperationRunnerTest, NotFoundError) {
91 bool done = false;
92 base::File::Error status = base::File::FILE_ERROR_FAILED;
94 // Regular NOT_FOUND error, which is called asynchronously.
95 operation_runner()->Truncate(URL("foo"), 0,
96 base::Bind(&GetStatus, &done, &status));
97 ASSERT_FALSE(done);
98 base::RunLoop().RunUntilIdle();
99 ASSERT_TRUE(done);
100 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, status);
103 TEST_F(FileSystemOperationRunnerTest, InvalidURLError) {
104 bool done = false;
105 base::File::Error status = base::File::FILE_ERROR_FAILED;
107 // Invalid URL error, which calls DidFinish synchronously.
108 operation_runner()->Truncate(FileSystemURL(), 0,
109 base::Bind(&GetStatus, &done, &status));
110 // The error call back shouldn't be fired synchronously.
111 ASSERT_FALSE(done);
113 base::RunLoop().RunUntilIdle();
114 ASSERT_TRUE(done);
115 ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL, status);
118 TEST_F(FileSystemOperationRunnerTest, NotFoundErrorAndCancel) {
119 bool done = false;
120 bool cancel_done = false;
121 base::File::Error status = base::File::FILE_ERROR_FAILED;
122 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
124 // Call Truncate with non-existent URL, and try to cancel it immediately
125 // after that (before its callback is fired).
126 FileSystemOperationRunner::OperationID id =
127 operation_runner()->Truncate(URL("foo"), 0,
128 base::Bind(&GetStatus, &done, &status));
129 operation_runner()->Cancel(id, base::Bind(&GetCancelStatus,
130 &done, &cancel_done,
131 &cancel_status));
133 ASSERT_FALSE(done);
134 ASSERT_FALSE(cancel_done);
135 base::RunLoop().RunUntilIdle();
137 ASSERT_TRUE(done);
138 ASSERT_TRUE(cancel_done);
139 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, status);
140 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
143 TEST_F(FileSystemOperationRunnerTest, InvalidURLErrorAndCancel) {
144 bool done = false;
145 bool cancel_done = false;
146 base::File::Error status = base::File::FILE_ERROR_FAILED;
147 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
149 // Call Truncate with invalid URL, and try to cancel it immediately
150 // after that (before its callback is fired).
151 FileSystemOperationRunner::OperationID id =
152 operation_runner()->Truncate(FileSystemURL(), 0,
153 base::Bind(&GetStatus, &done, &status));
154 operation_runner()->Cancel(id, base::Bind(&GetCancelStatus,
155 &done, &cancel_done,
156 &cancel_status));
158 ASSERT_FALSE(done);
159 ASSERT_FALSE(cancel_done);
160 base::RunLoop().RunUntilIdle();
162 ASSERT_TRUE(done);
163 ASSERT_TRUE(cancel_done);
164 ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL, status);
165 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
168 TEST_F(FileSystemOperationRunnerTest, CancelWithInvalidId) {
169 const FileSystemOperationRunner::OperationID kInvalidId = -1;
170 bool done = true; // The operation is not running.
171 bool cancel_done = false;
172 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
173 operation_runner()->Cancel(kInvalidId, base::Bind(&GetCancelStatus,
174 &done, &cancel_done,
175 &cancel_status));
177 ASSERT_TRUE(cancel_done);
178 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
181 class MultiThreadFileSystemOperationRunnerTest : public testing::Test {
182 public:
183 MultiThreadFileSystemOperationRunnerTest()
184 : thread_bundle_(
185 content::TestBrowserThreadBundle::REAL_FILE_THREAD |
186 content::TestBrowserThreadBundle::IO_MAINLOOP) {}
188 void SetUp() override {
189 ASSERT_TRUE(base_.CreateUniqueTempDir());
191 base::FilePath base_dir = base_.path();
192 ScopedVector<storage::FileSystemBackend> additional_providers;
193 file_system_context_ = new FileSystemContext(
194 base::ThreadTaskRunnerHandle::Get().get(),
195 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(),
196 storage::ExternalMountPoints::CreateRefCounted().get(),
197 make_scoped_refptr(new MockSpecialStoragePolicy()).get(),
198 nullptr,
199 additional_providers.Pass(),
200 std::vector<storage::URLRequestAutoMountHandler>(),
201 base_dir,
202 CreateAllowFileAccessOptions());
204 // Disallow IO on the main loop.
205 base::ThreadRestrictions::SetIOAllowed(false);
208 void TearDown() override {
209 base::ThreadRestrictions::SetIOAllowed(true);
210 file_system_context_ = nullptr;
213 FileSystemURL URL(const std::string& path) {
214 return file_system_context_->CreateCrackedFileSystemURL(
215 GURL("http://example.com"),
216 storage::kFileSystemTypeTemporary,
217 base::FilePath::FromUTF8Unsafe(path));
220 FileSystemOperationRunner* operation_runner() {
221 return file_system_context_->operation_runner();
224 private:
225 content::TestBrowserThreadBundle thread_bundle_;
226 base::ScopedTempDir base_;
227 scoped_refptr<FileSystemContext> file_system_context_;
229 DISALLOW_COPY_AND_ASSIGN(MultiThreadFileSystemOperationRunnerTest);
232 TEST_F(MultiThreadFileSystemOperationRunnerTest, OpenAndShutdown) {
233 // Call OpenFile and immediately shutdown the runner.
234 operation_runner()->OpenFile(
235 URL("foo"),
236 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE,
237 base::Bind(&DidOpenFile));
238 operation_runner()->Shutdown();
240 // Wait until the task posted on FILE thread is done.
241 base::RunLoop run_loop;
242 BrowserThread::PostTaskAndReply(
243 BrowserThread::FILE, FROM_HERE,
244 base::Bind(&base::DoNothing),
245 run_loop.QuitClosure());
246 run_loop.Run();
247 // This should finish without thread assertion failure on debug build.
250 } // namespace content