| OLD | NEW |
| 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" |
| 25 #include "webkit/fileapi/file_system_context.h" | 26 #include "webkit/fileapi/file_system_context.h" |
| 26 #include "webkit/fileapi/file_system_operation.h" | 27 #include "webkit/fileapi/file_system_operation.h" |
| 27 #include "webkit/fileapi/file_system_operation_context.h" | 28 #include "webkit/fileapi/file_system_operation_context.h" |
| 28 #include "webkit/fileapi/file_system_test_helper.h" | 29 #include "webkit/fileapi/file_system_test_helper.h" |
| 29 #include "webkit/fileapi/file_system_usage_cache.h" | 30 #include "webkit/fileapi/file_system_usage_cache.h" |
| 30 #include "webkit/fileapi/file_writer_delegate.h" | 31 #include "webkit/fileapi/file_writer_delegate.h" |
| 31 #include "webkit/fileapi/quota_file_util.h" | 32 #include "webkit/fileapi/quota_file_util.h" |
| 32 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 33 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
| 33 | 34 |
| 34 namespace fileapi { | 35 namespace fileapi { |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 class Result { | 39 class Result { |
| 39 public: | 40 public: |
| 40 Result() | 41 Result() |
| 41 : status_(base::PLATFORM_FILE_OK), | 42 : status_(base::PLATFORM_FILE_OK), |
| 42 bytes_written_(0), | 43 bytes_written_(0), |
| 43 complete_(false) {} | 44 complete_(false) {} |
| 44 | 45 |
| 46 void set_failure_status(base::PlatformFileError status) { |
| 47 EXPECT_FALSE(complete_); |
| 48 EXPECT_EQ(status_, base::PLATFORM_FILE_OK); |
| 49 EXPECT_NE(status, base::PLATFORM_FILE_OK); |
| 50 complete_ = true; |
| 51 status_ = status; |
| 52 } |
| 45 base::PlatformFileError status() const { return status_; } | 53 base::PlatformFileError status() const { return status_; } |
| 46 void add_bytes_written(int64 bytes, bool complete) { | 54 void add_bytes_written(int64 bytes, bool complete) { |
| 47 bytes_written_ += bytes; | 55 bytes_written_ += bytes; |
| 48 EXPECT_FALSE(complete_); | 56 EXPECT_FALSE(complete_); |
| 49 complete_ = complete; | 57 complete_ = complete; |
| 50 } | 58 } |
| 51 int64 bytes_written() const { return bytes_written_; } | 59 int64 bytes_written() const { return bytes_written_; } |
| 52 bool complete() const { return complete_; } | 60 bool complete() const { return complete_; } |
| 53 | 61 |
| 54 void DidWrite(base::PlatformFileError status, int64 bytes, bool complete) { | |
| 55 if (status == base::PLATFORM_FILE_OK) { | |
| 56 add_bytes_written(bytes, complete); | |
| 57 if (complete) | |
| 58 MessageLoop::current()->Quit(); | |
| 59 } else { | |
| 60 EXPECT_FALSE(complete_); | |
| 61 EXPECT_EQ(status_, base::PLATFORM_FILE_OK); | |
| 62 complete_ = true; | |
| 63 status_ = status; | |
| 64 MessageLoop::current()->Quit(); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 private: | 62 private: |
| 69 // For post-operation status. | 63 // For post-operation status. |
| 70 base::PlatformFileError status_; | 64 base::PlatformFileError status_; |
| 71 int64 bytes_written_; | 65 int64 bytes_written_; |
| 72 bool complete_; | 66 bool complete_; |
| 73 }; | 67 }; |
| 74 | 68 |
| 75 const char kData[] = "The quick brown fox jumps over the lazy dog.\n"; | 69 const char kData[] = "The quick brown fox jumps over the lazy dog.\n"; |
| 76 const int kDataSize = ARRAYSIZE_UNSAFE(kData) - 1; | 70 const int kDataSize = ARRAYSIZE_UNSAFE(kData) - 1; |
| 77 | 71 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 virtual int GetResponseCode() const OVERRIDE { | 168 virtual int GetResponseCode() const OVERRIDE { |
| 175 return 200; | 169 return 200; |
| 176 } | 170 } |
| 177 | 171 |
| 178 private: | 172 private: |
| 179 std::string content_; | 173 std::string content_; |
| 180 int remaining_bytes_; | 174 int remaining_bytes_; |
| 181 int cursor_; | 175 int cursor_; |
| 182 }; | 176 }; |
| 183 | 177 |
| 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 |
| 184 } // namespace (anonymous) | 217 } // namespace (anonymous) |
| 185 | 218 |
| 186 // static | 219 // static |
| 187 net::URLRequestJob* FileWriterDelegateTest::Factory( | 220 net::URLRequestJob* FileWriterDelegateTest::Factory( |
| 188 net::URLRequest* request, | 221 net::URLRequest* request, |
| 189 const std::string& scheme) { | 222 const std::string& scheme) { |
| 190 return new FileWriterDelegateTestJob( | 223 return new FileWriterDelegateTestJob( |
| 191 request, FileWriterDelegateTest::content_); | 224 request, FileWriterDelegateTest::content_); |
| 192 } | 225 } |
| 193 | 226 |
| 194 void FileWriterDelegateTest::SetUp() { | 227 void FileWriterDelegateTest::SetUp() { |
| 195 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 228 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| 196 FilePath base_dir = dir_.path().AppendASCII("filesystem"); | 229 FilePath base_dir = dir_.path().AppendASCII("filesystem"); |
| 197 SetUpTestHelper(base_dir); | 230 SetUpTestHelper(base_dir); |
| 198 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( | 231 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( |
| 199 test_helper_.GetOriginRootPath(), &file_path_)); | 232 test_helper_.GetOriginRootPath(), &file_path_)); |
| 200 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", &Factory); | 233 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", &Factory); |
| 201 } | 234 } |
| 202 | 235 |
| 203 void FileWriterDelegateTest::TearDown() { | 236 void FileWriterDelegateTest::TearDown() { |
| 204 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL); | 237 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL); |
| 205 base::ClosePlatformFile(file_); | 238 base::ClosePlatformFile(file_); |
| 206 test_helper_.TearDown(); | 239 test_helper_.TearDown(); |
| 207 } | 240 } |
| 208 | 241 |
| 209 FileSystemOperation* FileWriterDelegateTest::CreateNewOperation( | 242 FileSystemOperation* FileWriterDelegateTest::CreateNewOperation( |
| 210 Result* result, int64 quota) { | 243 Result* result, int64 quota) { |
| 211 FileSystemOperation* operation = test_helper_.NewOperation(); | 244 FileSystemOperation* operation = test_helper_.NewOperation( |
| 212 operation->set_write_callback(base::Bind(&Result::DidWrite, | 245 new MockDispatcher(result)); |
| 213 base::Unretained(result))); | |
| 214 FileSystemOperationContext* context = | 246 FileSystemOperationContext* context = |
| 215 operation->file_system_operation_context(); | 247 operation->file_system_operation_context(); |
| 216 context->set_allowed_bytes_growth(quota); | 248 context->set_allowed_bytes_growth(quota); |
| 217 return operation; | 249 return operation; |
| 218 } | 250 } |
| 219 | 251 |
| 220 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { | 252 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { |
| 221 const GURL kBlobURL("blob:nolimit"); | 253 const GURL kBlobURL("blob:nolimit"); |
| 222 content_ = kData; | 254 content_ = kData; |
| 223 | 255 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); | 455 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); |
| 424 EXPECT_TRUE(result_->complete()); | 456 EXPECT_TRUE(result_->complete()); |
| 425 } | 457 } |
| 426 | 458 |
| 427 class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest { | 459 class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest { |
| 428 protected: | 460 protected: |
| 429 virtual void SetUpTestHelper(const FilePath& path) OVERRIDE; | 461 virtual void SetUpTestHelper(const FilePath& path) OVERRIDE; |
| 430 }; | 462 }; |
| 431 | 463 |
| 432 } // namespace fileapi | 464 } // namespace fileapi |
| OLD | NEW |