| 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/ntp_snippets/download_suggestions_provider.h" | 5 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/test/simple_test_clock.h" |
| 14 #include "base/time/default_clock.h" |
| 13 #include "chrome/browser/ntp_snippets/fake_download_item.h" | 15 #include "chrome/browser/ntp_snippets/fake_download_item.h" |
| 14 #include "components/ntp_snippets/category.h" | 16 #include "components/ntp_snippets/category.h" |
| 15 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" | 17 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" |
| 16 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h" | 18 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h" |
| 17 #include "components/offline_pages/core/client_namespace_constants.h" | 19 #include "components/offline_pages/core/client_namespace_constants.h" |
| 18 #include "components/prefs/testing_pref_service.h" | 20 #include "components/prefs/testing_pref_service.h" |
| 19 #include "content/public/test/mock_download_item.h" | 21 #include "content/public/test/mock_download_item.h" |
| 20 #include "content/public/test/mock_download_manager.h" | 22 #include "content/public/test/mock_download_manager.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 EXPECT_CALL(observer_, | 263 EXPECT_CALL(observer_, |
| 262 OnCategoryStatusChanged(_, downloads_category(), | 264 OnCategoryStatusChanged(_, downloads_category(), |
| 263 CategoryStatus::AVAILABLE_LOADING)) | 265 CategoryStatus::AVAILABLE_LOADING)) |
| 264 .Times(AnyNumber()); | 266 .Times(AnyNumber()); |
| 265 } | 267 } |
| 266 | 268 |
| 267 void IgnoreOnSuggestionInvalidated() { | 269 void IgnoreOnSuggestionInvalidated() { |
| 268 EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber()); | 270 EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber()); |
| 269 } | 271 } |
| 270 | 272 |
| 271 DownloadSuggestionsProvider* CreateLoadedProvider(bool show_assets, | 273 DownloadSuggestionsProvider* CreateLoadedProvider( |
| 272 bool show_offline_pages) { | 274 bool show_assets, |
| 273 CreateProvider(show_assets, show_offline_pages); | 275 bool show_offline_pages, |
| 276 std::unique_ptr<base::Clock> clock) { |
| 277 CreateProvider(show_assets, show_offline_pages, std::move(clock)); |
| 274 FireHistoryQueryComplete(); | 278 FireHistoryQueryComplete(); |
| 275 return provider_.get(); | 279 return provider_.get(); |
| 276 } | 280 } |
| 277 | 281 |
| 278 DownloadSuggestionsProvider* CreateProvider(bool show_assets, | 282 DownloadSuggestionsProvider* CreateProvider( |
| 279 bool show_offline_pages) { | 283 bool show_assets, |
| 284 bool show_offline_pages, |
| 285 std::unique_ptr<base::Clock> clock) { |
| 280 DCHECK(!provider_); | 286 DCHECK(!provider_); |
| 281 DCHECK(show_assets || show_offline_pages); | 287 DCHECK(show_assets || show_offline_pages); |
| 282 | 288 |
| 283 // TODO(crbug.com/681766): Extract DownloadHistory interface and move | 289 // TODO(crbug.com/681766): Extract DownloadHistory interface and move |
| 284 // implementation into DownloadHistoryImpl. Then mock it. | 290 // implementation into DownloadHistoryImpl. Then mock it. |
| 285 provider_ = base::MakeUnique<DownloadSuggestionsProvider>( | 291 provider_ = base::MakeUnique<DownloadSuggestionsProvider>( |
| 286 &observer_, show_offline_pages ? &offline_pages_model_ : nullptr, | 292 &observer_, show_offline_pages ? &offline_pages_model_ : nullptr, |
| 287 show_assets ? &downloads_manager_ : nullptr, &download_history_, | 293 show_assets ? &downloads_manager_ : nullptr, &download_history_, |
| 288 pref_service()); | 294 pref_service(), std::move(clock)); |
| 289 return provider_.get(); | 295 return provider_.get(); |
| 290 } | 296 } |
| 291 | 297 |
| 292 void DestroyProvider() { provider_.reset(); } | 298 void DestroyProvider() { provider_.reset(); } |
| 293 | 299 |
| 294 Category downloads_category() { | 300 Category downloads_category() { |
| 295 return Category::FromKnownCategory( | 301 return Category::FromKnownCategory( |
| 296 ntp_snippets::KnownCategories::DOWNLOADS); | 302 ntp_snippets::KnownCategories::DOWNLOADS); |
| 297 } | 303 } |
| 298 | 304 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 OnNewSuggestions( | 385 OnNewSuggestions( |
| 380 _, downloads_category(), | 386 _, downloads_category(), |
| 381 UnorderedElementsAre(AllOf(HasUrl("http://dummy.com/1"), | 387 UnorderedElementsAre(AllOf(HasUrl("http://dummy.com/1"), |
| 382 HasDownloadSuggestionExtra( | 388 HasDownloadSuggestionExtra( |
| 383 /*is_download_asset=*/false, | 389 /*is_download_asset=*/false, |
| 384 FILE_PATH_LITERAL(""), "")), | 390 FILE_PATH_LITERAL(""), "")), |
| 385 AllOf(HasUrl("http://dummy.com/2"), | 391 AllOf(HasUrl("http://dummy.com/2"), |
| 386 HasDownloadSuggestionExtra( | 392 HasDownloadSuggestionExtra( |
| 387 /*is_download_asset=*/false, | 393 /*is_download_asset=*/false, |
| 388 FILE_PATH_LITERAL(""), ""))))); | 394 FILE_PATH_LITERAL(""), ""))))); |
| 389 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 395 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 396 base::MakeUnique<base::DefaultClock>()); |
| 390 } | 397 } |
| 391 | 398 |
| 392 TEST_F(DownloadSuggestionsProviderTest, | 399 TEST_F(DownloadSuggestionsProviderTest, |
| 393 ShouldConvertDownloadItemsToSuggestions) { | 400 ShouldConvertDownloadItemsToSuggestions) { |
| 394 IgnoreOnCategoryStatusChangedToAvailable(); | 401 IgnoreOnCategoryStatusChangedToAvailable(); |
| 395 IgnoreOnSuggestionInvalidated(); | 402 IgnoreOnSuggestionInvalidated(); |
| 396 | 403 |
| 397 EXPECT_CALL(*observer(), | 404 EXPECT_CALL(*observer(), |
| 398 OnNewSuggestions(_, downloads_category(), SizeIs(0))); | 405 OnNewSuggestions(_, downloads_category(), SizeIs(0))); |
| 399 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 406 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 407 base::MakeUnique<base::DefaultClock>()); |
| 400 | 408 |
| 401 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = | 409 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = |
| 402 CreateDummyAssetDownloads({1, 2}); | 410 CreateDummyAssetDownloads({1, 2}); |
| 403 | 411 |
| 404 EXPECT_CALL(*observer(), | 412 EXPECT_CALL(*observer(), |
| 405 OnNewSuggestions(_, downloads_category(), | 413 OnNewSuggestions(_, downloads_category(), |
| 406 UnorderedElementsAre(AllOf( | 414 UnorderedElementsAre(AllOf( |
| 407 HasUrl("http://download.com/1"), | 415 HasUrl("http://download.com/1"), |
| 408 HasDownloadSuggestionExtra( | 416 HasDownloadSuggestionExtra( |
| 409 /*is_download_asset=*/true, | 417 /*is_download_asset=*/true, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 430 | 438 |
| 431 TEST_F(DownloadSuggestionsProviderTest, ShouldMixInBothSources) { | 439 TEST_F(DownloadSuggestionsProviderTest, ShouldMixInBothSources) { |
| 432 IgnoreOnCategoryStatusChangedToAvailable(); | 440 IgnoreOnCategoryStatusChangedToAvailable(); |
| 433 IgnoreOnSuggestionInvalidated(); | 441 IgnoreOnSuggestionInvalidated(); |
| 434 | 442 |
| 435 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 443 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 436 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 444 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 437 UnorderedElementsAre( | 445 UnorderedElementsAre( |
| 438 HasUrl("http://dummy.com/1"), | 446 HasUrl("http://dummy.com/1"), |
| 439 HasUrl("http://dummy.com/2")))); | 447 HasUrl("http://dummy.com/2")))); |
| 440 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 448 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 449 base::MakeUnique<base::DefaultClock>()); |
| 441 | 450 |
| 442 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = | 451 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = |
| 443 CreateDummyAssetDownloads({1, 2}); | 452 CreateDummyAssetDownloads({1, 2}); |
| 444 | 453 |
| 445 EXPECT_CALL( | 454 EXPECT_CALL( |
| 446 *observer(), | 455 *observer(), |
| 447 OnNewSuggestions(_, downloads_category(), | 456 OnNewSuggestions(_, downloads_category(), |
| 448 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 457 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 449 HasUrl("http://dummy.com/2"), | 458 HasUrl("http://dummy.com/2"), |
| 450 HasUrl("http://download.com/1")))); | 459 HasUrl("http://download.com/1")))); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 471 | 480 |
| 472 (*offline_pages_model()->mutable_items()) | 481 (*offline_pages_model()->mutable_items()) |
| 473 .push_back(CreateDummyOfflinePage(1, yesterday)); | 482 .push_back(CreateDummyOfflinePage(1, yesterday)); |
| 474 (*offline_pages_model()->mutable_items()) | 483 (*offline_pages_model()->mutable_items()) |
| 475 .push_back(CreateDummyOfflinePage(2, tomorrow)); | 484 .push_back(CreateDummyOfflinePage(2, tomorrow)); |
| 476 | 485 |
| 477 EXPECT_CALL(*observer(), | 486 EXPECT_CALL(*observer(), |
| 478 OnNewSuggestions(_, downloads_category(), | 487 OnNewSuggestions(_, downloads_category(), |
| 479 ElementsAre(HasUrl("http://dummy.com/2"), | 488 ElementsAre(HasUrl("http://dummy.com/2"), |
| 480 HasUrl("http://dummy.com/1")))); | 489 HasUrl("http://dummy.com/1")))); |
| 481 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 490 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 491 base::MakeUnique<base::DefaultClock>()); |
| 482 | 492 |
| 483 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads; | 493 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads; |
| 484 asset_downloads.push_back(CreateDummyAssetDownload(3, next_week)); | 494 asset_downloads.push_back(CreateDummyAssetDownload(3, next_week)); |
| 485 asset_downloads.push_back(CreateDummyAssetDownload(4, now)); | 495 asset_downloads.push_back(CreateDummyAssetDownload(4, now)); |
| 486 | 496 |
| 487 EXPECT_CALL(*observer(), | 497 EXPECT_CALL(*observer(), |
| 488 OnNewSuggestions(_, downloads_category(), | 498 OnNewSuggestions(_, downloads_category(), |
| 489 ElementsAre(HasUrl("http://download.com/3"), | 499 ElementsAre(HasUrl("http://download.com/3"), |
| 490 HasUrl("http://dummy.com/2"), | 500 HasUrl("http://dummy.com/2"), |
| 491 HasUrl("http://dummy.com/1")))); | 501 HasUrl("http://dummy.com/1")))); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 506 | 516 |
| 507 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 517 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 508 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 518 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 509 EXPECT_CALL( | 519 EXPECT_CALL( |
| 510 *observer(), | 520 *observer(), |
| 511 OnNewSuggestions(_, downloads_category(), | 521 OnNewSuggestions(_, downloads_category(), |
| 512 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 522 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 513 HasUrl("http://dummy.com/2"), | 523 HasUrl("http://dummy.com/2"), |
| 514 HasUrl("http://download.com/1"), | 524 HasUrl("http://download.com/1"), |
| 515 HasUrl("http://download.com/2")))); | 525 HasUrl("http://download.com/2")))); |
| 516 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 526 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 527 base::MakeUnique<base::DefaultClock>()); |
| 517 | 528 |
| 518 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); | 529 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); |
| 519 EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, _)).Times(0); | 530 EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, _)).Times(0); |
| 520 provider()->DismissSuggestion( | 531 provider()->DismissSuggestion( |
| 521 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 532 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 522 provider()->DismissSuggestion( | 533 provider()->DismissSuggestion( |
| 523 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 534 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 524 | 535 |
| 525 // |downloads_manager_| is destroyed after the |provider_|, so the provider | 536 // |downloads_manager_| is destroyed after the |provider_|, so the provider |
| 526 // will not observe download items being destroyed. | 537 // will not observe download items being destroyed. |
| 527 } | 538 } |
| 528 | 539 |
| 529 TEST_F(DownloadSuggestionsProviderTest, | 540 TEST_F(DownloadSuggestionsProviderTest, |
| 530 ShouldNotReportDismissedSuggestionsOnNewData) { | 541 ShouldNotReportDismissedSuggestionsOnNewData) { |
| 531 IgnoreOnCategoryStatusChangedToAvailable(); | 542 IgnoreOnCategoryStatusChangedToAvailable(); |
| 532 | 543 |
| 533 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 544 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 534 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 545 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 535 EXPECT_CALL( | 546 EXPECT_CALL( |
| 536 *observer(), | 547 *observer(), |
| 537 OnNewSuggestions(_, downloads_category(), | 548 OnNewSuggestions(_, downloads_category(), |
| 538 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 549 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 539 HasUrl("http://dummy.com/2"), | 550 HasUrl("http://dummy.com/2"), |
| 540 HasUrl("http://download.com/1"), | 551 HasUrl("http://download.com/1"), |
| 541 HasUrl("http://download.com/2")))); | 552 HasUrl("http://download.com/2")))); |
| 542 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 553 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 554 base::MakeUnique<base::DefaultClock>()); |
| 543 | 555 |
| 544 provider()->DismissSuggestion( | 556 provider()->DismissSuggestion( |
| 545 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 557 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 546 provider()->DismissSuggestion( | 558 provider()->DismissSuggestion( |
| 547 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 559 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 548 | 560 |
| 549 EXPECT_CALL( | 561 EXPECT_CALL( |
| 550 *observer(), | 562 *observer(), |
| 551 OnNewSuggestions(_, downloads_category(), | 563 OnNewSuggestions(_, downloads_category(), |
| 552 UnorderedElementsAre(HasUrl("http://dummy.com/2"), | 564 UnorderedElementsAre(HasUrl("http://dummy.com/2"), |
| 553 HasUrl("http://dummy.com/3"), | 565 HasUrl("http://dummy.com/3"), |
| 554 HasUrl("http://download.com/2")))); | 566 HasUrl("http://download.com/2")))); |
| 555 AddOfflinePage(CreateDummyOfflinePage(3)); | 567 AddOfflinePage(CreateDummyOfflinePage(3)); |
| 556 } | 568 } |
| 557 | 569 |
| 558 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { | 570 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { |
| 559 IgnoreOnCategoryStatusChangedToAvailable(); | 571 IgnoreOnCategoryStatusChangedToAvailable(); |
| 560 | 572 |
| 561 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 573 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 562 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 574 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 563 EXPECT_CALL( | 575 EXPECT_CALL( |
| 564 *observer(), | 576 *observer(), |
| 565 OnNewSuggestions(_, downloads_category(), | 577 OnNewSuggestions(_, downloads_category(), |
| 566 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 578 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 567 HasUrl("http://dummy.com/2"), | 579 HasUrl("http://dummy.com/2"), |
| 568 HasUrl("http://download.com/1"), | 580 HasUrl("http://download.com/1"), |
| 569 HasUrl("http://download.com/2")))); | 581 HasUrl("http://download.com/2")))); |
| 570 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 582 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 583 base::MakeUnique<base::DefaultClock>()); |
| 571 | 584 |
| 572 provider()->DismissSuggestion( | 585 provider()->DismissSuggestion( |
| 573 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 586 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 574 provider()->DismissSuggestion( | 587 provider()->DismissSuggestion( |
| 575 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 588 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 576 | 589 |
| 577 EXPECT_THAT(GetDismissedSuggestions(), | 590 EXPECT_THAT(GetDismissedSuggestions(), |
| 578 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 591 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 579 HasUrl("http://download.com/1"))); | 592 HasUrl("http://download.com/1"))); |
| 580 } | 593 } |
| 581 | 594 |
| 582 TEST_F(DownloadSuggestionsProviderTest, ShouldClearDismissedSuggestions) { | 595 TEST_F(DownloadSuggestionsProviderTest, ShouldClearDismissedSuggestions) { |
| 583 IgnoreOnCategoryStatusChangedToAvailable(); | 596 IgnoreOnCategoryStatusChangedToAvailable(); |
| 584 | 597 |
| 585 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 598 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 586 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 599 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 587 EXPECT_CALL( | 600 EXPECT_CALL( |
| 588 *observer(), | 601 *observer(), |
| 589 OnNewSuggestions(_, downloads_category(), | 602 OnNewSuggestions(_, downloads_category(), |
| 590 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 603 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 591 HasUrl("http://dummy.com/2"), | 604 HasUrl("http://dummy.com/2"), |
| 592 HasUrl("http://download.com/1"), | 605 HasUrl("http://download.com/1"), |
| 593 HasUrl("http://download.com/2")))); | 606 HasUrl("http://download.com/2")))); |
| 594 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 607 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 608 base::MakeUnique<base::DefaultClock>()); |
| 595 | 609 |
| 596 provider()->DismissSuggestion( | 610 provider()->DismissSuggestion( |
| 597 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 611 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 598 provider()->DismissSuggestion( | 612 provider()->DismissSuggestion( |
| 599 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 613 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 600 | 614 |
| 601 EXPECT_CALL( | 615 EXPECT_CALL( |
| 602 *observer(), | 616 *observer(), |
| 603 OnNewSuggestions(_, downloads_category(), | 617 OnNewSuggestions(_, downloads_category(), |
| 604 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 618 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 615 | 629 |
| 616 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 630 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 617 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 631 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 618 EXPECT_CALL( | 632 EXPECT_CALL( |
| 619 *observer(), | 633 *observer(), |
| 620 OnNewSuggestions(_, downloads_category(), | 634 OnNewSuggestions(_, downloads_category(), |
| 621 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 635 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 622 HasUrl("http://dummy.com/2"), | 636 HasUrl("http://dummy.com/2"), |
| 623 HasUrl("http://download.com/1"), | 637 HasUrl("http://download.com/1"), |
| 624 HasUrl("http://download.com/2")))); | 638 HasUrl("http://download.com/2")))); |
| 625 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 639 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 640 base::MakeUnique<base::DefaultClock>()); |
| 626 | 641 |
| 627 provider()->DismissSuggestion( | 642 provider()->DismissSuggestion( |
| 628 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 643 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 629 | 644 |
| 630 EXPECT_CALL( | 645 EXPECT_CALL( |
| 631 *observer(), | 646 *observer(), |
| 632 OnNewSuggestions(_, downloads_category(), | 647 OnNewSuggestions(_, downloads_category(), |
| 633 UnorderedElementsAre(HasUrl("http://dummy.com/2"), | 648 UnorderedElementsAre(HasUrl("http://dummy.com/2"), |
| 634 HasUrl("http://dummy.com/3"), | 649 HasUrl("http://dummy.com/3"), |
| 635 HasUrl("http://download.com/1"), | 650 HasUrl("http://download.com/1"), |
| 636 HasUrl("http://download.com/2")))); | 651 HasUrl("http://download.com/2")))); |
| 637 AddOfflinePage(CreateDummyOfflinePage(3)); | 652 AddOfflinePage(CreateDummyOfflinePage(3)); |
| 638 } | 653 } |
| 639 | 654 |
| 640 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { | 655 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { |
| 641 IgnoreOnCategoryStatusChangedToAvailable(); | 656 IgnoreOnCategoryStatusChangedToAvailable(); |
| 642 | 657 |
| 643 // Currently the provider stores five items in its internal cache, so six | 658 // Currently the provider stores five items in its internal cache, so six |
| 644 // items are needed to check whether all downloads are fetched on dismissal. | 659 // items are needed to check whether all downloads are fetched on dismissal. |
| 645 *(downloads_manager()->mutable_items()) = | 660 *(downloads_manager()->mutable_items()) = |
| 646 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6}); | 661 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6}); |
| 647 EXPECT_CALL( | 662 EXPECT_CALL( |
| 648 *observer(), | 663 *observer(), |
| 649 OnNewSuggestions(_, downloads_category(), | 664 OnNewSuggestions(_, downloads_category(), |
| 650 UnorderedElementsAre(HasUrl("http://download.com/1"), | 665 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 651 HasUrl("http://download.com/2"), | 666 HasUrl("http://download.com/2"), |
| 652 HasUrl("http://download.com/3"), | 667 HasUrl("http://download.com/3"), |
| 653 HasUrl("http://download.com/4"), | 668 HasUrl("http://download.com/4"), |
| 654 HasUrl("http://download.com/5")))); | 669 HasUrl("http://download.com/5")))); |
| 655 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 670 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 671 base::MakeUnique<base::DefaultClock>()); |
| 656 | 672 |
| 657 provider()->DismissSuggestion( | 673 provider()->DismissSuggestion( |
| 658 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 674 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 659 provider()->DismissSuggestion( | 675 provider()->DismissSuggestion( |
| 660 GetDummySuggestionId(2, /*is_offline_page=*/false)); | 676 GetDummySuggestionId(2, /*is_offline_page=*/false)); |
| 661 | 677 |
| 662 // The provider is not notified about the 6th item, however, it must report | 678 // The provider is not notified about the 6th item, however, it must report |
| 663 // it now. | 679 // it now. |
| 664 EXPECT_CALL( | 680 EXPECT_CALL( |
| 665 *observer(), | 681 *observer(), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 677 IgnoreOnCategoryStatusChangedToAvailable(); | 693 IgnoreOnCategoryStatusChangedToAvailable(); |
| 678 | 694 |
| 679 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 695 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 680 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 696 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 681 EXPECT_CALL( | 697 EXPECT_CALL( |
| 682 *observer(), | 698 *observer(), |
| 683 OnNewSuggestions(_, downloads_category(), | 699 OnNewSuggestions(_, downloads_category(), |
| 684 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 700 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 685 HasUrl("http://dummy.com/2"), | 701 HasUrl("http://dummy.com/2"), |
| 686 HasUrl("http://download.com/1")))); | 702 HasUrl("http://download.com/1")))); |
| 687 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 703 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 704 base::MakeUnique<base::DefaultClock>()); |
| 688 | 705 |
| 689 // We add another item manually, so that when it gets deleted it is not | 706 // We add another item manually, so that when it gets deleted it is not |
| 690 // present in DownloadsManager list. | 707 // present in DownloadsManager list. |
| 691 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload(2); | 708 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload(2); |
| 692 EXPECT_CALL( | 709 EXPECT_CALL( |
| 693 *observer(), | 710 *observer(), |
| 694 OnNewSuggestions(_, downloads_category(), | 711 OnNewSuggestions(_, downloads_category(), |
| 695 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 712 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 696 HasUrl("http://dummy.com/2"), | 713 HasUrl("http://dummy.com/2"), |
| 697 HasUrl("http://download.com/1"), | 714 HasUrl("http://download.com/1"), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 717 *(downloads_manager()->mutable_items()) = | 734 *(downloads_manager()->mutable_items()) = |
| 718 CreateDummyAssetDownloads({1, 2, 3, 4, 5}); | 735 CreateDummyAssetDownloads({1, 2, 3, 4, 5}); |
| 719 EXPECT_CALL( | 736 EXPECT_CALL( |
| 720 *observer(), | 737 *observer(), |
| 721 OnNewSuggestions(_, downloads_category(), | 738 OnNewSuggestions(_, downloads_category(), |
| 722 UnorderedElementsAre(HasUrl("http://download.com/1"), | 739 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 723 HasUrl("http://download.com/2"), | 740 HasUrl("http://download.com/2"), |
| 724 HasUrl("http://download.com/3"), | 741 HasUrl("http://download.com/3"), |
| 725 HasUrl("http://download.com/4"), | 742 HasUrl("http://download.com/4"), |
| 726 HasUrl("http://download.com/5")))); | 743 HasUrl("http://download.com/5")))); |
| 727 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 744 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 745 base::MakeUnique<base::DefaultClock>()); |
| 728 | 746 |
| 729 // Note that |CreateDummyAssetDownloads| creates items "downloaded" before | 747 // Note that |CreateDummyAssetDownloads| creates items "downloaded" before |
| 730 // |base::Time::Now()|, so for a new item the time is set in future to enforce | 748 // |base::Time::Now()|, so for a new item the time is set in future to enforce |
| 731 // the provider to show the new item. | 749 // the provider to show the new item. |
| 732 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload( | 750 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload( |
| 733 100, base::Time::Now() + base::TimeDelta::FromDays(1)); | 751 100, base::Time::Now() + base::TimeDelta::FromDays(1)); |
| 734 EXPECT_CALL( | 752 EXPECT_CALL( |
| 735 *observer(), | 753 *observer(), |
| 736 OnNewSuggestions( | 754 OnNewSuggestions( |
| 737 _, downloads_category(), | 755 _, downloads_category(), |
| (...skipping 23 matching lines...) Expand all Loading... |
| 761 IgnoreOnSuggestionInvalidated(); | 779 IgnoreOnSuggestionInvalidated(); |
| 762 | 780 |
| 763 auto offline_pages = CreateDummyOfflinePages({1, 2, 3}); | 781 auto offline_pages = CreateDummyOfflinePages({1, 2, 3}); |
| 764 | 782 |
| 765 *(offline_pages_model()->mutable_items()) = offline_pages; | 783 *(offline_pages_model()->mutable_items()) = offline_pages; |
| 766 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 784 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 767 UnorderedElementsAre( | 785 UnorderedElementsAre( |
| 768 HasUrl("http://dummy.com/1"), | 786 HasUrl("http://dummy.com/1"), |
| 769 HasUrl("http://dummy.com/2"), | 787 HasUrl("http://dummy.com/2"), |
| 770 HasUrl("http://dummy.com/3")))); | 788 HasUrl("http://dummy.com/3")))); |
| 771 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 789 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 790 base::MakeUnique<base::DefaultClock>()); |
| 772 | 791 |
| 773 provider()->DismissSuggestion( | 792 provider()->DismissSuggestion( |
| 774 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 793 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 775 provider()->DismissSuggestion( | 794 provider()->DismissSuggestion( |
| 776 GetDummySuggestionId(2, /*is_offline_page=*/true)); | 795 GetDummySuggestionId(2, /*is_offline_page=*/true)); |
| 777 provider()->DismissSuggestion( | 796 provider()->DismissSuggestion( |
| 778 GetDummySuggestionId(3, /*is_offline_page=*/true)); | 797 GetDummySuggestionId(3, /*is_offline_page=*/true)); |
| 779 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); | 798 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); |
| 780 | 799 |
| 781 // Note that the first suggestion is not removed from |offline_pages_model| | 800 // Note that the first suggestion is not removed from |offline_pages_model| |
| 782 // storage, because otherwise |GetDismissedSuggestions| cannot return it. | 801 // storage, because otherwise |GetDismissedSuggestions| cannot return it. |
| 783 FireOfflinePageDeleted(offline_pages[0]); | 802 FireOfflinePageDeleted(offline_pages[0]); |
| 784 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); | 803 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); |
| 785 | 804 |
| 786 // Prune when offline page is deleted. | 805 // Prune when offline page is deleted. |
| 787 FireOfflinePageDeleted(offline_pages_model()->items()[1]); | 806 FireOfflinePageDeleted(offline_pages_model()->items()[1]); |
| 788 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); | 807 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); |
| 789 } | 808 } |
| 790 | 809 |
| 791 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { | 810 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { |
| 792 IgnoreOnCategoryStatusChangedToAvailable(); | 811 IgnoreOnCategoryStatusChangedToAvailable(); |
| 793 IgnoreOnSuggestionInvalidated(); | 812 IgnoreOnSuggestionInvalidated(); |
| 794 | 813 |
| 795 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 814 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 796 EXPECT_CALL( | 815 EXPECT_CALL( |
| 797 *observer(), | 816 *observer(), |
| 798 OnNewSuggestions(_, downloads_category(), | 817 OnNewSuggestions(_, downloads_category(), |
| 799 UnorderedElementsAre(HasUrl("http://download.com/1"), | 818 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 800 HasUrl("http://download.com/2")))); | 819 HasUrl("http://download.com/2")))); |
| 801 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 820 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 821 base::MakeUnique<base::DefaultClock>()); |
| 802 | 822 |
| 803 provider()->DismissSuggestion( | 823 provider()->DismissSuggestion( |
| 804 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 824 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 805 provider()->DismissSuggestion( | 825 provider()->DismissSuggestion( |
| 806 GetDummySuggestionId(2, /*is_offline_page=*/false)); | 826 GetDummySuggestionId(2, /*is_offline_page=*/false)); |
| 807 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); | 827 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); |
| 808 | 828 |
| 809 downloads_manager()->items()[0]->NotifyDownloadDestroyed(); | 829 downloads_manager()->items()[0]->NotifyDownloadDestroyed(); |
| 810 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); | 830 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); |
| 811 } | 831 } |
| 812 | 832 |
| 813 TEST_F(DownloadSuggestionsProviderTest, ShouldFetchAssetDownloadsOnStartup) { | 833 TEST_F(DownloadSuggestionsProviderTest, ShouldFetchAssetDownloadsOnStartup) { |
| 814 IgnoreOnCategoryStatusChangedToAvailable(); | 834 IgnoreOnCategoryStatusChangedToAvailable(); |
| 815 | 835 |
| 816 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 836 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 817 EXPECT_CALL( | 837 EXPECT_CALL( |
| 818 *observer(), | 838 *observer(), |
| 819 OnNewSuggestions(_, downloads_category(), | 839 OnNewSuggestions(_, downloads_category(), |
| 820 UnorderedElementsAre(HasUrl("http://download.com/1"), | 840 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 821 HasUrl("http://download.com/2")))); | 841 HasUrl("http://download.com/2")))); |
| 822 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 842 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 843 base::MakeUnique<base::DefaultClock>()); |
| 823 } | 844 } |
| 824 | 845 |
| 825 TEST_F(DownloadSuggestionsProviderTest, | 846 TEST_F(DownloadSuggestionsProviderTest, |
| 826 ShouldFetchOfflinePageDownloadsOnStartup) { | 847 ShouldFetchOfflinePageDownloadsOnStartup) { |
| 827 IgnoreOnCategoryStatusChangedToAvailable(); | 848 IgnoreOnCategoryStatusChangedToAvailable(); |
| 828 | 849 |
| 829 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 850 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 830 offline_pages_model()->set_is_loaded(true); | 851 offline_pages_model()->set_is_loaded(true); |
| 831 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 852 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 832 UnorderedElementsAre( | 853 UnorderedElementsAre( |
| 833 HasUrl("http://dummy.com/1"), | 854 HasUrl("http://dummy.com/1"), |
| 834 HasUrl("http://dummy.com/2")))); | 855 HasUrl("http://dummy.com/2")))); |
| 835 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); | 856 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, |
| 857 base::MakeUnique<base::DefaultClock>()); |
| 836 FireOfflinePageModelLoaded(); | 858 FireOfflinePageModelLoaded(); |
| 837 } | 859 } |
| 838 | 860 |
| 839 TEST_F(DownloadSuggestionsProviderTest, | 861 TEST_F(DownloadSuggestionsProviderTest, |
| 840 ShouldFetchAssetDownloadsOnHistoryQueryComplete) { | 862 ShouldFetchAssetDownloadsOnHistoryQueryComplete) { |
| 841 IgnoreOnCategoryStatusChangedToAvailable(); | 863 IgnoreOnCategoryStatusChangedToAvailable(); |
| 842 | 864 |
| 843 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); | 865 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); |
| 844 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 866 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 867 base::MakeUnique<base::DefaultClock>()); |
| 845 | 868 |
| 846 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 869 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 847 EXPECT_CALL( | 870 EXPECT_CALL( |
| 848 *observer(), | 871 *observer(), |
| 849 OnNewSuggestions(_, downloads_category(), | 872 OnNewSuggestions(_, downloads_category(), |
| 850 UnorderedElementsAre(HasUrl("http://download.com/1"), | 873 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 851 HasUrl("http://download.com/2")))); | 874 HasUrl("http://download.com/2")))); |
| 852 FireHistoryQueryComplete(); | 875 FireHistoryQueryComplete(); |
| 853 } | 876 } |
| 854 | 877 |
| 855 TEST_F(DownloadSuggestionsProviderTest, | 878 TEST_F(DownloadSuggestionsProviderTest, |
| 856 ShouldInvalidateAssetDownloadWhenItsFileRemoved) { | 879 ShouldInvalidateAssetDownloadWhenItsFileRemoved) { |
| 857 IgnoreOnCategoryStatusChangedToAvailable(); | 880 IgnoreOnCategoryStatusChangedToAvailable(); |
| 858 | 881 |
| 859 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 882 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 860 EXPECT_CALL( | 883 EXPECT_CALL( |
| 861 *observer(), | 884 *observer(), |
| 862 OnNewSuggestions(_, downloads_category(), | 885 OnNewSuggestions(_, downloads_category(), |
| 863 UnorderedElementsAre(HasUrl("http://download.com/1")))); | 886 UnorderedElementsAre(HasUrl("http://download.com/1")))); |
| 864 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 887 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 888 base::MakeUnique<base::DefaultClock>()); |
| 865 | 889 |
| 866 EXPECT_CALL(*observer(), | 890 EXPECT_CALL(*observer(), |
| 867 OnSuggestionInvalidated( | 891 OnSuggestionInvalidated( |
| 868 _, GetDummySuggestionId(1, /*is_offline_page=*/false))); | 892 _, GetDummySuggestionId(1, /*is_offline_page=*/false))); |
| 869 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true); | 893 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true); |
| 870 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); | 894 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); |
| 871 } | 895 } |
| 872 | 896 |
| 873 TEST_F(DownloadSuggestionsProviderTest, | 897 TEST_F(DownloadSuggestionsProviderTest, |
| 874 ShouldNotShowOfflinePagesWhenTurnedOff) { | 898 ShouldNotShowOfflinePagesWhenTurnedOff) { |
| 875 IgnoreOnCategoryStatusChangedToAvailable(); | 899 IgnoreOnCategoryStatusChangedToAvailable(); |
| 876 IgnoreOnSuggestionInvalidated(); | 900 IgnoreOnSuggestionInvalidated(); |
| 877 | 901 |
| 878 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 902 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 879 EXPECT_CALL(*observer(), | 903 EXPECT_CALL(*observer(), |
| 880 OnNewSuggestions(_, downloads_category(), IsEmpty())); | 904 OnNewSuggestions(_, downloads_category(), IsEmpty())); |
| 881 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 905 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, |
| 906 base::MakeUnique<base::DefaultClock>()); |
| 882 | 907 |
| 883 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = | 908 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = |
| 884 CreateDummyAssetDownloads({1}); | 909 CreateDummyAssetDownloads({1}); |
| 885 EXPECT_CALL( | 910 EXPECT_CALL( |
| 886 *observer(), | 911 *observer(), |
| 887 OnNewSuggestions(_, downloads_category(), | 912 OnNewSuggestions(_, downloads_category(), |
| 888 UnorderedElementsAre(HasUrl("http://download.com/1")))); | 913 UnorderedElementsAre(HasUrl("http://download.com/1")))); |
| 889 FireDownloadCreated(asset_downloads[0].get()); | 914 FireDownloadCreated(asset_downloads[0].get()); |
| 890 // TODO(vitaliii): Notify the provider that an offline page has been updated. | 915 // TODO(vitaliii): Notify the provider that an offline page has been updated. |
| 891 } | 916 } |
| 892 | 917 |
| 893 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowAssetsWhenTurnedOff) { | 918 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowAssetsWhenTurnedOff) { |
| 894 IgnoreOnCategoryStatusChangedToAvailable(); | 919 IgnoreOnCategoryStatusChangedToAvailable(); |
| 895 IgnoreOnSuggestionInvalidated(); | 920 IgnoreOnSuggestionInvalidated(); |
| 896 | 921 |
| 897 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 922 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 898 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 923 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 899 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 924 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 900 UnorderedElementsAre( | 925 UnorderedElementsAre( |
| 901 HasUrl("http://dummy.com/1"), | 926 HasUrl("http://dummy.com/1"), |
| 902 HasUrl("http://dummy.com/2")))); | 927 HasUrl("http://dummy.com/2")))); |
| 903 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); | 928 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, |
| 929 base::MakeUnique<base::DefaultClock>()); |
| 904 downloads_manager()->NotifyDownloadCreated( | 930 downloads_manager()->NotifyDownloadCreated( |
| 905 downloads_manager()->items()[0].get()); | 931 downloads_manager()->items()[0].get()); |
| 906 // This notification should not reach the provider, because the asset | 932 // This notification should not reach the provider, because the asset |
| 907 // downloads data source is not provided. If it is and the provider reacts to | 933 // downloads data source is not provided. If it is and the provider reacts to |
| 908 // the notification, the test will fail because the observer is a strict mock. | 934 // the notification, the test will fail because the observer is a strict mock. |
| 909 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); | 935 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); |
| 910 } | 936 } |
| 911 | 937 |
| 912 TEST_F(DownloadSuggestionsProviderTest, | 938 TEST_F(DownloadSuggestionsProviderTest, |
| 913 ShouldLoadAndSubmitMissedAssetsEvenIfOfflinePagesAreTurnedOff) { | 939 ShouldLoadAndSubmitMissedAssetsEvenIfOfflinePagesAreTurnedOff) { |
| 914 IgnoreOnCategoryStatusChangedToAvailable(); | 940 IgnoreOnCategoryStatusChangedToAvailable(); |
| 915 IgnoreOnSuggestionInvalidated(); | 941 IgnoreOnSuggestionInvalidated(); |
| 916 | 942 |
| 917 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 943 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 918 EXPECT_CALL( | 944 EXPECT_CALL( |
| 919 *observer(), | 945 *observer(), |
| 920 OnNewSuggestions(_, downloads_category(), | 946 OnNewSuggestions(_, downloads_category(), |
| 921 UnorderedElementsAre(HasUrl("http://download.com/1"), | 947 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 922 HasUrl("http://download.com/2")))); | 948 HasUrl("http://download.com/2")))); |
| 923 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 949 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, |
| 950 base::MakeUnique<base::DefaultClock>()); |
| 924 } | 951 } |
| 925 | 952 |
| 926 TEST_F(DownloadSuggestionsProviderTest, | 953 TEST_F(DownloadSuggestionsProviderTest, |
| 927 ShouldLoadAndSubmitOfflinePagesEvenIfAssetDownloadsAreTurnedOff) { | 954 ShouldLoadAndSubmitOfflinePagesEvenIfAssetDownloadsAreTurnedOff) { |
| 928 IgnoreOnCategoryStatusChangedToAvailable(); | 955 IgnoreOnCategoryStatusChangedToAvailable(); |
| 929 IgnoreOnSuggestionInvalidated(); | 956 IgnoreOnSuggestionInvalidated(); |
| 930 | 957 |
| 931 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 958 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 932 offline_pages_model()->set_is_loaded(true); | 959 offline_pages_model()->set_is_loaded(true); |
| 933 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 960 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 934 UnorderedElementsAre( | 961 UnorderedElementsAre( |
| 935 HasUrl("http://dummy.com/1"), | 962 HasUrl("http://dummy.com/1"), |
| 936 HasUrl("http://dummy.com/2")))); | 963 HasUrl("http://dummy.com/2")))); |
| 937 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); | 964 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true, |
| 965 base::MakeUnique<base::DefaultClock>()); |
| 938 } | 966 } |
| 939 | 967 |
| 940 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) { | 968 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) { |
| 941 IgnoreOnCategoryStatusChangedToAvailable(); | 969 IgnoreOnCategoryStatusChangedToAvailable(); |
| 942 IgnoreOnSuggestionInvalidated(); | 970 IgnoreOnSuggestionInvalidated(); |
| 943 | 971 |
| 944 // Dismiss items to store them in the list of dismissed items. | 972 // Dismiss items to store them in the list of dismissed items. |
| 945 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1}); | 973 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1}); |
| 946 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 974 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 947 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); | 975 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); |
| 948 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 976 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 977 base::MakeUnique<base::DefaultClock>()); |
| 949 provider()->DismissSuggestion( | 978 provider()->DismissSuggestion( |
| 950 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 979 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 951 provider()->DismissSuggestion( | 980 provider()->DismissSuggestion( |
| 952 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 981 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 953 // Destroy and create provider to simulate turning off Chrome. | 982 // Destroy and create provider to simulate turning off Chrome. |
| 954 DestroyProvider(); | 983 DestroyProvider(); |
| 955 | 984 |
| 956 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); | 985 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); |
| 957 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 986 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 987 base::MakeUnique<base::DefaultClock>()); |
| 958 EXPECT_THAT(GetDismissedSuggestions(), | 988 EXPECT_THAT(GetDismissedSuggestions(), |
| 959 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 989 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 960 HasUrl("http://download.com/1"))); | 990 HasUrl("http://download.com/1"))); |
| 961 } | 991 } |
| 962 | 992 |
| 963 TEST_F(DownloadSuggestionsProviderTest, | 993 TEST_F(DownloadSuggestionsProviderTest, |
| 964 ShouldNotPruneDismissedAssetDownloadsBeforeHistoryQueryComplete) { | 994 ShouldNotPruneDismissedAssetDownloadsBeforeHistoryQueryComplete) { |
| 965 IgnoreOnCategoryStatusChangedToAvailable(); | 995 IgnoreOnCategoryStatusChangedToAvailable(); |
| 966 IgnoreOnSuggestionInvalidated(); | 996 IgnoreOnSuggestionInvalidated(); |
| 967 | 997 |
| 968 // Dismiss items to store them in the list of dismissed items. | 998 // Dismiss items to store them in the list of dismissed items. |
| 969 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 999 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 970 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); | 1000 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); |
| 971 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 1001 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false, |
| 1002 base::MakeUnique<base::DefaultClock>()); |
| 972 provider()->DismissSuggestion( | 1003 provider()->DismissSuggestion( |
| 973 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 1004 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 974 ASSERT_THAT(GetDismissedSuggestions(), | 1005 ASSERT_THAT(GetDismissedSuggestions(), |
| 975 UnorderedElementsAre(HasUrl("http://download.com/1"))); | 1006 UnorderedElementsAre(HasUrl("http://download.com/1"))); |
| 976 // Destroy and create provider to simulate turning off Chrome. | 1007 // Destroy and create provider to simulate turning off Chrome. |
| 977 DestroyProvider(); | 1008 DestroyProvider(); |
| 978 | 1009 |
| 979 downloads_manager()->mutable_items()->clear(); | 1010 downloads_manager()->mutable_items()->clear(); |
| 980 | 1011 |
| 981 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); | 1012 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); |
| 982 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 1013 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false, |
| 1014 base::MakeUnique<base::DefaultClock>()); |
| 983 | 1015 |
| 984 // Dismissed IDs should not be pruned yet, because the downloads list at the | 1016 // Dismissed IDs should not be pruned yet, because the downloads list at the |
| 985 // manager is not complete. | 1017 // manager is not complete. |
| 986 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 1018 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 987 EXPECT_THAT(GetDismissedSuggestions(), | 1019 EXPECT_THAT(GetDismissedSuggestions(), |
| 988 UnorderedElementsAre(HasUrl("http://download.com/1"))); | 1020 UnorderedElementsAre(HasUrl("http://download.com/1"))); |
| 989 | 1021 |
| 990 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)); | 1022 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)); |
| 991 | 1023 |
| 992 downloads_manager()->mutable_items()->clear(); | 1024 downloads_manager()->mutable_items()->clear(); |
| 993 FireHistoryQueryComplete(); | 1025 FireHistoryQueryComplete(); |
| 994 | 1026 |
| 995 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 1027 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 996 // Once the manager has been loaded, the ids should be pruned. | 1028 // Once the manager has been loaded, the ids should be pruned. |
| 997 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); | 1029 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); |
| 998 } | 1030 } |
| 1031 |
| 1032 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOutdatedDownloads) { |
| 1033 IgnoreOnCategoryStatusChangedToAvailable(); |
| 1034 IgnoreOnSuggestionInvalidated(); |
| 1035 |
| 1036 const int kDefaultMaxDownloadAgeHours = 6 * 7 * 24; |
| 1037 |
| 1038 base::Time now; |
| 1039 ASSERT_TRUE(base::Time::FromString("Tue, 31 Jan 2017 13:00:00", &now)); |
| 1040 const base::Time not_outdated = |
| 1041 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) + |
| 1042 base::TimeDelta::FromSeconds(1); |
| 1043 const base::Time outdated = |
| 1044 now - base::TimeDelta::FromHours(kDefaultMaxDownloadAgeHours) - |
| 1045 base::TimeDelta::FromSeconds(1); |
| 1046 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({0, 1}); |
| 1047 offline_pages_model()->mutable_items()->at(0).url = |
| 1048 GURL("http://dummy.com/0"); |
| 1049 offline_pages_model()->mutable_items()->at(0).creation_time = not_outdated; |
| 1050 offline_pages_model()->mutable_items()->at(1).url = |
| 1051 GURL("http://dummy.com/1"); |
| 1052 offline_pages_model()->mutable_items()->at(1).creation_time = outdated; |
| 1053 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({0, 1}); |
| 1054 downloads_manager()->mutable_items()->at(0)->SetURL( |
| 1055 GURL("http://download.com/0")); |
| 1056 downloads_manager()->mutable_items()->at(0)->SetStartTime(not_outdated); |
| 1057 downloads_manager()->mutable_items()->at(1)->SetURL( |
| 1058 GURL("http://download.com/1")); |
| 1059 downloads_manager()->mutable_items()->at(1)->SetStartTime(outdated); |
| 1060 |
| 1061 EXPECT_CALL( |
| 1062 *observer(), |
| 1063 OnNewSuggestions(_, downloads_category(), |
| 1064 UnorderedElementsAre(HasUrl("http://dummy.com/0"), |
| 1065 HasUrl("http://download.com/0")))); |
| 1066 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 1067 test_clock->SetNow(now); |
| 1068 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true, |
| 1069 std::move(test_clock)); |
| 1070 } |
| OLD | NEW |