2
0

MainSpec.js 36 KB

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