From d128068889bb44b45a976e16b89e0bcaa688f24a Mon Sep 17 00:00:00 2001 From: Alexandre Lissy Date: Mon, 6 Jan 2014 13:31:45 +0100 Subject: [PATCH] Bug 938540 - Use Notification API for WapPush --- apps/system/js/notifications.js | 12 +---- apps/wappush/js/wappush.js | 39 +++++++++++----- apps/wappush/test/unit/wappush_test.js | 63 +++++++++++++++++++++++--- 3 files changed, 85 insertions(+), 29 deletions(-) diff --git a/apps/system/js/notifications.js b/apps/system/js/notifications.js index 0bda7b4bdac9..7fd097d59274 100644 --- a/apps/system/js/notifications.js +++ b/apps/system/js/notifications.js @@ -146,21 +146,11 @@ var NotificationScreen = { } }, - // TODO: Workaround for bug 929895 until bug 890440 is addressed - clearBlacklist: [ - window.location.protocol + '//wappush.gaiamobile.org/manifest.webapp' - ], - + // TODO: Remove this when we ditch mozNotification (bug 952453) handleAppopen: function ns_handleAppopen(evt) { var manifestURL = evt.detail.manifestURL, selector = '[data-manifest-u-r-l="' + manifestURL + '"]'; - var isBlacklisted = (this.clearBlacklist.indexOf(manifestURL) >= 0); - - if (isBlacklisted) { - return; - } - var nodes = this.container.querySelectorAll(selector); for (var i = nodes.length - 1; i >= 0; i--) { diff --git a/apps/wappush/js/wappush.js b/apps/wappush/js/wappush.js index 59c5237ee787..0cf633ea8acd 100644 --- a/apps/wappush/js/wappush.js +++ b/apps/wappush/js/wappush.js @@ -163,10 +163,7 @@ var WapPushManager = { req.onsuccess = (function wpm_gotApp(event) { var _ = navigator.mozL10n.get; var app = event.target.result; - /* We store the message timestamp as a parameter to be able to - * retrieve the message from the notification code */ - var iconURL = NotificationHelper.getIconURI(app) + - '?timestamp=' + encodeURIComponent(message.timestamp); + var iconURL = NotificationHelper.getIconURI(app); message.text = (message.type == 'text/vnd.wap.connectivity-xml') ? _(message.text) : message.text; @@ -174,11 +171,20 @@ var WapPushManager = { text += message.href ? message.href : ''; - NotificationHelper.send(message.sender, text, iconURL, - (function wpm_notificationOnClick() { - app.launch(); - this.displayWapPushMessage(message.timestamp); - }).bind(this)); + var options = { + icon: iconURL, + body: text, + tag: message.timestamp + }; + + var onClick = function wpm_notificationOnClick(timestamp) { + app.launch(); + this.displayWapPushMessage(timestamp); + }; + + var notification = new Notification(message.sender, options); + notification.addEventListener('click', + onClick.bind(this, options.tag)); this.finish(); }).bind(this); @@ -209,11 +215,10 @@ var WapPushManager = { this._closeTimeout = null; navigator.mozApps.getSelf().onsuccess = (function wpm_gotApp(event) { - var params = Utils.deserializeParameters(message.imageURL); var app = event.target.result; app.launch(); - this.displayWapPushMessage(params.timestamp); + this.displayWapPushMessage(message.tag); }).bind(this); }, @@ -225,6 +230,18 @@ var WapPushManager = { displayWapPushMessage: function wpm_displayWapPushMessage(timestamp) { ParsedMessage.load(timestamp, function wpm_loadSuccess(message) { + // Retrieve pending notifications and close the matching ones + Notification.get({tag: timestamp}).then( + function onSuccess(notifications) { + for (var i = 0; i < notifications.length; i++) { + notifications[i].close(); + } + }, + function onError(reason) { + console.error('Notification.get() promise error: ' + reason); + } + ); + if (message) { switch (message.type) { case 'text/vnd.wap.si': diff --git a/apps/wappush/test/unit/wappush_test.js b/apps/wappush/test/unit/wappush_test.js index 08b69a2fa1a0..8aa9e063f5a0 100644 --- a/apps/wappush/test/unit/wappush_test.js +++ b/apps/wappush/test/unit/wappush_test.js @@ -3,7 +3,8 @@ /* global loadBodyHTML, mocha, MockL10n, MockMessageDB, MockNavigatormozApps, MockNavigatormozSetMessageHandler, MockNavigatorSettings, - MockNotificationHelper, MocksHelper, ParsedMessage, WapPushManager */ + MockNotification, MockNotificationHelper, MocksHelper, ParsedMessage, + WapPushManager */ 'use strict'; @@ -13,6 +14,7 @@ requireApp('wappush/shared/test/unit/mocks/mock_navigator_moz_apps.js'); requireApp( 'wappush/shared/test/unit/mocks/mock_navigator_moz_set_message_handler.js' ); +requireApp('wappush/shared/test/unit/mocks/mock_notification.js'); requireApp('wappush/shared/test/unit/mocks/mock_notification_helper.js'); requireApp('wappush/shared/test/unit/mocks/mock_navigator_moz_settings.js'); @@ -32,6 +34,7 @@ var mocksHelperWapPush = new MocksHelper([ 'LinkActionHandler', 'MessageDB', 'NotificationHelper', + 'Notification', 'WhiteList' ]).init(); @@ -65,6 +68,20 @@ suite('WAP Push', function() { }); setup(function() { + var notificationGetStub = function notificationGet() { + var options = {}; + options.icon = 'icon'; + options.tag = '0'; + return { + then: function(onSuccess, onError, onProgress) { + onSuccess([ + new MockNotification('1', options) + ]); + } + }; + }; + this.sinon.stub(MockNotification, 'get', notificationGetStub); + mocksHelperWapPush.setup(); MockNavigatorSettings.createLock().set({ 'wap.push.enabled': 'true' }); loadBodyHTML('/index.html'); @@ -143,11 +160,17 @@ suite('WAP Push', function() { }); test('the notification is sent', function() { - var sendSpy = this.sinon.spy(MockNotificationHelper, 'send'); + var notificationSpy = this.sinon.spy(window, 'Notification'); + MockNavigatormozSetMessageHandler.mTrigger('wappush-received', message); + MockNavigatormozApps.mTriggerLastRequestSuccess(); + assert.isTrue(notificationSpy.calledOnce); + }); + test('Notification.get() called with correct tag', function() { MockNavigatormozSetMessageHandler.mTrigger('wappush-received', message); MockNavigatormozApps.mTriggerLastRequestSuccess(); - assert.isTrue(sendSpy.calledOnce); + WapPushManager.displayWapPushMessage(0); + assert.ok(Notification.get.calledWith({tag: 0})); }); test('the display is populated with the message contents', function() { @@ -170,6 +193,15 @@ suite('WAP Push', function() { assert.equal(link.dataset.url, 'http://www.mozilla.org'); assert.equal(link.href, 'http://www.mozilla.org/'); }); + + test('Notification is closed', function() { + var closeSpy = this.sinon.spy(MockNotification.prototype, 'close'); + + MockNavigatormozSetMessageHandler.mTrigger('wappush-received', message); + MockNavigatormozApps.mTriggerLastRequestSuccess(); + WapPushManager.displayWapPushMessage(0); + sinon.assert.calledOnce(closeSpy); + }); }); suite('receiving and displaying a CP message', function() { @@ -215,10 +247,17 @@ suite('WAP Push', function() { }); test('the notification is sent', function() { - var sendSpy = this.sinon.spy(MockNotificationHelper, 'send'); + var notificationSpy = this.sinon.spy(window, 'Notification'); MockNavigatormozSetMessageHandler.mTrigger('wappush-received', message); MockNavigatormozApps.mTriggerLastRequestSuccess(); - assert.isTrue(sendSpy.calledOnce); + assert.isTrue(notificationSpy.calledOnce); + }); + + test('Notification.get() called with correct tag', function() { + MockNavigatormozSetMessageHandler.mTrigger('wappush-received', message); + MockNavigatormozApps.mTriggerLastRequestSuccess(); + WapPushManager.displayWapPushMessage(0); + assert.ok(Notification.get.calledWith({tag: 0})); }); test('the display is populated with the message contents', function() { @@ -229,6 +268,7 @@ suite('WAP Push', function() { pin = screen.querySelector('input'); var retrieveSpy = this.sinon.spy(MockMessageDB, 'retrieve'); + MockNavigatormozSetMessageHandler.mTrigger('wappush-received', message); MockNavigatormozApps.mTriggerLastRequestSuccess(); WapPushManager.displayWapPushMessage(0); @@ -237,6 +277,15 @@ suite('WAP Push', function() { assert.equal(acceptButton.hidden, false); assert.equal(pin.type, 'hidden'); }); + + test('Notification is closed', function() { + var closeSpy = this.sinon.spy(MockNotification.prototype, 'close'); + + MockNavigatormozSetMessageHandler.mTrigger('wappush-received', message); + MockNavigatormozApps.mTriggerLastRequestSuccess(); + WapPushManager.displayWapPushMessage(0); + sinon.assert.called(closeSpy); + }); }); suite('handling out-of-order reception of messages', function() { @@ -310,11 +359,11 @@ suite('WAP Push', function() { }); test('an outdated message does not replace a newer one', function() { - var sendSpy = this.sinon.spy(MockNotificationHelper, 'send'); + var notificationSpy = this.sinon.spy(window, 'Notification'); MockNavigatormozSetMessageHandler.mTrigger('wappush-received', messages.old); MockMessageDB.put.yield('discarded'); - assert.isTrue(sendSpy.notCalled); + assert.isTrue(notificationSpy.notCalled); }); test('the current message is displayed', function() {