MainSpec.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. (function (root, factory) {
  2. define([
  3. "mock"
  4. ], function (mock_connection) {
  5. return factory(mock_connection);
  6. }
  7. );
  8. } (this, function (mock_connection) {
  9. return describe("Converse.js", function() {
  10. // Names from http://www.fakenamegenerator.com/
  11. var req_names = [
  12. 'Louw Spekman', 'Mohamad Stet', 'Dominik Beyer'
  13. ];
  14. var pend_names = [
  15. 'Suleyman van Beusichem', 'Nicole Diederich', 'Nanja van Yperen'
  16. ];
  17. var cur_names = [
  18. 'Max Frankfurter', 'Candice van der Knijff', 'Irini Vlastuin', 'Rinse Sommer', 'Annegreet Gomez',
  19. 'Robin Schook', 'Marcel Eberhardt', 'Simone Brauer', 'Asmaa Haakman', 'Felix Amsel',
  20. 'Lena Grunewald', 'Laura Grunewald', 'Mandy Seiler', 'Sven Bosch', 'Nuriye Cuypers'
  21. ];
  22. var num_contacts = req_names.length + pend_names.length + cur_names.length;
  23. var open_controlbox;
  24. closeAllChatBoxes = function () {
  25. var i, chatbox, num_chatboxes = converse.chatboxes.models.length;
  26. for (i=num_chatboxes-1; i>-1; i--) {
  27. chatbox = converse.chatboxes.models[i];
  28. converse.chatboxesview.views[chatbox.get('id')].closeChat();
  29. }
  30. };
  31. openControlBox = function () {
  32. if (!$("div#controlbox").is(':visible')) {
  33. $('.toggle-online-users').click();
  34. }
  35. };
  36. describe("The Control Box", $.proxy(function () {
  37. it("is not shown by default", $.proxy(function () {
  38. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  39. }, converse));
  40. open_controlbox = $.proxy(function () {
  41. // This spec will only pass if the controlbox is not currently
  42. // open yet.
  43. expect($("div#controlbox").is(':visible')).toBe(false);
  44. spyOn(this, 'toggleControlBox').andCallThrough();
  45. spyOn(this, 'showControlBox').andCallThrough();
  46. $('.toggle-online-users').click();
  47. expect(this.toggleControlBox).toHaveBeenCalled();
  48. expect(this.showControlBox).toHaveBeenCalled();
  49. expect($("div#controlbox").is(':visible')).toBe(true);
  50. }, converse);
  51. it("can be opened by clicking a DOM element with class 'toggle-online-users'", open_controlbox);
  52. describe("The Status Widget", $.proxy(function () {
  53. it("shows the user's chat status, which is online by default", $.proxy(function () {
  54. var view = this.xmppstatusview;
  55. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  56. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online');
  57. }, converse));
  58. it("can be used to set the current user's chat status", $.proxy(function () {
  59. var view = this.xmppstatusview;
  60. spyOn(view, 'toggleOptions').andCallThrough();
  61. spyOn(view, 'setStatus').andCallThrough();
  62. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  63. runs(function () {
  64. view.$el.find('a.choose-xmpp-status').click();
  65. expect(view.toggleOptions).toHaveBeenCalled();
  66. });
  67. waits(250);
  68. runs(function () {
  69. spyOn(view, 'updateStatusUI').andCallThrough();
  70. view.initialize(); // Rebind events for spy
  71. $(view.$el.find('.dropdown dd ul li a')[1]).click();
  72. expect(view.setStatus).toHaveBeenCalled();
  73. });
  74. waits(250);
  75. runs($.proxy(function () {
  76. expect(view.updateStatusUI).toHaveBeenCalled();
  77. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false);
  78. expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true);
  79. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy');
  80. }, converse));
  81. }, converse));
  82. it("can be used to set a custom status message", $.proxy(function () {
  83. var view = this.xmppstatusview;
  84. this.xmppstatus.save({'status': 'online'});
  85. spyOn(view, 'setStatusMessage').andCallThrough();
  86. spyOn(view, 'renderStatusChangeForm').andCallThrough();
  87. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  88. view.$el.find('a.change-xmpp-status-message').click();
  89. expect(view.renderStatusChangeForm).toHaveBeenCalled();
  90. // The async testing here is used only to provide time for
  91. // visual feedback
  92. var msg = 'I am happy';
  93. runs (function () {
  94. view.$el.find('form input.custom-xmpp-status').val(msg);
  95. });
  96. waits(250);
  97. runs (function () {
  98. view.$el.find('form#set-custom-xmpp-status').submit();
  99. expect(view.setStatusMessage).toHaveBeenCalled();
  100. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  101. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg);
  102. });
  103. }, converse));
  104. }, converse));
  105. }, converse));
  106. describe("The Contacts Roster", $.proxy(function () {
  107. describe("Pending Contacts", $.proxy(function () {
  108. beforeEach(function () {
  109. if (!$("div#controlbox").is(':visible')) {
  110. $('.toggle-online-users').click();
  111. }
  112. });
  113. it("do not have a heading if there aren't any", $.proxy(function () {
  114. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none');
  115. }, converse));
  116. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  117. var i, t, is_last;
  118. spyOn(this.rosterview, 'render').andCallThrough();
  119. spyOn(this.xmppstatus, 'sendPresence');
  120. for (i=0; i<pend_names.length; i++) {
  121. is_last = i===(pend_names.length-1);
  122. this.roster.create({
  123. jid: pend_names[i].replace(' ','.').toLowerCase() + '@localhost',
  124. subscription: 'none',
  125. ask: 'subscribe',
  126. fullname: pend_names[i],
  127. is_last: is_last
  128. });
  129. // For performance reasons, the roster should only be shown once
  130. // the last contact has been added.
  131. if (is_last) {
  132. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  133. expect(this.xmppstatus.sendPresence).toHaveBeenCalled();
  134. } else {
  135. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  136. }
  137. expect(this.rosterview.render).toHaveBeenCalled();
  138. // Check that they are sorted alphabetically
  139. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').text();
  140. expect(t).toEqual(pend_names.slice(0,i+1).sort().join(''));
  141. }
  142. }, converse));
  143. it("will have their own heading once they have been added", $.proxy(function () {
  144. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('block');
  145. }, converse));
  146. }, converse));
  147. describe("Existing Contacts", $.proxy(function () {
  148. it("do not have a heading if there aren't any", $.proxy(function () {
  149. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('none');
  150. }, converse));
  151. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  152. var i, t;
  153. spyOn(this.rosterview, 'render').andCallThrough();
  154. for (i=0; i<cur_names.length; i++) {
  155. this.roster.create({
  156. jid: cur_names[i].replace(' ','.').toLowerCase() + '@localhost',
  157. subscription: 'both',
  158. ask: null,
  159. fullname: cur_names[i],
  160. is_last: i===(cur_names.length-1)
  161. });
  162. expect(this.rosterview.render).toHaveBeenCalled();
  163. // Check that they are sorted alphabetically
  164. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  165. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  166. }
  167. }, converse));
  168. it("will have their own heading once they have been added", $.proxy(function () {
  169. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('block');
  170. }, converse));
  171. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  172. var item, view, jid, t;
  173. spyOn(this.rosterview, 'render').andCallThrough();
  174. for (i=0; i<3; i++) {
  175. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  176. view = this.rosterview.rosteritemviews[jid];
  177. spyOn(view, 'render').andCallThrough();
  178. item = view.model;
  179. item.set('chat_status', 'online');
  180. expect(view.render).toHaveBeenCalled();
  181. expect(this.rosterview.render).toHaveBeenCalled();
  182. // Check that they are sorted alphabetically
  183. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  184. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  185. }
  186. }, converse));
  187. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  188. var item, view, jid, t;
  189. spyOn(this.rosterview, 'render').andCallThrough();
  190. for (i=3; i<6; i++) {
  191. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  192. view = this.rosterview.rosteritemviews[jid];
  193. spyOn(view, 'render').andCallThrough();
  194. item = view.model;
  195. item.set('chat_status', 'dnd');
  196. expect(view.render).toHaveBeenCalled();
  197. expect(this.rosterview.render).toHaveBeenCalled();
  198. // Check that they are sorted alphabetically
  199. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  200. expect(t).toEqual(cur_names.slice(3,i+1).sort().join(''));
  201. }
  202. }, converse));
  203. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  204. var item, view, jid, t;
  205. spyOn(this.rosterview, 'render').andCallThrough();
  206. for (i=6; i<9; i++) {
  207. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  208. view = this.rosterview.rosteritemviews[jid];
  209. spyOn(view, 'render').andCallThrough();
  210. item = view.model;
  211. item.set('chat_status', 'away');
  212. expect(view.render).toHaveBeenCalled();
  213. expect(this.rosterview.render).toHaveBeenCalled();
  214. // Check that they are sorted alphabetically
  215. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  216. expect(t).toEqual(cur_names.slice(6,i+1).sort().join(''));
  217. }
  218. }, converse));
  219. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  220. var item, view, jid, t;
  221. spyOn(this.rosterview, 'render').andCallThrough();
  222. for (i=9; i<12; i++) {
  223. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  224. view = this.rosterview.rosteritemviews[jid];
  225. spyOn(view, 'render').andCallThrough();
  226. item = view.model;
  227. item.set('chat_status', 'unavailable');
  228. expect(view.render).toHaveBeenCalled();
  229. expect(this.rosterview.render).toHaveBeenCalled();
  230. // Check that they are sorted alphabetically
  231. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  232. expect(t).toEqual(cur_names.slice(9, i+1).sort().join(''));
  233. }
  234. }, converse));
  235. it("are ordered according to status: online, busy, away, unavailable, offline", $.proxy(function () {
  236. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  237. var i;
  238. // The first five contacts are online.
  239. for (i=0; i<3; i++) {
  240. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  241. }
  242. // The next five are busy
  243. for (i=3; i<6; i++) {
  244. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  245. }
  246. // The next five are away
  247. for (i=6; i<9; i++) {
  248. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  249. }
  250. // The next five are unavailable
  251. for (i=9; i<12; i++) {
  252. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  253. }
  254. // The next 20 are offline
  255. for (i=12; i<cur_names.length; i++) {
  256. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  257. }
  258. }, converse));
  259. }, converse));
  260. describe("Requesting Contacts", $.proxy(function () {
  261. // by default the dts are hidden from css class and only later they will be hidden
  262. // by jQuery therefore for the first check we will see if visible instead of none
  263. it("do not have a heading if there aren't any", $.proxy(function () {
  264. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  265. }, converse));
  266. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  267. var i, t;
  268. spyOn(this.rosterview, 'render').andCallThrough();
  269. spyOn(this, 'showControlBox').andCallThrough();
  270. for (i=0; i<req_names.length; i++) {
  271. this.roster.create({
  272. jid: req_names[i].replace(' ','.').toLowerCase() + '@localhost',
  273. subscription: 'none',
  274. ask: 'request',
  275. fullname: req_names[i],
  276. is_last: i===(req_names.length-1)
  277. });
  278. expect(this.rosterview.render).toHaveBeenCalled();
  279. // Check that they are sorted alphabetically
  280. t = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').text().replace(/AcceptDecline/g, '');
  281. expect(t).toEqual(req_names.slice(0,i+1).sort().join(''));
  282. // When a requesting contact is added, the controlbox must
  283. // be opened.
  284. expect(this.showControlBox).toHaveBeenCalled();
  285. }
  286. }, converse));
  287. it("will have their own heading once they have been added", $.proxy(function () {
  288. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').css('display')).toEqual('block');
  289. }, converse));
  290. it("can have their requests accepted by the user", $.proxy(function () {
  291. // TODO: Testing can be more thorough here, the user is
  292. // actually not accepted/authorized because of
  293. // mock_connection.
  294. var jid = req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost';
  295. var view = this.rosterview.rosteritemviews[jid];
  296. spyOn(this.connection.roster, 'authorize');
  297. spyOn(view, 'acceptRequest').andCallThrough();
  298. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  299. var accept_button = view.$el.find('.accept-xmpp-request');
  300. accept_button.click();
  301. expect(view.acceptRequest).toHaveBeenCalled();
  302. expect(this.connection.roster.authorize).toHaveBeenCalled();
  303. }, converse));
  304. it("can have their requests denied by the user", $.proxy(function () {
  305. var jid = req_names.sort()[1].replace(' ','.').toLowerCase() + '@localhost';
  306. var view = this.rosterview.rosteritemviews[jid];
  307. spyOn(this.connection.roster, 'unauthorize');
  308. spyOn(this.rosterview, 'removeRosterItem').andCallThrough();
  309. spyOn(view, 'declineRequest').andCallThrough();
  310. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  311. var accept_button = view.$el.find('.decline-xmpp-request');
  312. accept_button.click();
  313. expect(view.declineRequest).toHaveBeenCalled();
  314. expect(this.rosterview.removeRosterItem).toHaveBeenCalled();
  315. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  316. // There should now be one less contact
  317. expect(this.roster.length).toEqual(num_contacts-1);
  318. }, converse));
  319. }, converse));
  320. describe("All Contacts", $.proxy(function () {
  321. it("are saved to, and can be retrieved from, localStorage", $.proxy(function () {
  322. var new_attrs, old_attrs, attrs, old_roster;
  323. var num_contacts = this.roster.length;
  324. new_roster = new this.RosterItems();
  325. // Roster items are yet to be fetched from localStorage
  326. expect(new_roster.length).toEqual(0);
  327. new_roster.localStorage = new Backbone.LocalStorage(
  328. hex_sha1('converse.rosteritems-dummy@localhost'));
  329. new_roster.fetch();
  330. expect(this.roster.length).toEqual(num_contacts);
  331. // Check that the roster items retrieved from localStorage
  332. // have the same attributes values as the original ones.
  333. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  334. for (i=0; i<attrs.length; i++) {
  335. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  336. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  337. // Roster items in storage are not necessarily sorted,
  338. // so we have to sort them here to do a proper
  339. // comparison
  340. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  341. }
  342. this.rosterview.render();
  343. }, converse));
  344. afterEach($.proxy(function () {
  345. // Contacts retrieved from localStorage have chat_status of
  346. // "offline".
  347. // In the next test suite, we need some online contacts, so
  348. // we make some online now
  349. for (i=0; i<5; i++) {
  350. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  351. view = this.rosterview.rosteritemviews[jid];
  352. view.model.set('chat_status', 'online');
  353. }
  354. }, converse));
  355. }, converse));
  356. }, converse));
  357. describe("The 'Add Contact' widget", $.proxy(function () {
  358. it("opens up an add form when you click on it", $.proxy(function () {
  359. var panel = this.chatboxesview.views.controlbox.contactspanel;
  360. spyOn(panel, 'toggleContactForm').andCallThrough();
  361. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  362. panel.$el.find('a.toggle-xmpp-contact-form').click();
  363. expect(panel.toggleContactForm).toHaveBeenCalled();
  364. // XXX: Awaiting more tests, close it again for now...
  365. panel.$el.find('a.toggle-xmpp-contact-form').click();
  366. }, converse));
  367. }, converse));
  368. describe("A Chatbox", $.proxy(function () {
  369. it("is created when you click on a roster item", $.proxy(function () {
  370. var i, $el, click, jid, view;
  371. // showControlBox was called earlier, so the controlbox is
  372. // visible, but no other chat boxes have been created.
  373. expect(this.chatboxes.length).toEqual(1);
  374. var online_contacts = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat');
  375. for (i=0; i<online_contacts.length; i++) {
  376. $el = $(online_contacts[i]);
  377. jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
  378. view = this.rosterview.rosteritemviews[jid];
  379. spyOn(view, 'openChat').andCallThrough();
  380. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  381. $el.click();
  382. expect(view.openChat).toHaveBeenCalled();
  383. expect(this.chatboxes.length).toEqual(i+2);
  384. }
  385. }, converse));
  386. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  387. // We instantiate a new ChatBoxes collection, which by default
  388. // will be empty.
  389. var newchatboxes = new this.ChatBoxes();
  390. expect(newchatboxes.length).toEqual(0);
  391. // The chatboxes will then be fetched from localStorage inside the
  392. // onConnected method
  393. newchatboxes.onConnected();
  394. expect(newchatboxes.length).toEqual(6);
  395. // Check that the chatboxes items retrieved from localStorage
  396. // have the same attributes values as the original ones.
  397. attrs = ['id', 'box_id', 'visible'];
  398. for (i=0; i<attrs.length; i++) {
  399. new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
  400. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  401. expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
  402. }
  403. this.rosterview.render();
  404. }, converse));
  405. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  406. var chatbox, view, $el,
  407. num_open_chats = this.chatboxes.length;
  408. for (i=0; i<num_open_chats; i++) {
  409. chatbox = this.chatboxes.models[0];
  410. view = this.chatboxesview.views[chatbox.get('id')];
  411. spyOn(view, 'closeChat').andCallThrough();
  412. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  413. view.$el.find('.close-chatbox-button').click();
  414. expect(view.closeChat).toHaveBeenCalled();
  415. }
  416. }, converse));
  417. it("will be removed from localStorage when closed", $.proxy(function () {
  418. var newchatboxes = new this.ChatBoxes();
  419. expect(newchatboxes.length).toEqual(0);
  420. // onConnected will fetch chatboxes in localStorage, but
  421. // because there aren't any open chatboxes, there won't be any
  422. // in localStorage either.
  423. newchatboxes.onConnected();
  424. expect(newchatboxes.length).toEqual(0);
  425. // Lets open the controlbox again, purely for visual feedback
  426. open_controlbox();
  427. }, converse));
  428. describe("A Chat Message", $.proxy(function () {
  429. it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
  430. var message = 'This is a received message';
  431. var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  432. msg = $msg({
  433. from: sender_jid,
  434. to: this.connection.jid,
  435. type: 'chat',
  436. id: (new Date()).getTime()
  437. }).c('body').t(message).up()
  438. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  439. // We don't already have an open chatbox for this user
  440. expect(this.chatboxes.get(sender_jid)).not.toBeDefined();
  441. runs($.proxy(function () {
  442. // messageReceived is a handler for received XMPP
  443. // messages
  444. this.chatboxes.messageReceived(msg);
  445. }, converse));
  446. waits(500);
  447. runs($.proxy(function () {
  448. // Check that the chatbox and its view now exist
  449. var chatbox = this.chatboxes.get(sender_jid);
  450. var chatboxview = this.chatboxesview.views[sender_jid];
  451. expect(chatbox).toBeDefined();
  452. expect(chatboxview).toBeDefined();
  453. // Check that the message was received and check the
  454. // message parameters
  455. expect(chatbox.messages.length).toEqual(1);
  456. var msg_obj = chatbox.messages.models[0];
  457. expect(msg_obj.get('message')).toEqual(message);
  458. // XXX: This is stupid, fullname is actually only the
  459. // users first name
  460. expect(msg_obj.get('fullname')).toEqual(cur_names[0].split(' ')[0]);
  461. expect(msg_obj.get('sender')).toEqual('them');
  462. expect(msg_obj.get('delayed')).toEqual(false);
  463. // Now check that the message appears inside the
  464. // chatbox in the DOM
  465. var $chat_content = chatboxview.$el.find('.chat-content');
  466. var msg_txt = $chat_content.find('.chat-message').find('.chat-message-content').text();
  467. expect(msg_txt).toEqual(message);
  468. var sender_txt = $chat_content.find('span.chat-message-them').text();
  469. expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy();
  470. }, converse));
  471. }, converse));
  472. it("can be sent from a chatbox, and will appear inside it", $.proxy(function () {
  473. var contact_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  474. var view = this.chatboxesview.views[contact_jid];
  475. var message = 'This message is sent from this chatbox';
  476. spyOn(view, 'sendMessage').andCallThrough();
  477. view.$el.find('.chat-textarea').text(message);
  478. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  479. expect(view.sendMessage).toHaveBeenCalled();
  480. expect(view.model.messages.length, 2);
  481. var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text();
  482. expect(txt).toEqual(message);
  483. }, converse));
  484. }, converse));
  485. }, converse));
  486. describe("A Message Counter", $.proxy(function () {
  487. beforeEach($.proxy(function () {
  488. converse.clearMsgCounter();
  489. }, converse));
  490. it("is incremented when the message is received and the window is not focused", $.proxy(function () {
  491. expect(this.msg_counter).toBe(0);
  492. spyOn(converse, 'incrementMsgCounter').andCallThrough();
  493. $(window).trigger('blur');
  494. var message = 'This message will increment the message counter';
  495. var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  496. msg = $msg({
  497. from: sender_jid,
  498. to: this.connection.jid,
  499. type: 'chat',
  500. id: (new Date()).getTime()
  501. }).c('body').t(message).up()
  502. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  503. this.chatboxes.messageReceived(msg);
  504. expect(converse.incrementMsgCounter).toHaveBeenCalled();
  505. expect(this.msg_counter).toBe(1);
  506. }, converse));
  507. it("is cleared when the window is focused", $.proxy(function () {
  508. spyOn(converse, 'clearMsgCounter').andCallThrough();
  509. runs(function () {
  510. $(window).trigger('focus');
  511. });
  512. waits(50);
  513. runs(function () {
  514. expect(converse.clearMsgCounter).toHaveBeenCalled();
  515. });
  516. }, converse));
  517. it("is not incremented when the message is received and the window is focused", $.proxy(function () {
  518. expect(this.msg_counter).toBe(0);
  519. spyOn(converse, 'incrementMsgCounter').andCallThrough();
  520. $(window).trigger('focus');
  521. var message = 'This message will not increment the message counter';
  522. var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  523. msg = $msg({
  524. from: sender_jid,
  525. to: this.connection.jid,
  526. type: 'chat',
  527. id: (new Date()).getTime()
  528. }).c('body').t(message).up()
  529. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  530. this.chatboxes.messageReceived(msg);
  531. expect(converse.incrementMsgCounter).not.toHaveBeenCalled();
  532. expect(this.msg_counter).toBe(0);
  533. }, converse));
  534. }, converse));
  535. describe("The Controlbox Tabs", $.proxy(function () {
  536. beforeEach($.proxy(function () {
  537. closeAllChatBoxes();
  538. openControlBox();
  539. }, converse));
  540. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  541. var cbview = this.chatboxesview.views.controlbox;
  542. var $panels = cbview.$el.find('#controlbox-panes');
  543. expect($panels.children().length).toBe(2);
  544. expect($panels.children().first().attr('id')).toBe('users');
  545. expect($panels.children().first().is(':visible')).toBe(true);
  546. expect($panels.children().last().attr('id')).toBe('chatrooms');
  547. expect($panels.children().last().is(':visible')).toBe(false);
  548. }, converse));
  549. describe("The Chatrooms Panel", $.proxy(function () {
  550. beforeEach($.proxy(function () {
  551. closeAllChatBoxes();
  552. openControlBox();
  553. }, converse));
  554. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  555. var cbview = this.chatboxesview.views.controlbox;
  556. var $tabs = cbview.$el.find('#controlbox-tabs');
  557. var $panels = cbview.$el.find('#controlbox-panes');
  558. var $contacts = $panels.children().first();
  559. var $chatrooms = $panels.children().last();
  560. spyOn(cbview, 'switchTab').andCallThrough();
  561. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  562. runs(function () {
  563. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  564. });
  565. waits(250);
  566. runs(function () {
  567. expect($contacts.is(':visible')).toBe(false);
  568. expect($chatrooms.is(':visible')).toBe(true);
  569. expect(cbview.switchTab).toHaveBeenCalled();
  570. });
  571. }, converse));
  572. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  573. var roomspanel = this.chatboxesview.views.controlbox.roomspanel;
  574. var $input = roomspanel.$el.find('input.new-chatroom-name');
  575. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  576. var $server = roomspanel.$el.find('input.new-chatroom-server');
  577. expect($input.length).toBe(1);
  578. expect($server.length).toBe(1);
  579. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  580. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  581. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  582. runs(function () {
  583. $input.val('Lounge');
  584. $nick.val('dummy');
  585. $server.val('muc.localhost');
  586. });
  587. waits('250');
  588. runs(function () {
  589. roomspanel.$el.find('form').submit();
  590. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  591. });
  592. waits('250');
  593. runs($.proxy(function () {
  594. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  595. }, converse));
  596. }, converse));
  597. }, converse));
  598. }, converse));
  599. });
  600. }));