OLD | NEW |
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 "content/browser/renderer_host/pepper/quota_reservation.h" | 5 #include "content/browser/renderer_host/pepper/quota_reservation.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/file.h" | 10 #include "base/files/file.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "webkit/browser/fileapi/quota/quota_reservation.h" | 15 #include "storage/browser/fileapi/quota/quota_reservation.h" |
16 | 16 |
17 using fileapi::QuotaReservationManager; | 17 using storage::QuotaReservationManager; |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 const char kOrigin[] = "http://example.com"; | 23 const char kOrigin[] = "http://example.com"; |
24 const fileapi::FileSystemType kType = fileapi::kFileSystemTypeTemporary; | 24 const storage::FileSystemType kType = storage::kFileSystemTypeTemporary; |
25 | 25 |
26 const base::FilePath::StringType file1_name = FILE_PATH_LITERAL("file1"); | 26 const base::FilePath::StringType file1_name = FILE_PATH_LITERAL("file1"); |
27 const base::FilePath::StringType file2_name = FILE_PATH_LITERAL("file2"); | 27 const base::FilePath::StringType file2_name = FILE_PATH_LITERAL("file2"); |
28 const base::FilePath::StringType file3_name = FILE_PATH_LITERAL("file3"); | 28 const base::FilePath::StringType file3_name = FILE_PATH_LITERAL("file3"); |
29 const int kFile1ID = 1; | 29 const int kFile1ID = 1; |
30 const int kFile2ID = 2; | 30 const int kFile2ID = 2; |
31 const int kFile3ID = 3; | 31 const int kFile3ID = 3; |
32 | 32 |
33 class FakeBackend : public QuotaReservationManager::QuotaBackend { | 33 class FakeBackend : public QuotaReservationManager::QuotaBackend { |
34 public: | 34 public: |
35 FakeBackend() {} | 35 FakeBackend() {} |
36 virtual ~FakeBackend() {} | 36 virtual ~FakeBackend() {} |
37 | 37 |
38 virtual void ReserveQuota( | 38 virtual void ReserveQuota( |
39 const GURL& origin, | 39 const GURL& origin, |
40 fileapi::FileSystemType type, | 40 storage::FileSystemType type, |
41 int64 delta, | 41 int64 delta, |
42 const QuotaReservationManager::ReserveQuotaCallback& callback) OVERRIDE { | 42 const QuotaReservationManager::ReserveQuotaCallback& callback) OVERRIDE { |
43 base::MessageLoopProxy::current()->PostTask( | 43 base::MessageLoopProxy::current()->PostTask( |
44 FROM_HERE, | 44 FROM_HERE, |
45 base::Bind(base::IgnoreResult(callback), base::File::FILE_OK, delta)); | 45 base::Bind(base::IgnoreResult(callback), base::File::FILE_OK, delta)); |
46 } | 46 } |
47 | 47 |
48 virtual void ReleaseReservedQuota(const GURL& origin, | 48 virtual void ReleaseReservedQuota(const GURL& origin, |
49 fileapi::FileSystemType type, | 49 storage::FileSystemType type, |
50 int64 size) OVERRIDE {} | 50 int64 size) OVERRIDE {} |
51 | 51 |
52 virtual void CommitQuotaUsage(const GURL& origin, | 52 virtual void CommitQuotaUsage(const GURL& origin, |
53 fileapi::FileSystemType type, | 53 storage::FileSystemType type, |
54 int64 delta) OVERRIDE {} | 54 int64 delta) OVERRIDE {} |
55 | 55 |
56 virtual void IncrementDirtyCount(const GURL& origin, | 56 virtual void IncrementDirtyCount(const GURL& origin, |
57 fileapi::FileSystemType type) OVERRIDE {} | 57 storage::FileSystemType type) OVERRIDE {} |
58 virtual void DecrementDirtyCount(const GURL& origin, | 58 virtual void DecrementDirtyCount(const GURL& origin, |
59 fileapi::FileSystemType type) OVERRIDE {} | 59 storage::FileSystemType type) OVERRIDE {} |
60 | 60 |
61 private: | 61 private: |
62 DISALLOW_COPY_AND_ASSIGN(FakeBackend); | 62 DISALLOW_COPY_AND_ASSIGN(FakeBackend); |
63 }; | 63 }; |
64 | 64 |
65 } // namespace | 65 } // namespace |
66 | 66 |
67 class QuotaReservationTest : public testing::Test { | 67 class QuotaReservationTest : public testing::Test { |
68 public: | 68 public: |
69 QuotaReservationTest() {} | 69 QuotaReservationTest() {} |
70 virtual ~QuotaReservationTest() {} | 70 virtual ~QuotaReservationTest() {} |
71 | 71 |
72 virtual void SetUp() OVERRIDE { | 72 virtual void SetUp() OVERRIDE { |
73 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); | 73 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); |
74 | 74 |
75 reservation_manager_.reset(new QuotaReservationManager( | 75 reservation_manager_.reset(new QuotaReservationManager( |
76 scoped_ptr<QuotaReservationManager::QuotaBackend>(new FakeBackend))); | 76 scoped_ptr<QuotaReservationManager::QuotaBackend>(new FakeBackend))); |
77 } | 77 } |
78 | 78 |
79 virtual void TearDown() OVERRIDE { | 79 virtual void TearDown() OVERRIDE { |
80 reservation_manager_.reset(); | 80 reservation_manager_.reset(); |
81 base::RunLoop().RunUntilIdle(); | 81 base::RunLoop().RunUntilIdle(); |
82 } | 82 } |
83 | 83 |
84 base::FilePath MakeFilePath(const base::FilePath::StringType& file_name) { | 84 base::FilePath MakeFilePath(const base::FilePath::StringType& file_name) { |
85 return work_dir_.path().Append(file_name); | 85 return work_dir_.path().Append(file_name); |
86 } | 86 } |
87 | 87 |
88 fileapi::FileSystemURL MakeFileSystemURL( | 88 storage::FileSystemURL MakeFileSystemURL( |
89 const base::FilePath::StringType& file_name) { | 89 const base::FilePath::StringType& file_name) { |
90 return fileapi::FileSystemURL::CreateForTest( | 90 return storage::FileSystemURL::CreateForTest( |
91 GURL(kOrigin), kType, MakeFilePath(file_name)); | 91 GURL(kOrigin), kType, MakeFilePath(file_name)); |
92 } | 92 } |
93 | 93 |
94 scoped_refptr<QuotaReservation> CreateQuotaReservation( | 94 scoped_refptr<QuotaReservation> CreateQuotaReservation( |
95 scoped_refptr<fileapi::QuotaReservation> reservation, | 95 scoped_refptr<storage::QuotaReservation> reservation, |
96 const GURL& origin, | 96 const GURL& origin, |
97 fileapi::FileSystemType type) { | 97 storage::FileSystemType type) { |
98 // Sets reservation_ as a side effect. | 98 // Sets reservation_ as a side effect. |
99 return scoped_refptr<QuotaReservation>( | 99 return scoped_refptr<QuotaReservation>( |
100 new QuotaReservation(reservation, origin, type)); | 100 new QuotaReservation(reservation, origin, type)); |
101 } | 101 } |
102 | 102 |
103 void SetFileSize(const base::FilePath::StringType& file_name, int64 size) { | 103 void SetFileSize(const base::FilePath::StringType& file_name, int64 size) { |
104 base::File file(MakeFilePath(file_name), | 104 base::File file(MakeFilePath(file_name), |
105 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); | 105 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); |
106 ASSERT_TRUE(file.IsValid()); | 106 ASSERT_TRUE(file.IsValid()); |
107 ASSERT_TRUE(file.SetLength(size)); | 107 ASSERT_TRUE(file.SetLength(size)); |
108 } | 108 } |
109 | 109 |
110 QuotaReservationManager* reservation_manager() { | 110 QuotaReservationManager* reservation_manager() { |
111 return reservation_manager_.get(); | 111 return reservation_manager_.get(); |
112 } | 112 } |
113 | 113 |
114 private: | 114 private: |
115 base::MessageLoop message_loop_; | 115 base::MessageLoop message_loop_; |
116 base::ScopedTempDir work_dir_; | 116 base::ScopedTempDir work_dir_; |
117 scoped_ptr<fileapi::QuotaReservationManager> reservation_manager_; | 117 scoped_ptr<storage::QuotaReservationManager> reservation_manager_; |
118 | 118 |
119 DISALLOW_COPY_AND_ASSIGN(QuotaReservationTest); | 119 DISALLOW_COPY_AND_ASSIGN(QuotaReservationTest); |
120 }; | 120 }; |
121 | 121 |
122 void GotReservedQuota(int64* reserved_quota_ptr, | 122 void GotReservedQuota(int64* reserved_quota_ptr, |
123 ppapi::FileGrowthMap* file_growths_ptr, | 123 ppapi::FileGrowthMap* file_growths_ptr, |
124 int64 reserved_quota, | 124 int64 reserved_quota, |
125 const ppapi::FileSizeMap& maximum_written_offsets) { | 125 const ppapi::FileSizeMap& maximum_written_offsets) { |
126 *reserved_quota_ptr = reserved_quota; | 126 *reserved_quota_ptr = reserved_quota; |
127 | 127 |
(...skipping 13 matching lines...) Expand all Loading... |
141 *file_growths, | 141 *file_growths, |
142 base::Bind(&GotReservedQuota, reserved_quota, file_growths)); | 142 base::Bind(&GotReservedQuota, reserved_quota, file_growths)); |
143 base::RunLoop().RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
144 } | 144 } |
145 | 145 |
146 // Tests that: | 146 // Tests that: |
147 // 1) We can reserve quota with no files open. | 147 // 1) We can reserve quota with no files open. |
148 // 2) Open a file, grow it, close it, and reserve quota with correct sizes. | 148 // 2) Open a file, grow it, close it, and reserve quota with correct sizes. |
149 TEST_F(QuotaReservationTest, ReserveQuota) { | 149 TEST_F(QuotaReservationTest, ReserveQuota) { |
150 GURL origin(kOrigin); | 150 GURL origin(kOrigin); |
151 fileapi::FileSystemType type = kType; | 151 storage::FileSystemType type = kType; |
152 | 152 |
153 scoped_refptr<fileapi::QuotaReservation> reservation( | 153 scoped_refptr<storage::QuotaReservation> reservation( |
154 reservation_manager()->CreateReservation(origin, type)); | 154 reservation_manager()->CreateReservation(origin, type)); |
155 scoped_refptr<QuotaReservation> test = | 155 scoped_refptr<QuotaReservation> test = |
156 CreateQuotaReservation(reservation, origin, type); | 156 CreateQuotaReservation(reservation, origin, type); |
157 | 157 |
158 // Reserve quota with no files open. | 158 // Reserve quota with no files open. |
159 int64 amount = 100; | 159 int64 amount = 100; |
160 int64 reserved_quota; | 160 int64 reserved_quota; |
161 ppapi::FileGrowthMap file_growths; | 161 ppapi::FileGrowthMap file_growths; |
162 ReserveQuota(test, amount, &reserved_quota, &file_growths); | 162 ReserveQuota(test, amount, &reserved_quota, &file_growths); |
163 EXPECT_EQ(amount, reserved_quota); | 163 EXPECT_EQ(amount, reserved_quota); |
(...skipping 18 matching lines...) Expand all Loading... |
182 EXPECT_EQ(amount, reservation->remaining_quota()); | 182 EXPECT_EQ(amount, reservation->remaining_quota()); |
183 test->CloseFile(kFile1ID, ppapi::FileGrowth(new_file_size, 0)); | 183 test->CloseFile(kFile1ID, ppapi::FileGrowth(new_file_size, 0)); |
184 EXPECT_EQ(amount - (new_file_size - file_size), | 184 EXPECT_EQ(amount - (new_file_size - file_size), |
185 reservation->remaining_quota()); | 185 reservation->remaining_quota()); |
186 } | 186 } |
187 | 187 |
188 // Tests that: | 188 // Tests that: |
189 // 1) We can open and close multiple files. | 189 // 1) We can open and close multiple files. |
190 TEST_F(QuotaReservationTest, MultipleFiles) { | 190 TEST_F(QuotaReservationTest, MultipleFiles) { |
191 GURL origin(kOrigin); | 191 GURL origin(kOrigin); |
192 fileapi::FileSystemType type = kType; | 192 storage::FileSystemType type = kType; |
193 | 193 |
194 scoped_refptr<fileapi::QuotaReservation> reservation( | 194 scoped_refptr<storage::QuotaReservation> reservation( |
195 reservation_manager()->CreateReservation(origin, type)); | 195 reservation_manager()->CreateReservation(origin, type)); |
196 scoped_refptr<QuotaReservation> test = | 196 scoped_refptr<QuotaReservation> test = |
197 CreateQuotaReservation(reservation, origin, type); | 197 CreateQuotaReservation(reservation, origin, type); |
198 | 198 |
199 // Open some files of different sizes. | 199 // Open some files of different sizes. |
200 int64 file1_size = 10; | 200 int64 file1_size = 10; |
201 SetFileSize(file1_name, file1_size); | 201 SetFileSize(file1_name, file1_size); |
202 int64 open_file1_size = | 202 int64 open_file1_size = |
203 test->OpenFile(kFile1ID, MakeFileSystemURL(file1_name)); | 203 test->OpenFile(kFile1ID, MakeFileSystemURL(file1_name)); |
204 EXPECT_EQ(file1_size, open_file1_size); | 204 EXPECT_EQ(file1_size, open_file1_size); |
(...skipping 30 matching lines...) Expand all Loading... |
235 EXPECT_EQ(amount, reserved_quota); | 235 EXPECT_EQ(amount, reserved_quota); |
236 EXPECT_EQ(2U, file_growths.size()); | 236 EXPECT_EQ(2U, file_growths.size()); |
237 EXPECT_EQ(file1_size, file_growths[kFile1ID].max_written_offset); | 237 EXPECT_EQ(file1_size, file_growths[kFile1ID].max_written_offset); |
238 EXPECT_EQ(file3_size, file_growths[kFile3ID].max_written_offset); | 238 EXPECT_EQ(file3_size, file_growths[kFile3ID].max_written_offset); |
239 | 239 |
240 test->CloseFile(kFile1ID, ppapi::FileGrowth(file1_size, 0)); | 240 test->CloseFile(kFile1ID, ppapi::FileGrowth(file1_size, 0)); |
241 test->CloseFile(kFile3ID, ppapi::FileGrowth(file3_size, 0)); | 241 test->CloseFile(kFile3ID, ppapi::FileGrowth(file3_size, 0)); |
242 } | 242 } |
243 | 243 |
244 } // namespace content | 244 } // namespace content |
OLD | NEW |