2016-03-08 11:42:52 +01:00
|
|
|
(function (root, factory) {
|
2019-04-06 16:31:42 +02:00
|
|
|
define(["jasmine", "mock", "test-utils"], factory);
|
|
|
|
} (this, function (jasmine, mock, test_utils) {
|
2016-03-08 11:42:52 +01:00
|
|
|
"use strict";
|
2018-08-16 14:19:36 +02:00
|
|
|
const Strophe = converse.env.Strophe,
|
|
|
|
_ = converse.env._,
|
|
|
|
$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",
|
2019-02-12 14:21:45 +01:00
|
|
|
mock.initConverse(
|
2017-07-11 10:41:11 +02:00
|
|
|
null, ['rosterGroupsFetched'], {},
|
2019-02-12 14:21:45 +01:00
|
|
|
async (done, _converse) => {
|
2017-07-11 10:41:11 +02:00
|
|
|
|
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');
|
2019-01-25 11:53:07 +01:00
|
|
|
await test_utils.createContacts(_converse, 'current');
|
2018-12-06 15:04:18 +01:00
|
|
|
spyOn(_converse, 'showMessageNotification').and.callThrough();
|
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);
|
2019-01-25 11:53:07 +01:00
|
|
|
|
|
|
|
const message = 'This message will show a desktop notification';
|
|
|
|
const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
|
2016-03-08 11:42:52 +01:00
|
|
|
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();
|
2019-01-25 11:53:07 +01:00
|
|
|
await _converse.chatboxes.onMessage(msg); // This will emit 'message'
|
|
|
|
await test_utils.waitUntil(() => _converse.api.chatviews.get(sender_jid));
|
2016-12-20 10:30:20 +01:00
|
|
|
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
|
|
|
|
2019-01-25 11:53:07 +01:00
|
|
|
it("is shown when you are mentioned in a groupchat",
|
2019-02-12 14:21:45 +01:00
|
|
|
mock.initConverse(
|
2017-07-11 10:41:11 +02:00
|
|
|
null, ['rosterGroupsFetched'], {},
|
2019-02-12 14:21:45 +01:00
|
|
|
async (done, _converse) => {
|
2019-01-25 11:53:07 +01:00
|
|
|
|
|
|
|
await test_utils.createContacts(_converse, 'current');
|
|
|
|
await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
|
|
|
|
const view = _converse.api.chatviews.get('lounge@localhost');
|
|
|
|
if (!view.el.querySelectorAll('.chat-area').length) {
|
|
|
|
view.renderChatArea();
|
|
|
|
}
|
|
|
|
let 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
|
|
|
};
|
2019-01-25 11:53:07 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
spyOn(_converse, 'showMessageNotification').and.callThrough();
|
|
|
|
spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
|
|
|
|
|
|
|
|
const message = 'dummy: This message will show a desktop notification';
|
|
|
|
const 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();
|
2019-02-13 15:24:24 +01:00
|
|
|
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(msg));
|
2019-01-25 11:53:07 +01:00
|
|
|
await new Promise((resolve, reject) => view.once('messageInserted', resolve));
|
|
|
|
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",
|
2019-02-12 14:21:45 +01:00
|
|
|
mock.initConverse(
|
2017-07-11 10:41:11 +02:00
|
|
|
null, ['rosterGroupsFetched'], {},
|
2019-02-12 14:21:45 +01:00
|
|
|
async (done, _converse) => {
|
2017-07-11 10:41:11 +02:00
|
|
|
|
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);
|
2019-01-25 11:53:07 +01:00
|
|
|
const stanza = $msg({
|
2017-04-05 11:01:31 +02:00
|
|
|
'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));
|
2019-01-25 11:53:07 +01:00
|
|
|
await test_utils.waitUntil(() => _converse.chatboxviews.keys().length);
|
|
|
|
const view = _converse.chatboxviews.get('notify.example.com');
|
|
|
|
await new Promise((resolve, reject) => view.once('messageInserted', resolve));
|
2017-04-05 11:01:31 +02:00
|
|
|
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
|
|
|
}));
|
|
|
|
|
2019-02-12 14:21:45 +01:00
|
|
|
it("is not shown for full JID headline messages if allow_non_roster_messaging is false", mock.initConverse((done, _converse) => {
|
2017-02-24 16:10:54 +01:00
|
|
|
_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);
|
2019-02-12 14:21:45 +01:00
|
|
|
const stanza = $msg({
|
2017-04-05 11:01:31 +02:00
|
|
|
'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();
|
2019-02-12 14:21:45 +01:00
|
|
|
done();
|
2017-02-24 16:10:54 +01:00
|
|
|
}));
|
|
|
|
|
2019-02-12 14:21:45 +01:00
|
|
|
it("is shown when a user changes their chat state (if show_chatstate_notifications is true)", mock.initConverse((done, _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');
|
2019-02-12 14:21:45 +01:00
|
|
|
const jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
|
2018-05-22 16:44:58 +02:00
|
|
|
_converse.roster.get(jid).presence.set('show', 'busy'); // This will emit 'contactStatusChanged'
|
2016-12-20 10:30:20 +01:00
|
|
|
expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
|
|
|
|
expect(_converse.showChatStateNotification).toHaveBeenCalled();
|
2019-02-12 14:21:45 +01:00
|
|
|
done()
|
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 () {
|
2019-02-12 14:21:45 +01:00
|
|
|
it("an HTML5 Notification is received", mock.initConverse((done, _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');
|
2019-03-29 21:10:45 +01:00
|
|
|
_converse.api.trigger('contactRequest', {'fullname': 'Peter Parker', 'jid': 'peter@parker.com'});
|
2016-12-20 10:30:20 +01:00
|
|
|
expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
|
|
|
|
expect(_converse.showContactRequestNotification).toHaveBeenCalled();
|
2019-02-12 14:21:45 +01:00
|
|
|
done();
|
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 () {
|
|
|
|
|
2019-01-25 11:53:07 +01:00
|
|
|
it("is played when the current user is mentioned in a groupchat",
|
2019-02-12 14:21:45 +01:00
|
|
|
mock.initConverse(
|
2017-07-11 10:41:11 +02:00
|
|
|
null, ['rosterGroupsFetched'], {},
|
2019-02-12 14:21:45 +01:00
|
|
|
async (done, _converse) => {
|
2017-07-11 10:41:11 +02:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
test_utils.createContacts(_converse, 'current');
|
2018-10-26 10:15:01 +02:00
|
|
|
await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
|
|
|
|
_converse.play_sounds = true;
|
|
|
|
spyOn(_converse, 'playSoundNotification');
|
|
|
|
const view = _converse.chatboxviews.get('lounge@localhost');
|
|
|
|
if (!view.el.querySelectorAll('.chat-area').length) {
|
|
|
|
view.renderChatArea();
|
|
|
|
}
|
|
|
|
let text = 'This message will play a sound because it mentions dummy';
|
|
|
|
let message = $msg({
|
|
|
|
from: 'lounge@localhost/otheruser',
|
|
|
|
id: '1',
|
|
|
|
to: 'dummy@localhost',
|
|
|
|
type: 'groupchat'
|
|
|
|
}).c('body').t(text);
|
2019-01-25 11:53:07 +01:00
|
|
|
await view.model.onMessage(message.nodeTree);
|
2018-10-26 10:15:01 +02:00
|
|
|
await test_utils.waitUntil(() => _converse.playSoundNotification.calls.count());
|
|
|
|
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);
|
2019-01-25 11:53:07 +01:00
|
|
|
await view.model.onMessage(message.nodeTree);
|
2018-10-26 10:15:01 +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);
|
2019-01-25 11:53:07 +01:00
|
|
|
await view.model.onMessage(message.nodeTree);
|
2018-10-26 10:15:01 +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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|