3 <title>Background Sync API: Verifies that the one-shot sync API works
5 <script src=
"../resources/testharness.js"></script>
6 <script src=
"../resources/testharness-helpers.js"></script>
7 <script src=
"../resources/testharnessreport.js"></script>
8 <script src=
"../serviceworker/resources/test-helpers.js"></script>
9 <script src=
"resources/test-helpers.js"></script>
12 promise_test(function(t
) {
13 var url
= 'resources/empty_worker.js';
14 var scope
= 'resources/scope/background_sync/oneshot.html';
16 var sync_registration
;
18 // One-shot syncs can only be registered from a controlled document. This
19 // test creates a frame, after the service worker is active, in order to use
20 // its service worker registration.
21 return service_worker_unregister_and_register(t
, url
, scope
)
22 .then(function(sw_registration_page
) {
23 return wait_for_state(t
, sw_registration_page
.installing
, 'activated');
26 return with_iframe(scope
)
28 .then(function(frame
) {
29 var w
= frame
.contentWindow
;
30 return w
.navigator
.serviceWorker
.getRegistration(scope
);
32 .then(function(sw_registration_frame
) {
33 sync_manager
= sw_registration_frame
.sync
;
34 return clear_registered_syncs(sync_manager
);
36 .then(function() { return sync_manager
.getRegistrations(); })
37 .then(function(registrations
) {
38 assert_equals(registrations
.length
, 0, 'One-shot syncs should be ' +
39 'cleared at the start of the test.');
40 return sync_manager
.register({tag
: 'abcde'});
42 .then(function(registration
) {
43 sync_registration
= registration
;
44 assert_class_string(sync_registration
, 'SyncRegistration', 'One-' +
45 'shot sync registrations should have the correct ' +
47 assert_equals('abcde', registration
.tag
, 'Sync registration tag ' +
48 'returned should match the tag registered.');
49 return service_worker_unregister(t
, scope
);
51 }, 'Background Sync API should allow one-shot syncs to be registered from ' +
52 'the Document scope');
54 promise_test(function(t
) {
55 var url
= 'resources/empty_worker.js';
56 var scope
= 'resources/scope/background_sync/oneshot-uncontrolled.html';
58 var sync_registration
;
60 // One-shot syncs can also be registered from uncontrolled documents. This
61 // test creates a frame, after the service worker is active, in order to use
62 // its service worker registration.
63 return service_worker_unregister_and_register(t
, url
, scope
)
64 .then(function(sw_registration
) {
65 sync_manager
= sw_registration
.sync
;
66 return wait_for_state(t
, sw_registration
.installing
, 'activated');
68 .then(function() { return clear_registered_syncs(sync_manager
); })
69 .then(function() { return sync_manager
.getRegistrations(); })
70 .then(function(registrations
) {
71 assert_equals(registrations
.length
, 0, 'One-shot syncs should be ' +
72 'cleared at the start of the test.');
73 return sync_manager
.register({tag
: 'abcde'});
75 .then(function(registration
) {
76 sync_registration
= registration
;
77 assert_class_string(sync_registration
, 'SyncRegistration', 'One-' +
78 'shot sync registrations should have the correct ' +
80 assert_equals('abcde', registration
.tag
, 'Sync registration tag ' +
81 'returned should match the tag registered.');
82 return service_worker_unregister(t
, scope
);
84 .then(function() { return service_worker_unregister(t
, scope
); })
85 }, 'Background Sync API should allow one-shot syncs to be registered ' +
86 'with window clients not currently controlled by service worker');