MainSpec.js 26 KB

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