Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: webkit/fileapi/file_writer_delegate_unittest.cc

Issue 9372044: Refactor FileSystemOperation to take callback for each method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) 5 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit)
6 // rather than as part of test_shell_tests because they rely on being able 6 // rather than as part of test_shell_tests because they rely on being able
7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses
8 // TYPE_UI, which URLRequest doesn't allow. 8 // TYPE_UI, which URLRequest doesn't allow.
9 // 9 //
10 10
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/basictypes.h" 14 #include "base/basictypes.h"
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/file_util_proxy.h" 16 #include "base/file_util_proxy.h"
17 #include "base/message_loop.h" 17 #include "base/message_loop.h"
18 #include "base/scoped_temp_dir.h" 18 #include "base/scoped_temp_dir.h"
19 #include "googleurl/src/gurl.h" 19 #include "googleurl/src/gurl.h"
20 #include "net/base/io_buffer.h" 20 #include "net/base/io_buffer.h"
21 #include "net/url_request/url_request.h" 21 #include "net/url_request/url_request.h"
22 #include "net/url_request/url_request_job.h" 22 #include "net/url_request/url_request_job.h"
23 #include "net/url_request/url_request_status.h" 23 #include "net/url_request/url_request_status.h"
24 #include "testing/platform_test.h" 24 #include "testing/platform_test.h"
25 #include "webkit/fileapi/file_system_callback_dispatcher.h"
26 #include "webkit/fileapi/file_system_context.h" 25 #include "webkit/fileapi/file_system_context.h"
27 #include "webkit/fileapi/file_system_operation.h" 26 #include "webkit/fileapi/file_system_operation.h"
28 #include "webkit/fileapi/file_system_operation_context.h" 27 #include "webkit/fileapi/file_system_operation_context.h"
29 #include "webkit/fileapi/file_system_test_helper.h" 28 #include "webkit/fileapi/file_system_test_helper.h"
30 #include "webkit/fileapi/file_system_usage_cache.h" 29 #include "webkit/fileapi/file_system_usage_cache.h"
31 #include "webkit/fileapi/file_writer_delegate.h" 30 #include "webkit/fileapi/file_writer_delegate.h"
32 #include "webkit/fileapi/quota_file_util.h" 31 #include "webkit/fileapi/quota_file_util.h"
33 #include "webkit/fileapi/sandbox_mount_point_provider.h" 32 #include "webkit/fileapi/sandbox_mount_point_provider.h"
34 33
35 namespace fileapi { 34 namespace fileapi {
(...skipping 16 matching lines...) Expand all
52 } 51 }
53 base::PlatformFileError status() const { return status_; } 52 base::PlatformFileError status() const { return status_; }
54 void add_bytes_written(int64 bytes, bool complete) { 53 void add_bytes_written(int64 bytes, bool complete) {
55 bytes_written_ += bytes; 54 bytes_written_ += bytes;
56 EXPECT_FALSE(complete_); 55 EXPECT_FALSE(complete_);
57 complete_ = complete; 56 complete_ = complete;
58 } 57 }
59 int64 bytes_written() const { return bytes_written_; } 58 int64 bytes_written() const { return bytes_written_; }
60 bool complete() const { return complete_; } 59 bool complete() const { return complete_; }
61 60
61 void DidWrite(base::PlatformFileError status, int64 bytes, bool complete) {
62 if (status == base::PLATFORM_FILE_OK) {
63 add_bytes_written(bytes, complete);
64 if (complete)
65 MessageLoop::current()->Quit();
66 } else {
67 set_failure_status(status);
68 MessageLoop::current()->Quit();
69 }
70 }
71
62 private: 72 private:
63 // For post-operation status. 73 // For post-operation status.
64 base::PlatformFileError status_; 74 base::PlatformFileError status_;
65 int64 bytes_written_; 75 int64 bytes_written_;
66 bool complete_; 76 bool complete_;
67 }; 77 };
68 78
69 const char kData[] = "The quick brown fox jumps over the lazy dog.\n"; 79 const char kData[] = "The quick brown fox jumps over the lazy dog.\n";
70 const int kDataSize = ARRAYSIZE_UNSAFE(kData) - 1; 80 const int kDataSize = ARRAYSIZE_UNSAFE(kData) - 1;
71 81
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 virtual int GetResponseCode() const OVERRIDE { 178 virtual int GetResponseCode() const OVERRIDE {
169 return 200; 179 return 200;
170 } 180 }
171 181
172 private: 182 private:
173 std::string content_; 183 std::string content_;
174 int remaining_bytes_; 184 int remaining_bytes_;
175 int cursor_; 185 int cursor_;
176 }; 186 };
177 187
178 class MockDispatcher : public FileSystemCallbackDispatcher {
179 public:
180 explicit MockDispatcher(Result* result) : result_(result) {}
181
182 virtual void DidFail(base::PlatformFileError status) {
183 result_->set_failure_status(status);
184 MessageLoop::current()->Quit();
185 }
186
187 virtual void DidSucceed() {
188 ADD_FAILURE();
189 }
190
191 virtual void DidReadMetadata(
192 const base::PlatformFileInfo& info,
193 const FilePath& platform_path) {
194 ADD_FAILURE();
195 }
196
197 virtual void DidReadDirectory(
198 const std::vector<base::FileUtilProxy::Entry>& entries,
199 bool /* has_more */) {
200 ADD_FAILURE();
201 }
202
203 virtual void DidOpenFileSystem(const std::string&, const GURL&) {
204 ADD_FAILURE();
205 }
206
207 virtual void DidWrite(int64 bytes, bool complete) {
208 result_->add_bytes_written(bytes, complete);
209 if (complete)
210 MessageLoop::current()->Quit();
211 }
212
213 private:
214 Result* result_;
215 };
216
217 } // namespace (anonymous) 188 } // namespace (anonymous)
218 189
219 // static 190 // static
220 net::URLRequestJob* FileWriterDelegateTest::Factory( 191 net::URLRequestJob* FileWriterDelegateTest::Factory(
221 net::URLRequest* request, 192 net::URLRequest* request,
222 const std::string& scheme) { 193 const std::string& scheme) {
223 return new FileWriterDelegateTestJob( 194 return new FileWriterDelegateTestJob(
224 request, FileWriterDelegateTest::content_); 195 request, FileWriterDelegateTest::content_);
225 } 196 }
226 197
227 void FileWriterDelegateTest::SetUp() { 198 void FileWriterDelegateTest::SetUp() {
228 ASSERT_TRUE(dir_.CreateUniqueTempDir()); 199 ASSERT_TRUE(dir_.CreateUniqueTempDir());
229 FilePath base_dir = dir_.path().AppendASCII("filesystem"); 200 FilePath base_dir = dir_.path().AppendASCII("filesystem");
230 SetUpTestHelper(base_dir); 201 SetUpTestHelper(base_dir);
231 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( 202 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
232 test_helper_.GetOriginRootPath(), &file_path_)); 203 test_helper_.GetOriginRootPath(), &file_path_));
233 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", &Factory); 204 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", &Factory);
234 } 205 }
235 206
236 void FileWriterDelegateTest::TearDown() { 207 void FileWriterDelegateTest::TearDown() {
237 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL); 208 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL);
238 base::ClosePlatformFile(file_); 209 base::ClosePlatformFile(file_);
239 test_helper_.TearDown(); 210 test_helper_.TearDown();
240 } 211 }
241 212
242 FileSystemOperation* FileWriterDelegateTest::CreateNewOperation( 213 FileSystemOperation* FileWriterDelegateTest::CreateNewOperation(
243 Result* result, int64 quota) { 214 Result* result, int64 quota) {
244 FileSystemOperation* operation = test_helper_.NewOperation( 215 FileSystemOperation* operation = test_helper_.NewOperation();
245 new MockDispatcher(result)); 216 operation->set_write_callback(base::Bind(&Result::DidWrite,
217 base::Unretained(result)));
246 FileSystemOperationContext* context = 218 FileSystemOperationContext* context =
247 operation->file_system_operation_context(); 219 operation->file_system_operation_context();
248 context->set_allowed_bytes_growth(quota); 220 context->set_allowed_bytes_growth(quota);
249 return operation; 221 return operation;
250 } 222 }
251 223
252 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { 224 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) {
253 const GURL kBlobURL("blob:nolimit"); 225 const GURL kBlobURL("blob:nolimit");
254 content_ = kData; 226 content_ = kData;
255 227
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); 427 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status());
456 EXPECT_TRUE(result_->complete()); 428 EXPECT_TRUE(result_->complete());
457 } 429 }
458 430
459 class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest { 431 class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest {
460 protected: 432 protected:
461 virtual void SetUpTestHelper(const FilePath& path) OVERRIDE; 433 virtual void SetUpTestHelper(const FilePath& path) OVERRIDE;
462 }; 434 };
463 435
464 } // namespace fileapi 436 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698