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