OLD | NEW |
| (Empty) |
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/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/public/test/test_browser_thread.h" | |
15 #include "webkit/quota/mock_storage_client.h" | |
16 #include "webkit/quota/quota_manager.h" | |
17 | |
18 using content::BrowserThread; | |
19 | |
20 class BrowsingDataQuotaHelperTest : public testing::Test { | |
21 public: | |
22 typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo; | |
23 typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray; | |
24 | |
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)) {} | |
32 | |
33 virtual ~BrowsingDataQuotaHelperTest() {} | |
34 | |
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_); | |
46 } | |
47 | |
48 virtual void TearDown() OVERRIDE { | |
49 helper_ = NULL; | |
50 quota_manager_ = NULL; | |
51 quota_info_.clear(); | |
52 MessageLoop::current()->RunAllPending(); | |
53 } | |
54 | |
55 protected: | |
56 const QuotaInfoArray& quota_info() const { | |
57 return quota_info_; | |
58 } | |
59 | |
60 bool fetching_completed() const { | |
61 return fetching_completed_; | |
62 } | |
63 | |
64 void StartFetching() { | |
65 fetching_completed_ = false; | |
66 helper_->StartFetching( | |
67 base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted, | |
68 weak_factory_.GetWeakPtr())); | |
69 } | |
70 | |
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, quota::QuotaClient::kFileSystem, | |
75 data_len); | |
76 quota_manager_->proxy()->RegisterClient(client); | |
77 client->TouchAllOriginsAndNotify(); | |
78 } | |
79 | |
80 void SetPersistentHostQuota(const std::string& host, int64 quota) { | |
81 quota_ = -1; | |
82 quota_manager_->SetPersistentHostQuota( | |
83 host, quota, | |
84 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota, | |
85 weak_factory_.GetWeakPtr())); | |
86 } | |
87 | |
88 void GetPersistentHostQuota(const std::string& host) { | |
89 quota_ = -1; | |
90 quota_manager_->GetPersistentHostQuota( | |
91 host, | |
92 base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota, | |
93 weak_factory_.GetWeakPtr())); | |
94 } | |
95 | |
96 void GotPersistentHostQuota(quota::QuotaStatusCode status, | |
97 const std::string& host, | |
98 quota::StorageType type, | |
99 int64 quota) { | |
100 EXPECT_EQ(quota::kQuotaStatusOk, status); | |
101 EXPECT_EQ(quota::kStorageTypePersistent, type); | |
102 quota_ = quota; | |
103 } | |
104 | |
105 void RevokeHostQuota(const std::string& host) { | |
106 helper_->RevokeHostQuota(host); | |
107 } | |
108 | |
109 int64 quota() { | |
110 return quota_; | |
111 } | |
112 | |
113 private: | |
114 void FetchCompleted(const QuotaInfoArray& quota_info) { | |
115 quota_info_ = quota_info; | |
116 fetching_completed_ = true; | |
117 } | |
118 | |
119 MessageLoop message_loop_; | |
120 content::TestBrowserThread ui_thread_; | |
121 content::TestBrowserThread db_thread_; | |
122 content::TestBrowserThread io_thread_; | |
123 scoped_refptr<quota::QuotaManager> quota_manager_; | |
124 | |
125 ScopedTempDir dir_; | |
126 scoped_refptr<BrowsingDataQuotaHelper> helper_; | |
127 | |
128 bool fetching_completed_; | |
129 QuotaInfoArray quota_info_; | |
130 int64 quota_; | |
131 base::WeakPtrFactory<BrowsingDataQuotaHelperTest> weak_factory_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest); | |
134 }; | |
135 | |
136 TEST_F(BrowsingDataQuotaHelperTest, Empty) { | |
137 StartFetching(); | |
138 MessageLoop::current()->RunAllPending(); | |
139 EXPECT_TRUE(fetching_completed()); | |
140 EXPECT_TRUE(quota_info().empty()); | |
141 } | |
142 | |
143 TEST_F(BrowsingDataQuotaHelperTest, FetchData) { | |
144 const quota::MockOriginData kOrigins[] = { | |
145 {"http://example.com/", quota::kStorageTypeTemporary, 1}, | |
146 {"https://example.com/", quota::kStorageTypeTemporary, 10}, | |
147 {"http://example.com/", quota::kStorageTypePersistent, 100}, | |
148 {"http://example2.com/", quota::kStorageTypeTemporary, 1000}, | |
149 }; | |
150 | |
151 RegisterClient(kOrigins, arraysize(kOrigins)); | |
152 StartFetching(); | |
153 MessageLoop::current()->RunAllPending(); | |
154 EXPECT_TRUE(fetching_completed()); | |
155 | |
156 std::set<QuotaInfo> expected, actual; | |
157 actual.insert(quota_info().begin(), quota_info().end()); | |
158 expected.insert(QuotaInfo("example.com", 11, 100)); | |
159 expected.insert(QuotaInfo("example2.com", 1000, 0)); | |
160 EXPECT_TRUE(expected == actual); | |
161 } | |
162 | |
163 TEST_F(BrowsingDataQuotaHelperTest, IgnoreExtensionsAndDevTools) { | |
164 const quota::MockOriginData kOrigins[] = { | |
165 {"http://example.com/", quota::kStorageTypeTemporary, 1}, | |
166 {"https://example.com/", quota::kStorageTypeTemporary, 10}, | |
167 {"http://example.com/", quota::kStorageTypePersistent, 100}, | |
168 {"http://example2.com/", quota::kStorageTypeTemporary, 1000}, | |
169 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/", | |
170 quota::kStorageTypeTemporary, 10000}, | |
171 {"chrome-extension://abcdefghijklmnopqrstuvwxyz/", | |
172 quota::kStorageTypePersistent, 100000}, | |
173 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/", | |
174 quota::kStorageTypeTemporary, 10000}, | |
175 {"chrome-devtools://abcdefghijklmnopqrstuvwxyz/", | |
176 quota::kStorageTypePersistent, 100000}, | |
177 }; | |
178 | |
179 RegisterClient(kOrigins, arraysize(kOrigins)); | |
180 StartFetching(); | |
181 MessageLoop::current()->RunAllPending(); | |
182 EXPECT_TRUE(fetching_completed()); | |
183 | |
184 std::set<QuotaInfo> expected, actual; | |
185 actual.insert(quota_info().begin(), quota_info().end()); | |
186 expected.insert(QuotaInfo("example.com", 11, 100)); | |
187 expected.insert(QuotaInfo("example2.com", 1000, 0)); | |
188 EXPECT_TRUE(expected == actual); | |
189 } | |
190 | |
191 TEST_F(BrowsingDataQuotaHelperTest, RevokeHostQuota) { | |
192 const std::string kHost1("example1.com"); | |
193 const std::string kHost2("example2.com"); | |
194 | |
195 SetPersistentHostQuota(kHost1, 1); | |
196 SetPersistentHostQuota(kHost2, 10); | |
197 MessageLoop::current()->RunAllPending(); | |
198 | |
199 RevokeHostQuota(kHost1); | |
200 MessageLoop::current()->RunAllPending(); | |
201 | |
202 GetPersistentHostQuota(kHost1); | |
203 MessageLoop::current()->RunAllPending(); | |
204 EXPECT_EQ(0, quota()); | |
205 | |
206 GetPersistentHostQuota(kHost2); | |
207 MessageLoop::current()->RunAllPending(); | |
208 EXPECT_EQ(10, quota()); | |
209 } | |
OLD | NEW |