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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/background_fetch/background_fetch_client_impl.h" 5 #include "chrome/browser/background_fetch/background_fetch_client_impl.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/threading/thread_task_runner_handle.h" 8 #include "base/threading/thread_task_runner_handle.h"
9 #include "chrome/browser/background_fetch/background_fetch_client_factory.h" 9 #include "chrome/browser/background_fetch/background_fetch_client_factory.h"
10 #include "chrome/test/base/testing_profile.h" 10 #include "chrome/test/base/testing_profile.h"
11 #include "components/offline_items_collection/core/offline_content_provider.h"
11 #include "components/offline_items_collection/core/offline_item.h" 12 #include "components/offline_items_collection/core/offline_item.h"
12 #include "content/public/browser/background_fetch_client.h" 13 #include "content/public/browser/background_fetch_client.h"
13 #include "content/public/test/test_browser_thread_bundle.h" 14 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
17 using offline_items_collection::ContentId;
18 using offline_items_collection::OfflineContentProvider;
19 using offline_items_collection::OfflineItem;
20
16 namespace { 21 namespace {
17 22
18 const char kRegistrationId[] = "1234:www.example.com:game_data"; 23 const char kRegistrationId[] = "1234:www.example.com:game_data";
19 const char kNamespace[] = "BackgroundFetchNamespace"; 24 const char kNamespace[] = "BackgroundFetchNamespace";
20 25
21 class FakeBackgroundFetchDelegate 26 class FakeBackgroundFetchDelegate
22 : public content::BackgroundFetchClient::Delegate { 27 : public content::BackgroundFetchClient::Delegate {
23 public: 28 public:
24 explicit FakeBackgroundFetchDelegate(const std::string& id) 29 explicit FakeBackgroundFetchDelegate(const std::string& id)
25 : expected_id_(id) {} 30 : expected_id_(id) {}
26 31
27 void CleanupAllTasks() override {} 32 void CleanupAllTasks() override {}
28 33
29 void CancelDownload(const std::string& registration_id) override { 34 void CancelDownload(const std::string& registration_id) override {
30 ASSERT_EQ(registration_id, expected_id_); 35 ASSERT_EQ(registration_id, expected_id_);
31 } 36 }
32 37
33 void PauseDownload(const std::string& registration_id) override { 38 void PauseDownload(const std::string& registration_id) override {
34 ASSERT_EQ(registration_id, expected_id_); 39 ASSERT_EQ(registration_id, expected_id_);
35 } 40 }
36 41
37 void ResumeDownload(const std::string& registration_id) override { 42 void ResumeDownload(const std::string& registration_id) override {
38 ASSERT_EQ(registration_id, expected_id_); 43 ASSERT_EQ(registration_id, expected_id_);
39 } 44 }
40 45
41 private: 46 private:
42 std::string expected_id_; 47 std::string expected_id_;
43 }; 48 };
44 49
50 class FakeBackgroundFetchObserver : public OfflineContentProvider::Observer {
51 public:
52 FakeBackgroundFetchObserver() = default;
53 ~FakeBackgroundFetchObserver() override = default;
54
55 // OfflineContentProvider::Observer implementation.
56 void OnItemsAvailable(OfflineContentProvider* provider) override {
57 provider_ = provider;
58 }
59
60 void OnItemsAdded(
61 const OfflineContentProvider::OfflineItemList& items) override {
62 added_items_ = items;
63 }
64
65 void OnItemRemoved(const ContentId& id) override { removed_item_ = id; }
66
67 void OnItemUpdated(const OfflineItem& item) override { updated_item_ = item; }
68
69 OfflineContentProvider* provider_ = nullptr;
70 OfflineContentProvider::OfflineItemList added_items_;
71 ContentId removed_item_;
72 OfflineItem updated_item_;
73 };
74
45 } // namespace 75 } // namespace
46 76
47 class BackgroundFetchClientTest : public ::testing::Test { 77 class BackgroundFetchClientTest : public ::testing::Test {
48 public: 78 public:
49 BackgroundFetchClientTest() {} 79 BackgroundFetchClientTest() {}
50 ~BackgroundFetchClientTest() override {} 80 ~BackgroundFetchClientTest() override {}
51 81
52 protected: 82 protected:
53 content::TestBrowserThreadBundle thread_bundle_; 83 content::TestBrowserThreadBundle thread_bundle_;
54 84
55 TestingProfile profile_; 85 TestingProfile profile_;
56 }; 86 };
57 87
58 TEST_F(BackgroundFetchClientTest, CancelDownloadTest) { 88 TEST_F(BackgroundFetchClientTest, CancelDownloadTest) {
59 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( 89 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>(
60 profile_.GetBackgroundFetchClient()); 90 profile_.GetBackgroundFetchClient());
61 FakeBackgroundFetchDelegate delegate(kRegistrationId); 91 FakeBackgroundFetchDelegate delegate(kRegistrationId);
62 client->SetDelegate(&delegate); 92 client->SetDelegate(&delegate);
63 93
64 offline_items_collection::ContentId id(kNamespace, kRegistrationId); 94 ContentId id(kNamespace, kRegistrationId);
65 ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id)); 95 ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id));
66 96
67 client->SetDelegate(nullptr); 97 client->SetDelegate(nullptr);
68 ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id)); 98 ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id));
69 } 99 }
70 100
71 TEST_F(BackgroundFetchClientTest, PauseDownloadTest) { 101 TEST_F(BackgroundFetchClientTest, PauseDownloadTest) {
72 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( 102 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>(
73 profile_.GetBackgroundFetchClient()); 103 profile_.GetBackgroundFetchClient());
74 FakeBackgroundFetchDelegate delegate(kRegistrationId); 104 FakeBackgroundFetchDelegate delegate(kRegistrationId);
75 client->SetDelegate(&delegate); 105 client->SetDelegate(&delegate);
76 106
77 offline_items_collection::ContentId id(kNamespace, kRegistrationId); 107 ContentId id(kNamespace, kRegistrationId);
78 ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id)); 108 ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id));
79 109
80 client->SetDelegate(nullptr); 110 client->SetDelegate(nullptr);
81 ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id)); 111 ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id));
82 } 112 }
83 113
84 TEST_F(BackgroundFetchClientTest, ResumeDownloadTest) { 114 TEST_F(BackgroundFetchClientTest, ResumeDownloadTest) {
85 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( 115 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>(
86 profile_.GetBackgroundFetchClient()); 116 profile_.GetBackgroundFetchClient());
87 FakeBackgroundFetchDelegate delegate(kRegistrationId); 117 FakeBackgroundFetchDelegate delegate(kRegistrationId);
88 client->SetDelegate(&delegate); 118 client->SetDelegate(&delegate);
89 119
90 offline_items_collection::ContentId id(kNamespace, kRegistrationId); 120 ContentId id(kNamespace, kRegistrationId);
91 ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); 121 ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id));
92 122
93 client->SetDelegate(nullptr); 123 client->SetDelegate(nullptr);
94 ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); 124 ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id));
95 } 125 }
126
127 TEST_F(BackgroundFetchClientTest, AddObserverImmediateNotificationTest) {
128 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>(
129 profile_.GetBackgroundFetchClient());
130 FakeBackgroundFetchDelegate delegate(kRegistrationId);
131 client->SetDelegate(&delegate);
132
133 // The delegate is already available, so AddObserver should trigger an
134 // immediate OnItemsAvailable call.
135 FakeBackgroundFetchObserver observer;
136 client->AddObserver(&observer);
137 EXPECT_EQ(client, observer.provider_);
138 }
139
140 TEST_F(BackgroundFetchClientTest, AddObserverDelayedNotificationTest) {
141 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>(
142 profile_.GetBackgroundFetchClient());
143
144 // The observer should not get a OnItemsAvailable call since there is no
145 // connected delegate.
146 FakeBackgroundFetchObserver observer;
147 client->AddObserver(&observer);
148 EXPECT_EQ(nullptr, observer.provider_);
149
150 // Adding a delegate should trigger the OnItemsAvailable call.
151 FakeBackgroundFetchDelegate delegate(kRegistrationId);
152 client->SetDelegate(&delegate);
153 EXPECT_EQ(client, observer.provider_);
154 }
155
156 TEST_F(BackgroundFetchClientTest, AddDownloadTest) {
157 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>(
158 profile_.GetBackgroundFetchClient());
159 FakeBackgroundFetchDelegate delegate(kRegistrationId);
160 client->SetDelegate(&delegate);
161
162 FakeBackgroundFetchObserver observer;
163 client->AddObserver(&observer);
164
165 client->AddDownload(GURL("http://www.example.com"), kRegistrationId,
166 "download_title", 1024);
167 ASSERT_EQ(1U, observer.added_items_.size());
168 OfflineItem item = observer.added_items_[0];
169 EXPECT_EQ("download_title", item.title);
170 EXPECT_EQ(1024, item.total_size_bytes);
171 EXPECT_FALSE(item.is_openable);
172 EXPECT_EQ(GURL("http://www.example.com"), item.page_url);
173 EXPECT_FALSE(item.is_off_the_record);
174 EXPECT_TRUE(item.is_transient);
175 EXPECT_EQ(0, item.percent_completed);
176 EXPECT_EQ(offline_items_collection::OfflineItemState::IN_PROGRESS,
177 item.state);
178 EXPECT_TRUE(item.is_resumable);
179 EXPECT_EQ(kRegistrationId, item.id.id);
180 EXPECT_EQ(kNamespace, item.id.name_space);
181 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698