| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" | 5 #include "chrome/browser/ntp_snippets/download_suggestions_provider.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "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 "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using content::DownloadItem; | 24 using content::DownloadItem; |
| 24 using content::MockDownloadManager; | 25 using content::MockDownloadManager; |
| 25 using ntp_snippets::Category; | 26 using ntp_snippets::Category; |
| 27 using ntp_snippets::CategoryStatus; |
| 26 using ntp_snippets::ContentSuggestion; | 28 using ntp_snippets::ContentSuggestion; |
| 27 using ntp_snippets::ContentSuggestionsProvider; | 29 using ntp_snippets::ContentSuggestionsProvider; |
| 28 using ntp_snippets::MockContentSuggestionsProviderObserver; | 30 using ntp_snippets::MockContentSuggestionsProviderObserver; |
| 29 using ntp_snippets::test::CaptureDismissedSuggestions; | 31 using ntp_snippets::test::CaptureDismissedSuggestions; |
| 30 using ntp_snippets::test::FakeOfflinePageModel; | 32 using ntp_snippets::test::FakeOfflinePageModel; |
| 31 using ntp_snippets::CategoryStatus; | |
| 32 using offline_pages::ClientId; | 33 using offline_pages::ClientId; |
| 33 using offline_pages::OfflinePageItem; | 34 using offline_pages::OfflinePageItem; |
| 34 using test::FakeDownloadItem; | 35 using test::FakeDownloadItem; |
| 35 using testing::_; | 36 using testing::_; |
| 36 using testing::AllOf; | 37 using testing::AllOf; |
| 37 using testing::AnyNumber; | 38 using testing::AnyNumber; |
| 38 using testing::ElementsAre; | 39 using testing::ElementsAre; |
| 39 using testing::IsEmpty; | 40 using testing::IsEmpty; |
| 40 using testing::Lt; | 41 using testing::Lt; |
| 41 using testing::Mock; | 42 using testing::Mock; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 for (const auto& item : items_) { | 221 for (const auto& item : items_) { |
| 221 all_downloads->push_back(item.get()); | 222 all_downloads->push_back(item.get()); |
| 222 } | 223 } |
| 223 } | 224 } |
| 224 | 225 |
| 225 private: | 226 private: |
| 226 base::ObserverList<Observer> observers_; | 227 base::ObserverList<Observer> observers_; |
| 227 std::vector<std::unique_ptr<FakeDownloadItem>> items_; | 228 std::vector<std::unique_ptr<FakeDownloadItem>> items_; |
| 228 }; | 229 }; |
| 229 | 230 |
| 231 class DummyHistoryAdapter : public DownloadHistory::HistoryAdapter { |
| 232 public: |
| 233 DummyHistoryAdapter() : HistoryAdapter(nullptr) {} |
| 234 void QueryDownloads( |
| 235 const history::HistoryService::DownloadQueryCallback& callback) override { |
| 236 } |
| 237 void CreateDownload(const history::DownloadRow& info, |
| 238 const history::HistoryService::DownloadCreateCallback& |
| 239 callback) override {} |
| 240 void UpdateDownload(const history::DownloadRow& data, |
| 241 bool should_commit_immediately) override {} |
| 242 void RemoveDownloads(const std::set<uint32_t>& ids) override {} |
| 243 }; |
| 244 |
| 230 } // namespace | 245 } // namespace |
| 231 | 246 |
| 232 class DownloadSuggestionsProviderTest : public testing::Test { | 247 class DownloadSuggestionsProviderTest : public testing::Test { |
| 233 public: | 248 public: |
| 234 DownloadSuggestionsProviderTest() | 249 DownloadSuggestionsProviderTest() |
| 235 : pref_service_(new TestingPrefServiceSimple()) { | 250 : download_history_(&downloads_manager_for_history_, |
| 251 base::MakeUnique<DummyHistoryAdapter>()), |
| 252 pref_service_(new TestingPrefServiceSimple()) { |
| 236 DownloadSuggestionsProvider::RegisterProfilePrefs( | 253 DownloadSuggestionsProvider::RegisterProfilePrefs( |
| 237 pref_service()->registry()); | 254 pref_service()->registry()); |
| 238 } | 255 } |
| 239 | 256 |
| 240 void IgnoreOnCategoryStatusChangedToAvailable() { | 257 void IgnoreOnCategoryStatusChangedToAvailable() { |
| 241 EXPECT_CALL(observer_, OnCategoryStatusChanged(_, downloads_category(), | 258 EXPECT_CALL(observer_, OnCategoryStatusChanged(_, downloads_category(), |
| 242 CategoryStatus::AVAILABLE)) | 259 CategoryStatus::AVAILABLE)) |
| 243 .Times(AnyNumber()); | 260 .Times(AnyNumber()); |
| 244 EXPECT_CALL(observer_, | 261 EXPECT_CALL(observer_, |
| 245 OnCategoryStatusChanged(_, downloads_category(), | 262 OnCategoryStatusChanged(_, downloads_category(), |
| 246 CategoryStatus::AVAILABLE_LOADING)) | 263 CategoryStatus::AVAILABLE_LOADING)) |
| 247 .Times(AnyNumber()); | 264 .Times(AnyNumber()); |
| 248 } | 265 } |
| 249 | 266 |
| 250 void IgnoreOnSuggestionInvalidated() { | 267 void IgnoreOnSuggestionInvalidated() { |
| 251 EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber()); | 268 EXPECT_CALL(observer_, OnSuggestionInvalidated(_, _)).Times(AnyNumber()); |
| 252 } | 269 } |
| 253 | 270 |
| 271 DownloadSuggestionsProvider* CreateLoadedProvider(bool show_assets, |
| 272 bool show_offline_pages) { |
| 273 CreateProvider(show_assets, show_offline_pages); |
| 274 FireHistoryQueryComplete(); |
| 275 return provider_.get(); |
| 276 } |
| 277 |
| 254 DownloadSuggestionsProvider* CreateProvider(bool show_assets, | 278 DownloadSuggestionsProvider* CreateProvider(bool show_assets, |
| 255 bool show_offline_pages) { | 279 bool show_offline_pages) { |
| 256 DCHECK(!provider_); | 280 DCHECK(!provider_); |
| 257 DCHECK(show_assets || show_offline_pages); | 281 DCHECK(show_assets || show_offline_pages); |
| 282 |
| 283 // TODO(crbug.com/681766): Extract DownloadHistory interface and move |
| 284 // implementation into DownloadHistoryImpl. Then mock it. |
| 258 provider_ = base::MakeUnique<DownloadSuggestionsProvider>( | 285 provider_ = base::MakeUnique<DownloadSuggestionsProvider>( |
| 259 &observer_, show_offline_pages ? &offline_pages_model_ : nullptr, | 286 &observer_, show_offline_pages ? &offline_pages_model_ : nullptr, |
| 260 show_assets ? &downloads_manager_ : nullptr, pref_service(), | 287 show_assets ? &downloads_manager_ : nullptr, &download_history_, |
| 288 pref_service(), |
| 261 /*download_manager_ui_enabled=*/false); | 289 /*download_manager_ui_enabled=*/false); |
| 262 return provider_.get(); | 290 return provider_.get(); |
| 263 } | 291 } |
| 264 | 292 |
| 265 void DestroyProvider() { provider_.reset(); } | 293 void DestroyProvider() { provider_.reset(); } |
| 266 | 294 |
| 267 Category downloads_category() { | 295 Category downloads_category() { |
| 268 return Category::FromKnownCategory( | 296 return Category::FromKnownCategory( |
| 269 ntp_snippets::KnownCategories::DOWNLOADS); | 297 ntp_snippets::KnownCategories::DOWNLOADS); |
| 270 } | 298 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 290 downloads_manager_.NotifyDownloadCreated(item); | 318 downloads_manager_.NotifyDownloadCreated(item); |
| 291 } | 319 } |
| 292 | 320 |
| 293 void FireDownloadsCreated( | 321 void FireDownloadsCreated( |
| 294 const std::vector<std::unique_ptr<FakeDownloadItem>>& items) { | 322 const std::vector<std::unique_ptr<FakeDownloadItem>>& items) { |
| 295 for (const auto& item : items) { | 323 for (const auto& item : items) { |
| 296 FireDownloadCreated(item.get()); | 324 FireDownloadCreated(item.get()); |
| 297 } | 325 } |
| 298 } | 326 } |
| 299 | 327 |
| 328 void FireHistoryQueryComplete() { provider_->OnHistoryQueryComplete(); } |
| 329 |
| 300 ContentSuggestion::ID GetDummySuggestionId(int id, bool is_offline_page) { | 330 ContentSuggestion::ID GetDummySuggestionId(int id, bool is_offline_page) { |
| 301 return ContentSuggestion::ID( | 331 return ContentSuggestion::ID( |
| 302 downloads_category(), | 332 downloads_category(), |
| 303 (is_offline_page ? "O" : "D") + base::IntToString(id)); | 333 (is_offline_page ? "O" : "D") + base::IntToString(id)); |
| 304 } | 334 } |
| 305 | 335 |
| 306 std::vector<ContentSuggestion> GetDismissedSuggestions() { | 336 std::vector<ContentSuggestion> GetDismissedSuggestions() { |
| 307 std::vector<ContentSuggestion> dismissed_suggestions; | 337 std::vector<ContentSuggestion> dismissed_suggestions; |
| 308 // This works synchronously because both fake data sources were designed so. | 338 // This works synchronously because both fake data sources were designed so. |
| 309 provider()->GetDismissedSuggestionsForDebugging( | 339 provider()->GetDismissedSuggestionsForDebugging( |
| 310 downloads_category(), | 340 downloads_category(), |
| 311 base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions)); | 341 base::Bind(&CaptureDismissedSuggestions, &dismissed_suggestions)); |
| 312 return dismissed_suggestions; | 342 return dismissed_suggestions; |
| 313 } | 343 } |
| 314 | 344 |
| 315 ContentSuggestionsProvider* provider() { | 345 ContentSuggestionsProvider* provider() { |
| 316 DCHECK(provider_); | 346 DCHECK(provider_); |
| 317 return provider_.get(); | 347 return provider_.get(); |
| 318 } | 348 } |
| 319 ObservedMockDownloadManager* downloads_manager() { | 349 ObservedMockDownloadManager* downloads_manager() { |
| 320 return &downloads_manager_; | 350 return &downloads_manager_; |
| 321 } | 351 } |
| 322 FakeOfflinePageModel* offline_pages_model() { return &offline_pages_model_; } | 352 FakeOfflinePageModel* offline_pages_model() { return &offline_pages_model_; } |
| 323 MockContentSuggestionsProviderObserver* observer() { return &observer_; } | 353 MockContentSuggestionsProviderObserver* observer() { return &observer_; } |
| 324 TestingPrefServiceSimple* pref_service() { return pref_service_.get(); } | 354 TestingPrefServiceSimple* pref_service() { return pref_service_.get(); } |
| 325 | 355 |
| 326 private: | 356 private: |
| 357 // DownloadHistory requires UI thread. |
| 358 content::TestBrowserThreadBundle thread_bundle_; |
| 359 |
| 360 // We do not use DownloadHistory functionality in the tests, so we provide an |
| 361 // empty manager to ensure no notifications, so that it does not intervene. |
| 362 ObservedMockDownloadManager downloads_manager_for_history_; |
| 363 DownloadHistory download_history_; |
| 327 ObservedMockDownloadManager downloads_manager_; | 364 ObservedMockDownloadManager downloads_manager_; |
| 328 FakeOfflinePageModel offline_pages_model_; | 365 FakeOfflinePageModel offline_pages_model_; |
| 329 StrictMock<MockContentSuggestionsProviderObserver> observer_; | 366 StrictMock<MockContentSuggestionsProviderObserver> observer_; |
| 330 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 367 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 331 // Last so that the dependencies are deleted after the provider. | 368 // Last so that the dependencies are deleted after the provider. |
| 332 std::unique_ptr<DownloadSuggestionsProvider> provider_; | 369 std::unique_ptr<DownloadSuggestionsProvider> provider_; |
| 333 | 370 |
| 334 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProviderTest); | 371 DISALLOW_COPY_AND_ASSIGN(DownloadSuggestionsProviderTest); |
| 335 }; | 372 }; |
| 336 | 373 |
| 337 TEST_F(DownloadSuggestionsProviderTest, | 374 TEST_F(DownloadSuggestionsProviderTest, |
| 338 ShouldConvertOfflinePagesToSuggestions) { | 375 ShouldConvertOfflinePagesToSuggestions) { |
| 339 IgnoreOnCategoryStatusChangedToAvailable(); | 376 IgnoreOnCategoryStatusChangedToAvailable(); |
| 340 | 377 |
| 341 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 378 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 342 EXPECT_CALL(*observer(), | 379 EXPECT_CALL(*observer(), |
| 343 OnNewSuggestions( | 380 OnNewSuggestions( |
| 344 _, downloads_category(), | 381 _, downloads_category(), |
| 345 UnorderedElementsAre(AllOf(HasUrl("http://dummy.com/1"), | 382 UnorderedElementsAre(AllOf(HasUrl("http://dummy.com/1"), |
| 346 HasDownloadSuggestionExtra( | 383 HasDownloadSuggestionExtra( |
| 347 /*is_download_asset=*/false, | 384 /*is_download_asset=*/false, |
| 348 FILE_PATH_LITERAL(""), "")), | 385 FILE_PATH_LITERAL(""), "")), |
| 349 AllOf(HasUrl("http://dummy.com/2"), | 386 AllOf(HasUrl("http://dummy.com/2"), |
| 350 HasDownloadSuggestionExtra( | 387 HasDownloadSuggestionExtra( |
| 351 /*is_download_asset=*/false, | 388 /*is_download_asset=*/false, |
| 352 FILE_PATH_LITERAL(""), ""))))); | 389 FILE_PATH_LITERAL(""), ""))))); |
| 353 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 390 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 354 } | 391 } |
| 355 | 392 |
| 356 TEST_F(DownloadSuggestionsProviderTest, | 393 TEST_F(DownloadSuggestionsProviderTest, |
| 357 ShouldConvertDownloadItemsToSuggestions) { | 394 ShouldConvertDownloadItemsToSuggestions) { |
| 358 IgnoreOnCategoryStatusChangedToAvailable(); | 395 IgnoreOnCategoryStatusChangedToAvailable(); |
| 359 IgnoreOnSuggestionInvalidated(); | 396 IgnoreOnSuggestionInvalidated(); |
| 360 | 397 |
| 361 EXPECT_CALL(*observer(), | 398 EXPECT_CALL(*observer(), |
| 362 OnNewSuggestions(_, downloads_category(), SizeIs(0))); | 399 OnNewSuggestions(_, downloads_category(), SizeIs(0))); |
| 363 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 400 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 364 | 401 |
| 365 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = | 402 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = |
| 366 CreateDummyAssetDownloads({1, 2}); | 403 CreateDummyAssetDownloads({1, 2}); |
| 367 | 404 |
| 368 EXPECT_CALL(*observer(), | 405 EXPECT_CALL(*observer(), |
| 369 OnNewSuggestions(_, downloads_category(), | 406 OnNewSuggestions(_, downloads_category(), |
| 370 UnorderedElementsAre(AllOf( | 407 UnorderedElementsAre(AllOf( |
| 371 HasUrl("http://download.com/1"), | 408 HasUrl("http://download.com/1"), |
| 372 HasDownloadSuggestionExtra( | 409 HasDownloadSuggestionExtra( |
| 373 /*is_download_asset=*/true, | 410 /*is_download_asset=*/true, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 394 | 431 |
| 395 TEST_F(DownloadSuggestionsProviderTest, ShouldMixInBothSources) { | 432 TEST_F(DownloadSuggestionsProviderTest, ShouldMixInBothSources) { |
| 396 IgnoreOnCategoryStatusChangedToAvailable(); | 433 IgnoreOnCategoryStatusChangedToAvailable(); |
| 397 IgnoreOnSuggestionInvalidated(); | 434 IgnoreOnSuggestionInvalidated(); |
| 398 | 435 |
| 399 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 436 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 400 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 437 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 401 UnorderedElementsAre( | 438 UnorderedElementsAre( |
| 402 HasUrl("http://dummy.com/1"), | 439 HasUrl("http://dummy.com/1"), |
| 403 HasUrl("http://dummy.com/2")))); | 440 HasUrl("http://dummy.com/2")))); |
| 404 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 441 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 405 | 442 |
| 406 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = | 443 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = |
| 407 CreateDummyAssetDownloads({1, 2}); | 444 CreateDummyAssetDownloads({1, 2}); |
| 408 | 445 |
| 409 EXPECT_CALL( | 446 EXPECT_CALL( |
| 410 *observer(), | 447 *observer(), |
| 411 OnNewSuggestions(_, downloads_category(), | 448 OnNewSuggestions(_, downloads_category(), |
| 412 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 449 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 413 HasUrl("http://dummy.com/2"), | 450 HasUrl("http://dummy.com/2"), |
| 414 HasUrl("http://download.com/1")))); | 451 HasUrl("http://download.com/1")))); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 435 | 472 |
| 436 (*offline_pages_model()->mutable_items()) | 473 (*offline_pages_model()->mutable_items()) |
| 437 .push_back(CreateDummyOfflinePage(1, yesterday)); | 474 .push_back(CreateDummyOfflinePage(1, yesterday)); |
| 438 (*offline_pages_model()->mutable_items()) | 475 (*offline_pages_model()->mutable_items()) |
| 439 .push_back(CreateDummyOfflinePage(2, tomorrow)); | 476 .push_back(CreateDummyOfflinePage(2, tomorrow)); |
| 440 | 477 |
| 441 EXPECT_CALL(*observer(), | 478 EXPECT_CALL(*observer(), |
| 442 OnNewSuggestions(_, downloads_category(), | 479 OnNewSuggestions(_, downloads_category(), |
| 443 ElementsAre(HasUrl("http://dummy.com/2"), | 480 ElementsAre(HasUrl("http://dummy.com/2"), |
| 444 HasUrl("http://dummy.com/1")))); | 481 HasUrl("http://dummy.com/1")))); |
| 445 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 482 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 446 | 483 |
| 447 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads; | 484 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads; |
| 448 asset_downloads.push_back(CreateDummyAssetDownload(3, next_week)); | 485 asset_downloads.push_back(CreateDummyAssetDownload(3, next_week)); |
| 449 asset_downloads.push_back(CreateDummyAssetDownload(4, now)); | 486 asset_downloads.push_back(CreateDummyAssetDownload(4, now)); |
| 450 | 487 |
| 451 EXPECT_CALL(*observer(), | 488 EXPECT_CALL(*observer(), |
| 452 OnNewSuggestions(_, downloads_category(), | 489 OnNewSuggestions(_, downloads_category(), |
| 453 ElementsAre(HasUrl("http://download.com/3"), | 490 ElementsAre(HasUrl("http://download.com/3"), |
| 454 HasUrl("http://dummy.com/2"), | 491 HasUrl("http://dummy.com/2"), |
| 455 HasUrl("http://dummy.com/1")))); | 492 HasUrl("http://dummy.com/1")))); |
| 456 FireDownloadCreated(asset_downloads[0].get()); | 493 FireDownloadCreated(asset_downloads[0].get()); |
| 457 | 494 |
| 458 EXPECT_CALL(*observer(), | 495 EXPECT_CALL(*observer(), |
| 459 OnNewSuggestions(_, downloads_category(), | 496 OnNewSuggestions(_, downloads_category(), |
| 460 ElementsAre(HasUrl("http://download.com/3"), | 497 ElementsAre(HasUrl("http://download.com/3"), |
| 461 HasUrl("http://dummy.com/2"), | 498 HasUrl("http://dummy.com/2"), |
| 462 HasUrl("http://download.com/4"), | 499 HasUrl("http://download.com/4"), |
| 463 HasUrl("http://dummy.com/1")))); | 500 HasUrl("http://dummy.com/1")))); |
| 464 FireDownloadCreated(asset_downloads[1].get()); | 501 FireDownloadCreated(asset_downloads[1].get()); |
| 465 } | 502 } |
| 466 | 503 |
| 467 TEST_F(DownloadSuggestionsProviderTest, | 504 TEST_F(DownloadSuggestionsProviderTest, |
| 468 ShouldDismissWithoutNotifyingObservers) { | 505 ShouldDismissWithoutNotifyingObservers) { |
| 469 IgnoreOnCategoryStatusChangedToAvailable(); | 506 IgnoreOnCategoryStatusChangedToAvailable(); |
| 470 | 507 |
| 471 EXPECT_CALL(*observer(), | 508 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 472 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) | 509 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 473 .Times(2); | |
| 474 EXPECT_CALL( | 510 EXPECT_CALL( |
| 475 *observer(), | 511 *observer(), |
| 476 OnNewSuggestions(_, downloads_category(), | 512 OnNewSuggestions(_, downloads_category(), |
| 477 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 513 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 478 HasUrl("http://dummy.com/2"), | 514 HasUrl("http://dummy.com/2"), |
| 479 HasUrl("http://download.com/1"), | 515 HasUrl("http://download.com/1"), |
| 480 HasUrl("http://download.com/2")))); | 516 HasUrl("http://download.com/2")))); |
| 481 | 517 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 | 518 |
| 487 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); | 519 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); |
| 488 EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, _)).Times(0); | 520 EXPECT_CALL(*observer(), OnSuggestionInvalidated(_, _)).Times(0); |
| 489 provider()->DismissSuggestion( | 521 provider()->DismissSuggestion( |
| 490 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 522 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 491 provider()->DismissSuggestion( | 523 provider()->DismissSuggestion( |
| 492 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 524 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 493 | 525 |
| 494 // |downloads_manager_| is destroyed after the |provider_|, so the provider | 526 // |downloads_manager_| is destroyed after the |provider_|, so the provider |
| 495 // will not observe download items being destroyed. | 527 // will not observe download items being destroyed. |
| 496 } | 528 } |
| 497 | 529 |
| 498 TEST_F(DownloadSuggestionsProviderTest, | 530 TEST_F(DownloadSuggestionsProviderTest, |
| 499 ShouldNotReportDismissedSuggestionsOnNewData) { | 531 ShouldNotReportDismissedSuggestionsOnNewData) { |
| 500 IgnoreOnCategoryStatusChangedToAvailable(); | 532 IgnoreOnCategoryStatusChangedToAvailable(); |
| 501 | 533 |
| 502 EXPECT_CALL(*observer(), | 534 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 503 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) | 535 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 504 .Times(2); | |
| 505 EXPECT_CALL( | 536 EXPECT_CALL( |
| 506 *observer(), | 537 *observer(), |
| 507 OnNewSuggestions(_, downloads_category(), | 538 OnNewSuggestions(_, downloads_category(), |
| 508 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 539 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 509 HasUrl("http://dummy.com/2"), | 540 HasUrl("http://dummy.com/2"), |
| 510 HasUrl("http://download.com/1"), | 541 HasUrl("http://download.com/1"), |
| 511 HasUrl("http://download.com/2")))); | 542 HasUrl("http://download.com/2")))); |
| 512 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 543 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 | 544 |
| 517 provider()->DismissSuggestion( | 545 provider()->DismissSuggestion( |
| 518 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 546 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 519 provider()->DismissSuggestion( | 547 provider()->DismissSuggestion( |
| 520 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 548 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 521 | 549 |
| 522 EXPECT_CALL( | 550 EXPECT_CALL( |
| 523 *observer(), | 551 *observer(), |
| 524 OnNewSuggestions(_, downloads_category(), | 552 OnNewSuggestions(_, downloads_category(), |
| 525 UnorderedElementsAre(HasUrl("http://dummy.com/2"), | 553 UnorderedElementsAre(HasUrl("http://dummy.com/2"), |
| 526 HasUrl("http://dummy.com/3"), | 554 HasUrl("http://dummy.com/3"), |
| 527 HasUrl("http://download.com/2")))); | 555 HasUrl("http://download.com/2")))); |
| 528 AddOfflinePage(CreateDummyOfflinePage(3)); | 556 AddOfflinePage(CreateDummyOfflinePage(3)); |
| 529 } | 557 } |
| 530 | 558 |
| 531 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { | 559 TEST_F(DownloadSuggestionsProviderTest, ShouldReturnDismissedSuggestions) { |
| 532 IgnoreOnCategoryStatusChangedToAvailable(); | 560 IgnoreOnCategoryStatusChangedToAvailable(); |
| 533 | 561 |
| 534 EXPECT_CALL(*observer(), | 562 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 535 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) | 563 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 536 .Times(2); | |
| 537 EXPECT_CALL( | 564 EXPECT_CALL( |
| 538 *observer(), | 565 *observer(), |
| 539 OnNewSuggestions(_, downloads_category(), | 566 OnNewSuggestions(_, downloads_category(), |
| 540 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 567 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 541 HasUrl("http://dummy.com/2"), | 568 HasUrl("http://dummy.com/2"), |
| 542 HasUrl("http://download.com/1"), | 569 HasUrl("http://download.com/1"), |
| 543 HasUrl("http://download.com/2")))); | 570 HasUrl("http://download.com/2")))); |
| 544 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 571 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 | 572 |
| 549 provider()->DismissSuggestion( | 573 provider()->DismissSuggestion( |
| 550 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 574 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 551 provider()->DismissSuggestion( | 575 provider()->DismissSuggestion( |
| 552 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 576 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 553 | 577 |
| 554 EXPECT_THAT(GetDismissedSuggestions(), | 578 EXPECT_THAT(GetDismissedSuggestions(), |
| 555 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 579 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 556 HasUrl("http://download.com/1"))); | 580 HasUrl("http://download.com/1"))); |
| 557 } | 581 } |
| 558 | 582 |
| 559 TEST_F(DownloadSuggestionsProviderTest, ShouldClearDismissedSuggestions) { | 583 TEST_F(DownloadSuggestionsProviderTest, ShouldClearDismissedSuggestions) { |
| 560 IgnoreOnCategoryStatusChangedToAvailable(); | 584 IgnoreOnCategoryStatusChangedToAvailable(); |
| 561 | 585 |
| 562 EXPECT_CALL(*observer(), | 586 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 563 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) | 587 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 564 .Times(2); | |
| 565 EXPECT_CALL( | 588 EXPECT_CALL( |
| 566 *observer(), | 589 *observer(), |
| 567 OnNewSuggestions(_, downloads_category(), | 590 OnNewSuggestions(_, downloads_category(), |
| 568 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 591 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 569 HasUrl("http://dummy.com/2"), | 592 HasUrl("http://dummy.com/2"), |
| 570 HasUrl("http://download.com/1"), | 593 HasUrl("http://download.com/1"), |
| 571 HasUrl("http://download.com/2")))); | 594 HasUrl("http://download.com/2")))); |
| 572 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 595 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 | 596 |
| 577 provider()->DismissSuggestion( | 597 provider()->DismissSuggestion( |
| 578 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 598 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 579 provider()->DismissSuggestion( | 599 provider()->DismissSuggestion( |
| 580 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 600 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 581 | 601 |
| 582 EXPECT_CALL( | 602 EXPECT_CALL( |
| 583 *observer(), | 603 *observer(), |
| 584 OnNewSuggestions(_, downloads_category(), | 604 OnNewSuggestions(_, downloads_category(), |
| 585 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 605 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 586 HasUrl("http://dummy.com/2"), | 606 HasUrl("http://dummy.com/2"), |
| 587 HasUrl("http://download.com/1"), | 607 HasUrl("http://download.com/1"), |
| 588 HasUrl("http://download.com/2")))); | 608 HasUrl("http://download.com/2")))); |
| 589 provider()->ClearDismissedSuggestionsForDebugging(downloads_category()); | 609 provider()->ClearDismissedSuggestionsForDebugging(downloads_category()); |
| 590 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); | 610 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); |
| 591 } | 611 } |
| 592 | 612 |
| 593 TEST_F(DownloadSuggestionsProviderTest, | 613 TEST_F(DownloadSuggestionsProviderTest, |
| 594 ShouldNotDismissOtherTypeWithTheSameID) { | 614 ShouldNotDismissOtherTypeWithTheSameID) { |
| 595 IgnoreOnCategoryStatusChangedToAvailable(); | 615 IgnoreOnCategoryStatusChangedToAvailable(); |
| 596 | 616 |
| 597 EXPECT_CALL(*observer(), | 617 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 598 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(4ul)))) | 618 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 599 .Times(2); | |
| 600 EXPECT_CALL( | 619 EXPECT_CALL( |
| 601 *observer(), | 620 *observer(), |
| 602 OnNewSuggestions(_, downloads_category(), | 621 OnNewSuggestions(_, downloads_category(), |
| 603 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 622 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 604 HasUrl("http://dummy.com/2"), | 623 HasUrl("http://dummy.com/2"), |
| 605 HasUrl("http://download.com/1"), | 624 HasUrl("http://download.com/1"), |
| 606 HasUrl("http://download.com/2")))); | 625 HasUrl("http://download.com/2")))); |
| 607 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 626 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 | 627 |
| 612 provider()->DismissSuggestion( | 628 provider()->DismissSuggestion( |
| 613 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 629 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 614 | 630 |
| 615 EXPECT_CALL( | 631 EXPECT_CALL( |
| 616 *observer(), | 632 *observer(), |
| 617 OnNewSuggestions(_, downloads_category(), | 633 OnNewSuggestions(_, downloads_category(), |
| 618 UnorderedElementsAre(HasUrl("http://dummy.com/2"), | 634 UnorderedElementsAre(HasUrl("http://dummy.com/2"), |
| 619 HasUrl("http://dummy.com/3"), | 635 HasUrl("http://dummy.com/3"), |
| 620 HasUrl("http://download.com/1"), | 636 HasUrl("http://download.com/1"), |
| 621 HasUrl("http://download.com/2")))); | 637 HasUrl("http://download.com/2")))); |
| 622 AddOfflinePage(CreateDummyOfflinePage(3)); | 638 AddOfflinePage(CreateDummyOfflinePage(3)); |
| 623 } | 639 } |
| 624 | 640 |
| 625 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { | 641 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceDismissedItemWithNewData) { |
| 626 IgnoreOnCategoryStatusChangedToAvailable(); | 642 IgnoreOnCategoryStatusChangedToAvailable(); |
| 627 | 643 |
| 628 EXPECT_CALL(*observer(), | 644 // Currently the provider stores five items in its internal cache, so six |
| 629 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(5ul)))) | 645 // items are needed to check whether all downloads are fetched on dismissal. |
| 630 .Times(5); | 646 *(downloads_manager()->mutable_items()) = |
| 647 CreateDummyAssetDownloads({1, 2, 3, 4, 5, 6}); |
| 631 EXPECT_CALL( | 648 EXPECT_CALL( |
| 632 *observer(), | 649 *observer(), |
| 633 OnNewSuggestions(_, downloads_category(), | 650 OnNewSuggestions(_, downloads_category(), |
| 634 UnorderedElementsAre(HasUrl("http://download.com/1"), | 651 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 635 HasUrl("http://download.com/2"), | 652 HasUrl("http://download.com/2"), |
| 636 HasUrl("http://download.com/3"), | 653 HasUrl("http://download.com/3"), |
| 637 HasUrl("http://download.com/4"), | 654 HasUrl("http://download.com/4"), |
| 638 HasUrl("http://download.com/5")))); | 655 HasUrl("http://download.com/5")))); |
| 639 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 656 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 | 657 |
| 646 provider()->DismissSuggestion( | 658 provider()->DismissSuggestion( |
| 647 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 659 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 648 provider()->DismissSuggestion( | 660 provider()->DismissSuggestion( |
| 649 GetDummySuggestionId(2, /*is_offline_page=*/false)); | 661 GetDummySuggestionId(2, /*is_offline_page=*/false)); |
| 650 | 662 |
| 651 // The provider is not notified about the 6th item, however, it must report | 663 // The provider is not notified about the 6th item, however, it must report |
| 652 // it now. | 664 // it now. |
| 653 EXPECT_CALL( | 665 EXPECT_CALL( |
| 654 *observer(), | 666 *observer(), |
| 655 OnNewSuggestions(_, downloads_category(), | 667 OnNewSuggestions(_, downloads_category(), |
| 656 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 668 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 657 HasUrl("http://download.com/3"), | 669 HasUrl("http://download.com/3"), |
| 658 HasUrl("http://download.com/4"), | 670 HasUrl("http://download.com/4"), |
| 659 HasUrl("http://download.com/5"), | 671 HasUrl("http://download.com/5"), |
| 660 HasUrl("http://download.com/6")))); | 672 HasUrl("http://download.com/6")))); |
| 661 AddOfflinePage(CreateDummyOfflinePage(1)); | 673 AddOfflinePage(CreateDummyOfflinePage(1)); |
| 662 } | 674 } |
| 663 | 675 |
| 664 TEST_F(DownloadSuggestionsProviderTest, | 676 TEST_F(DownloadSuggestionsProviderTest, |
| 665 ShouldInvalidateWhenUnderlyingItemDeleted) { | 677 ShouldInvalidateWhenUnderlyingItemDeleted) { |
| 666 IgnoreOnCategoryStatusChangedToAvailable(); | 678 IgnoreOnCategoryStatusChangedToAvailable(); |
| 667 | 679 |
| 668 EXPECT_CALL(*observer(), | 680 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 669 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(3ul)))); | 681 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 670 EXPECT_CALL( | 682 EXPECT_CALL( |
| 671 *observer(), | 683 *observer(), |
| 672 OnNewSuggestions(_, downloads_category(), | 684 OnNewSuggestions(_, downloads_category(), |
| 673 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 685 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 674 HasUrl("http://dummy.com/2"), | 686 HasUrl("http://dummy.com/2"), |
| 675 HasUrl("http://download.com/1")))); | 687 HasUrl("http://download.com/1")))); |
| 676 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 688 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 | 689 |
| 681 // We add another item manually, so that when it gets deleted it is not | 690 // We add another item manually, so that when it gets deleted it is not |
| 682 // present in DownloadsManager list. | 691 // present in DownloadsManager list. |
| 683 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload(2); | 692 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload(2); |
| 684 EXPECT_CALL( | 693 EXPECT_CALL( |
| 685 *observer(), | 694 *observer(), |
| 686 OnNewSuggestions(_, downloads_category(), | 695 OnNewSuggestions(_, downloads_category(), |
| 687 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 696 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 688 HasUrl("http://dummy.com/2"), | 697 HasUrl("http://dummy.com/2"), |
| 689 HasUrl("http://download.com/1"), | 698 HasUrl("http://download.com/1"), |
| 690 HasUrl("http://download.com/2")))); | 699 HasUrl("http://download.com/2")))); |
| 691 FireDownloadCreated(removed_item.get()); | 700 FireDownloadCreated(removed_item.get()); |
| 692 | 701 |
| 693 EXPECT_CALL(*observer(), | 702 EXPECT_CALL(*observer(), |
| 694 OnSuggestionInvalidated( | 703 OnSuggestionInvalidated( |
| 695 _, GetDummySuggestionId(1, /*is_offline_page=*/true))); | 704 _, GetDummySuggestionId(1, /*is_offline_page=*/true))); |
| 696 FireOfflinePageDeleted(offline_pages_model()->items()[0]); | 705 FireOfflinePageDeleted(offline_pages_model()->items()[0]); |
| 697 | 706 |
| 698 EXPECT_CALL(*observer(), | 707 EXPECT_CALL(*observer(), |
| 699 OnSuggestionInvalidated( | 708 OnSuggestionInvalidated( |
| 700 _, GetDummySuggestionId(2, /*is_offline_page=*/false))); | 709 _, GetDummySuggestionId(2, /*is_offline_page=*/false))); |
| 701 // |OnDownloadItemDestroyed| is called from |removed_item|'s destructor. | 710 // |OnDownloadItemDestroyed| is called from |removed_item|'s destructor. |
| 702 removed_item.reset(); | 711 removed_item.reset(); |
| 703 } | 712 } |
| 704 | 713 |
| 705 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceRemovedItemWithNewData) { | 714 TEST_F(DownloadSuggestionsProviderTest, ShouldReplaceRemovedItemWithNewData) { |
| 706 IgnoreOnCategoryStatusChangedToAvailable(); | 715 IgnoreOnCategoryStatusChangedToAvailable(); |
| 707 IgnoreOnSuggestionInvalidated(); | 716 IgnoreOnSuggestionInvalidated(); |
| 708 | 717 |
| 709 EXPECT_CALL(*observer(), | 718 *(downloads_manager()->mutable_items()) = |
| 710 OnNewSuggestions(_, downloads_category(), SizeIs(Lt(5ul)))) | 719 CreateDummyAssetDownloads({1, 2, 3, 4, 5}); |
| 711 .Times(5); | |
| 712 EXPECT_CALL( | 720 EXPECT_CALL( |
| 713 *observer(), | 721 *observer(), |
| 714 OnNewSuggestions(_, downloads_category(), | 722 OnNewSuggestions(_, downloads_category(), |
| 715 UnorderedElementsAre(HasUrl("http://download.com/1"), | 723 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 716 HasUrl("http://download.com/2"), | 724 HasUrl("http://download.com/2"), |
| 717 HasUrl("http://download.com/3"), | 725 HasUrl("http://download.com/3"), |
| 718 HasUrl("http://download.com/4"), | 726 HasUrl("http://download.com/4"), |
| 719 HasUrl("http://download.com/5")))); | 727 HasUrl("http://download.com/5")))); |
| 720 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 728 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 | 729 |
| 725 // Note that |CreateDummyAssetDownloads| creates items "downloaded" before | 730 // Note that |CreateDummyAssetDownloads| creates items "downloaded" before |
| 726 // |base::Time::Now()|, so for a new item the time is set in future to enforce | 731 // |base::Time::Now()|, so for a new item the time is set in future to enforce |
| 727 // the provider to show the new item. | 732 // the provider to show the new item. |
| 728 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload( | 733 std::unique_ptr<FakeDownloadItem> removed_item = CreateDummyAssetDownload( |
| 729 100, base::Time::Now() + base::TimeDelta::FromDays(1)); | 734 100, base::Time::Now() + base::TimeDelta::FromDays(1)); |
| 730 EXPECT_CALL( | 735 EXPECT_CALL( |
| 731 *observer(), | 736 *observer(), |
| 732 OnNewSuggestions( | 737 OnNewSuggestions( |
| 733 _, downloads_category(), | 738 _, downloads_category(), |
| (...skipping 23 matching lines...) Expand all Loading... |
| 757 IgnoreOnSuggestionInvalidated(); | 762 IgnoreOnSuggestionInvalidated(); |
| 758 | 763 |
| 759 auto offline_pages = CreateDummyOfflinePages({1, 2, 3}); | 764 auto offline_pages = CreateDummyOfflinePages({1, 2, 3}); |
| 760 | 765 |
| 761 *(offline_pages_model()->mutable_items()) = offline_pages; | 766 *(offline_pages_model()->mutable_items()) = offline_pages; |
| 762 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 767 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 763 UnorderedElementsAre( | 768 UnorderedElementsAre( |
| 764 HasUrl("http://dummy.com/1"), | 769 HasUrl("http://dummy.com/1"), |
| 765 HasUrl("http://dummy.com/2"), | 770 HasUrl("http://dummy.com/2"), |
| 766 HasUrl("http://dummy.com/3")))); | 771 HasUrl("http://dummy.com/3")))); |
| 767 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 772 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 768 | 773 |
| 769 provider()->DismissSuggestion( | 774 provider()->DismissSuggestion( |
| 770 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 775 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 771 provider()->DismissSuggestion( | 776 provider()->DismissSuggestion( |
| 772 GetDummySuggestionId(2, /*is_offline_page=*/true)); | 777 GetDummySuggestionId(2, /*is_offline_page=*/true)); |
| 773 provider()->DismissSuggestion( | 778 provider()->DismissSuggestion( |
| 774 GetDummySuggestionId(3, /*is_offline_page=*/true)); | 779 GetDummySuggestionId(3, /*is_offline_page=*/true)); |
| 775 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); | 780 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(3)); |
| 776 | 781 |
| 777 // Note that the first suggestion is not removed from |offline_pages_model| | 782 // Note that the first suggestion is not removed from |offline_pages_model| |
| 778 // storage, because otherwise |GetDismissedSuggestions| cannot return it. | 783 // storage, because otherwise |GetDismissedSuggestions| cannot return it. |
| 779 FireOfflinePageDeleted(offline_pages[0]); | 784 FireOfflinePageDeleted(offline_pages[0]); |
| 780 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); | 785 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); |
| 781 | 786 |
| 782 // Prune when offline page is deleted. | 787 // Prune when offline page is deleted. |
| 783 FireOfflinePageDeleted(offline_pages_model()->items()[1]); | 788 FireOfflinePageDeleted(offline_pages_model()->items()[1]); |
| 784 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); | 789 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); |
| 785 } | 790 } |
| 786 | 791 |
| 787 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { | 792 TEST_F(DownloadSuggestionsProviderTest, ShouldPruneAssetDownloadsDismissedIDs) { |
| 788 IgnoreOnCategoryStatusChangedToAvailable(); | 793 IgnoreOnCategoryStatusChangedToAvailable(); |
| 789 IgnoreOnSuggestionInvalidated(); | 794 IgnoreOnSuggestionInvalidated(); |
| 790 | 795 |
| 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}); | 796 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 796 FireDownloadsCreated(downloads_manager()->items()); | 797 EXPECT_CALL( |
| 798 *observer(), |
| 799 OnNewSuggestions(_, downloads_category(), |
| 800 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 801 HasUrl("http://download.com/2")))); |
| 802 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 797 | 803 |
| 798 provider()->DismissSuggestion( | 804 provider()->DismissSuggestion( |
| 799 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 805 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 800 provider()->DismissSuggestion( | 806 provider()->DismissSuggestion( |
| 801 GetDummySuggestionId(2, /*is_offline_page=*/false)); | 807 GetDummySuggestionId(2, /*is_offline_page=*/false)); |
| 802 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); | 808 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(2)); |
| 803 | 809 |
| 804 downloads_manager()->items()[0]->NotifyDownloadDestroyed(); | 810 downloads_manager()->items()[0]->NotifyDownloadDestroyed(); |
| 805 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); | 811 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(1)); |
| 806 } | 812 } |
| 807 | 813 |
| 808 TEST_F(DownloadSuggestionsProviderTest, | 814 TEST_F(DownloadSuggestionsProviderTest, |
| 809 ShouldFetchAssetDownloadsOnStartupButOnlyOnce) { | 815 ShouldFetchAssetDownloadsOnStartupButOnlyOnce) { |
| 810 IgnoreOnCategoryStatusChangedToAvailable(); | 816 IgnoreOnCategoryStatusChangedToAvailable(); |
| 811 | 817 |
| 812 // Downloads manager was created before the provider, so |OnDownloadCreated| | |
| 813 // calls "were" missed, but the provider must show missed items anyway. | |
| 814 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 818 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 815 EXPECT_CALL( | 819 EXPECT_CALL( |
| 816 *observer(), | 820 *observer(), |
| 817 OnNewSuggestions(_, downloads_category(), | 821 OnNewSuggestions(_, downloads_category(), |
| 818 UnorderedElementsAre(HasUrl("http://download.com/1"), | 822 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 819 HasUrl("http://download.com/2")))); | 823 HasUrl("http://download.com/2")))); |
| 824 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 825 } |
| 826 |
| 827 TEST_F(DownloadSuggestionsProviderTest, |
| 828 ShouldFetchAssetDownloadsOnHistoryQueryComplete) { |
| 829 IgnoreOnCategoryStatusChangedToAvailable(); |
| 830 |
| 831 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); |
| 820 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 832 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 833 |
| 834 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 835 EXPECT_CALL( |
| 836 *observer(), |
| 837 OnNewSuggestions(_, downloads_category(), |
| 838 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 839 HasUrl("http://download.com/2")))); |
| 840 FireHistoryQueryComplete(); |
| 821 } | 841 } |
| 822 | 842 |
| 823 TEST_F(DownloadSuggestionsProviderTest, | 843 TEST_F(DownloadSuggestionsProviderTest, |
| 824 ShouldInvalidateAssetDownloadWhenItsFileRemoved) { | 844 ShouldInvalidateAssetDownloadWhenItsFileRemoved) { |
| 825 IgnoreOnCategoryStatusChangedToAvailable(); | 845 IgnoreOnCategoryStatusChangedToAvailable(); |
| 826 | 846 |
| 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}); | 847 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 833 FireDownloadsCreated(downloads_manager()->items()); | 848 EXPECT_CALL( |
| 849 *observer(), |
| 850 OnNewSuggestions(_, downloads_category(), |
| 851 UnorderedElementsAre(HasUrl("http://download.com/1")))); |
| 852 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 834 | 853 |
| 835 EXPECT_CALL(*observer(), | 854 EXPECT_CALL(*observer(), |
| 836 OnSuggestionInvalidated( | 855 OnSuggestionInvalidated( |
| 837 _, GetDummySuggestionId(1, /*is_offline_page=*/false))); | 856 _, GetDummySuggestionId(1, /*is_offline_page=*/false))); |
| 838 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true); | 857 (*downloads_manager()->mutable_items())[0]->SetFileExternallyRemoved(true); |
| 839 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); | 858 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); |
| 840 } | 859 } |
| 841 | 860 |
| 842 TEST_F(DownloadSuggestionsProviderTest, | 861 TEST_F(DownloadSuggestionsProviderTest, |
| 843 ShouldNotShowOfflinePagesWhenTurnedOff) { | 862 ShouldNotShowOfflinePagesWhenTurnedOff) { |
| 844 IgnoreOnCategoryStatusChangedToAvailable(); | 863 IgnoreOnCategoryStatusChangedToAvailable(); |
| 845 IgnoreOnSuggestionInvalidated(); | 864 IgnoreOnSuggestionInvalidated(); |
| 846 | 865 |
| 847 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 866 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 848 EXPECT_CALL(*observer(), | 867 EXPECT_CALL(*observer(), |
| 849 OnNewSuggestions(_, downloads_category(), IsEmpty())); | 868 OnNewSuggestions(_, downloads_category(), IsEmpty())); |
| 850 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 869 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); |
| 851 | 870 |
| 852 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = | 871 std::vector<std::unique_ptr<FakeDownloadItem>> asset_downloads = |
| 853 CreateDummyAssetDownloads({1}); | 872 CreateDummyAssetDownloads({1}); |
| 854 EXPECT_CALL( | 873 EXPECT_CALL( |
| 855 *observer(), | 874 *observer(), |
| 856 OnNewSuggestions(_, downloads_category(), | 875 OnNewSuggestions(_, downloads_category(), |
| 857 UnorderedElementsAre(HasUrl("http://download.com/1")))); | 876 UnorderedElementsAre(HasUrl("http://download.com/1")))); |
| 858 FireDownloadCreated(asset_downloads[0].get()); | 877 FireDownloadCreated(asset_downloads[0].get()); |
| 859 // TODO(vitaliii): Notify the provider that an offline page has been updated. | 878 // TODO(vitaliii): Notify the provider that an offline page has been updated. |
| 860 } | 879 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 878 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); | 897 (*downloads_manager()->mutable_items())[0]->NotifyDownloadUpdated(); |
| 879 } | 898 } |
| 880 | 899 |
| 881 TEST_F(DownloadSuggestionsProviderTest, ShouldLoadOfflinePagesOnModelLoaded) { | 900 TEST_F(DownloadSuggestionsProviderTest, ShouldLoadOfflinePagesOnModelLoaded) { |
| 882 IgnoreOnCategoryStatusChangedToAvailable(); | 901 IgnoreOnCategoryStatusChangedToAvailable(); |
| 883 IgnoreOnSuggestionInvalidated(); | 902 IgnoreOnSuggestionInvalidated(); |
| 884 | 903 |
| 885 offline_pages_model()->set_is_loaded(false); | 904 offline_pages_model()->set_is_loaded(false); |
| 886 EXPECT_CALL(*observer(), | 905 EXPECT_CALL(*observer(), |
| 887 OnNewSuggestions(_, downloads_category(), IsEmpty())); | 906 OnNewSuggestions(_, downloads_category(), IsEmpty())); |
| 888 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 907 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); |
| 889 | 908 |
| 890 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 909 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 891 offline_pages_model()->set_is_loaded(true); | 910 offline_pages_model()->set_is_loaded(true); |
| 892 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 911 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 893 UnorderedElementsAre( | 912 UnorderedElementsAre( |
| 894 HasUrl("http://dummy.com/1"), | 913 HasUrl("http://dummy.com/1"), |
| 895 HasUrl("http://dummy.com/2")))); | 914 HasUrl("http://dummy.com/2")))); |
| 896 FireOfflinePageModelLoaded(); | 915 FireOfflinePageModelLoaded(); |
| 897 } | 916 } |
| 898 | 917 |
| 899 TEST_F(DownloadSuggestionsProviderTest, | 918 TEST_F(DownloadSuggestionsProviderTest, |
| 900 ShouldLoadOfflinePagesIfMissesOnModelLoaded) { | 919 ShouldLoadOfflinePagesIfMissesOnModelLoaded) { |
| 901 IgnoreOnCategoryStatusChangedToAvailable(); | 920 IgnoreOnCategoryStatusChangedToAvailable(); |
| 902 IgnoreOnSuggestionInvalidated(); | 921 IgnoreOnSuggestionInvalidated(); |
| 903 | 922 |
| 904 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); | 923 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 905 offline_pages_model()->set_is_loaded(true); | 924 offline_pages_model()->set_is_loaded(true); |
| 906 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), | 925 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 907 UnorderedElementsAre( | 926 UnorderedElementsAre( |
| 908 HasUrl("http://dummy.com/1"), | 927 HasUrl("http://dummy.com/1"), |
| 909 HasUrl("http://dummy.com/2")))); | 928 HasUrl("http://dummy.com/2")))); |
| 910 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 929 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); |
| 911 } | 930 } |
| 912 | 931 |
| 913 TEST_F(DownloadSuggestionsProviderTest, | 932 TEST_F(DownloadSuggestionsProviderTest, |
| 914 ShouldLoadAndSubmitMissedAssetsEvenIfOfflinePagesAreTurnedOff) { | 933 ShouldLoadAndSubmitMissedAssetsEvenIfOfflinePagesAreTurnedOff) { |
| 915 IgnoreOnCategoryStatusChangedToAvailable(); | 934 IgnoreOnCategoryStatusChangedToAvailable(); |
| 916 IgnoreOnSuggestionInvalidated(); | 935 IgnoreOnSuggestionInvalidated(); |
| 917 | 936 |
| 918 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); | 937 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1, 2}); |
| 919 EXPECT_CALL( | 938 EXPECT_CALL( |
| 920 *observer(), | 939 *observer(), |
| 921 OnNewSuggestions(_, downloads_category(), | 940 OnNewSuggestions(_, downloads_category(), |
| 922 UnorderedElementsAre(HasUrl("http://download.com/1"), | 941 UnorderedElementsAre(HasUrl("http://download.com/1"), |
| 923 HasUrl("http://download.com/2")))); | 942 HasUrl("http://download.com/2")))); |
| 924 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 943 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); |
| 925 } | 944 } |
| 926 | 945 |
| 927 TEST_F(DownloadSuggestionsProviderTest, | 946 TEST_F(DownloadSuggestionsProviderTest, |
| 928 ShouldNotPruneDismissedSuggestionsOnStartup) { | 947 ShouldLoadAndSubmitOfflinePagesEvenIfAssetDownloadsAreTurnedOff) { |
| 929 IgnoreOnCategoryStatusChangedToAvailable(); | 948 IgnoreOnCategoryStatusChangedToAvailable(); |
| 930 IgnoreOnSuggestionInvalidated(); | 949 IgnoreOnSuggestionInvalidated(); |
| 931 | 950 |
| 932 // We dismiss an item to store it in the list of dismissed items. | 951 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1, 2}); |
| 933 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 952 offline_pages_model()->set_is_loaded(true); |
| 934 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); | 953 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), |
| 935 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 954 UnorderedElementsAre( |
| 936 provider()->DismissSuggestion( | 955 HasUrl("http://dummy.com/1"), |
| 937 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 956 HasUrl("http://dummy.com/2")))); |
| 938 DestroyProvider(); | 957 CreateProvider(/*show_assets=*/false, /*show_offline_pages=*/true); |
| 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 } | 958 } |
| 954 | 959 |
| 955 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) { | 960 TEST_F(DownloadSuggestionsProviderTest, ShouldStoreDismissedSuggestions) { |
| 956 IgnoreOnCategoryStatusChangedToAvailable(); | 961 IgnoreOnCategoryStatusChangedToAvailable(); |
| 957 IgnoreOnSuggestionInvalidated(); | 962 IgnoreOnSuggestionInvalidated(); |
| 958 | 963 |
| 959 // Dismiss items to store them in the list of dismissed items. | 964 // Dismiss items to store them in the list of dismissed items. |
| 960 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1}); | 965 *(offline_pages_model()->mutable_items()) = CreateDummyOfflinePages({1}); |
| 961 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); | 966 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 962 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); | 967 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); |
| 963 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 968 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 964 provider()->DismissSuggestion( | 969 provider()->DismissSuggestion( |
| 965 GetDummySuggestionId(1, /*is_offline_page=*/true)); | 970 GetDummySuggestionId(1, /*is_offline_page=*/true)); |
| 966 provider()->DismissSuggestion( | 971 provider()->DismissSuggestion( |
| 967 GetDummySuggestionId(1, /*is_offline_page=*/false)); | 972 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 968 // Destroy and create provider to simulate turning off Chrome. | 973 // Destroy and create provider to simulate turning off Chrome. |
| 969 DestroyProvider(); | 974 DestroyProvider(); |
| 970 | 975 |
| 971 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); | 976 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); |
| 972 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/true); | 977 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/true); |
| 973 EXPECT_THAT(GetDismissedSuggestions(), | 978 EXPECT_THAT(GetDismissedSuggestions(), |
| 974 UnorderedElementsAre(HasUrl("http://dummy.com/1"), | 979 UnorderedElementsAre(HasUrl("http://dummy.com/1"), |
| 975 HasUrl("http://download.com/1"))); | 980 HasUrl("http://download.com/1"))); |
| 976 } | 981 } |
| 977 | 982 |
| 978 // TODO(vitaliii): Remove this test once the dismissed ids are pruned. See | 983 TEST_F(DownloadSuggestionsProviderTest, |
| 979 // crbug.com/672758. | 984 ShouldNotPruneDismissedAssetDownloadsBeforeHistoryQueryComplete) { |
| 980 TEST_F(DownloadSuggestionsProviderTest, ShouldRemoveOldDismissedIdsIfTooMany) { | |
| 981 IgnoreOnCategoryStatusChangedToAvailable(); | 985 IgnoreOnCategoryStatusChangedToAvailable(); |
| 982 IgnoreOnSuggestionInvalidated(); | 986 IgnoreOnSuggestionInvalidated(); |
| 983 | 987 |
| 984 const int kMaxDismissedIdCount = | 988 // Dismiss items to store them in the list of dismissed items. |
| 985 DownloadSuggestionsProvider::GetMaxDismissedCountForTesting(); | 989 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 986 std::vector<int> ids; | 990 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); |
| 987 for (int i = 0; i < kMaxDismissedIdCount + 1; ++i) { | 991 CreateLoadedProvider(/*show_assets=*/true, /*show_offline_pages=*/false); |
| 988 ids.push_back(i); | 992 provider()->DismissSuggestion( |
| 989 } | 993 GetDummySuggestionId(1, /*is_offline_page=*/false)); |
| 994 ASSERT_THAT(GetDismissedSuggestions(), |
| 995 UnorderedElementsAre(HasUrl("http://download.com/1"))); |
| 996 // Destroy and create provider to simulate turning off Chrome. |
| 997 DestroyProvider(); |
| 990 | 998 |
| 991 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads(ids); | 999 downloads_manager()->mutable_items()->clear(); |
| 992 EXPECT_CALL(*observer(), OnNewSuggestions(_, downloads_category(), _)); | 1000 |
| 1001 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)).Times(0); |
| 993 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | 1002 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); |
| 994 | 1003 |
| 995 for (int i = 0; i < static_cast<int>(ids.size()); ++i) { | 1004 // Dismissed IDs should not be pruned yet, because the downloads list at the |
| 996 provider()->DismissSuggestion( | 1005 // manager is not complete. |
| 997 GetDummySuggestionId(i, /*is_offline_page=*/false)); | 1006 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 998 } | 1007 EXPECT_THAT(GetDismissedSuggestions(), |
| 1008 UnorderedElementsAre(HasUrl("http://download.com/1"))); |
| 999 | 1009 |
| 1000 EXPECT_THAT(GetDismissedSuggestions(), SizeIs(kMaxDismissedIdCount)); | 1010 EXPECT_CALL(*observer(), OnNewSuggestions(_, _, _)); |
| 1001 DestroyProvider(); | 1011 |
| 1002 // The oldest dismissed suggestion must become undismissed now. This is a | 1012 downloads_manager()->mutable_items()->clear(); |
| 1003 // temporary workaround and not what we want in long term. This test must be | 1013 FireHistoryQueryComplete(); |
| 1004 // removed once we start pruning dismissed asset downloads on startup. | 1014 |
| 1005 EXPECT_CALL(*observer(), | 1015 *(downloads_manager()->mutable_items()) = CreateDummyAssetDownloads({1}); |
| 1006 OnNewSuggestions(_, downloads_category(), | 1016 // Once the manager has been loaded, the ids should be pruned. |
| 1007 ElementsAre(HasUrl("http://download.com/0")))); | 1017 EXPECT_THAT(GetDismissedSuggestions(), IsEmpty()); |
| 1008 CreateProvider(/*show_assets=*/true, /*show_offline_pages=*/false); | |
| 1009 } | 1018 } |
| OLD | NEW |