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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <set>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/macros.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/task_runner_util.h"
#include "content/browser/background_sync/background_sync_manager.h"
#include "content/browser/background_sync/background_sync_network_observer.h"
#include "content/browser/background_sync/background_sync_status.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/public/browser/background_sync_context.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/background_sync_test_util.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/base/network_change_notifier.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
using net::NetworkChangeNotifier;
namespace content {
namespace {
const char kDefaultTestURL[] = "/background_sync/test.html";
const char kEmptyURL[] = "/background_sync/empty.html";
const char kRegisterSyncURL[] = "/background_sync/register_sync.html";
const char kSuccessfulOperationPrefix[] = "ok - ";
std::string BuildScriptString(const std::string& function,
const std::string& argument) {
return base::StringPrintf("%s('%s');", function.c_str(), argument.c_str());
}
std::string BuildExpectedResult(const std::string& tag,
const std::string& action) {
return base::StringPrintf("%s%s %s", kSuccessfulOperationPrefix, tag.c_str(),
action.c_str());
}
void RegistrationPendingCallback(
const base::Closure& quit,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
bool* result_out,
bool result) {
*result_out = result;
task_runner->PostTask(FROM_HERE, quit);
}
void RegistrationPendingDidGetSyncRegistration(
const std::string& tag,
const base::Callback<void(bool)>& callback,
BackgroundSyncStatus error_type,
scoped_ptr<ScopedVector<BackgroundSyncRegistration>> registrations) {
ASSERT_EQ(BACKGROUND_SYNC_STATUS_OK, error_type);
// Find the right registration in the list and check its status.
for (const BackgroundSyncRegistration* registration : *registrations) {
if (registration->options()->tag == tag) {
callback.Run(registration->sync_state() ==
mojom::BackgroundSyncState::PENDING);
return;
}
}
ADD_FAILURE() << "Registration should exist";
}
void RegistrationPendingDidGetSWRegistration(
const scoped_refptr<BackgroundSyncContext> sync_context,
const std::string& tag,
const base::Callback<void(bool)>& callback,
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& registration) {
ASSERT_EQ(SERVICE_WORKER_OK, status);
int64_t service_worker_id = registration->id();
BackgroundSyncManager* sync_manager = sync_context->background_sync_manager();
sync_manager->GetRegistrations(
service_worker_id,
base::Bind(&RegistrationPendingDidGetSyncRegistration, tag, callback));
}
void RegistrationPendingOnIOThread(
const scoped_refptr<BackgroundSyncContext> sync_context,
const scoped_refptr<ServiceWorkerContextWrapper> sw_context,
const std::string& tag,
const GURL& url,
const base::Callback<void(bool)>& callback) {
sw_context->FindReadyRegistrationForDocument(
url, base::Bind(&RegistrationPendingDidGetSWRegistration, sync_context,
tag, callback));
}
void SetMaxSyncAttemptsOnIOThread(
const scoped_refptr<BackgroundSyncContext>& sync_context,
int max_sync_attempts) {
BackgroundSyncManager* background_sync_manager =
sync_context->background_sync_manager();
background_sync_manager->SetMaxSyncAttemptsForTesting(max_sync_attempts);
}
} // namespace
class BackgroundSyncBrowserTest : public ContentBrowserTest {
public:
BackgroundSyncBrowserTest() {}
~BackgroundSyncBrowserTest() override {}
void SetUp() override {
background_sync_test_util::SetIgnoreNetworkChangeNotifier(true);
ContentBrowserTest::SetUp();
}
void SetIncognitoMode(bool incognito) {
shell_ = incognito ? CreateOffTheRecordBrowser() : shell();
}
StoragePartition* GetStorage() {
WebContents* web_contents = shell_->web_contents();
return BrowserContext::GetStoragePartition(
web_contents->GetBrowserContext(), web_contents->GetSiteInstance());
}
BackgroundSyncContext* GetSyncContext() {
return GetStorage()->GetBackgroundSyncContext();
}
WebContents* web_contents() { return shell_->web_contents(); }
void SetUpCommandLine(base::CommandLine* command_line) override {
// TODO(jkarlin): Remove this once background sync is no longer
// experimental.
command_line->AppendSwitch(
switches::kEnableExperimentalWebPlatformFeatures);
}
void SetUpOnMainThread() override {
https_server_.reset(
new net::EmbeddedTestServer(net::EmbeddedTestServer::TYPE_HTTPS));
https_server_->ServeFilesFromSourceDirectory("content/test/data");
ASSERT_TRUE(https_server_->Start());
SetIncognitoMode(false);
SetMaxSyncAttempts(1);
background_sync_test_util::SetOnline(web_contents(), true);
ASSERT_TRUE(LoadTestPage(kDefaultTestURL));
ContentBrowserTest::SetUpOnMainThread();
}
void TearDownOnMainThread() override { https_server_.reset(); }
bool LoadTestPage(const std::string& path) {
return NavigateToURL(shell_, https_server_->GetURL(path));
}
bool RunScript(const std::string& script, std::string* result) {
return content::ExecuteScriptAndExtractString(web_contents(), script,
result);
}
// Returns true if the registration with tag |tag| is currently pending. Fails
// (assertion failure) if the tag isn't registered.
bool RegistrationPending(const std::string& tag);
// Sets the BackgroundSyncManager's max sync attempts per registration.
void SetMaxSyncAttempts(int max_sync_attempts);
void ClearStoragePartitionData();
std::string PopConsoleString();
bool PopConsole(const std::string& expected_msg);
bool RegisterServiceWorker();
bool Register(const std::string& tag);
bool RegisterFromServiceWorker(const std::string& tag);
bool HasTag(const std::string& tag);
bool HasTagFromServiceWorker(const std::string& tag);
bool MatchTags(const std::string& script_result,
const std::vector<std::string>& expected_tags);
bool GetTags(const std::vector<std::string>& expected_tags);
bool GetTagsFromServiceWorker(const std::vector<std::string>& expected_tags);
bool CompleteDelayedSyncEvent();
bool RejectDelayedSyncEvent();
net::EmbeddedTestServer* https_server() { return https_server_.get(); }
private:
scoped_ptr<net::EmbeddedTestServer> https_server_;
Shell* shell_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest);
};
bool BackgroundSyncBrowserTest::RegistrationPending(const std::string& tag) {
bool is_pending;
base::RunLoop run_loop;
StoragePartition* storage = GetStorage();
BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext();
ServiceWorkerContextWrapper* service_worker_context =
static_cast<ServiceWorkerContextWrapper*>(
storage->GetServiceWorkerContext());
base::Callback<void(bool)> callback =
base::Bind(&RegistrationPendingCallback, run_loop.QuitClosure(),
base::ThreadTaskRunnerHandle::Get(), &is_pending);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&RegistrationPendingOnIOThread,
make_scoped_refptr(sync_context),
make_scoped_refptr(service_worker_context), tag,
https_server_->GetURL(kDefaultTestURL), callback));
run_loop.Run();
return is_pending;
}
void BackgroundSyncBrowserTest::SetMaxSyncAttempts(int max_sync_attempts) {
base::RunLoop run_loop;
StoragePartition* storage = GetStorage();
BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext();
BrowserThread::PostTaskAndReply(
BrowserThread::IO, FROM_HERE,
base::Bind(&SetMaxSyncAttemptsOnIOThread,
make_scoped_refptr(sync_context), max_sync_attempts),
run_loop.QuitClosure());
run_loop.Run();
}
void BackgroundSyncBrowserTest::ClearStoragePartitionData() {
// Clear data from the storage partition. Parameters are set to clear data
// for service workers, for all origins, for an unbounded time range.
StoragePartition* storage = GetStorage();
uint32_t storage_partition_mask =
StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
uint32_t quota_storage_mask =
StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
const GURL& delete_origin = GURL();
const base::Time delete_begin = base::Time();
base::Time delete_end = base::Time::Max();
base::RunLoop run_loop;
storage->ClearData(storage_partition_mask, quota_storage_mask, delete_origin,
StoragePartition::OriginMatcherFunction(), delete_begin,
delete_end, run_loop.QuitClosure());
run_loop.Run();
}
std::string BackgroundSyncBrowserTest::PopConsoleString() {
std::string script_result;
EXPECT_TRUE(RunScript("resultQueue.pop()", &script_result));
return script_result;
}
bool BackgroundSyncBrowserTest::PopConsole(const std::string& expected_msg) {
std::string script_result = PopConsoleString();
return script_result == expected_msg;
}
bool BackgroundSyncBrowserTest::RegisterServiceWorker() {
std::string script_result;
EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
return script_result == BuildExpectedResult("service worker", "registered");
}
bool BackgroundSyncBrowserTest::Register(const std::string& tag) {
std::string script_result;
EXPECT_TRUE(RunScript(BuildScriptString("register", tag), &script_result));
return script_result == BuildExpectedResult(tag, "registered");
}
bool BackgroundSyncBrowserTest::RegisterFromServiceWorker(
const std::string& tag) {
std::string script_result;
EXPECT_TRUE(RunScript(BuildScriptString("registerFromServiceWorker", tag),
&script_result));
return script_result == BuildExpectedResult(tag, "register sent to SW");
}
bool BackgroundSyncBrowserTest::HasTag(const std::string& tag) {
std::string script_result;
EXPECT_TRUE(RunScript(BuildScriptString("hasTag", tag), &script_result));
return script_result == BuildExpectedResult(tag, "found");
}
bool BackgroundSyncBrowserTest::HasTagFromServiceWorker(
const std::string& tag) {
std::string script_result;
EXPECT_TRUE(RunScript(BuildScriptString("hasTagFromServiceWorker", tag),
&script_result));
EXPECT_TRUE(script_result == "ok - hasTag sent to SW");
return PopConsole(BuildExpectedResult(tag, "found"));
}
bool BackgroundSyncBrowserTest::MatchTags(
const std::string& script_result,
const std::vector<std::string>& expected_tags) {
EXPECT_TRUE(base::StartsWith(script_result, kSuccessfulOperationPrefix,
base::CompareCase::INSENSITIVE_ASCII));
std::string tag_string =
script_result.substr(strlen(kSuccessfulOperationPrefix));
std::vector<std::string> result_tags = base::SplitString(
tag_string, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
return std::set<std::string>(expected_tags.begin(), expected_tags.end()) ==
std::set<std::string>(result_tags.begin(), result_tags.end());
}
bool BackgroundSyncBrowserTest::GetTags(
const std::vector<std::string>& expected_tags) {
std::string script_result;
EXPECT_TRUE(RunScript("getTags()", &script_result));
return MatchTags(script_result, expected_tags);
}
bool BackgroundSyncBrowserTest::GetTagsFromServiceWorker(
const std::vector<std::string>& expected_tags) {
std::string script_result;
EXPECT_TRUE(RunScript("getTagsFromServiceWorker()", &script_result));
EXPECT_TRUE(script_result == "ok - getTags sent to SW");
return MatchTags(PopConsoleString(), expected_tags);
}
bool BackgroundSyncBrowserTest::CompleteDelayedSyncEvent() {
std::string script_result;
EXPECT_TRUE(RunScript("completeDelayedSyncEvent()", &script_result));
return script_result == BuildExpectedResult("delay", "completing");
}
bool BackgroundSyncBrowserTest::RejectDelayedSyncEvent() {
std::string script_result;
EXPECT_TRUE(RunScript("rejectDelayedSyncEvent()", &script_result));
return script_result == BuildExpectedResult("delay", "rejecting");
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
RegisterFromControlledDocument) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
EXPECT_TRUE(Register("foo"));
EXPECT_TRUE(PopConsole("foo fired"));
EXPECT_FALSE(HasTag("foo"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
RegisterFromUncontrolledDocument) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(Register("foo"));
EXPECT_TRUE(PopConsole("foo fired"));
EXPECT_FALSE(HasTag("foo"));
}
// Verify that Register works in a service worker
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, RegisterFromServiceWorker) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
EXPECT_TRUE(RegisterFromServiceWorker("foo_sw"));
EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW"));
EXPECT_TRUE(PopConsole("foo_sw fired"));
EXPECT_FALSE(HasTag("foo_sw"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
RegistrationDelaysForNetwork) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
// Prevent firing by going offline.
background_sync_test_util::SetOnline(web_contents(), false);
EXPECT_TRUE(Register("foo"));
EXPECT_TRUE(HasTag("foo"));
EXPECT_TRUE(RegistrationPending("foo"));
// Resume firing by going online.
background_sync_test_util::SetOnline(web_contents(), true);
EXPECT_TRUE(PopConsole("foo fired"));
EXPECT_FALSE(HasTag("foo"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntil) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
background_sync_test_util::SetOnline(web_contents(), true);
EXPECT_TRUE(Register("delay"));
// Verify that it is firing.
EXPECT_TRUE(HasTag("delay"));
EXPECT_FALSE(RegistrationPending("delay"));
// Complete the task.
EXPECT_TRUE(CompleteDelayedSyncEvent());
EXPECT_TRUE(PopConsole("ok - delay completed"));
// Verify that it finished firing.
EXPECT_FALSE(HasTag("delay"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
background_sync_test_util::SetOnline(web_contents(), true);
EXPECT_TRUE(Register("delay"));
// Verify that it is firing.
EXPECT_TRUE(HasTag("delay"));
EXPECT_FALSE(RegistrationPending("delay"));
// Complete the task.
EXPECT_TRUE(RejectDelayedSyncEvent());
EXPECT_TRUE(PopConsole("ok - delay rejected"));
EXPECT_FALSE(HasTag("delay"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Incognito) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
background_sync_test_util::SetOnline(web_contents(), false);
EXPECT_TRUE(Register("normal"));
EXPECT_TRUE(RegistrationPending("normal"));
// Go incognito and verify that incognito doesn't see the registration.
SetIncognitoMode(true);
// Tell the new network observer that we're offline (it initializes from
// NetworkChangeNotifier::GetCurrentConnectionType() which is not mocked out
// in this test).
background_sync_test_util::SetOnline(web_contents(), false);
EXPECT_TRUE(LoadTestPage(kDefaultTestURL));
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_FALSE(HasTag("normal"));
EXPECT_TRUE(Register("incognito"));
EXPECT_TRUE(RegistrationPending("incognito"));
// Switch back and make sure the registration is still there.
SetIncognitoMode(false);
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Should be controlled.
EXPECT_TRUE(HasTag("normal"));
EXPECT_FALSE(HasTag("incognito"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, GetTags) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
std::vector<std::string> registered_tags;
EXPECT_TRUE(GetTags(registered_tags));
background_sync_test_util::SetOnline(web_contents(), false);
registered_tags.push_back("foo");
registered_tags.push_back("bar");
for (const std::string& tag : registered_tags)
EXPECT_TRUE(Register(tag));
EXPECT_TRUE(GetTags(registered_tags));
}
// Verify that GetRegistrations works in a service worker
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
GetRegistrationsFromServiceWorker) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
std::vector<std::string> registered_tags;
EXPECT_TRUE(GetTags(registered_tags));
background_sync_test_util::SetOnline(web_contents(), false);
registered_tags.push_back("foo_sw");
registered_tags.push_back("bar_sw");
for (const std::string& tag : registered_tags) {
EXPECT_TRUE(RegisterFromServiceWorker(tag));
EXPECT_TRUE(PopConsole(BuildExpectedResult(tag, "registered in SW")));
}
EXPECT_TRUE(GetTagsFromServiceWorker(registered_tags));
}
// Verify that GetRegistration works in a service worker
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, HasTagFromServiceWorker) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
std::vector<std::string> registered_tags;
EXPECT_TRUE(GetTags(registered_tags));
background_sync_test_util::SetOnline(web_contents(), false);
EXPECT_TRUE(RegisterFromServiceWorker("foo_sw"));
EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW"));
EXPECT_TRUE(HasTagFromServiceWorker("foo_sw"));
}
// Verify that a background sync registration is deleted when site data is
// cleared.
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
SyncRegistrationDeletedWhenClearingSiteData) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
// Prevent firing by going offline.
background_sync_test_util::SetOnline(web_contents(), false);
EXPECT_TRUE(Register("foo"));
EXPECT_TRUE(HasTag("foo"));
EXPECT_TRUE(RegistrationPending("foo"));
// Simulate a user clearing site data (including Service Workers, crucially),
// by clearing data from the storage partition.
ClearStoragePartitionData();
EXPECT_FALSE(HasTag("foo"));
}
// Verify that a background sync registration, from a service worker, is deleted
// when site data is cleared.
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
SyncRegistrationFromSWDeletedWhenClearingSiteData) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
std::vector<std::string> registered_tags;
EXPECT_TRUE(GetTags(registered_tags));
background_sync_test_util::SetOnline(web_contents(), false);
EXPECT_TRUE(RegisterFromServiceWorker("foo_sw"));
EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW"));
EXPECT_TRUE(HasTagFromServiceWorker("foo_sw"));
// Simulate a user clearing site data (including Service Workers, crucially),
// by clearing data from the storage partition.
ClearStoragePartitionData();
EXPECT_FALSE(HasTagFromServiceWorker("foo"));
}
// Verify that multiple background sync registrations are deleted when site
// data is cleared.
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
SyncRegistrationsDeletedWhenClearingSiteData) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
std::vector<std::string> registered_tags;
EXPECT_TRUE(GetTags(registered_tags));
background_sync_test_util::SetOnline(web_contents(), false);
registered_tags.push_back("foo");
registered_tags.push_back("bar");
for (const std::string& tag : registered_tags)
EXPECT_TRUE(Register(tag));
EXPECT_TRUE(GetTags(registered_tags));
for (const std::string& tag : registered_tags)
EXPECT_TRUE(RegistrationPending(tag));
// Simulate a user clearing site data (including Service Workers, crucially),
// by clearing data from the storage partition.
ClearStoragePartitionData();
for (const std::string& tag : registered_tags)
EXPECT_FALSE(HasTag(tag));
}
// Verify that a sync event that is currently firing is deleted when site
// data is cleared.
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
FiringSyncEventDeletedWhenClearingSiteData) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
background_sync_test_util::SetOnline(web_contents(), true);
EXPECT_TRUE(Register("delay"));
// Verify that it is firing.
EXPECT_TRUE(HasTag("delay"));
EXPECT_FALSE(RegistrationPending("delay"));
// Simulate a user clearing site data (including Service Workers, crucially),
// by clearing data from the storage partition.
ClearStoragePartitionData();
// Verify that it was deleted.
EXPECT_FALSE(HasTag("delay"));
}
// Disabled due to flakiness. See https://crbug.com/578952.
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, DISABLED_VerifyRetry) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetMaxSyncAttempts(2);
EXPECT_TRUE(Register("delay"));
EXPECT_TRUE(RejectDelayedSyncEvent());
EXPECT_TRUE(PopConsole("ok - delay rejected"));
// Verify that the registration is still around and waiting to try again.
EXPECT_TRUE(RegistrationPending("delay"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, RegisterFromNonMainFrame) {
std::string script_result;
GURL url = https_server()->GetURL(kEmptyURL);
EXPECT_TRUE(RunScript(BuildScriptString("registerFromLocalFrame", url.spec()),
&script_result));
EXPECT_EQ(BuildExpectedResult("iframe", "failed to register sync"),
script_result);
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
RegisterFromServiceWorkerWithoutMainFrameHost) {
// Start a second https server to use as a second origin.
net::EmbeddedTestServer alt_server(net::EmbeddedTestServer::TYPE_HTTPS);
alt_server.ServeFilesFromSourceDirectory("content/test/data");
ASSERT_TRUE(alt_server.Start());
std::string script_result;
GURL url = alt_server.GetURL(kRegisterSyncURL);
EXPECT_TRUE(RunScript(
BuildScriptString("registerFromCrossOriginServiceWorker", url.spec()),
&script_result));
EXPECT_EQ(BuildExpectedResult("worker", "failed to register sync"),
script_result);
}
} // namespace content
|