Rename Animate as Begin(Main)Frame
[chromium-blink-merge.git] / content / browser / database_quota_client_unittest.cc
blob4ac6cb3cb2d7dfbe53ca559fc95f25809cac4493
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
3 // found in the LICENSE file.
5 #include <map>
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "net/base/completion_callback.h"
13 #include "net/base/net_errors.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/browser/database/database_quota_client.h"
16 #include "webkit/browser/database/database_tracker.h"
17 #include "webkit/browser/database/database_util.h"
18 #include "webkit/common/database/database_identifier.h"
20 using webkit_database::DatabaseQuotaClient;
21 using webkit_database::DatabaseTracker;
22 using webkit_database::OriginInfo;
24 namespace content {
26 // Declared to shorten the line lengths.
27 static const quota::StorageType kTemp = quota::kStorageTypeTemporary;
28 static const quota::StorageType kPerm = quota::kStorageTypePersistent;
30 // Mock tracker class the mocks up those methods of the tracker
31 // that are used by the QuotaClient.
32 class MockDatabaseTracker : public DatabaseTracker {
33 public:
34 MockDatabaseTracker()
35 : DatabaseTracker(base::FilePath(), false, NULL, NULL, NULL),
36 delete_called_count_(0),
37 async_delete_(false) {}
39 virtual bool GetOriginInfo(
40 const std::string& origin_identifier,
41 OriginInfo* info) OVERRIDE {
42 std::map<GURL, MockOriginInfo>::const_iterator found =
43 mock_origin_infos_.find(
44 webkit_database::GetOriginFromIdentifier(origin_identifier));
45 if (found == mock_origin_infos_.end())
46 return false;
47 *info = OriginInfo(found->second);
48 return true;
51 virtual bool GetAllOriginIdentifiers(
52 std::vector<std::string>* origins_identifiers) OVERRIDE {
53 std::map<GURL, MockOriginInfo>::const_iterator iter;
54 for (iter = mock_origin_infos_.begin();
55 iter != mock_origin_infos_.end();
56 ++iter) {
57 origins_identifiers->push_back(iter->second.GetOriginIdentifier());
59 return true;
62 virtual bool GetAllOriginsInfo(
63 std::vector<OriginInfo>* origins_info) OVERRIDE {
64 std::map<GURL, MockOriginInfo>::const_iterator iter;
65 for (iter = mock_origin_infos_.begin();
66 iter != mock_origin_infos_.end();
67 ++iter) {
68 origins_info->push_back(OriginInfo(iter->second));
70 return true;
73 virtual int DeleteDataForOrigin(
74 const std::string& origin_identifier,
75 const net::CompletionCallback& callback) OVERRIDE {
76 ++delete_called_count_;
77 if (async_delete()) {
78 base::MessageLoopProxy::current()->PostTask(
79 FROM_HERE,
80 base::Bind(&MockDatabaseTracker::AsyncDeleteDataForOrigin, this,
81 callback));
82 return net::ERR_IO_PENDING;
84 return net::OK;
87 void AsyncDeleteDataForOrigin(const net::CompletionCallback& callback) {
88 callback.Run(net::OK);
91 void AddMockDatabase(const GURL& origin, const char* name, int size) {
92 MockOriginInfo& info = mock_origin_infos_[origin];
93 info.set_origin(webkit_database::GetIdentifierFromOrigin(origin));
94 info.AddMockDatabase(base::ASCIIToUTF16(name), size);
97 int delete_called_count() { return delete_called_count_; }
98 bool async_delete() { return async_delete_; }
99 void set_async_delete(bool async) { async_delete_ = async; }
101 protected:
102 virtual ~MockDatabaseTracker() {}
104 private:
105 class MockOriginInfo : public OriginInfo {
106 public:
107 void set_origin(const std::string& origin_identifier) {
108 origin_identifier_ = origin_identifier;
111 void AddMockDatabase(const base::string16& name, int size) {
112 EXPECT_TRUE(database_info_.find(name) == database_info_.end());
113 database_info_[name].first = size;
114 total_size_ += size;
118 int delete_called_count_;
119 bool async_delete_;
120 std::map<GURL, MockOriginInfo> mock_origin_infos_;
124 // Base class for our test fixtures.
125 class DatabaseQuotaClientTest : public testing::Test {
126 public:
127 const GURL kOriginA;
128 const GURL kOriginB;
129 const GURL kOriginOther;
131 DatabaseQuotaClientTest()
132 : kOriginA("http://host"),
133 kOriginB("http://host:8000"),
134 kOriginOther("http://other"),
135 usage_(0),
136 mock_tracker_(new MockDatabaseTracker),
137 weak_factory_(this) {
140 int64 GetOriginUsage(
141 quota::QuotaClient* client,
142 const GURL& origin,
143 quota::StorageType type) {
144 usage_ = 0;
145 client->GetOriginUsage(
146 origin, type,
147 base::Bind(&DatabaseQuotaClientTest::OnGetOriginUsageComplete,
148 weak_factory_.GetWeakPtr()));
149 base::RunLoop().RunUntilIdle();
150 return usage_;
153 const std::set<GURL>& GetOriginsForType(
154 quota::QuotaClient* client,
155 quota::StorageType type) {
156 origins_.clear();
157 client->GetOriginsForType(
158 type,
159 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete,
160 weak_factory_.GetWeakPtr()));
161 base::RunLoop().RunUntilIdle();
162 return origins_;
165 const std::set<GURL>& GetOriginsForHost(
166 quota::QuotaClient* client,
167 quota::StorageType type,
168 const std::string& host) {
169 origins_.clear();
170 client->GetOriginsForHost(
171 type, host,
172 base::Bind(&DatabaseQuotaClientTest::OnGetOriginsComplete,
173 weak_factory_.GetWeakPtr()));
174 base::RunLoop().RunUntilIdle();
175 return origins_;
178 bool DeleteOriginData(
179 quota::QuotaClient* client,
180 quota::StorageType type,
181 const GURL& origin) {
182 delete_status_ = quota::kQuotaStatusUnknown;
183 client->DeleteOriginData(
184 origin, type,
185 base::Bind(&DatabaseQuotaClientTest::OnDeleteOriginDataComplete,
186 weak_factory_.GetWeakPtr()));
187 base::RunLoop().RunUntilIdle();
188 return delete_status_ == quota::kQuotaStatusOk;
191 MockDatabaseTracker* mock_tracker() { return mock_tracker_.get(); }
194 private:
195 void OnGetOriginUsageComplete(int64 usage) {
196 usage_ = usage;
199 void OnGetOriginsComplete(const std::set<GURL>& origins) {
200 origins_ = origins;
203 void OnDeleteOriginDataComplete(quota::QuotaStatusCode status) {
204 delete_status_ = status;
207 base::MessageLoop message_loop_;
208 int64 usage_;
209 std::set<GURL> origins_;
210 quota::QuotaStatusCode delete_status_;
211 scoped_refptr<MockDatabaseTracker> mock_tracker_;
212 base::WeakPtrFactory<DatabaseQuotaClientTest> weak_factory_;
216 TEST_F(DatabaseQuotaClientTest, GetOriginUsage) {
217 DatabaseQuotaClient client(base::MessageLoopProxy::current().get(),
218 mock_tracker());
220 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp));
221 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
223 mock_tracker()->AddMockDatabase(kOriginA, "fooDB", 1000);
224 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp));
225 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm));
227 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm));
228 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kTemp));
231 TEST_F(DatabaseQuotaClientTest, GetOriginsForHost) {
232 DatabaseQuotaClient client(base::MessageLoopProxy::current().get(),
233 mock_tracker());
235 EXPECT_EQ(kOriginA.host(), kOriginB.host());
236 EXPECT_NE(kOriginA.host(), kOriginOther.host());
238 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
239 EXPECT_TRUE(origins.empty());
241 mock_tracker()->AddMockDatabase(kOriginA, "fooDB", 1000);
242 origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
243 EXPECT_EQ(origins.size(), 1ul);
244 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
246 mock_tracker()->AddMockDatabase(kOriginB, "barDB", 1000);
247 origins = GetOriginsForHost(&client, kTemp, kOriginA.host());
248 EXPECT_EQ(origins.size(), 2ul);
249 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
250 EXPECT_TRUE(origins.find(kOriginB) != origins.end());
252 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty());
253 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty());
256 TEST_F(DatabaseQuotaClientTest, GetOriginsForType) {
257 DatabaseQuotaClient client(base::MessageLoopProxy::current().get(),
258 mock_tracker());
260 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty());
261 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
263 mock_tracker()->AddMockDatabase(kOriginA, "fooDB", 1000);
264 std::set<GURL> origins = GetOriginsForType(&client, kTemp);
265 EXPECT_EQ(origins.size(), 1ul);
266 EXPECT_TRUE(origins.find(kOriginA) != origins.end());
268 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty());
271 TEST_F(DatabaseQuotaClientTest, DeleteOriginData) {
272 DatabaseQuotaClient client(base::MessageLoopProxy::current().get(),
273 mock_tracker());
275 // Perm deletions are short circuited in the Client and
276 // should not reach the DatabaseTracker.
277 EXPECT_TRUE(DeleteOriginData(&client, kPerm, kOriginA));
278 EXPECT_EQ(0, mock_tracker()->delete_called_count());
280 mock_tracker()->set_async_delete(false);
281 EXPECT_TRUE(DeleteOriginData(&client, kTemp, kOriginA));
282 EXPECT_EQ(1, mock_tracker()->delete_called_count());
284 mock_tracker()->set_async_delete(true);
285 EXPECT_TRUE(DeleteOriginData(&client, kTemp, kOriginA));
286 EXPECT_EQ(2, mock_tracker()->delete_called_count());
289 } // namespace content