Chromium Code Reviews| Index: chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc |
| diff --git a/chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc b/chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc |
| index 471a417c07655195d3972d0de0c1a226571f6046..758a669ca0b747aee4aadc206cd8b5f20e7286cc 100644 |
| --- a/chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc |
| +++ b/chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc |
| @@ -8,11 +8,16 @@ |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "chrome/browser/background_fetch/background_fetch_client_factory.h" |
| #include "chrome/test/base/testing_profile.h" |
| +#include "components/offline_items_collection/core/offline_content_provider.h" |
| #include "components/offline_items_collection/core/offline_item.h" |
| #include "content/public/browser/background_fetch_client.h" |
| #include "content/public/test/test_browser_thread_bundle.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +using offline_items_collection::ContentId; |
| +using offline_items_collection::OfflineContentProvider; |
| +using offline_items_collection::OfflineItem; |
| + |
| namespace { |
| const char kRegistrationId[] = "1234:www.example.com:game_data"; |
| @@ -42,6 +47,31 @@ class FakeBackgroundFetchDelegate |
| std::string expected_id_; |
| }; |
| +class FakeBackgroundFetchObserver |
| + : public offline_items_collection::OfflineContentProvider::Observer { |
|
Peter Beverloo
2017/04/20 16:27:54
drop oic::
harkness
2017/04/21 14:55:20
Done.
|
| + public: |
| + FakeBackgroundFetchObserver() {} |
| + ~FakeBackgroundFetchObserver() override{}; |
|
Peter Beverloo
2017/04/20 16:27:54
= default; for both
harkness
2017/04/21 14:55:20
Done.
|
| + |
| + void OnItemsAvailable(OfflineContentProvider* provider) override { |
|
Peter Beverloo
2017/04/20 16:27:54
// OfflineContentProvider::Observer implementation
harkness
2017/04/21 14:55:20
Done.
|
| + provider_ = provider; |
| + } |
| + |
| + void OnItemsAdded( |
| + const OfflineContentProvider::OfflineItemList& items) override { |
| + added_items_ = items; |
| + } |
| + |
| + void OnItemRemoved(const ContentId& id) override { removed_item_ = id; } |
| + |
| + void OnItemUpdated(const OfflineItem& item) override { updated_item_ = item; } |
| + |
| + OfflineContentProvider* provider_ = nullptr; |
| + OfflineContentProvider::OfflineItemList added_items_; |
| + ContentId removed_item_; |
| + OfflineItem updated_item_; |
| +}; |
| + |
| } // namespace |
| class BackgroundFetchClientTest : public ::testing::Test { |
| @@ -93,3 +123,53 @@ TEST_F(BackgroundFetchClientTest, ResumeDownloadTest) { |
| client->SetDelegate(nullptr); |
| ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); |
| } |
| + |
| +TEST_F(BackgroundFetchClientTest, AddObserverImmediateNotificationTest) { |
| + BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| + profile_.GetBackgroundFetchClient()); |
|
Peter Beverloo
2017/04/20 16:27:54
Can we add a method to the fixture to avoid these
Peter Beverloo
2017/04/24 14:39:14
Ping.
|
| + FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| + client->SetDelegate(&delegate); |
| + |
| + // The delegate is already available, so AddObserver should trigger an |
| + // immediate OnItemsAvailable call. |
| + FakeBackgroundFetchObserver observer; |
| + client->AddObserver(&observer); |
| + EXPECT_EQ(client, observer.provider_); |
| +} |
| + |
| +TEST_F(BackgroundFetchClientTest, AddObserverDelayedNotificationTest) { |
| + BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| + profile_.GetBackgroundFetchClient()); |
| + |
| + // The observer should not get a OnItemsAvailable call since there is no |
| + // connected delegate. |
| + FakeBackgroundFetchObserver observer; |
| + client->AddObserver(&observer); |
| + EXPECT_EQ(nullptr, observer.provider_); |
| + |
| + // Adding a delegate should trigger the OnItemsAvailable call. |
| + FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| + client->SetDelegate(&delegate); |
| + EXPECT_EQ(client, observer.provider_); |
| +} |
| + |
| +TEST_F(BackgroundFetchClientTest, AddDownloadTest) { |
| + BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| + profile_.GetBackgroundFetchClient()); |
| + FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| + client->SetDelegate(&delegate); |
| + |
| + FakeBackgroundFetchObserver observer; |
| + client->AddObserver(&observer); |
| + |
| + client->AddDownload(kRegistrationId, "download_title", 1024); |
| + ASSERT_EQ(1U, observer.added_items_.size()); |
| + OfflineItem item = observer.added_items_[0]; |
| + EXPECT_EQ("download_title", item.title); |
| + EXPECT_EQ(1024, item.total_size_bytes); |
| + EXPECT_EQ(true, item.is_transient); |
| + EXPECT_EQ(0, item.percent_completed); |
| + EXPECT_EQ(true, item.is_resumable); |
| + EXPECT_EQ(kRegistrationId, item.id.id); |
| + EXPECT_EQ(kNamespace, item.id.name_space); |
| +} |