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

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

Issue 2629603002: [NTP::Downloads] Fetch assets once the manager is loaded. (Closed)
Patch Set: clean rebase. Created 3 years, 11 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 "chrome/browser/ntp_snippets/fake_download_item.h" 13 #include "chrome/browser/ntp_snippets/fake_download_item.h"
14 #include "components/ntp_snippets/category.h" 14 #include "components/ntp_snippets/category.h"
15 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h" 15 #include "components/ntp_snippets/mock_content_suggestions_provider_observer.h"
16 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h" 16 #include "components/ntp_snippets/offline_pages/offline_pages_test_utils.h"
17 #include "components/offline_pages/core/client_namespace_constants.h" 17 #include "components/offline_pages/core/client_namespace_constants.h"
18 #include "components/prefs/testing_pref_service.h" 18 #include "components/prefs/testing_pref_service.h"
19 #include "content/public/test/mock_download_item.h" 19 #include "content/public/test/mock_download_item.h"
20 #include "content/public/test/mock_download_manager.h" 20 #include "content/public/test/mock_download_manager.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using content::DownloadItem; 23 using content::DownloadItem;
24 using content::MockDownloadManager; 24 using content::MockDownloadManager;
25 using ntp_snippets::Category; 25 using ntp_snippets::Category;
26 using ntp_snippets::CategoryStatus;
26 using ntp_snippets::ContentSuggestion; 27 using ntp_snippets::ContentSuggestion;
27 using ntp_snippets::ContentSuggestionsProvider; 28 using ntp_snippets::ContentSuggestionsProvider;
28 using ntp_snippets::MockContentSuggestionsProviderObserver; 29 using ntp_snippets::MockContentSuggestionsProviderObserver;
29 using ntp_snippets::test::CaptureDismissedSuggestions; 30 using ntp_snippets::test::CaptureDismissedSuggestions;
30 using ntp_snippets::test::FakeOfflinePageModel; 31 using ntp_snippets::test::FakeOfflinePageModel;
31 using ntp_snippets::CategoryStatus;
32 using offline_pages::ClientId; 32 using offline_pages::ClientId;
33 using offline_pages::OfflinePageItem; 33 using offline_pages::OfflinePageItem;
34 using test::FakeDownloadItem; 34 using test::FakeDownloadItem;
35 using testing::_; 35 using testing::_;
36 using testing::AllOf; 36 using testing::AllOf;
37 using testing::AnyNumber; 37 using testing::AnyNumber;
38 using testing::ElementsAre; 38 using testing::ElementsAre;
39 using testing::IsEmpty; 39 using testing::IsEmpty;
40 using testing::Lt; 40 using testing::Lt;
41 using testing::Mock; 41 using testing::Mock;
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 EXPECT_CALL(observer_, 244 EXPECT_CALL(observer_,
245 OnCategoryStatusChanged(_, downloads_category(), 245 OnCategoryStatusChanged(_, downloads_category(),
246 CategoryStatus::AVAILABLE_LOADING)) 246 CategoryStatus::AVAILABLE_LOADING))
247 .Times(AnyNumber()); 247 .Times(AnyNumber());
248 } 248 }
249 249
250 void IgnoreOnSuggestionInvalidated() { 250 void IgnoreOnSuggestionInvalidated() {
251 EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber()); 251 EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber());
252 } 252 }
253 253
254 DownloadSuggestionsProvider* CreateLoadedProvider(bool show_assets,
255 bool show_offline_pages) {
256 CreateProvider(show_assets, show_offline_pages);
257 FireHistoryQueryComplete();
258 return provider_.get();
259 }
260
254 DownloadSuggestionsProvider* CreateProvider(bool show_assets, 261 DownloadSuggestionsProvider* CreateProvider(bool show_assets,
255 bool show_offline_pages) { 262 bool show_offline_pages) {
256 DCHECK(!provider_); 263 DCHECK(!provider_);
257 DCHECK(show_assets || show_offline_pages); 264 DCHECK(show_assets || show_offline_pages);
258 provider_ = base::MakeUnique<DownloadSuggestionsProvider>( 265 provider_ = base::MakeUnique<DownloadSuggestionsProvider>(
259 &observer_, show_offline_pages ? &offline_pages_model_ : nullptr, 266 &observer_, show_offline_pages ? &offline_pages_model_ : nullptr,
260 show_assets ? &downloads_manager_ : nullptr, pref_service(), 267 show_assets ? &downloads_manager_ : nullptr,
268 /*download_history=*/nullptr, pref_service(),
vitaliii 2017/01/16 14:32:39 treib@, do you have any idea how I could test this
Marc Treib 2017/01/16 14:51:07 There are some tests that instantiate a DownloadHi
vitaliii 2017/01/17 08:33:28 Thank you for the links. Constructing it happened
Marc Treib 2017/01/17 08:50:04 What exactly do you mean by "requires UI thread"?
vitaliii 2017/01/17 16:12:41 The issue was in |DCHECK_CURRENTLY_ON(content::Bro
261 /*download_manager_ui_enabled=*/false); 269 /*download_manager_ui_enabled=*/false);
262 return provider_.get(); 270 return provider_.get();
263 } 271 }
264 272
265 void DestroyProvider() { provider_.reset(); } 273 void DestroyProvider() { provider_.reset(); }
266 274
267 Category downloads_category() { 275 Category downloads_category() {
268 return Category::FromKnownCategory( 276 return Category::FromKnownCategory(
269 ntp_snippets::KnownCategories::DOWNLOADS); 277 ntp_snippets::KnownCategories::DOWNLOADS);
270 } 278 }
(...skipping 19 matching lines...) Expand all
290 downloads_manager_.NotifyDownloadCreated(item); 298 downloads_manager_.NotifyDownloadCreated(item);
291 } 299 }
292 300
293 void FireDownloadsCreated( 301 void FireDownloadsCreated(
294 const std::vector<std::unique_ptr<FakeDownloadItem>>& items) { 302 const std::vector<std::unique_ptr<FakeDownloadItem>>& items) {
295 for (const auto& item : items) { 303 for (const auto& item : items) {
296 FireDownloadCreated(item.get()); 304 FireDownloadCreated(item.get());
297 } 305 }
298 } 306 }
299 307
308 void FireHistoryQueryComplete() { provider_->OnHistoryQueryComplete(); }
309
300 ContentSuggestion::ID GetDummySuggestionId(int id, bool is_offline_page) { 310 ContentSuggestion::ID GetDummySuggestionId(int id, bool is_offline_page) {
301 return ContentSuggestion::ID( 311 return ContentSuggestion::ID(
302 downloads_category(), 312 downloads_category(),
303 (is_offline_page ? "O" : "D") + base::IntToString(id)); 313 (is_offline_page ? "O" : "D") + base::IntToString(id));
304 } 314 }
305 315
306 std::vector<ContentSuggestion> GetDismissedSuggestions() { 316 std::vector<ContentSuggestion> GetDismissedSuggestions() {
307 std::vector<ContentSuggestion> dismissed_suggestions; 317 std::vector<ContentSuggestion> dismissed_suggestions;
308 // This works synchronously because both fake data sources were designed so. 318 // This works synchronously because both fake data sources were designed so.
309 provider()->GetDismissedSuggestionsForDebugging( 319 provider()->GetDismissedSuggestionsForDebugging(
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 OnNewSuggestions( 353 OnNewSuggestions(
344 _, downloads_category(), 354 _, downloads_category(),
345 UnorderedElementsAre(AllOf(HasUrl("http://dummy.com/1"), 355 UnorderedElementsAre(AllOf(HasUrl("http://dummy.com/1"),
346 HasDownloadSuggestionExtra( 356 HasDownloadSuggestionExtra(
347 /*is_download_asset=*/false, 357 /*is_download_asset=*/false,
348 FILE_PATH_LITERAL(""), "")), 358 FILE_PATH_LITERAL(""), "")),
349 AllOf(HasUrl("http://dummy.com/2"), 359 AllOf(HasUrl("http://dummy.com/2"),
350 HasDownloadSuggestionExtra( 360 HasDownloadSuggestionExtra(
351 /*is_download_asset=*/false, 361 /*is_download_asset=*/false,
352 FILE_PATH_LITERAL(""), ""))))); 362 FILE_PATH_LITERAL(""), "")))));
353 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 363 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
354 } 364 }
355 365
356 TEST_F(DownloadSuggestionsProviderTest, 366 TEST_F(DownloadSuggestionsProviderTest,
357 ShouldConvertDownloadItemsToSuggestions) { 367 ShouldConvertDownloadItemsToSuggestions) {
358 IgnoreOnCategoryStatusChangedToAvailable(); 368 IgnoreOnCategoryStatusChangedToAvailable();
359 IgnoreOnSuggestionInvalidated(); 369 IgnoreOnSuggestionInvalidated();
360 370
361 EXPECT_CALL(*observer(), 371 EXPECT_CALL(*observer(),
362 OnNewSuggestions(_, downloads_category(), SizeIs(0))); 372 OnNewSuggestions(_, downloads_category(), SizeIs(0)));
363 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 373 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
364 374
365 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = 375 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads =
366 CreateDummyAssetDownloads({1, 2}); 376 CreateDummyAssetDownloads({1, 2});
367 377
368 EXPECT_CALL(*observer(), 378 EXPECT_CALL(*observer(),
369 OnNewSuggestions(_, downloads_category(), 379 OnNewSuggestions(_, downloads_category(),
370 UnorderedElementsAre(AllOf( 380 UnorderedElementsAre(AllOf(
371 HasUrl("http://download.com/1"), 381 HasUrl("http://download.com/1"),
372 HasDownloadSuggestionExtra( 382 HasDownloadSuggestionExtra(
373 /*is_download_asset=*/true, 383 /*is_download_asset=*/true,
(...skipping 20 matching lines...) Expand all
394 404
395 TEST_F(DownloadSuggestionsProviderTest, ShouldMixInBothSources) { 405 TEST_F(DownloadSuggestionsProviderTest, ShouldMixInBothSources) {
396 IgnoreOnCategoryStatusChangedToAvailable(); 406 IgnoreOnCategoryStatusChangedToAvailable();
397 IgnoreOnSuggestionInvalidated(); 407 IgnoreOnSuggestionInvalidated();
398 408
399 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 409 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
400 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), 410 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(),
401 UnorderedElementsAre( 411 UnorderedElementsAre(
402 HasUrl("http://dummy.com/1"), 412 HasUrl("http://dummy.com/1"),
403 HasUrl("http://dummy.com/2")))); 413 HasUrl("http://dummy.com/2"))));
404 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 414 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
405 415
406 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = 416 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads =
407 CreateDummyAssetDownloads({1, 2}); 417 CreateDummyAssetDownloads({1, 2});
408 418
409 EXPECT_CALL( 419 EXPECT_CALL(
410 *observer(), 420 *observer(),
411 OnNewSuggestions(_, downloads_category(), 421 OnNewSuggestions(_, downloads_category(),
412 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 422 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
413 HasUrl("http://dummy.com/2"), 423 HasUrl("http://dummy.com/2"),
414 HasUrl("http://download.com/1")))); 424 HasUrl("http://download.com/1"))));
(...skipping 20 matching lines...) Expand all
435 445
436 (*offline_pages_model()->mutable_items()) 446 (*offline_pages_model()->mutable_items())
437 .push_back(CreateDummyOfflinePage(1, yesterday)); 447 .push_back(CreateDummyOfflinePage(1, yesterday));
438 (*offline_pages_model()->mutable_items()) 448 (*offline_pages_model()->mutable_items())
439 .push_back(CreateDummyOfflinePage(2, tomorrow)); 449 .push_back(CreateDummyOfflinePage(2, tomorrow));
440 450
441 EXPECT_CALL(*observer(), 451 EXPECT_CALL(*observer(),
442 OnNewSuggestions(_, downloads_category(), 452 OnNewSuggestions(_, downloads_category(),
443 ElementsAre(HasUrl("http://dummy.com/2"), 453 ElementsAre(HasUrl("http://dummy.com/2"),
444 HasUrl("http://dummy.com/1")))); 454 HasUrl("http://dummy.com/1"))));
445 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 455 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
446 456
447 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads; 457 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads;
448 asset_downloads.push_back(CreateDummyAssetDownload(3, next_week)); 458 asset_downloads.push_back(CreateDummyAssetDownload(3, next_week));
449 asset_downloads.push_back(CreateDummyAssetDownload(4, now)); 459 asset_downloads.push_back(CreateDummyAssetDownload(4, now));
450 460
451 EXPECT_CALL(*observer(), 461 EXPECT_CALL(*observer(),
452 OnNewSuggestions(_, downloads_category(), 462 OnNewSuggestions(_, downloads_category(),
453 ElementsAre(HasUrl("http://download.com/3"), 463 ElementsAre(HasUrl("http://download.com/3"),
454 HasUrl("http://dummy.com/2"), 464 HasUrl("http://dummy.com/2"),
455 HasUrl("http://dummy.com/1")))); 465 HasUrl("http://dummy.com/1"))));
456 FireDownloadCreated(asset_downloads[0].get()); 466 FireDownloadCreated(asset_downloads[0].get());
457 467
458 EXPECT_CALL(*observer(), 468 EXPECT_CALL(*observer(),
459 OnNewSuggestions(_, downloads_category(), 469 OnNewSuggestions(_, downloads_category(),
460 ElementsAre(HasUrl("http://download.com/3"), 470 ElementsAre(HasUrl("http://download.com/3"),
461 HasUrl("http://dummy.com/2"), 471 HasUrl("http://dummy.com/2"),
462 HasUrl("http://download.com/4"), 472 HasUrl("http://download.com/4"),
463 HasUrl("http://dummy.com/1")))); 473 HasUrl("http://dummy.com/1"))));
464 FireDownloadCreated(asset_downloads[1].get()); 474 FireDownloadCreated(asset_downloads[1].get());
465 } 475 }
466 476
467 TEST_F(DownloadSuggestionsProviderTest, 477 TEST_F(DownloadSuggestionsProviderTest,
468 ShouldDismissWithoutNotifyingObservers) { 478 ShouldDismissWithoutNotifyingObservers) {
469 IgnoreOnCategoryStatusChangedToAvailable(); 479 IgnoreOnCategoryStatusChangedToAvailable();
470 480
471 EXPECT_CALL(*observer(), 481 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
472 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) 482 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
473 .Times(2);
474 EXPECT_CALL( 483 EXPECT_CALL(
475 *observer(), 484 *observer(),
476 OnNewSuggestions(_, downloads_category(), 485 OnNewSuggestions(_, downloads_category(),
477 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 486 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
478 HasUrl("http://dummy.com/2"), 487 HasUrl("http://dummy.com/2"),
479 HasUrl("http://download.com/1"), 488 HasUrl("http://download.com/1"),
480 HasUrl("http://download.com/2")))); 489 HasUrl("http://download.com/2"))));
481 490 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
482 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
483 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
484 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
485 FireDownloadsCreated(downloads_manager()->items());
486 491
487 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); 492 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0);
488 EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, _)).Times(0); 493 EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, _)).Times(0);
489 provider()->DismissSuggestion( 494 provider()->DismissSuggestion(
490 GetDummySuggestionId(1, /*is_offline_page=*/true)); 495 GetDummySuggestionId(1, /*is_offline_page=*/true));
491 provider()->DismissSuggestion( 496 provider()->DismissSuggestion(
492 GetDummySuggestionId(1, /*is_offline_page=*/false)); 497 GetDummySuggestionId(1, /*is_offline_page=*/false));
493 498
494 // |downloads_manager_| is destroyed after the |provider_|, so the provider 499 // |downloads_manager_| is destroyed after the |provider_|, so the provider
495 // will not observe download items being destroyed. 500 // will not observe download items being destroyed.
496 } 501 }
497 502
498 TEST_F(DownloadSuggestionsProviderTest, 503 TEST_F(DownloadSuggestionsProviderTest,
499 ShouldNotReportDismissedSuggestionsOnNewData) { 504 ShouldNotReportDismissedSuggestionsOnNewData) {
500 IgnoreOnCategoryStatusChangedToAvailable(); 505 IgnoreOnCategoryStatusChangedToAvailable();
501 506
502 EXPECT_CALL(*observer(), 507 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
503 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) 508 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
504 .Times(2);
505 EXPECT_CALL( 509 EXPECT_CALL(
506 *observer(), 510 *observer(),
507 OnNewSuggestions(_, downloads_category(), 511 OnNewSuggestions(_, downloads_category(),
508 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 512 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
509 HasUrl("http://dummy.com/2"), 513 HasUrl("http://dummy.com/2"),
510 HasUrl("http://download.com/1"), 514 HasUrl("http://download.com/1"),
511 HasUrl("http://download.com/2")))); 515 HasUrl("http://download.com/2"))));
512 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 516 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
513 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
514 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
515 FireDownloadsCreated(downloads_manager()->items());
516 517
517 provider()->DismissSuggestion( 518 provider()->DismissSuggestion(
518 GetDummySuggestionId(1, /*is_offline_page=*/true)); 519 GetDummySuggestionId(1, /*is_offline_page=*/true));
519 provider()->DismissSuggestion( 520 provider()->DismissSuggestion(
520 GetDummySuggestionId(1, /*is_offline_page=*/false)); 521 GetDummySuggestionId(1, /*is_offline_page=*/false));
521 522
522 EXPECT_CALL( 523 EXPECT_CALL(
523 *observer(), 524 *observer(),
524 OnNewSuggestions(_, downloads_category(), 525 OnNewSuggestions(_, downloads_category(),
525 UnorderedElementsAre(HasUrl("http://dummy.com/2"), 526 UnorderedElementsAre(HasUrl("http://dummy.com/2"),
526 HasUrl("http://dummy.com/3"), 527 HasUrl("http://dummy.com/3"),
527 HasUrl("http://download.com/2")))); 528 HasUrl("http://download.com/2"))));
528 AddOfflinePage(CreateDummyOfflinePage(3)); 529 AddOfflinePage(CreateDummyOfflinePage(3));
529 } 530 }
530 531
531 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { 532 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) {
532 IgnoreOnCategoryStatusChangedToAvailable(); 533 IgnoreOnCategoryStatusChangedToAvailable();
533 534
534 EXPECT_CALL(*observer(), 535 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
535 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) 536 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
536 .Times(2);
537 EXPECT_CALL( 537 EXPECT_CALL(
538 *observer(), 538 *observer(),
539 OnNewSuggestions(_, downloads_category(), 539 OnNewSuggestions(_, downloads_category(),
540 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 540 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
541 HasUrl("http://dummy.com/2"), 541 HasUrl("http://dummy.com/2"),
542 HasUrl("http://download.com/1"), 542 HasUrl("http://download.com/1"),
543 HasUrl("http://download.com/2")))); 543 HasUrl("http://download.com/2"))));
544 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 544 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
545 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
546 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
547 FireDownloadsCreated(downloads_manager()->items());
548 545
549 provider()->DismissSuggestion( 546 provider()->DismissSuggestion(
550 GetDummySuggestionId(1, /*is_offline_page=*/true)); 547 GetDummySuggestionId(1, /*is_offline_page=*/true));
551 provider()->DismissSuggestion( 548 provider()->DismissSuggestion(
552 GetDummySuggestionId(1, /*is_offline_page=*/false)); 549 GetDummySuggestionId(1, /*is_offline_page=*/false));
553 550
554 EXPECT_THAT(GetDismissedSuggestions(), 551 EXPECT_THAT(GetDismissedSuggestions(),
555 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 552 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
556 HasUrl("http://download.com/1"))); 553 HasUrl("http://download.com/1")));
557 } 554 }
558 555
559 TEST_F(DownloadSuggestionsProviderTest, ShouldClearDismissedSuggestions) { 556 TEST_F(DownloadSuggestionsProviderTest, ShouldClearDismissedSuggestions) {
560 IgnoreOnCategoryStatusChangedToAvailable(); 557 IgnoreOnCategoryStatusChangedToAvailable();
561 558
562 EXPECT_CALL(*observer(), 559 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
563 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) 560 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
564 .Times(2);
565 EXPECT_CALL( 561 EXPECT_CALL(
566 *observer(), 562 *observer(),
567 OnNewSuggestions(_, downloads_category(), 563 OnNewSuggestions(_, downloads_category(),
568 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 564 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
569 HasUrl("http://dummy.com/2"), 565 HasUrl("http://dummy.com/2"),
570 HasUrl("http://download.com/1"), 566 HasUrl("http://download.com/1"),
571 HasUrl("http://download.com/2")))); 567 HasUrl("http://download.com/2"))));
572 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 568 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
573 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
574 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
575 FireDownloadsCreated(downloads_manager()->items());
576 569
577 provider()->DismissSuggestion( 570 provider()->DismissSuggestion(
578 GetDummySuggestionId(1, /*is_offline_page=*/true)); 571 GetDummySuggestionId(1, /*is_offline_page=*/true));
579 provider()->DismissSuggestion( 572 provider()->DismissSuggestion(
580 GetDummySuggestionId(1, /*is_offline_page=*/false)); 573 GetDummySuggestionId(1, /*is_offline_page=*/false));
581 574
582 EXPECT_CALL( 575 EXPECT_CALL(
583 *observer(), 576 *observer(),
584 OnNewSuggestions(_, downloads_category(), 577 OnNewSuggestions(_, downloads_category(),
585 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 578 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
586 HasUrl("http://dummy.com/2"), 579 HasUrl("http://dummy.com/2"),
587 HasUrl("http://download.com/1"), 580 HasUrl("http://download.com/1"),
588 HasUrl("http://download.com/2")))); 581 HasUrl("http://download.com/2"))));
589 provider()->ClearDismissedSuggestionsForDebugging(downloads_category()); 582 provider()->ClearDismissedSuggestionsForDebugging(downloads_category());
590 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); 583 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty());
591 } 584 }
592 585
593 TEST_F(DownloadSuggestionsProviderTest, 586 TEST_F(DownloadSuggestionsProviderTest,
594 ShouldNotDismissOtherTypeWithTheSameID) { 587 ShouldNotDismissOtherTypeWithTheSameID) {
595 IgnoreOnCategoryStatusChangedToAvailable(); 588 IgnoreOnCategoryStatusChangedToAvailable();
596 589
597 EXPECT_CALL(*observer(), 590 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
598 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) 591 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
599 .Times(2);
600 EXPECT_CALL( 592 EXPECT_CALL(
601 *observer(), 593 *observer(),
602 OnNewSuggestions(_, downloads_category(), 594 OnNewSuggestions(_, downloads_category(),
603 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 595 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
604 HasUrl("http://dummy.com/2"), 596 HasUrl("http://dummy.com/2"),
605 HasUrl("http://download.com/1"), 597 HasUrl("http://download.com/1"),
606 HasUrl("http://download.com/2")))); 598 HasUrl("http://download.com/2"))));
607 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 599 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
608 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
609 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
610 FireDownloadsCreated(downloads_manager()->items());
611 600
612 provider()->DismissSuggestion( 601 provider()->DismissSuggestion(
613 GetDummySuggestionId(1, /*is_offline_page=*/true)); 602 GetDummySuggestionId(1, /*is_offline_page=*/true));
614 603
615 EXPECT_CALL( 604 EXPECT_CALL(
616 *observer(), 605 *observer(),
617 OnNewSuggestions(_, downloads_category(), 606 OnNewSuggestions(_, downloads_category(),
618 UnorderedElementsAre(HasUrl("http://dummy.com/2"), 607 UnorderedElementsAre(HasUrl("http://dummy.com/2"),
619 HasUrl("http://dummy.com/3"), 608 HasUrl("http://dummy.com/3"),
620 HasUrl("http://download.com/1"), 609 HasUrl("http://download.com/1"),
621 HasUrl("http://download.com/2")))); 610 HasUrl("http://download.com/2"))));
622 AddOfflinePage(CreateDummyOfflinePage(3)); 611 AddOfflinePage(CreateDummyOfflinePage(3));
623 } 612 }
624 613
625 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { 614 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) {
626 IgnoreOnCategoryStatusChangedToAvailable(); 615 IgnoreOnCategoryStatusChangedToAvailable();
627 616
628 EXPECT_CALL(*observer(), 617 // Currently the provider stores five items in its internal cache, so six
629 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(5ul)))) 618 // items are needed to check whether all downloads are fetched on dismissal.
630 .Times(5); 619 *(downloads_manager()->mutable_items()) =
620 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6});
631 EXPECT_CALL( 621 EXPECT_CALL(
632 *observer(), 622 *observer(),
633 OnNewSuggestions(_, downloads_category(), 623 OnNewSuggestions(_, downloads_category(),
634 UnorderedElementsAre(HasUrl("http://download.com/1"), 624 UnorderedElementsAre(HasUrl("http://download.com/1"),
635 HasUrl("http://download.com/2"), 625 HasUrl("http://download.com/2"),
636 HasUrl("http://download.com/3"), 626 HasUrl("http://download.com/3"),
637 HasUrl("http://download.com/4"), 627 HasUrl("http://download.com/4"),
638 HasUrl("http://download.com/5")))); 628 HasUrl("http://download.com/5"))));
639 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 629 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
640 // Currently the provider stores five items in its internal cache, so six
641 // items are needed to check whether all downloads are fetched on dismissal.
642 *(downloads_manager()->mutable_items()) =
643 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6});
644 FireDownloadsCreated(downloads_manager()->items());
645 630
646 provider()->DismissSuggestion( 631 provider()->DismissSuggestion(
647 GetDummySuggestionId(1, /*is_offline_page=*/false)); 632 GetDummySuggestionId(1, /*is_offline_page=*/false));
648 provider()->DismissSuggestion( 633 provider()->DismissSuggestion(
649 GetDummySuggestionId(2, /*is_offline_page=*/false)); 634 GetDummySuggestionId(2, /*is_offline_page=*/false));
650 635
651 // The provider is not notified about the 6th item, however, it must report 636 // The provider is not notified about the 6th item, however, it must report
652 // it now. 637 // it now.
653 EXPECT_CALL( 638 EXPECT_CALL(
654 *observer(), 639 *observer(),
655 OnNewSuggestions(_, downloads_category(), 640 OnNewSuggestions(_, downloads_category(),
656 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 641 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
657 HasUrl("http://download.com/3"), 642 HasUrl("http://download.com/3"),
658 HasUrl("http://download.com/4"), 643 HasUrl("http://download.com/4"),
659 HasUrl("http://download.com/5"), 644 HasUrl("http://download.com/5"),
660 HasUrl("http://download.com/6")))); 645 HasUrl("http://download.com/6"))));
661 AddOfflinePage(CreateDummyOfflinePage(1)); 646 AddOfflinePage(CreateDummyOfflinePage(1));
662 } 647 }
663 648
664 TEST_F(DownloadSuggestionsProviderTest, 649 TEST_F(DownloadSuggestionsProviderTest,
665 ShouldInvalidateWhenUnderlyingItemDeleted) { 650 ShouldInvalidateWhenUnderlyingItemDeleted) {
666 IgnoreOnCategoryStatusChangedToAvailable(); 651 IgnoreOnCategoryStatusChangedToAvailable();
667 652
668 EXPECT_CALL(*observer(), 653 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
669 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(3ul)))); 654 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
670 EXPECT_CALL( 655 EXPECT_CALL(
671 *observer(), 656 *observer(),
672 OnNewSuggestions(_, downloads_category(), 657 OnNewSuggestions(_, downloads_category(),
673 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 658 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
674 HasUrl("http://dummy.com/2"), 659 HasUrl("http://dummy.com/2"),
675 HasUrl("http://download.com/1")))); 660 HasUrl("http://download.com/1"))));
676 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 661 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
677 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
678 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
679 FireDownloadsCreated(downloads_manager()->items());
680 662
681 // We add another item manually, so that when it gets deleted it is not 663 // We add another item manually, so that when it gets deleted it is not
682 // present in DownloadsManager list. 664 // present in DownloadsManager list.
683 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload(2); 665 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload(2);
684 EXPECT_CALL( 666 EXPECT_CALL(
685 *observer(), 667 *observer(),
686 OnNewSuggestions(_, downloads_category(), 668 OnNewSuggestions(_, downloads_category(),
687 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 669 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
688 HasUrl("http://dummy.com/2"), 670 HasUrl("http://dummy.com/2"),
689 HasUrl("http://download.com/1"), 671 HasUrl("http://download.com/1"),
690 HasUrl("http://download.com/2")))); 672 HasUrl("http://download.com/2"))));
691 FireDownloadCreated(removed_item.get()); 673 FireDownloadCreated(removed_item.get());
692 674
693 EXPECT_CALL(*observer(), 675 EXPECT_CALL(*observer(),
694 OnSuggestionInvalidated( 676 OnSuggestionInvalidated(
695 _, GetDummySuggestionId(1, /*is_offline_page=*/true))); 677 _, GetDummySuggestionId(1, /*is_offline_page=*/true)));
696 FireOfflinePageDeleted(offline_pages_model()->items()[0]); 678 FireOfflinePageDeleted(offline_pages_model()->items()[0]);
697 679
698 EXPECT_CALL(*observer(), 680 EXPECT_CALL(*observer(),
699 OnSuggestionInvalidated( 681 OnSuggestionInvalidated(
700 _, GetDummySuggestionId(2, /*is_offline_page=*/false))); 682 _, GetDummySuggestionId(2, /*is_offline_page=*/false)));
701 // |OnDownloadItemDestroyed| is called from |removed_item|'s destructor. 683 // |OnDownloadItemDestroyed| is called from |removed_item|'s destructor.
702 removed_item.reset(); 684 removed_item.reset();
703 } 685 }
704 686
705 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceRemovedItemWithNewData) { 687 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceRemovedItemWithNewData) {
706 IgnoreOnCategoryStatusChangedToAvailable(); 688 IgnoreOnCategoryStatusChangedToAvailable();
707 IgnoreOnSuggestionInvalidated(); 689 IgnoreOnSuggestionInvalidated();
708 690
709 EXPECT_CALL(*observer(), 691 *(downloads_manager()->mutable_items()) =
710 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(5ul)))) 692 CreateDummyAssetDownloads({1, 2, 3, 4, 5});
711 .Times(5);
712 EXPECT_CALL( 693 EXPECT_CALL(
713 *observer(), 694 *observer(),
714 OnNewSuggestions(_, downloads_category(), 695 OnNewSuggestions(_, downloads_category(),
715 UnorderedElementsAre(HasUrl("http://download.com/1"), 696 UnorderedElementsAre(HasUrl("http://download.com/1"),
716 HasUrl("http://download.com/2"), 697 HasUrl("http://download.com/2"),
717 HasUrl("http://download.com/3"), 698 HasUrl("http://download.com/3"),
718 HasUrl("http://download.com/4"), 699 HasUrl("http://download.com/4"),
719 HasUrl("http://download.com/5")))); 700 HasUrl("http://download.com/5"))));
720 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 701 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
721 *(downloads_manager()->mutable_items()) =
722 CreateDummyAssetDownloads({1, 2, 3, 4, 5});
723 FireDownloadsCreated(downloads_manager()->items());
724 702
725 // Note that |CreateDummyAssetDownloads| creates items "downloaded" before 703 // Note that |CreateDummyAssetDownloads| creates items "downloaded" before
726 // |base::Time::Now()|, so for a new item the time is set in future to enforce 704 // |base::Time::Now()|, so for a new item the time is set in future to enforce
727 // the provider to show the new item. 705 // the provider to show the new item.
728 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload( 706 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload(
729 100, base::Time::Now() + base::TimeDelta::FromDays(1)); 707 100, base::Time::Now() + base::TimeDelta::FromDays(1));
730 EXPECT_CALL( 708 EXPECT_CALL(
731 *observer(), 709 *observer(),
732 OnNewSuggestions( 710 OnNewSuggestions(
733 _, downloads_category(), 711 _, downloads_category(),
(...skipping 23 matching lines...) Expand all
757 IgnoreOnSuggestionInvalidated(); 735 IgnoreOnSuggestionInvalidated();
758 736
759 auto offline_pages = CreateDummyOfflinePages({1, 2, 3}); 737 auto offline_pages = CreateDummyOfflinePages({1, 2, 3});
760 738
761 *(offline_pages_model()->mutable_items()) = offline_pages; 739 *(offline_pages_model()->mutable_items()) = offline_pages;
762 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), 740 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(),
763 UnorderedElementsAre( 741 UnorderedElementsAre(
764 HasUrl("http://dummy.com/1"), 742 HasUrl("http://dummy.com/1"),
765 HasUrl("http://dummy.com/2"), 743 HasUrl("http://dummy.com/2"),
766 HasUrl("http://dummy.com/3")))); 744 HasUrl("http://dummy.com/3"))));
767 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 745 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
768 746
769 provider()->DismissSuggestion( 747 provider()->DismissSuggestion(
770 GetDummySuggestionId(1, /*is_offline_page=*/true)); 748 GetDummySuggestionId(1, /*is_offline_page=*/true));
771 provider()->DismissSuggestion( 749 provider()->DismissSuggestion(
772 GetDummySuggestionId(2, /*is_offline_page=*/true)); 750 GetDummySuggestionId(2, /*is_offline_page=*/true));
773 provider()->DismissSuggestion( 751 provider()->DismissSuggestion(
774 GetDummySuggestionId(3, /*is_offline_page=*/true)); 752 GetDummySuggestionId(3, /*is_offline_page=*/true));
775 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); 753 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3));
776 754
777 // Note that the first suggestion is not removed from |offline_pages_model| 755 // Note that the first suggestion is not removed from |offline_pages_model|
778 // storage, because otherwise |GetDismissedSuggestions| cannot return it. 756 // storage, because otherwise |GetDismissedSuggestions| cannot return it.
779 FireOfflinePageDeleted(offline_pages[0]); 757 FireOfflinePageDeleted(offline_pages[0]);
780 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); 758 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2));
781 759
782 // Prune when offline page is deleted. 760 // Prune when offline page is deleted.
783 FireOfflinePageDeleted(offline_pages_model()->items()[1]); 761 FireOfflinePageDeleted(offline_pages_model()->items()[1]);
784 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); 762 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1));
785 } 763 }
786 764
787 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { 765 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) {
788 IgnoreOnCategoryStatusChangedToAvailable(); 766 IgnoreOnCategoryStatusChangedToAvailable();
789 IgnoreOnSuggestionInvalidated(); 767 IgnoreOnSuggestionInvalidated();
790 768
791 EXPECT_CALL(*observer(),
792 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(3ul))))
793 .Times(3);
794 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
795 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); 769 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
796 FireDownloadsCreated(downloads_manager()->items()); 770 EXPECT_CALL(
771 *observer(),
772 OnNewSuggestions(_, downloads_category(),
773 UnorderedElementsAre(HasUrl("http://download.com/1"),
774 HasUrl("http://download.com/2"))));
775 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
797 776
798 provider()->DismissSuggestion( 777 provider()->DismissSuggestion(
799 GetDummySuggestionId(1, /*is_offline_page=*/false)); 778 GetDummySuggestionId(1, /*is_offline_page=*/false));
800 provider()->DismissSuggestion( 779 provider()->DismissSuggestion(
801 GetDummySuggestionId(2, /*is_offline_page=*/false)); 780 GetDummySuggestionId(2, /*is_offline_page=*/false));
802 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); 781 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2));
803 782
804 downloads_manager()->items()[0]->NotifyDownloadDestroyed(); 783 downloads_manager()->items()[0]->NotifyDownloadDestroyed();
805 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); 784 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1));
806 } 785 }
807 786
808 TEST_F(DownloadSuggestionsProviderTest, 787 TEST_F(DownloadSuggestionsProviderTest,
809 ShouldFetchAssetDownloadsOnStartupButOnlyOnce) { 788 ShouldFetchAssetDownloadsOnStartupButOnlyOnce) {
810 IgnoreOnCategoryStatusChangedToAvailable(); 789 IgnoreOnCategoryStatusChangedToAvailable();
811 790
812 // Downloads manager was created before the provider, so |OnDownloadCreated| 791 // Downloads manager was created before the provider, so |OnDownloadCreated|
813 // calls "were" missed, but the provider must show missed items anyway. 792 // calls "were" missed, but the provider must show missed items anyway.
814 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); 793 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
815 EXPECT_CALL( 794 EXPECT_CALL(*observer(),
816 *observer(), 795 OnNewSuggestions(
817 OnNewSuggestions(_, downloads_category(), 796 _, downloads_category(),
818 UnorderedElementsAre(HasUrl("http://download.com/1"), 797 UnorderedElementsAre(HasUrl("http://download.com/1"),
819 HasUrl("http://download.com/2")))); 798 HasUrl("http://download.com/2!!!!!"))));
820 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 799 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
821 } 800 }
822 801
823 TEST_F(DownloadSuggestionsProviderTest, 802 TEST_F(DownloadSuggestionsProviderTest,
824 ShouldInvalidateAssetDownloadWhenItsFileRemoved) { 803 ShouldInvalidateAssetDownloadWhenItsFileRemoved) {
825 IgnoreOnCategoryStatusChangedToAvailable(); 804 IgnoreOnCategoryStatusChangedToAvailable();
826 805
827 EXPECT_CALL(*observer(),
828 OnNewSuggestions(_, downloads_category(), IsEmpty()));
829 EXPECT_CALL(*observer(),
830 OnNewSuggestions(_, downloads_category(), SizeIs(1)));
831 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
832 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); 806 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
833 FireDownloadsCreated(downloads_manager()->items()); 807 EXPECT_CALL(
808 *observer(),
809 OnNewSuggestions(_, downloads_category(),
810 UnorderedElementsAre(HasUrl("http://download.com/1"))));
811 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
834 812
835 EXPECT_CALL(*observer(), 813 EXPECT_CALL(*observer(),
836 OnSuggestionInvalidated( 814 OnSuggestionInvalidated(
837 _, GetDummySuggestionId(1, /*is_offline_page=*/false))); 815 _, GetDummySuggestionId(1, /*is_offline_page=*/false)));
838 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true); 816 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true);
839 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); 817 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated();
840 } 818 }
841 819
842 TEST_F(DownloadSuggestionsProviderTest, 820 TEST_F(DownloadSuggestionsProviderTest,
843 ShouldNotShowOfflinePagesWhenTurnedOff) { 821 ShouldNotShowOfflinePagesWhenTurnedOff) {
844 IgnoreOnCategoryStatusChangedToAvailable(); 822 IgnoreOnCategoryStatusChangedToAvailable();
845 IgnoreOnSuggestionInvalidated(); 823 IgnoreOnSuggestionInvalidated();
846 824
847 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 825 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
848 EXPECT_CALL(*observer(), 826 EXPECT_CALL(*observer(),
849 OnNewSuggestions(_, downloads_category(), IsEmpty())); 827 OnNewSuggestions(_, downloads_category(), IsEmpty()));
850 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); 828 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
851 829
852 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = 830 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads =
853 CreateDummyAssetDownloads({1}); 831 CreateDummyAssetDownloads({1});
854 EXPECT_CALL( 832 EXPECT_CALL(
855 *observer(), 833 *observer(),
856 OnNewSuggestions(_, downloads_category(), 834 OnNewSuggestions(_, downloads_category(),
857 UnorderedElementsAre(HasUrl("http://download.com/1")))); 835 UnorderedElementsAre(HasUrl("http://download.com/1"))));
858 FireDownloadCreated(asset_downloads[0].get()); 836 FireDownloadCreated(asset_downloads[0].get());
859 // TODO(vitaliii): Notify the provider that an offline page has been updated. 837 // TODO(vitaliii): Notify the provider that an offline page has been updated.
860 } 838 }
(...skipping 17 matching lines...) Expand all
878 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); 856 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated();
879 } 857 }
880 858
881 TEST_F(DownloadSuggestionsProviderTest, ShouldLoadOfflinePagesOnModelLoaded) { 859 TEST_F(DownloadSuggestionsProviderTest, ShouldLoadOfflinePagesOnModelLoaded) {
882 IgnoreOnCategoryStatusChangedToAvailable(); 860 IgnoreOnCategoryStatusChangedToAvailable();
883 IgnoreOnSuggestionInvalidated(); 861 IgnoreOnSuggestionInvalidated();
884 862
885 offline_pages_model()->set_is_loaded(false); 863 offline_pages_model()->set_is_loaded(false);
886 EXPECT_CALL(*observer(), 864 EXPECT_CALL(*observer(),
887 OnNewSuggestions(_, downloads_category(), IsEmpty())); 865 OnNewSuggestions(_, downloads_category(), IsEmpty()));
888 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 866 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true);
889 867
890 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 868 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
891 offline_pages_model()->set_is_loaded(true); 869 offline_pages_model()->set_is_loaded(true);
892 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), 870 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(),
893 UnorderedElementsAre( 871 UnorderedElementsAre(
894 HasUrl("http://dummy.com/1"), 872 HasUrl("http://dummy.com/1"),
895 HasUrl("http://dummy.com/2")))); 873 HasUrl("http://dummy.com/2"))));
896 FireOfflinePageModelLoaded(); 874 FireOfflinePageModelLoaded();
897 } 875 }
898 876
899 TEST_F(DownloadSuggestionsProviderTest, 877 TEST_F(DownloadSuggestionsProviderTest,
900 ShouldLoadOfflinePagesIfMissesOnModelLoaded) { 878 ShouldLoadOfflinePagesIfMissesOnModelLoaded) {
901 IgnoreOnCategoryStatusChangedToAvailable(); 879 IgnoreOnCategoryStatusChangedToAvailable();
902 IgnoreOnSuggestionInvalidated(); 880 IgnoreOnSuggestionInvalidated();
903 881
904 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); 882 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2});
905 offline_pages_model()->set_is_loaded(true); 883 offline_pages_model()->set_is_loaded(true);
906 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), 884 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(),
907 UnorderedElementsAre( 885 UnorderedElementsAre(
908 HasUrl("http://dummy.com/1"), 886 HasUrl("http://dummy.com/1"),
909 HasUrl("http://dummy.com/2")))); 887 HasUrl("http://dummy.com/2"))));
910 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 888 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true);
911 } 889 }
912 890
913 TEST_F(DownloadSuggestionsProviderTest, 891 TEST_F(DownloadSuggestionsProviderTest,
914 ShouldLoadAndSubmitMissedAssetsEvenIfOfflinePagesAreTurnedOff) { 892 ShouldLoadAndSubmitMissedAssetsEvenIfOfflinePagesAreTurnedOff) {
915 IgnoreOnCategoryStatusChangedToAvailable(); 893 IgnoreOnCategoryStatusChangedToAvailable();
916 IgnoreOnSuggestionInvalidated(); 894 IgnoreOnSuggestionInvalidated();
917 895
918 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); 896 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2});
919 EXPECT_CALL( 897 EXPECT_CALL(
920 *observer(), 898 *observer(),
921 OnNewSuggestions(_, downloads_category(), 899 OnNewSuggestions(_, downloads_category(),
922 UnorderedElementsAre(HasUrl("http://download.com/1"), 900 UnorderedElementsAre(HasUrl("http://download.com/1"),
923 HasUrl("http://download.com/2")))); 901 HasUrl("http://download.com/2"))));
924 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); 902 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
925 }
926
927 TEST_F(DownloadSuggestionsProviderTest,
928 ShouldNotPruneDismissedSuggestionsOnStartup) {
929 IgnoreOnCategoryStatusChangedToAvailable();
930 IgnoreOnSuggestionInvalidated();
931
932 // We dismiss an item to store it in the list of dismissed items.
933 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
934 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
935 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
936 provider()->DismissSuggestion(
937 GetDummySuggestionId(1, /*is_offline_page=*/false));
938 DestroyProvider();
939
940 // We simulate current DownloadManager behaviour;
941 // The download manager has not started reading the list yet, so it is empty.
942 downloads_manager()->mutable_items()->clear();
943 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
944 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
945 Mock::VerifyAndClearExpectations(observer());
946
947 // The first download is being read.
948 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
949 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _))
950 .Times(0);
951 FireDownloadCreated(downloads_manager()->items()[0].get());
952 // The first download should not be reported, because it is dismissed.
953 } 903 }
954 904
955 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) { 905 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) {
956 IgnoreOnCategoryStatusChangedToAvailable(); 906 IgnoreOnCategoryStatusChangedToAvailable();
957 IgnoreOnSuggestionInvalidated(); 907 IgnoreOnSuggestionInvalidated();
958 908
959 // Dismiss items to store them in the list of dismissed items. 909 // Dismiss items to store them in the list of dismissed items.
960 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1}); 910 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1});
961 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); 911 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1});
962 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); 912 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
963 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 913 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
964 provider()->DismissSuggestion( 914 provider()->DismissSuggestion(
965 GetDummySuggestionId(1, /*is_offline_page=*/true)); 915 GetDummySuggestionId(1, /*is_offline_page=*/true));
966 provider()->DismissSuggestion( 916 provider()->DismissSuggestion(
967 GetDummySuggestionId(1, /*is_offline_page=*/false)); 917 GetDummySuggestionId(1, /*is_offline_page=*/false));
968 // Destroy and create provider to simulate turning off Chrome. 918 // Destroy and create provider to simulate turning off Chrome.
969 DestroyProvider(); 919 DestroyProvider();
970 920
971 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); 921 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
972 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); 922 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true);
973 EXPECT_THAT(GetDismissedSuggestions(), 923 EXPECT_THAT(GetDismissedSuggestions(),
974 UnorderedElementsAre(HasUrl("http://dummy.com/1"), 924 UnorderedElementsAre(HasUrl("http://dummy.com/1"),
975 HasUrl("http://download.com/1"))); 925 HasUrl("http://download.com/1")));
976 } 926 }
977
978 // TODO(vitaliii): Remove this test once the dismissed ids are pruned. See
979 // crbug.com/672758.
980 TEST_F(DownloadSuggestionsProviderTest, ShouldRemoveOldDismissedIdsIfTooMany) {
981 IgnoreOnCategoryStatusChangedToAvailable();
982 IgnoreOnSuggestionInvalidated();
983
984 const int kMaxDismissedIdCount =
985 DownloadSuggestionsProvider::GetMaxDismissedCountForTesting();
986 std::vector<int> ids;
987 for (int i = 0; i < kMaxDismissedIdCount + 1; ++i) {
988 ids.push_back(i);
989 }
990
991 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads(ids);
992 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _));
993 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
994
995 for (int i = 0; i < static_cast<int>(ids.size()); ++i) {
996 provider()->DismissSuggestion(
997 GetDummySuggestionId(i, /*is_offline_page=*/false));
998 }
999
1000 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(kMaxDismissedIdCount));
1001 DestroyProvider();
1002 // The oldest dismissed suggestion must become undismissed now. This is a
1003 // temporary workaround and not what we want in long term. This test must be
1004 // removed once we start pruning dismissed asset downloads on startup.
1005 EXPECT_CALL(*observer(),
1006 OnNewSuggestions(_, downloads_category(),
1007 ElementsAre(HasUrl("http://download.com/0"))));
1008 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false);
1009 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698