notification.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. (function (root, factory) {
  2. define(["jquery", "jasmine", "mock", "converse-core", "test-utils", "utils"], factory);
  3. } (this, function ($, jasmine, mock, converse, test_utils, utils) {
  4. "use strict";
  5. var _ = converse.env._;
  6. var $msg = converse.env.$msg;
  7. describe("Notifications", function () {
  8. // Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
  9. describe("When show_desktop_notifications is set to true", function () {
  10. describe("And the desktop is not focused", function () {
  11. describe("an HTML5 Notification", function () {
  12. it("is shown when a new private message is received",
  13. mock.initConverseWithPromises(
  14. null, ['rosterGroupsFetched'], {},
  15. function (done, _converse) {
  16. // TODO: not yet testing show_desktop_notifications setting
  17. test_utils.createContacts(_converse, 'current');
  18. spyOn(_converse, 'showMessageNotification');
  19. spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
  20. spyOn(_converse, 'isMessageToHiddenChat').and.returnValue(true);
  21. var message = 'This message will show a desktop notification';
  22. var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  23. msg = $msg({
  24. from: sender_jid,
  25. to: _converse.connection.jid,
  26. type: 'chat',
  27. id: (new Date()).getTime()
  28. }).c('body').t(message).up()
  29. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  30. _converse.chatboxes.onMessage(msg); // This will emit 'message'
  31. expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
  32. expect(_converse.showMessageNotification).toHaveBeenCalled();
  33. done();
  34. }));
  35. it("is shown when you are mentioned in a chat room",
  36. mock.initConverseWithPromises(
  37. null, ['rosterGroupsFetched'], {},
  38. function (done, _converse) {
  39. test_utils.createContacts(_converse, 'current');
  40. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  41. var view = _converse.chatboxviews.get('lounge@localhost');
  42. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  43. var no_notification = false;
  44. if (typeof window.Notification === 'undefined') {
  45. no_notification = true;
  46. window.Notification = function () {
  47. return {
  48. 'close': function () {}
  49. };
  50. };
  51. }
  52. spyOn(_converse, 'showMessageNotification').and.callThrough();
  53. spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
  54. var message = 'dummy: This message will show a desktop notification';
  55. var nick = mock.chatroom_names[0],
  56. msg = $msg({
  57. from: 'lounge@localhost/'+nick,
  58. id: (new Date()).getTime(),
  59. to: 'dummy@localhost',
  60. type: 'groupchat'
  61. }).c('body').t(message).tree();
  62. _converse.chatboxes.onMessage(msg); // This will emit 'message'
  63. expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
  64. expect(_converse.showMessageNotification).toHaveBeenCalled();
  65. if (no_notification) {
  66. delete window.Notification;
  67. }
  68. done();
  69. });
  70. }));
  71. it("is shown for headline messages",
  72. mock.initConverseWithPromises(
  73. null, ['rosterGroupsFetched'], {},
  74. function (done, _converse) {
  75. spyOn(_converse, 'showMessageNotification').and.callThrough();
  76. spyOn(_converse, 'isMessageToHiddenChat').and.returnValue(true);
  77. spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
  78. var stanza = $msg({
  79. 'type': 'headline',
  80. 'from': 'notify.example.com',
  81. 'to': 'dummy@localhost',
  82. 'xml:lang': 'en'
  83. })
  84. .c('subject').t('SIEVE').up()
  85. .c('body').t('<juliet@example.com> You got mail.').up()
  86. .c('x', {'xmlns': 'jabber:x:oob'})
  87. .c('url').t('imap://romeo@example.com/INBOX;UIDVALIDITY=385759043/;UID=18');
  88. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  89. expect(
  90. _.includes(_converse.chatboxviews.keys(),
  91. 'notify.example.com')
  92. ).toBeTruthy();
  93. expect(_converse.showMessageNotification).toHaveBeenCalled();
  94. done();
  95. }));
  96. it("is not shown for full JID headline messages if allow_non_roster_messaging is false", mock.initConverse(function (_converse) {
  97. _converse.allow_non_roster_messaging = false;
  98. spyOn(_converse, 'showMessageNotification').and.callThrough();
  99. spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
  100. var stanza = $msg({
  101. 'type': 'headline',
  102. 'from': 'someone@notify.example.com',
  103. 'to': 'dummy@localhost',
  104. 'xml:lang': 'en'
  105. })
  106. .c('subject').t('SIEVE').up()
  107. .c('body').t('<juliet@example.com> You got mail.').up()
  108. .c('x', {'xmlns': 'jabber:x:oob'})
  109. .c('url').t('imap://romeo@example.com/INBOX;UIDVALIDITY=385759043/;UID=18');
  110. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  111. expect(
  112. _.includes(_converse.chatboxviews.keys(),
  113. 'someone@notify.example.com')
  114. ).toBeFalsy();
  115. expect(_converse.showMessageNotification).not.toHaveBeenCalled();
  116. }));
  117. it("is shown when a user changes their chat state (if show_chatstate_notifications is true)", mock.initConverse(function (_converse) {
  118. // TODO: not yet testing show_desktop_notifications setting
  119. _converse.show_chatstate_notifications = true;
  120. test_utils.createContacts(_converse, 'current');
  121. spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
  122. spyOn(_converse, 'showChatStateNotification');
  123. var jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  124. _converse.roster.get(jid).set('chat_status', 'busy'); // This will emit 'contactStatusChanged'
  125. expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
  126. expect(_converse.showChatStateNotification).toHaveBeenCalled();
  127. }));
  128. });
  129. });
  130. describe("When a new contact request is received", function () {
  131. it("an HTML5 Notification is received", mock.initConverse(function (_converse) {
  132. spyOn(_converse, 'areDesktopNotificationsEnabled').and.returnValue(true);
  133. spyOn(_converse, 'showContactRequestNotification');
  134. _converse.emit('contactRequest', {'fullname': 'Peter Parker', 'jid': 'peter@parker.com'});
  135. expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
  136. expect(_converse.showContactRequestNotification).toHaveBeenCalled();
  137. }));
  138. });
  139. });
  140. describe("When play_sounds is set to true", function () {
  141. describe("A notification sound", function () {
  142. it("is played when the current user is mentioned in a chat room",
  143. mock.initConverseWithPromises(
  144. null, ['rosterGroupsFetched'], {},
  145. function (done, _converse) {
  146. test_utils.createContacts(_converse, 'current');
  147. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  148. _converse.play_sounds = true;
  149. spyOn(_converse, 'playSoundNotification');
  150. var view = _converse.chatboxviews.get('lounge@localhost');
  151. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  152. var text = 'This message will play a sound because it mentions dummy';
  153. var message = $msg({
  154. from: 'lounge@localhost/otheruser',
  155. id: '1',
  156. to: 'dummy@localhost',
  157. type: 'groupchat'
  158. }).c('body').t(text);
  159. view.onChatRoomMessage(message.nodeTree);
  160. expect(_converse.playSoundNotification).toHaveBeenCalled();
  161. text = "This message won't play a sound";
  162. message = $msg({
  163. from: 'lounge@localhost/otheruser',
  164. id: '2',
  165. to: 'dummy@localhost',
  166. type: 'groupchat'
  167. }).c('body').t(text);
  168. view.onChatRoomMessage(message.nodeTree);
  169. expect(_converse.playSoundNotification, 1);
  170. _converse.play_sounds = false;
  171. text = "This message won't play a sound because it is sent by dummy";
  172. message = $msg({
  173. from: 'lounge@localhost/dummy',
  174. id: '3',
  175. to: 'dummy@localhost',
  176. type: 'groupchat'
  177. }).c('body').t(text);
  178. view.onChatRoomMessage(message.nodeTree);
  179. expect(_converse.playSoundNotification, 1);
  180. _converse.play_sounds = false;
  181. done();
  182. });
  183. }));
  184. });
  185. });
  186. });
  187. }));