chromeos: bluetooth: tie Proxy lifetime to object, not observer
[chromium-blink-merge.git] / chrome / browser / browsing_data_quota_helper_unittest.cc
blob153af3434b0f5494403307c4a4510a606f26f75a
1 // Copyright (c) 2011 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/memory/weak_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/message_loop_proxy.h"
11 #include "base/message_loop.h"
12 #include "base/scoped_temp_dir.h"
13 #include "chrome/browser/browsing_data_quota_helper_impl.h"
14 #include "content/test/test_browser_thread.h"
15 #include "webkit/quota/mock_storage_client.h"
16 #include "webkit/quota/quota_manager.h"
18 using content::BrowserThread;
20 class BrowsingDataQuotaHelperTest : public testing::Test {
21 public:
22 typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo;
23 typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray;
25 BrowsingDataQuotaHelperTest()
26 : ui_thread_(BrowserThread::UI, &message_loop_),
27 db_thread_(BrowserThread::DB, &message_loop_),
28 io_thread_(BrowserThread::IO, &message_loop_),
29 fetching_completed_(true),
30 quota_(-1),
31 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
33 virtual ~BrowsingDataQuotaHelperTest() {}
35 virtual void SetUp() OVERRIDE {
36 EXPECT_TRUE(dir_.CreateUniqueTempDir());
37 quota_manager_ = new quota::QuotaManager(
38 false, dir_.path(),
39 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
40 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
41 NULL);
42 helper_ = new BrowsingDataQuotaHelperImpl(
43 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
44 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
45 quota_manager_);
48 virtual void TearDown() OVERRIDE {
49 helper_ = NULL;
50 quota_manager_ = NULL;
51 quota_info_.clear();
52 MessageLoop::current()->RunAllPending();
55 protected:
56 const QuotaInfoArray& quota_info() const {
57 return quota_info_;
60 bool fetching_completed() const {
61 return fetching_completed_;
64 void StartFetching() {
65 fetching_completed_ = false;
66 helper_->StartFetching(
67 base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted,
68 weak_factory_.GetWeakPtr()));
71 void RegisterClient(const quota::MockOriginData* data, std::size_t data_len) {
72 quota::MockStorageClient* client =
73 new quota::MockStorageClient(
74 quota_manager_->proxy(), data, 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 const std::string& host,
97 quota::StorageType type,
98 int64 quota) {
99 EXPECT_EQ(quota::kQuotaStatusOk, status);
100 EXPECT_EQ(quota::kStorageTypePersistent, type);
101 quota_ = quota;
104 void RevokeHostQuota(const std::string& host) {
105 helper_->RevokeHostQuota(host);
108 int64 quota() {
109 return quota_;
112 private:
113 void FetchCompleted(const QuotaInfoArray& quota_info) {
114 quota_info_ = quota_info;
115 fetching_completed_ = true;
118 MessageLoop message_loop_;
119 content::TestBrowserThread ui_thread_;
120 content::TestBrowserThread db_thread_;
121 content::TestBrowserThread io_thread_;
122 scoped_refptr<quota::QuotaManager> quota_manager_;
124 ScopedTempDir dir_;
125 scoped_refptr<BrowsingDataQuotaHelper> helper_;
127 bool fetching_completed_;
128 QuotaInfoArray quota_info_;
129 int64 quota_;
130 base::WeakPtrFactory<BrowsingDataQuotaHelperTest> weak_factory_;
132 DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest);
135 TEST_F(BrowsingDataQuotaHelperTest, Empty) {
136 StartFetching();
137 MessageLoop::current()->RunAllPending();
138 EXPECT_TRUE(fetching_completed());
139 EXPECT_TRUE(quota_info().empty());
142 TEST_F(BrowsingDataQuotaHelperTest, FetchData) {
143 const quota::MockOriginData kOrigins[] = {
144 {"http://example.com/", quota::kStorageTypeTemporary, 1},
145 {"https://example.com/", quota::kStorageTypeTemporary, 10},
146 {"http://example.com/", quota::kStorageTypePersistent, 100},
147 {"http://example2.com/", quota::kStorageTypeTemporary, 1000},
150 RegisterClient(kOrigins, arraysize(kOrigins));
151 StartFetching();
152 MessageLoop::current()->RunAllPending();
153 EXPECT_TRUE(fetching_completed());
155 std::set<QuotaInfo> expected, actual;
156 actual.insert(quota_info().begin(), quota_info().end());
157 expected.insert(QuotaInfo("example.com", 11, 100));
158 expected.insert(QuotaInfo("example2.com", 1000, 0));
159 EXPECT_TRUE(expected == actual);
162 TEST_F(BrowsingDataQuotaHelperTest, RevokeHostQuota) {
163 const std::string kHost1("example1.com");
164 const std::string kHost2("example2.com");
166 SetPersistentHostQuota(kHost1, 1);
167 SetPersistentHostQuota(kHost2, 10);
168 MessageLoop::current()->RunAllPending();
170 RevokeHostQuota(kHost1);
171 MessageLoop::current()->RunAllPending();
173 GetPersistentHostQuota(kHost1);
174 MessageLoop::current()->RunAllPending();
175 EXPECT_EQ(0, quota());
177 GetPersistentHostQuota(kHost2);
178 MessageLoop::current()->RunAllPending();
179 EXPECT_EQ(10, quota());