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" |
11 #include "base/memory/scoped_callback_factory.h" | |
12 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
14 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
15 #include "base/scoped_temp_dir.h" | 14 #include "base/scoped_temp_dir.h" |
16 #include "base/task.h" | 15 #include "base/task.h" |
17 #include "webkit/plugins/ppapi/mock_plugin_delegate.h" | 16 #include "webkit/plugins/ppapi/mock_plugin_delegate.h" |
18 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 17 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
19 #include "webkit/plugins/ppapi/ppapi_unittest.h" | 18 #include "webkit/plugins/ppapi/ppapi_unittest.h" |
20 #include "webkit/plugins/ppapi/quota_file_io.h" | 19 #include "webkit/plugins/ppapi/quota_file_io.h" |
21 | 20 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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_ptr_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 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
90 | 89 |
91 virtual void SetUp() OVERRIDE { | 90 virtual void SetUp() OVERRIDE { |
92 PpapiUnittest::SetUp(); | 91 PpapiUnittest::SetUp(); |
93 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 92 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
94 FilePath path; | 93 FilePath path; |
95 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(dir_.path(), &path)); | 94 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(dir_.path(), &path)); |
96 int file_flags = base::PLATFORM_FILE_OPEN | | 95 int file_flags = base::PLATFORM_FILE_OPEN | |
97 base::PLATFORM_FILE_READ | | 96 base::PLATFORM_FILE_READ | |
98 base::PLATFORM_FILE_WRITE | | 97 base::PLATFORM_FILE_WRITE | |
99 base::PLATFORM_FILE_WRITE_ATTRIBUTES; | 98 base::PLATFORM_FILE_WRITE_ATTRIBUTES; |
(...skipping 16 matching lines...) Expand all Loading... |
116 PpapiUnittest::TearDown(); | 115 PpapiUnittest::TearDown(); |
117 } | 116 } |
118 | 117 |
119 protected: | 118 protected: |
120 virtual MockPluginDelegate* NewPluginDelegate() OVERRIDE { | 119 virtual MockPluginDelegate* NewPluginDelegate() OVERRIDE { |
121 return static_cast<MockPluginDelegate*>(new QuotaMockPluginDelegate); | 120 return static_cast<MockPluginDelegate*>(new QuotaMockPluginDelegate); |
122 } | 121 } |
123 | 122 |
124 void WriteTestBody(bool will_operation) { | 123 void WriteTestBody(bool will_operation) { |
125 // Attempt to write zero bytes. | 124 // Attempt to write zero bytes. |
126 EXPECT_FALSE(quota_file_io_->Write(0, "data", 0, | 125 EXPECT_FALSE(quota_file_io_->Write( |
127 callback_factory_.NewCallback( | 126 0, "data", 0, |
128 &QuotaFileIOTest::DidWrite))); | 127 base::Bind(&QuotaFileIOTest::DidWrite, |
| 128 weak_ptr_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", |
132 callback_factory_.NewCallback( | 132 std::numeric_limits<int32_t>::min(), |
133 &QuotaFileIOTest::DidWrite))); | 133 base::Bind(&QuotaFileIOTest::DidWrite, |
| 134 weak_ptr_factory_.GetWeakPtr()))); |
134 | 135 |
135 quota_plugin_delegate()->set_available_space(100); | 136 quota_plugin_delegate()->set_available_space(100); |
136 std::string read_buffer; | 137 std::string read_buffer; |
137 | 138 |
138 // Write 8 bytes at offset 0 (-> length=8). | 139 // Write 8 bytes at offset 0 (-> length=8). |
139 std::string data("12345678"); | 140 std::string data("12345678"); |
140 Write(0, data, will_operation); | 141 Write(0, data, will_operation); |
141 MessageLoop::current()->RunAllPending(); | 142 MessageLoop::current()->RunAllPending(); |
142 ASSERT_EQ(1U, num_results()); | 143 ASSERT_EQ(1U, num_results()); |
143 EXPECT_EQ(static_cast<int>(data.size()), bytes_written().front()); | 144 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(); | 349 reset_results(); |
349 } | 350 } |
350 | 351 |
351 QuotaMockPluginDelegate* quota_plugin_delegate() { | 352 QuotaMockPluginDelegate* quota_plugin_delegate() { |
352 return static_cast<QuotaMockPluginDelegate*>(delegate()); | 353 return static_cast<QuotaMockPluginDelegate*>(delegate()); |
353 } | 354 } |
354 | 355 |
355 void Write(int64_t offset, const std::string& data, bool will_operation) { | 356 void Write(int64_t offset, const std::string& data, bool will_operation) { |
356 if (will_operation) { | 357 if (will_operation) { |
357 ASSERT_TRUE(quota_file_io_->WillWrite( | 358 ASSERT_TRUE(quota_file_io_->WillWrite( |
358 offset, data.size(), | 359 offset, data.size(), |
359 callback_factory_.NewCallback( | 360 base::Bind(&QuotaFileIOTest::DidWrite, |
360 &QuotaFileIOTest::DidWrite))); | 361 weak_ptr_factory_.GetWeakPtr()))); |
361 } else { | 362 } else { |
362 ASSERT_TRUE(quota_file_io_->Write( | 363 ASSERT_TRUE(quota_file_io_->Write( |
363 offset, data.c_str(), data.size(), | 364 offset, data.c_str(), data.size(), |
364 callback_factory_.NewCallback( | 365 base::Bind(&QuotaFileIOTest::DidWrite, |
365 &QuotaFileIOTest::DidWrite))); | 366 weak_ptr_factory_.GetWeakPtr()))); |
366 } | 367 } |
367 } | 368 } |
368 | 369 |
369 void SetLength(int64_t length, bool will_operation) { | 370 void SetLength(int64_t length, bool will_operation) { |
370 if (will_operation) { | 371 if (will_operation) { |
371 ASSERT_TRUE(quota_file_io_->WillSetLength( | 372 ASSERT_TRUE(quota_file_io_->WillSetLength( |
372 length, | 373 length, |
373 callback_factory_.NewCallback( | 374 base::Bind(&QuotaFileIOTest::DidSetLength, |
374 &QuotaFileIOTest::DidSetLength))); | 375 weak_ptr_factory_.GetWeakPtr()))); |
375 } else { | 376 } else { |
376 ASSERT_TRUE(quota_file_io_->SetLength( | 377 ASSERT_TRUE(quota_file_io_->SetLength( |
377 length, | 378 length, |
378 callback_factory_.NewCallback( | 379 base::Bind(&QuotaFileIOTest::DidSetLength, |
379 &QuotaFileIOTest::DidSetLength))); | 380 weak_ptr_factory_.GetWeakPtr()))); |
380 } | 381 } |
381 } | 382 } |
382 | 383 |
383 void DidWrite(PlatformFileError status, int bytes_written) { | 384 void DidWrite(PlatformFileError status, int bytes_written) { |
384 status_.push_back(status); | 385 status_.push_back(status); |
385 bytes_written_.push_back(bytes_written); | 386 bytes_written_.push_back(bytes_written); |
386 } | 387 } |
387 | 388 |
388 void DidSetLength(PlatformFileError status) { | 389 void DidSetLength(PlatformFileError status) { |
389 status_.push_back(status); | 390 status_.push_back(status); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 void SetPlatformFileSize(int64_t length) { | 427 void SetPlatformFileSize(int64_t length) { |
427 EXPECT_TRUE(base::TruncatePlatformFile(file_, length)); | 428 EXPECT_TRUE(base::TruncatePlatformFile(file_, length)); |
428 } | 429 } |
429 | 430 |
430 private: | 431 private: |
431 ScopedTempDir dir_; | 432 ScopedTempDir dir_; |
432 PlatformFile file_; | 433 PlatformFile file_; |
433 scoped_ptr<QuotaFileIO> quota_file_io_; | 434 scoped_ptr<QuotaFileIO> quota_file_io_; |
434 std::deque<int> bytes_written_; | 435 std::deque<int> bytes_written_; |
435 std::deque<PlatformFileError> status_; | 436 std::deque<PlatformFileError> status_; |
436 base::ScopedCallbackFactory<QuotaFileIOTest> callback_factory_; | 437 base::WeakPtrFactory<QuotaFileIOTest> weak_ptr_factory_; |
437 }; | 438 }; |
438 | 439 |
439 TEST_F(QuotaFileIOTest, Write) { | 440 TEST_F(QuotaFileIOTest, Write) { |
440 WriteTestBody(false); | 441 WriteTestBody(false); |
441 } | 442 } |
442 | 443 |
443 TEST_F(QuotaFileIOTest, WillWrite) { | 444 TEST_F(QuotaFileIOTest, WillWrite) { |
444 WriteTestBody(true); | 445 WriteTestBody(true); |
445 } | 446 } |
446 | 447 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 pop_result(); | 497 pop_result(); |
497 | 498 |
498 EXPECT_EQ(22 - 15, quota_plugin_delegate()->available_space()); | 499 EXPECT_EQ(22 - 15, quota_plugin_delegate()->available_space()); |
499 EXPECT_EQ(15, GetPlatformFileSize()); | 500 EXPECT_EQ(15, GetPlatformFileSize()); |
500 ReadPlatformFile(&read_buffer); | 501 ReadPlatformFile(&read_buffer); |
501 EXPECT_EQ("123355559012345", read_buffer); | 502 EXPECT_EQ("123355559012345", read_buffer); |
502 } | 503 } |
503 | 504 |
504 } // namespace ppapi | 505 } // namespace ppapi |
505 } // namespace webkit | 506 } // namespace webkit |
OLD | NEW |