notification.js 12 KB

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