Cast: Skip receiver log messages with time delta that can't be encoded.
[chromium-blink-merge.git] / content / browser / fileapi / file_system_operation_runner_unittest.cc
blob571ddd1b1682fde4547eff21ea752e6f24443f09
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;
20 namespace content {
22 void GetStatus(bool* done,
23 base::File::Error *status_out,
24 base::File::Error status) {
25 ASSERT_FALSE(*done);
26 *done = true;
27 *status_out = status;
30 void GetCancelStatus(bool* operation_done,
31 bool* cancel_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);
37 *cancel_done = true;
38 *status_out = status;
41 class FileSystemOperationRunnerTest : public testing::Test {
42 protected:
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();
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