Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / quota_reservation_unittest.cc
blob05eb4bf19a469e7ed8c865324dad50c4449a59bc
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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/pepper/quota_reservation.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/file_util.h"
10 #include "base/files/file.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/browser/fileapi/quota/quota_reservation.h"
17 using fileapi::FileSystemType;
18 using fileapi::QuotaReservationManager;
20 namespace content {
22 namespace {
24 const char kOrigin[] = "http://example.com";
25 const FileSystemType kType = fileapi::kFileSystemTypeTemporary;
27 const base::FilePath::StringType file1_name = FILE_PATH_LITERAL("file1");
28 const base::FilePath::StringType file2_name = FILE_PATH_LITERAL("file2");
29 const base::FilePath::StringType file3_name = FILE_PATH_LITERAL("file3");
30 const int kFile1ID = 1;
31 const int kFile2ID = 2;
32 const int kFile3ID = 3;
34 class FakeBackend : public QuotaReservationManager::QuotaBackend {
35 public:
36 FakeBackend() {}
37 virtual ~FakeBackend() {}
39 virtual void ReserveQuota(
40 const GURL& origin,
41 FileSystemType type,
42 int64 delta,
43 const QuotaReservationManager::ReserveQuotaCallback& callback) OVERRIDE {
44 base::MessageLoopProxy::current()->PostTask(
45 FROM_HERE,
46 base::Bind(base::IgnoreResult(callback), base::File::FILE_OK, delta));
49 virtual void ReleaseReservedQuota(const GURL& origin,
50 FileSystemType type,
51 int64 size) OVERRIDE {}
53 virtual void CommitQuotaUsage(const GURL& origin,
54 FileSystemType type,
55 int64 delta) OVERRIDE {}
57 virtual void IncrementDirtyCount(const GURL& origin,
58 FileSystemType type) OVERRIDE {}
59 virtual void DecrementDirtyCount(const GURL& origin,
60 FileSystemType type) OVERRIDE {}
62 private:
63 DISALLOW_COPY_AND_ASSIGN(FakeBackend);
66 } // namespace
68 class QuotaReservationTest : public testing::Test {
69 public:
70 QuotaReservationTest() {}
71 virtual ~QuotaReservationTest() {}
73 virtual void SetUp() OVERRIDE {
74 ASSERT_TRUE(work_dir_.CreateUniqueTempDir());
76 reservation_manager_.reset(new QuotaReservationManager(
77 scoped_ptr<QuotaReservationManager::QuotaBackend>(new FakeBackend)));
80 virtual void TearDown() OVERRIDE {
81 reservation_manager_.reset();
82 base::RunLoop().RunUntilIdle();
85 base::FilePath MakeFilePath(const base::FilePath::StringType& file_name) {
86 return work_dir_.path().Append(file_name);
89 fileapi::FileSystemURL MakeFileSystemURL(
90 const base::FilePath::StringType& file_name) {
91 return fileapi::FileSystemURL::CreateForTest(
92 GURL(kOrigin), kType, MakeFilePath(file_name));
95 scoped_refptr<QuotaReservation> CreateQuotaReservation(
96 scoped_refptr<fileapi::QuotaReservation> reservation,
97 const GURL& origin,
98 FileSystemType type) {
99 // Sets reservation_ as a side effect.
100 return scoped_refptr<QuotaReservation>(
101 new QuotaReservation(reservation, origin, type));
104 void SetFileSize(const base::FilePath::StringType& file_name, int64 size) {
105 base::File file(MakeFilePath(file_name),
106 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE);
107 ASSERT_TRUE(file.IsValid());
108 ASSERT_TRUE(file.SetLength(size));
111 QuotaReservationManager* reservation_manager() {
112 return reservation_manager_.get();
115 private:
116 base::MessageLoop message_loop_;
117 base::ScopedTempDir work_dir_;
118 scoped_ptr<fileapi::QuotaReservationManager> reservation_manager_;
120 DISALLOW_COPY_AND_ASSIGN(QuotaReservationTest);
123 void GotReservedQuota(int64* reserved_quota_ptr,
124 ppapi::FileGrowthMap* file_growths_ptr,
125 int64 reserved_quota,
126 const ppapi::FileSizeMap& maximum_written_offsets) {
127 *reserved_quota_ptr = reserved_quota;
129 file_growths_ptr->clear();
130 for (ppapi::FileSizeMap::const_iterator it = maximum_written_offsets.begin();
131 it != maximum_written_offsets.end();
132 ++it)
133 (*file_growths_ptr)[it->first] = ppapi::FileGrowth(it->second, 0);
136 void ReserveQuota(scoped_refptr<QuotaReservation> quota_reservation,
137 int64 amount,
138 int64* reserved_quota,
139 ppapi::FileGrowthMap* file_growths) {
140 quota_reservation->ReserveQuota(
141 amount,
142 *file_growths,
143 base::Bind(&GotReservedQuota, reserved_quota, file_growths));
144 base::RunLoop().RunUntilIdle();
147 // Tests that:
148 // 1) We can reserve quota with no files open.
149 // 2) Open a file, grow it, close it, and reserve quota with correct sizes.
150 TEST_F(QuotaReservationTest, ReserveQuota) {
151 GURL origin(kOrigin);
152 FileSystemType type = kType;
154 scoped_refptr<fileapi::QuotaReservation> reservation(
155 reservation_manager()->CreateReservation(origin, type));
156 scoped_refptr<QuotaReservation> test =
157 CreateQuotaReservation(reservation, origin, type);
159 // Reserve quota with no files open.
160 int64 amount = 100;
161 int64 reserved_quota;
162 ppapi::FileGrowthMap file_growths;
163 ReserveQuota(test, amount, &reserved_quota, &file_growths);
164 EXPECT_EQ(amount, reserved_quota);
165 EXPECT_EQ(0U, file_growths.size());
167 // Open a file, refresh the reservation, extend the file, and close it.
168 int64 file_size = 10;
169 SetFileSize(file1_name, file_size);
170 int64 open_file_size =
171 test->OpenFile(kFile1ID, MakeFileSystemURL(file1_name));
172 EXPECT_EQ(file_size, open_file_size);
174 file_growths[kFile1ID] = ppapi::FileGrowth(file_size, 0); // 1 file open.
175 ReserveQuota(test, amount, &reserved_quota, &file_growths);
176 EXPECT_EQ(amount, reserved_quota);
177 EXPECT_EQ(1U, file_growths.size());
178 EXPECT_EQ(file_size, file_growths[kFile1ID].max_written_offset);
180 int64 new_file_size = 30;
181 SetFileSize(file1_name, new_file_size);
183 EXPECT_EQ(amount, reservation->remaining_quota());
184 test->CloseFile(kFile1ID, ppapi::FileGrowth(new_file_size, 0));
185 EXPECT_EQ(amount - (new_file_size - file_size),
186 reservation->remaining_quota());
189 // Tests that:
190 // 1) We can open and close multiple files.
191 TEST_F(QuotaReservationTest, MultipleFiles) {
192 GURL origin(kOrigin);
193 FileSystemType type = kType;
195 scoped_refptr<fileapi::QuotaReservation> reservation(
196 reservation_manager()->CreateReservation(origin, type));
197 scoped_refptr<QuotaReservation> test =
198 CreateQuotaReservation(reservation, origin, type);
200 // Open some files of different sizes.
201 int64 file1_size = 10;
202 SetFileSize(file1_name, file1_size);
203 int64 open_file1_size =
204 test->OpenFile(kFile1ID, MakeFileSystemURL(file1_name));
205 EXPECT_EQ(file1_size, open_file1_size);
206 int64 file2_size = 20;
207 SetFileSize(file2_name, file2_size);
208 int64 open_file2_size =
209 test->OpenFile(kFile2ID, MakeFileSystemURL(file2_name));
210 EXPECT_EQ(file2_size, open_file2_size);
211 int64 file3_size = 30;
212 SetFileSize(file3_name, file3_size);
213 int64 open_file3_size =
214 test->OpenFile(kFile3ID, MakeFileSystemURL(file3_name));
215 EXPECT_EQ(file3_size, open_file3_size);
217 // Reserve quota.
218 int64 amount = 100;
219 int64 reserved_quota;
220 ppapi::FileGrowthMap file_growths;
221 file_growths[kFile1ID] = ppapi::FileGrowth(file1_size, 0); // 3 files open.
222 file_growths[kFile2ID] = ppapi::FileGrowth(file2_size, 0);
223 file_growths[kFile3ID] = ppapi::FileGrowth(file3_size, 0);
225 ReserveQuota(test, amount, &reserved_quota, &file_growths);
226 EXPECT_EQ(amount, reserved_quota);
227 EXPECT_EQ(3U, file_growths.size());
228 EXPECT_EQ(file1_size, file_growths[kFile1ID].max_written_offset);
229 EXPECT_EQ(file2_size, file_growths[kFile2ID].max_written_offset);
230 EXPECT_EQ(file3_size, file_growths[kFile3ID].max_written_offset);
232 test->CloseFile(kFile2ID, ppapi::FileGrowth(file2_size, 0));
234 file_growths.erase(kFile2ID);
235 ReserveQuota(test, amount, &reserved_quota, &file_growths);
236 EXPECT_EQ(amount, reserved_quota);
237 EXPECT_EQ(2U, file_growths.size());
238 EXPECT_EQ(file1_size, file_growths[kFile1ID].max_written_offset);
239 EXPECT_EQ(file3_size, file_growths[kFile3ID].max_written_offset);
241 test->CloseFile(kFile1ID, ppapi::FileGrowth(file1_size, 0));
242 test->CloseFile(kFile3ID, ppapi::FileGrowth(file3_size, 0));
245 } // namespace content