MainSpec.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. (function (root, factory) {
  2. define([
  3. "converse"
  4. ], function (xmppchat) {
  5. return factory(xmppchat);
  6. }
  7. );
  8. } (this, function (xmppchat) {
  9. return describe("Converse.js", $.proxy(function() {
  10. // Names from http://www.fakenamegenerator.com/
  11. var req_names = [
  12. 'Louw Spekman', 'Mohamad Stet', 'Dominik Beyer'
  13. ];
  14. var pend_names = [
  15. 'Suleyman van Beusichem', 'Nicole Diederich', 'Nanja van Yperen'
  16. ];
  17. var cur_names = [
  18. 'Max Frankfurter', 'Candice van der Knijff', 'Irini Vlastuin', 'Rinse Sommer', 'Annegreet Gomez',
  19. 'Robin Schook', 'Marcel Eberhardt', 'Simone Brauer', 'Asmaa Haakman', 'Felix Amsel',
  20. 'Lena Grunewald', 'Laura Grunewald', 'Mandy Seiler', 'Sven Bosch', 'Nuriye Cuypers'
  21. ];
  22. var num_contacts = req_names.length + pend_names.length + cur_names.length;
  23. this.bare_jid = 'dummy@localhost';
  24. mock_connection = {
  25. 'muc': {
  26. 'listRooms': function () {}
  27. },
  28. 'jid': this.bare_jid,
  29. 'addHandler': function (handler, ns, name, type, id, from, options) {
  30. return function () {};
  31. },
  32. 'roster': {
  33. 'add': function () {},
  34. 'authorize': function () {},
  35. 'unauthorize': function () {},
  36. 'get': function () {},
  37. 'subscribe': function () {},
  38. 'registerCallback': function () {}
  39. },
  40. 'vcard': {
  41. 'get': function (callback, jid) {
  42. var name = jid.split('@')[0].replace('.', ' ').split(' ');
  43. var firstname = name[0].charAt(0).toUpperCase()+name[0].slice(1);
  44. var lastname = name[1].charAt(0).toUpperCase()+name[1].slice(1);
  45. var fullname = firstname+' '+lastname;
  46. var vcard = $iq().c('vCard').c('FN').t(fullname);
  47. callback(vcard.tree());
  48. }
  49. }
  50. };
  51. // Clear localStorage
  52. window.localStorage.removeItem(
  53. hex_sha1('converse.rosteritems-'+this.bare_jid));
  54. window.localStorage.removeItem(
  55. hex_sha1('converse.chatboxes-'+this.bare_jid));
  56. window.localStorage.removeItem(
  57. hex_sha1('converse.xmppstatus-'+this.bare_jid));
  58. window.localStorage.removeItem(
  59. hex_sha1('converse.messages'+cur_names[0].replace(' ','.').toLowerCase() + '@localhost'));
  60. this.prebind = true;
  61. this.onConnected(mock_connection);
  62. this.animate = false; // don't use animations
  63. describe("The Contacts Roster", $.proxy(function () {
  64. it("is not shown by default", $.proxy(function () {
  65. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  66. }, xmppchat));
  67. it("can be opened by clicking a DOM element with id 'toggle-online-users'", $.proxy(function () {
  68. spyOn(this, 'toggleControlBox').andCallThrough();
  69. $('#toggle-online-users').click();
  70. expect(this.toggleControlBox).toHaveBeenCalled();
  71. }, xmppchat));
  72. describe("Pending Contacts", $.proxy(function () {
  73. it("do not have a heading if there aren't any", $.proxy(function () {
  74. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none');
  75. }, xmppchat));
  76. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  77. var i, t, is_last;
  78. spyOn(this.rosterview, 'render').andCallThrough();
  79. for (i=0; i<pend_names.length; i++) {
  80. is_last = i==(pend_names.length-1);
  81. this.roster.create({
  82. jid: pend_names[i].replace(' ','.').toLowerCase() + '@localhost',
  83. subscription: 'none',
  84. ask: 'subscribe',
  85. fullname: pend_names[i],
  86. is_last: is_last
  87. });
  88. // For performance reasons, the roster should only be shown once
  89. // the last contact has been added.
  90. if (is_last) {
  91. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  92. } else {
  93. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  94. }
  95. expect(this.rosterview.render).toHaveBeenCalled();
  96. // Check that they are sorted alphabetically
  97. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').text();
  98. expect(t).toEqual(pend_names.slice(0,i+1).sort().join(''));
  99. }
  100. }, xmppchat));
  101. it("will have their own heading once they have been added", $.proxy(function () {
  102. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('block');
  103. }, xmppchat));
  104. }, xmppchat));
  105. describe("Existing Contacts", $.proxy(function () {
  106. it("do not have a heading if there aren't any", $.proxy(function () {
  107. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('none');
  108. }, xmppchat));
  109. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  110. var i, t;
  111. spyOn(this.rosterview, 'render').andCallThrough();
  112. for (i=0; i<cur_names.length; i++) {
  113. this.roster.create({
  114. jid: cur_names[i].replace(' ','.').toLowerCase() + '@localhost',
  115. subscription: 'both',
  116. ask: null,
  117. fullname: cur_names[i],
  118. is_last: i==(cur_names.length-1)
  119. });
  120. expect(this.rosterview.render).toHaveBeenCalled();
  121. // Check that they are sorted alphabetically
  122. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  123. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  124. }
  125. }, xmppchat));
  126. it("will have their own heading once they have been added", $.proxy(function () {
  127. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('block');
  128. }, xmppchat));
  129. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  130. var item, view, jid, t;
  131. spyOn(this.rosterview, 'render').andCallThrough();
  132. for (i=0; i<3; i++) {
  133. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  134. view = this.rosterview.rosteritemviews[jid];
  135. spyOn(view, 'render').andCallThrough();
  136. item = view.model;
  137. item.set('chat_status', 'online');
  138. expect(view.render).toHaveBeenCalled();
  139. expect(this.rosterview.render).toHaveBeenCalled();
  140. // Check that they are sorted alphabetically
  141. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  142. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  143. }
  144. }, xmppchat));
  145. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  146. var item, view, jid, t;
  147. spyOn(this.rosterview, 'render').andCallThrough();
  148. for (i=3; i<6; i++) {
  149. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  150. view = this.rosterview.rosteritemviews[jid];
  151. spyOn(view, 'render').andCallThrough();
  152. item = view.model;
  153. item.set('chat_status', 'dnd');
  154. expect(view.render).toHaveBeenCalled();
  155. expect(this.rosterview.render).toHaveBeenCalled();
  156. // Check that they are sorted alphabetically
  157. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  158. expect(t).toEqual(cur_names.slice(3,i+1).sort().join(''));
  159. }
  160. }, xmppchat));
  161. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  162. var item, view, jid, t;
  163. spyOn(this.rosterview, 'render').andCallThrough();
  164. for (i=6; i<9; i++) {
  165. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  166. view = this.rosterview.rosteritemviews[jid];
  167. spyOn(view, 'render').andCallThrough();
  168. item = view.model;
  169. item.set('chat_status', 'away');
  170. expect(view.render).toHaveBeenCalled();
  171. expect(this.rosterview.render).toHaveBeenCalled();
  172. // Check that they are sorted alphabetically
  173. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  174. expect(t).toEqual(cur_names.slice(6,i+1).sort().join(''));
  175. }
  176. }, xmppchat));
  177. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  178. var item, view, jid, t;
  179. spyOn(this.rosterview, 'render').andCallThrough();
  180. for (i=9; i<12; i++) {
  181. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  182. view = this.rosterview.rosteritemviews[jid];
  183. spyOn(view, 'render').andCallThrough();
  184. item = view.model;
  185. item.set('chat_status', 'unavailable');
  186. expect(view.render).toHaveBeenCalled();
  187. expect(this.rosterview.render).toHaveBeenCalled();
  188. // Check that they are sorted alphabetically
  189. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  190. expect(t).toEqual(cur_names.slice(9, i+1).sort().join(''));
  191. }
  192. }, xmppchat));
  193. it("are ordered according to status: online, busy, away, unavailable, offline", $.proxy(function () {
  194. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  195. var i;
  196. // The first five contacts are online.
  197. for (i=0; i<3; i++) {
  198. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  199. }
  200. // The next five are busy
  201. for (i=3; i<6; i++) {
  202. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  203. }
  204. // The next five are away
  205. for (i=6; i<9; i++) {
  206. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  207. }
  208. // The next five are unavailable
  209. for (i=9; i<12; i++) {
  210. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  211. }
  212. // The next 20 are offline
  213. for (i=12; i<cur_names.length; i++) {
  214. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  215. }
  216. }, xmppchat));
  217. }, xmppchat));
  218. describe("Requesting Contacts", $.proxy(function () {
  219. // by default the dts are hidden from css class and only later they will be hidden
  220. // by jQuery therefore for the first check we will see if visible instead of none
  221. it("do not have a heading if there aren't any", $.proxy(function () {
  222. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  223. }, xmppchat));
  224. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  225. var i, t;
  226. spyOn(this.rosterview, 'render').andCallThrough();
  227. spyOn(this, 'showControlBox').andCallThrough();
  228. for (i=0; i<req_names.length; i++) {
  229. this.roster.create({
  230. jid: req_names[i].replace(' ','.').toLowerCase() + '@localhost',
  231. subscription: 'none',
  232. ask: 'request',
  233. fullname: req_names[i],
  234. is_last: i==(req_names.length-1)
  235. });
  236. expect(this.rosterview.render).toHaveBeenCalled();
  237. // Check that they are sorted alphabetically
  238. t = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').text().replace(/AcceptDecline/g, '');
  239. expect(t).toEqual(req_names.slice(0,i+1).sort().join(''));
  240. // When a requesting contact is added, the controlbox must
  241. // be opened.
  242. expect(this.showControlBox).toHaveBeenCalled();
  243. }
  244. }, xmppchat));
  245. it("will have their own heading once they have been added", $.proxy(function () {
  246. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').css('display')).toEqual('block');
  247. }, xmppchat));
  248. it("can have their requests accepted by the user", $.proxy(function () {
  249. // TODO: Testing can be more thorough here, the user is
  250. // actually not accepted/authorized because of
  251. // mock_connection.
  252. var jid = req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost';
  253. var view = this.rosterview.rosteritemviews[jid];
  254. spyOn(this.connection.roster, 'authorize');
  255. spyOn(view, 'acceptRequest').andCallThrough();
  256. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  257. var accept_button = view.$el.find('.accept-xmpp-request');
  258. accept_button.click();
  259. expect(view.acceptRequest).toHaveBeenCalled();
  260. expect(this.connection.roster.authorize).toHaveBeenCalled();
  261. }, xmppchat));
  262. it("can have their requests denied by the user", $.proxy(function () {
  263. var jid = req_names.sort()[1].replace(' ','.').toLowerCase() + '@localhost';
  264. var view = this.rosterview.rosteritemviews[jid];
  265. spyOn(this.connection.roster, 'unauthorize');
  266. spyOn(this.rosterview, 'removeRosterItem').andCallThrough();
  267. spyOn(view, 'declineRequest').andCallThrough();
  268. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  269. var accept_button = view.$el.find('.decline-xmpp-request');
  270. accept_button.click();
  271. expect(view.declineRequest).toHaveBeenCalled();
  272. expect(this.rosterview.removeRosterItem).toHaveBeenCalled();
  273. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  274. // There should now be one less contact
  275. expect(this.roster.length).toEqual(num_contacts-1);
  276. }, xmppchat));
  277. }, xmppchat));
  278. describe("All Contacts", $.proxy(function () {
  279. it("are saved to, and can be retrieved from, localStorage", $.proxy(function () {
  280. var new_attrs, old_attrs, attrs, old_roster;
  281. // One contact was declined, so we have 1 less contact then originally
  282. expect(this.roster.length).toEqual(num_contacts-1);
  283. old_roster = this.roster;
  284. this.roster = new this.RosterItems();
  285. expect(this.roster.length).toEqual(0);
  286. this.roster.localStorage = new Backbone.LocalStorage(
  287. hex_sha1('converse.rosteritems-dummy@localhost'));
  288. this.chatboxes.onConnected();
  289. spyOn(this.roster, 'fetch').andCallThrough();
  290. this.rosterview = new this.RosterView({'model':this.roster});
  291. expect(this.roster.fetch).toHaveBeenCalled();
  292. expect(this.roster.length).toEqual(num_contacts-1);
  293. // Check that the roster items retrieved from localStorage
  294. // have the same attributes values as the original ones.
  295. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  296. for (i=0; i<attrs.length; i++) {
  297. new_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  298. old_attrs = _.pluck(_.pluck(old_roster.models, 'attributes'), attrs[i]);
  299. // Roster items in storage are not necessarily sorted,
  300. // so we have to sort them here to do a proper
  301. // comparison
  302. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  303. }
  304. this.rosterview.render();
  305. }, xmppchat));
  306. afterEach($.proxy(function () {
  307. // Contacts retrieved from localStorage have chat_status of
  308. // "offline".
  309. // In the next test suite, we need some online contacts, so
  310. // we make some online now
  311. for (i=0; i<5; i++) {
  312. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  313. view = this.rosterview.rosteritemviews[jid];
  314. view.model.set('chat_status', 'online');
  315. }
  316. }, xmppchat));
  317. }, xmppchat));
  318. }, xmppchat));
  319. describe("A Chatbox", $.proxy(function () {
  320. it("is created when you click on a roster item", $.proxy(function () {
  321. var i, $el, click, jid, view;
  322. // showControlBox was called earlier, so the controlbox is
  323. // visible, but no other chat boxes have been created.
  324. expect(this.chatboxes.length).toEqual(1);
  325. var online_contacts = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat');
  326. for (i=0; i<online_contacts.length; i++) {
  327. $el = $(online_contacts[i]);
  328. jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
  329. view = this.rosterview.rosteritemviews[jid];
  330. spyOn(view, 'openChat').andCallThrough();
  331. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  332. $el.click();
  333. expect(view.openChat).toHaveBeenCalled();
  334. expect(this.chatboxes.length).toEqual(i+2);
  335. }
  336. }, xmppchat));
  337. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  338. // We instantiate a new ChatBoxes collection, which by default
  339. // will be empty.
  340. this.newchatboxes = new this.ChatBoxes();
  341. expect(this.newchatboxes.length).toEqual(0);
  342. // The chatboxes will then be fetched from localStorage inside the
  343. // onConnected method
  344. this.newchatboxes.onConnected();
  345. expect(this.newchatboxes.length).toEqual(6);
  346. // Check that the roster items retrieved from localStorage
  347. // have the same attributes values as the original ones.
  348. attrs = ['id', 'box_id', 'visible'];
  349. for (i=0; i<attrs.length; i++) {
  350. new_attrs = _.pluck(_.pluck(this.newchatboxes.models, 'attributes'), attrs[i]);
  351. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  352. expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
  353. }
  354. this.rosterview.render();
  355. }, xmppchat));
  356. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  357. var chatbox, view, $el,
  358. num_open_chats = this.chatboxes.length;
  359. for (i=0; i<num_open_chats; i++) {
  360. chatbox = this.chatboxes.models[0];
  361. view = this.chatboxesview.views[chatbox.get('id')];
  362. spyOn(view, 'closeChat').andCallThrough();
  363. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  364. view.$el.find('.close-chatbox-button').click();
  365. expect(view.closeChat).toHaveBeenCalled();
  366. }
  367. }, xmppchat));
  368. it("will be removed from localStorage when closed", $.proxy(function () {
  369. this.newchatboxes = new this.ChatBoxes();
  370. expect(this.newchatboxes.length).toEqual(0);
  371. // onConnected will fetch chatboxes in localStorage, but
  372. // because there aren't any open chatboxes, there won't be any
  373. // in localStorage either.
  374. this.chatboxes.onConnected();
  375. expect(this.chatboxes.length).toEqual(0);
  376. }, xmppchat));
  377. describe("A Chat Message", $.proxy(function () {
  378. it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
  379. var message = 'This is a received message';
  380. var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  381. msg = $msg({
  382. from: sender_jid,
  383. to: this.bare_jid,
  384. type: 'chat',
  385. id: (new Date()).getTime()
  386. }).c('body').t(message).up()
  387. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  388. spyOn(this, 'getVCard').andCallThrough();
  389. // We don't already have an open chatbox for this user
  390. expect(this.chatboxes.get(sender_jid)).not.toBeDefined();
  391. runs($.proxy(function () {
  392. // messageReceived is a handler for received XMPP
  393. // messages
  394. this.chatboxes.messageReceived(msg);
  395. }, xmppchat));
  396. waits(500);
  397. runs($.proxy(function () {
  398. // Since we didn't already have an open chatbox, one
  399. // will asynchronously created inside a callback to
  400. // getVCard
  401. expect(this.getVCard).toHaveBeenCalled();
  402. // Check that the chatbox and its view now exist
  403. var chatbox = this.chatboxes.get(sender_jid);
  404. var chatboxview = this.chatboxesview.views[sender_jid];
  405. expect(chatbox).toBeDefined();
  406. expect(chatboxview).toBeDefined();
  407. // Check that the message was received and check the
  408. // message parameters
  409. expect(chatbox.messages.length).toEqual(1);
  410. var msg_obj = chatbox.messages.models[0];
  411. expect(msg_obj.get('message')).toEqual(message);
  412. // XXX: This is stupid, fullname is actually only the
  413. // users first name
  414. expect(msg_obj.get('fullname')).toEqual(cur_names[0].split(' ')[0]);
  415. expect(msg_obj.get('sender')).toEqual('them');
  416. expect(msg_obj.get('delayed')).toEqual(false);
  417. // Now check that the message appears inside the
  418. // chatbox in the DOM
  419. var txt = chatboxview.$el.find('.chat-content').find('.chat-message').find('.chat-message-content').text();
  420. expect(txt).toEqual(message);
  421. }, xmppchat));
  422. }, xmppchat));
  423. it("can be sent from a chatbox, and will appear inside it", $.proxy(function () {
  424. // TODO
  425. }, xmppchat));
  426. }, xmppchat));
  427. }, xmppchat));
  428. }, xmppchat));
  429. }));