From c3a41445609639c11369a84ff38769e788545769 Mon Sep 17 00:00:00 2001 From: Matt Gaunt Date: Thu, 31 Mar 2016 18:05:55 +0100 Subject: [PATCH] Moving stub to support actually running on FF if the appropriate API's work and moving back to Notification API to get FF support back on the roadmap --- README.md | 2 +- package.json | 2 +- src/client/push-client.js | 31 +- src/client/subscription-failed-error.js | 4 +- test/automated-suite.js | 32 +- test/browser-tests/index.html | 29 +- test/browser-tests/push-client/constructor.js | 112 +++ .../push-client/get-permission-state.js | 73 ++ .../push-client/get-registration.js | 69 ++ .../push-client/get-subscription.js | 104 +++ .../browser-tests/push-client/is-supported.js | 35 + .../{push-client-lib.js => lib-usage.js} | 0 .../push-client/push-client-methods.js | 702 ------------------ .../push-client/request-permission.js | 145 ++++ .../push-client/status-change-event.js | 119 +++ test/browser-tests/push-client/subscribe.js | 189 +++++ test/browser-tests/push-client/unsubscribe.js | 163 ++++ test/data/demos/just-subscribe.html | 2 +- test/data/demos/sw.js | 0 test/libs/helper-functions.js | 53 -- test/libs/is-supported.js | 24 + test/libs/state-stub-singleton.js | 51 ++ test/libs/state-stub.js | 82 -- test/libs/stubs/base-stub.js | 42 ++ test/libs/stubs/firefox-stub.js | 74 ++ test/libs/stubs/full-stub.js | 120 +++ 26 files changed, 1395 insertions(+), 864 deletions(-) create mode 100644 test/browser-tests/push-client/constructor.js create mode 100644 test/browser-tests/push-client/get-permission-state.js create mode 100644 test/browser-tests/push-client/get-registration.js create mode 100644 test/browser-tests/push-client/get-subscription.js create mode 100644 test/browser-tests/push-client/is-supported.js rename test/browser-tests/push-client/{push-client-lib.js => lib-usage.js} (100%) delete mode 100644 test/browser-tests/push-client/push-client-methods.js create mode 100644 test/browser-tests/push-client/request-permission.js create mode 100644 test/browser-tests/push-client/status-change-event.js create mode 100644 test/browser-tests/push-client/subscribe.js create mode 100644 test/browser-tests/push-client/unsubscribe.js create mode 100644 test/data/demos/sw.js delete mode 100644 test/libs/helper-functions.js create mode 100644 test/libs/is-supported.js create mode 100644 test/libs/state-stub-singleton.js delete mode 100644 test/libs/state-stub.js create mode 100644 test/libs/stubs/base-stub.js create mode 100644 test/libs/stubs/firefox-stub.js create mode 100644 test/libs/stubs/full-stub.js diff --git a/README.md b/README.md index d01a3fb..b869f13 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ To use the Propel library do the following: var PropelClient = window.goog.propel.PropelClient; // Check if push is supported by the current browsers - if (PropelClient.supported()) { + if (PropelClient.isSupported()) { // Initialise Push Client var propelClient = new PropelClient('/sw.js'); propelClient.addEventListener('statuschange', function(event) { diff --git a/package.json b/package.json index 53a5dbe..db27bba 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "require-dir": "^0.3.0", "rollupify": "^0.2.0", "run-sequence": "^1.1.5", - "selenium-webdriver": "^2.52.0", + "selenium-webdriver": "^2.53.1", "sinon": "^1.17.3", "sw-testing-helpers": "0.0.6", "vinyl-buffer": "^1.0.0", diff --git a/src/client/push-client.js b/src/client/push-client.js index 866d6f7..e23f50b 100644 --- a/src/client/push-client.js +++ b/src/client/push-client.js @@ -19,7 +19,6 @@ import EventDispatch from './event-dispatch'; const SUPPORTED = 'serviceWorker' in navigator && 'PushManager' in window && 'Notification' in window && - 'permissions' in navigator && 'showNotification' in ServiceWorkerRegistration.prototype; const ERROR_MESSAGES = { @@ -78,7 +77,7 @@ export default class PushClient extends EventDispatch { constructor(workerUrlOrRegistration, scope) { super(); - if (!PushClient.supported()) { + if (!PushClient.isSupported()) { throw new Error('Your browser does not support the web push API'); } @@ -106,14 +105,16 @@ export default class PushClient extends EventDispatch { _dispatchStatusUpdate() { return Promise.all([ - this.getSubscription(), + this.getSubscription().catch(() => { + return null; + }), PushClient.getPermissionState() ]) .then(results => { return { isSubscribed: (results[0] !== null), currentSubscription: results[0], - permissionState: results[1].state + permissionState: results[1] }; }) .then(status => { @@ -187,6 +188,12 @@ export default class PushClient extends EventDispatch { }) .then(() => { this._dispatchStatusUpdate(); + }) + .catch(err => { + return this._dispatchStatusUpdate() + .then(() => { + throw err; + }); }); } @@ -223,13 +230,17 @@ export default class PushClient extends EventDispatch { /** * Will manage requesting permission for push messages, resolving * with the final permission status. + * @param {Boolean} dispatchStatusChange - Optional parameter with a + * default value of true. If true, a `statuschange` event will be + * dispatched once the permission state has resolved (i.e. use interacted + * with the permission dialog). * @return {Promise} Permission status of granted, default or denied */ requestPermission(dispatchStatusChange = true) { return PushClient.getPermissionState() .then(permissionState => { // Check if requesting permission will show a prompt - if (permissionState.state === 'prompt') { + if (permissionState === 'default') { this.dispatchEvent(new PushClientEvent('requestingpermission')); } @@ -248,17 +259,19 @@ export default class PushClient extends EventDispatch { * @return {Boolean} Whether the current browser has everything needed * to use push messaging. */ - static supported() { + static isSupported() { return SUPPORTED; } /** * This method can be used to check if subscribing the user will display * the permission dialog or not. - * @return {Promise} PermistionStatus.state will be - * 'granted', 'denied' or 'prompt' to reflect the current permission state + * @return {Promise} PermistionStatus will be + * 'granted', 'denied' or 'default' to reflect the current permission state */ static getPermissionState() { - return navigator.permissions.query({name: 'push', userVisibleOnly: true}); + return new Promise(resolve => { + resolve(Notification.permission); + }); } } diff --git a/src/client/subscription-failed-error.js b/src/client/subscription-failed-error.js index 25fc29e..7383a90 100644 --- a/src/client/subscription-failed-error.js +++ b/src/client/subscription-failed-error.js @@ -13,8 +13,8 @@ const MESSAGES = { 'not supported': 'Your browser doesn\'t support push messaging.', - 'blocked': 'The user denied permission to show notifications.', - 'prompt': 'The user dismissed the notification permission dialog.', + 'denied': 'The user denied permission to show notifications.', + 'default': 'The user dismissed the notification permission dialog.', 'endpoint': 'No endpoint URL specified.', 'nogcmid': 'Please ensure you have a Web App Manifest with ' + 'a "gcm_sender_id" defined.' diff --git a/test/automated-suite.js b/test/automated-suite.js index 7dc4022..035195c 100644 --- a/test/automated-suite.js +++ b/test/automated-suite.js @@ -27,6 +27,7 @@ const path = require('path'); const swTestingHelpers = require('sw-testing-helpers'); const testServer = swTestingHelpers.testServer; const automatedBrowserTesting = swTestingHelpers.automatedBrowserTesting; +const seleniumFirefox = require('selenium-webdriver/firefox'); describe('Test Propel', function() { // Browser tests can be slow @@ -57,13 +58,34 @@ describe('Test Propel', function() { const queueUnitTest = browserInfo => { it(`should pass all tests in ${browserInfo.prettyName}`, () => { + + if (browserInfo.seleniumBrowserId === 'firefox') { + const ffProfile = new seleniumFirefox.Profile(); + ffProfile.setPreference('security.turn_off_all_security_so_that_viruses_can_take_over_this_computer', true); + browserInfo.seleniumOptions.setProfile(ffProfile); + } + globalDriverReference = browserInfo.getSeleniumDriver(); - return automatedBrowserTesting.runMochaTests( - browserInfo.prettyName, - globalDriverReference, - `${testServerURL}/test/browser-tests/` - ) + let initialisePromise = Promise.resolve(); + if (browserInfo.seleniumBrowserId === 'firefox') { + // H/T to web-push for this trick to get permissions accepted / denied + // https://github.com/marco-c/web-push + initialisePromise = globalDriverReference.executeScript(() => { + /* global window, Components */ + window.netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); + Components.utils.import('resource://gre/modules/Services.jsm'); + }); + } + + return initialisePromise + .then(() => { + return automatedBrowserTesting.runMochaTests( + browserInfo.prettyName, + globalDriverReference, + `${testServerURL}/test/browser-tests/` + ); + }) .then(testResults => { automatedBrowserTesting.manageTestResults( browserInfo.prettyName, diff --git a/test/browser-tests/index.html b/test/browser-tests/index.html index 82cbb62..fc980b0 100644 --- a/test/browser-tests/index.html +++ b/test/browser-tests/index.html @@ -33,18 +33,29 @@ + - - - + + + + + - - + + + + + + + + + +