Index: chrome/test/data/extensions/api_test/service_worker/sync/page.js |
diff --git a/chrome/test/data/extensions/api_test/service_worker/sync/page.js b/chrome/test/data/extensions/api_test/service_worker/sync/page.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d46bc887d7a41d99e617db93f305ecd9dada700d |
--- /dev/null |
+++ b/chrome/test/data/extensions/api_test/service_worker/sync/page.js |
@@ -0,0 +1,41 @@ |
+// 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. |
+ |
+var SYNC_TAG = 'send-chats'; |
Devlin
2015/11/17 19:21:17
I would either use this variable at both line 16 a
lazyboy
2015/11/17 21:41:28
Done.
|
+ |
+var startServiceWorker = new Promise(function(resolve, reject) { |
jkarlin
2015/11/17 20:36:15
Perhaps rename to registerSyncOnServiceWorker sinc
lazyboy
2015/11/17 21:41:28
Done.
|
+ var serviceWorker; |
+ navigator.serviceWorker.register('sw.js').then(function() { |
+ // Wait until the service worker is active. |
+ return navigator.serviceWorker.ready; |
+ }).then(function(registration) { |
+ serviceWorker = registration.active; |
+ return registration; |
+ }).then(function(serviceWorkerRegistration) { |
jkarlin
2015/11/17 20:36:15
You don't need the above two lines. You can:
lazyboy
2015/11/17 21:41:28
Done.
|
+ return serviceWorkerRegistration.sync.register(SYNC_TAG); |
+ }).then(function() { |
+ resolve(serviceWorker); |
+ }).catch(function(err) { |
+ reject(err); |
+ }); |
+}); |
+ |
+window.runServiceWorker = function() { |
+ startServiceWorker.then(function(serviceWorker) { |
+ var mc = new MessageChannel(); |
+ // Called when ServiceWorker.onsync fires. |
+ mc.port1.onmessage = function(e) { |
+ if (e.data != 'SYNC: send-chats') { |
+ console.log('SW returned incorrect data: ' + e.data); |
+ chrome.test.sendMessage('FAIL'); // Fails the test fast. |
+ return; |
+ } |
+ chrome.test.sendMessage(e.data); |
+ }; |
+ serviceWorker.postMessage('connect', [mc.port2]); |
+ window.domAutomationController.send('SERVICE_WORKER_READY'); |
+ }).catch(function(err) { |
+ window.domAutomationController.send(err); |
+ }); |
+}; |