Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_quota_helper_unittest.cc
blob3307656522255d42929087a63b2c11ef58e0a61e
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"
7 #include "base/bind.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/thread_task_runner_handle.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 {
24 public:
25 typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo;
26 typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray;
28 BrowsingDataQuotaHelperTest() : weak_factory_(this) {}
30 ~BrowsingDataQuotaHelperTest() override {}
32 void SetUp() override {
33 EXPECT_TRUE(dir_.CreateUniqueTempDir());
34 quota_manager_ = new storage::QuotaManager(
35 false, dir_.path(),
36 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
37 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
38 nullptr);
39 helper_ = new BrowsingDataQuotaHelperImpl(
40 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
41 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
42 quota_manager_.get());
45 void TearDown() override {
46 helper_ = nullptr;
47 quota_manager_ = nullptr;
48 quota_info_.clear();
49 base::MessageLoop::current()->RunUntilIdle();
52 protected:
53 const QuotaInfoArray& quota_info() const {
54 return quota_info_;
57 bool fetching_completed() const {
58 return fetching_completed_;
61 void StartFetching() {
62 fetching_completed_ = false;
63 helper_->StartFetching(
64 base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted,
65 weak_factory_.GetWeakPtr()));
68 void RegisterClient(const MockOriginData* data, std::size_t data_len) {
69 MockStorageClient* client =
70 new MockStorageClient(quota_manager_->proxy(),
71 data,
72 storage::QuotaClient::kFileSystem,
73 data_len);
74 quota_manager_->proxy()->RegisterClient(client);
75 client->TouchAllOriginsAndNotify();
78 void SetPersistentHostQuota(const std::string& host, int64 quota) {
79 quota_ = -1;
80 quota_manager_->SetPersistentHostQuota(
81 host, quota,
82 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
83 weak_factory_.GetWeakPtr()));
86 void GetPersistentHostQuota(const std::string& host) {
87 quota_ = -1;
88 quota_manager_->GetPersistentHostQuota(
89 host,
90 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
91 weak_factory_.GetWeakPtr()));
94 void GotPersistentHostQuota(storage::QuotaStatusCode status, int64 quota) {
95 EXPECT_EQ(storage::kQuotaStatusOk, status);
96 quota_ = quota;
99 void RevokeHostQuota(const std::string& host) {
100 helper_->RevokeHostQuota(host);
103 int64 quota() {
104 return quota_;
107 private:
108 void FetchCompleted(const QuotaInfoArray& quota_info) {
109 quota_info_ = quota_info;
110 fetching_completed_ = true;
113 content::TestBrowserThreadBundle thread_bundle_;
114 scoped_refptr<storage::QuotaManager> quota_manager_;
116 base::ScopedTempDir dir_;
117 scoped_refptr<BrowsingDataQuotaHelper> helper_;
119 bool fetching_completed_ = true;
120 QuotaInfoArray quota_info_;
121 int64 quota_ = -1;
122 base::WeakPtrFactory<BrowsingDataQuotaHelperTest> weak_factory_;
124 DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest);
127 TEST_F(BrowsingDataQuotaHelperTest, Empty) {
128 StartFetching();
129 base::MessageLoop::current()->RunUntilIdle();
130 EXPECT_TRUE(fetching_completed());
131 EXPECT_TRUE(quota_info().empty());
134 TEST_F(BrowsingDataQuotaHelperTest, FetchData) {
135 const MockOriginData kOrigins[] = {
136 {"http://example.com/", storage::kStorageTypeTemporary, 1},
137 {"https://example.com/", storage::kStorageTypeTemporary, 10},
138 {"http://example.com/", storage::kStorageTypePersistent, 100},
139 {"https://example.com/", storage::kStorageTypeSyncable, 1},
140 {"http://example2.com/", storage::kStorageTypeTemporary, 1000},
143 RegisterClient(kOrigins, arraysize(kOrigins));
144 StartFetching();
145 base::MessageLoop::current()->RunUntilIdle();
146 EXPECT_TRUE(fetching_completed());
148 std::set<QuotaInfo> expected, actual;
149 actual.insert(quota_info().begin(), quota_info().end());
150 expected.insert(QuotaInfo("example.com", 11, 100, 1));
151 expected.insert(QuotaInfo("example2.com", 1000, 0, 0));
152 EXPECT_TRUE(expected == actual);
155 TEST_F(BrowsingDataQuotaHelperTest, IgnoreExtensionsAndDevTools) {
156 const MockOriginData kOrigins[] = {
157 {"http://example.com/", storage::kStorageTypeTemporary, 1},
158 {"https://example.com/", storage::kStorageTypeTemporary, 10},
159 {"http://example.com/", storage::kStorageTypePersistent, 100},
160 {"https://example.com/", storage::kStorageTypeSyncable, 1},
161 {"http://example2.com/", storage::kStorageTypeTemporary, 1000},
162 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
163 storage::kStorageTypeTemporary, 10000},
164 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
165 storage::kStorageTypePersistent, 100000},
166 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
167 storage::kStorageTypeTemporary, 10000},
168 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
169 storage::kStorageTypePersistent, 100000},
172 RegisterClient(kOrigins, arraysize(kOrigins));
173 StartFetching();
174 base::MessageLoop::current()->RunUntilIdle();
175 EXPECT_TRUE(fetching_completed());
177 std::set<QuotaInfo> expected, actual;
178 actual.insert(quota_info().begin(), quota_info().end());
179 expected.insert(QuotaInfo("example.com", 11, 100, 1));
180 expected.insert(QuotaInfo("example2.com", 1000, 0, 0));
181 EXPECT_TRUE(expected == actual);
184 TEST_F(BrowsingDataQuotaHelperTest, RevokeHostQuota) {
185 const std::string kHost1("example1.com");
186 const std::string kHost2("example2.com");
188 SetPersistentHostQuota(kHost1, 1);
189 SetPersistentHostQuota(kHost2, 10);
190 base::MessageLoop::current()->RunUntilIdle();
192 RevokeHostQuota(kHost1);
193 base::MessageLoop::current()->RunUntilIdle();
195 GetPersistentHostQuota(kHost1);
196 base::MessageLoop::current()->RunUntilIdle();
197 EXPECT_EQ(0, quota());
199 GetPersistentHostQuota(kHost2);
200 base::MessageLoop::current()->RunUntilIdle();
201 EXPECT_EQ(10, quota());