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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
// 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 <set>
#include <string>
#include <vector>
#include "base/command_line.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_registration_handle.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/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 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 OneShotPendingCallback(
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 OneShotPendingDidGetSyncRegistration(
const base::Callback<void(bool)>& callback,
BackgroundSyncStatus error_type,
scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) {
ASSERT_EQ(BACKGROUND_SYNC_STATUS_OK, error_type);
callback.Run(registration_handle->sync_state() ==
BACKGROUND_SYNC_STATE_PENDING);
}
void OneShotPendingDidGetSWRegistration(
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 service_worker_id = registration->id();
BackgroundSyncManager* sync_manager = sync_context->background_sync_manager();
sync_manager->GetRegistration(
service_worker_id, tag, SYNC_ONE_SHOT,
base::Bind(&OneShotPendingDidGetSyncRegistration, callback));
}
void OneShotPendingOnIOThread(
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->FindRegistrationForDocument(
url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag,
callback));
}
} // namespace
class BackgroundSyncBrowserTest : public ContentBrowserTest {
public:
BackgroundSyncBrowserTest() {}
~BackgroundSyncBrowserTest() override {}
void SetUp() override {
BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(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();
}
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);
SetOnline(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(shell_->web_contents(),
script, result);
}
// This runs asynchronously on the IO thread, but we don't need to wait for it
// to complete before running a background sync operation, since those also
// run on the IO thread.
void SetOnline(bool online);
void SetOnlineOnIOThread(
const scoped_refptr<BackgroundSyncContext>& sync_context,
bool online);
// Returns true if the one-shot sync with tag is currently pending. Fails
// (assertion failure) if the tag isn't registered.
bool OneShotPending(const std::string& tag);
void ClearStoragePartitionData();
std::string PopConsoleString();
bool PopConsole(const std::string& expected_msg);
bool RegisterServiceWorker();
bool RegisterOneShot(const std::string& tag);
bool RegisterOneShotFromServiceWorker(const std::string& tag);
bool UnregisterOneShot(const std::string& tag);
bool UnregisterOneShotTwice(const std::string& tag);
bool GetRegistrationOneShot(const std::string& tag);
bool GetRegistrationOneShotFromServiceWorker(const std::string& tag);
bool MatchRegistrations(const std::string& script_result,
const std::vector<std::string>& expected_tags);
bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags);
bool GetRegistrationsOneShotFromServiceWorker(
const std::vector<std::string>& expected_tags);
bool CompleteDelayedOneShot();
bool RejectDelayedOneShot();
bool NotifyWhenFinishedOneShot(const std::string& tag);
bool NotifyWhenFinishedImmediateOneShot(const std::string& expected_msg);
bool StoreRegistrationOneShot(const std::string& tag);
private:
scoped_ptr<net::EmbeddedTestServer> https_server_;
Shell* shell_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest);
};
void BackgroundSyncBrowserTest::SetOnline(bool online) {
ASSERT_TRUE(shell_);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread,
base::Unretained(this), base::Unretained(GetSyncContext()),
online));
base::RunLoop().RunUntilIdle();
}
void BackgroundSyncBrowserTest::SetOnlineOnIOThread(
const scoped_refptr<BackgroundSyncContext>& sync_context,
bool online) {
BackgroundSyncManager* sync_manager = sync_context->background_sync_manager();
BackgroundSyncNetworkObserver* network_observer =
sync_manager->GetNetworkObserverForTesting();
if (online) {
network_observer->NotifyManagerIfNetworkChangedForTesting(
NetworkChangeNotifier::CONNECTION_WIFI);
} else {
network_observer->NotifyManagerIfNetworkChangedForTesting(
NetworkChangeNotifier::CONNECTION_NONE);
}
}
bool BackgroundSyncBrowserTest::OneShotPending(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(&OneShotPendingCallback, run_loop.QuitClosure(),
base::ThreadTaskRunnerHandle::Get(), &is_pending);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&OneShotPendingOnIOThread, 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::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 storage_partition_mask =
StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
uint32 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::RegisterOneShot(const std::string& tag) {
std::string script_result;
EXPECT_TRUE(
RunScript(BuildScriptString("registerOneShot", tag), &script_result));
return script_result == BuildExpectedResult(tag, "registered");
}
bool BackgroundSyncBrowserTest::RegisterOneShotFromServiceWorker(
const std::string& tag) {
std::string script_result;
EXPECT_TRUE(
RunScript(BuildScriptString("registerOneShotFromServiceWorker", tag),
&script_result));
return script_result == BuildExpectedResult(tag, "register sent to SW");
}
bool BackgroundSyncBrowserTest::UnregisterOneShot(const std::string& tag) {
std::string script_result;
EXPECT_TRUE(
RunScript(BuildScriptString("unregisterOneShot", tag), &script_result));
return script_result == BuildExpectedResult(tag, "unregistered");
}
bool BackgroundSyncBrowserTest::UnregisterOneShotTwice(const std::string& tag) {
std::string script_result;
EXPECT_TRUE(RunScript(BuildScriptString("unregisterOneShotTwice", tag),
&script_result));
return script_result ==
BuildExpectedResult(tag, "failed to unregister twice");
}
bool BackgroundSyncBrowserTest::GetRegistrationOneShot(const std::string& tag) {
std::string script_result;
EXPECT_TRUE(RunScript(BuildScriptString("getRegistrationOneShot", tag),
&script_result));
return script_result == BuildExpectedResult(tag, "found");
}
bool BackgroundSyncBrowserTest::GetRegistrationOneShotFromServiceWorker(
const std::string& tag) {
std::string script_result;
EXPECT_TRUE(RunScript(
BuildScriptString("getRegistrationOneShotFromServiceWorker", tag),
&script_result));
EXPECT_TRUE(script_result == "ok - getRegistration sent to SW");
return PopConsole(BuildExpectedResult(tag, "found"));
}
bool BackgroundSyncBrowserTest::MatchRegistrations(
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::GetRegistrationsOneShot(
const std::vector<std::string>& expected_tags) {
std::string script_result;
EXPECT_TRUE(RunScript("getRegistrationsOneShot()", &script_result));
return MatchRegistrations(script_result, expected_tags);
}
bool BackgroundSyncBrowserTest::GetRegistrationsOneShotFromServiceWorker(
const std::vector<std::string>& expected_tags) {
std::string script_result;
EXPECT_TRUE(
RunScript("getRegistrationsOneShotFromServiceWorker()", &script_result));
EXPECT_TRUE(script_result == "ok - getRegistrations sent to SW");
return MatchRegistrations(PopConsoleString(), expected_tags);
}
bool BackgroundSyncBrowserTest::CompleteDelayedOneShot() {
std::string script_result;
EXPECT_TRUE(RunScript("completeDelayedOneShot()", &script_result));
return script_result == BuildExpectedResult("delay", "completing");
}
bool BackgroundSyncBrowserTest::RejectDelayedOneShot() {
std::string script_result;
EXPECT_TRUE(RunScript("rejectDelayedOneShot()", &script_result));
return script_result == BuildExpectedResult("delay", "rejecting");
}
bool BackgroundSyncBrowserTest::NotifyWhenFinishedOneShot(
const std::string& tag) {
EXPECT_TRUE(content::ExecuteScript(
shell_->web_contents(),
BuildScriptString("notifyWhenFinishedOneShot", tag)));
return PopConsole(BuildExpectedResult(tag, "finished"));
}
bool BackgroundSyncBrowserTest::NotifyWhenFinishedImmediateOneShot(
const std::string& expected_msg) {
std::string script_result;
EXPECT_TRUE(
RunScript("notifyWhenFinishedImmediateOneShot()", &script_result));
return script_result == expected_msg;
}
bool BackgroundSyncBrowserTest::StoreRegistrationOneShot(
const std::string& tag) {
std::string script_result;
EXPECT_TRUE(
RunScript(BuildScriptString("storeRegistration", tag), &script_result));
return script_result == BuildExpectedResult(tag, "stored");
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotFires) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
EXPECT_TRUE(RegisterOneShot("foo"));
EXPECT_TRUE(PopConsole("foo fired"));
EXPECT_FALSE(GetRegistrationOneShot("foo"));
}
// Verify that Register works in a service worker
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
OneShotFromServiceWorkerFires) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw"));
EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW"));
EXPECT_TRUE(PopConsole("foo_sw fired"));
EXPECT_FALSE(GetRegistrationOneShot("foo_sw"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, OneShotDelaysForNetwork) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
// Prevent firing by going offline.
SetOnline(false);
EXPECT_TRUE(RegisterOneShot("foo"));
EXPECT_TRUE(GetRegistrationOneShot("foo"));
EXPECT_TRUE(OneShotPending("foo"));
// Resume firing by going online.
SetOnline(true);
EXPECT_TRUE(PopConsole("foo fired"));
EXPECT_FALSE(GetRegistrationOneShot("foo"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntil) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(true);
EXPECT_TRUE(RegisterOneShot("delay"));
// Verify that it is firing.
EXPECT_TRUE(GetRegistrationOneShot("delay"));
EXPECT_FALSE(OneShotPending("delay"));
// Complete the task.
EXPECT_TRUE(CompleteDelayedOneShot());
EXPECT_TRUE(PopConsole("ok - delay completed"));
// Verify that it finished firing.
// TODO(jkarlin): Use registration.finished to verify that the event actually
// completed successfully.
EXPECT_FALSE(GetRegistrationOneShot("delay"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, WaitUntilReject) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(true);
EXPECT_TRUE(RegisterOneShot("delay"));
// Verify that it is firing.
EXPECT_TRUE(GetRegistrationOneShot("delay"));
EXPECT_FALSE(OneShotPending("delay"));
// Complete the task.
EXPECT_TRUE(RejectDelayedOneShot());
EXPECT_TRUE(PopConsole("ok - delay rejected"));
EXPECT_FALSE(GetRegistrationOneShot("delay"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Incognito) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(false);
EXPECT_TRUE(RegisterOneShot("normal"));
EXPECT_TRUE(OneShotPending("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).
SetOnline(false);
EXPECT_TRUE(LoadTestPage(kDefaultTestURL));
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
EXPECT_FALSE(GetRegistrationOneShot("normal"));
EXPECT_TRUE(RegisterOneShot("incognito"));
EXPECT_TRUE(OneShotPending("incognito"));
// Switch back and make sure the registration is still there.
SetIncognitoMode(false);
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Should be controlled.
EXPECT_TRUE(GetRegistrationOneShot("normal"));
EXPECT_FALSE(GetRegistrationOneShot("incognito"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, GetRegistrations) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
std::vector<std::string> registered_tags;
EXPECT_TRUE(GetRegistrationsOneShot(registered_tags));
SetOnline(false);
registered_tags.push_back("foo");
registered_tags.push_back("bar");
for (const std::string& tag : registered_tags)
EXPECT_TRUE(RegisterOneShot(tag));
EXPECT_TRUE(GetRegistrationsOneShot(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(GetRegistrationsOneShot(registered_tags));
SetOnline(false);
registered_tags.push_back("foo_sw");
registered_tags.push_back("bar_sw");
for (const std::string& tag : registered_tags) {
EXPECT_TRUE(RegisterOneShotFromServiceWorker(tag));
EXPECT_TRUE(PopConsole(BuildExpectedResult(tag, "registered in SW")));
}
EXPECT_TRUE(GetRegistrationsOneShotFromServiceWorker(registered_tags));
}
// Verify that GetRegistration works in a service worker
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
GetRegistrationFromServiceWorker) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
std::vector<std::string> registered_tags;
EXPECT_TRUE(GetRegistrationsOneShot(registered_tags));
SetOnline(false);
EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw"));
EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW"));
EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, Unregister) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(false);
EXPECT_TRUE(RegisterOneShot("foo"));
EXPECT_TRUE(UnregisterOneShot("foo"));
EXPECT_FALSE(GetRegistrationOneShot("foo"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, UnregisterTwice) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(false);
EXPECT_TRUE(RegisterOneShot("foo"));
EXPECT_TRUE(UnregisterOneShotTwice("foo"));
EXPECT_FALSE(GetRegistrationOneShot("foo"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, UnregisterMidSync) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
EXPECT_TRUE(RegisterOneShot("unregister"));
EXPECT_TRUE(PopConsole("ok - unregister completed"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
CallFinishedBeforeSyncSucceeds) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(false);
EXPECT_TRUE(RegisterOneShot("foo"));
EXPECT_TRUE(NotifyWhenFinishedOneShot("foo"));
SetOnline(true);
// The ordering of PopConsole messages tells us that the event fired
// before finished resolved.
EXPECT_TRUE(PopConsole("foo fired"));
EXPECT_TRUE(PopConsole("foo finished result: true"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, CallFinishedBeforeSyncFails) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(true);
EXPECT_TRUE(RegisterOneShot("delay"));
EXPECT_FALSE(OneShotPending("delay"));
EXPECT_TRUE(NotifyWhenFinishedOneShot("delay"));
EXPECT_TRUE(RejectDelayedOneShot());
// The ordering of PopConsole messages tells us that the event fired
// before finished resolved.
EXPECT_TRUE(PopConsole("ok - delay rejected"));
EXPECT_TRUE(PopConsole("delay finished result: false"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
CallFinishedAfterSyncSucceeds) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(false);
EXPECT_TRUE(RegisterOneShot("foo"));
EXPECT_TRUE(StoreRegistrationOneShot("foo"));
SetOnline(true);
EXPECT_TRUE(PopConsole("foo fired"));
EXPECT_FALSE(GetRegistrationOneShot("foo"));
EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - foo result: true"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest,
CallFinishedAfterSyncUnregistered) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(false);
EXPECT_TRUE(RegisterOneShot("foo"));
EXPECT_TRUE(StoreRegistrationOneShot("foo"));
EXPECT_TRUE(UnregisterOneShot("foo"));
EXPECT_FALSE(GetRegistrationOneShot("foo"));
EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - foo result: false"));
}
IN_PROC_BROWSER_TEST_F(BackgroundSyncBrowserTest, CallFinishedAfterSyncFails) {
EXPECT_TRUE(RegisterServiceWorker());
EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
SetOnline(true);
EXPECT_TRUE(RegisterOneShot("delay"));
EXPECT_FALSE(OneShotPending("delay"));
EXPECT_TRUE(StoreRegistrationOneShot("delay"));
EXPECT_TRUE(RejectDelayedOneShot());
EXPECT_TRUE(PopConsole("ok - delay rejected"));
EXPECT_TRUE(NotifyWhenFinishedImmediateOneShot("ok - delay result: false"));
}
// 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.
SetOnline(false);
EXPECT_TRUE(RegisterOneShot("foo"));
EXPECT_TRUE(GetRegistrationOneShot("foo"));
EXPECT_TRUE(OneShotPending("foo"));
// Simulate a user clearing site data (including Service Workers, crucially),
// by clearing data from the storage partition.
ClearStoragePartitionData();
EXPECT_FALSE(GetRegistrationOneShot("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(GetRegistrationsOneShot(registered_tags));
SetOnline(false);
EXPECT_TRUE(RegisterOneShotFromServiceWorker("foo_sw"));
EXPECT_TRUE(PopConsole("ok - foo_sw registered in SW"));
EXPECT_TRUE(GetRegistrationOneShotFromServiceWorker("foo_sw"));
// Simulate a user clearing site data (including Service Workers, crucially),
// by clearing data from the storage partition.
ClearStoragePartitionData();
EXPECT_FALSE(GetRegistrationOneShotFromServiceWorker("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(GetRegistrationsOneShot(registered_tags));
SetOnline(false);
registered_tags.push_back("foo");
registered_tags.push_back("bar");
for (const std::string& tag : registered_tags)
EXPECT_TRUE(RegisterOneShot(tag));
EXPECT_TRUE(GetRegistrationsOneShot(registered_tags));
for (const std::string& tag : registered_tags)
EXPECT_TRUE(OneShotPending(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(GetRegistrationOneShot(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.
SetOnline(true);
EXPECT_TRUE(RegisterOneShot("delay"));
// Verify that it is firing.
EXPECT_TRUE(GetRegistrationOneShot("delay"));
EXPECT_FALSE(OneShotPending("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(GetRegistrationOneShot("delay"));
}
} // namespace content
|