Update To 11.40.268.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 "storage/browser/quota/quota_manager.h"
17 #include "storage/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   ~BrowsingDataQuotaHelperTest() override {}
34
35   void SetUp() override {
36     EXPECT_TRUE(dir_.CreateUniqueTempDir());
37     quota_manager_ = new storage::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   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(quota_manager_->proxy(),
75                               data,
76                               storage::QuotaClient::kFileSystem,
77                               data_len);
78     quota_manager_->proxy()->RegisterClient(client);
79     client->TouchAllOriginsAndNotify();
80   }
81
82   void SetPersistentHostQuota(const std::string& host, int64 quota) {
83     quota_ = -1;
84     quota_manager_->SetPersistentHostQuota(
85         host, quota,
86         base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
87                    weak_factory_.GetWeakPtr()));
88   }
89
90   void GetPersistentHostQuota(const std::string& host) {
91     quota_ = -1;
92     quota_manager_->GetPersistentHostQuota(
93         host,
94         base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
95                    weak_factory_.GetWeakPtr()));
96   }
97
98   void GotPersistentHostQuota(storage::QuotaStatusCode status, int64 quota) {
99     EXPECT_EQ(storage::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<storage::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/", storage::kStorageTypeTemporary, 1},
141       {"https://example.com/", storage::kStorageTypeTemporary, 10},
142       {"http://example.com/", storage::kStorageTypePersistent, 100},
143       {"https://example.com/", storage::kStorageTypeSyncable, 1},
144       {"http://example2.com/", storage::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/", storage::kStorageTypeTemporary, 1},
162       {"https://example.com/", storage::kStorageTypeTemporary, 10},
163       {"http://example.com/", storage::kStorageTypePersistent, 100},
164       {"https://example.com/", storage::kStorageTypeSyncable, 1},
165       {"http://example2.com/", storage::kStorageTypeTemporary, 1000},
166       {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
167        storage::kStorageTypeTemporary, 10000},
168       {"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
169        storage::kStorageTypePersistent, 100000},
170       {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
171        storage::kStorageTypeTemporary, 10000},
172       {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
173        storage::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 }