2016-03-08 11:42:52 +01:00
|
|
|
(function (root, factory) {
|
2016-11-03 11:01:09 +01:00
|
|
|
define(["mock", "test_utils"], factory);
|
|
|
|
} (this, function (mock, test_utils) {
|
2016-03-08 11:42:52 +01:00
|
|
|
"use strict";
|
|
|
|
var $msg = converse_api.env.$msg;
|
|
|
|
|
|
|
|
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 () {
|
2016-11-03 11:01:09 +01:00
|
|
|
afterEach(function () {
|
|
|
|
converse_api.user.logout();
|
|
|
|
test_utils.clearBrowserStorage();
|
|
|
|
});
|
2016-03-08 11:42:52 +01:00
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
it("is shown when a new private message is received", mock.initConverse(function (converse) {
|
2016-03-08 11:42:52 +01:00
|
|
|
// TODO: not yet testing show_desktop_notifications setting
|
2016-11-03 11:01:09 +01:00
|
|
|
test_utils.createContacts(converse, 'current');
|
2016-03-08 11:42:52 +01:00
|
|
|
spyOn(converse, 'showMessageNotification');
|
|
|
|
spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true);
|
|
|
|
|
|
|
|
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,
|
|
|
|
to: converse.connection.jid,
|
|
|
|
type: 'chat',
|
|
|
|
id: (new Date()).getTime()
|
|
|
|
}).c('body').t(message).up()
|
|
|
|
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
|
|
|
|
converse.chatboxes.onMessage(msg); // This will emit 'message'
|
2016-03-08 12:01:23 +01:00
|
|
|
expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
|
2016-03-08 11:42:52 +01:00
|
|
|
expect(converse.showMessageNotification).toHaveBeenCalled();
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2016-03-08 11:42:52 +01:00
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
it("is shown when you are mentioned in a chat room", mock.initConverse(function (converse) {
|
|
|
|
test_utils.createContacts(converse, 'current');
|
|
|
|
test_utils.openAndEnterChatRoom(converse, 'lounge', 'localhost', 'dummy');
|
2016-04-28 16:51:50 +02:00
|
|
|
var view = converse.chatboxviews.get('lounge@localhost');
|
|
|
|
if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
|
|
|
|
var no_notification = false;
|
|
|
|
if (typeof window.Notification === 'undefined') {
|
|
|
|
no_notification = true;
|
|
|
|
window.Notification = function () {
|
|
|
|
return {
|
|
|
|
'close': function () {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
spyOn(converse, 'showMessageNotification').andCallThrough();
|
|
|
|
spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(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;
|
|
|
|
}
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2016-04-28 16:51:50 +02:00
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
it("is shown when a user changes their chat state", mock.initConverse(function (converse) {
|
2016-03-08 11:42:52 +01:00
|
|
|
// TODO: not yet testing show_desktop_notifications setting
|
2016-11-03 11:01:09 +01:00
|
|
|
test_utils.createContacts(converse, 'current');
|
2016-03-08 11:42:52 +01:00
|
|
|
spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true);
|
|
|
|
spyOn(converse, 'showChatStateNotification');
|
|
|
|
var jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
|
|
|
|
converse.roster.get(jid).set('chat_status', 'busy'); // This will emit 'contactStatusChanged'
|
2016-03-08 12:01:23 +01:00
|
|
|
expect(converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
|
2016-03-08 11:42:52 +01:00
|
|
|
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-11-03 11:01:09 +01:00
|
|
|
it("an HTML5 Notification is received", mock.initConverse(function (converse) {
|
2016-03-08 12:01:23 +01:00
|
|
|
spyOn(converse, 'areDesktopNotificationsEnabled').andReturn(true);
|
|
|
|
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 () {
|
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
afterEach(function () {
|
|
|
|
converse_api.user.logout();
|
|
|
|
test_utils.clearBrowserStorage();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is played when the current user is mentioned in a chat room", mock.initConverse(function (converse) {
|
|
|
|
test_utils.createContacts(converse, 'current');
|
|
|
|
test_utils.openAndEnterChatRoom(converse, 'lounge', 'localhost', 'dummy');
|
2016-03-08 11:42:52 +01:00
|
|
|
converse.play_sounds = true;
|
|
|
|
spyOn(converse, 'playSoundNotification');
|
2016-11-03 11:01:09 +01:00
|
|
|
var view = converse.chatboxviews.get('lounge@localhost');
|
2016-03-08 11:42:52 +01:00
|
|
|
if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
|
|
|
|
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);
|
|
|
|
view.onChatRoomMessage(message.nodeTree);
|
|
|
|
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);
|
|
|
|
view.onChatRoomMessage(message.nodeTree);
|
|
|
|
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);
|
|
|
|
view.onChatRoomMessage(message.nodeTree);
|
|
|
|
expect(converse.playSoundNotification, 1);
|
|
|
|
converse.play_sounds = false;
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2016-03-08 11:42:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|