controlbox.js 38 KB

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