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/mock_storage_client.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "storage/browser/quota/quota_manager.h"
17 #include "storage/browser/quota/quota_manager_proxy.h"
19 using content::BrowserThread
;
20 using content::MockOriginData
;
21 using content::MockStorageClient
;
23 class BrowsingDataQuotaHelperTest
: public testing::Test
{
25 typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo
;
26 typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray
;
28 BrowsingDataQuotaHelperTest()
29 : fetching_completed_(true),
31 weak_factory_(this) {}
33 ~BrowsingDataQuotaHelperTest() override
{}
35 void SetUp() override
{
36 EXPECT_TRUE(dir_
.CreateUniqueTempDir());
37 quota_manager_
= new storage::QuotaManager(
40 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
41 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
43 helper_
= new BrowsingDataQuotaHelperImpl(
44 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
).get(),
45 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
46 quota_manager_
.get());
49 void TearDown() override
{
51 quota_manager_
= NULL
;
53 base::MessageLoop::current()->RunUntilIdle();
57 const QuotaInfoArray
& quota_info() const {
61 bool fetching_completed() const {
62 return fetching_completed_
;
65 void StartFetching() {
66 fetching_completed_
= false;
67 helper_
->StartFetching(
68 base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted
,
69 weak_factory_
.GetWeakPtr()));
72 void RegisterClient(const MockOriginData
* data
, std::size_t data_len
) {
73 MockStorageClient
* client
=
74 new MockStorageClient(quota_manager_
->proxy(),
76 storage::QuotaClient::kFileSystem
,
78 quota_manager_
->proxy()->RegisterClient(client
);
79 client
->TouchAllOriginsAndNotify();
82 void SetPersistentHostQuota(const std::string
& host
, int64 quota
) {
84 quota_manager_
->SetPersistentHostQuota(
86 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota
,
87 weak_factory_
.GetWeakPtr()));
90 void GetPersistentHostQuota(const std::string
& host
) {
92 quota_manager_
->GetPersistentHostQuota(
94 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota
,
95 weak_factory_
.GetWeakPtr()));
98 void GotPersistentHostQuota(storage::QuotaStatusCode status
, int64 quota
) {
99 EXPECT_EQ(storage::kQuotaStatusOk
, status
);
103 void RevokeHostQuota(const std::string
& host
) {
104 helper_
->RevokeHostQuota(host
);
112 void FetchCompleted(const QuotaInfoArray
& quota_info
) {
113 quota_info_
= quota_info
;
114 fetching_completed_
= true;
117 content::TestBrowserThreadBundle thread_bundle_
;
118 scoped_refptr
<storage::QuotaManager
> quota_manager_
;
120 base::ScopedTempDir dir_
;
121 scoped_refptr
<BrowsingDataQuotaHelper
> helper_
;
123 bool fetching_completed_
;
124 QuotaInfoArray quota_info_
;
126 base::WeakPtrFactory
<BrowsingDataQuotaHelperTest
> weak_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest
);
131 TEST_F(BrowsingDataQuotaHelperTest
, Empty
) {
133 base::MessageLoop::current()->RunUntilIdle();
134 EXPECT_TRUE(fetching_completed());
135 EXPECT_TRUE(quota_info().empty());
138 TEST_F(BrowsingDataQuotaHelperTest
, FetchData
) {
139 const MockOriginData kOrigins
[] = {
140 {"http://example.com/", storage::kStorageTypeTemporary
, 1},
141 {"https://example.com/", storage::kStorageTypeTemporary
, 10},
142 {"http://example.com/", storage::kStorageTypePersistent
, 100},
143 {"https://example.com/", storage::kStorageTypeSyncable
, 1},
144 {"http://example2.com/", storage::kStorageTypeTemporary
, 1000},
147 RegisterClient(kOrigins
, arraysize(kOrigins
));
149 base::MessageLoop::current()->RunUntilIdle();
150 EXPECT_TRUE(fetching_completed());
152 std::set
<QuotaInfo
> expected
, actual
;
153 actual
.insert(quota_info().begin(), quota_info().end());
154 expected
.insert(QuotaInfo("example.com", 11, 100, 1));
155 expected
.insert(QuotaInfo("example2.com", 1000, 0, 0));
156 EXPECT_TRUE(expected
== actual
);
159 TEST_F(BrowsingDataQuotaHelperTest
, IgnoreExtensionsAndDevTools
) {
160 const MockOriginData kOrigins
[] = {
161 {"http://example.com/", storage::kStorageTypeTemporary
, 1},
162 {"https://example.com/", storage::kStorageTypeTemporary
, 10},
163 {"http://example.com/", storage::kStorageTypePersistent
, 100},
164 {"https://example.com/", storage::kStorageTypeSyncable
, 1},
165 {"http://example2.com/", storage::kStorageTypeTemporary
, 1000},
166 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
167 storage::kStorageTypeTemporary
, 10000},
168 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
169 storage::kStorageTypePersistent
, 100000},
170 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
171 storage::kStorageTypeTemporary
, 10000},
172 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
173 storage::kStorageTypePersistent
, 100000},
176 RegisterClient(kOrigins
, arraysize(kOrigins
));
178 base::MessageLoop::current()->RunUntilIdle();
179 EXPECT_TRUE(fetching_completed());
181 std::set
<QuotaInfo
> expected
, actual
;
182 actual
.insert(quota_info().begin(), quota_info().end());
183 expected
.insert(QuotaInfo("example.com", 11, 100, 1));
184 expected
.insert(QuotaInfo("example2.com", 1000, 0, 0));
185 EXPECT_TRUE(expected
== actual
);
188 TEST_F(BrowsingDataQuotaHelperTest
, RevokeHostQuota
) {
189 const std::string
kHost1("example1.com");
190 const std::string
kHost2("example2.com");
192 SetPersistentHostQuota(kHost1
, 1);
193 SetPersistentHostQuota(kHost2
, 10);
194 base::MessageLoop::current()->RunUntilIdle();
196 RevokeHostQuota(kHost1
);
197 base::MessageLoop::current()->RunUntilIdle();
199 GetPersistentHostQuota(kHost1
);
200 base::MessageLoop::current()->RunUntilIdle();
201 EXPECT_EQ(0, quota());
203 GetPersistentHostQuota(kHost2
);
204 base::MessageLoop::current()->RunUntilIdle();
205 EXPECT_EQ(10, quota());