OLD | NEW |
---|---|
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 | |
51 : public offline_items_collection::OfflineContentProvider::Observer { | |
Peter Beverloo
2017/04/20 16:27:54
drop oic::
harkness
2017/04/21 14:55:20
Done.
| |
52 public: | |
53 FakeBackgroundFetchObserver() {} | |
54 ~FakeBackgroundFetchObserver() override{}; | |
Peter Beverloo
2017/04/20 16:27:54
= default; for both
harkness
2017/04/21 14:55:20
Done.
| |
55 | |
56 void OnItemsAvailable(OfflineContentProvider* provider) override { | |
Peter Beverloo
2017/04/20 16:27:54
// OfflineContentProvider::Observer implementation
harkness
2017/04/21 14:55:20
Done.
| |
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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 offline_items_collection::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()); | |
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.
| |
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(kRegistrationId, "download_title", 1024); | |
166 ASSERT_EQ(1U, observer.added_items_.size()); | |
167 OfflineItem item = observer.added_items_[0]; | |
168 EXPECT_EQ("download_title", item.title); | |
169 EXPECT_EQ(1024, item.total_size_bytes); | |
170 EXPECT_EQ(true, item.is_transient); | |
171 EXPECT_EQ(0, item.percent_completed); | |
172 EXPECT_EQ(true, item.is_resumable); | |
173 EXPECT_EQ(kRegistrationId, item.id.id); | |
174 EXPECT_EQ(kNamespace, item.id.name_space); | |
175 } | |
OLD | NEW |