MainSpec.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 () {}
  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. this.prebind = true;
  59. this.onConnected(mock_connection);
  60. this.animate = false; // don't use animations
  61. // The timeout is used to slow down the tests so that one can see
  62. // visually what is happening in the page.
  63. var timeout = 0;
  64. var sleep = function (delay) {
  65. // Yes this is blocking and stupid, but these are tests and this is
  66. // the easiest way to delay execution without having to use
  67. // callbacks.
  68. var start = new Date().getTime();
  69. while (new Date().getTime() < start + delay) {
  70. continue;
  71. }
  72. };
  73. describe("The Contacts Roster", $.proxy(function () {
  74. it("is not shown by default", $.proxy(function () {
  75. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  76. }, xmppchat));
  77. it("can be opened by clicking a DOM element with id 'toggle-online-users'", $.proxy(function () {
  78. spyOn(this, 'toggleControlBox').andCallThrough();
  79. $('#toggle-online-users').click();
  80. expect(this.toggleControlBox).toHaveBeenCalled();
  81. }, xmppchat));
  82. describe("Pending Contacts", $.proxy(function () {
  83. it("do not have a heading if there aren't any", $.proxy(function () {
  84. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none');
  85. }, xmppchat));
  86. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  87. var i, t, is_last;
  88. spyOn(this.rosterview, 'render').andCallThrough();
  89. for (i=0; i<pend_names.length; i++) {
  90. is_last = i==(pend_names.length-1);
  91. this.roster.create({
  92. jid: pend_names[i].replace(' ','.').toLowerCase() + '@localhost',
  93. subscription: 'none',
  94. ask: 'subscribe',
  95. fullname: pend_names[i],
  96. is_last: is_last
  97. });
  98. // For performance reasons, the roster should only be shown once
  99. // the last contact has been added.
  100. if (is_last) {
  101. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  102. } else {
  103. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  104. }
  105. expect(this.rosterview.render).toHaveBeenCalled();
  106. // Check that they are sorted alphabetically
  107. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').text();
  108. expect(t).toEqual(pend_names.slice(0,i+1).sort().join(''));
  109. }
  110. sleep(timeout);
  111. }, xmppchat));
  112. it("will have their own heading once they have been added", $.proxy(function () {
  113. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('block');
  114. }, xmppchat));
  115. }, xmppchat));
  116. describe("Existing Contacts", $.proxy(function () {
  117. it("do not have a heading if there aren't any", $.proxy(function () {
  118. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('none');
  119. }, xmppchat));
  120. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  121. var i, t;
  122. spyOn(this.rosterview, 'render').andCallThrough();
  123. for (i=0; i<cur_names.length; i++) {
  124. this.roster.create({
  125. jid: cur_names[i].replace(' ','.').toLowerCase() + '@localhost',
  126. subscription: 'both',
  127. ask: null,
  128. fullname: cur_names[i],
  129. is_last: i==(cur_names.length-1)
  130. });
  131. expect(this.rosterview.render).toHaveBeenCalled();
  132. // Check that they are sorted alphabetically
  133. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  134. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  135. }
  136. sleep(timeout);
  137. }, xmppchat));
  138. it("will have their own heading once they have been added", $.proxy(function () {
  139. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('block');
  140. }, xmppchat));
  141. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  142. var item, view, jid, t;
  143. spyOn(this.rosterview, 'render').andCallThrough();
  144. for (i=0; i<5; i++) {
  145. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  146. view = this.rosterview.rosteritemviews[jid];
  147. spyOn(view, 'render').andCallThrough();
  148. item = view.model;
  149. item.set('chat_status', 'online');
  150. expect(view.render).toHaveBeenCalled();
  151. expect(this.rosterview.render).toHaveBeenCalled();
  152. // Check that they are sorted alphabetically
  153. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  154. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  155. sleep(timeout);
  156. }
  157. }, xmppchat));
  158. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  159. var item, view, jid, t;
  160. spyOn(this.rosterview, 'render').andCallThrough();
  161. for (i=5; i<10; i++) {
  162. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  163. view = this.rosterview.rosteritemviews[jid];
  164. spyOn(view, 'render').andCallThrough();
  165. item = view.model;
  166. item.set('chat_status', 'dnd');
  167. expect(view.render).toHaveBeenCalled();
  168. expect(this.rosterview.render).toHaveBeenCalled();
  169. // Check that they are sorted alphabetically
  170. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  171. expect(t).toEqual(cur_names.slice(5,i+1).sort().join(''));
  172. sleep(timeout);
  173. }
  174. }, xmppchat));
  175. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  176. var item, view, jid, t;
  177. spyOn(this.rosterview, 'render').andCallThrough();
  178. for (i=10; i<15; i++) {
  179. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  180. view = this.rosterview.rosteritemviews[jid];
  181. spyOn(view, 'render').andCallThrough();
  182. item = view.model;
  183. item.set('chat_status', 'away');
  184. expect(view.render).toHaveBeenCalled();
  185. expect(this.rosterview.render).toHaveBeenCalled();
  186. // Check that they are sorted alphabetically
  187. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  188. expect(t).toEqual(cur_names.slice(10,i+1).sort().join(''));
  189. sleep(timeout);
  190. }
  191. }, xmppchat));
  192. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  193. var item, view, jid, t;
  194. spyOn(this.rosterview, 'render').andCallThrough();
  195. for (i=15; i<20; i++) {
  196. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  197. view = this.rosterview.rosteritemviews[jid];
  198. spyOn(view, 'render').andCallThrough();
  199. item = view.model;
  200. item.set('chat_status', 'unavailable');
  201. expect(view.render).toHaveBeenCalled();
  202. expect(this.rosterview.render).toHaveBeenCalled();
  203. // Check that they are sorted alphabetically
  204. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  205. expect(t).toEqual(cur_names.slice(15, i+1).sort().join(''));
  206. sleep(timeout);
  207. }
  208. }, xmppchat));
  209. it("are ordered according to status: online, busy, away, unavailable, offline", $.proxy(function () {
  210. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  211. var i;
  212. // The first five contacts are online.
  213. for (i=0; i<5; i++) {
  214. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  215. }
  216. // The next five are busy
  217. for (i=5; i<10; i++) {
  218. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  219. }
  220. // The next five are away
  221. for (i=10; i<15; i++) {
  222. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  223. }
  224. // The next five are unavailable
  225. for (i=15; i<20; i++) {
  226. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  227. }
  228. // The next 20 are offline
  229. for (i=20; i<cur_names.length; i++) {
  230. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  231. }
  232. }, xmppchat));
  233. }, xmppchat));
  234. describe("Requesting Contacts", $.proxy(function () {
  235. // by default the dts are hidden from css class and only later they will be hidden
  236. // by jQuery therefore for the first check we will see if visible instead of none
  237. it("do not have a heading if there aren't any", $.proxy(function () {
  238. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  239. }, xmppchat));
  240. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  241. var i, t;
  242. spyOn(this.rosterview, 'render').andCallThrough();
  243. spyOn(this, 'showControlBox').andCallThrough();
  244. for (i=0; i<req_names.length; i++) {
  245. this.roster.create({
  246. jid: req_names[i].replace(' ','.').toLowerCase() + '@localhost',
  247. subscription: 'none',
  248. ask: 'request',
  249. fullname: req_names[i],
  250. is_last: i==(req_names.length-1)
  251. });
  252. expect(this.rosterview.render).toHaveBeenCalled();
  253. // Check that they are sorted alphabetically
  254. t = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').text().replace(/AcceptDecline/g, '');
  255. expect(t).toEqual(req_names.slice(0,i+1).sort().join(''));
  256. // When a requesting contact is added, the controlbox must
  257. // be opened.
  258. expect(this.showControlBox).toHaveBeenCalled();
  259. }
  260. sleep(timeout);
  261. }, xmppchat));
  262. it("will have their own heading once they have been added", $.proxy(function () {
  263. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').css('display')).toEqual('block');
  264. }, xmppchat));
  265. it("can have their requests accepted by the user", $.proxy(function () {
  266. // TODO: Testing can be more thorough here, the user is
  267. // actually not accepted/authorized because of
  268. // mock_connection.
  269. var jid = req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost';
  270. var view = this.rosterview.rosteritemviews[jid];
  271. spyOn(this.connection.roster, 'authorize');
  272. spyOn(view, 'acceptRequest').andCallThrough();
  273. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  274. var accept_button = view.$el.find('.accept-xmpp-request');
  275. accept_button.click();
  276. expect(view.acceptRequest).toHaveBeenCalled();
  277. expect(this.connection.roster.authorize).toHaveBeenCalled();
  278. sleep(timeout);
  279. }, xmppchat));
  280. it("can have their requests denied by the user", $.proxy(function () {
  281. var jid = req_names.sort()[1].replace(' ','.').toLowerCase() + '@localhost';
  282. var view = this.rosterview.rosteritemviews[jid];
  283. spyOn(this.connection.roster, 'unauthorize');
  284. spyOn(this.rosterview, 'removeRosterItem').andCallThrough();
  285. spyOn(view, 'declineRequest').andCallThrough();
  286. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  287. var accept_button = view.$el.find('.decline-xmpp-request');
  288. accept_button.click();
  289. expect(view.declineRequest).toHaveBeenCalled();
  290. expect(this.rosterview.removeRosterItem).toHaveBeenCalled();
  291. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  292. // There should now be one less contact
  293. expect(this.roster.length).toEqual(num_contacts-1);
  294. sleep(timeout);
  295. }, xmppchat));
  296. }, xmppchat));
  297. describe("All Contacts", $.proxy(function () {
  298. it("are saved to, and can be retrieved from, localStorage", $.proxy(function () {
  299. var new_attrs, old_attrs, attrs, old_roster;
  300. // One contact was declined, so we have 1 less contact then originally
  301. expect(this.roster.length).toEqual(num_contacts-1);
  302. old_roster = this.roster;
  303. this.roster = new this.RosterItems();
  304. expect(this.roster.length).toEqual(0);
  305. this.roster.localStorage = new Backbone.LocalStorage(
  306. hex_sha1('converse.rosteritems-dummy@localhost'));
  307. this.chatboxes.onConnected();
  308. spyOn(this.roster, 'fetch').andCallThrough();
  309. this.rosterview = new this.RosterView({'model':this.roster});
  310. expect(this.roster.fetch).toHaveBeenCalled();
  311. expect(this.roster.length).toEqual(num_contacts-1);
  312. // Check that the roster items retrieved from localStorage
  313. // have the same attributes values as the original ones.
  314. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  315. for (i=0; i<attrs.length; i++) {
  316. new_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  317. old_attrs = _.pluck(_.pluck(old_roster.models, 'attributes'), attrs[i]);
  318. // Roster items in storage are not necessarily sorted,
  319. // so we have to sort them here to do a proper
  320. // comparison
  321. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  322. }
  323. this.rosterview.render();
  324. }, xmppchat));
  325. afterEach($.proxy(function () {
  326. // Contacts retrieved from localStorage have chat_status of
  327. // "offline".
  328. // In the next test suite, we need some online contacts, so
  329. // we make some online now
  330. for (i=0; i<5; i++) {
  331. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  332. view = this.rosterview.rosteritemviews[jid];
  333. view.model.set('chat_status', 'online');
  334. }
  335. }, xmppchat));
  336. }, xmppchat));
  337. }, xmppchat));
  338. describe("A Chatbox", $.proxy(function () {
  339. it("is created when you click on a roster item", $.proxy(function () {
  340. var i, $el, click, jid, view;
  341. // showControlBox was called earlier, so the controlbox is
  342. // visible, but no other chat boxes have been created.
  343. expect(this.chatboxes.length).toEqual(1);
  344. var online_contacts = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat');
  345. for (i=0; i<online_contacts.length; i++) {
  346. $el = $(online_contacts[i]);
  347. jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
  348. view = this.rosterview.rosteritemviews[jid];
  349. spyOn(view, 'openChat').andCallThrough();
  350. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  351. $el.click();
  352. expect(view.openChat).toHaveBeenCalled();
  353. expect(this.chatboxes.length).toEqual(i+2);
  354. sleep(timeout);
  355. }
  356. }, xmppchat));
  357. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  358. var old_chatboxes = this.chatboxes;
  359. expect(this.chatboxes.length).toEqual(6);
  360. this.chatboxes = new this.ChatBoxes();
  361. expect(this.chatboxes.length).toEqual(0);
  362. this.chatboxes.onConnected();
  363. expect(this.chatboxes.length).toEqual(6);
  364. // Check that the roster items retrieved from localStorage
  365. // have the same attributes values as the original ones.
  366. attrs = ['id', 'box_id', 'visible'];
  367. for (i=0; i<attrs.length; i++) {
  368. new_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  369. old_attrs = _.pluck(_.pluck(old_chatboxes.models, 'attributes'), attrs[i]);
  370. expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
  371. }
  372. this.rosterview.render();
  373. }, xmppchat));
  374. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  375. var chatbox, view, $el;
  376. for (i=0; i<this.chatboxes.length; i++) {
  377. chatbox = this.chatboxes.models[i];
  378. view = this.chatboxesview.views[chatbox.get('id')];
  379. spyOn(view, 'closeChat').andCallThrough();
  380. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  381. view.$el.find('.close-chatbox-button').click();
  382. expect(view.closeChat).toHaveBeenCalled();
  383. sleep(timeout);
  384. }
  385. }, xmppchat));
  386. it("will be removed from localStorage when closed", $.proxy(function () {
  387. var old_chatboxes = this.chatboxes;
  388. expect(this.chatboxes.length).toEqual(6);
  389. this.chatboxes = new this.ChatBoxes();
  390. expect(this.chatboxes.length).toEqual(0);
  391. this.chatboxes.onConnected();
  392. expect(this.chatboxes.length).toEqual(0);
  393. }, xmppchat));
  394. describe("A Chat Message", $.proxy(function () {
  395. it("received from a contact will open a chatbox and appear inside it", $.proxy(function () {
  396. var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  397. var timestamp = (new Date()).getTime(),
  398. msg = $msg({
  399. from: sender_jid,
  400. to: this.bare_jid,
  401. type: 'chat',
  402. id: timestamp
  403. }).c('body').t('This is a received message').up()
  404. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  405. var chatbox = this.chatboxesview.views[sender_jid].model;
  406. spyOn(chatbox, 'messageReceived');
  407. this.chatboxes.messageReceived(msg);
  408. expect(chatbox.messageReceived).toHaveBeenCalled();
  409. }, xmppchat));
  410. it("can be sent from a chatbox, and will appear inside it", $.proxy(function () {
  411. var hello;
  412. }, xmppchat));
  413. }, xmppchat));
  414. }, xmppchat));
  415. }, xmppchat));
  416. }));