Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #18709 from lissyx/bug963234
Browse files Browse the repository at this point in the history
Bug 963234 - Switch to mozNotificationEvent r=mhenretty,alive
  • Loading branch information
lissyx committed May 1, 2014
2 parents dcb0467 + 78f6aee commit 1b2f9f1
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 14 deletions.
41 changes: 34 additions & 7 deletions apps/system/js/notifications.js
Expand Up @@ -55,7 +55,9 @@ var NotificationScreen = {
vibrates: true,

init: function ns_init() {
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
window.addEventListener('mozChromeEvent', this);
window.addEventListener('mozChromeNotificationEvent', this);
this.container =
document.getElementById('desktop-notifications-container');
this.lockScreenContainer =
Expand Down Expand Up @@ -109,7 +111,9 @@ var NotificationScreen = {

handleEvent: function ns_handleEvent(evt) {
switch (evt.type) {
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
case 'mozChromeEvent':
case 'mozChromeNotificationEvent':
var detail = evt.detail;
switch (detail.type) {
case 'desktop-notification':
Expand Down Expand Up @@ -225,8 +229,16 @@ var NotificationScreen = {
var notificationNode = this.container.querySelector(
'[data-notification-id="' + notificationId + '"]');

// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
var contentEvent = document.createEvent('CustomEvent');
contentEvent.initCustomEvent('mozContentEvent', true, true, {
type: 'desktop-notification-click',
id: notificationId
});
window.dispatchEvent(contentEvent);

var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentEvent', true, true, {
event.initCustomEvent('mozContentNotificationEvent', true, true, {
type: 'desktop-notification-click',
id: notificationId
});
Expand All @@ -241,7 +253,7 @@ var NotificationScreen = {
// Desktop notifications are removed when they are clicked (see bug 890440)
if (notificationNode.dataset.type === 'desktop-notification' &&
notificationNode.dataset.obsoleteAPI === 'true') {
this.removeNotification(notificationId, false);
this.closeNotification(notificationNode);
}

if (node == this.toaster) {
Expand Down Expand Up @@ -360,8 +372,16 @@ var NotificationScreen = {
this.container.firstElementChild);
}

// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
var contentEvent = document.createEvent('CustomEvent');
contentEvent.initCustomEvent('mozContentEvent', true, true, {
type: 'desktop-notification-show',
id: detail.id
});
window.dispatchEvent(contentEvent);

var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentEvent', true, true, {
event.initCustomEvent('mozContentNotificationEvent', true, true, {
type: 'desktop-notification-show',
id: detail.id
});
Expand Down Expand Up @@ -459,17 +479,24 @@ var NotificationScreen = {

closeNotification: function ns_closeNotification(notificationNode) {
var notificationId = notificationNode.dataset.notificationId;
this.removeNotification(notificationNode.dataset.notificationId);
},
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
var contentEvent = document.createEvent('CustomEvent');
contentEvent.initCustomEvent('mozContentEvent', true, true, {
type: 'desktop-notification-close',
id: notificationId
});
window.dispatchEvent(contentEvent);

removeNotification: function ns_removeNotification(notificationId) {
var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentEvent', true, true, {
event.initCustomEvent('mozContentNotificationEvent', true, true, {
type: 'desktop-notification-close',
id: notificationId
});
window.dispatchEvent(event);
this.removeNotification(notificationId);
},

removeNotification: function ns_removeNotification(notificationId) {
var notifSelector = '[data-notification-id="' + notificationId + '"]';
var notificationNode = this.container.querySelector(notifSelector);
this.lockScreenContainer = this.lockScreenContainer ||
Expand Down
9 changes: 8 additions & 1 deletion apps/system/test/marionette/notification_get_test.js
Expand Up @@ -232,8 +232,15 @@ marionette('Notification.get():', function() {
marionetteScriptFinished('no node to query');
}
for (var i = nodes.length - 1; i >= 0; i--) {
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
var contentEvent = document.createEvent('CustomEvent');
contentEvent.initCustomEvent('mozContentEvent', true, true, {
type: 'desktop-notification-close',
id: nodes[i].dataset.notificationId
});
window.dispatchEvent(contentEvent);
var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentEvent', true, true, {
event.initCustomEvent('mozContentNotificationEvent', true, true, {
type: 'desktop-notification-close',
id: nodes[i].dataset.notificationId
});
Expand Down
84 changes: 78 additions & 6 deletions apps/system/test/unit/notifications_test.js
Expand Up @@ -82,16 +82,22 @@ suite('system/NotificationScreen >', function() {
this.sinon.stub(NotificationScreen, 'removeNotification');
});

function sendChromeEvent(detail) {
var event = new CustomEvent('mozChromeEvent', {
function sendChromeNotificationEvent(detail) {
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
var chromeEvent = new CustomEvent('mozChromeEvent', {
detail: detail
});

window.dispatchEvent(chromeEvent);
var event = new CustomEvent('mozChromeNotificationEvent', {
detail: detail
});

window.dispatchEvent(event);
}

test('showing a notification', function() {
sendChromeEvent({
sendChromeNotificationEvent({
type: 'desktop-notification',
id: 'id-1'
});
Expand All @@ -101,7 +107,7 @@ suite('system/NotificationScreen >', function() {
});

test('closing a notification', function() {
sendChromeEvent({
sendChromeNotificationEvent({
type: 'desktop-notification-close',
id: 'id-1'
});
Expand Down Expand Up @@ -243,7 +249,9 @@ suite('system/NotificationScreen >', function() {
});

suite('tap a notification >', function() {
var notificationNode, notifClickedStub, contentEventStub;
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
var notificationNode, notifClickedStub, contentEventStub,
contentNotificationEventStub;
var details = {
type: 'desktop-notification',
id: 'id-1',
Expand All @@ -255,41 +263,61 @@ suite('system/NotificationScreen >', function() {
notificationNode = NotificationScreen.addNotification(details);

notifClickedStub = sinon.stub();
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
contentEventStub = sinon.stub();
contentNotificationEventStub = sinon.stub();

window.addEventListener('notification-clicked', notifClickedStub);
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
window.addEventListener('mozContentEvent', contentEventStub);
window.addEventListener(
'mozContentNotificationEvent', contentNotificationEventStub);

var event = new CustomEvent('tap', { bubbles: true, cancelable: true });
notificationNode.dispatchEvent(event);
});

teardown(function() {
window.removeEventListener('notification-clicked', notifClickedStub);
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
window.removeEventListener('mozContentEvent', contentEventStub);
window.removeEventListener(
'mozContentNotificationEvent', contentNotificationEventStub);
});

test('dispatch events once with the expected parameters', function() {
sinon.assert.calledOnce(notifClickedStub);
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
sinon.assert.calledOnce(contentEventStub);
sinon.assert.calledOnce(contentNotificationEventStub);

sinon.assert.calledWithMatch(notifClickedStub, {
detail: {
id: details.id
}
});

// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
sinon.assert.calledWithMatch(contentEventStub, {
detail: {
type: 'desktop-notification-click',
id: details.id
}
});

sinon.assert.calledWithMatch(contentNotificationEventStub, {
detail: {
type: 'desktop-notification-click',
id: details.id
}
});
});
});

suite('tap a notification using the obsolete API >', function() {
var notificationNode, notifClickedStub, contentEventStub;
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
var notificationNode, notifClickedStub, contentEventStub,
contentNotificationEventStub;
var details = {
type: 'desktop-notification',
id: 'app-notif-1',
Expand All @@ -301,71 +329,115 @@ suite('system/NotificationScreen >', function() {
notificationNode = NotificationScreen.addNotification(details);

notifClickedStub = sinon.stub();
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
contentEventStub = sinon.stub();
contentNotificationEventStub = sinon.stub();

window.addEventListener('notification-clicked', notifClickedStub);
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
window.addEventListener('mozContentEvent', contentEventStub);
window.addEventListener(
'mozContentNotificationEvent', contentNotificationEventStub);
});

teardown(function() {
window.removeEventListener('notification-clicked', notifClickedStub);
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
window.removeEventListener('mozContentEvent', contentEventStub);
window.removeEventListener(
'mozContentNotificationEvent', contentNotificationEventStub);
});

test('tapping on the notification', function() {
var event = new CustomEvent('tap', { bubbles: true, cancelable: true });
notificationNode.dispatchEvent(event);

sinon.assert.calledOnce(notifClickedStub);
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
sinon.assert.calledTwice(contentEventStub);
sinon.assert.calledTwice(contentNotificationEventStub);

sinon.assert.calledWithMatch(notifClickedStub, {
detail: {
id: details.id
}
});

// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
sinon.assert.calledWithMatch(contentEventStub, {
detail: {
type: 'desktop-notification-click',
id: details.id
}
});

// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
sinon.assert.calledWithMatch(contentEventStub, {
detail: {
type: 'desktop-notification-close',
id: details.id
}
});

sinon.assert.calledWithMatch(contentNotificationEventStub, {
detail: {
type: 'desktop-notification-click',
id: details.id
}
});

sinon.assert.calledWithMatch(contentNotificationEventStub, {
detail: {
type: 'desktop-notification-close',
id: details.id
}
});
});

test('tapping on the toaster', function() {
var event = new CustomEvent('tap', { bubbles: true, cancelable: true });
fakeToaster.dispatchEvent(event);

sinon.assert.calledOnce(notifClickedStub);
// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
sinon.assert.calledTwice(contentEventStub);
sinon.assert.calledTwice(contentNotificationEventStub);

sinon.assert.calledWithMatch(notifClickedStub, {
detail: {
id: details.id
}
});

// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
sinon.assert.calledWithMatch(contentEventStub, {
detail: {
type: 'desktop-notification-click',
id: details.id
}
});

// FIXME: mozContentEvent to be removed once bug 963234 lands on gecko
sinon.assert.calledWithMatch(contentEventStub, {
detail: {
type: 'desktop-notification-close',
id: details.id
}
});

sinon.assert.calledWithMatch(contentNotificationEventStub, {
detail: {
type: 'desktop-notification-click',
id: details.id
}
});

sinon.assert.calledWithMatch(contentNotificationEventStub, {
detail: {
type: 'desktop-notification-close',
id: details.id
}
});
});
});

Expand Down

0 comments on commit 1b2f9f1

Please sign in to comment.