| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <deque> | 5 #include <deque> |
| 6 #include <limits> | 6 #include <limits> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 class QuotaMockPluginDelegate : public MockPluginDelegate { | 30 class QuotaMockPluginDelegate : public MockPluginDelegate { |
| 31 public: | 31 public: |
| 32 typedef PluginDelegate::AvailableSpaceCallback Callback; | 32 typedef PluginDelegate::AvailableSpaceCallback Callback; |
| 33 | 33 |
| 34 QuotaMockPluginDelegate() | 34 QuotaMockPluginDelegate() |
| 35 : available_space_(0), | 35 : available_space_(0), |
| 36 will_update_count_(0), | 36 will_update_count_(0), |
| 37 file_thread_(MessageLoopProxy::current()), | 37 file_thread_(MessageLoopProxy::current()), |
| 38 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 38 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 39 } | 39 } |
| 40 virtual ~QuotaMockPluginDelegate() {} | 40 virtual ~QuotaMockPluginDelegate() {} |
| 41 | 41 |
| 42 virtual scoped_refptr<MessageLoopProxy> GetFileThreadMessageLoopProxy() { | 42 virtual scoped_refptr<MessageLoopProxy> GetFileThreadMessageLoopProxy() { |
| 43 return file_thread_; | 43 return file_thread_; |
| 44 } | 44 } |
| 45 | 45 |
| 46 virtual void QueryAvailableSpace( | 46 virtual void QueryAvailableSpace( |
| 47 const GURL& origin, | 47 const GURL& origin, |
| 48 quota::StorageType type, | 48 quota::StorageType type, |
| 49 const Callback& callback) OVERRIDE { | 49 const Callback& callback) OVERRIDE { |
| 50 DCHECK(!callback.is_null()); | 50 DCHECK_EQ(false, callback.is_null()); |
| 51 MessageLoopProxy::current()->PostTask( | 51 MessageLoopProxy::current()->PostTask( |
| 52 FROM_HERE, base::Bind( | 52 FROM_HERE, base::Bind( |
| 53 &QuotaMockPluginDelegate::RunAvailableSpaceCallback, | 53 &QuotaMockPluginDelegate::RunAvailableSpaceCallback, |
| 54 weak_ptr_factory_.GetWeakPtr(), | 54 weak_factory_.GetWeakPtr(), callback)); |
| 55 callback)); | |
| 56 } | 55 } |
| 57 | 56 |
| 58 virtual void WillUpdateFile(const GURL& file_path) OVERRIDE { | 57 virtual void WillUpdateFile(const GURL& file_path) OVERRIDE { |
| 59 file_path_ = file_path; | 58 file_path_ = file_path; |
| 60 ++will_update_count_; | 59 ++will_update_count_; |
| 61 } | 60 } |
| 62 | 61 |
| 63 virtual void DidUpdateFile(const GURL& file_path, int64_t delta) OVERRIDE { | 62 virtual void DidUpdateFile(const GURL& file_path, int64_t delta) OVERRIDE { |
| 64 ASSERT_EQ(file_path_, file_path); | 63 ASSERT_EQ(file_path_, file_path); |
| 65 ASSERT_GT(will_update_count_, 0); | 64 ASSERT_GT(will_update_count_, 0); |
| 66 --will_update_count_; | 65 --will_update_count_; |
| 67 available_space_ -= delta; | 66 available_space_ -= delta; |
| 68 } | 67 } |
| 69 | 68 |
| 70 void set_available_space(int64 available) { available_space_ = available; } | 69 void set_available_space(int64 available) { available_space_ = available; } |
| 71 int64_t available_space() const { return available_space_; } | 70 int64_t available_space() const { return available_space_; } |
| 72 | 71 |
| 73 private: | 72 private: |
| 74 void RunAvailableSpaceCallback(const Callback& callback) { | 73 void RunAvailableSpaceCallback(const Callback& callback) { |
| 75 callback.Run(available_space_); | 74 callback.Run(available_space_); |
| 76 } | 75 } |
| 77 | 76 |
| 78 int64_t available_space_; | 77 int64_t available_space_; |
| 79 int will_update_count_; | 78 int will_update_count_; |
| 80 GURL file_path_; | 79 GURL file_path_; |
| 81 scoped_refptr<MessageLoopProxy> file_thread_; | 80 scoped_refptr<MessageLoopProxy> file_thread_; |
| 82 base::WeakPtrFactory<QuotaMockPluginDelegate> weak_ptr_factory_; | 81 base::WeakPtrFactory<QuotaMockPluginDelegate> weak_factory_; |
| 83 }; | 82 }; |
| 84 } // namespace | 83 } // namespace |
| 85 | 84 |
| 86 class QuotaFileIOTest : public PpapiUnittest { | 85 class QuotaFileIOTest : public PpapiUnittest { |
| 87 public: | 86 public: |
| 88 QuotaFileIOTest() | 87 QuotaFileIOTest() |
| 89 : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | 88 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), |
| 89 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
| 90 | 90 |
| 91 virtual void SetUp() OVERRIDE { | 91 virtual void SetUp() OVERRIDE { |
| 92 PpapiUnittest::SetUp(); | 92 PpapiUnittest::SetUp(); |
| 93 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 93 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| 94 FilePath path; | 94 FilePath path; |
| 95 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(dir_.path(), &path)); | 95 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(dir_.path(), &path)); |
| 96 int file_flags = base::PLATFORM_FILE_OPEN | | 96 int file_flags = base::PLATFORM_FILE_OPEN | |
| 97 base::PLATFORM_FILE_READ | | 97 base::PLATFORM_FILE_READ | |
| 98 base::PLATFORM_FILE_WRITE | | 98 base::PLATFORM_FILE_WRITE | |
| 99 base::PLATFORM_FILE_WRITE_ATTRIBUTES; | 99 base::PLATFORM_FILE_WRITE_ATTRIBUTES; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 116 PpapiUnittest::TearDown(); | 116 PpapiUnittest::TearDown(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 protected: | 119 protected: |
| 120 virtual MockPluginDelegate* NewPluginDelegate() OVERRIDE { | 120 virtual MockPluginDelegate* NewPluginDelegate() OVERRIDE { |
| 121 return static_cast<MockPluginDelegate*>(new QuotaMockPluginDelegate); | 121 return static_cast<MockPluginDelegate*>(new QuotaMockPluginDelegate); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void WriteTestBody(bool will_operation) { | 124 void WriteTestBody(bool will_operation) { |
| 125 // Attempt to write zero bytes. | 125 // Attempt to write zero bytes. |
| 126 EXPECT_FALSE(quota_file_io_->Write(0, "data", 0, | 126 EXPECT_FALSE(quota_file_io_->Write( |
| 127 callback_factory_.NewCallback( | 127 0, "data", 0, |
| 128 &QuotaFileIOTest::DidWrite))); | 128 base::Bind(&QuotaFileIOTest::DidWrite, weak_factory_.GetWeakPtr()))); |
| 129 // Attempt to write negative number of bytes. | 129 // Attempt to write negative number of bytes. |
| 130 EXPECT_FALSE(quota_file_io_->Write(0, "data", | 130 EXPECT_FALSE(quota_file_io_->Write( |
| 131 std::numeric_limits<int32_t>::min(), | 131 0, "data", std::numeric_limits<int32_t>::min(), |
| 132 callback_factory_.NewCallback( | 132 base::Bind(&QuotaFileIOTest::DidWrite, weak_factory_.GetWeakPtr()))); |
| 133 &QuotaFileIOTest::DidWrite))); | |
| 134 | 133 |
| 135 quota_plugin_delegate()->set_available_space(100); | 134 quota_plugin_delegate()->set_available_space(100); |
| 136 std::string read_buffer; | 135 std::string read_buffer; |
| 137 | 136 |
| 138 // Write 8 bytes at offset 0 (-> length=8). | 137 // Write 8 bytes at offset 0 (-> length=8). |
| 139 std::string data("12345678"); | 138 std::string data("12345678"); |
| 140 Write(0, data, will_operation); | 139 Write(0, data, will_operation); |
| 141 MessageLoop::current()->RunAllPending(); | 140 MessageLoop::current()->RunAllPending(); |
| 142 ASSERT_EQ(1U, num_results()); | 141 ASSERT_EQ(1U, num_results()); |
| 143 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); | 142 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 reset_results(); | 347 reset_results(); |
| 349 } | 348 } |
| 350 | 349 |
| 351 QuotaMockPluginDelegate* quota_plugin_delegate() { | 350 QuotaMockPluginDelegate* quota_plugin_delegate() { |
| 352 return static_cast<QuotaMockPluginDelegate*>(delegate()); | 351 return static_cast<QuotaMockPluginDelegate*>(delegate()); |
| 353 } | 352 } |
| 354 | 353 |
| 355 void Write(int64_t offset, const std::string& data, bool will_operation) { | 354 void Write(int64_t offset, const std::string& data, bool will_operation) { |
| 356 if (will_operation) { | 355 if (will_operation) { |
| 357 ASSERT_TRUE(quota_file_io_->WillWrite( | 356 ASSERT_TRUE(quota_file_io_->WillWrite( |
| 358 offset, data.size(), | 357 offset, data.size(), |
| 359 callback_factory_.NewCallback( | 358 base::Bind(&QuotaFileIOTest::DidWrite, weak_factory_.GetWeakPtr()))); |
| 360 &QuotaFileIOTest::DidWrite))); | |
| 361 } else { | 359 } else { |
| 362 ASSERT_TRUE(quota_file_io_->Write( | 360 ASSERT_TRUE(quota_file_io_->Write( |
| 363 offset, data.c_str(), data.size(), | 361 offset, data.c_str(), data.size(), |
| 364 callback_factory_.NewCallback( | 362 base::Bind(&QuotaFileIOTest::DidWrite, weak_factory_.GetWeakPtr()))); |
| 365 &QuotaFileIOTest::DidWrite))); | |
| 366 } | 363 } |
| 367 } | 364 } |
| 368 | 365 |
| 369 void SetLength(int64_t length, bool will_operation) { | 366 void SetLength(int64_t length, bool will_operation) { |
| 370 if (will_operation) { | 367 if (will_operation) { |
| 371 ASSERT_TRUE(quota_file_io_->WillSetLength( | 368 ASSERT_TRUE(quota_file_io_->WillSetLength( |
| 372 length, | 369 length, |
| 373 callback_factory_.NewCallback( | 370 callback_factory_.NewCallback( |
| 374 &QuotaFileIOTest::DidSetLength))); | 371 &QuotaFileIOTest::DidSetLength))); |
| 375 } else { | 372 } else { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 EXPECT_TRUE(base::TruncatePlatformFile(file_, length)); | 424 EXPECT_TRUE(base::TruncatePlatformFile(file_, length)); |
| 428 } | 425 } |
| 429 | 426 |
| 430 private: | 427 private: |
| 431 ScopedTempDir dir_; | 428 ScopedTempDir dir_; |
| 432 PlatformFile file_; | 429 PlatformFile file_; |
| 433 scoped_ptr<QuotaFileIO> quota_file_io_; | 430 scoped_ptr<QuotaFileIO> quota_file_io_; |
| 434 std::deque<int> bytes_written_; | 431 std::deque<int> bytes_written_; |
| 435 std::deque<PlatformFileError> status_; | 432 std::deque<PlatformFileError> status_; |
| 436 base::ScopedCallbackFactory<QuotaFileIOTest> callback_factory_; | 433 base::ScopedCallbackFactory<QuotaFileIOTest> callback_factory_; |
| 434 base::WeakPtrFactory<QuotaFileIOTest> weak_factory_; |
| 437 }; | 435 }; |
| 438 | 436 |
| 439 TEST_F(QuotaFileIOTest, Write) { | 437 TEST_F(QuotaFileIOTest, Write) { |
| 440 WriteTestBody(false); | 438 WriteTestBody(false); |
| 441 } | 439 } |
| 442 | 440 |
| 443 TEST_F(QuotaFileIOTest, WillWrite) { | 441 TEST_F(QuotaFileIOTest, WillWrite) { |
| 444 WriteTestBody(true); | 442 WriteTestBody(true); |
| 445 } | 443 } |
| 446 | 444 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 pop_result(); | 494 pop_result(); |
| 497 | 495 |
| 498 EXPECT_EQ(22 - 15, quota_plugin_delegate()->available_space()); | 496 EXPECT_EQ(22 - 15, quota_plugin_delegate()->available_space()); |
| 499 EXPECT_EQ(15, GetPlatformFileSize()); | 497 EXPECT_EQ(15, GetPlatformFileSize()); |
| 500 ReadPlatformFile(&read_buffer); | 498 ReadPlatformFile(&read_buffer); |
| 501 EXPECT_EQ("123355559012345", read_buffer); | 499 EXPECT_EQ("123355559012345", read_buffer); |
| 502 } | 500 } |
| 503 | 501 |
| 504 } // namespace ppapi | 502 } // namespace ppapi |
| 505 } // namespace webkit | 503 } // namespace webkit |
| OLD | NEW |