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

Side by Side Diff: chrome/browser/ntp_snippets/download_suggestions_provider_unittest.cc

Issue 2673633002: [NTP::Downloads] Do not show old downloads. (Closed)
Patch Set: Created 3 years, 10 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/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
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
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
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
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
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
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
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
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
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, 833 TEST_F(DownloadSuggestionsProviderTest,
814 ShouldFetchAssetDownloadsOnStartupButOnlyOnce) { 834 ShouldFetchAssetDownloadsOnStartupButOnlyOnce) {
815 IgnoreOnCategoryStatusChangedToAvailable(); 835 IgnoreOnCategoryStatusChangedToAvailable();
816 836
817 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); 837 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
818 EXPECT_CALL( 838 EXPECT_CALL(
819 *observer(), 839 *observer(),
820 OnNewSuggestions(_, downloads_category(), 840 OnNewSuggestions(_, downloads_category(),
821 UnorderedElementsAre(HasUrl("http://download.com/1"), 841 UnorderedElementsAre(HasUrl("http://download.com/1"),
822 HasUrl("http://download.com/2")))); 842 HasUrl("http://download.com/2"))));
823 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 843 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
844 base::MakeUnique<base::DefaultClock>());
824 } 845 }
825 846
826 TEST_F(DownloadSuggestionsProviderTest, 847 TEST_F(DownloadSuggestionsProviderTest,
827 ShouldFetchAssetDownloadsOnHistoryQueryComplete) { 848 ShouldFetchAssetDownloadsOnHistoryQueryComplete) {
828 IgnoreOnCategoryStatusChangedToAvailable(); 849 IgnoreOnCategoryStatusChangedToAvailable();
829 850
830 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); 851 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0);
831 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 852 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
853 base::MakeUnique<base::DefaultClock>());
832 854
833 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); 855 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
834 EXPECT_CALL( 856 EXPECT_CALL(
835 *observer(), 857 *observer(),
836 OnNewSuggestions(_, downloads_category(), 858 OnNewSuggestions(_, downloads_category(),
837 UnorderedElementsAre(HasUrl("http://download.com/1"), 859 UnorderedElementsAre(HasUrl("http://download.com/1"),
838 HasUrl("http://download.com/2")))); 860 HasUrl("http://download.com/2"))));
839 FireHistoryQueryComplete(); 861 FireHistoryQueryComplete();
840 } 862 }
841 863
842 TEST_F(DownloadSuggestionsProviderTest, 864 TEST_F(DownloadSuggestionsProviderTest,
843 ShouldInvalidateAssetDownloadWhenItsFileRemoved) { 865 ShouldInvalidateAssetDownloadWhenItsFileRemoved) {
844 IgnoreOnCategoryStatusChangedToAvailable(); 866 IgnoreOnCategoryStatusChangedToAvailable();
845 867
846 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); 868 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
847 EXPECT_CALL( 869 EXPECT_CALL(
848 *observer(), 870 *observer(),
849 OnNewSuggestions(_, downloads_category(), 871 OnNewSuggestions(_, downloads_category(),
850 UnorderedElementsAre(HasUrl("http://download.com/1")))); 872 UnorderedElementsAre(HasUrl("http://download.com/1"))));
851 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 873 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
874 base::MakeUnique<base::DefaultClock>());
852 875
853 EXPECT_CALL(*observer(), 876 EXPECT_CALL(*observer(),
854 OnSuggestionInvalidated( 877 OnSuggestionInvalidated(
855 _, GetDummySuggestionId(1, /*is_offline_page=*/false))); 878 _, GetDummySuggestionId(1, /*is_offline_page=*/false)));
856 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true); 879 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true);
857 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); 880 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated();
858 } 881 }
859 882
860 TEST_F(DownloadSuggestionsProviderTest, 883 TEST_F(DownloadSuggestionsProviderTest,
861 ShouldNotShowOfflinePagesWhenTurnedOff) { 884 ShouldNotShowOfflinePagesWhenTurnedOff) {
862 IgnoreOnCategoryStatusChangedToAvailable(); 885 IgnoreOnCategoryStatusChangedToAvailable();
863 IgnoreOnSuggestionInvalidated(); 886 IgnoreOnSuggestionInvalidated();
864 887
865 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 888 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
866 EXPECT_CALL(*observer(), 889 EXPECT_CALL(*observer(),
867 OnNewSuggestions(_, downloads_category(), IsEmpty())); 890 OnNewSuggestions(_, downloads_category(), IsEmpty()));
868 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); 891 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
892 base::MakeUnique<base::DefaultClock>());
869 893
870 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = 894 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads =
871 CreateDummyAssetDownloads({1}); 895 CreateDummyAssetDownloads({1});
872 EXPECT_CALL( 896 EXPECT_CALL(
873 *observer(), 897 *observer(),
874 OnNewSuggestions(_, downloads_category(), 898 OnNewSuggestions(_, downloads_category(),
875 UnorderedElementsAre(HasUrl("http://download.com/1")))); 899 UnorderedElementsAre(HasUrl("http://download.com/1"))));
876 FireDownloadCreated(asset_downloads[0].get()); 900 FireDownloadCreated(asset_downloads[0].get());
877 // TODO(vitaliii): Notify the provider that an offline page has been updated. 901 // TODO(vitaliii): Notify the provider that an offline page has been updated.
878 } 902 }
879 903
880 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowAssetsWhenTurnedOff) { 904 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowAssetsWhenTurnedOff) {
881 IgnoreOnCategoryStatusChangedToAvailable(); 905 IgnoreOnCategoryStatusChangedToAvailable();
882 IgnoreOnSuggestionInvalidated(); 906 IgnoreOnSuggestionInvalidated();
883 907
884 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 908 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
885 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); 909 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
886 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), 910 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(),
887 UnorderedElementsAre( 911 UnorderedElementsAre(
888 HasUrl("http://dummy.com/1"), 912 HasUrl("http://dummy.com/1"),
889 HasUrl("http://dummy.com/2")))); 913 HasUrl("http://dummy.com/2"))));
890 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); 914 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true,
915 base::MakeUnique<base::DefaultClock>());
891 downloads_manager()->NotifyDownloadCreated( 916 downloads_manager()->NotifyDownloadCreated(
892 downloads_manager()->items()[0].get()); 917 downloads_manager()->items()[0].get());
893 // This notification should not reach the provider, because the asset 918 // This notification should not reach the provider, because the asset
894 // downloads data source is not provided. If it is and the provider reacts to 919 // downloads data source is not provided. If it is and the provider reacts to
895 // the notification, the test will fail because the observer is a strict mock. 920 // the notification, the test will fail because the observer is a strict mock.
896 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); 921 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated();
897 } 922 }
898 923
899 TEST_F(DownloadSuggestionsProviderTest, ShouldLoadOfflinePagesOnModelLoaded) { 924 TEST_F(DownloadSuggestionsProviderTest, ShouldLoadOfflinePagesOnModelLoaded) {
900 IgnoreOnCategoryStatusChangedToAvailable(); 925 IgnoreOnCategoryStatusChangedToAvailable();
901 IgnoreOnSuggestionInvalidated(); 926 IgnoreOnSuggestionInvalidated();
902 927
903 offline_pages_model()->set_is_loaded(false); 928 offline_pages_model()->set_is_loaded(false);
904 EXPECT_CALL(*observer(), 929 EXPECT_CALL(*observer(),
905 OnNewSuggestions(_, downloads_category(), IsEmpty())); 930 OnNewSuggestions(_, downloads_category(), IsEmpty()));
906 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); 931 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true,
932 base::MakeUnique<base::DefaultClock>());
907 933
908 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 934 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
909 offline_pages_model()->set_is_loaded(true); 935 offline_pages_model()->set_is_loaded(true);
910 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), 936 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(),
911 UnorderedElementsAre( 937 UnorderedElementsAre(
912 HasUrl("http://dummy.com/1"), 938 HasUrl("http://dummy.com/1"),
913 HasUrl("http://dummy.com/2")))); 939 HasUrl("http://dummy.com/2"))));
914 FireOfflinePageModelLoaded(); 940 FireOfflinePageModelLoaded();
915 } 941 }
916 942
917 TEST_F(DownloadSuggestionsProviderTest, 943 TEST_F(DownloadSuggestionsProviderTest,
918 ShouldLoadOfflinePagesIfMissesOnModelLoaded) { 944 ShouldLoadOfflinePagesIfMissesOnModelLoaded) {
919 IgnoreOnCategoryStatusChangedToAvailable(); 945 IgnoreOnCategoryStatusChangedToAvailable();
920 IgnoreOnSuggestionInvalidated(); 946 IgnoreOnSuggestionInvalidated();
921 947
922 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 948 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
923 offline_pages_model()->set_is_loaded(true); 949 offline_pages_model()->set_is_loaded(true);
924 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), 950 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(),
925 UnorderedElementsAre( 951 UnorderedElementsAre(
926 HasUrl("http://dummy.com/1"), 952 HasUrl("http://dummy.com/1"),
927 HasUrl("http://dummy.com/2")))); 953 HasUrl("http://dummy.com/2"))));
928 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); 954 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true,
955 base::MakeUnique<base::DefaultClock>());
929 } 956 }
930 957
931 TEST_F(DownloadSuggestionsProviderTest, 958 TEST_F(DownloadSuggestionsProviderTest,
932 ShouldLoadAndSubmitMissedAssetsEvenIfOfflinePagesAreTurnedOff) { 959 ShouldLoadAndSubmitMissedAssetsEvenIfOfflinePagesAreTurnedOff) {
933 IgnoreOnCategoryStatusChangedToAvailable(); 960 IgnoreOnCategoryStatusChangedToAvailable();
934 IgnoreOnSuggestionInvalidated(); 961 IgnoreOnSuggestionInvalidated();
935 962
936 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); 963 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
937 EXPECT_CALL( 964 EXPECT_CALL(
938 *observer(), 965 *observer(),
939 OnNewSuggestions(_, downloads_category(), 966 OnNewSuggestions(_, downloads_category(),
940 UnorderedElementsAre(HasUrl("http://download.com/1"), 967 UnorderedElementsAre(HasUrl("http://download.com/1"),
941 HasUrl("http://download.com/2")))); 968 HasUrl("http://download.com/2"))));
942 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); 969 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
970 base::MakeUnique<base::DefaultClock>());
943 } 971 }
944 972
945 TEST_F(DownloadSuggestionsProviderTest, 973 TEST_F(DownloadSuggestionsProviderTest,
946 ShouldLoadAndSubmitOfflinePagesEvenIfAssetDownloadsAreTurnedOff) { 974 ShouldLoadAndSubmitOfflinePagesEvenIfAssetDownloadsAreTurnedOff) {
947 IgnoreOnCategoryStatusChangedToAvailable(); 975 IgnoreOnCategoryStatusChangedToAvailable();
948 IgnoreOnSuggestionInvalidated(); 976 IgnoreOnSuggestionInvalidated();
949 977
950 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 978 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
951 offline_pages_model()->set_is_loaded(true); 979 offline_pages_model()->set_is_loaded(true);
952 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), 980 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(),
953 UnorderedElementsAre( 981 UnorderedElementsAre(
954 HasUrl("http://dummy.com/1"), 982 HasUrl("http://dummy.com/1"),
955 HasUrl("http://dummy.com/2")))); 983 HasUrl("http://dummy.com/2"))));
956 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); 984 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true,
985 base::MakeUnique<base::DefaultClock>());
957 } 986 }
958 987
959 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) { 988 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) {
960 IgnoreOnCategoryStatusChangedToAvailable(); 989 IgnoreOnCategoryStatusChangedToAvailable();
961 IgnoreOnSuggestionInvalidated(); 990 IgnoreOnSuggestionInvalidated();
962 991
963 // Dismiss items to store them in the list of dismissed items. 992 // Dismiss items to store them in the list of dismissed items.
964 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1}); 993 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1});
965 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); 994 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
966 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); 995 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
967 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 996 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
997 base::MakeUnique<base::DefaultClock>());
968 provider()->DismissSuggestion( 998 provider()->DismissSuggestion(
969 GetDummySuggestionId(1, /*is_offline_page=*/true)); 999 GetDummySuggestionId(1, /*is_offline_page=*/true));
970 provider()->DismissSuggestion( 1000 provider()->DismissSuggestion(
971 GetDummySuggestionId(1, /*is_offline_page=*/false)); 1001 GetDummySuggestionId(1, /*is_offline_page=*/false));
972 // Destroy and create provider to simulate turning off Chrome. 1002 // Destroy and create provider to simulate turning off Chrome.
973 DestroyProvider(); 1003 DestroyProvider();
974 1004
975 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); 1005 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
976 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 1006 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
1007 base::MakeUnique<base::DefaultClock>());
977 EXPECT_THAT(GetDismissedSuggestions(), 1008 EXPECT_THAT(GetDismissedSuggestions(),
978 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 1009 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
979 HasUrl("http://download.com/1"))); 1010 HasUrl("http://download.com/1")));
980 } 1011 }
981 1012
982 TEST_F(DownloadSuggestionsProviderTest, 1013 TEST_F(DownloadSuggestionsProviderTest,
983 ShouldNotPruneDismissedAssetDownloadsBeforeHistoryQueryComplete) { 1014 ShouldNotPruneDismissedAssetDownloadsBeforeHistoryQueryComplete) {
984 IgnoreOnCategoryStatusChangedToAvailable(); 1015 IgnoreOnCategoryStatusChangedToAvailable();
985 IgnoreOnSuggestionInvalidated(); 1016 IgnoreOnSuggestionInvalidated();
986 1017
987 // Dismiss items to store them in the list of dismissed items. 1018 // Dismiss items to store them in the list of dismissed items.
988 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); 1019 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
989 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); 1020 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
990 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); 1021 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
1022 base::MakeUnique<base::DefaultClock>());
991 provider()->DismissSuggestion( 1023 provider()->DismissSuggestion(
992 GetDummySuggestionId(1, /*is_offline_page=*/false)); 1024 GetDummySuggestionId(1, /*is_offline_page=*/false));
993 ASSERT_THAT(GetDismissedSuggestions(), 1025 ASSERT_THAT(GetDismissedSuggestions(),
994 UnorderedElementsAre(HasUrl("http://download.com/1"))); 1026 UnorderedElementsAre(HasUrl("http://download.com/1")));
995 // Destroy and create provider to simulate turning off Chrome. 1027 // Destroy and create provider to simulate turning off Chrome.
996 DestroyProvider(); 1028 DestroyProvider();
997 1029
998 downloads_manager()->mutable_items()->clear(); 1030 downloads_manager()->mutable_items()->clear();
999 1031
1000 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); 1032 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0);
1001 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); 1033 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false,
1034 base::MakeUnique<base::DefaultClock>());
1002 1035
1003 // Dismissed IDs should not be pruned yet, because the downloads list at the 1036 // Dismissed IDs should not be pruned yet, because the downloads list at the
1004 // manager is not complete. 1037 // manager is not complete.
1005 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); 1038 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
1006 EXPECT_THAT(GetDismissedSuggestions(), 1039 EXPECT_THAT(GetDismissedSuggestions(),
1007 UnorderedElementsAre(HasUrl("http://download.com/1"))); 1040 UnorderedElementsAre(HasUrl("http://download.com/1")));
1008 1041
1009 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)); 1042 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _));
1010 1043
1011 downloads_manager()->mutable_items()->clear(); 1044 downloads_manager()->mutable_items()->clear();
1012 FireHistoryQueryComplete(); 1045 FireHistoryQueryComplete();
1013 1046
1014 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); 1047 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
1015 // Once the manager has been loaded, the ids should be pruned. 1048 // Once the manager has been loaded, the ids should be pruned.
1016 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); 1049 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty());
1017 } 1050 }
1051
1052 TEST_F(DownloadSuggestionsProviderTest, ShouldNotShowOldDownloads) {
1053 IgnoreOnCategoryStatusChangedToAvailable();
1054 IgnoreOnSuggestionInvalidated();
1055
1056 const int kDefaultMaxDownloadAgeDays = 6 * 7;
1057
1058 base::Time now;
1059 ASSERT_TRUE(base::Time::FromString("Tue, 31 Jan 2017 13:00:00", &now));
1060 const base::Time not_old =
1061 now - base::TimeDelta::FromDays(kDefaultMaxDownloadAgeDays - 1);
1062 const base::Time old =
1063 now - base::TimeDelta::FromDays(kDefaultMaxDownloadAgeDays + 1);
1064 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
1065 offline_pages_model()->mutable_items()->at(0).creation_time = not_old;
1066 offline_pages_model()->mutable_items()->at(1).creation_time = old;
1067 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
1068 downloads_manager()->mutable_items()->at(0)->SetStartTime(not_old);
1069 downloads_manager()->mutable_items()->at(1)->SetStartTime(old);
1070
1071 EXPECT_CALL(
1072 *observer(),
1073 OnNewSuggestions(_, downloads_category(),
1074 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
tschumann 2017/02/02 14:30:34 just as an FYI (no need to fix now). This is a go
1075 HasUrl("http://download.com/1"))));
1076 auto test_clock = base::MakeUnique<base::SimpleTestClock>();
1077 test_clock->SetNow(now);
1078 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true,
1079 std::move(test_clock));
1080 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698