2
0

MainSpec.js 40 KB

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