Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / browsing_data / browsing_data_quota_helper_unittest.cc
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.
4
5 #include "testing/gtest/include/gtest/gtest.h"
6
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/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 "webkit/browser/quota/quota_manager.h"
17 #include "webkit/browser/quota/quota_manager_proxy.h"
18
19 using content::BrowserThread;
20 using content::MockOriginData;
21 using content::MockStorageClient;
22
23 class BrowsingDataQuotaHelperTest : public testing::Test {
24  public:
25   typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo;
26   typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray;
27
28   BrowsingDataQuotaHelperTest()
29       : fetching_completed_(true),
30         quota_(-1),
31         weak_factory_(this) {}
32
33   virtual ~BrowsingDataQuotaHelperTest() {}
34
35   virtual void SetUp() OVERRIDE {
36     EXPECT_TRUE(dir_.CreateUniqueTempDir());
37     quota_manager_ = new quota::QuotaManager(
38         false,
39         dir_.path(),
40         BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
41         BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
42         NULL);
43     helper_ = new BrowsingDataQuotaHelperImpl(
44         BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
45         BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
46         quota_manager_.get());
47   }
48
49   virtual void TearDown() OVERRIDE {
50     helper_ = NULL;
51     quota_manager_ = NULL;
52     quota_info_.clear();
53     base::MessageLoop::current()->RunUntilIdle();
54   }
55
56  protected:
57   const QuotaInfoArray& quota_info() const {
58     return quota_info_;
59   }
60
61   bool fetching_completed() const {
62     return fetching_completed_;
63   }
64
65   void StartFetching() {
66     fetching_completed_ = false;
67     helper_->StartFetching(
68         base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted,
69                    weak_factory_.GetWeakPtr()));
70   }
71
72   void RegisterClient(const MockOriginData* data, std::size_t data_len) {
73     MockStorageClient* client =
74         new MockStorageClient(
75             quota_manager_->proxy(), data, quota::QuotaClient::kFileSystem,
76             data_len);
77     quota_manager_->proxy()->RegisterClient(client);
78     client->TouchAllOriginsAndNotify();
79   }
80
81   void SetPersistentHostQuota(const std::string& host, int64 quota) {
82     quota_ = -1;
83     quota_manager_->SetPersistentHostQuota(
84         host, quota,
85         base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
86                    weak_factory_.GetWeakPtr()));
87   }
88
89   void GetPersistentHostQuota(const std::string& host) {
90     quota_ = -1;
91     quota_manager_->GetPersistentHostQuota(
92         host,
93         base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
94                    weak_factory_.GetWeakPtr()));
95   }
96
97   void GotPersistentHostQuota(quota::QuotaStatusCode status,
98                               int64 quota) {
99     EXPECT_EQ(quota::kQuotaStatusOk, status);
100     quota_ = quota;
101   }
102
103   void RevokeHostQuota(const std::string& host) {
104     helper_->RevokeHostQuota(host);
105   }
106
107   int64 quota() {
108     return quota_;
109   }
110
111  private:
112   void FetchCompleted(const QuotaInfoArray& quota_info) {
113     quota_info_ = quota_info;
114     fetching_completed_ = true;
115   }
116
117   content::TestBrowserThreadBundle thread_bundle_;
118   scoped_refptr<quota::QuotaManager> quota_manager_;
119
120   base::ScopedTempDir dir_;
121   scoped_refptr<BrowsingDataQuotaHelper> helper_;
122
123   bool fetching_completed_;
124   QuotaInfoArray quota_info_;
125   int64 quota_;
126   base::WeakPtrFactory<BrowsingDataQuotaHelperTest> weak_factory_;
127
128   DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest);
129 };
130
131 TEST_F(BrowsingDataQuotaHelperTest, Empty) {
132   StartFetching();
133   base::MessageLoop::current()->RunUntilIdle();
134   EXPECT_TRUE(fetching_completed());
135   EXPECT_TRUE(quota_info().empty());
136 }
137
138 TEST_F(BrowsingDataQuotaHelperTest, FetchData) {
139   const MockOriginData kOrigins[] = {
140     {"http://example.com/", quota::kStorageTypeTemporary, 1},
141     {"https://example.com/", quota::kStorageTypeTemporary, 10},
142     {"http://example.com/", quota::kStorageTypePersistent, 100},
143     {"https://example.com/", quota::kStorageTypeSyncable, 1},
144     {"http://example2.com/", quota::kStorageTypeTemporary, 1000},
145   };
146
147   RegisterClient(kOrigins, arraysize(kOrigins));
148   StartFetching();
149   base::MessageLoop::current()->RunUntilIdle();
150   EXPECT_TRUE(fetching_completed());
151
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);
157 }
158
159 TEST_F(BrowsingDataQuotaHelperTest, IgnoreExtensionsAndDevTools) {
160   const MockOriginData kOrigins[] = {
161     {"http://example.com/", quota::kStorageTypeTemporary, 1},
162     {"https://example.com/", quota::kStorageTypeTemporary, 10},
163     {"http://example.com/", quota::kStorageTypePersistent, 100},
164     {"https://example.com/", quota::kStorageTypeSyncable, 1},
165     {"http://example2.com/", quota::kStorageTypeTemporary, 1000},
166     {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
167         quota::kStorageTypeTemporary, 10000},
168     {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
169         quota::kStorageTypePersistent, 100000},
170     {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
171         quota::kStorageTypeTemporary, 10000},
172     {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
173         quota::kStorageTypePersistent, 100000},
174   };
175
176   RegisterClient(kOrigins, arraysize(kOrigins));
177   StartFetching();
178   base::MessageLoop::current()->RunUntilIdle();
179   EXPECT_TRUE(fetching_completed());
180
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);
186 }
187
188 TEST_F(BrowsingDataQuotaHelperTest, RevokeHostQuota) {
189   const std::string kHost1("example1.com");
190   const std::string kHost2("example2.com");
191
192   SetPersistentHostQuota(kHost1, 1);
193   SetPersistentHostQuota(kHost2, 10);
194   base::MessageLoop::current()->RunUntilIdle();
195
196   RevokeHostQuota(kHost1);
197   base::MessageLoop::current()->RunUntilIdle();
198
199   GetPersistentHostQuota(kHost1);
200   base::MessageLoop::current()->RunUntilIdle();
201   EXPECT_EQ(0, quota());
202
203   GetPersistentHostQuota(kHost2);
204   base::MessageLoop::current()->RunUntilIdle();
205   EXPECT_EQ(10, quota());
206 }