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"
17 void GetStatus(bool* done
,
18 base::PlatformFileError
*status_out
,
19 base::PlatformFileError status
) {
25 void GetCancelStatus(bool* operation_done
,
27 base::PlatformFileError
*status_out
,
28 base::PlatformFileError status
) {
29 // Cancel callback must be always called after the operation's callback.
30 ASSERT_TRUE(*operation_done
);
31 ASSERT_FALSE(*cancel_done
);
36 class FileSystemOperationRunnerTest
: public testing::Test
{
38 FileSystemOperationRunnerTest() {}
39 virtual ~FileSystemOperationRunnerTest() {}
41 virtual void SetUp() OVERRIDE
{
42 ASSERT_TRUE(base_
.CreateUniqueTempDir());
43 base::FilePath base_dir
= base_
.path();
44 file_system_context_
=
45 CreateFileSystemContextForTesting(NULL
, base_dir
);
48 virtual void TearDown() OVERRIDE
{
49 file_system_context_
= NULL
;
50 base::RunLoop().RunUntilIdle();
53 FileSystemURL
URL(const std::string
& path
) {
54 return file_system_context_
->CreateCrackedFileSystemURL(
55 GURL("http://example.com"), kFileSystemTypeTemporary
,
56 base::FilePath::FromUTF8Unsafe(path
));
59 FileSystemOperationRunner
* operation_runner() {
60 return file_system_context_
->operation_runner();
64 base::ScopedTempDir base_
;
65 base::MessageLoop message_loop_
;
66 scoped_refptr
<FileSystemContext
> file_system_context_
;
68 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationRunnerTest
);
71 TEST_F(FileSystemOperationRunnerTest
, NotFoundError
) {
73 base::PlatformFileError status
= base::PLATFORM_FILE_ERROR_FAILED
;
75 // Regular NOT_FOUND error, which is called asynchronously.
76 operation_runner()->Truncate(URL("foo"), 0,
77 base::Bind(&GetStatus
, &done
, &status
));
79 base::RunLoop().RunUntilIdle();
81 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND
, status
);
84 TEST_F(FileSystemOperationRunnerTest
, InvalidURLError
) {
86 base::PlatformFileError status
= base::PLATFORM_FILE_ERROR_FAILED
;
88 // Invalid URL error, which calls DidFinish synchronously.
89 operation_runner()->Truncate(FileSystemURL(), 0,
90 base::Bind(&GetStatus
, &done
, &status
));
91 // The error call back shouldn't be fired synchronously.
94 base::RunLoop().RunUntilIdle();
96 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_URL
, status
);
99 TEST_F(FileSystemOperationRunnerTest
, NotFoundErrorAndCancel
) {
101 bool cancel_done
= false;
102 base::PlatformFileError status
= base::PLATFORM_FILE_ERROR_FAILED
;
103 base::PlatformFileError cancel_status
= base::PLATFORM_FILE_ERROR_FAILED
;
105 // Call Truncate with non-existent URL, and try to cancel it immediately
106 // after that (before its callback is fired).
107 FileSystemOperationRunner::OperationID id
=
108 operation_runner()->Truncate(URL("foo"), 0,
109 base::Bind(&GetStatus
, &done
, &status
));
110 operation_runner()->Cancel(id
, base::Bind(&GetCancelStatus
,
115 ASSERT_FALSE(cancel_done
);
116 base::RunLoop().RunUntilIdle();
119 ASSERT_TRUE(cancel_done
);
120 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND
, status
);
121 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION
, cancel_status
);
124 TEST_F(FileSystemOperationRunnerTest
, InvalidURLErrorAndCancel
) {
126 bool cancel_done
= false;
127 base::PlatformFileError status
= base::PLATFORM_FILE_ERROR_FAILED
;
128 base::PlatformFileError cancel_status
= base::PLATFORM_FILE_ERROR_FAILED
;
130 // Call Truncate with invalid URL, and try to cancel it immediately
131 // after that (before its callback is fired).
132 FileSystemOperationRunner::OperationID id
=
133 operation_runner()->Truncate(FileSystemURL(), 0,
134 base::Bind(&GetStatus
, &done
, &status
));
135 operation_runner()->Cancel(id
, base::Bind(&GetCancelStatus
,
140 ASSERT_FALSE(cancel_done
);
141 base::RunLoop().RunUntilIdle();
144 ASSERT_TRUE(cancel_done
);
145 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_URL
, status
);
146 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION
, cancel_status
);
149 TEST_F(FileSystemOperationRunnerTest
, CancelWithInvalidId
) {
150 const FileSystemOperationRunner::OperationID kInvalidId
= -1;
151 bool done
= true; // The operation is not running.
152 bool cancel_done
= false;
153 base::PlatformFileError cancel_status
= base::PLATFORM_FILE_ERROR_FAILED
;
154 operation_runner()->Cancel(kInvalidId
, base::Bind(&GetCancelStatus
,
158 ASSERT_TRUE(cancel_done
);
159 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION
, cancel_status
);
162 } // namespace fileapi