| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdint.h> | 5 #include <stdint.h> |
| 6 |
| 7 #include <memory> |
| 6 #include <utility> | 8 #include <utility> |
| 7 | 9 |
| 8 #include "base/bind.h" | 10 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 10 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/location.h" | 15 #include "base/location.h" |
| 14 #include "base/macros.h" | 16 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 18 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| 19 #include "storage/browser/fileapi/quota/open_file_handle.h" | 20 #include "storage/browser/fileapi/quota/open_file_handle.h" |
| 20 #include "storage/browser/fileapi/quota/quota_reservation.h" | 21 #include "storage/browser/fileapi/quota/quota_reservation.h" |
| 21 #include "storage/browser/fileapi/quota/quota_reservation_manager.h" | 22 #include "storage/browser/fileapi/quota/quota_reservation_manager.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 using storage::kFileSystemTypeTemporary; | 25 using storage::kFileSystemTypeTemporary; |
| 25 using storage::OpenFileHandle; | 26 using storage::OpenFileHandle; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 96 |
| 96 private: | 97 private: |
| 97 int64_t on_memory_usage_; | 98 int64_t on_memory_usage_; |
| 98 int64_t on_disk_usage_; | 99 int64_t on_disk_usage_; |
| 99 | 100 |
| 100 DISALLOW_COPY_AND_ASSIGN(FakeBackend); | 101 DISALLOW_COPY_AND_ASSIGN(FakeBackend); |
| 101 }; | 102 }; |
| 102 | 103 |
| 103 class FakeWriter { | 104 class FakeWriter { |
| 104 public: | 105 public: |
| 105 explicit FakeWriter(scoped_ptr<OpenFileHandle> handle) | 106 explicit FakeWriter(std::unique_ptr<OpenFileHandle> handle) |
| 106 : handle_(std::move(handle)), | 107 : handle_(std::move(handle)), |
| 107 path_(handle_->platform_path()), | 108 path_(handle_->platform_path()), |
| 108 max_written_offset_(handle_->GetEstimatedFileSize()), | 109 max_written_offset_(handle_->GetEstimatedFileSize()), |
| 109 append_mode_write_amount_(0), | 110 append_mode_write_amount_(0), |
| 110 dirty_(false) {} | 111 dirty_(false) {} |
| 111 | 112 |
| 112 ~FakeWriter() { | 113 ~FakeWriter() { |
| 113 if (handle_) | 114 if (handle_) |
| 114 EXPECT_FALSE(dirty_); | 115 EXPECT_FALSE(dirty_); |
| 115 } | 116 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 max_written_offset_ = handle_->GetEstimatedFileSize(); | 152 max_written_offset_ = handle_->GetEstimatedFileSize(); |
| 152 append_mode_write_amount_ = 0; | 153 append_mode_write_amount_ = 0; |
| 153 dirty_ = false; | 154 dirty_ = false; |
| 154 } | 155 } |
| 155 | 156 |
| 156 void ClearWithoutUsageReport() { | 157 void ClearWithoutUsageReport() { |
| 157 handle_.reset(); | 158 handle_.reset(); |
| 158 } | 159 } |
| 159 | 160 |
| 160 private: | 161 private: |
| 161 scoped_ptr<OpenFileHandle> handle_; | 162 std::unique_ptr<OpenFileHandle> handle_; |
| 162 base::FilePath path_; | 163 base::FilePath path_; |
| 163 int64_t max_written_offset_; | 164 int64_t max_written_offset_; |
| 164 int64_t append_mode_write_amount_; | 165 int64_t append_mode_write_amount_; |
| 165 bool dirty_; | 166 bool dirty_; |
| 166 }; | 167 }; |
| 167 | 168 |
| 168 void ExpectSuccess(bool* done, base::File::Error error) { | 169 void ExpectSuccess(bool* done, base::File::Error error) { |
| 169 EXPECT_FALSE(*done); | 170 EXPECT_FALSE(*done); |
| 170 *done = true; | 171 *done = true; |
| 171 EXPECT_EQ(base::File::FILE_OK, error); | 172 EXPECT_EQ(base::File::FILE_OK, error); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 185 class QuotaReservationManagerTest : public testing::Test { | 186 class QuotaReservationManagerTest : public testing::Test { |
| 186 public: | 187 public: |
| 187 QuotaReservationManagerTest() {} | 188 QuotaReservationManagerTest() {} |
| 188 ~QuotaReservationManagerTest() override {} | 189 ~QuotaReservationManagerTest() override {} |
| 189 | 190 |
| 190 void SetUp() override { | 191 void SetUp() override { |
| 191 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); | 192 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); |
| 192 file_path_ = work_dir_.path().Append(FILE_PATH_LITERAL("hoge")); | 193 file_path_ = work_dir_.path().Append(FILE_PATH_LITERAL("hoge")); |
| 193 SetFileSize(file_path_, kInitialFileSize); | 194 SetFileSize(file_path_, kInitialFileSize); |
| 194 | 195 |
| 195 scoped_ptr<QuotaReservationManager::QuotaBackend> backend(new FakeBackend); | 196 std::unique_ptr<QuotaReservationManager::QuotaBackend> backend( |
| 197 new FakeBackend); |
| 196 reservation_manager_.reset(new QuotaReservationManager(std::move(backend))); | 198 reservation_manager_.reset(new QuotaReservationManager(std::move(backend))); |
| 197 } | 199 } |
| 198 | 200 |
| 199 void TearDown() override { reservation_manager_.reset(); } | 201 void TearDown() override { reservation_manager_.reset(); } |
| 200 | 202 |
| 201 FakeBackend* fake_backend() { | 203 FakeBackend* fake_backend() { |
| 202 return static_cast<FakeBackend*>(reservation_manager_->backend_.get()); | 204 return static_cast<FakeBackend*>(reservation_manager_->backend_.get()); |
| 203 } | 205 } |
| 204 | 206 |
| 205 QuotaReservationManager* reservation_manager() { | 207 QuotaReservationManager* reservation_manager() { |
| 206 return reservation_manager_.get(); | 208 return reservation_manager_.get(); |
| 207 } | 209 } |
| 208 | 210 |
| 209 const base::FilePath& file_path() const { | 211 const base::FilePath& file_path() const { |
| 210 return file_path_; | 212 return file_path_; |
| 211 } | 213 } |
| 212 | 214 |
| 213 private: | 215 private: |
| 214 base::MessageLoop message_loop_; | 216 base::MessageLoop message_loop_; |
| 215 base::ScopedTempDir work_dir_; | 217 base::ScopedTempDir work_dir_; |
| 216 base::FilePath file_path_; | 218 base::FilePath file_path_; |
| 217 scoped_ptr<QuotaReservationManager> reservation_manager_; | 219 std::unique_ptr<QuotaReservationManager> reservation_manager_; |
| 218 | 220 |
| 219 DISALLOW_COPY_AND_ASSIGN(QuotaReservationManagerTest); | 221 DISALLOW_COPY_AND_ASSIGN(QuotaReservationManagerTest); |
| 220 }; | 222 }; |
| 221 | 223 |
| 222 TEST_F(QuotaReservationManagerTest, BasicTest) { | 224 TEST_F(QuotaReservationManagerTest, BasicTest) { |
| 223 scoped_refptr<QuotaReservation> reservation = | 225 scoped_refptr<QuotaReservation> reservation = |
| 224 reservation_manager()->CreateReservation(GURL(kOrigin), kType); | 226 reservation_manager()->CreateReservation(GURL(kOrigin), kType); |
| 225 | 227 |
| 226 { | 228 { |
| 227 RefreshReservation(reservation.get(), 10 + 20 + 3); | 229 RefreshReservation(reservation.get(), 10 + 20 + 3); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 scoped_refptr<QuotaReservation> reservation1 = | 300 scoped_refptr<QuotaReservation> reservation1 = |
| 299 reservation_manager()->CreateReservation(GURL(kOrigin), kType); | 301 reservation_manager()->CreateReservation(GURL(kOrigin), kType); |
| 300 RefreshReservation(reservation1.get(), 10); | 302 RefreshReservation(reservation1.get(), 10); |
| 301 int64_t cached_reserved_quota1 = reservation1->remaining_quota(); | 303 int64_t cached_reserved_quota1 = reservation1->remaining_quota(); |
| 302 | 304 |
| 303 scoped_refptr<QuotaReservation> reservation2 = | 305 scoped_refptr<QuotaReservation> reservation2 = |
| 304 reservation_manager()->CreateReservation(GURL(kOrigin), kType); | 306 reservation_manager()->CreateReservation(GURL(kOrigin), kType); |
| 305 RefreshReservation(reservation2.get(), 20); | 307 RefreshReservation(reservation2.get(), 20); |
| 306 int64_t cached_reserved_quota2 = reservation2->remaining_quota(); | 308 int64_t cached_reserved_quota2 = reservation2->remaining_quota(); |
| 307 | 309 |
| 308 scoped_ptr<FakeWriter> writer1( | 310 std::unique_ptr<FakeWriter> writer1( |
| 309 new FakeWriter(reservation1->GetOpenFileHandle(file_path()))); | 311 new FakeWriter(reservation1->GetOpenFileHandle(file_path()))); |
| 310 | 312 |
| 311 scoped_ptr<FakeWriter> writer2( | 313 std::unique_ptr<FakeWriter> writer2( |
| 312 new FakeWriter(reservation2->GetOpenFileHandle(file_path()))); | 314 new FakeWriter(reservation2->GetOpenFileHandle(file_path()))); |
| 313 | 315 |
| 314 cached_reserved_quota1 -= writer1->Write(kInitialFileSize + 10); | 316 cached_reserved_quota1 -= writer1->Write(kInitialFileSize + 10); |
| 315 EXPECT_LE(0, cached_reserved_quota1); | 317 EXPECT_LE(0, cached_reserved_quota1); |
| 316 | 318 |
| 317 cached_reserved_quota2 -= writer2->Append(20); | 319 cached_reserved_quota2 -= writer2->Append(20); |
| 318 EXPECT_LE(0, cached_reserved_quota2); | 320 EXPECT_LE(0, cached_reserved_quota2); |
| 319 | 321 |
| 320 writer1->ReportUsage(); | 322 writer1->ReportUsage(); |
| 321 RefreshReservation(reservation1.get(), 2); | 323 RefreshReservation(reservation1.get(), 2); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 363 |
| 362 EXPECT_EQ(kInitialFileSize + 10, GetFileSize(file_path())); | 364 EXPECT_EQ(kInitialFileSize + 10, GetFileSize(file_path())); |
| 363 EXPECT_EQ(kInitialFileSize + 15 + 20, fake_backend()->on_memory_usage()); | 365 EXPECT_EQ(kInitialFileSize + 15 + 20, fake_backend()->on_memory_usage()); |
| 364 EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_disk_usage()); | 366 EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_disk_usage()); |
| 365 | 367 |
| 366 reservation2 = NULL; | 368 reservation2 = NULL; |
| 367 EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_memory_usage()); | 369 EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_memory_usage()); |
| 368 } | 370 } |
| 369 | 371 |
| 370 } // namespace content | 372 } // namespace content |
| OLD | NEW |