1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
content / browser / background_fetch / background_fetch_registration_notifier_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/background_fetch/background_fetch_registration_notifier.h"
#include <stdint.h>
#include <memory>
#include <vector>
#include "base/test/task_environment.h"
#include "base/test/test_simple_task_runner.h"
#include "content/browser/background_fetch/background_fetch_registration_service_impl.h"
#include "content/common/background_fetch/background_fetch_types.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"
namespace content {
namespace {
const char kDeveloperId[] = "my-fetch";
const char kPrimaryUniqueId[] = "7e57ab1e-c0de-a150-ca75-1e75f005ba11";
const char kSecondaryUniqueId[] = "bb48a9fb-c21f-4c2d-a9ae-58bd48a9fb53";
const char kURL[] = "https://example.com";
constexpr uint64_t kDownloadTotal = 2u;
constexpr uint64_t kDownloaded = 1u;
constexpr uint64_t kUploadTotal = 3u;
constexpr uint64_t kUploaded = 3u;
class TestRegistrationObserver
: public blink::mojom::BackgroundFetchRegistrationObserver {
public:
using CompletedRequests =
std::vector<std::pair<blink::mojom::FetchAPIRequestPtr,
blink::mojom::FetchAPIResponsePtr>>;
struct ProgressUpdate {
ProgressUpdate(uint64_t upload_total,
uint64_t uploaded,
uint64_t download_total,
uint64_t downloaded,
blink::mojom::BackgroundFetchResult result,
blink::mojom::BackgroundFetchFailureReason failure_reason)
: upload_total(upload_total),
uploaded(uploaded),
download_total(download_total),
downloaded(downloaded),
result(result),
failure_reason(failure_reason) {}
uint64_t upload_total = 0;
uint64_t uploaded = 0;
uint64_t download_total = 0;
uint64_t downloaded = 0;
blink::mojom::BackgroundFetchResult result =
blink::mojom::BackgroundFetchResult::UNSET;
blink::mojom::BackgroundFetchFailureReason failure_reason =
blink::mojom::BackgroundFetchFailureReason::NONE;
};
TestRegistrationObserver() = default;
TestRegistrationObserver(const TestRegistrationObserver&) = delete;
TestRegistrationObserver& operator=(const TestRegistrationObserver&) = delete;
~TestRegistrationObserver() override = default;
// Closes the bindings associated with this observer.
void Close() { receiver_.reset(); }
// Returns an InterfacePtr to this observer.
mojo::PendingRemote<blink::mojom::BackgroundFetchRegistrationObserver>
GetRemote() {
return receiver_.BindNewPipeAndPassRemote();
}
// Returns the vector of progress updates received by this observer.
const std::vector<ProgressUpdate>& progress_updates() const {
return progress_updates_;
}
// Returns the vector of completed request notifications received by this
// observer.
const CompletedRequests& completed_requests() const {
return completed_requests_;
}
bool records_available() const { return records_available_; }
// blink::mojom::BackgroundFetchRegistrationObserver implementation.
void OnProgress(
uint64_t upload_total,
uint64_t uploaded,
uint64_t download_total,
uint64_t downloaded,
blink::mojom::BackgroundFetchResult result,
blink::mojom::BackgroundFetchFailureReason failure_reason) override {
progress_updates_.emplace_back(upload_total, uploaded, download_total,
downloaded, result, failure_reason);
}
void OnRecordsUnavailable() override { records_available_ = false; }
void OnRequestCompleted(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::FetchAPIResponsePtr response) override {
completed_requests_.emplace_back(std::move(request), std::move(response));
}
private:
std::vector<ProgressUpdate> progress_updates_;
CompletedRequests completed_requests_;
mojo::Receiver<blink::mojom::BackgroundFetchRegistrationObserver> receiver_{
this};
bool records_available_ = true;
};
class BackgroundFetchRegistrationNotifierTest : public ::testing::Test {
public:
BackgroundFetchRegistrationNotifierTest()
: notifier_(std::make_unique<BackgroundFetchRegistrationNotifier>()) {}
BackgroundFetchRegistrationNotifierTest(
const BackgroundFetchRegistrationNotifierTest&) = delete;
BackgroundFetchRegistrationNotifierTest& operator=(
const BackgroundFetchRegistrationNotifierTest&) = delete;
~BackgroundFetchRegistrationNotifierTest() override = default;
// Notifies all observers for the |unique_id| of the made progress, and waits
// until the task runner managing the Mojo connection has finished.
void Notify(const std::string& unique_id,
blink::mojom::BackgroundFetchRegistrationDataPtr registration) {
notifier_->Notify(unique_id, *registration);
task_environment_.RunUntilIdle();
}
void NotifyRecordsUnavailable(const std::string& unique_id) {
notifier_->NotifyRecordsUnavailable(unique_id);
task_environment_.RunUntilIdle();
}
void NotifyRequestCompleted(const std::string& unique_id,
blink::mojom::FetchAPIRequestPtr request,
blink::mojom::FetchAPIResponsePtr response) {
notifier_->NotifyRequestCompleted(unique_id, std::move(request),
std::move(response));
task_environment_.RunUntilIdle();
}
void AddObservedUrl(const std::string& unique_id, const GURL& url) {
notifier_->AddObservedUrl(unique_id, url);
task_environment_.RunUntilIdle();
}
protected:
base::test::TaskEnvironment task_environment_;
std::unique_ptr<BackgroundFetchRegistrationNotifier> notifier_;
};
TEST_F(BackgroundFetchRegistrationNotifierTest, NotifySingleObserver) {
auto observer = std::make_unique<TestRegistrationObserver>();
notifier_->AddObserver(kPrimaryUniqueId, observer->GetRemote());
ASSERT_EQ(observer->progress_updates().size(), 0u);
Notify(kPrimaryUniqueId,
blink::mojom::BackgroundFetchRegistrationData::New(
kDeveloperId, kUploadTotal, kUploaded, kDownloadTotal, kDownloaded,
blink::mojom::BackgroundFetchResult::UNSET,
blink::mojom::BackgroundFetchFailureReason::NONE));
ASSERT_EQ(observer->progress_updates().size(), 1u);
auto& update = observer->progress_updates()[0];
EXPECT_EQ(update.upload_total, kUploadTotal);
EXPECT_EQ(update.uploaded, kUploaded);
EXPECT_EQ(update.download_total, kDownloadTotal);
EXPECT_EQ(update.downloaded, kDownloaded);
EXPECT_EQ(update.result, blink::mojom::BackgroundFetchResult::UNSET);
EXPECT_EQ(update.failure_reason,
blink::mojom::BackgroundFetchFailureReason::NONE);
}
TEST_F(BackgroundFetchRegistrationNotifierTest, NotifyMultipleObservers) {
std::vector<std::unique_ptr<TestRegistrationObserver>> primary_observers;
primary_observers.push_back(std::make_unique<TestRegistrationObserver>());
primary_observers.push_back(std::make_unique<TestRegistrationObserver>());
primary_observers.push_back(std::make_unique<TestRegistrationObserver>());
auto secondary_observer = std::make_unique<TestRegistrationObserver>();
for (auto& observer : primary_observers) {
notifier_->AddObserver(kPrimaryUniqueId, observer->GetRemote());
ASSERT_EQ(observer->progress_updates().size(), 0u);
}
notifier_->AddObserver(kSecondaryUniqueId, secondary_observer->GetRemote());
ASSERT_EQ(secondary_observer->progress_updates().size(), 0u);
// Notify the |kPrimaryUniqueId|.
Notify(kPrimaryUniqueId,
blink::mojom::BackgroundFetchRegistrationData::New(
kDeveloperId, kUploadTotal, kUploaded, kDownloadTotal, kDownloaded,
blink::mojom::BackgroundFetchResult::UNSET,
blink::mojom::BackgroundFetchFailureReason::NONE));
for (auto& observer : primary_observers) {
ASSERT_EQ(observer->progress_updates().size(), 1u);
auto& update = observer->progress_updates()[0];
EXPECT_EQ(update.upload_total, kUploadTotal);
EXPECT_EQ(update.uploaded, kUploaded);
EXPECT_EQ(update.download_total, kDownloadTotal);
EXPECT_EQ(update.downloaded, kDownloaded);
EXPECT_EQ(update.result, blink::mojom::BackgroundFetchResult::UNSET);
EXPECT_EQ(update.failure_reason,
blink::mojom::BackgroundFetchFailureReason::NONE);
}
// The observer for |kSecondaryUniqueId| should not have been notified.
ASSERT_EQ(secondary_observer->progress_updates().size(), 0u);
}
TEST_F(BackgroundFetchRegistrationNotifierTest,
NotifyFollowingObserverInitiatedRemoval) {
auto observer = std::make_unique<TestRegistrationObserver>();
notifier_->AddObserver(kPrimaryUniqueId, observer->GetRemote());
ASSERT_EQ(observer->progress_updates().size(), 0u);
Notify(kPrimaryUniqueId,
blink::mojom::BackgroundFetchRegistrationData::New(
kDeveloperId, kUploadTotal, kUploaded, kDownloadTotal, kDownloaded,
blink::mojom::BackgroundFetchResult::UNSET,
blink::mojom::BackgroundFetchFailureReason::NONE));
ASSERT_EQ(observer->progress_updates().size(), 1u);
// Closes the binding as would be done from the renderer process.
observer->Close();
Notify(kPrimaryUniqueId,
blink::mojom::BackgroundFetchRegistrationData::New(
kDeveloperId, kUploadTotal, kUploaded, kDownloadTotal, kDownloaded,
blink::mojom::BackgroundFetchResult::UNSET,
blink::mojom::BackgroundFetchFailureReason::NONE));
// The observers for |kPrimaryUniqueId| were removed, so no second update
// should have been received by the |observer|.
ASSERT_EQ(observer->progress_updates().size(), 1u);
}
TEST_F(BackgroundFetchRegistrationNotifierTest, NotifyWithoutObservers) {
auto observer = std::make_unique<TestRegistrationObserver>();
notifier_->AddObserver(kPrimaryUniqueId, observer->GetRemote());
ASSERT_EQ(observer->progress_updates().size(), 0u);
Notify(kSecondaryUniqueId,
blink::mojom::BackgroundFetchRegistrationData::New(
kDeveloperId,
/* upload_total*/ 0, /* uploaded*/ 0, kDownloadTotal, kDownloaded,
blink::mojom::BackgroundFetchResult::UNSET,
blink::mojom::BackgroundFetchFailureReason::NONE));
// Because the notification was for |kSecondaryUniqueId|, no progress updates
// should be received by the |observer|.
EXPECT_EQ(observer->progress_updates().size(), 0u);
}
TEST_F(BackgroundFetchRegistrationNotifierTest, NotifyRecordsUnavailable) {
auto observer = std::make_unique<TestRegistrationObserver>();
notifier_->AddObserver(kPrimaryUniqueId, observer->GetRemote());
ASSERT_TRUE(observer->records_available());
NotifyRecordsUnavailable(kPrimaryUniqueId);
ASSERT_FALSE(observer->records_available());
}
TEST_F(BackgroundFetchRegistrationNotifierTest, NotifyRequestCompleted) {
auto observer = std::make_unique<TestRegistrationObserver>();
notifier_->AddObserver(kPrimaryUniqueId, observer->GetRemote());
// No observed URLs. Observers shouldn't have been notified.
ASSERT_EQ(observer->completed_requests().size(), 0u);
auto request = blink::mojom::FetchAPIRequest::New();
request->url = GURL(kURL);
NotifyRequestCompleted(kPrimaryUniqueId,
BackgroundFetchSettledFetch::CloneRequest(request),
/* response */ nullptr);
ASSERT_EQ(observer->completed_requests().size(), 0u);
// Add observed URL. NotifyRequestCompleted() should now notify the observer.
AddObservedUrl(kPrimaryUniqueId, request->url);
NotifyRequestCompleted(kPrimaryUniqueId, std::move(request),
/* response */ nullptr);
ASSERT_EQ(observer->completed_requests().size(), 1u);
auto& received_pair = observer->completed_requests()[0];
EXPECT_EQ(received_pair.first->url, GURL(kURL));
EXPECT_TRUE(received_pair.second.is_null());
}
} // namespace
} // namespace content