1 // Copyright (c) 2012 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 "testing/gtest/include/gtest/gtest.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "chrome/browser/browsing_data/browsing_data_quota_helper_impl.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "webkit/browser/quota/mock_storage_client.h"
16 #include "webkit/browser/quota/quota_manager.h"
18 using content::BrowserThread
;
20 class BrowsingDataQuotaHelperTest
: public testing::Test
{
22 typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo
;
23 typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray
;
25 BrowsingDataQuotaHelperTest()
26 : fetching_completed_(true),
28 weak_factory_(this) {}
30 virtual ~BrowsingDataQuotaHelperTest() {}
32 virtual void SetUp() OVERRIDE
{
33 EXPECT_TRUE(dir_
.CreateUniqueTempDir());
34 quota_manager_
= new quota::QuotaManager(
37 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
38 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
40 helper_
= new BrowsingDataQuotaHelperImpl(
41 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
).get(),
42 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
43 quota_manager_
.get());
46 virtual void TearDown() OVERRIDE
{
48 quota_manager_
= NULL
;
50 base::MessageLoop::current()->RunUntilIdle();
54 const QuotaInfoArray
& quota_info() const {
58 bool fetching_completed() const {
59 return fetching_completed_
;
62 void StartFetching() {
63 fetching_completed_
= false;
64 helper_
->StartFetching(
65 base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted
,
66 weak_factory_
.GetWeakPtr()));
69 void RegisterClient(const quota::MockOriginData
* data
, std::size_t data_len
) {
70 quota::MockStorageClient
* client
=
71 new quota::MockStorageClient(
72 quota_manager_
->proxy(), data
, quota::QuotaClient::kFileSystem
,
74 quota_manager_
->proxy()->RegisterClient(client
);
75 client
->TouchAllOriginsAndNotify();
78 void SetPersistentHostQuota(const std::string
& host
, int64 quota
) {
80 quota_manager_
->SetPersistentHostQuota(
82 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota
,
83 weak_factory_
.GetWeakPtr()));
86 void GetPersistentHostQuota(const std::string
& host
) {
88 quota_manager_
->GetPersistentHostQuota(
90 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota
,
91 weak_factory_
.GetWeakPtr()));
94 void GotPersistentHostQuota(quota::QuotaStatusCode status
,
96 EXPECT_EQ(quota::kQuotaStatusOk
, status
);
100 void RevokeHostQuota(const std::string
& host
) {
101 helper_
->RevokeHostQuota(host
);
109 void FetchCompleted(const QuotaInfoArray
& quota_info
) {
110 quota_info_
= quota_info
;
111 fetching_completed_
= true;
114 content::TestBrowserThreadBundle thread_bundle_
;
115 scoped_refptr
<quota::QuotaManager
> quota_manager_
;
117 base::ScopedTempDir dir_
;
118 scoped_refptr
<BrowsingDataQuotaHelper
> helper_
;
120 bool fetching_completed_
;
121 QuotaInfoArray quota_info_
;
123 base::WeakPtrFactory
<BrowsingDataQuotaHelperTest
> weak_factory_
;
125 DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest
);
128 TEST_F(BrowsingDataQuotaHelperTest
, Empty
) {
130 base::MessageLoop::current()->RunUntilIdle();
131 EXPECT_TRUE(fetching_completed());
132 EXPECT_TRUE(quota_info().empty());
135 TEST_F(BrowsingDataQuotaHelperTest
, FetchData
) {
136 const quota::MockOriginData kOrigins
[] = {
137 {"http://example.com/", quota::kStorageTypeTemporary
, 1},
138 {"https://example.com/", quota::kStorageTypeTemporary
, 10},
139 {"http://example.com/", quota::kStorageTypePersistent
, 100},
140 {"https://example.com/", quota::kStorageTypeSyncable
, 1},
141 {"http://example2.com/", quota::kStorageTypeTemporary
, 1000},
144 RegisterClient(kOrigins
, arraysize(kOrigins
));
146 base::MessageLoop::current()->RunUntilIdle();
147 EXPECT_TRUE(fetching_completed());
149 std::set
<QuotaInfo
> expected
, actual
;
150 actual
.insert(quota_info().begin(), quota_info().end());
151 expected
.insert(QuotaInfo("example.com", 11, 100, 1));
152 expected
.insert(QuotaInfo("example2.com", 1000, 0, 0));
153 EXPECT_TRUE(expected
== actual
);
156 TEST_F(BrowsingDataQuotaHelperTest
, IgnoreExtensionsAndDevTools
) {
157 const quota::MockOriginData kOrigins
[] = {
158 {"http://example.com/", quota::kStorageTypeTemporary
, 1},
159 {"https://example.com/", quota::kStorageTypeTemporary
, 10},
160 {"http://example.com/", quota::kStorageTypePersistent
, 100},
161 {"https://example.com/", quota::kStorageTypeSyncable
, 1},
162 {"http://example2.com/", quota::kStorageTypeTemporary
, 1000},
163 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
164 quota::kStorageTypeTemporary
, 10000},
165 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
166 quota::kStorageTypePersistent
, 100000},
167 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
168 quota::kStorageTypeTemporary
, 10000},
169 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
170 quota::kStorageTypePersistent
, 100000},
173 RegisterClient(kOrigins
, arraysize(kOrigins
));
175 base::MessageLoop::current()->RunUntilIdle();
176 EXPECT_TRUE(fetching_completed());
178 std::set
<QuotaInfo
> expected
, actual
;
179 actual
.insert(quota_info().begin(), quota_info().end());
180 expected
.insert(QuotaInfo("example.com", 11, 100, 1));
181 expected
.insert(QuotaInfo("example2.com", 1000, 0, 0));
182 EXPECT_TRUE(expected
== actual
);
185 TEST_F(BrowsingDataQuotaHelperTest
, RevokeHostQuota
) {
186 const std::string
kHost1("example1.com");
187 const std::string
kHost2("example2.com");
189 SetPersistentHostQuota(kHost1
, 1);
190 SetPersistentHostQuota(kHost2
, 10);
191 base::MessageLoop::current()->RunUntilIdle();
193 RevokeHostQuota(kHost1
);
194 base::MessageLoop::current()->RunUntilIdle();
196 GetPersistentHostQuota(kHost1
);
197 base::MessageLoop::current()->RunUntilIdle();
198 EXPECT_EQ(0, quota());
200 GetPersistentHostQuota(kHost2
);
201 base::MessageLoop::current()->RunUntilIdle();
202 EXPECT_EQ(10, quota());