notification.js 12 KB

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