notification.js 12 KB

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