Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4472)

Unified Diff: chrome/test/data/extensions/api_test/service_worker/sync/page.js

Issue 1445603003: Extension SW - add test for ServiceWorkerRegistration.sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more foo Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3ba9272993d8d2abbb025fa3ad8b019f575097ea
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/service_worker/sync/page.js
@@ -0,0 +1,46 @@
+// 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';
+
+var startServiceWorker = new Promise(function(resolve, reject) {
+ var serviceWorker;
+ var serviceWorkerRegistration;
+ navigator.serviceWorker.register('sw.js').then(function() {
+ // Wait until the service worker is active.
+ return navigator.serviceWorker.ready;
+ }).then(function(registration) {
+ serviceWorker = registration.active;
+ serviceWorkerRegistration = registration;
+ return registration.sync.permissionState();
Devlin 2015/11/16 18:12:41 Could we just return the registration here, and av
lazyboy 2015/11/16 19:30:16 I want to check permissionState()'s return value a
+ }).then(function(syncPermissionState) {
+ if (syncPermissionState != 'granted') {
+ reject('permissionState: ' + syncPermissionState);
+ }
+ return serviceWorkerRegistration.sync.register({tag: 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('message: ' + e.data);
+ chrome.test.sendMessage('FAIL'); // Fails the test fast.
+ return;
+ }
+ chrome.test.sendMessage('' + e.data);
Devlin 2015/11/16 18:12:41 Does just sendMessage(e.data) not work (i.e., do y
lazyboy 2015/11/16 19:30:16 Don't need it, just being safe to force cast, in c
+ };
+ serviceWorker.postMessage('connect', [mc.port2]);
+ window.domAutomationController.send('SERVICE_WORKER_READY');
+ }).catch(function(err) {
+ window.domAutomationController.send(err);
+ });
+};

Powered by Google App Engine
This is Rietveld 408576698