2
0

controlbox.js 39 KB

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