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";
|
2019-07-11 22:50:30 +02:00
|
|
|
const _ = converse.env._;
|
|
|
|
const $msg = converse.env.$msg;
|
|
|
|
const u = converse.env.utils;
|
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
|
|
|
|
2019-08-23 21:34:53 +02:00
|
|
|
await test_utils.waitForRoster(_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';
|
2019-06-03 07:58:51 +02:00
|
|
|
const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit',
|
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'
|
2019-07-11 22:50:30 +02:00
|
|
|
await u.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');
|
2019-07-03 14:18:46 +02:00
|
|
|
await test_utils.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
|
2019-06-03 07:58:51 +02:00
|
|
|
const view = _converse.api.chatviews.get('lounge@montague.lit');
|
2019-01-25 11:53:07 +01:00
|
|
|
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);
|
|
|
|
|
2019-06-03 07:58:51 +02:00
|
|
|
const message = 'romeo: This message will show a desktop notification';
|
2019-01-25 11:53:07 +01:00
|
|
|
const nick = mock.chatroom_names[0],
|
|
|
|
msg = $msg({
|
2019-06-03 07:58:51 +02:00
|
|
|
from: 'lounge@montague.lit/'+nick,
|
2019-01-25 11:53:07 +01:00
|
|
|
id: (new Date()).getTime(),
|
2019-06-03 07:58:51 +02:00
|
|
|
to: 'romeo@montague.lit',
|
2019-01-25 11:53:07 +01:00
|
|
|
type: 'groupchat'
|
|
|
|
}).c('body').t(message).tree();
|
2019-02-13 15:24:24 +01:00
|
|
|
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(msg));
|
2019-10-09 16:01:38 +02:00
|
|
|
await new Promise(resolve => view.once('messageInserted', resolve));
|
2019-01-25 11:53:07 +01:00
|
|
|
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',
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'romeo@montague.lit',
|
2017-04-05 11:01:31 +02:00
|
|
|
'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-07-11 22:50:30 +02:00
|
|
|
await u.waitUntil(() => _converse.chatboxviews.keys().length);
|
2019-01-25 11:53:07 +01:00
|
|
|
const view = _converse.chatboxviews.get('notify.example.com');
|
2019-10-09 16:01:38 +02:00
|
|
|
await new Promise(resolve => 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',
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'romeo@montague.lit',
|
2017-04-05 11:01:31 +02:00
|
|
|
'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-08-03 21:49:44 +02:00
|
|
|
it("is shown when a user changes their chat state (if show_chat_state_notifications is true)", mock.initConverse((done, _converse) => {
|
2016-03-08 11:42:52 +01:00
|
|
|
// TODO: not yet testing show_desktop_notifications setting
|
2019-08-03 21:49:44 +02:00
|
|
|
_converse.show_chat_state_notifications = true;
|
2017-02-14 14:39:22 +01:00
|
|
|
|
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-06-03 07:58:51 +02:00
|
|
|
const jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
|
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');
|
2019-07-03 14:18:46 +02:00
|
|
|
await test_utils.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
|
2018-10-26 10:15:01 +02:00
|
|
|
_converse.play_sounds = true;
|
|
|
|
spyOn(_converse, 'playSoundNotification');
|
2019-06-03 07:58:51 +02:00
|
|
|
const view = _converse.chatboxviews.get('lounge@montague.lit');
|
2018-10-26 10:15:01 +02:00
|
|
|
if (!view.el.querySelectorAll('.chat-area').length) {
|
|
|
|
view.renderChatArea();
|
|
|
|
}
|
2019-06-03 07:58:51 +02:00
|
|
|
let text = 'This message will play a sound because it mentions romeo';
|
2018-10-26 10:15:01 +02:00
|
|
|
let message = $msg({
|
2019-06-03 07:58:51 +02:00
|
|
|
from: 'lounge@montague.lit/otheruser',
|
2018-10-26 10:15:01 +02:00
|
|
|
id: '1',
|
2019-06-03 07:58:51 +02:00
|
|
|
to: 'romeo@montague.lit',
|
2018-10-26 10:15:01 +02:00
|
|
|
type: 'groupchat'
|
|
|
|
}).c('body').t(text);
|
2019-01-25 11:53:07 +01:00
|
|
|
await view.model.onMessage(message.nodeTree);
|
2019-07-11 22:50:30 +02:00
|
|
|
await u.waitUntil(() => _converse.playSoundNotification.calls.count());
|
2018-10-26 10:15:01 +02:00
|
|
|
expect(_converse.playSoundNotification).toHaveBeenCalled();
|
|
|
|
|
|
|
|
text = "This message won't play a sound";
|
|
|
|
message = $msg({
|
2019-06-03 07:58:51 +02:00
|
|
|
from: 'lounge@montague.lit/otheruser',
|
2018-10-26 10:15:01 +02:00
|
|
|
id: '2',
|
2019-06-03 07:58:51 +02:00
|
|
|
to: 'romeo@montague.lit',
|
2018-10-26 10:15:01 +02:00
|
|
|
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;
|
|
|
|
|
2019-06-03 07:58:51 +02:00
|
|
|
text = "This message won't play a sound because it is sent by romeo";
|
2018-10-26 10:15:01 +02:00
|
|
|
message = $msg({
|
2019-06-03 07:58:51 +02:00
|
|
|
from: 'lounge@montague.lit/romeo',
|
2018-10-26 10:15:01 +02:00
|
|
|
id: '3',
|
2019-06-03 07:58:51 +02:00
|
|
|
to: 'romeo@montague.lit',
|
2018-10-26 10:15:01 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|