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/platform_file.h"
9 #include "base/run_loop.h"
10 #include "content/public/test/test_file_system_context.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webkit/browser/fileapi/file_system_context.h"
13 #include "webkit/browser/fileapi/file_system_operation_runner.h"
15 using fileapi::FileSystemContext
;
16 using fileapi::FileSystemOperationRunner
;
17 using fileapi::FileSystemType
;
18 using fileapi::FileSystemURL
;
22 void GetStatus(bool* done
,
23 base::File::Error
*status_out
,
24 base::File::Error status
) {
30 void GetCancelStatus(bool* operation_done
,
32 base::File::Error
*status_out
,
33 base::File::Error status
) {
34 // Cancel callback must be always called after the operation's callback.
35 ASSERT_TRUE(*operation_done
);
36 ASSERT_FALSE(*cancel_done
);
41 class FileSystemOperationRunnerTest
: public testing::Test
{
43 FileSystemOperationRunnerTest() {}
44 virtual ~FileSystemOperationRunnerTest() {}
46 virtual void SetUp() OVERRIDE
{
47 ASSERT_TRUE(base_
.CreateUniqueTempDir());
48 base::FilePath base_dir
= base_
.path();
49 file_system_context_
=
50 CreateFileSystemContextForTesting(NULL
, base_dir
);
53 virtual void TearDown() OVERRIDE
{
54 file_system_context_
= NULL
;
55 base::RunLoop().RunUntilIdle();
58 FileSystemURL
URL(const std::string
& path
) {
59 return file_system_context_
->CreateCrackedFileSystemURL(
60 GURL("http://example.com"), fileapi::kFileSystemTypeTemporary
,
61 base::FilePath::FromUTF8Unsafe(path
));
64 FileSystemOperationRunner
* operation_runner() {
65 return file_system_context_
->operation_runner();
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
) {
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
));
84 base::RunLoop().RunUntilIdle();
86 ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND
, status
);
89 TEST_F(FileSystemOperationRunnerTest
, InvalidURLError
) {
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.
99 base::RunLoop().RunUntilIdle();
101 ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL
, status
);
104 TEST_F(FileSystemOperationRunnerTest
, NotFoundErrorAndCancel
) {
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
,
120 ASSERT_FALSE(cancel_done
);
121 base::RunLoop().RunUntilIdle();
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
) {
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
,
145 ASSERT_FALSE(cancel_done
);
146 base::RunLoop().RunUntilIdle();
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
,
163 ASSERT_TRUE(cancel_done
);
164 ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION
, cancel_status
);
167 } // namespace content