notification.js 10 KB

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