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 "chrome/browser/browsing_data/browsing_data_quota_helper_impl.h"
12 #include "content/public/test/mock_storage_client.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "storage/browser/quota/quota_manager.h"
16 #include "storage/browser/quota/quota_manager_proxy.h"
18 using content::BrowserThread
;
19 using content::MockOriginData
;
20 using content::MockStorageClient
;
22 class BrowsingDataQuotaHelperTest
: public testing::Test
{
24 typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo
;
25 typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray
;
27 BrowsingDataQuotaHelperTest() : weak_factory_(this) {}
29 ~BrowsingDataQuotaHelperTest() override
{}
31 void SetUp() override
{
32 EXPECT_TRUE(dir_
.CreateUniqueTempDir());
33 quota_manager_
= new storage::QuotaManager(
35 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
36 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
38 helper_
= new BrowsingDataQuotaHelperImpl(quota_manager_
.get());
41 void TearDown() override
{
43 quota_manager_
= nullptr;
45 base::MessageLoop::current()->RunUntilIdle();
49 const QuotaInfoArray
& quota_info() const {
53 bool fetching_completed() const {
54 return fetching_completed_
;
57 void StartFetching() {
58 fetching_completed_
= false;
59 helper_
->StartFetching(
60 base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted
,
61 weak_factory_
.GetWeakPtr()));
64 void RegisterClient(const MockOriginData
* data
, std::size_t data_len
) {
65 MockStorageClient
* client
=
66 new MockStorageClient(quota_manager_
->proxy(),
68 storage::QuotaClient::kFileSystem
,
70 quota_manager_
->proxy()->RegisterClient(client
);
71 client
->TouchAllOriginsAndNotify();
74 void SetPersistentHostQuota(const std::string
& host
, int64 quota
) {
76 quota_manager_
->SetPersistentHostQuota(
78 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota
,
79 weak_factory_
.GetWeakPtr()));
82 void GetPersistentHostQuota(const std::string
& host
) {
84 quota_manager_
->GetPersistentHostQuota(
86 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota
,
87 weak_factory_
.GetWeakPtr()));
90 void GotPersistentHostQuota(storage::QuotaStatusCode status
, int64 quota
) {
91 EXPECT_EQ(storage::kQuotaStatusOk
, status
);
95 void RevokeHostQuota(const std::string
& host
) {
96 helper_
->RevokeHostQuota(host
);
104 void FetchCompleted(const QuotaInfoArray
& quota_info
) {
105 quota_info_
= quota_info
;
106 fetching_completed_
= true;
109 content::TestBrowserThreadBundle thread_bundle_
;
110 scoped_refptr
<storage::QuotaManager
> quota_manager_
;
112 base::ScopedTempDir dir_
;
113 scoped_refptr
<BrowsingDataQuotaHelper
> helper_
;
115 bool fetching_completed_
= true;
116 QuotaInfoArray quota_info_
;
118 base::WeakPtrFactory
<BrowsingDataQuotaHelperTest
> weak_factory_
;
120 DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest
);
123 TEST_F(BrowsingDataQuotaHelperTest
, Empty
) {
125 base::MessageLoop::current()->RunUntilIdle();
126 EXPECT_TRUE(fetching_completed());
127 EXPECT_TRUE(quota_info().empty());
130 TEST_F(BrowsingDataQuotaHelperTest
, FetchData
) {
131 const MockOriginData kOrigins
[] = {
132 {"http://example.com/", storage::kStorageTypeTemporary
, 1},
133 {"https://example.com/", storage::kStorageTypeTemporary
, 10},
134 {"http://example.com/", storage::kStorageTypePersistent
, 100},
135 {"https://example.com/", storage::kStorageTypeSyncable
, 1},
136 {"http://example2.com/", storage::kStorageTypeTemporary
, 1000},
139 RegisterClient(kOrigins
, arraysize(kOrigins
));
141 base::MessageLoop::current()->RunUntilIdle();
142 EXPECT_TRUE(fetching_completed());
144 std::set
<QuotaInfo
> expected
, actual
;
145 actual
.insert(quota_info().begin(), quota_info().end());
146 expected
.insert(QuotaInfo("example.com", 11, 100, 1));
147 expected
.insert(QuotaInfo("example2.com", 1000, 0, 0));
148 EXPECT_TRUE(expected
== actual
);
151 TEST_F(BrowsingDataQuotaHelperTest
, IgnoreExtensionsAndDevTools
) {
152 const MockOriginData kOrigins
[] = {
153 {"http://example.com/", storage::kStorageTypeTemporary
, 1},
154 {"https://example.com/", storage::kStorageTypeTemporary
, 10},
155 {"http://example.com/", storage::kStorageTypePersistent
, 100},
156 {"https://example.com/", storage::kStorageTypeSyncable
, 1},
157 {"http://example2.com/", storage::kStorageTypeTemporary
, 1000},
158 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
159 storage::kStorageTypeTemporary
, 10000},
160 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
161 storage::kStorageTypePersistent
, 100000},
162 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
163 storage::kStorageTypeTemporary
, 10000},
164 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
165 storage::kStorageTypePersistent
, 100000},
168 RegisterClient(kOrigins
, arraysize(kOrigins
));
170 base::MessageLoop::current()->RunUntilIdle();
171 EXPECT_TRUE(fetching_completed());
173 std::set
<QuotaInfo
> expected
, actual
;
174 actual
.insert(quota_info().begin(), quota_info().end());
175 expected
.insert(QuotaInfo("example.com", 11, 100, 1));
176 expected
.insert(QuotaInfo("example2.com", 1000, 0, 0));
177 EXPECT_TRUE(expected
== actual
);
180 TEST_F(BrowsingDataQuotaHelperTest
, RevokeHostQuota
) {
181 const std::string
kHost1("example1.com");
182 const std::string
kHost2("example2.com");
184 SetPersistentHostQuota(kHost1
, 1);
185 SetPersistentHostQuota(kHost2
, 10);
186 base::MessageLoop::current()->RunUntilIdle();
188 RevokeHostQuota(kHost1
);
189 base::MessageLoop::current()->RunUntilIdle();
191 GetPersistentHostQuota(kHost1
);
192 base::MessageLoop::current()->RunUntilIdle();
193 EXPECT_EQ(0, quota());
195 GetPersistentHostQuota(kHost2
);
196 base::MessageLoop::current()->RunUntilIdle();
197 EXPECT_EQ(10, quota());