controlbox.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. (function (root, factory) {
  2. define([
  3. "mock",
  4. "utils"
  5. ], function (mock, utils) {
  6. return factory(mock, utils);
  7. }
  8. );
  9. } (this, function (mock, utils) {
  10. var checkHeaderToggling = function ($header) {
  11. var $toggle = $header.find('a.group-toggle');
  12. expect($header.css('display')).toEqual('block');
  13. spyOn(this.rosterview, 'toggleGroup').andCallThrough();
  14. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  15. this.rosterview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  16. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  17. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  18. $toggle.click();
  19. expect(this.rosterview.toggleGroup).toHaveBeenCalled();
  20. expect($toggle.hasClass('icon-closed')).toBeTruthy();
  21. expect($toggle.hasClass('icon-opened')).toBeFalsy();
  22. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:hidden').length).toBeTruthy();
  23. $toggle.click();
  24. expect(this.rosterview.toggleGroup).toHaveBeenCalled();
  25. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  26. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  27. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  28. };
  29. describe("The Control Box", $.proxy(function (mock, utils) {
  30. beforeEach(function () {
  31. runs(function () {
  32. utils.openControlBox();
  33. });
  34. });
  35. it("can be opened by clicking a DOM element with class 'toggle-controlbox'", $.proxy(function () {
  36. runs(function () {
  37. utils.closeControlBox();
  38. });
  39. waits(50);
  40. runs(function () {
  41. // This spec will only pass if the controlbox is not currently
  42. // open yet.
  43. expect($("div#controlbox").is(':visible')).toBe(false);
  44. spyOn(this.controlboxtoggle, 'onClick').andCallThrough();
  45. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  46. spyOn(converse, 'emit');
  47. // Redelegate so that the spies are now registered as the event handlers (specifically for 'onClick')
  48. this.controlboxtoggle.delegateEvents();
  49. $('.toggle-controlbox').click();
  50. }.bind(converse));
  51. waits(50);
  52. runs(function () {
  53. expect(this.controlboxtoggle.onClick).toHaveBeenCalled();
  54. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  55. expect(this.emit).toHaveBeenCalledWith('controlBoxOpened', jasmine.any(Object));
  56. expect($("div#controlbox").is(':visible')).toBe(true);
  57. }.bind(converse));
  58. }, converse));
  59. describe("The Status Widget", $.proxy(function () {
  60. beforeEach(function () {
  61. utils.openControlBox();
  62. });
  63. it("shows the user's chat status, which is online by default", $.proxy(function () {
  64. var view = this.xmppstatusview;
  65. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  66. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online');
  67. }, converse));
  68. it("can be used to set the current user's chat status", $.proxy(function () {
  69. var view = this.xmppstatusview;
  70. spyOn(view, 'toggleOptions').andCallThrough();
  71. spyOn(view, 'setStatus').andCallThrough();
  72. spyOn(converse, 'emit');
  73. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  74. runs(function () {
  75. view.$el.find('a.choose-xmpp-status').click();
  76. expect(view.toggleOptions).toHaveBeenCalled();
  77. });
  78. waits(250);
  79. runs(function () {
  80. spyOn(view, 'updateStatusUI').andCallThrough();
  81. view.initialize(); // Rebind events for spy
  82. $(view.$el.find('.dropdown dd ul li a')[1]).click(); // Change status to "dnd"
  83. expect(view.setStatus).toHaveBeenCalled();
  84. expect(converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd');
  85. });
  86. waits(250);
  87. runs($.proxy(function () {
  88. expect(view.updateStatusUI).toHaveBeenCalled();
  89. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false);
  90. expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true);
  91. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy');
  92. }, converse));
  93. }, converse));
  94. it("can be used to set a custom status message", $.proxy(function () {
  95. var view = this.xmppstatusview;
  96. this.xmppstatus.save({'status': 'online'});
  97. spyOn(view, 'setStatusMessage').andCallThrough();
  98. spyOn(view, 'renderStatusChangeForm').andCallThrough();
  99. spyOn(converse, 'emit');
  100. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  101. view.$el.find('a.change-xmpp-status-message').click();
  102. expect(view.renderStatusChangeForm).toHaveBeenCalled();
  103. // The async testing here is used only to provide time for
  104. // visual feedback
  105. var msg = 'I am happy';
  106. runs (function () {
  107. view.$el.find('form input.custom-xmpp-status').val(msg);
  108. });
  109. waits(250);
  110. runs (function () {
  111. view.$el.find('form#set-custom-xmpp-status').submit();
  112. expect(view.setStatusMessage).toHaveBeenCalled();
  113. expect(converse.emit).toHaveBeenCalledWith('statusMessageChanged', msg);
  114. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  115. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg);
  116. });
  117. }, converse));
  118. }, converse));
  119. }, converse, mock, utils));
  120. describe("The Contacts Roster", $.proxy(function (mock, utils) {
  121. describe("Roster Groups", $.proxy(function () {
  122. function _clearContacts () {
  123. utils.clearBrowserStorage();
  124. converse.rosterview.model.reset();
  125. converse.rosterview.initialize(); // Register new listeners and document fragment
  126. };
  127. it("can be used to organize existing contacts", $.proxy(function () {
  128. _clearContacts();
  129. var i=0, j=0, t;
  130. spyOn(converse, 'emit');
  131. spyOn(this.rosterview, 'updateCount').andCallThrough();
  132. converse.roster_groups = true;
  133. converse.rosterview.render();
  134. utils.createContacts('pending');
  135. utils.createContacts('requesting');
  136. var groups = {
  137. 'colleagues': 3,
  138. 'friends & acquaintences': 3,
  139. 'Family': 4,
  140. 'ænemies': 3,
  141. 'Ungrouped': 2
  142. };
  143. _.each(_.keys(groups), $.proxy(function (name) {
  144. j = i;
  145. for (i=j; i<j+groups[name]; i++) {
  146. this.roster.create({
  147. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  148. subscription: 'both',
  149. ask: null,
  150. groups: name === 'ungrouped'? [] : [name],
  151. fullname: mock.cur_names[i],
  152. is_last: i===(mock.cur_names.length-1)
  153. });
  154. }
  155. }, converse));
  156. // Check that the groups appear alphabetically and that
  157. // requesting and pending contacts are last.
  158. var group_titles = $.map(this.rosterview.$el.find('dt'), function (o) { return $(o).text().trim(); });
  159. expect(group_titles).toEqual([
  160. "colleagues",
  161. "Family",
  162. "friends & acquaintences",
  163. "Ungrouped",
  164. "Contact requests",
  165. "Pending contacts"
  166. ]);
  167. // Check that usernames appear alphabetically per group
  168. _.each(_.keys(groups), $.proxy(function (name) {
  169. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  170. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  171. expect(names).toEqual(_.clone(names).sort());
  172. }, converse));
  173. }, converse));
  174. it("can share contacts among them (values aren't distinct)", $.proxy(function () {
  175. // TODO: this test is not finished yet and the thing that it's
  176. // testing not yet implemented.
  177. _clearContacts();
  178. var i=0, j=0, t;
  179. spyOn(converse, 'emit');
  180. spyOn(this.rosterview, 'updateCount').andCallThrough();
  181. converse.roster_groups = true;
  182. converse.rosterview.render();
  183. for (i=0; i<mock.cur_names.length; i++) {
  184. this.roster.create({
  185. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  186. subscription: 'both',
  187. ask: null,
  188. groups: ['colleagues', 'friends'],
  189. fullname: mock.cur_names[i],
  190. is_last: i===(mock.cur_names.length-1)
  191. });
  192. }
  193. }, converse));
  194. }, converse));
  195. describe("Pending Contacts", $.proxy(function () {
  196. function _clearContacts () {
  197. utils.clearBrowserStorage();
  198. converse.rosterview.model.reset();
  199. converse.rosterview.initialize(); // Register new listeners and document fragment
  200. };
  201. function _addContacts () {
  202. _clearContacts();
  203. // Must be initialized, so that render is called and documentFragment set up.
  204. utils.createContacts('pending').openControlBox().openContactsPanel();
  205. };
  206. it("can be collapsed under their own header", $.proxy(function () {
  207. _addContacts();
  208. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt#pending-xmpp-contacts')]);
  209. }, converse));
  210. it("can be added to the roster", $.proxy(function () {
  211. _clearContacts();
  212. spyOn(converse, 'emit');
  213. spyOn(this.rosterview, 'updateCount').andCallThrough();
  214. runs($.proxy(function () {
  215. this.roster.create({
  216. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  217. subscription: 'none',
  218. ask: 'subscribe',
  219. fullname: mock.pend_names[0],
  220. is_last: true
  221. });
  222. }, converse));
  223. waits(300);
  224. runs($.proxy(function () {
  225. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  226. expect(this.rosterview.updateCount).toHaveBeenCalled();
  227. }, converse));
  228. }, converse));
  229. it("can be removed by the user", $.proxy(function () {
  230. _addContacts();
  231. var name = mock.pend_names[0];
  232. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  233. spyOn(window, 'confirm').andReturn(true);
  234. spyOn(converse, 'emit');
  235. spyOn(this.connection.roster, 'remove').andCallThrough();
  236. spyOn(this.connection.roster, 'unauthorize');
  237. spyOn(this.rosterview.model, 'remove').andCallThrough();
  238. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  239. .siblings('.remove-xmpp-contact').click();
  240. expect(window.confirm).toHaveBeenCalled();
  241. expect(this.connection.roster.remove).toHaveBeenCalled();
  242. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  243. expect(this.rosterview.model.remove).toHaveBeenCalled();
  244. expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
  245. }, converse));
  246. it("do not have a header if there aren't any", $.proxy(function () {
  247. var name = mock.pend_names[0];
  248. _clearContacts();
  249. spyOn(window, 'confirm').andReturn(true);
  250. this.roster.create({
  251. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  252. subscription: 'none',
  253. ask: 'subscribe',
  254. fullname: name,
  255. is_last: true
  256. });
  257. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('block');
  258. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  259. .siblings('.remove-xmpp-contact').click();
  260. expect(window.confirm).toHaveBeenCalled();
  261. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none');
  262. }, converse));
  263. it("will lose their own header once the last one has been removed", $.proxy(function () {
  264. _addContacts();
  265. var name;
  266. spyOn(window, 'confirm').andReturn(true);
  267. for (i=0; i<mock.pend_names.length; i++) {
  268. name = mock.pend_names[i];
  269. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  270. .siblings('.remove-xmpp-contact').click();
  271. }
  272. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  273. }, converse));
  274. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  275. _clearContacts();
  276. var i, t, is_last;
  277. spyOn(converse, 'emit');
  278. spyOn(this.rosterview, 'updateCount').andCallThrough();
  279. for (i=0; i<mock.pend_names.length; i++) {
  280. is_last = i===(mock.pend_names.length-1);
  281. this.roster.create({
  282. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  283. subscription: 'none',
  284. ask: 'subscribe',
  285. fullname: mock.pend_names[i],
  286. is_last: is_last
  287. });
  288. expect(this.rosterview.updateCount).toHaveBeenCalled();
  289. }
  290. // Check that they are sorted alphabetically
  291. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').find('span').text();
  292. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  293. }, converse));
  294. }, converse));
  295. describe("Existing Contacts", $.proxy(function () {
  296. function _clearContacts () {
  297. utils.clearBrowserStorage();
  298. converse.rosterview.model.reset();
  299. converse.rosterview.initialize(); // Register new listeners and document fragment
  300. };
  301. var _addContacts = function () {
  302. _clearContacts();
  303. utils.createContacts().openControlBox().openContactsPanel();
  304. };
  305. it("can be collapsed under their own header", $.proxy(function () {
  306. _addContacts();
  307. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]);
  308. }, converse));
  309. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  310. _clearContacts();
  311. var i, t;
  312. spyOn(converse, 'emit');
  313. spyOn(this.rosterview, 'updateCount').andCallThrough();
  314. for (i=0; i<mock.cur_names.length; i++) {
  315. this.roster.create({
  316. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  317. subscription: 'both',
  318. ask: null,
  319. fullname: mock.cur_names[i],
  320. is_last: i===(mock.cur_names.length-1)
  321. });
  322. expect(this.rosterview.updateCount).toHaveBeenCalled();
  323. }
  324. // Check that they are sorted alphabetically
  325. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  326. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  327. }, converse));
  328. it("can be removed by the user", $.proxy(function () {
  329. _addContacts();
  330. var name = mock.cur_names[0];
  331. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  332. spyOn(window, 'confirm').andReturn(true);
  333. spyOn(converse, 'emit');
  334. spyOn(this.connection.roster, 'remove').andCallThrough();
  335. spyOn(this.connection.roster, 'unauthorize');
  336. spyOn(this.rosterview.model, 'remove').andCallThrough();
  337. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  338. .siblings('.remove-xmpp-contact').click();
  339. expect(window.confirm).toHaveBeenCalled();
  340. expect(this.connection.roster.remove).toHaveBeenCalled();
  341. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  342. expect(this.rosterview.model.remove).toHaveBeenCalled();
  343. expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
  344. }, converse));
  345. it("do not have a header if there aren't any", $.proxy(function () {
  346. var name = mock.cur_names[0];
  347. _clearContacts();
  348. spyOn(window, 'confirm').andReturn(true);
  349. this.roster.create({
  350. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  351. subscription: 'both',
  352. ask: null,
  353. fullname: name,
  354. is_last: true
  355. });
  356. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
  357. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  358. .siblings('.remove-xmpp-contact').click();
  359. expect(window.confirm).toHaveBeenCalled();
  360. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  361. }, converse));
  362. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  363. _addContacts();
  364. var jid, t;
  365. spyOn(converse, 'emit');
  366. spyOn(this.rosterview, 'updateCount').andCallThrough();
  367. for (i=0; i<mock.cur_names.length; i++) {
  368. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  369. this.roster.get(jid).set('chat_status', 'online');
  370. expect(this.rosterview.updateCount).toHaveBeenCalled();
  371. // Check that they are sorted alphabetically
  372. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  373. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  374. }
  375. }, converse));
  376. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  377. _addContacts();
  378. var jid, t;
  379. spyOn(converse, 'emit');
  380. spyOn(this.rosterview, 'updateCount').andCallThrough();
  381. for (i=0; i<mock.cur_names.length; i++) {
  382. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  383. this.roster.get(jid).set('chat_status', 'dnd');
  384. expect(this.rosterview.updateCount).toHaveBeenCalled();
  385. // Check that they are sorted alphabetically
  386. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  387. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  388. }
  389. }, converse));
  390. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  391. _addContacts();
  392. var jid, t;
  393. spyOn(converse, 'emit');
  394. spyOn(this.rosterview, 'updateCount').andCallThrough();
  395. for (i=0; i<mock.cur_names.length; i++) {
  396. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  397. this.roster.get(jid).set('chat_status', 'away');
  398. expect(this.rosterview.updateCount).toHaveBeenCalled();
  399. // Check that they are sorted alphabetically
  400. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  401. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  402. }
  403. }, converse));
  404. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  405. _addContacts();
  406. var jid, t;
  407. spyOn(converse, 'emit');
  408. spyOn(this.rosterview, 'updateCount').andCallThrough();
  409. for (i=0; i<mock.cur_names.length; i++) {
  410. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  411. this.roster.get(jid).set('chat_status', 'xa');
  412. expect(this.rosterview.updateCount).toHaveBeenCalled();
  413. // Check that they are sorted alphabetically
  414. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  415. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  416. }
  417. }, converse));
  418. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  419. _addContacts();
  420. var jid, t;
  421. spyOn(converse, 'emit');
  422. spyOn(this.rosterview, 'updateCount').andCallThrough();
  423. for (i=0; i<mock.cur_names.length; i++) {
  424. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  425. this.roster.get(jid).set('chat_status', 'unavailable');
  426. expect(this.rosterview.updateCount).toHaveBeenCalled();
  427. // Check that they are sorted alphabetically
  428. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  429. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  430. }
  431. }, converse));
  432. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  433. _addContacts();
  434. var i;
  435. for (i=0; i<3; i++) {
  436. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  437. this.roster.get(jid).set('chat_status', 'online');
  438. }
  439. for (i=3; i<6; i++) {
  440. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  441. this.roster.get(jid).set('chat_status', 'dnd');
  442. }
  443. for (i=6; i<9; i++) {
  444. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  445. this.roster.get(jid).set('chat_status', 'away');
  446. }
  447. for (i=9; i<12; i++) {
  448. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  449. this.roster.get(jid).set('chat_status', 'xa');
  450. }
  451. for (i=12; i<15; i++) {
  452. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  453. this.roster.get(jid).set('chat_status', 'unavailable');
  454. }
  455. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  456. for (i=0; i<3; i++) {
  457. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  458. }
  459. for (i=3; i<6; i++) {
  460. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  461. }
  462. for (i=6; i<9; i++) {
  463. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  464. }
  465. for (i=9; i<12; i++) {
  466. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  467. }
  468. for (i=12; i<15; i++) {
  469. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  470. }
  471. for (i=15; i<mock.cur_names.length; i++) {
  472. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  473. }
  474. }, converse));
  475. }, converse));
  476. describe("Requesting Contacts", $.proxy(function () {
  477. beforeEach($.proxy(function () {
  478. runs(function () {
  479. utils.clearBrowserStorage();
  480. converse.rosterview.model.reset();
  481. utils.createContacts('requesting').openControlBox();
  482. });
  483. waits(50);
  484. runs(function () {
  485. utils.openContactsPanel();
  486. });
  487. }, converse));
  488. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  489. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  490. var i, children;
  491. var names = [];
  492. spyOn(converse, 'emit');
  493. spyOn(this.rosterview, 'updateCount').andCallThrough();
  494. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  495. var addName = function (idx, item) {
  496. if (!$(item).hasClass('request-actions')) {
  497. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  498. }
  499. };
  500. for (i=0; i<mock.req_names.length; i++) {
  501. this.roster.create({
  502. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  503. subscription: 'none',
  504. ask: null,
  505. requesting: true,
  506. fullname: mock.req_names[i],
  507. is_last: i===(mock.req_names.length-1)
  508. });
  509. expect(this.rosterview.updateCount).toHaveBeenCalled();
  510. // When a requesting contact is added, the controlbox must
  511. // be opened.
  512. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  513. }
  514. // Check that they are sorted alphabetically
  515. children = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').children('span');
  516. names = [];
  517. children.each(addName);
  518. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  519. }, converse));
  520. it("do not have a header if there aren't any", $.proxy(function () {
  521. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  522. var name = mock.req_names[0];
  523. spyOn(window, 'confirm').andReturn(true);
  524. this.roster.create({
  525. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  526. subscription: 'none',
  527. ask: null,
  528. requesting: true,
  529. fullname: name,
  530. is_last: true
  531. });
  532. expect(this.rosterview.$('dt#xmpp-contact-requests').css('display')).toEqual('block');
  533. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  534. .siblings('.request-actions')
  535. .find('.decline-xmpp-request').click();
  536. expect(window.confirm).toHaveBeenCalled();
  537. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  538. }, converse));
  539. it("can be collapsed under their own header", $.proxy(function () {
  540. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt#xmpp-contact-requests')]);
  541. }, converse));
  542. it("can have their requests accepted by the user", $.proxy(function () {
  543. // TODO: Testing can be more thorough here, the user is
  544. // actually not accepted/authorized because of
  545. // mock_connection.
  546. var name = mock.req_names.sort()[0];
  547. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  548. spyOn(this.connection.roster, 'authorize');
  549. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  550. .siblings('.request-actions')
  551. .find('.accept-xmpp-request').click();
  552. expect(this.connection.roster.authorize).toHaveBeenCalled();
  553. }, converse));
  554. it("can have their requests denied by the user", $.proxy(function () {
  555. this.rosterview.model.reset();
  556. spyOn(converse, 'emit');
  557. spyOn(this.connection.roster, 'unauthorize');
  558. spyOn(this.rosterview, 'update').andCallThrough();
  559. spyOn(window, 'confirm').andReturn(true);
  560. this.rosterview.initialize(); // Must be initialized only after the spy has been called
  561. utils.createContacts('requesting').openControlBox();
  562. var name = mock.req_names.sort()[1];
  563. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  564. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  565. .siblings('.request-actions')
  566. .find('.decline-xmpp-request').click();
  567. expect(window.confirm).toHaveBeenCalled();
  568. expect(this.rosterview.update).toHaveBeenCalled();
  569. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  570. // There should now be one less contact
  571. expect(this.roster.length).toEqual(mock.req_names.length-1);
  572. }, converse));
  573. }, converse));
  574. describe("All Contacts", $.proxy(function () {
  575. beforeEach($.proxy(function () {
  576. runs(function () {
  577. utils.clearBrowserStorage();
  578. converse.rosterview.model.reset();
  579. converse.rosterview.model.browserStorage._clear();
  580. utils.createContacts('all').openControlBox();
  581. });
  582. waits(50);
  583. runs(function () {
  584. utils.openContactsPanel();
  585. });
  586. }, converse));
  587. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  588. var new_attrs, old_attrs, attrs, old_roster;
  589. var num_contacts = this.roster.length;
  590. new_roster = new this.RosterItems();
  591. // Roster items are yet to be fetched from browserStorage
  592. expect(new_roster.length).toEqual(0);
  593. new_roster.browserStorage = new Backbone.BrowserStorage.session(
  594. b64_sha1('converse.rosteritems-dummy@localhost'));
  595. new_roster.fetch();
  596. expect(new_roster.length).toEqual(num_contacts);
  597. // Check that the roster items retrieved from browserStorage
  598. // have the same attributes values as the original ones.
  599. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  600. for (i=0; i<attrs.length; i++) {
  601. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  602. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  603. // Roster items in storage are not necessarily sorted,
  604. // so we have to sort them here to do a proper
  605. // comparison
  606. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  607. }
  608. // XXX: this.rosterview.updateCount();
  609. }, converse));
  610. afterEach($.proxy(function () {
  611. // Contacts retrieved from browserStorage have chat_status of
  612. // "offline".
  613. // In the next test suite, we need some online contacts, so
  614. // we make some online now
  615. for (i=0; i<5; i++) {
  616. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  617. this.roster.get(jid).set('chat_status', 'online');
  618. }
  619. }, converse));
  620. }, converse));
  621. }, converse, mock, utils));
  622. describe("The 'Add Contact' widget", $.proxy(function (mock, utils) {
  623. it("opens up an add form when you click on it", $.proxy(function () {
  624. var panel = this.chatboxviews.get('controlbox').contactspanel;
  625. spyOn(panel, 'toggleContactForm').andCallThrough();
  626. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  627. panel.$el.find('a.toggle-xmpp-contact-form').click();
  628. expect(panel.toggleContactForm).toHaveBeenCalled();
  629. // XXX: Awaiting more tests, close it again for now...
  630. panel.$el.find('a.toggle-xmpp-contact-form').click();
  631. }, converse));
  632. }, converse, mock, utils));
  633. describe("The Controlbox Tabs", $.proxy(function () {
  634. beforeEach($.proxy(function () {
  635. runs(function () {
  636. utils.closeAllChatBoxes();
  637. });
  638. waits(50);
  639. runs(function () {
  640. utils.openControlBox();
  641. });
  642. }, converse));
  643. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  644. var cbview = this.chatboxviews.get('controlbox');
  645. var $panels = cbview.$el.find('.controlbox-panes');
  646. expect($panels.children().length).toBe(2);
  647. expect($panels.children().first().attr('id')).toBe('users');
  648. expect($panels.children().first().is(':visible')).toBe(true);
  649. expect($panels.children().last().attr('id')).toBe('chatrooms');
  650. expect($panels.children().last().is(':visible')).toBe(false);
  651. }, converse));
  652. describe("chatrooms panel", $.proxy(function () {
  653. beforeEach($.proxy(function () {
  654. runs(function () {
  655. utils.closeAllChatBoxes();
  656. });
  657. waits(50);
  658. runs(function () {
  659. utils.openControlBox();
  660. });
  661. }, converse));
  662. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  663. var cbview = this.chatboxviews.get('controlbox');
  664. var $tabs = cbview.$el.find('#controlbox-tabs');
  665. var $panels = cbview.$el.find('.controlbox-panes');
  666. var $contacts = $panels.children().first();
  667. var $chatrooms = $panels.children().last();
  668. spyOn(cbview, 'switchTab').andCallThrough();
  669. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  670. runs(function () {
  671. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  672. });
  673. waits(250);
  674. runs(function () {
  675. expect($contacts.is(':visible')).toBe(false);
  676. expect($chatrooms.is(':visible')).toBe(true);
  677. expect(cbview.switchTab).toHaveBeenCalled();
  678. });
  679. }, converse));
  680. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  681. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  682. var $input = roomspanel.$el.find('input.new-chatroom-name');
  683. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  684. var $server = roomspanel.$el.find('input.new-chatroom-server');
  685. expect($input.length).toBe(1);
  686. expect($server.length).toBe(1);
  687. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  688. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  689. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  690. runs(function () {
  691. $input.val('Lounge');
  692. $nick.val('dummy');
  693. $server.val('muc.localhost');
  694. });
  695. waits('250');
  696. runs(function () {
  697. roomspanel.$el.find('form').submit();
  698. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  699. });
  700. waits('250');
  701. runs($.proxy(function () {
  702. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  703. }, converse));
  704. }, converse));
  705. }, converse));
  706. }, converse, mock, utils));
  707. }));