controlbox.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. var groups = ['colleagues', 'friends'];
  184. for (i=0; i<mock.cur_names.length; i++) {
  185. this.roster.create({
  186. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  187. subscription: 'both',
  188. ask: null,
  189. groups: groups,
  190. fullname: mock.cur_names[i],
  191. is_last: i===(mock.cur_names.length-1)
  192. });
  193. }
  194. // Check that usernames appear alphabetically per group
  195. _.each(groups, $.proxy(function (name) {
  196. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  197. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  198. expect(names).toEqual(_.clone(names).sort());
  199. expect(names.length).toEqual(mock.cur_names.length);
  200. }, converse));
  201. }, converse));
  202. }, converse));
  203. describe("Pending Contacts", $.proxy(function () {
  204. function _clearContacts () {
  205. utils.clearBrowserStorage();
  206. converse.rosterview.model.reset();
  207. converse.rosterview.initialize(); // Register new listeners and document fragment
  208. };
  209. function _addContacts () {
  210. _clearContacts();
  211. // Must be initialized, so that render is called and documentFragment set up.
  212. utils.createContacts('pending').openControlBox().openContactsPanel();
  213. };
  214. it("can be collapsed under their own header", $.proxy(function () {
  215. _addContacts();
  216. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt#pending-xmpp-contacts')]);
  217. }, converse));
  218. it("can be added to the roster", $.proxy(function () {
  219. _clearContacts();
  220. spyOn(converse, 'emit');
  221. spyOn(this.rosterview, 'updateCount').andCallThrough();
  222. runs($.proxy(function () {
  223. this.roster.create({
  224. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  225. subscription: 'none',
  226. ask: 'subscribe',
  227. fullname: mock.pend_names[0],
  228. is_last: true
  229. });
  230. }, converse));
  231. waits(300);
  232. runs($.proxy(function () {
  233. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  234. expect(this.rosterview.updateCount).toHaveBeenCalled();
  235. }, converse));
  236. }, converse));
  237. it("can be removed by the user", $.proxy(function () {
  238. _addContacts();
  239. var name = mock.pend_names[0];
  240. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  241. spyOn(window, 'confirm').andReturn(true);
  242. spyOn(converse, 'emit');
  243. spyOn(this.connection.roster, 'remove').andCallThrough();
  244. spyOn(this.connection.roster, 'unauthorize');
  245. spyOn(this.rosterview.model, 'remove').andCallThrough();
  246. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  247. .siblings('.remove-xmpp-contact').click();
  248. expect(window.confirm).toHaveBeenCalled();
  249. expect(this.connection.roster.remove).toHaveBeenCalled();
  250. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  251. expect(this.rosterview.model.remove).toHaveBeenCalled();
  252. expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
  253. }, converse));
  254. it("do not have a header if there aren't any", $.proxy(function () {
  255. var name = mock.pend_names[0];
  256. _clearContacts();
  257. spyOn(window, 'confirm').andReturn(true);
  258. this.roster.create({
  259. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  260. subscription: 'none',
  261. ask: 'subscribe',
  262. fullname: name,
  263. is_last: true
  264. });
  265. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('block');
  266. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  267. .siblings('.remove-xmpp-contact').click();
  268. expect(window.confirm).toHaveBeenCalled();
  269. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none');
  270. }, converse));
  271. it("will lose their own header once the last one has been removed", $.proxy(function () {
  272. _addContacts();
  273. var name;
  274. spyOn(window, 'confirm').andReturn(true);
  275. for (i=0; i<mock.pend_names.length; i++) {
  276. name = mock.pend_names[i];
  277. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  278. .siblings('.remove-xmpp-contact').click();
  279. }
  280. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  281. }, converse));
  282. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  283. _clearContacts();
  284. var i, t, is_last;
  285. spyOn(converse, 'emit');
  286. spyOn(this.rosterview, 'updateCount').andCallThrough();
  287. for (i=0; i<mock.pend_names.length; i++) {
  288. is_last = i===(mock.pend_names.length-1);
  289. this.roster.create({
  290. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  291. subscription: 'none',
  292. ask: 'subscribe',
  293. fullname: mock.pend_names[i],
  294. is_last: is_last
  295. });
  296. expect(this.rosterview.updateCount).toHaveBeenCalled();
  297. }
  298. // Check that they are sorted alphabetically
  299. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').find('span').text();
  300. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  301. }, converse));
  302. }, converse));
  303. describe("Existing Contacts", $.proxy(function () {
  304. function _clearContacts () {
  305. utils.clearBrowserStorage();
  306. converse.rosterview.model.reset();
  307. converse.rosterview.initialize(); // Register new listeners and document fragment
  308. };
  309. var _addContacts = function () {
  310. _clearContacts();
  311. utils.createContacts().openControlBox().openContactsPanel();
  312. };
  313. it("can be collapsed under their own header", $.proxy(function () {
  314. _addContacts();
  315. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]);
  316. }, converse));
  317. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  318. _clearContacts();
  319. var i, t;
  320. spyOn(converse, 'emit');
  321. spyOn(this.rosterview, 'updateCount').andCallThrough();
  322. for (i=0; i<mock.cur_names.length; i++) {
  323. this.roster.create({
  324. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  325. subscription: 'both',
  326. ask: null,
  327. fullname: mock.cur_names[i],
  328. is_last: i===(mock.cur_names.length-1)
  329. });
  330. expect(this.rosterview.updateCount).toHaveBeenCalled();
  331. }
  332. // Check that they are sorted alphabetically
  333. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  334. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  335. }, converse));
  336. it("can be removed by the user", $.proxy(function () {
  337. _addContacts();
  338. var name = mock.cur_names[0];
  339. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  340. spyOn(window, 'confirm').andReturn(true);
  341. spyOn(converse, 'emit');
  342. spyOn(this.connection.roster, 'remove').andCallThrough();
  343. spyOn(this.connection.roster, 'unauthorize');
  344. spyOn(this.rosterview.model, 'remove').andCallThrough();
  345. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  346. .siblings('.remove-xmpp-contact').click();
  347. expect(window.confirm).toHaveBeenCalled();
  348. expect(this.connection.roster.remove).toHaveBeenCalled();
  349. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  350. expect(this.rosterview.model.remove).toHaveBeenCalled();
  351. expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
  352. }, converse));
  353. it("do not have a header if there aren't any", $.proxy(function () {
  354. var name = mock.cur_names[0];
  355. _clearContacts();
  356. spyOn(window, 'confirm').andReturn(true);
  357. this.roster.create({
  358. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  359. subscription: 'both',
  360. ask: null,
  361. fullname: name,
  362. is_last: true
  363. });
  364. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
  365. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  366. .siblings('.remove-xmpp-contact').click();
  367. expect(window.confirm).toHaveBeenCalled();
  368. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  369. }, converse));
  370. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  371. _addContacts();
  372. var jid, t;
  373. spyOn(converse, 'emit');
  374. spyOn(this.rosterview, 'updateCount').andCallThrough();
  375. for (i=0; i<mock.cur_names.length; i++) {
  376. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  377. this.roster.get(jid).set('chat_status', 'online');
  378. expect(this.rosterview.updateCount).toHaveBeenCalled();
  379. // Check that they are sorted alphabetically
  380. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  381. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  382. }
  383. }, converse));
  384. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  385. _addContacts();
  386. var jid, t;
  387. spyOn(converse, 'emit');
  388. spyOn(this.rosterview, 'updateCount').andCallThrough();
  389. for (i=0; i<mock.cur_names.length; i++) {
  390. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  391. this.roster.get(jid).set('chat_status', 'dnd');
  392. expect(this.rosterview.updateCount).toHaveBeenCalled();
  393. // Check that they are sorted alphabetically
  394. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  395. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  396. }
  397. }, converse));
  398. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  399. _addContacts();
  400. var jid, t;
  401. spyOn(converse, 'emit');
  402. spyOn(this.rosterview, 'updateCount').andCallThrough();
  403. for (i=0; i<mock.cur_names.length; i++) {
  404. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  405. this.roster.get(jid).set('chat_status', 'away');
  406. expect(this.rosterview.updateCount).toHaveBeenCalled();
  407. // Check that they are sorted alphabetically
  408. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  409. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  410. }
  411. }, converse));
  412. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  413. _addContacts();
  414. var jid, t;
  415. spyOn(converse, 'emit');
  416. spyOn(this.rosterview, 'updateCount').andCallThrough();
  417. for (i=0; i<mock.cur_names.length; i++) {
  418. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  419. this.roster.get(jid).set('chat_status', 'xa');
  420. expect(this.rosterview.updateCount).toHaveBeenCalled();
  421. // Check that they are sorted alphabetically
  422. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  423. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  424. }
  425. }, converse));
  426. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  427. _addContacts();
  428. var jid, t;
  429. spyOn(converse, 'emit');
  430. spyOn(this.rosterview, 'updateCount').andCallThrough();
  431. for (i=0; i<mock.cur_names.length; i++) {
  432. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  433. this.roster.get(jid).set('chat_status', 'unavailable');
  434. expect(this.rosterview.updateCount).toHaveBeenCalled();
  435. // Check that they are sorted alphabetically
  436. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  437. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  438. }
  439. }, converse));
  440. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  441. _addContacts();
  442. var i;
  443. for (i=0; i<3; i++) {
  444. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  445. this.roster.get(jid).set('chat_status', 'online');
  446. }
  447. for (i=3; i<6; i++) {
  448. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  449. this.roster.get(jid).set('chat_status', 'dnd');
  450. }
  451. for (i=6; i<9; i++) {
  452. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  453. this.roster.get(jid).set('chat_status', 'away');
  454. }
  455. for (i=9; i<12; i++) {
  456. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  457. this.roster.get(jid).set('chat_status', 'xa');
  458. }
  459. for (i=12; i<15; i++) {
  460. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  461. this.roster.get(jid).set('chat_status', 'unavailable');
  462. }
  463. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  464. for (i=0; i<3; i++) {
  465. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  466. }
  467. for (i=3; i<6; i++) {
  468. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  469. }
  470. for (i=6; i<9; i++) {
  471. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  472. }
  473. for (i=9; i<12; i++) {
  474. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  475. }
  476. for (i=12; i<15; i++) {
  477. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  478. }
  479. for (i=15; i<mock.cur_names.length; i++) {
  480. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  481. }
  482. }, converse));
  483. }, converse));
  484. describe("Requesting Contacts", $.proxy(function () {
  485. beforeEach($.proxy(function () {
  486. runs(function () {
  487. utils.clearBrowserStorage();
  488. converse.rosterview.model.reset();
  489. utils.createContacts('requesting').openControlBox();
  490. });
  491. waits(50);
  492. runs(function () {
  493. utils.openContactsPanel();
  494. });
  495. }, converse));
  496. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  497. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  498. var i, children;
  499. var names = [];
  500. spyOn(converse, 'emit');
  501. spyOn(this.rosterview, 'updateCount').andCallThrough();
  502. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  503. var addName = function (idx, item) {
  504. if (!$(item).hasClass('request-actions')) {
  505. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  506. }
  507. };
  508. for (i=0; i<mock.req_names.length; i++) {
  509. this.roster.create({
  510. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  511. subscription: 'none',
  512. ask: null,
  513. requesting: true,
  514. fullname: mock.req_names[i],
  515. is_last: i===(mock.req_names.length-1)
  516. });
  517. expect(this.rosterview.updateCount).toHaveBeenCalled();
  518. // When a requesting contact is added, the controlbox must
  519. // be opened.
  520. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  521. }
  522. // Check that they are sorted alphabetically
  523. children = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').children('span');
  524. names = [];
  525. children.each(addName);
  526. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  527. }, converse));
  528. it("do not have a header if there aren't any", $.proxy(function () {
  529. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  530. var name = mock.req_names[0];
  531. spyOn(window, 'confirm').andReturn(true);
  532. this.roster.create({
  533. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  534. subscription: 'none',
  535. ask: null,
  536. requesting: true,
  537. fullname: name,
  538. is_last: true
  539. });
  540. expect(this.rosterview.$('dt#xmpp-contact-requests').css('display')).toEqual('block');
  541. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  542. .siblings('.request-actions')
  543. .find('.decline-xmpp-request').click();
  544. expect(window.confirm).toHaveBeenCalled();
  545. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  546. }, converse));
  547. it("can be collapsed under their own header", $.proxy(function () {
  548. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt#xmpp-contact-requests')]);
  549. }, converse));
  550. it("can have their requests accepted by the user", $.proxy(function () {
  551. // TODO: Testing can be more thorough here, the user is
  552. // actually not accepted/authorized because of
  553. // mock_connection.
  554. var name = mock.req_names.sort()[0];
  555. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  556. spyOn(this.connection.roster, 'authorize');
  557. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  558. .siblings('.request-actions')
  559. .find('.accept-xmpp-request').click();
  560. expect(this.connection.roster.authorize).toHaveBeenCalled();
  561. }, converse));
  562. it("can have their requests denied by the user", $.proxy(function () {
  563. this.rosterview.model.reset();
  564. spyOn(converse, 'emit');
  565. spyOn(this.connection.roster, 'unauthorize');
  566. spyOn(this.rosterview, 'update').andCallThrough();
  567. spyOn(window, 'confirm').andReturn(true);
  568. this.rosterview.initialize(); // Must be initialized only after the spy has been called
  569. utils.createContacts('requesting').openControlBox();
  570. var name = mock.req_names.sort()[1];
  571. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  572. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  573. .siblings('.request-actions')
  574. .find('.decline-xmpp-request').click();
  575. expect(window.confirm).toHaveBeenCalled();
  576. expect(this.rosterview.update).toHaveBeenCalled();
  577. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  578. // There should now be one less contact
  579. expect(this.roster.length).toEqual(mock.req_names.length-1);
  580. }, converse));
  581. }, converse));
  582. describe("All Contacts", $.proxy(function () {
  583. beforeEach($.proxy(function () {
  584. runs(function () {
  585. utils.clearBrowserStorage();
  586. converse.rosterview.model.reset();
  587. converse.rosterview.model.browserStorage._clear();
  588. utils.createContacts('all').openControlBox();
  589. });
  590. waits(50);
  591. runs(function () {
  592. utils.openContactsPanel();
  593. });
  594. }, converse));
  595. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  596. var new_attrs, old_attrs, attrs, old_roster;
  597. var num_contacts = this.roster.length;
  598. new_roster = new this.RosterContacts();
  599. // Roster items are yet to be fetched from browserStorage
  600. expect(new_roster.length).toEqual(0);
  601. new_roster.browserStorage = new Backbone.BrowserStorage.session(
  602. b64_sha1('converse.contacts-dummy@localhost'));
  603. new_roster.fetch();
  604. expect(new_roster.length).toEqual(num_contacts);
  605. // Check that the roster items retrieved from browserStorage
  606. // have the same attributes values as the original ones.
  607. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  608. for (i=0; i<attrs.length; i++) {
  609. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  610. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  611. // Roster items in storage are not necessarily sorted,
  612. // so we have to sort them here to do a proper
  613. // comparison
  614. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  615. }
  616. // XXX: this.rosterview.updateCount();
  617. }, converse));
  618. afterEach($.proxy(function () {
  619. // Contacts retrieved from browserStorage have chat_status of
  620. // "offline".
  621. // In the next test suite, we need some online contacts, so
  622. // we make some online now
  623. for (i=0; i<5; i++) {
  624. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  625. this.roster.get(jid).set('chat_status', 'online');
  626. }
  627. }, converse));
  628. }, converse));
  629. }, converse, mock, utils));
  630. describe("The 'Add Contact' widget", $.proxy(function (mock, utils) {
  631. it("opens up an add form when you click on it", $.proxy(function () {
  632. var panel = this.chatboxviews.get('controlbox').contactspanel;
  633. spyOn(panel, 'toggleContactForm').andCallThrough();
  634. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  635. panel.$el.find('a.toggle-xmpp-contact-form').click();
  636. expect(panel.toggleContactForm).toHaveBeenCalled();
  637. // XXX: Awaiting more tests, close it again for now...
  638. panel.$el.find('a.toggle-xmpp-contact-form').click();
  639. }, converse));
  640. }, converse, mock, utils));
  641. describe("The Controlbox Tabs", $.proxy(function () {
  642. beforeEach($.proxy(function () {
  643. runs(function () {
  644. utils.closeAllChatBoxes();
  645. });
  646. waits(50);
  647. runs(function () {
  648. utils.openControlBox();
  649. });
  650. }, converse));
  651. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  652. var cbview = this.chatboxviews.get('controlbox');
  653. var $panels = cbview.$el.find('.controlbox-panes');
  654. expect($panels.children().length).toBe(2);
  655. expect($panels.children().first().attr('id')).toBe('users');
  656. expect($panels.children().first().is(':visible')).toBe(true);
  657. expect($panels.children().last().attr('id')).toBe('chatrooms');
  658. expect($panels.children().last().is(':visible')).toBe(false);
  659. }, converse));
  660. describe("chatrooms panel", $.proxy(function () {
  661. beforeEach($.proxy(function () {
  662. runs(function () {
  663. utils.closeAllChatBoxes();
  664. });
  665. waits(50);
  666. runs(function () {
  667. utils.openControlBox();
  668. });
  669. }, converse));
  670. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  671. var cbview = this.chatboxviews.get('controlbox');
  672. var $tabs = cbview.$el.find('#controlbox-tabs');
  673. var $panels = cbview.$el.find('.controlbox-panes');
  674. var $contacts = $panels.children().first();
  675. var $chatrooms = $panels.children().last();
  676. spyOn(cbview, 'switchTab').andCallThrough();
  677. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  678. runs(function () {
  679. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  680. });
  681. waits(250);
  682. runs(function () {
  683. expect($contacts.is(':visible')).toBe(false);
  684. expect($chatrooms.is(':visible')).toBe(true);
  685. expect(cbview.switchTab).toHaveBeenCalled();
  686. });
  687. }, converse));
  688. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  689. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  690. var $input = roomspanel.$el.find('input.new-chatroom-name');
  691. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  692. var $server = roomspanel.$el.find('input.new-chatroom-server');
  693. expect($input.length).toBe(1);
  694. expect($server.length).toBe(1);
  695. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  696. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  697. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  698. runs(function () {
  699. $input.val('Lounge');
  700. $nick.val('dummy');
  701. $server.val('muc.localhost');
  702. });
  703. waits('250');
  704. runs(function () {
  705. roomspanel.$el.find('form').submit();
  706. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  707. });
  708. waits('250');
  709. runs($.proxy(function () {
  710. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  711. }, converse));
  712. }, converse));
  713. }, converse));
  714. }, converse, mock, utils));
  715. }));