From 6078ab5924ed304c4e598902eac7d67787251757 Mon Sep 17 00:00:00 2001 From: witek Date: Wed, 8 May 2013 16:27:47 +0200 Subject: [PATCH 1/2] fixed message counter to be windowState aware. --- converse.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/converse.js b/converse.js index 28a7884e9..2b4af5e30 100644 --- a/converse.js +++ b/converse.js @@ -335,7 +335,7 @@ 'extra_classes': message.get('delayed') && 'delayed' || '' })); } - if (message.get('sender') != 'me') { + if ((message.get('sender') != 'me') && (converse.windowState == 'blur')) { converse.incrementMsgCounter(); } this.scrollDown(); @@ -480,7 +480,6 @@ if (this.model.get('status')) { this.showStatusMessage(this.model.get('status')); } - converse.clearMsgCounter(); }, template: _.template( @@ -956,7 +955,6 @@ this); this.$el.appendTo(converse.chatboxesview.$el); this.render().show().model.messages.fetch({add: true}); - converse.clearMsgCounter(); }, onLeave: function () { @@ -2121,6 +2119,14 @@ this.xmppstatus.initStatus(); }, this)); + + $(window).on("blur focus", $.proxy(function(e) { + if ((this.windowState != e.type) && (e.type == 'focus')) { + converse.clearMsgCounter(); + } + this.windowState = e.type; + },this)) + this.giveFeedback('Online Contacts'); }; From b84d4519a9f3615d59b6169a3d67f7f939804478 Mon Sep 17 00:00:00 2001 From: witek Date: Wed, 8 May 2013 17:42:19 +0200 Subject: [PATCH 2/2] add tests for message counter functionality --- spec/MainSpec.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/spec/MainSpec.js b/spec/MainSpec.js index 62604547c..9d2d05f01 100644 --- a/spec/MainSpec.js +++ b/spec/MainSpec.js @@ -551,6 +551,50 @@ }, converse)); }, converse)); + describe("A Message Counter", $.proxy(function () { + it("is incremented when the message is received and the window is not focused", $.proxy(function () { + expect(this.msg_counter).toBe(0); + spyOn(converse, 'incrementMsgCounter').andCallThrough(); + $(window).trigger('blur'); + var message = 'This message will increment the message counter'; + var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost'; + msg = $msg({ + from: sender_jid, + to: this.connection.jid, + type: 'chat', + id: (new Date()).getTime() + }).c('body').t(message).up() + .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); + this.chatboxes.messageReceived(msg); + expect(converse.incrementMsgCounter).toHaveBeenCalled(); + expect(this.msg_counter).toBe(1); + }, converse)); + + it("is cleared when the window is focused", $.proxy(function () { + spyOn(converse, 'clearMsgCounter').andCallThrough(); + $(window).trigger('focus'); + expect(converse.clearMsgCounter).toHaveBeenCalled(); + }, converse)); + + it("is not incremented when the message is received and the window is focused", $.proxy(function () { + expect(this.msg_counter).toBe(0); + spyOn(converse, 'incrementMsgCounter').andCallThrough(); + $(window).trigger('focus'); + var message = 'This message will not increment the message counter'; + var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost'; + msg = $msg({ + from: sender_jid, + to: this.connection.jid, + type: 'chat', + id: (new Date()).getTime() + }).c('body').t(message).up() + .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree(); + this.chatboxes.messageReceived(msg); + expect(converse.incrementMsgCounter).not.toHaveBeenCalled(); + expect(this.msg_counter).toBe(0); + }, converse)); + }, converse)); + xdescribe("The Controlbox Tabs", $.proxy(function () { // XXX: Disabled for now, these tests don't pass due to service // discovery changes.