notification.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. (function (root, factory) {
  2. define(["mock", "converse-api", "test_utils"], factory);
  3. } (this, function (mock, converse, test_utils) {
  4. "use strict";
  5. var $msg = converse.env.$msg;
  6. describe("Notifications", function () {
  7. // Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
  8. describe("When show_desktop_notifications is set to true", function () {
  9. describe("And the desktop is not focused", function () {
  10. describe("an HTML5 Notification", function () {
  11. it("is shown when a new private message is received", mock.initConverse(function (_converse) {
  12. // TODO: not yet testing show_desktop_notifications setting
  13. test_utils.createContacts(_converse, 'current');
  14. spyOn(_converse, 'showMessageNotification');
  15. spyOn(_converse, 'areDesktopNotificationsEnabled').andReturn(true);
  16. var message = 'This message will show a desktop notification';
  17. var sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  18. msg = $msg({
  19. from: sender_jid,
  20. to: _converse.connection.jid,
  21. type: 'chat',
  22. id: (new Date()).getTime()
  23. }).c('body').t(message).up()
  24. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  25. _converse.chatboxes.onMessage(msg); // This will emit 'message'
  26. expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
  27. expect(_converse.showMessageNotification).toHaveBeenCalled();
  28. }));
  29. it("is shown when you are mentioned in a chat room", mock.initConverse(function (_converse) {
  30. test_utils.createContacts(_converse, 'current');
  31. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  32. var view = _converse.chatboxviews.get('lounge@localhost');
  33. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  34. var no_notification = false;
  35. if (typeof window.Notification === 'undefined') {
  36. no_notification = true;
  37. window.Notification = function () {
  38. return {
  39. 'close': function () {}
  40. };
  41. };
  42. }
  43. spyOn(_converse, 'showMessageNotification').andCallThrough();
  44. spyOn(_converse, 'areDesktopNotificationsEnabled').andReturn(true);
  45. var message = 'dummy: This message will show a desktop notification';
  46. var nick = mock.chatroom_names[0],
  47. msg = $msg({
  48. from: 'lounge@localhost/'+nick,
  49. id: (new Date()).getTime(),
  50. to: 'dummy@localhost',
  51. type: 'groupchat'
  52. }).c('body').t(message).tree();
  53. _converse.chatboxes.onMessage(msg); // This will emit 'message'
  54. expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
  55. expect(_converse.showMessageNotification).toHaveBeenCalled();
  56. if (no_notification) {
  57. delete window.Notification;
  58. }
  59. }));
  60. it("is shown when a user changes their chat state (if show_chatstate_notifications is true)", mock.initConverse(function (_converse) {
  61. // TODO: not yet testing show_desktop_notifications setting
  62. _converse.show_chatstate_notifications = true;
  63. test_utils.createContacts(_converse, 'current');
  64. spyOn(_converse, 'areDesktopNotificationsEnabled').andReturn(true);
  65. spyOn(_converse, 'showChatStateNotification');
  66. var jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  67. _converse.roster.get(jid).set('chat_status', 'busy'); // This will emit 'contactStatusChanged'
  68. expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
  69. expect(_converse.showChatStateNotification).toHaveBeenCalled();
  70. }));
  71. });
  72. });
  73. describe("When a new contact request is received", function () {
  74. it("an HTML5 Notification is received", mock.initConverse(function (_converse) {
  75. spyOn(_converse, 'areDesktopNotificationsEnabled').andReturn(true);
  76. spyOn(_converse, 'showContactRequestNotification');
  77. _converse.emit('contactRequest', {'fullname': 'Peter Parker', 'jid': 'peter@parker.com'});
  78. expect(_converse.areDesktopNotificationsEnabled).toHaveBeenCalled();
  79. expect(_converse.showContactRequestNotification).toHaveBeenCalled();
  80. }));
  81. });
  82. });
  83. describe("When play_sounds is set to true", function () {
  84. describe("A notification sound", function () {
  85. it("is played when the current user is mentioned in a chat room", mock.initConverse(function (_converse) {
  86. test_utils.createContacts(_converse, 'current');
  87. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  88. _converse.play_sounds = true;
  89. spyOn(_converse, 'playSoundNotification');
  90. var view = _converse.chatboxviews.get('lounge@localhost');
  91. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  92. var text = 'This message will play a sound because it mentions dummy';
  93. var message = $msg({
  94. from: 'lounge@localhost/otheruser',
  95. id: '1',
  96. to: 'dummy@localhost',
  97. type: 'groupchat'
  98. }).c('body').t(text);
  99. view.onChatRoomMessage(message.nodeTree);
  100. expect(_converse.playSoundNotification).toHaveBeenCalled();
  101. text = "This message won't play a sound";
  102. message = $msg({
  103. from: 'lounge@localhost/otheruser',
  104. id: '2',
  105. to: 'dummy@localhost',
  106. type: 'groupchat'
  107. }).c('body').t(text);
  108. view.onChatRoomMessage(message.nodeTree);
  109. expect(_converse.playSoundNotification, 1);
  110. _converse.play_sounds = false;
  111. text = "This message won't play a sound because it is sent by dummy";
  112. message = $msg({
  113. from: 'lounge@localhost/dummy',
  114. id: '3',
  115. to: 'dummy@localhost',
  116. type: 'groupchat'
  117. }).c('body').t(text);
  118. view.onChatRoomMessage(message.nodeTree);
  119. expect(_converse.playSoundNotification, 1);
  120. _converse.play_sounds = false;
  121. }));
  122. });
  123. });
  124. });
  125. }));