2
0

chatbox.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. (function (root, factory) {
  2. define([
  3. "mock",
  4. "utils"
  5. ], function (mock, utils) {
  6. return factory(mock, utils);
  7. }
  8. );
  9. } (this, function (mock, utils) {
  10. return describe("Chatboxes", $.proxy(function(mock, utils) {
  11. describe("A Chatbox", $.proxy(function () {
  12. beforeEach(function () {
  13. runs(function () {
  14. utils.closeAllChatBoxes();
  15. utils.removeControlBox();
  16. });
  17. waits(250);
  18. runs(function () {
  19. converse.roster.localStorage._clear();
  20. utils.initConverse();
  21. utils.createCurrentContacts();
  22. utils.openControlBox();
  23. });
  24. waits(250);
  25. runs(function () {
  26. utils.openContactsPanel();
  27. });
  28. waits(250);
  29. runs(function () {});
  30. });
  31. it("is created when you click on a roster item", $.proxy(function () {
  32. var i, $el, click, jid, view;
  33. // openControlBox was called earlier, so the controlbox is
  34. // visible, but no other chat boxes have been created.
  35. expect(this.chatboxes.length).toEqual(1);
  36. var online_contacts = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat');
  37. for (i=0; i<online_contacts.length; i++) {
  38. $el = $(online_contacts[i]);
  39. jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
  40. view = this.rosterview.get(jid);
  41. spyOn(view, 'openChat').andCallThrough();
  42. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  43. $el.click();
  44. expect(view.openChat).toHaveBeenCalled();
  45. expect(this.chatboxes.length).toEqual(i+2);
  46. }
  47. }, converse));
  48. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  49. spyOn(converse, 'emit');
  50. runs(function () {
  51. utils.closeControlBox();
  52. });
  53. waits(250);
  54. runs(function () {
  55. expect(converse.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
  56. utils.openChatBoxes(6);
  57. // We instantiate a new ChatBoxes collection, which by default
  58. // will be empty.
  59. var newchatboxes = new this.ChatBoxes();
  60. expect(newchatboxes.length).toEqual(0);
  61. // The chatboxes will then be fetched from localStorage inside the
  62. // onConnected method
  63. newchatboxes.onConnected();
  64. expect(newchatboxes.length).toEqual(6);
  65. // Check that the chatboxes items retrieved from localStorage
  66. // have the same attributes values as the original ones.
  67. attrs = ['id', 'box_id', 'visible'];
  68. for (i=0; i<attrs.length; i++) {
  69. new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
  70. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  71. expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
  72. }
  73. this.rosterview.render();
  74. }.bind(converse));
  75. }, converse));
  76. it("can be closed by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  77. var chatbox = utils.openChatBoxes(1)[0],
  78. controlview = this.chatboxviews.get('controlbox'), // The controlbox is currently open
  79. chatview = this.chatboxviews.get(chatbox.get('jid'));
  80. spyOn(chatview, 'closeChat').andCallThrough();
  81. spyOn(controlview, 'closeChat').andCallThrough();
  82. spyOn(converse, 'emit');
  83. // We need to rebind all events otherwise our spy won't be called
  84. controlview.delegateEvents();
  85. chatview.delegateEvents();
  86. runs(function () {
  87. controlview.$el.find('.close-chatbox-button').click();
  88. });
  89. waits(250);
  90. runs(function () {
  91. expect(controlview.closeChat).toHaveBeenCalled();
  92. expect(converse.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
  93. expect(converse.emit.callCount, 1);
  94. chatview.$el.find('.close-chatbox-button').click();
  95. });
  96. waits(250);
  97. runs(function () {
  98. expect(chatview.closeChat).toHaveBeenCalled();
  99. expect(converse.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
  100. expect(converse.emit.callCount, 2);
  101. });
  102. }, converse));
  103. it("can be toggled by clicking a DOM element with class 'toggle-chatbox-button'", function () {
  104. var chatbox = utils.openChatBoxes(1)[0],
  105. chatview = this.chatboxviews.get(chatbox.get('jid'));
  106. spyOn(chatview, 'toggleChatBox').andCallThrough();
  107. spyOn(converse, 'emit');
  108. // We need to rebind all events otherwise our spy won't be called
  109. chatview.delegateEvents();
  110. runs(function () {
  111. chatview.$el.find('.toggle-chatbox-button').click();
  112. });
  113. waits(250);
  114. runs(function () {
  115. expect(chatview.toggleChatBox).toHaveBeenCalled();
  116. expect(converse.emit).toHaveBeenCalledWith('onChatBoxToggled', jasmine.any(Object));
  117. expect(converse.emit.callCount, 2);
  118. expect(chatview.$el.find('.chat-body').is(':visible')).toBeFalsy();
  119. expect(chatview.$el.find('.toggle-chatbox-button').hasClass('icon-minus')).toBeFalsy();
  120. expect(chatview.$el.find('.toggle-chatbox-button').hasClass('icon-plus')).toBeTruthy();
  121. expect(chatview.model.get('minimized')).toBeTruthy();
  122. chatview.$el.find('.toggle-chatbox-button').click();
  123. });
  124. waits(250);
  125. runs(function () {
  126. expect(chatview.toggleChatBox).toHaveBeenCalled();
  127. expect(converse.emit).toHaveBeenCalledWith('onChatBoxToggled', jasmine.any(Object));
  128. expect(chatview.$el.find('.chat-body').is(':visible')).toBeTruthy();
  129. expect(chatview.$el.find('.toggle-chatbox-button').hasClass('icon-minus')).toBeTruthy();
  130. expect(chatview.$el.find('.toggle-chatbox-button').hasClass('icon-plus')).toBeFalsy();
  131. expect(chatview.model.get('minimized')).toBeFalsy();
  132. expect(converse.emit.callCount, 3);
  133. });
  134. }.bind(converse));
  135. it("will be removed from localStorage when closed", $.proxy(function () {
  136. spyOn(converse, 'emit');
  137. this.chatboxes.localStorage._clear();
  138. runs(function () {
  139. utils.closeControlBox();
  140. });
  141. waits(250);
  142. runs(function () {
  143. expect(converse.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
  144. expect(converse.chatboxes.length).toEqual(0);
  145. utils.openChatBoxes(6);
  146. expect(converse.chatboxes.length).toEqual(6);
  147. expect(converse.emit).toHaveBeenCalledWith('onChatBoxOpened', jasmine.any(Object));
  148. utils.closeAllChatBoxes();
  149. });
  150. waits(250);
  151. runs(function () {
  152. expect(converse.chatboxes.length).toEqual(0);
  153. expect(converse.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
  154. var newchatboxes = new this.ChatBoxes();
  155. expect(newchatboxes.length).toEqual(0);
  156. // onConnected will fetch chatboxes in localStorage, but
  157. // because there aren't any open chatboxes, there won't be any
  158. // in localStorage either.
  159. newchatboxes.onConnected();
  160. expect(newchatboxes.length).toEqual(0);
  161. }.bind(converse));
  162. }, converse));
  163. describe("A chat toolbar", $.proxy(function () {
  164. it("can be found on each chat box", $.proxy(function () {
  165. var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
  166. utils.openChatBoxFor(contact_jid);
  167. var chatbox = this.chatboxes.get(contact_jid);
  168. var view = this.chatboxviews.get(contact_jid);
  169. expect(chatbox).toBeDefined();
  170. expect(view).toBeDefined();
  171. var $toolbar = view.$el.find('ul.chat-toolbar');
  172. expect($toolbar.length).toBe(1);
  173. expect($toolbar.children('li').length).toBe(3);
  174. }, converse));
  175. it("contains a button for inserting emoticons", $.proxy(function () {
  176. var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
  177. utils.openChatBoxFor(contact_jid);
  178. var chatbox = this.chatboxes.get(contact_jid);
  179. var view = this.chatboxviews.get(contact_jid);
  180. var $toolbar = view.$el.find('ul.chat-toolbar');
  181. var $textarea = view.$el.find('textarea.chat-textarea');
  182. expect($toolbar.children('li.toggle-smiley').length).toBe(1);
  183. // Register spies
  184. spyOn(view, 'toggleEmoticonMenu').andCallThrough();
  185. spyOn(view, 'insertEmoticon').andCallThrough();
  186. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  187. runs(function () {
  188. $toolbar.children('li.toggle-smiley').click();
  189. });
  190. waits(250);
  191. runs(function () {
  192. expect(view.toggleEmoticonMenu).toHaveBeenCalled();
  193. var $menu = view.$el.find('.toggle-smiley ul');
  194. var $items = $menu.children('li');
  195. expect($menu.is(':visible')).toBeTruthy();
  196. expect($items.length).toBe(13);
  197. expect($($items[0]).children('a').data('emoticon')).toBe(':)');
  198. expect($($items[1]).children('a').data('emoticon')).toBe(';)');
  199. expect($($items[2]).children('a').data('emoticon')).toBe(':D');
  200. expect($($items[3]).children('a').data('emoticon')).toBe(':P');
  201. expect($($items[4]).children('a').data('emoticon')).toBe('8)');
  202. expect($($items[5]).children('a').data('emoticon')).toBe('>:)');
  203. expect($($items[6]).children('a').data('emoticon')).toBe(':S');
  204. expect($($items[7]).children('a').data('emoticon')).toBe(':\\');
  205. expect($($items[8]).children('a').data('emoticon')).toBe('>:(');
  206. expect($($items[9]).children('a').data('emoticon')).toBe(':(');
  207. expect($($items[10]).children('a').data('emoticon')).toBe(':O');
  208. expect($($items[11]).children('a').data('emoticon')).toBe('(^.^)b');
  209. expect($($items[12]).children('a').data('emoticon')).toBe('<3');
  210. $items[0].click();
  211. });
  212. waits(250);
  213. runs(function () {
  214. expect(view.insertEmoticon).toHaveBeenCalled();
  215. expect($textarea.val()).toBe(':) ');
  216. expect(view.$el.find('.toggle-smiley ul').is(':visible')).toBeFalsy();
  217. $toolbar.children('li.toggle-smiley').click();
  218. });
  219. waits(250);
  220. runs(function () {
  221. expect(view.toggleEmoticonMenu).toHaveBeenCalled();
  222. expect(view.$el.find('.toggle-smiley ul').is(':visible')).toBeTruthy();
  223. view.$el.find('.toggle-smiley ul').children('li').last().click();
  224. });
  225. waits(250);
  226. runs(function () {
  227. expect(view.insertEmoticon).toHaveBeenCalled();
  228. expect(view.$el.find('.toggle-smiley ul').is(':visible')).toBeFalsy();
  229. expect($textarea.val()).toBe(':) <3 ');
  230. });
  231. }, converse));
  232. it("contains a button for starting an encrypted chat session", $.proxy(function () {
  233. // TODO: More tests can be added here...
  234. var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
  235. utils.openChatBoxFor(contact_jid);
  236. var chatbox = this.chatboxes.get(contact_jid);
  237. var view = this.chatboxviews.get(contact_jid);
  238. var $toolbar = view.$el.find('ul.chat-toolbar');
  239. expect($toolbar.children('li.toggle-otr').length).toBe(1);
  240. // Register spies
  241. spyOn(view, 'toggleOTRMenu').andCallThrough();
  242. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  243. runs(function () {
  244. $toolbar.children('li.toggle-otr').click();
  245. });
  246. waits(250);
  247. runs(function () {
  248. expect(view.toggleOTRMenu).toHaveBeenCalled();
  249. var $menu = view.$el.find('.toggle-otr ul');
  250. expect($menu.is(':visible')).toBeTruthy();
  251. expect($menu.children('li').length).toBe(2);
  252. });
  253. }, converse));
  254. it("contains a button for starting a call", $.proxy(function () {
  255. spyOn(converse, 'emit');
  256. var contact_jid = mock.cur_names[2].replace(' ','.').toLowerCase() + '@localhost';
  257. utils.openChatBoxFor(contact_jid);
  258. var chatbox = this.chatboxes.get(contact_jid);
  259. var view = this.chatboxviews.get(contact_jid);
  260. var $toolbar = view.$el.find('ul.chat-toolbar');
  261. var callButton = $toolbar.find('.toggle-call');
  262. expect(callButton.length).toBe(1);
  263. runs(function () {
  264. callButton.click();
  265. expect(converse.emit).toHaveBeenCalledWith('onCallButtonClicked', jasmine.any(Object));
  266. });
  267. }, converse));
  268. }, converse));
  269. describe("A Chat Message", $.proxy(function () {
  270. beforeEach(function () {
  271. runs(function () {
  272. utils.closeAllChatBoxes();
  273. });
  274. waits(250);
  275. runs(function () {});
  276. });
  277. it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
  278. spyOn(converse, 'emit');
  279. var message = 'This is a received message';
  280. var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  281. msg = $msg({
  282. from: sender_jid,
  283. to: this.connection.jid,
  284. type: 'chat',
  285. id: (new Date()).getTime()
  286. }).c('body').t(message).up()
  287. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  288. // We don't already have an open chatbox for this user
  289. expect(this.chatboxes.get(sender_jid)).not.toBeDefined();
  290. runs($.proxy(function () {
  291. // onMessage is a handler for received XMPP messages
  292. this.chatboxes.onMessage(msg);
  293. expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
  294. }, converse));
  295. waits(300);
  296. runs($.proxy(function () {
  297. // Check that the chatbox and its view now exist
  298. var chatbox = this.chatboxes.get(sender_jid);
  299. var chatboxview = this.chatboxviews.get(sender_jid);
  300. expect(chatbox).toBeDefined();
  301. expect(chatboxview).toBeDefined();
  302. // Check that the message was received and check the
  303. // message parameters
  304. expect(chatbox.messages.length).toEqual(1);
  305. var msg_obj = chatbox.messages.models[0];
  306. expect(msg_obj.get('message')).toEqual(message);
  307. // XXX: This is stupid, fullname is actually only the
  308. // users first name
  309. expect(msg_obj.get('fullname')).toEqual(mock.cur_names[0].split(' ')[0]);
  310. expect(msg_obj.get('sender')).toEqual('them');
  311. expect(msg_obj.get('delayed')).toEqual(false);
  312. // Now check that the message appears inside the
  313. // chatbox in the DOM
  314. var $chat_content = chatboxview.$el.find('.chat-content');
  315. var msg_txt = $chat_content.find('.chat-message').find('.chat-message-content').text();
  316. expect(msg_txt).toEqual(message);
  317. var sender_txt = $chat_content.find('span.chat-message-them').text();
  318. expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy();
  319. }, converse));
  320. }, converse));
  321. it("will indate when it has a time difference of more than a day between it and it's predecessor", $.proxy(function () {
  322. spyOn(converse, 'emit');
  323. var contact_name = mock.cur_names[1];
  324. var contact_jid = contact_name.replace(' ','.').toLowerCase() + '@localhost';
  325. utils.openChatBoxFor(contact_jid);
  326. utils.clearChatBoxMessages(contact_jid);
  327. var one_day_ago = new Date(new Date().setDate(new Date().getDate()-1));
  328. var message = 'This is a day old message';
  329. var chatbox = this.chatboxes.get(contact_jid);
  330. var chatboxview = this.chatboxviews.get(contact_jid);
  331. var $chat_content = chatboxview.$el.find('.chat-content');
  332. var msg_obj;
  333. var msg_txt;
  334. var sender_txt;
  335. var msg = $msg({
  336. from: contact_jid,
  337. to: this.connection.jid,
  338. type: 'chat',
  339. id: one_day_ago.getTime()
  340. }).c('body').t(message).up()
  341. .c('delay', { xmlns:'urn:xmpp:delay', from: 'localhost', stamp: converse.toISOString(one_day_ago) })
  342. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  343. this.chatboxes.onMessage(msg);
  344. expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
  345. expect(chatbox.messages.length).toEqual(1);
  346. msg_obj = chatbox.messages.models[0];
  347. expect(msg_obj.get('message')).toEqual(message);
  348. expect(msg_obj.get('fullname')).toEqual(contact_name.split(' ')[0]);
  349. expect(msg_obj.get('sender')).toEqual('them');
  350. expect(msg_obj.get('delayed')).toEqual(true);
  351. msg_txt = $chat_content.find('.chat-message').find('.chat-message-content').text();
  352. expect(msg_txt).toEqual(message);
  353. sender_txt = $chat_content.find('span.chat-message-them').text();
  354. expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy();
  355. message = 'This is a current message';
  356. msg = $msg({
  357. from: contact_jid,
  358. to: this.connection.jid,
  359. type: 'chat',
  360. id: new Date().getTime()
  361. }).c('body').t(message).up()
  362. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  363. this.chatboxes.onMessage(msg);
  364. expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
  365. // Check that there is a <time> element, with the required
  366. // props.
  367. var $time = $chat_content.find('time');
  368. var message_date = new Date();
  369. message_date.setUTCHours(0,0,0,0);
  370. expect($time.length).toEqual(1);
  371. expect($time.attr('class')).toEqual('chat-date');
  372. expect($time.attr('datetime')).toEqual(converse.toISOString(message_date));
  373. expect($time.text()).toEqual(message_date.toString().substring(0,15));
  374. // Normal checks for the 2nd message
  375. expect(chatbox.messages.length).toEqual(2);
  376. msg_obj = chatbox.messages.models[1];
  377. expect(msg_obj.get('message')).toEqual(message);
  378. expect(msg_obj.get('fullname')).toEqual(contact_name.split(' ')[0]);
  379. expect(msg_obj.get('sender')).toEqual('them');
  380. expect(msg_obj.get('delayed')).toEqual(false);
  381. msg_txt = $chat_content.find('.chat-message').last().find('.chat-message-content').text();
  382. expect(msg_txt).toEqual(message);
  383. sender_txt = $chat_content.find('span.chat-message-them').last().text();
  384. expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy();
  385. }, converse));
  386. it("can be sent from a chatbox, and will appear inside it", $.proxy(function () {
  387. spyOn(converse, 'emit');
  388. var contact_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  389. runs(function () {
  390. utils.openChatBoxFor(contact_jid);
  391. });
  392. waits(250);
  393. runs(function () {
  394. expect(converse.emit).toHaveBeenCalledWith('onChatBoxFocused', jasmine.any(Object));
  395. var view = this.chatboxviews.get(contact_jid);
  396. var message = 'This message is sent from this chatbox';
  397. spyOn(view, 'sendMessage').andCallThrough();
  398. view.$el.find('.chat-textarea').text(message);
  399. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  400. expect(view.sendMessage).toHaveBeenCalled();
  401. expect(view.model.messages.length, 2);
  402. expect(converse.emit.mostRecentCall.args, ['onMessageSend', message]);
  403. var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text();
  404. expect(txt).toEqual(message);
  405. }.bind(converse));
  406. }, converse));
  407. it("are sanitized to prevent Javascript injection attacks", $.proxy(function () {
  408. var contact_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  409. utils.openChatBoxFor(contact_jid);
  410. var view = this.chatboxviews.get(contact_jid);
  411. var message = 'This message contains <b>markup</b>';
  412. spyOn(view, 'sendMessage').andCallThrough();
  413. view.$el.find('.chat-textarea').text(message);
  414. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  415. expect(view.sendMessage).toHaveBeenCalled();
  416. var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text();
  417. expect(txt).toEqual(message);
  418. }, converse));
  419. }, converse));
  420. }, converse));
  421. describe("Special Messages", $.proxy(function () {
  422. it("'/clear' can be used to clear messages in a conversation", $.proxy(function () {
  423. spyOn(converse, 'emit');
  424. var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  425. var view = this.chatboxviews.get(contact_jid);
  426. var message = 'This message is another sent from this chatbox';
  427. // Lets make sure there is at least one message already
  428. // (e.g for when this test is run on its own).
  429. view.$el.find('.chat-textarea').val(message).text(message);
  430. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  431. expect(view.model.messages.length > 0).toBeTruthy();
  432. expect(view.model.messages.localStorage.records.length > 0).toBeTruthy();
  433. expect(converse.emit).toHaveBeenCalledWith('onMessageSend', message);
  434. message = '/clear';
  435. var old_length = view.model.messages.length;
  436. spyOn(view, 'sendMessage').andCallThrough();
  437. view.$el.find('.chat-textarea').val(message).text(message);
  438. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  439. expect(view.sendMessage).toHaveBeenCalled();
  440. expect(view.model.messages.length, 0); // The messages must be removed from the modal
  441. expect(view.model.messages.localStorage.records.length, 0); // And also from localStorage
  442. expect(converse.emit.callCount, 1);
  443. expect(converse.emit.mostRecentCall.args, ['onMessageSend', message]);
  444. }, converse));
  445. }, converse));
  446. describe("A Message Counter", $.proxy(function () {
  447. beforeEach($.proxy(function () {
  448. converse.clearMsgCounter();
  449. }, converse));
  450. it("is incremented when the message is received and the window is not focused", $.proxy(function () {
  451. spyOn(converse, 'emit');
  452. expect(this.msg_counter).toBe(0);
  453. spyOn(converse, 'incrementMsgCounter').andCallThrough();
  454. $(window).trigger('blur');
  455. var message = 'This message will increment the message counter';
  456. var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  457. msg = $msg({
  458. from: sender_jid,
  459. to: this.connection.jid,
  460. type: 'chat',
  461. id: (new Date()).getTime()
  462. }).c('body').t(message).up()
  463. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  464. this.chatboxes.onMessage(msg);
  465. expect(converse.incrementMsgCounter).toHaveBeenCalled();
  466. expect(this.msg_counter).toBe(1);
  467. expect(converse.emit).toHaveBeenCalledWith('onMessage', msg);
  468. }, converse));
  469. it("is cleared when the window is focused", $.proxy(function () {
  470. spyOn(converse, 'clearMsgCounter').andCallThrough();
  471. runs(function () {
  472. $(window).trigger('focus');
  473. });
  474. waits(50);
  475. runs(function () {
  476. expect(converse.clearMsgCounter).toHaveBeenCalled();
  477. });
  478. }, converse));
  479. it("is not incremented when the message is received and the window is focused", $.proxy(function () {
  480. expect(this.msg_counter).toBe(0);
  481. spyOn(converse, 'incrementMsgCounter').andCallThrough();
  482. $(window).trigger('focus');
  483. var message = 'This message will not increment the message counter';
  484. var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  485. msg = $msg({
  486. from: sender_jid,
  487. to: this.connection.jid,
  488. type: 'chat',
  489. id: (new Date()).getTime()
  490. }).c('body').t(message).up()
  491. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  492. this.chatboxes.onMessage(msg);
  493. expect(converse.incrementMsgCounter).not.toHaveBeenCalled();
  494. expect(this.msg_counter).toBe(0);
  495. }, converse));
  496. }, converse));
  497. }, converse, mock, utils));
  498. }));