Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: webkit/browser/fileapi/quota/quota_reservation_manager_unittest.cc

Issue 61593002: Quota: Implement QuotaBackendImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: build fix Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "webkit/browser/fileapi/quota/quota_reservation_manager.h" 5 #include "webkit/browser/fileapi/quota/quota_reservation_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webkit/browser/fileapi/quota/open_file_handle.h" 14 #include "webkit/browser/fileapi/quota/open_file_handle.h"
15 #include "webkit/browser/fileapi/quota/quota_reservation.h" 15 #include "webkit/browser/fileapi/quota/quota_reservation.h"
16 16
17 namespace fileapi { 17 namespace fileapi {
18 18
19 namespace { 19 namespace {
20 20
21 const char kOrigin[] = "http://example.com"; 21 const char kOrigin[] = "http://example.com";
22 const FileSystemType kType = kFileSystemTypeTemporary; 22 const FileSystemType kType = kFileSystemTypeTemporary;
23 const int64 kInitialFileSize = 30; 23 const int64 kInitialFileSize = 30;
24 24
25 typedef QuotaReservationManager::ReserveQuotaCallback ReserveQuotaCallback; 25 typedef QuotaReservationManager::ReserveQuotaCallback ReserveQuotaCallback;
26 typedef QuotaReservationManager::StatusCallback StatusCallback;
27 26
28 class FakeBackend : public QuotaReservationManager::QuotaBackend { 27 class FakeBackend : public QuotaReservationManager::QuotaBackend {
29 public: 28 public:
30 FakeBackend() 29 FakeBackend()
31 : on_memory_usage_(0), 30 : on_memory_usage_(0),
32 on_disk_usage_(0) {} 31 on_disk_usage_(0) {}
33 virtual ~FakeBackend() {} 32 virtual ~FakeBackend() {}
34 33
35 virtual void ReserveQuota(const GURL& origin, 34 virtual void ReserveQuota(const GURL& origin,
36 FileSystemType type, 35 FileSystemType type,
(...skipping 11 matching lines...) Expand all
48 FileSystemType type, 47 FileSystemType type,
49 int64 size) OVERRIDE { 48 int64 size) OVERRIDE {
50 EXPECT_LE(0, size); 49 EXPECT_LE(0, size);
51 EXPECT_EQ(GURL(kOrigin), origin); 50 EXPECT_EQ(GURL(kOrigin), origin);
52 EXPECT_EQ(kType, type); 51 EXPECT_EQ(kType, type);
53 on_memory_usage_ -= size; 52 on_memory_usage_ -= size;
54 } 53 }
55 54
56 virtual void CommitQuotaUsage(const GURL& origin, 55 virtual void CommitQuotaUsage(const GURL& origin,
57 FileSystemType type, 56 FileSystemType type,
58 int64 delta, 57 int64 delta) OVERRIDE {
59 const StatusCallback& callback) OVERRIDE {
60 EXPECT_EQ(GURL(kOrigin), origin); 58 EXPECT_EQ(GURL(kOrigin), origin);
61 EXPECT_EQ(kType, type); 59 EXPECT_EQ(kType, type);
62 on_disk_usage_ += delta; 60 on_disk_usage_ += delta;
63 base::MessageLoopProxy::current()->PostTask(
64 FROM_HERE, base::Bind(callback, base::PLATFORM_FILE_OK));
65 } 61 }
66 62
67 virtual void IncreaseDirtyCount(const GURL& origin, 63 virtual void IncrementDirtyCount(const GURL& origin,
68 FileSystemType type) OVERRIDE {} 64 FileSystemType type) OVERRIDE {}
69 virtual void DecreaseDirtyCount(const GURL& origin, 65 virtual void DecrementDirtyCount(const GURL& origin,
70 FileSystemType type) OVERRIDE {} 66 FileSystemType type) OVERRIDE {}
71 67
72 int64 on_memory_usage() { return on_memory_usage_; } 68 int64 on_memory_usage() { return on_memory_usage_; }
73 int64 on_disk_usage() { return on_disk_usage_; } 69 int64 on_disk_usage() { return on_disk_usage_; }
74 70
75 private: 71 private:
76 int64 on_memory_usage_; 72 int64 on_memory_usage_;
77 int64 on_disk_usage_; 73 int64 on_disk_usage_;
78 74
79 DISALLOW_COPY_AND_ASSIGN(FakeBackend); 75 DISALLOW_COPY_AND_ASSIGN(FakeBackend);
80 }; 76 };
(...skipping 18 matching lines...) Expand all
99 class QuotaReservationManagerTest : public testing::Test { 95 class QuotaReservationManagerTest : public testing::Test {
100 public: 96 public:
101 QuotaReservationManagerTest() {} 97 QuotaReservationManagerTest() {}
102 virtual ~QuotaReservationManagerTest() {} 98 virtual ~QuotaReservationManagerTest() {}
103 99
104 virtual void SetUp() OVERRIDE { 100 virtual void SetUp() OVERRIDE {
105 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); 101 ASSERT_TRUE(work_dir_.CreateUniqueTempDir());
106 file_path_ = work_dir_.path().Append(FILE_PATH_LITERAL("hoge")); 102 file_path_ = work_dir_.path().Append(FILE_PATH_LITERAL("hoge"));
107 SetFileSize(kInitialFileSize); 103 SetFileSize(kInitialFileSize);
108 104
109 fake_backend_.reset(new FakeBackend); 105 scoped_ptr<QuotaReservationManager::QuotaBackend> backend(new FakeBackend);
110 reservation_manager_.reset(new QuotaReservationManager( 106 reservation_manager_.reset(new QuotaReservationManager(backend.Pass()));
111 fake_backend_.get()));
112 } 107 }
113 108
114 virtual void TearDown() OVERRIDE { 109 virtual void TearDown() OVERRIDE {
115 reservation_manager_.reset(); 110 reservation_manager_.reset();
116 fake_backend_.reset();
117 } 111 }
118 112
119 int64 GetFileSize() { 113 int64 GetFileSize() {
120 int64 size = 0; 114 int64 size = 0;
121 file_util::GetFileSize(file_path_, &size); 115 file_util::GetFileSize(file_path_, &size);
122 return size; 116 return size;
123 } 117 }
124 118
125 void SetFileSize(int64 size) { 119 void SetFileSize(int64 size) {
126 bool created = false; 120 bool created = false;
127 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; 121 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED;
128 base::PlatformFile file = CreatePlatformFile( 122 base::PlatformFile file = CreatePlatformFile(
129 file_path_, 123 file_path_,
130 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE, 124 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE,
131 &created, &error); 125 &created, &error);
132 ASSERT_EQ(base::PLATFORM_FILE_OK, error); 126 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
133 ASSERT_TRUE(base::TruncatePlatformFile(file, size)); 127 ASSERT_TRUE(base::TruncatePlatformFile(file, size));
134 ASSERT_TRUE(base::ClosePlatformFile(file)); 128 ASSERT_TRUE(base::ClosePlatformFile(file));
135 } 129 }
136 130
137 void ExtendFileTo(int64 size) { 131 void ExtendFileTo(int64 size) {
138 if (GetFileSize() < size) 132 if (GetFileSize() < size)
139 SetFileSize(size); 133 SetFileSize(size);
140 } 134 }
141 135
142 FakeBackend* fake_backend() { 136 FakeBackend* fake_backend() {
143 return fake_backend_.get(); 137 return static_cast<FakeBackend*>(reservation_manager_->backend_.get());
144 } 138 }
145 139
146 QuotaReservationManager* reservation_manager() { 140 QuotaReservationManager* reservation_manager() {
147 return reservation_manager_.get(); 141 return reservation_manager_.get();
148 } 142 }
149 143
150 const base::FilePath& file_path() const { 144 const base::FilePath& file_path() const {
151 return file_path_; 145 return file_path_;
152 } 146 }
153 147
154 private: 148 private:
155 base::MessageLoop message_loop_; 149 base::MessageLoop message_loop_;
156 base::ScopedTempDir work_dir_; 150 base::ScopedTempDir work_dir_;
157 base::FilePath file_path_; 151 base::FilePath file_path_;
158 scoped_ptr<FakeBackend> fake_backend_;
159 scoped_ptr<QuotaReservationManager> reservation_manager_; 152 scoped_ptr<QuotaReservationManager> reservation_manager_;
160 153
161 DISALLOW_COPY_AND_ASSIGN(QuotaReservationManagerTest); 154 DISALLOW_COPY_AND_ASSIGN(QuotaReservationManagerTest);
162 }; 155 };
163 156
164 TEST_F(QuotaReservationManagerTest, BasicTest) { 157 TEST_F(QuotaReservationManagerTest, BasicTest) {
165 GURL origin(kOrigin); 158 GURL origin(kOrigin);
166 FileSystemType type = kType; 159 FileSystemType type = kType;
167 160
168 // Create Reservation channel for the origin and type. 161 // Create Reservation channel for the origin and type.
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 349
357 EXPECT_EQ(400, GetFileSize()); 350 EXPECT_EQ(400, GetFileSize());
358 EXPECT_EQ(0, fake_backend()->on_memory_usage()); 351 EXPECT_EQ(0, fake_backend()->on_memory_usage());
359 EXPECT_EQ(400 - kInitialFileSize, fake_backend()->on_disk_usage()); 352 EXPECT_EQ(400 - kInitialFileSize, fake_backend()->on_disk_usage());
360 } 353 }
361 354
362 // TODO(tzik): Add Truncate test. 355 // TODO(tzik): Add Truncate test.
363 // TODO(tzik): Add PluginCrash test and DropReservationManager test. 356 // TODO(tzik): Add PluginCrash test and DropReservationManager test.
364 357
365 } // namespace fileapi 358 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698