MainSpec.js 35 KB

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