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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
content / browser / devtools / devtools_background_services_context_impl_unittest.cc [blame]
// Copyright 2019 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/devtools/devtools_background_services_context_impl.h"
#include <memory>
#include <string>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "content/browser/devtools/devtools_background_services.pb.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/origin.h"
namespace content {
namespace {
using testing::_;
const std::string kEventName = "Test Event";
const std::string kInstanceId = "my-instance";
class TestBrowserClient : public ContentBrowserClient {
public:
TestBrowserClient() {}
~TestBrowserClient() override {}
void UpdateDevToolsBackgroundServiceExpiration(
BrowserContext* browser_context,
int service,
base::Time expiration_time) override {
exp_dict_[service] = expiration_time;
}
base::flat_map<int, base::Time> GetDevToolsBackgroundServiceExpirations(
BrowserContext* browser_context) override {
return exp_dict_;
}
private:
base::flat_map<int, base::Time> exp_dict_;
};
void DidRegisterServiceWorker(int64_t* out_service_worker_registration_id,
base::OnceClosure quit_closure,
blink::ServiceWorkerStatusCode status,
const std::string& status_message,
int64_t service_worker_registration_id) {
DCHECK(out_service_worker_registration_id);
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk, status) << status_message;
*out_service_worker_registration_id = service_worker_registration_id;
std::move(quit_closure).Run();
}
void DidFindServiceWorkerRegistration(
scoped_refptr<ServiceWorkerRegistration>* out_service_worker_registration,
base::OnceClosure quit_closure,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> service_worker_registration) {
DCHECK(out_service_worker_registration);
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk, status)
<< blink::ServiceWorkerStatusToString(status);
*out_service_worker_registration = service_worker_registration;
std::move(quit_closure).Run();
}
void DidGetLoggedBackgroundServiceEvents(
base::OnceClosure quit_closure,
std::vector<devtools::proto::BackgroundServiceEvent>* out_feature_states,
std::vector<devtools::proto::BackgroundServiceEvent> feature_states) {
*out_feature_states = std::move(feature_states);
std::move(quit_closure).Run();
}
} // namespace
class DevToolsBackgroundServicesContextTest
: public ::testing::Test,
DevToolsBackgroundServicesContextImpl::EventObserver {
public:
DevToolsBackgroundServicesContextTest()
: task_environment_(BrowserTaskEnvironment::IO_MAINLOOP),
embedded_worker_test_helper_(base::FilePath() /* in memory */) {}
DevToolsBackgroundServicesContextTest(
const DevToolsBackgroundServicesContextTest&) = delete;
DevToolsBackgroundServicesContextTest& operator=(
const DevToolsBackgroundServicesContextTest&) = delete;
~DevToolsBackgroundServicesContextTest() override = default;
void SetUp() override {
// Register Service Worker.
service_worker_registration_id_ = RegisterServiceWorker();
ASSERT_NE(service_worker_registration_id_,
blink::mojom::kInvalidServiceWorkerRegistrationId);
browser_client_ = std::make_unique<TestBrowserClient>();
scoped_content_browser_client_setting_ =
std::make_unique<ScopedContentBrowserClientSetting>(
browser_client_.get());
SimulateBrowserRestart();
}
void TearDown() override { context_->RemoveObserver(this); }
mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage_control() {
return embedded_worker_test_helper_.context()
->registry()
->GetRemoteStorageControl();
}
protected:
MOCK_METHOD1(OnEventReceived,
void(const devtools::proto::BackgroundServiceEvent& event));
MOCK_METHOD2(OnRecordingStateChanged,
void(bool shoul_record,
devtools::proto::BackgroundService service));
void SimulateBrowserRestart() {
if (context_)
context_->RemoveObserver(this);
// Create |context_|.
context_ = std::make_unique<DevToolsBackgroundServicesContextImpl>(
&browser_context_, embedded_worker_test_helper_.context_wrapper());
context_->AddObserver(this);
ASSERT_TRUE(context_);
}
void SimulateOneWeekPassing() {
base::Time one_week_ago = base::Time::Now() - base::Days(7);
context_->expiration_times_
[devtools::proto::BackgroundService::BACKGROUND_FETCH] = one_week_ago;
}
bool IsRecording() {
return context_->IsRecording(
devtools::proto::BackgroundService::BACKGROUND_FETCH);
}
base::Time GetExpirationTime() {
return context_->expiration_times_
[devtools::proto::BackgroundService::BACKGROUND_FETCH];
}
std::vector<devtools::proto::BackgroundServiceEvent>
GetLoggedBackgroundServiceEvents() {
std::vector<devtools::proto::BackgroundServiceEvent> feature_states;
base::RunLoop run_loop;
context_->GetLoggedBackgroundServiceEvents(
devtools::proto::BackgroundService::BACKGROUND_FETCH,
base::BindOnce(&DidGetLoggedBackgroundServiceEvents,
run_loop.QuitClosure(), &feature_states));
run_loop.Run();
return feature_states;
}
void LogTestBackgroundServiceEvent(const std::string& log_message) {
context_->LogBackgroundServiceEvent(
service_worker_registration_id_,
blink::StorageKey::CreateFirstParty(origin_),
DevToolsBackgroundService::kBackgroundFetch, kEventName, kInstanceId,
{{"key", log_message}});
}
void StartRecording() {
EXPECT_CALL(
*this, OnRecordingStateChanged(
true, devtools::proto::BackgroundService::BACKGROUND_FETCH));
context_->StartRecording(
devtools::proto::BackgroundService::BACKGROUND_FETCH);
// Wait for the messages to propagate to the browser client.
task_environment_.RunUntilIdle();
}
void StopRecording() {
EXPECT_CALL(
*this,
OnRecordingStateChanged(
false, devtools::proto::BackgroundService::BACKGROUND_FETCH));
context_->StopRecording(
devtools::proto::BackgroundService::BACKGROUND_FETCH);
// Wait for the messages to propagate to the browser client.
task_environment_.RunUntilIdle();
}
void ClearLoggedBackgroundServiceEvents() {
context_->ClearLoggedBackgroundServiceEvents(
devtools::proto::BackgroundService::BACKGROUND_FETCH);
}
BrowserTaskEnvironment task_environment_; // Must be first member.
url::Origin origin_ = url::Origin::Create(GURL("https://example.com"));
int64_t service_worker_registration_id_ =
blink::mojom::kInvalidServiceWorkerRegistrationId;
private:
int64_t RegisterServiceWorker() {
GURL script_url(origin_.GetURL().spec() + "sw.js");
const blink::StorageKey key = blink::StorageKey::CreateFirstParty(origin_);
int64_t service_worker_registration_id =
blink::mojom::kInvalidServiceWorkerRegistrationId;
{
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = origin_.GetURL();
base::RunLoop run_loop;
embedded_worker_test_helper_.context()->RegisterServiceWorker(
script_url, key, options,
blink::mojom::FetchClientSettingsObject::New(),
base::BindOnce(&DidRegisterServiceWorker,
&service_worker_registration_id,
run_loop.QuitClosure()),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
run_loop.Run();
}
if (service_worker_registration_id ==
blink::mojom::kInvalidServiceWorkerRegistrationId) {
ADD_FAILURE() << "Could not obtain a valid Service Worker registration";
return blink::mojom::kInvalidServiceWorkerRegistrationId;
}
{
base::RunLoop run_loop;
embedded_worker_test_helper_.context()->registry()->FindRegistrationForId(
service_worker_registration_id, key,
base::BindOnce(&DidFindServiceWorkerRegistration,
&service_worker_registration_,
run_loop.QuitClosure()));
run_loop.Run();
}
// Wait for the worker to be activated.
base::RunLoop().RunUntilIdle();
if (!service_worker_registration_) {
ADD_FAILURE() << "Could not find the new Service Worker registration.";
return blink::mojom::kInvalidServiceWorkerRegistrationId;
}
return service_worker_registration_id;
}
EmbeddedWorkerTestHelper embedded_worker_test_helper_;
TestBrowserContext browser_context_;
std::unique_ptr<DevToolsBackgroundServicesContextImpl> context_;
scoped_refptr<ServiceWorkerRegistration> service_worker_registration_;
std::unique_ptr<ContentBrowserClient> browser_client_;
std::unique_ptr<ScopedContentBrowserClientSetting>
scoped_content_browser_client_setting_;
};
// Flaky on Fuchsia.
// TODO(crbug.com/40936408): Reenable test on Fuchsia.
#if BUILDFLAG(IS_FUCHSIA)
#define MAYBE_NothingStoredWithRecordingModeOff \
DISABLED_NothingStoredWithRecordingModeOff
#else
#define MAYBE_NothingStoredWithRecordingModeOff \
NothingStoredWithRecordingModeOff
#endif
TEST_F(DevToolsBackgroundServicesContextTest,
MAYBE_NothingStoredWithRecordingModeOff) {
// Initially there are no entries.
EXPECT_TRUE(GetLoggedBackgroundServiceEvents().empty());
// "Log" some events and wait for them to finish.
LogTestBackgroundServiceEvent("f1");
LogTestBackgroundServiceEvent("f2");
// There should still be nothing since recording mode is off.
EXPECT_TRUE(GetLoggedBackgroundServiceEvents().empty());
}
TEST_F(DevToolsBackgroundServicesContextTest, GetLoggedEvents) {
StartRecording();
// "Log" some events and wait for them to finish.
LogTestBackgroundServiceEvent("f1");
LogTestBackgroundServiceEvent("f2");
storage_control().FlushForTesting();
// Check the values.
auto feature_events = GetLoggedBackgroundServiceEvents();
ASSERT_EQ(feature_events.size(), 2u);
for (const auto& feature_event : feature_events) {
EXPECT_EQ(feature_event.background_service(),
devtools::proto::BackgroundService::BACKGROUND_FETCH);
EXPECT_EQ(feature_event.origin(), origin_.GetURL().spec());
EXPECT_EQ(feature_event.service_worker_registration_id(),
service_worker_registration_id_);
EXPECT_EQ(feature_event.event_name(), kEventName);
EXPECT_EQ(feature_event.instance_id(), kInstanceId);
ASSERT_EQ(feature_event.event_metadata().size(), 1u);
}
EXPECT_EQ(feature_events[0].event_metadata().at("key"), "f1");
EXPECT_EQ(feature_events[1].event_metadata().at("key"), "f2");
EXPECT_LE(feature_events[0].timestamp(), feature_events[1].timestamp());
}
TEST_F(DevToolsBackgroundServicesContextTest, StopRecording) {
StartRecording();
// Initially there are no entries.
EXPECT_TRUE(GetLoggedBackgroundServiceEvents().empty());
// "Log" some events and wait for them to finish.
LogTestBackgroundServiceEvent("f1");
StopRecording();
LogTestBackgroundServiceEvent("f2");
// Check the values.
ASSERT_EQ(GetLoggedBackgroundServiceEvents().size(), 1u);
}
TEST_F(DevToolsBackgroundServicesContextTest, DelegateExpirationTimes) {
// Initially expiration time is null.
EXPECT_TRUE(GetExpirationTime().is_null());
EXPECT_FALSE(IsRecording());
// Toggle Recording mode, and now this should be non-null.
StartRecording();
EXPECT_FALSE(GetExpirationTime().is_null());
EXPECT_TRUE(IsRecording());
// The value should still be there on browser restarts.
SimulateBrowserRestart();
EXPECT_FALSE(GetExpirationTime().is_null());
EXPECT_TRUE(IsRecording());
// Stopping Recording mode should clear the value.
StopRecording();
EXPECT_TRUE(GetExpirationTime().is_null());
EXPECT_FALSE(IsRecording());
SimulateBrowserRestart();
EXPECT_TRUE(GetExpirationTime().is_null());
EXPECT_FALSE(IsRecording());
}
TEST_F(DevToolsBackgroundServicesContextTest, RecordingExpiration) {
// Initially expiration time is null.
EXPECT_FALSE(IsRecording());
// Toggle Recording mode, and now this should be non-null.
StartRecording();
EXPECT_TRUE(IsRecording());
SimulateOneWeekPassing();
EXPECT_FALSE(GetExpirationTime().is_null());
// Recording should be true, with an expired value.
EXPECT_TRUE(IsRecording());
// Logging should not happen.
EXPECT_CALL(*this, OnEventReceived(_)).Times(0);
// Observers should be informed when recording stops.
EXPECT_CALL(*this,
OnRecordingStateChanged(
false, devtools::proto::BackgroundService::BACKGROUND_FETCH));
LogTestBackgroundServiceEvent("f1");
task_environment_.RunUntilIdle();
// The expiration time entry should be cleared.
EXPECT_TRUE(GetExpirationTime().is_null());
EXPECT_FALSE(IsRecording());
}
TEST_F(DevToolsBackgroundServicesContextTest, ClearLoggedEvents) {
StartRecording();
// "Log" some events and wait for them to finish.
LogTestBackgroundServiceEvent("f1");
LogTestBackgroundServiceEvent("f2");
storage_control().FlushForTesting();
// Check the values.
auto feature_events = GetLoggedBackgroundServiceEvents();
ASSERT_EQ(feature_events.size(), 2u);
ClearLoggedBackgroundServiceEvents();
storage_control().FlushForTesting();
// Should be empty now.
feature_events = GetLoggedBackgroundServiceEvents();
EXPECT_TRUE(feature_events.empty());
}
TEST_F(DevToolsBackgroundServicesContextTest, EventObserverCalled) {
{
EXPECT_CALL(*this, OnEventReceived(_)).Times(0);
LogTestBackgroundServiceEvent("f1");
task_environment_.RunUntilIdle();
}
StartRecording();
{
EXPECT_CALL(*this, OnEventReceived(_));
LogTestBackgroundServiceEvent("f2");
task_environment_.RunUntilIdle();
}
}
} // namespace content