Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_quota_helper_unittest.cc
blob8b652f25bd62e88a3d20f8e48bd860b594f9b77f
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/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"
17 #include "webkit/browser/quota/quota_manager_proxy.h"
19 using content::BrowserThread;
21 class BrowsingDataQuotaHelperTest : public testing::Test {
22 public:
23 typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo;
24 typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray;
26 BrowsingDataQuotaHelperTest()
27 : fetching_completed_(true),
28 quota_(-1),
29 weak_factory_(this) {}
31 virtual ~BrowsingDataQuotaHelperTest() {}
33 virtual void SetUp() OVERRIDE {
34 EXPECT_TRUE(dir_.CreateUniqueTempDir());
35 quota_manager_ = new quota::QuotaManager(
36 false,
37 dir_.path(),
38 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
39 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
40 NULL);
41 helper_ = new BrowsingDataQuotaHelperImpl(
42 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
43 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
44 quota_manager_.get());
47 virtual void TearDown() OVERRIDE {
48 helper_ = NULL;
49 quota_manager_ = NULL;
50 quota_info_.clear();
51 base::MessageLoop::current()->RunUntilIdle();
54 protected:
55 const QuotaInfoArray& quota_info() const {
56 return quota_info_;
59 bool fetching_completed() const {
60 return fetching_completed_;
63 void StartFetching() {
64 fetching_completed_ = false;
65 helper_->StartFetching(
66 base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted,
67 weak_factory_.GetWeakPtr()));
70 void RegisterClient(const quota::MockOriginData* data, std::size_t data_len) {
71 quota::MockStorageClient* client =
72 new quota::MockStorageClient(
73 quota_manager_->proxy(), data, quota::QuotaClient::kFileSystem,
74 data_len);
75 quota_manager_->proxy()->RegisterClient(client);
76 client->TouchAllOriginsAndNotify();
79 void SetPersistentHostQuota(const std::string& host, int64 quota) {
80 quota_ = -1;
81 quota_manager_->SetPersistentHostQuota(
82 host, quota,
83 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
84 weak_factory_.GetWeakPtr()));
87 void GetPersistentHostQuota(const std::string& host) {
88 quota_ = -1;
89 quota_manager_->GetPersistentHostQuota(
90 host,
91 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
92 weak_factory_.GetWeakPtr()));
95 void GotPersistentHostQuota(quota::QuotaStatusCode status,
96 int64 quota) {
97 EXPECT_EQ(quota::kQuotaStatusOk, status);
98 quota_ = quota;
101 void RevokeHostQuota(const std::string& host) {
102 helper_->RevokeHostQuota(host);
105 int64 quota() {
106 return quota_;
109 private:
110 void FetchCompleted(const QuotaInfoArray& quota_info) {
111 quota_info_ = quota_info;
112 fetching_completed_ = true;
115 content::TestBrowserThreadBundle thread_bundle_;
116 scoped_refptr<quota::QuotaManager> quota_manager_;
118 base::ScopedTempDir dir_;
119 scoped_refptr<BrowsingDataQuotaHelper> helper_;
121 bool fetching_completed_;
122 QuotaInfoArray quota_info_;
123 int64 quota_;
124 base::WeakPtrFactory<BrowsingDataQuotaHelperTest> weak_factory_;
126 DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest);
129 TEST_F(BrowsingDataQuotaHelperTest, Empty) {
130 StartFetching();
131 base::MessageLoop::current()->RunUntilIdle();
132 EXPECT_TRUE(fetching_completed());
133 EXPECT_TRUE(quota_info().empty());
136 TEST_F(BrowsingDataQuotaHelperTest, FetchData) {
137 const quota::MockOriginData kOrigins[] = {
138 {"http://example.com/", quota::kStorageTypeTemporary, 1},
139 {"https://example.com/", quota::kStorageTypeTemporary, 10},
140 {"http://example.com/", quota::kStorageTypePersistent, 100},
141 {"https://example.com/", quota::kStorageTypeSyncable, 1},
142 {"http://example2.com/", quota::kStorageTypeTemporary, 1000},
145 RegisterClient(kOrigins, arraysize(kOrigins));
146 StartFetching();
147 base::MessageLoop::current()->RunUntilIdle();
148 EXPECT_TRUE(fetching_completed());
150 std::set<QuotaInfo> expected, actual;
151 actual.insert(quota_info().begin(), quota_info().end());
152 expected.insert(QuotaInfo("example.com", 11, 100, 1));
153 expected.insert(QuotaInfo("example2.com", 1000, 0, 0));
154 EXPECT_TRUE(expected == actual);
157 TEST_F(BrowsingDataQuotaHelperTest, IgnoreExtensionsAndDevTools) {
158 const quota::MockOriginData kOrigins[] = {
159 {"http://example.com/", quota::kStorageTypeTemporary, 1},
160 {"https://example.com/", quota::kStorageTypeTemporary, 10},
161 {"http://example.com/", quota::kStorageTypePersistent, 100},
162 {"https://example.com/", quota::kStorageTypeSyncable, 1},
163 {"http://example2.com/", quota::kStorageTypeTemporary, 1000},
164 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
165 quota::kStorageTypeTemporary, 10000},
166 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
167 quota::kStorageTypePersistent, 100000},
168 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
169 quota::kStorageTypeTemporary, 10000},
170 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
171 quota::kStorageTypePersistent, 100000},
174 RegisterClient(kOrigins, arraysize(kOrigins));
175 StartFetching();
176 base::MessageLoop::current()->RunUntilIdle();
177 EXPECT_TRUE(fetching_completed());
179 std::set<QuotaInfo> expected, actual;
180 actual.insert(quota_info().begin(), quota_info().end());
181 expected.insert(QuotaInfo("example.com", 11, 100, 1));
182 expected.insert(QuotaInfo("example2.com", 1000, 0, 0));
183 EXPECT_TRUE(expected == actual);
186 TEST_F(BrowsingDataQuotaHelperTest, RevokeHostQuota) {
187 const std::string kHost1("example1.com");
188 const std::string kHost2("example2.com");
190 SetPersistentHostQuota(kHost1, 1);
191 SetPersistentHostQuota(kHost2, 10);
192 base::MessageLoop::current()->RunUntilIdle();
194 RevokeHostQuota(kHost1);
195 base::MessageLoop::current()->RunUntilIdle();
197 GetPersistentHostQuota(kHost1);
198 base::MessageLoop::current()->RunUntilIdle();
199 EXPECT_EQ(0, quota());
201 GetPersistentHostQuota(kHost2);
202 base::MessageLoop::current()->RunUntilIdle();
203 EXPECT_EQ(10, quota());