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

Side by Side Diff: chrome/browser/browsing_data/downloads_counter_browsertest.cc

Issue 2084903002: Moved BrowsingDataCounter and part of BrowsingDataCounterUtils to components. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed round 2 of comments Created 4 years, 5 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/browsing_data/downloads_counter.h" 5 #include "chrome/browser/browsing_data/downloads_counter.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/guid.h" 10 #include "base/guid.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "chrome/browser/download/download_history.h" 13 #include "chrome/browser/download/download_history.h"
14 #include "chrome/browser/download/download_service.h" 14 #include "chrome/browser/download/download_service.h"
15 #include "chrome/browser/download/download_service_factory.h" 15 #include "chrome/browser/download/download_service_factory.h"
16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/in_process_browser_test.h" 19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "components/browsing_data/browsing_data_utils.h"
21 #include "components/browsing_data/pref_names.h"
19 #include "components/history/core/browser/download_row.h" 22 #include "components/history/core/browser/download_row.h"
20 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
21 #include "content/public/browser/download_manager.h" 24 #include "content/public/browser/download_manager.h"
22 25
23 #if defined(ENABLE_EXTENSIONS) 26 #if defined(ENABLE_EXTENSIONS)
24 #include "extensions/common/extension.h" 27 #include "extensions/common/extension.h"
25 #endif 28 #endif
26 29
27 namespace { 30 namespace {
28 31
29 class DownloadsCounterTest : public InProcessBrowserTest, 32 class DownloadsCounterTest : public InProcessBrowserTest,
30 public DownloadHistory::Observer { 33 public DownloadHistory::Observer {
31 public: 34 public:
32 void SetUpOnMainThread() override { 35 void SetUpOnMainThread() override {
33 time_ = base::Time::Now(); 36 time_ = base::Time::Now();
34 items_count_ = 0; 37 items_count_ = 0;
35 manager_ = 38 manager_ =
36 content::BrowserContext::GetDownloadManager(browser()->profile()); 39 content::BrowserContext::GetDownloadManager(browser()->profile());
37 history_ = 40 history_ =
38 DownloadServiceFactory::GetForBrowserContext(browser()->profile())-> 41 DownloadServiceFactory::GetForBrowserContext(browser()->profile())->
39 GetDownloadHistory(); 42 GetDownloadHistory();
40 history_->AddObserver(this); 43 history_->AddObserver(this);
41 44
42 otr_manager_ = 45 otr_manager_ =
43 content::BrowserContext::GetDownloadManager( 46 content::BrowserContext::GetDownloadManager(
44 browser()->profile()->GetOffTheRecordProfile()); 47 browser()->profile()->GetOffTheRecordProfile());
45 SetDownloadsDeletionPref(true); 48 SetDownloadsDeletionPref(true);
46 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); 49 SetDeletionPeriodPref(browsing_data::EVERYTHING);
47 } 50 }
48 51
49 void TearDownOnMainThread() override { 52 void TearDownOnMainThread() override {
50 history_->RemoveObserver(this); 53 history_->RemoveObserver(this);
51 } 54 }
52 55
53 // Adding and removing download items. --------------------------------------- 56 // Adding and removing download items. ---------------------------------------
54 57
55 std::string AddDownload() { 58 std::string AddDownload() {
56 std::string guid = AddDownloadInternal( 59 std::string guid = AddDownloadInternal(
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 item->Remove(); 161 item->Remove();
159 } 162 }
160 163
161 // Miscellaneous. ------------------------------------------------------------ 164 // Miscellaneous. ------------------------------------------------------------
162 165
163 void SetDownloadsDeletionPref(bool value) { 166 void SetDownloadsDeletionPref(bool value) {
164 browser()->profile()->GetPrefs()->SetBoolean( 167 browser()->profile()->GetPrefs()->SetBoolean(
165 prefs::kDeleteDownloadHistory, value); 168 prefs::kDeleteDownloadHistory, value);
166 } 169 }
167 170
168 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period) { 171 void SetDeletionPeriodPref(browsing_data::TimePeriod period) {
169 browser()->profile()->GetPrefs()->SetInteger( 172 browser()->profile()->GetPrefs()->SetInteger(
170 prefs::kDeleteTimePeriod, static_cast<int>(period)); 173 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period));
171 } 174 }
172 175
173 void RevertTimeInHours(int days) { 176 void RevertTimeInHours(int days) {
174 time_ -= base::TimeDelta::FromHours(days); 177 time_ -= base::TimeDelta::FromHours(days);
175 } 178 }
176 179
177 // Waiting for downloads to be stored. --------------------------------------- 180 // Waiting for downloads to be stored. ---------------------------------------
178 181
179 void OnDownloadStored( 182 void OnDownloadStored(
180 content::DownloadItem* item, 183 content::DownloadItem* item,
(...skipping 26 matching lines...) Expand all
207 if (guids_to_add_.empty() && ids_to_remove_.empty()) 210 if (guids_to_add_.empty() && ids_to_remove_.empty())
208 return; 211 return;
209 212
210 DCHECK(!run_loop_ || !run_loop_->running()); 213 DCHECK(!run_loop_ || !run_loop_->running());
211 run_loop_.reset(new base::RunLoop()); 214 run_loop_.reset(new base::RunLoop());
212 run_loop_->Run(); 215 run_loop_->Run();
213 } 216 }
214 217
215 // Retrieving the result. ---------------------------------------------------- 218 // Retrieving the result. ----------------------------------------------------
216 219
217 BrowsingDataCounter::ResultInt GetResult() { 220 browsing_data::BrowsingDataCounter::ResultInt GetResult() {
218 DCHECK(finished_); 221 DCHECK(finished_);
219 return result_; 222 return result_;
220 } 223 }
221 224
222 void ResultCallback(std::unique_ptr<BrowsingDataCounter::Result> result) { 225 void ResultCallback(
226 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
223 finished_ = result->Finished(); 227 finished_ = result->Finished();
224 228
225 if (finished_) { 229 if (finished_) {
226 result_ = static_cast<BrowsingDataCounter::FinishedResult*>( 230 result_ =
227 result.get())->Value(); 231 static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>(
232 result.get())
233 ->Value();
228 } 234 }
229 } 235 }
230 236
231 private: 237 private:
232 std::unique_ptr<base::RunLoop> run_loop_; 238 std::unique_ptr<base::RunLoop> run_loop_;
233 239
234 // GUIDs of download items that were added and for which we expect 240 // GUIDs of download items that were added and for which we expect
235 // the OnDownloadStored() callback to be called. 241 // the OnDownloadStored() callback to be called.
236 std::set<std::string> guids_to_add_; 242 std::set<std::string> guids_to_add_;
237 243
238 // IDs of download items that are being removed from the download service 244 // IDs of download items that are being removed from the download service
239 // and for which we expect the OnDownloadsRemoved() callback. Unlike in 245 // and for which we expect the OnDownloadsRemoved() callback. Unlike in
240 // |guids_to_add_|, we don't store GUIDs, because OnDownloadsRemoved() returns 246 // |guids_to_add_|, we don't store GUIDs, because OnDownloadsRemoved() returns
241 // a set of IDs. 247 // a set of IDs.
242 std::set<uint32_t> ids_to_remove_; 248 std::set<uint32_t> ids_to_remove_;
243 249
244 content::DownloadManager* manager_; 250 content::DownloadManager* manager_;
245 content::DownloadManager* otr_manager_; 251 content::DownloadManager* otr_manager_;
246 DownloadHistory* history_; 252 DownloadHistory* history_;
247 base::Time time_; 253 base::Time time_;
248 254
249 int items_count_; 255 int items_count_;
250 256
251 bool finished_; 257 bool finished_;
252 BrowsingDataCounter::ResultInt result_; 258 browsing_data::BrowsingDataCounter::ResultInt result_;
253 }; 259 };
254 260
255 // Tests that we count the total number of downloads correctly. 261 // Tests that we count the total number of downloads correctly.
256 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, Count) { 262 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, Count) {
257 DownloadsCounter counter; 263 Profile* profile = browser()->profile();
258 counter.Init(browser()->profile(), 264 DownloadsCounter counter(profile);
265 counter.Init(profile->GetPrefs(),
259 base::Bind(&DownloadsCounterTest::ResultCallback, 266 base::Bind(&DownloadsCounterTest::ResultCallback,
260 base::Unretained(this))); 267 base::Unretained(this)));
261 counter.Restart(); 268 counter.Restart();
262 EXPECT_EQ(0u, GetResult()); 269 EXPECT_EQ(0u, GetResult());
263 270
264 std::string first_download = AddDownload(); 271 std::string first_download = AddDownload();
265 AddDownload(); 272 AddDownload();
266 std::string last_download = AddDownload(); 273 std::string last_download = AddDownload();
267 WaitForDownloadHistory(); 274 WaitForDownloadHistory();
268 counter.Restart(); 275 counter.Restart();
269 EXPECT_EQ(3, GetResult()); 276 EXPECT_EQ(3, GetResult());
270 277
271 RemoveDownload(last_download); 278 RemoveDownload(last_download);
272 RemoveDownload(first_download); 279 RemoveDownload(first_download);
273 WaitForDownloadHistory(); 280 WaitForDownloadHistory();
274 counter.Restart(); 281 counter.Restart();
275 EXPECT_EQ(1, GetResult()); 282 EXPECT_EQ(1, GetResult());
276 283
277 AddDownload(); 284 AddDownload();
278 WaitForDownloadHistory(); 285 WaitForDownloadHistory();
279 counter.Restart(); 286 counter.Restart();
280 EXPECT_EQ(2, GetResult()); 287 EXPECT_EQ(2, GetResult());
281 } 288 }
282 289
283 // Tests that not just standard complete downloads are counted. 290 // Tests that not just standard complete downloads are counted.
284 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, Types) { 291 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, Types) {
285 DownloadsCounter counter; 292 Profile* profile = browser()->profile();
286 counter.Init(browser()->profile(), 293 DownloadsCounter counter(profile);
294 counter.Init(profile->GetPrefs(),
287 base::Bind(&DownloadsCounterTest::ResultCallback, 295 base::Bind(&DownloadsCounterTest::ResultCallback,
288 base::Unretained(this))); 296 base::Unretained(this)));
289 297
290 AddDownload(); 298 AddDownload();
291 AddDownloadWithProperties( 299 AddDownloadWithProperties(
292 content::DownloadItem::COMPLETE, 300 content::DownloadItem::COMPLETE,
293 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, 301 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
294 content::DOWNLOAD_INTERRUPT_REASON_NONE); 302 content::DOWNLOAD_INTERRUPT_REASON_NONE);
295 AddDownloadWithProperties( 303 AddDownloadWithProperties(
296 content::DownloadItem::COMPLETE, 304 content::DownloadItem::COMPLETE,
(...skipping 12 matching lines...) Expand all
309 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, 317 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT,
310 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); 318 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
311 319
312 WaitForDownloadHistory(); 320 WaitForDownloadHistory();
313 counter.Restart(); 321 counter.Restart();
314 EXPECT_EQ(6u, GetResult()); 322 EXPECT_EQ(6u, GetResult());
315 } 323 }
316 324
317 // Tests that downloads not persisted by DownloadHistory are not counted. 325 // Tests that downloads not persisted by DownloadHistory are not counted.
318 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, NotPersisted) { 326 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, NotPersisted) {
319 DownloadsCounter counter; 327 Profile* profile = browser()->profile();
320 counter.Init(browser()->profile(), 328 DownloadsCounter counter(profile);
329 counter.Init(profile->GetPrefs(),
321 base::Bind(&DownloadsCounterTest::ResultCallback, 330 base::Bind(&DownloadsCounterTest::ResultCallback,
322 base::Unretained(this))); 331 base::Unretained(this)));
323 332
324 // Extension and user scripts download are not persisted. 333 // Extension and user scripts download are not persisted.
325 AddDownload(); 334 AddDownload();
326 #if defined(ENABLE_EXTENSIONS) 335 #if defined(ENABLE_EXTENSIONS)
327 AddUserScriptDownload(); 336 AddUserScriptDownload();
328 AddExtensionDownload(); 337 AddExtensionDownload();
329 #endif 338 #endif
330 339
(...skipping 26 matching lines...) Expand all
357 RevertTimeInHours(10 * 24); 366 RevertTimeInHours(10 * 24);
358 AddDownload(); // 8 items 367 AddDownload(); // 8 items
359 368
360 RevertTimeInHours(30 * 24); 369 RevertTimeInHours(30 * 24);
361 AddDownload(); 370 AddDownload();
362 AddDownload(); 371 AddDownload();
363 AddDownload(); // 11 items 372 AddDownload(); // 11 items
364 373
365 WaitForDownloadHistory(); 374 WaitForDownloadHistory();
366 375
367 DownloadsCounter counter; 376 Profile* profile = browser()->profile();
368 counter.Init(browser()->profile(), 377 DownloadsCounter counter(profile);
378 counter.Init(profile->GetPrefs(),
369 base::Bind(&DownloadsCounterTest::ResultCallback, 379 base::Bind(&DownloadsCounterTest::ResultCallback,
370 base::Unretained(this))); 380 base::Unretained(this)));
371 381
372 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR); 382 SetDeletionPeriodPref(browsing_data::LAST_HOUR);
373 EXPECT_EQ(2u, GetResult()); 383 EXPECT_EQ(2u, GetResult());
374 384
375 SetDeletionPeriodPref(BrowsingDataRemover::LAST_DAY); 385 SetDeletionPeriodPref(browsing_data::LAST_DAY);
376 EXPECT_EQ(5u, GetResult()); 386 EXPECT_EQ(5u, GetResult());
377 387
378 SetDeletionPeriodPref(BrowsingDataRemover::LAST_WEEK); 388 SetDeletionPeriodPref(browsing_data::LAST_WEEK);
379 EXPECT_EQ(7u, GetResult()); 389 EXPECT_EQ(7u, GetResult());
380 390
381 SetDeletionPeriodPref(BrowsingDataRemover::FOUR_WEEKS); 391 SetDeletionPeriodPref(browsing_data::FOUR_WEEKS);
382 EXPECT_EQ(8u, GetResult()); 392 EXPECT_EQ(8u, GetResult());
383 393
384 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); 394 SetDeletionPeriodPref(browsing_data::EVERYTHING);
385 EXPECT_EQ(11u, GetResult()); 395 EXPECT_EQ(11u, GetResult());
386 } 396 }
387 397
388 } // namespace 398 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698