2016-03-08 11:42:52 +01:00
|
|
|
(function (root, factory) {
|
2018-01-04 12:41:03 +01:00
|
|
|
define(["jquery", "jasmine", "mock", "converse-core", "test-utils", "utils"], factory);
|
2018-01-03 20:02:05 +01:00
|
|
|
} (this, function ($, jasmine, mock, converse, test_utils, utils) {
|
2016-03-08 11:42:52 +01:00
|
|
|
"use strict";
|
2017-02-24 16:10:54 +01:00
|
|
|
var _ = converse.env._;
|
2016-12-20 10:30:20 +01:00
|
|
|
var $msg = converse.env.$msg;
|
2016-03-08 11:42:52 +01:00
|
|
|
|
|
|
|
describe("Notifications", function () {
|
|
|
|
// Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
|
|
|
|
|
|
|
|
describe("When show_desktop_notifications is set to true", function () {
|
|
|
|
describe("And the desktop is not focused", function () {
|
|
|
|
describe("an HTML5 Notification", function () {
|
|
|
|
|
2017-07-11 10:41:11 +02:00
|
|
|
it("is shown when a new private message is received",
|
|
|
|
mock.initConverseWithPromises(
|
|
|
|
null, ['rosterGroupsFetched'], {},
|
|
|
|
function (done, _converse) {
|
|
|
|
|
2016-03-08 11:42:52 +01:00
|
|
|
// TODO: not yet testing show_desktop_notifications setting
|
2016-12-20 10:30:20 +01:00
|
|
|
test_utils.createContacts(_converse, 'current');
|
|
|
|
spyOn(_converse, 'showMessageNotification');
|
2017-04-05 11:01:31 +02:00
|
|
|
spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
|
2017-11-01 10:37:24 +01:00
|
|
|
spyOn(_converse, 'isMessageToHiddenChat').and.returnValue(true);
|
2016-03-08 11:42:52 +01:00
|
|
|
|
|
|
|
var message = 'This message will show a desktop notification';
|
|
|
|
var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
|
|
|
|
msg = $msg({
|
|
|
|
from: sender_jid,
|
2016-12-20 10:30:20 +01:00
|
|
|
to: _converse.connection.jid,
|
2016-03-08 11:42:52 +01:00
|
|
|
type: 'chat',
|
|
|
|
id: (new Date()).getTime()
|
|
|
|
}).c('body').t(message).up()
|
|
|
|
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.chatboxes.onMessage(msg); // This will emit 'message'
|
|
|
|
expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
|
|
|
|
expect(_converse.showMessageNotification).toHaveBeenCalled();
|
2017-07-11 10:41:11 +02:00
|
|
|
done();
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2016-03-08 11:42:52 +01:00
|
|
|
|
2017-07-11 10:41:11 +02:00
|
|
|
it("is shown when you are mentioned in a chat room",
|
|
|
|
mock.initConverseWithPromises(
|
|
|
|
null, ['rosterGroupsFetched'], {},
|
|
|
|
function (done, _converse) {
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
test_utils.createContacts(_converse, 'current');
|
2017-07-12 09:55:43 +02:00
|
|
|
test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
|
|
|
|
var view = _converse.chatboxviews.get('lounge@localhost');
|
2018-01-03 20:02:05 +01:00
|
|
|
if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
|
2017-07-12 09:55:43 +02:00
|
|
|
var no_notification = false;
|
|
|
|
if (typeof window.Notification === 'undefined') {
|
|
|
|
no_notification = true;
|
|
|
|
window.Notification = function () {
|
|
|
|
return {
|
|
|
|
'close': function () {}
|
|
|
|
};
|
2016-04-28 16:51:50 +02:00
|
|
|
};
|
2017-07-12 09:55:43 +02:00
|
|
|
}
|
|
|
|
spyOn(_converse, 'showMessageNotification').and.callThrough();
|
|
|
|
spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
|
|
|
|
|
|
|
|
var message = 'dummy: This message will show a desktop notification';
|
|
|
|
var nick = mock.chatroom_names[0],
|
|
|
|
msg = $msg({
|
|
|
|
from: 'lounge@localhost/'+nick,
|
|
|
|
id: (new Date()).getTime(),
|
|
|
|
to: 'dummy@localhost',
|
|
|
|
type: 'groupchat'
|
|
|
|
}).c('body').t(message).tree();
|
|
|
|
_converse.chatboxes.onMessage(msg); // This will emit 'message'
|
|
|
|
expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
|
|
|
|
expect(_converse.showMessageNotification).toHaveBeenCalled();
|
|
|
|
if (no_notification) {
|
|
|
|
delete window.Notification;
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2016-04-28 16:51:50 +02:00
|
|
|
|
2017-07-11 10:41:11 +02:00
|
|
|
it("is shown for headline messages",
|
|
|
|
mock.initConverseWithPromises(
|
|
|
|
null, ['rosterGroupsFetched'], {},
|
|
|
|
function (done, _converse) {
|
|
|
|
|
2017-04-05 11:01:31 +02:00
|
|
|
spyOn(_converse, 'showMessageNotification').and.callThrough();
|
2017-11-01 10:37:24 +01:00
|
|
|
spyOn(_converse, 'isMessageToHiddenChat').and.returnValue(true);
|
2017-04-05 11:01:31 +02:00
|
|
|
spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
|
|
|
|
var stanza = $msg({
|
|
|
|
'type': 'headline',
|
|
|
|
'from': 'notify.example.com',
|
|
|
|
'to': 'dummy@localhost',
|
|
|
|
'xml:lang': 'en'
|
|
|
|
})
|
|
|
|
.c('subject').t('SIEVE').up()
|
|
|
|
.c('body').t('<juliet@example.com> You got mail.').up()
|
|
|
|
.c('x', {'xmlns': 'jabber:x:oob'})
|
|
|
|
.c('url').t('imap://romeo@example.com/INBOX;UIDVALIDITY=385759043/;UID=18');
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(stanza));
|
|
|
|
expect(
|
|
|
|
_.includes(_converse.chatboxviews.keys(),
|
|
|
|
'notify.example.com')
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(_converse.showMessageNotification).toHaveBeenCalled();
|
2017-07-11 10:41:11 +02:00
|
|
|
done();
|
2017-02-24 16:10:54 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
it("is not shown for full JID headline messages if allow_non_roster_messaging is false", mock.initConverse(function (_converse) {
|
|
|
|
_converse.allow_non_roster_messaging = false;
|
2017-04-05 11:01:31 +02:00
|
|
|
spyOn(_converse, 'showMessageNotification').and.callThrough();
|
|
|
|
spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
|
|
|
|
var stanza = $msg({
|
|
|
|
'type': 'headline',
|
|
|
|
'from': 'someone@notify.example.com',
|
|
|
|
'to': 'dummy@localhost',
|
|
|
|
'xml:lang': 'en'
|
|
|
|
})
|
|
|
|
.c('subject').t('SIEVE').up()
|
|
|
|
.c('body').t('<juliet@example.com> You got mail.').up()
|
|
|
|
.c('x', {'xmlns': 'jabber:x:oob'})
|
|
|
|
.c('url').t('imap://romeo@example.com/INBOX;UIDVALIDITY=385759043/;UID=18');
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(stanza));
|
|
|
|
expect(
|
|
|
|
_.includes(_converse.chatboxviews.keys(),
|
|
|
|
'someone@notify.example.com')
|
|
|
|
).toBeFalsy();
|
|
|
|
expect(_converse.showMessageNotification).not.toHaveBeenCalled();
|
2017-02-24 16:10:54 +01:00
|
|
|
}));
|
|
|
|
|
2017-02-14 14:39:22 +01:00
|
|
|
it("is shown when a user changes their chat state (if show_chatstate_notifications is true)", mock.initConverse(function (_converse) {
|
2016-03-08 11:42:52 +01:00
|
|
|
// TODO: not yet testing show_desktop_notifications setting
|
2017-02-14 14:39:22 +01:00
|
|
|
_converse.show_chatstate_notifications = true;
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
test_utils.createContacts(_converse, 'current');
|
2017-04-05 11:01:31 +02:00
|
|
|
spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
|
2016-12-20 10:30:20 +01:00
|
|
|
spyOn(_converse, 'showChatStateNotification');
|
2016-03-08 11:42:52 +01:00
|
|
|
var jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster.get(jid).set('chat_status', 'busy'); // This will emit 'contactStatusChanged'
|
|
|
|
expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
|
|
|
|
expect(_converse.showChatStateNotification).toHaveBeenCalled();
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2016-03-08 11:42:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("When a new contact request is received", function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
it("an HTML5 Notification is received", mock.initConverse(function (_converse) {
|
2017-04-05 11:01:31 +02:00
|
|
|
spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
|
2016-12-20 10:30:20 +01:00
|
|
|
spyOn(_converse, 'showContactRequestNotification');
|
|
|
|
_converse.emit('contactRequest', {'fullname': 'Peter Parker', 'jid': 'peter@parker.com'});
|
|
|
|
expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
|
|
|
|
expect(_converse.showContactRequestNotification).toHaveBeenCalled();
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2016-03-08 11:42:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("When play_sounds is set to true", function () {
|
|
|
|
describe("A notification sound", function () {
|
|
|
|
|
2017-07-11 10:41:11 +02:00
|
|
|
it("is played when the current user is mentioned in a chat room",
|
|
|
|
mock.initConverseWithPromises(
|
|
|
|
null, ['rosterGroupsFetched'], {},
|
|
|
|
function (done, _converse) {
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
test_utils.createContacts(_converse, 'current');
|
2017-07-12 09:55:43 +02:00
|
|
|
test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
|
|
|
|
_converse.play_sounds = true;
|
|
|
|
spyOn(_converse, 'playSoundNotification');
|
|
|
|
var view = _converse.chatboxviews.get('lounge@localhost');
|
2018-01-03 20:02:05 +01:00
|
|
|
if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
|
2017-07-12 09:55:43 +02:00
|
|
|
var text = 'This message will play a sound because it mentions dummy';
|
|
|
|
var message = $msg({
|
|
|
|
from: 'lounge@localhost/otheruser',
|
|
|
|
id: '1',
|
|
|
|
to: 'dummy@localhost',
|
|
|
|
type: 'groupchat'
|
|
|
|
}).c('body').t(text);
|
2018-04-08 19:44:53 +02:00
|
|
|
view.model.onMessage(message.nodeTree);
|
2017-07-12 09:55:43 +02:00
|
|
|
expect(_converse.playSoundNotification).toHaveBeenCalled();
|
|
|
|
|
|
|
|
text = "This message won't play a sound";
|
|
|
|
message = $msg({
|
|
|
|
from: 'lounge@localhost/otheruser',
|
|
|
|
id: '2',
|
|
|
|
to: 'dummy@localhost',
|
|
|
|
type: 'groupchat'
|
|
|
|
}).c('body').t(text);
|
2018-04-08 19:44:53 +02:00
|
|
|
view.model.onMessage(message.nodeTree);
|
2017-07-12 09:55:43 +02:00
|
|
|
expect(_converse.playSoundNotification, 1);
|
|
|
|
_converse.play_sounds = false;
|
|
|
|
|
|
|
|
text = "This message won't play a sound because it is sent by dummy";
|
|
|
|
message = $msg({
|
|
|
|
from: 'lounge@localhost/dummy',
|
|
|
|
id: '3',
|
|
|
|
to: 'dummy@localhost',
|
|
|
|
type: 'groupchat'
|
|
|
|
}).c('body').t(text);
|
2018-04-08 19:44:53 +02:00
|
|
|
view.model.onMessage(message.nodeTree);
|
2017-07-12 09:55:43 +02:00
|
|
|
expect(_converse.playSoundNotification, 1);
|
|
|
|
_converse.play_sounds = false;
|
|
|
|
done();
|
|
|
|
});
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2016-03-08 11:42:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|