Browse Source

Add a test to check that notifications are properly sent in chat rooms.

JC Brand 10 năm trước cách đây
mục cha
commit
d0b8a3870b
3 tập tin đã thay đổi với 57 bổ sung15 xóa
  1. 15 15
      converse.js
  2. 1 0
      docs/CHANGES.rst
  3. 41 0
      spec/chatroom.js

+ 15 - 15
converse.js

@@ -125,19 +125,6 @@
         return this;
     };
 
-    var playNotification = function () {
-        var audio;
-        if (converse.play_sounds && typeof Audio !== "undefined"){
-            audio = new Audio("sounds/msg_received.ogg");
-            if (audio.canPlayType('/audio/ogg')) {
-                audio.play();
-            } else {
-                audio = new Audio("/sounds/msg_received.mp3");
-                audio.play();
-                }
-            }
-    };
-
     var converse = {
         plugins: {},
         templates: templates,
@@ -359,6 +346,19 @@
 
         // Module-level functions
         // ----------------------
+        this.playNotification = function () {
+            var audio;
+            if (converse.play_sounds && typeof Audio !== "undefined"){
+                audio = new Audio("sounds/msg_received.ogg");
+                if (audio.canPlayType('/audio/ogg')) {
+                    audio.play();
+                } else {
+                    audio = new Audio("/sounds/msg_received.mp3");
+                    audio.play();
+                    }
+                }
+        };
+
         this.giveFeedback = function (message, klass) {
             $('.conn-feedback').each(function (idx, el) {
                 var $el = $(el);
@@ -2972,7 +2972,7 @@
                 }
                 this.model.createMessage($message);
                 if (!delayed && sender !== this.model.get('nick') && (new RegExp("\\b"+this.model.get('nick')+"\\b")).test(body)) {
-                    playNotification();
+                    converse.playNotification();
                 }
                 if (sender !== this.model.get('nick')) {
                     // We only emit an event if it's not our own message
@@ -3142,7 +3142,7 @@
                     return true; // We already have this message stored.
                 }
                 if (!this.isOnlyChatStateNotification($message) && from !== converse.bare_jid) {
-                    playNotification();
+                    converse.playNotification();
                 }
                 chatbox.receiveMessage($message);
                 converse.roster.addResource(contact_jid, resource);

+ 1 - 0
docs/CHANGES.rst

@@ -7,6 +7,7 @@ Changelog
 * Set the JID input field in the login form to ``type=email``. [chatme]
 * New configuration setting ``allow_contact_removal``. [jcbrand]
 * Document that event handlers receive 'event' obj as first arg. [jcbrand]
+* Add a test to check that notifications are played in chat rooms. [jcbrand] 
 
 0.9.0 (2015-03-06)
 ------------------

+ 41 - 0
spec/chatroom.js

@@ -176,6 +176,47 @@
                 expect(converse.emit).toHaveBeenCalledWith('message', message.nodeTree);
             }, converse));
 
+            it("plays a sound when the current user is mentioned (if configured)", $.proxy(function () {
+                test_utils.openChatRoom('lounge', 'localhost', 'dummy');
+                spyOn(converse, 'emit');
+                converse.play_sounds = true;
+                spyOn(converse, 'playNotification');
+                var view = this.chatboxviews.get('lounge@localhost');
+                if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
+                var nick = mock.chatroom_names[0];
+                var text = 'This message will play a sound because it mentions dummy';
+                var message = $msg({
+                    from: 'lounge@localhost/otheruser',
+                    id: '1',
+                    to: 'dummy@localhost',
+                    type: 'groupchat'
+                }).c('body').t(text);
+                view.onChatRoomMessage(message.nodeTree);
+                expect(converse.playNotification).toHaveBeenCalled();
+
+                text = "This message won't play a sound";
+                message = $msg({
+                    from: 'lounge@localhost/otheruser',
+                    id: '2',
+                    to: 'dummy@localhost',
+                    type: 'groupchat'
+                }).c('body').t(text);
+                view.onChatRoomMessage(message.nodeTree);
+                expect(converse.playNotification, 1);
+                converse.play_sounds = false;
+
+                text = "This message won't play a sound because it is sent by dummy";
+                message = $msg({
+                    from: 'lounge@localhost/dummy',
+                    id: '3',
+                    to: 'dummy@localhost',
+                    type: 'groupchat'
+                }).c('body').t(text);
+                view.onChatRoomMessage(message.nodeTree);
+                expect(converse.playNotification, 1);
+                converse.play_sounds = false;
+            }, converse));
+
             it("shows sent groupchat messages", $.proxy(function () {
                 test_utils.openChatRoom('lounge', 'localhost', 'dummy');
                 spyOn(converse, 'emit');