Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: ios/chrome/browser/browsing_data/cache_counter_unittest.cc

Issue 2354643002: Add a cache counter for iOS. (Closed)
Patch Set: Fix another hangup. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 // Note that this file only tests the basic behavior of the cache counter, as in
6 // when it counts and when not, when result is nonzero and when not. It does not
7 // test whether the result of the counting is correct. This is the
8 // responsibility of a lower layer, and is tested in
9 // DiskCacheBackendTest.CalculateSizeOfAllEntries in net_unittests.
10
11 #include "ios/chrome/browser/browsing_data/cache_counter.h"
12
13 #include "base/bind.h"
14 #include "base/run_loop.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "base/time/time.h"
17 #include "components/browsing_data/core/browsing_data_utils.h"
18 #include "components/browsing_data/core/pref_names.h"
19 #include "components/prefs/pref_registry_simple.h"
20 #include "components/prefs/testing_pref_service.h"
21 #include "ios/web/public/test/test_browser_state.h"
22 #include "ios/web/public/test/test_web_thread_bundle.h"
23 #include "ios/web/public/web_thread.h"
24 #include "net/disk_cache/disk_cache.h"
25 #include "net/http/http_cache.h"
26 #include "net/http/http_transaction_factory.h"
27 #include "net/url_request/url_request_context.h"
28 #include "net/url_request/url_request_context_getter.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30
31 namespace {
32
33 class CacheCounterTest : public testing::Test {
34 public:
35 void SetUp() override {
droger 2016/09/22 14:13:01 call the parent function testing::Test::SetUp();
msramek 2016/09/22 17:02:40 Done.
36 prefs_.registry()->RegisterIntegerPref(
37 browsing_data::prefs::kDeleteTimePeriod,
38 static_cast<int>(browsing_data::ALL_TIME));
39 prefs_.registry()->RegisterBooleanPref(browsing_data::prefs::kDeleteCache,
40 true);
41
42 context_getter_ = browser_state_.GetRequestContext();
43 }
44
45 web::TestBrowserState* browser_state() { return &browser_state_; }
46
47 PrefService* prefs() { return &prefs_; }
48
49 void SetCacheDeletionPref(bool value) {
50 prefs()->SetBoolean(browsing_data::prefs::kDeleteCache, value);
51 }
52
53 void SetDeletionPeriodPref(browsing_data::TimePeriod period) {
54 prefs()->SetInteger(browsing_data::prefs::kDeleteTimePeriod,
55 static_cast<int>(period));
56 }
57
58 // Create a cache entry on the IO thread.
59 void CreateCacheEntry() {
60 current_operation_ = OPERATION_ADD_ENTRY;
61 next_step_ = STEP_GET_BACKEND;
62
63 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE,
64 base::Bind(&CacheCounterTest::CacheOperationStep,
65 base::Unretained(this), net::OK));
66 WaitForIOThread();
67 }
68
69 // Clear the cache on the IO thread.
70 void ClearCache() {
71 current_operation_ = OPERATION_CLEAR_CACHE;
72 next_step_ = STEP_GET_BACKEND;
73
74 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE,
75 base::Bind(&CacheCounterTest::CacheOperationStep,
76 base::Unretained(this), net::OK));
77 WaitForIOThread();
78 }
79
80 // Wait for IO thread operations, such as cache creation, counting, writing,
81 // deletion etc.
82 void WaitForIOThread() {
83 DCHECK_CURRENTLY_ON(web::WebThread::UI);
84 run_loop_.reset(new base::RunLoop());
85 run_loop_->Run();
86 }
87
88 // A callback method to be used by counters to report the result.
89 void CountingCallback(
90 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
91 DCHECK_CURRENTLY_ON(web::WebThread::UI);
92 finished_ = result->Finished();
93
94 if (finished_) {
95 result_ =
96 static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>(
97 result.get())
98 ->Value();
99 }
100
101 if (run_loop_ && finished_)
102 run_loop_->Quit();
103 }
104
105 // Get the last reported counter result.
106 browsing_data::BrowsingDataCounter::ResultInt GetResult() {
107 DCHECK(finished_);
108 return result_;
109 }
110
111 private:
112 enum CacheOperation {
113 OPERATION_ADD_ENTRY,
114 OPERATION_CLEAR_CACHE,
115 };
116
117 enum CacheEntryCreationStep {
118 STEP_GET_BACKEND,
119 STEP_CLEAR_CACHE,
120 STEP_CREATE_ENTRY,
121 STEP_WRITE_DATA,
122 STEP_CALLBACK,
123 };
124
125 // One step in the process of creating a cache entry. Every step must be
126 // executed on IO thread after the previous one has finished.
127 void CacheOperationStep(int rv) {
128 while (rv != net::ERR_IO_PENDING) {
129 // The testing browser state uses a memory cache which should not cause
130 // any errors.
131 DCHECK_GE(rv, 0);
132
133 switch (next_step_) {
134 case STEP_GET_BACKEND: {
135 next_step_ = current_operation_ == OPERATION_ADD_ENTRY
136 ? STEP_CREATE_ENTRY
137 : STEP_CLEAR_CACHE;
138
139 net::HttpCache* http_cache = context_getter_->GetURLRequestContext()
140 ->http_transaction_factory()
141 ->GetCache();
142
143 rv = http_cache->GetBackend(
144 &backend_, base::Bind(&CacheCounterTest::CacheOperationStep,
145 base::Unretained(this)));
146
147 break;
148 }
149
150 case STEP_CLEAR_CACHE: {
151 next_step_ = STEP_CALLBACK;
152
153 DCHECK(backend_);
154 rv = backend_->DoomAllEntries(base::Bind(
155 &CacheCounterTest::CacheOperationStep, base::Unretained(this)));
156
157 break;
158 }
159
160 case STEP_CREATE_ENTRY: {
161 next_step_ = STEP_WRITE_DATA;
162
163 DCHECK(backend_);
164 rv = backend_->CreateEntry(
165 "entry_key", &entry_,
166 base::Bind(&CacheCounterTest::CacheOperationStep,
167 base::Unretained(this)));
168
169 break;
170 }
171
172 case STEP_WRITE_DATA: {
173 next_step_ = STEP_CALLBACK;
174
175 std::string data = "entry data";
176 scoped_refptr<net::StringIOBuffer> buffer =
177 new net::StringIOBuffer(data);
178
179 rv = entry_->WriteData(
180 0, 0, buffer.get(), data.size(),
181 base::Bind(&CacheCounterTest::CacheOperationStep,
182 base::Unretained(this)),
183 true);
184
185 break;
186 }
187
188 case STEP_CALLBACK: {
189 if (current_operation_ == OPERATION_ADD_ENTRY)
190 entry_->Close();
191
192 web::WebThread::PostTask(
193 web::WebThread::UI, FROM_HERE,
194 base::Bind(&CacheCounterTest::Callback, base::Unretained(this)));
195
196 return;
197 }
198 }
199 }
200 }
201
202 // General completion callback.
203 void Callback() {
204 DCHECK_CURRENTLY_ON(web::WebThread::UI);
205 if (run_loop_)
206 run_loop_->Quit();
207 }
208
209 web::TestWebThreadBundle bundle_;
210 std::unique_ptr<base::RunLoop> run_loop_;
211
212 CacheOperation current_operation_;
213 CacheEntryCreationStep next_step_;
214
215 scoped_refptr<net::URLRequestContextGetter> context_getter_;
216 disk_cache::Backend* backend_;
217 disk_cache::Entry* entry_;
218
219 bool finished_;
220 browsing_data::BrowsingDataCounter::ResultInt result_;
221
222 web::TestBrowserState browser_state_;
223 TestingPrefServiceSimple prefs_;
224 };
225
226 // Tests that for the empty cache, the result is zero.
227 TEST_F(CacheCounterTest, Empty) {
228 CacheCounter counter(browser_state());
229 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
230 base::Unretained(this)));
231 counter.Restart();
232
233 WaitForIOThread();
234 EXPECT_EQ(0u, GetResult());
235 }
236
237 // Tests that for a non-empty cache, the result is nonzero, and after deleting
238 // its contents, it's zero again. Note that the exact value of the result
239 // is tested in DiskCacheBackendTest.CalculateSizeOfAllEntries.
240 TEST_F(CacheCounterTest, BeforeAndAfterClearing) {
241 CreateCacheEntry();
242
243 CacheCounter counter(browser_state());
244 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
245 base::Unretained(this)));
246 counter.Restart();
247
248 WaitForIOThread();
249 EXPECT_NE(0u, GetResult());
250
251 ClearCache();
252 counter.Restart();
253
254 WaitForIOThread();
255 EXPECT_EQ(0u, GetResult());
256 }
257
258 // Tests that the counter starts counting automatically when the deletion
259 // pref changes to true.
260 TEST_F(CacheCounterTest, PrefChanged) {
261 SetCacheDeletionPref(false);
262
263 CacheCounter counter(browser_state());
264 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
265 base::Unretained(this)));
266 SetCacheDeletionPref(true);
267
268 WaitForIOThread();
269 EXPECT_EQ(0u, GetResult());
270 }
271
272 // Tests that the counter does not count if the deletion preference is false.
273 TEST_F(CacheCounterTest, PrefIsFalse) {
274 SetCacheDeletionPref(false);
275
276 CacheCounter counter(browser_state());
277 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
278 base::Unretained(this)));
279 counter.Restart();
280
281 EXPECT_FALSE(counter.pending());
282 }
283
284 // Tests that the counting is restarted when the time period changes. Currently,
285 // the results should be the same for every period. This is because the counter
286 // always counts the size of the entire cache, and it is up to the UI
287 // to interpret it as exact value or upper bound.
288 TEST_F(CacheCounterTest, PeriodChanged) {
289 CreateCacheEntry();
290
291 CacheCounter counter(browser_state());
292 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
293 base::Unretained(this)));
294
295 SetDeletionPeriodPref(browsing_data::LAST_HOUR);
296 WaitForIOThread();
297 browsing_data::BrowsingDataCounter::ResultInt result = GetResult();
298
299 SetDeletionPeriodPref(browsing_data::LAST_DAY);
300 WaitForIOThread();
301 EXPECT_EQ(result, GetResult());
302
303 SetDeletionPeriodPref(browsing_data::LAST_WEEK);
304 WaitForIOThread();
305 EXPECT_EQ(result, GetResult());
306
307 SetDeletionPeriodPref(browsing_data::FOUR_WEEKS);
308 WaitForIOThread();
309 EXPECT_EQ(result, GetResult());
310
311 SetDeletionPeriodPref(browsing_data::ALL_TIME);
312 WaitForIOThread();
313 EXPECT_EQ(result, GetResult());
314 }
315
316 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698