Standardize usage of virtual/override/final specifiers.
[chromium-blink-merge.git] / content / browser / fileapi / file_system_operation_runner_unittest.cc
blob12ba010a31803a0beae96c9924bf5e94023bc5f2
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/run_loop.h"
9 #include "content/public/test/test_file_system_context.h"
10 #include "storage/browser/fileapi/file_system_context.h"
11 #include "storage/browser/fileapi/file_system_operation_runner.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using storage::FileSystemContext;
15 using storage::FileSystemOperationRunner;
16 using storage::FileSystemType;
17 using storage::FileSystemURL;
19 namespace content {
21 void GetStatus(bool* done,
22 base::File::Error *status_out,
23 base::File::Error status) {
24 ASSERT_FALSE(*done);
25 *done = true;
26 *status_out = status;
29 void GetCancelStatus(bool* operation_done,
30 bool* cancel_done,
31 base::File::Error *status_out,
32 base::File::Error status) {
33 // Cancel callback must be always called after the operation's callback.
34 ASSERT_TRUE(*operation_done);
35 ASSERT_FALSE(*cancel_done);
36 *cancel_done = true;
37 *status_out = status;
40 class FileSystemOperationRunnerTest : public testing::Test {
41 protected:
42 FileSystemOperationRunnerTest() {}
43 ~FileSystemOperationRunnerTest() override {}
45 void SetUp() override {
46 ASSERT_TRUE(base_.CreateUniqueTempDir());
47 base::FilePath base_dir = base_.path();
48 file_system_context_ =
49 CreateFileSystemContextForTesting(NULL, base_dir);
52 void TearDown() override {
53 file_system_context_ = NULL;
54 base::RunLoop().RunUntilIdle();
57 FileSystemURL URL(const std::string& path) {
58 return file_system_context_->CreateCrackedFileSystemURL(
59 GURL("http://example.com"),
60 storage::kFileSystemTypeTemporary,
61 base::FilePath::FromUTF8Unsafe(path));
64 FileSystemOperationRunner* operation_runner() {
65 return file_system_context_->operation_runner();
68 private:
69 base::ScopedTempDir base_;
70 base::MessageLoop message_loop_;
71 scoped_refptr<FileSystemContext> file_system_context_;
73 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationRunnerTest);
76 TEST_F(FileSystemOperationRunnerTest, NotFoundError) {
77 bool done = false;
78 base::File::Error status = base::File::FILE_ERROR_FAILED;
80 // Regular NOT_FOUND error, which is called asynchronously.
81 operation_runner()->Truncate(URL("foo"), 0,
82 base::Bind(&GetStatus, &done, &status));
83 ASSERT_FALSE(done);
84 base::RunLoop().RunUntilIdle();
85 ASSERT_TRUE(done);
86 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, status);
89 TEST_F(FileSystemOperationRunnerTest, InvalidURLError) {
90 bool done = false;
91 base::File::Error status = base::File::FILE_ERROR_FAILED;
93 // Invalid URL error, which calls DidFinish synchronously.
94 operation_runner()->Truncate(FileSystemURL(), 0,
95 base::Bind(&GetStatus, &done, &status));
96 // The error call back shouldn't be fired synchronously.
97 ASSERT_FALSE(done);
99 base::RunLoop().RunUntilIdle();
100 ASSERT_TRUE(done);
101 ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL, status);
104 TEST_F(FileSystemOperationRunnerTest, NotFoundErrorAndCancel) {
105 bool done = false;
106 bool cancel_done = false;
107 base::File::Error status = base::File::FILE_ERROR_FAILED;
108 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
110 // Call Truncate with non-existent URL, and try to cancel it immediately
111 // after that (before its callback is fired).
112 FileSystemOperationRunner::OperationID id =
113 operation_runner()->Truncate(URL("foo"), 0,
114 base::Bind(&GetStatus, &done, &status));
115 operation_runner()->Cancel(id, base::Bind(&GetCancelStatus,
116 &done, &cancel_done,
117 &cancel_status));
119 ASSERT_FALSE(done);
120 ASSERT_FALSE(cancel_done);
121 base::RunLoop().RunUntilIdle();
123 ASSERT_TRUE(done);
124 ASSERT_TRUE(cancel_done);
125 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, status);
126 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
129 TEST_F(FileSystemOperationRunnerTest, InvalidURLErrorAndCancel) {
130 bool done = false;
131 bool cancel_done = false;
132 base::File::Error status = base::File::FILE_ERROR_FAILED;
133 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
135 // Call Truncate with invalid URL, and try to cancel it immediately
136 // after that (before its callback is fired).
137 FileSystemOperationRunner::OperationID id =
138 operation_runner()->Truncate(FileSystemURL(), 0,
139 base::Bind(&GetStatus, &done, &status));
140 operation_runner()->Cancel(id, base::Bind(&GetCancelStatus,
141 &done, &cancel_done,
142 &cancel_status));
144 ASSERT_FALSE(done);
145 ASSERT_FALSE(cancel_done);
146 base::RunLoop().RunUntilIdle();
148 ASSERT_TRUE(done);
149 ASSERT_TRUE(cancel_done);
150 ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL, status);
151 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
154 TEST_F(FileSystemOperationRunnerTest, CancelWithInvalidId) {
155 const FileSystemOperationRunner::OperationID kInvalidId = -1;
156 bool done = true; // The operation is not running.
157 bool cancel_done = false;
158 base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
159 operation_runner()->Cancel(kInvalidId, base::Bind(&GetCancelStatus,
160 &done, &cancel_done,
161 &cancel_status));
163 ASSERT_TRUE(cancel_done);
164 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
167 } // namespace content