| OLD | NEW |
| 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" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 content::BrowserContext::GetDownloadManager(browser()->profile()); | 39 content::BrowserContext::GetDownloadManager(browser()->profile()); |
| 40 history_ = | 40 history_ = |
| 41 DownloadServiceFactory::GetForBrowserContext(browser()->profile())-> | 41 DownloadServiceFactory::GetForBrowserContext(browser()->profile())-> |
| 42 GetDownloadHistory(); | 42 GetDownloadHistory(); |
| 43 history_->AddObserver(this); | 43 history_->AddObserver(this); |
| 44 | 44 |
| 45 otr_manager_ = | 45 otr_manager_ = |
| 46 content::BrowserContext::GetDownloadManager( | 46 content::BrowserContext::GetDownloadManager( |
| 47 browser()->profile()->GetOffTheRecordProfile()); | 47 browser()->profile()->GetOffTheRecordProfile()); |
| 48 SetDownloadsDeletionPref(true); | 48 SetDownloadsDeletionPref(true); |
| 49 SetDeletionPeriodPref(browsing_data::ALL_TIME); | 49 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void TearDownOnMainThread() override { | 52 void TearDownOnMainThread() override { |
| 53 history_->RemoveObserver(this); | 53 history_->RemoveObserver(this); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Adding and removing download items. --------------------------------------- | 56 // Adding and removing download items. --------------------------------------- |
| 57 | 57 |
| 58 std::string AddDownload() { | 58 std::string AddDownload() { |
| 59 std::string guid = AddDownloadInternal( | 59 std::string guid = AddDownloadInternal( |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 | 257 |
| 258 bool finished_; | 258 bool finished_; |
| 259 browsing_data::BrowsingDataCounter::ResultInt result_; | 259 browsing_data::BrowsingDataCounter::ResultInt result_; |
| 260 }; | 260 }; |
| 261 | 261 |
| 262 // Tests that we count the total number of downloads correctly. | 262 // Tests that we count the total number of downloads correctly. |
| 263 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, Count) { | 263 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, Count) { |
| 264 Profile* profile = browser()->profile(); | 264 Profile* profile = browser()->profile(); |
| 265 DownloadsCounter counter(profile); | 265 DownloadsCounter counter(profile); |
| 266 counter.Init(profile->GetPrefs(), | 266 counter.Init(profile->GetPrefs(), |
| 267 browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 267 base::Bind(&DownloadsCounterTest::ResultCallback, | 268 base::Bind(&DownloadsCounterTest::ResultCallback, |
| 268 base::Unretained(this))); | 269 base::Unretained(this))); |
| 269 counter.Restart(); | 270 counter.Restart(); |
| 270 EXPECT_EQ(0u, GetResult()); | 271 EXPECT_EQ(0u, GetResult()); |
| 271 | 272 |
| 272 std::string first_download = AddDownload(); | 273 std::string first_download = AddDownload(); |
| 273 AddDownload(); | 274 AddDownload(); |
| 274 std::string last_download = AddDownload(); | 275 std::string last_download = AddDownload(); |
| 275 WaitForDownloadHistory(); | 276 WaitForDownloadHistory(); |
| 276 counter.Restart(); | 277 counter.Restart(); |
| 277 EXPECT_EQ(3, GetResult()); | 278 EXPECT_EQ(3, GetResult()); |
| 278 | 279 |
| 279 RemoveDownload(last_download); | 280 RemoveDownload(last_download); |
| 280 RemoveDownload(first_download); | 281 RemoveDownload(first_download); |
| 281 WaitForDownloadHistory(); | 282 WaitForDownloadHistory(); |
| 282 counter.Restart(); | 283 counter.Restart(); |
| 283 EXPECT_EQ(1, GetResult()); | 284 EXPECT_EQ(1, GetResult()); |
| 284 | 285 |
| 285 AddDownload(); | 286 AddDownload(); |
| 286 WaitForDownloadHistory(); | 287 WaitForDownloadHistory(); |
| 287 counter.Restart(); | 288 counter.Restart(); |
| 288 EXPECT_EQ(2, GetResult()); | 289 EXPECT_EQ(2, GetResult()); |
| 289 } | 290 } |
| 290 | 291 |
| 291 // Tests that not just standard complete downloads are counted. | 292 // Tests that not just standard complete downloads are counted. |
| 292 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, Types) { | 293 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, Types) { |
| 293 Profile* profile = browser()->profile(); | 294 Profile* profile = browser()->profile(); |
| 294 DownloadsCounter counter(profile); | 295 DownloadsCounter counter(profile); |
| 295 counter.Init(profile->GetPrefs(), | 296 counter.Init(profile->GetPrefs(), |
| 297 browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 296 base::Bind(&DownloadsCounterTest::ResultCallback, | 298 base::Bind(&DownloadsCounterTest::ResultCallback, |
| 297 base::Unretained(this))); | 299 base::Unretained(this))); |
| 298 | 300 |
| 299 AddDownload(); | 301 AddDownload(); |
| 300 AddDownloadWithProperties( | 302 AddDownloadWithProperties( |
| 301 content::DownloadItem::COMPLETE, | 303 content::DownloadItem::COMPLETE, |
| 302 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, | 304 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 303 content::DOWNLOAD_INTERRUPT_REASON_NONE); | 305 content::DOWNLOAD_INTERRUPT_REASON_NONE); |
| 304 AddDownloadWithProperties( | 306 AddDownloadWithProperties( |
| 305 content::DownloadItem::COMPLETE, | 307 content::DownloadItem::COMPLETE, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 321 WaitForDownloadHistory(); | 323 WaitForDownloadHistory(); |
| 322 counter.Restart(); | 324 counter.Restart(); |
| 323 EXPECT_EQ(6u, GetResult()); | 325 EXPECT_EQ(6u, GetResult()); |
| 324 } | 326 } |
| 325 | 327 |
| 326 // Tests that downloads not persisted by DownloadHistory are not counted. | 328 // Tests that downloads not persisted by DownloadHistory are not counted. |
| 327 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, NotPersisted) { | 329 IN_PROC_BROWSER_TEST_F(DownloadsCounterTest, NotPersisted) { |
| 328 Profile* profile = browser()->profile(); | 330 Profile* profile = browser()->profile(); |
| 329 DownloadsCounter counter(profile); | 331 DownloadsCounter counter(profile); |
| 330 counter.Init(profile->GetPrefs(), | 332 counter.Init(profile->GetPrefs(), |
| 333 browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 331 base::Bind(&DownloadsCounterTest::ResultCallback, | 334 base::Bind(&DownloadsCounterTest::ResultCallback, |
| 332 base::Unretained(this))); | 335 base::Unretained(this))); |
| 333 | 336 |
| 334 // Extension and user scripts download are not persisted. | 337 // Extension and user scripts download are not persisted. |
| 335 AddDownload(); | 338 AddDownload(); |
| 336 #if BUILDFLAG(ENABLE_EXTENSIONS) | 339 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 337 AddUserScriptDownload(); | 340 AddUserScriptDownload(); |
| 338 AddExtensionDownload(); | 341 AddExtensionDownload(); |
| 339 #endif | 342 #endif |
| 340 | 343 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 370 RevertTimeInHours(30 * 24); | 373 RevertTimeInHours(30 * 24); |
| 371 AddDownload(); | 374 AddDownload(); |
| 372 AddDownload(); | 375 AddDownload(); |
| 373 AddDownload(); // 11 items | 376 AddDownload(); // 11 items |
| 374 | 377 |
| 375 WaitForDownloadHistory(); | 378 WaitForDownloadHistory(); |
| 376 | 379 |
| 377 Profile* profile = browser()->profile(); | 380 Profile* profile = browser()->profile(); |
| 378 DownloadsCounter counter(profile); | 381 DownloadsCounter counter(profile); |
| 379 counter.Init(profile->GetPrefs(), | 382 counter.Init(profile->GetPrefs(), |
| 383 browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 380 base::Bind(&DownloadsCounterTest::ResultCallback, | 384 base::Bind(&DownloadsCounterTest::ResultCallback, |
| 381 base::Unretained(this))); | 385 base::Unretained(this))); |
| 382 | 386 |
| 383 SetDeletionPeriodPref(browsing_data::LAST_HOUR); | 387 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_HOUR); |
| 384 EXPECT_EQ(2u, GetResult()); | 388 EXPECT_EQ(2u, GetResult()); |
| 385 | 389 |
| 386 SetDeletionPeriodPref(browsing_data::LAST_DAY); | 390 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_DAY); |
| 387 EXPECT_EQ(5u, GetResult()); | 391 EXPECT_EQ(5u, GetResult()); |
| 388 | 392 |
| 389 SetDeletionPeriodPref(browsing_data::LAST_WEEK); | 393 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_WEEK); |
| 390 EXPECT_EQ(7u, GetResult()); | 394 EXPECT_EQ(7u, GetResult()); |
| 391 | 395 |
| 392 SetDeletionPeriodPref(browsing_data::FOUR_WEEKS); | 396 SetDeletionPeriodPref(browsing_data::TimePeriod::FOUR_WEEKS); |
| 393 EXPECT_EQ(8u, GetResult()); | 397 EXPECT_EQ(8u, GetResult()); |
| 394 | 398 |
| 395 SetDeletionPeriodPref(browsing_data::ALL_TIME); | 399 SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME); |
| 396 EXPECT_EQ(11u, GetResult()); | 400 EXPECT_EQ(11u, GetResult()); |
| 397 } | 401 } |
| 398 | 402 |
| 399 } // namespace | 403 } // namespace |
| OLD | NEW |