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

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

Issue 2354643002: Add a cache counter for iOS. (Closed)
Patch Set: Forgot |next_step_| 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 CacheCounterTest() {
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 ~CacheCounterTest() override {}
46
47 web::TestBrowserState* browser_state() { return &browser_state_; }
48
49 PrefService* prefs() { return &prefs_; }
50
51 void SetCacheDeletionPref(bool value) {
52 prefs()->SetBoolean(browsing_data::prefs::kDeleteCache, value);
53 }
54
55 void SetDeletionPeriodPref(browsing_data::TimePeriod period) {
56 prefs()->SetInteger(browsing_data::prefs::kDeleteTimePeriod,
57 static_cast<int>(period));
58 }
59
60 // Create a cache entry on the IO thread.
61 void CreateCacheEntry() {
62 current_operation_ = OPERATION_ADD_ENTRY;
63 next_step_ = STEP_GET_BACKEND;
64
65 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE,
66 base::Bind(&CacheCounterTest::CacheOperationStep,
67 base::Unretained(this), net::OK));
68 WaitForIOThread();
69 }
70
71 // Clear the cache on the IO thread.
72 void ClearCache() {
73 current_operation_ = OPERATION_CLEAR_CACHE;
74 next_step_ = STEP_GET_BACKEND;
75
76 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE,
77 base::Bind(&CacheCounterTest::CacheOperationStep,
78 base::Unretained(this), net::OK));
79 WaitForIOThread();
80 }
81
82 // Wait for IO thread operations, such as cache creation, counting, writing,
83 // deletion etc.
84 void WaitForIOThread() {
85 DCHECK_CURRENTLY_ON(web::WebThread::UI);
86 run_loop_.reset(new base::RunLoop());
87 run_loop_->Run();
88 }
89
90 // A callback method to be used by counters to report the result.
91 void CountingCallback(
92 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
93 DCHECK_CURRENTLY_ON(web::WebThread::UI);
94 finished_ = result->Finished();
95
96 if (finished_) {
97 result_ =
98 static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>(
99 result.get())
100 ->Value();
101 }
102
103 if (run_loop_ && finished_)
104 run_loop_->Quit();
105 }
106
107 // Get the last reported counter result.
108 browsing_data::BrowsingDataCounter::ResultInt GetResult() {
109 DCHECK(finished_);
110 return result_;
111 }
112
113 private:
114 enum CacheOperation {
115 OPERATION_ADD_ENTRY,
116 OPERATION_CLEAR_CACHE,
117 };
118
119 enum CacheEntryCreationStep {
120 STEP_GET_BACKEND,
121 STEP_CLEAR_CACHE,
122 STEP_CREATE_ENTRY,
123 STEP_WRITE_DATA,
124 STEP_CALLBACK,
125 STEP_DONE
126 };
127
128 // One step in the process of creating a cache entry or clearing the cache.
129 // Every step must be executed on IO thread after the previous one has
130 // finished.
131 void CacheOperationStep(int rv) {
132 while (rv != net::ERR_IO_PENDING && next_step_ != STEP_DONE) {
133 // The testing browser state uses a memory cache which should not cause
134 // any errors.
135 DCHECK_GE(rv, 0);
136
137 switch (next_step_) {
138 case STEP_GET_BACKEND: {
139 next_step_ = current_operation_ == OPERATION_ADD_ENTRY
140 ? STEP_CREATE_ENTRY
141 : STEP_CLEAR_CACHE;
142
143 net::HttpCache* http_cache = context_getter_->GetURLRequestContext()
144 ->http_transaction_factory()
145 ->GetCache();
146
147 rv = http_cache->GetBackend(
148 &backend_, base::Bind(&CacheCounterTest::CacheOperationStep,
149 base::Unretained(this)));
150
151 break;
152 }
153
154 case STEP_CLEAR_CACHE: {
155 next_step_ = STEP_CALLBACK;
156
157 DCHECK(backend_);
158 rv = backend_->DoomAllEntries(base::Bind(
159 &CacheCounterTest::CacheOperationStep, base::Unretained(this)));
160
161 break;
162 }
163
164 case STEP_CREATE_ENTRY: {
165 next_step_ = STEP_WRITE_DATA;
166
167 DCHECK(backend_);
168 rv = backend_->CreateEntry(
169 "entry_key", &entry_,
170 base::Bind(&CacheCounterTest::CacheOperationStep,
171 base::Unretained(this)));
172
173 break;
174 }
175
176 case STEP_WRITE_DATA: {
177 next_step_ = STEP_CALLBACK;
178
179 std::string data = "entry data";
180 scoped_refptr<net::StringIOBuffer> buffer =
181 new net::StringIOBuffer(data);
182
183 rv = entry_->WriteData(
184 0, 0, buffer.get(), data.size(),
185 base::Bind(&CacheCounterTest::CacheOperationStep,
186 base::Unretained(this)),
187 true);
188
189 break;
190 }
191
192 case STEP_CALLBACK: {
193 next_step_ = STEP_DONE;
194
195 if (current_operation_ == OPERATION_ADD_ENTRY)
196 entry_->Close();
197
198 web::WebThread::PostTask(
199 web::WebThread::UI, FROM_HERE,
200 base::Bind(&CacheCounterTest::Callback, base::Unretained(this)));
201
202 break;
203 }
204
205 case STEP_DONE: {
206 NOTREACHED();
207 }
208 }
209 }
210 }
211
212 // General completion callback.
213 void Callback() {
214 DCHECK_CURRENTLY_ON(web::WebThread::UI);
215 if (run_loop_)
216 run_loop_->Quit();
217 }
218
219 web::TestWebThreadBundle bundle_;
220 std::unique_ptr<base::RunLoop> run_loop_;
221
222 CacheOperation current_operation_;
223 CacheEntryCreationStep next_step_;
224
225 scoped_refptr<net::URLRequestContextGetter> context_getter_;
226 disk_cache::Backend* backend_;
227 disk_cache::Entry* entry_;
228
229 bool finished_;
230 browsing_data::BrowsingDataCounter::ResultInt result_;
231
232 web::TestBrowserState browser_state_;
233 TestingPrefServiceSimple prefs_;
234 };
235
236 // Tests that for the empty cache, the result is zero.
237 TEST_F(CacheCounterTest, Empty) {
238 CacheCounter counter(browser_state());
239 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
240 base::Unretained(this)));
241 counter.Restart();
242
243 WaitForIOThread();
244 EXPECT_EQ(0u, GetResult());
245 }
246
247 // Tests that for a non-empty cache, the result is nonzero, and after deleting
248 // its contents, it's zero again. Note that the exact value of the result
249 // is tested in DiskCacheBackendTest.CalculateSizeOfAllEntries.
250 TEST_F(CacheCounterTest, BeforeAndAfterClearing) {
251 CreateCacheEntry();
252
253 CacheCounter counter(browser_state());
254 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
255 base::Unretained(this)));
256 counter.Restart();
257
258 WaitForIOThread();
259 EXPECT_NE(0u, GetResult());
260
261 ClearCache();
262 counter.Restart();
263
264 WaitForIOThread();
265 EXPECT_EQ(0u, GetResult());
266 }
267
268 // Tests that the counter starts counting automatically when the deletion
269 // pref changes to true.
270 TEST_F(CacheCounterTest, PrefChanged) {
271 SetCacheDeletionPref(false);
272
273 CacheCounter counter(browser_state());
274 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
275 base::Unretained(this)));
276 SetCacheDeletionPref(true);
277
278 WaitForIOThread();
279 EXPECT_EQ(0u, GetResult());
280 }
281
282 // Tests that the counter does not count if the deletion preference is false.
283 TEST_F(CacheCounterTest, PrefIsFalse) {
284 SetCacheDeletionPref(false);
285
286 CacheCounter counter(browser_state());
287 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
288 base::Unretained(this)));
289 counter.Restart();
290
291 EXPECT_FALSE(counter.pending());
292 }
293
294 // Tests that the counting is restarted when the time period changes. Currently,
295 // the results should be the same for every period. This is because the counter
296 // always counts the size of the entire cache, and it is up to the UI
297 // to interpret it as exact value or upper bound.
298 TEST_F(CacheCounterTest, PeriodChanged) {
299 CreateCacheEntry();
300
301 CacheCounter counter(browser_state());
302 counter.Init(prefs(), base::Bind(&CacheCounterTest::CountingCallback,
303 base::Unretained(this)));
304
305 SetDeletionPeriodPref(browsing_data::LAST_HOUR);
306 WaitForIOThread();
307 browsing_data::BrowsingDataCounter::ResultInt result = GetResult();
308
309 SetDeletionPeriodPref(browsing_data::LAST_DAY);
310 WaitForIOThread();
311 EXPECT_EQ(result, GetResult());
312
313 SetDeletionPeriodPref(browsing_data::LAST_WEEK);
314 WaitForIOThread();
315 EXPECT_EQ(result, GetResult());
316
317 SetDeletionPeriodPref(browsing_data::FOUR_WEEKS);
318 WaitForIOThread();
319 EXPECT_EQ(result, GetResult());
320
321 SetDeletionPeriodPref(browsing_data::ALL_TIME);
322 WaitForIOThread();
323 EXPECT_EQ(result, GetResult());
324 }
325
326 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698