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

Unified Diff: chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc

Issue 2833793002: Added AddDownload communication path and observers
Patch Set: Addressed comments and moved thrad jumps into the client proxy. Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
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..66dd47d07a88fc5eb2620e8ac8e881c186c8eab0 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 OfflineContentProvider::Observer {
+ public:
+ FakeBackgroundFetchObserver() = default;
+ ~FakeBackgroundFetchObserver() override = default;
+
+ // OfflineContentProvider::Observer implementation.
+ void OnItemsAvailable(OfflineContentProvider* provider) override {
+ 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 {
@@ -61,7 +91,7 @@ TEST_F(BackgroundFetchClientTest, CancelDownloadTest) {
FakeBackgroundFetchDelegate delegate(kRegistrationId);
client->SetDelegate(&delegate);
- offline_items_collection::ContentId id(kNamespace, kRegistrationId);
+ ContentId id(kNamespace, kRegistrationId);
ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id));
client->SetDelegate(nullptr);
@@ -74,7 +104,7 @@ TEST_F(BackgroundFetchClientTest, PauseDownloadTest) {
FakeBackgroundFetchDelegate delegate(kRegistrationId);
client->SetDelegate(&delegate);
- offline_items_collection::ContentId id(kNamespace, kRegistrationId);
+ ContentId id(kNamespace, kRegistrationId);
ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id));
client->SetDelegate(nullptr);
@@ -87,9 +117,65 @@ TEST_F(BackgroundFetchClientTest, ResumeDownloadTest) {
FakeBackgroundFetchDelegate delegate(kRegistrationId);
client->SetDelegate(&delegate);
- offline_items_collection::ContentId id(kNamespace, kRegistrationId);
+ ContentId id(kNamespace, kRegistrationId);
ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id));
client->SetDelegate(nullptr);
ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id));
}
+
+TEST_F(BackgroundFetchClientTest, AddObserverImmediateNotificationTest) {
+ BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>(
+ profile_.GetBackgroundFetchClient());
+ 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(GURL("http://www.example.com"), 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_FALSE(item.is_openable);
+ EXPECT_EQ(GURL("http://www.example.com"), item.page_url);
+ EXPECT_FALSE(item.is_off_the_record);
+ EXPECT_TRUE(item.is_transient);
+ EXPECT_EQ(0, item.percent_completed);
+ EXPECT_EQ(offline_items_collection::OfflineItemState::IN_PROGRESS,
+ item.state);
+ EXPECT_TRUE(item.is_resumable);
+ EXPECT_EQ(kRegistrationId, item.id.id);
+ EXPECT_EQ(kNamespace, item.id.name_space);
+}

Powered by Google App Engine
This is Rietveld 408576698