controlbox.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. (function (root, factory) {
  2. define([
  3. "jquery",
  4. "mock",
  5. "test_utils"
  6. ], function ($, mock, test_utils) {
  7. return factory($, mock, test_utils);
  8. }
  9. );
  10. } (this, function ($, mock, test_utils) {
  11. var checkHeaderToggling = function ($header) {
  12. var $toggle = $header.find('a.group-toggle');
  13. expect($header.css('display')).toEqual('block');
  14. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  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, test_utils) {
  27. beforeEach(function () {
  28. runs(function () {
  29. test_utils.openControlBox();
  30. });
  31. });
  32. it("can be opened by clicking a DOM element with class 'toggle-controlbox'", $.proxy(function () {
  33. runs(function () {
  34. test_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. test_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, test_utils));
  117. describe("The Contacts Roster", $.proxy(function (mock, utils) {
  118. function _clearContacts () {
  119. utils.clearBrowserStorage();
  120. converse.rosterview.model.reset();
  121. }
  122. describe("The live filter", $.proxy(function () {
  123. it("will only appear when roster contacts flow over the visible area", $.proxy(function () {
  124. _clearContacts();
  125. var $filter = converse.rosterview.$('.roster-filter');
  126. var names = mock.cur_names;
  127. expect($filter.length).toBe(1);
  128. expect($filter.is(':visible')).toBeFalsy();
  129. for (i=0; i<names.length; i++) {
  130. converse.roster.create({
  131. ask: null,
  132. fullname: names[i],
  133. jid: names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  134. requesting: 'false',
  135. subscription: 'both'
  136. });
  137. converse.rosterview.update(); // XXX: Will normally called as event handler
  138. if (converse.rosterview.$('.roster-contacts').hasScrollBar()) {
  139. expect($filter.is(':visible')).toBeTruthy();
  140. } else {
  141. expect($filter.is(':visible')).toBeFalsy();
  142. }
  143. }
  144. }, converse));
  145. it("can be used to filter the contacts shown", function () {
  146. converse.roster_groups = true;
  147. _clearContacts();
  148. utils.createGroupedContacts();
  149. var $filter = converse.rosterview.$('.roster-filter');
  150. var $roster = converse.rosterview.$('.roster-contacts');
  151. runs(function () {
  152. expect($roster.find('dd:visible').length).toBe(15);
  153. expect($roster.find('dt:visible').length).toBe(5);
  154. $filter.val("candice");
  155. expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
  156. expect($roster.find('dt:visible').length).toBe(5); // ditto
  157. $filter.trigger('keydown');
  158. });
  159. waits(350); // Needed, due to debounce
  160. runs (function () {
  161. expect($roster.find('dd:visible').length).toBe(1);
  162. expect($roster.find('dd:visible').eq(0).text().trim()).toBe('Candice van der Knijff');
  163. expect($roster.find('dt:visible').length).toBe(1);
  164. expect($roster.find('dt:visible').eq(0).text()).toBe('colleagues');
  165. $filter.val("an");
  166. $filter.trigger('keydown');
  167. });
  168. waits(350); // Needed, due to debounce
  169. runs (function () {
  170. expect($roster.find('dd:visible').length).toBe(5);
  171. expect($roster.find('dt:visible').length).toBe(4);
  172. $filter.val("xxx");
  173. $filter.trigger('keydown');
  174. });
  175. waits(350); // Needed, due to debounce
  176. runs (function () {
  177. expect($roster.find('dd:visible').length).toBe(0);
  178. expect($roster.find('dt:visible').length).toBe(0);
  179. $filter.val(""); // Check that contacts are shown again, when the filter string is cleared.
  180. $filter.trigger('keydown');
  181. });
  182. waits(350); // Needed, due to debounce
  183. runs(function () {
  184. expect($roster.find('dd:visible').length).toBe(15);
  185. expect($roster.find('dt:visible').length).toBe(5);
  186. });
  187. converse.roster_groups = false;
  188. });
  189. it("can be used to filter the groups shown", function () {
  190. converse.roster_groups = true;
  191. _clearContacts();
  192. utils.createGroupedContacts();
  193. var $filter = converse.rosterview.$('.roster-filter');
  194. var $roster = converse.rosterview.$('.roster-contacts');
  195. var $type = converse.rosterview.$('.filter-type');
  196. $type.val('groups');
  197. runs(function () {
  198. expect($roster.find('dd:visible').length).toBe(15);
  199. expect($roster.find('dt:visible').length).toBe(5);
  200. $filter.val("colleagues");
  201. expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
  202. expect($roster.find('dt:visible').length).toBe(5); // ditto
  203. $filter.trigger('keydown');
  204. });
  205. waits(350); // Needed, due to debounce
  206. runs (function () {
  207. expect($roster.find('dt:visible').length).toBe(1);
  208. expect($roster.find('dt:visible').eq(0).text()).toBe('colleagues');
  209. // Check that all contacts under the group are shown
  210. expect($roster.find('dt:visible').nextUntil('dt', 'dd:hidden').length).toBe(0);
  211. $filter.val("xxx");
  212. $filter.trigger('keydown');
  213. });
  214. waits(350); // Needed, due to debounce
  215. runs (function () {
  216. expect($roster.find('dt:visible').length).toBe(0);
  217. $filter.val(""); // Check that groups are shown again, when the filter string is cleared.
  218. $filter.trigger('keydown');
  219. });
  220. waits(350); // Needed, due to debounce
  221. runs(function () {
  222. expect($roster.find('dd:visible').length).toBe(15);
  223. expect($roster.find('dt:visible').length).toBe(5);
  224. });
  225. converse.roster_groups = false;
  226. });
  227. it("has a button with which its contents can be cleared", function () {
  228. converse.roster_groups = true;
  229. _clearContacts();
  230. utils.createGroupedContacts();
  231. var $filter = converse.rosterview.$('.roster-filter');
  232. var $roster = converse.rosterview.$('.roster-contacts');
  233. runs (function () {
  234. $filter.val("xxx");
  235. $filter.trigger('keydown');
  236. expect($filter.hasClass("x")).toBeFalsy();
  237. });
  238. waits(350); // Needed, due to debounce
  239. runs (function () {
  240. expect($filter.hasClass("x")).toBeTruthy();
  241. $filter.addClass("onX").click();
  242. expect($filter.val()).toBe("");
  243. });
  244. converse.roster_groups = false;
  245. });
  246. }, converse));
  247. describe("A Roster Group", $.proxy(function () {
  248. beforeEach(function () {
  249. converse.roster_groups = true;
  250. });
  251. afterEach(function () {
  252. converse.roster_groups = false;
  253. });
  254. it("can be used to organize existing contacts", $.proxy(function () {
  255. _clearContacts();
  256. spyOn(converse, 'emit');
  257. spyOn(this.rosterview, 'update').andCallThrough();
  258. converse.rosterview.render();
  259. utils.createContacts('pending');
  260. utils.createContacts('requesting');
  261. utils.createGroupedContacts();
  262. // Check that the groups appear alphabetically and that
  263. // requesting and pending contacts are last.
  264. var group_titles = $.map(this.rosterview.$el.find('dt'), function (o) { return $(o).text().trim(); });
  265. expect(group_titles).toEqual([
  266. "colleagues",
  267. "Family",
  268. "friends & acquaintences",
  269. "ænemies",
  270. "Ungrouped",
  271. "Contact requests",
  272. "Pending contacts"
  273. ]);
  274. // Check that usernames appear alphabetically per group
  275. _.each(_.keys(mock.groups), $.proxy(function (name) {
  276. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  277. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  278. expect(names).toEqual(_.clone(names).sort());
  279. }, converse));
  280. }, converse));
  281. it("can share contacts with other roster groups", $.proxy(function () {
  282. _clearContacts();
  283. var i=0, j=0;
  284. spyOn(converse, 'emit');
  285. spyOn(this.rosterview, 'update').andCallThrough();
  286. converse.rosterview.render();
  287. var groups = ['colleagues', 'friends'];
  288. for (i=0; i<mock.cur_names.length; i++) {
  289. this.roster.create({
  290. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  291. subscription: 'both',
  292. ask: null,
  293. groups: groups,
  294. fullname: mock.cur_names[i]
  295. });
  296. }
  297. // Check that usernames appear alphabetically per group
  298. _.each(groups, $.proxy(function (name) {
  299. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  300. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  301. expect(names).toEqual(_.clone(names).sort());
  302. expect(names.length).toEqual(mock.cur_names.length);
  303. }, converse));
  304. }, converse));
  305. it("remembers whether it is closed or opened", $.proxy(function () {
  306. var i=0, j=0;
  307. var groups = {
  308. 'colleagues': 3,
  309. 'friends & acquaintences': 3,
  310. 'Ungrouped': 2
  311. };
  312. _.each(_.keys(groups), $.proxy(function (name) {
  313. j = i;
  314. for (i=j; i<j+groups[name]; i++) {
  315. this.roster.create({
  316. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  317. subscription: 'both',
  318. ask: null,
  319. groups: name === 'ungrouped'? [] : [name],
  320. fullname: mock.cur_names[i]
  321. });
  322. }
  323. }, converse));
  324. var view = this.rosterview.get('colleagues');
  325. var $toggle = view.$el.find('a.group-toggle');
  326. expect(view.model.get('state')).toBe('opened');
  327. $toggle.click();
  328. expect(view.model.get('state')).toBe('closed');
  329. $toggle.click();
  330. expect(view.model.get('state')).toBe('opened');
  331. }, converse));
  332. }, converse));
  333. describe("Pending Contacts", $.proxy(function () {
  334. function _clearContacts () {
  335. utils.clearBrowserStorage();
  336. converse.rosterview.model.reset();
  337. }
  338. function _addContacts () {
  339. _clearContacts();
  340. // Must be initialized, so that render is called and documentFragment set up.
  341. utils.createContacts('pending').openControlBox().openContactsPanel();
  342. }
  343. it("can be collapsed under their own header", $.proxy(function () {
  344. _addContacts();
  345. checkHeaderToggling.apply(this, [this.rosterview.get('Pending contacts').$el]);
  346. }, converse));
  347. it("can be added to the roster", $.proxy(function () {
  348. _clearContacts();
  349. spyOn(converse, 'emit');
  350. spyOn(this.rosterview, 'update').andCallThrough();
  351. runs($.proxy(function () {
  352. this.roster.create({
  353. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  354. subscription: 'none',
  355. ask: 'subscribe',
  356. fullname: mock.pend_names[0]
  357. });
  358. }, converse));
  359. waits(300);
  360. runs($.proxy(function () {
  361. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  362. expect(this.rosterview.update).toHaveBeenCalled();
  363. }, converse));
  364. }, converse));
  365. it("can be removed by the user", $.proxy(function () {
  366. _addContacts();
  367. var name = mock.pend_names[0];
  368. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  369. spyOn(window, 'confirm').andReturn(true);
  370. spyOn(converse, 'emit');
  371. spyOn(this.connection.roster, 'remove').andCallThrough();
  372. spyOn(this.connection.roster, 'unauthorize');
  373. spyOn(this.rosterview.model, 'remove').andCallThrough();
  374. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  375. .siblings('.remove-xmpp-contact').click();
  376. expect(window.confirm).toHaveBeenCalled();
  377. expect(this.connection.roster.remove).toHaveBeenCalled();
  378. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  379. expect(this.rosterview.model.remove).toHaveBeenCalled();
  380. expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
  381. }, converse));
  382. it("do not have a header if there aren't any", $.proxy(function () {
  383. var name = mock.pend_names[0];
  384. _clearContacts();
  385. spyOn(window, 'confirm').andReturn(true);
  386. this.roster.create({
  387. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  388. subscription: 'none',
  389. ask: 'subscribe',
  390. fullname: name
  391. });
  392. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(true);
  393. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  394. .siblings('.remove-xmpp-contact').click();
  395. expect(window.confirm).toHaveBeenCalled();
  396. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(false);
  397. }, converse));
  398. it("will lose their own header once the last one has been removed", $.proxy(function () {
  399. _addContacts();
  400. var name;
  401. spyOn(window, 'confirm').andReturn(true);
  402. for (i=0; i<mock.pend_names.length; i++) {
  403. name = mock.pend_names[i];
  404. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  405. .siblings('.remove-xmpp-contact').click();
  406. }
  407. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  408. }, converse));
  409. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  410. _clearContacts();
  411. var i, t;
  412. spyOn(converse, 'emit');
  413. spyOn(this.rosterview, 'update').andCallThrough();
  414. for (i=0; i<mock.pend_names.length; i++) {
  415. this.roster.create({
  416. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  417. subscription: 'none',
  418. ask: 'subscribe',
  419. fullname: mock.pend_names[i]
  420. });
  421. expect(this.rosterview.update).toHaveBeenCalled();
  422. }
  423. // Check that they are sorted alphabetically
  424. t = this.rosterview.get('Pending contacts').$el.siblings('dd.pending-xmpp-contact').find('span').text();
  425. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  426. }, converse));
  427. }, converse));
  428. describe("Existing Contacts", $.proxy(function () {
  429. function _clearContacts () {
  430. utils.clearBrowserStorage();
  431. converse.rosterview.model.reset();
  432. }
  433. var _addContacts = function () {
  434. _clearContacts();
  435. utils.createContacts('current').openControlBox().openContactsPanel();
  436. };
  437. it("can be collapsed under their own header", $.proxy(function () {
  438. _addContacts();
  439. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]);
  440. }, converse));
  441. it("will be hidden when appearing under a collapsed group", $.proxy(function () {
  442. _addContacts();
  443. this.rosterview.$el.find('dt.roster-group').find('a.group-toggle').click();
  444. var name = "Max Mustermann";
  445. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  446. converse.roster.create({
  447. ask: null,
  448. fullname: name,
  449. jid: jid,
  450. requesting: false,
  451. subscription: 'both'
  452. });
  453. var view = this.rosterview.get('My contacts').get(jid);
  454. expect(view.$el.is(':visible')).toBe(false);
  455. }, converse));
  456. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  457. _clearContacts();
  458. var i, t;
  459. spyOn(converse, 'emit');
  460. spyOn(this.rosterview, 'update').andCallThrough();
  461. for (i=0; i<mock.cur_names.length; i++) {
  462. this.roster.create({
  463. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  464. subscription: 'both',
  465. ask: null,
  466. fullname: mock.cur_names[i]
  467. });
  468. expect(this.rosterview.update).toHaveBeenCalled();
  469. }
  470. // Check that they are sorted alphabetically
  471. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  472. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  473. }, converse));
  474. it("can be removed by the user", $.proxy(function () {
  475. _addContacts();
  476. var name = mock.cur_names[0];
  477. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  478. spyOn(window, 'confirm').andReturn(true);
  479. spyOn(converse, 'emit');
  480. spyOn(this.connection.roster, 'remove').andCallThrough();
  481. spyOn(this.connection.roster, 'unauthorize');
  482. spyOn(this.rosterview.model, 'remove').andCallThrough();
  483. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  484. .siblings('.remove-xmpp-contact').click();
  485. expect(window.confirm).toHaveBeenCalled();
  486. expect(this.connection.roster.remove).toHaveBeenCalled();
  487. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  488. expect(this.rosterview.model.remove).toHaveBeenCalled();
  489. expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
  490. }, converse));
  491. it("do not have a header if there aren't any", $.proxy(function () {
  492. var name = mock.cur_names[0];
  493. _clearContacts();
  494. spyOn(window, 'confirm').andReturn(true);
  495. this.roster.create({
  496. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  497. subscription: 'both',
  498. ask: null,
  499. fullname: name
  500. });
  501. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
  502. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  503. .siblings('.remove-xmpp-contact').click();
  504. expect(window.confirm).toHaveBeenCalled();
  505. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  506. }, converse));
  507. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  508. _addContacts();
  509. var jid, t;
  510. spyOn(converse, 'emit');
  511. spyOn(this.rosterview, 'update').andCallThrough();
  512. for (i=0; i<mock.cur_names.length; i++) {
  513. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  514. this.roster.get(jid).set('chat_status', 'online');
  515. expect(this.rosterview.update).toHaveBeenCalled();
  516. // Check that they are sorted alphabetically
  517. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  518. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  519. }
  520. }, converse));
  521. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  522. _addContacts();
  523. var jid, t;
  524. spyOn(converse, 'emit');
  525. spyOn(this.rosterview, 'update').andCallThrough();
  526. for (i=0; i<mock.cur_names.length; i++) {
  527. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  528. this.roster.get(jid).set('chat_status', 'dnd');
  529. expect(this.rosterview.update).toHaveBeenCalled();
  530. // Check that they are sorted alphabetically
  531. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  532. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  533. }
  534. }, converse));
  535. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  536. _addContacts();
  537. var jid, t;
  538. spyOn(converse, 'emit');
  539. spyOn(this.rosterview, 'update').andCallThrough();
  540. for (i=0; i<mock.cur_names.length; i++) {
  541. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  542. this.roster.get(jid).set('chat_status', 'away');
  543. expect(this.rosterview.update).toHaveBeenCalled();
  544. // Check that they are sorted alphabetically
  545. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  546. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  547. }
  548. }, converse));
  549. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  550. _addContacts();
  551. var jid, t;
  552. spyOn(converse, 'emit');
  553. spyOn(this.rosterview, 'update').andCallThrough();
  554. for (i=0; i<mock.cur_names.length; i++) {
  555. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  556. this.roster.get(jid).set('chat_status', 'xa');
  557. expect(this.rosterview.update).toHaveBeenCalled();
  558. // Check that they are sorted alphabetically
  559. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  560. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  561. }
  562. }, converse));
  563. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  564. _addContacts();
  565. var jid, t;
  566. spyOn(converse, 'emit');
  567. spyOn(this.rosterview, 'update').andCallThrough();
  568. for (i=0; i<mock.cur_names.length; i++) {
  569. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  570. this.roster.get(jid).set('chat_status', 'unavailable');
  571. expect(this.rosterview.update).toHaveBeenCalled();
  572. // Check that they are sorted alphabetically
  573. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  574. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  575. }
  576. }, converse));
  577. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  578. _addContacts();
  579. var i;
  580. for (i=0; i<3; i++) {
  581. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  582. this.roster.get(jid).set('chat_status', 'online');
  583. }
  584. for (i=3; i<6; i++) {
  585. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  586. this.roster.get(jid).set('chat_status', 'dnd');
  587. }
  588. for (i=6; i<9; i++) {
  589. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  590. this.roster.get(jid).set('chat_status', 'away');
  591. }
  592. for (i=9; i<12; i++) {
  593. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  594. this.roster.get(jid).set('chat_status', 'xa');
  595. }
  596. for (i=12; i<15; i++) {
  597. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  598. this.roster.get(jid).set('chat_status', 'unavailable');
  599. }
  600. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  601. for (i=0; i<3; i++) {
  602. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  603. }
  604. for (i=3; i<6; i++) {
  605. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  606. }
  607. for (i=6; i<9; i++) {
  608. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  609. }
  610. for (i=9; i<12; i++) {
  611. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  612. }
  613. for (i=12; i<15; i++) {
  614. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  615. }
  616. for (i=15; i<mock.cur_names.length; i++) {
  617. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  618. }
  619. }, converse));
  620. }, converse));
  621. describe("Requesting Contacts", $.proxy(function () {
  622. beforeEach($.proxy(function () {
  623. runs(function () {
  624. utils.clearBrowserStorage();
  625. converse.rosterview.model.reset();
  626. utils.createContacts('requesting').openControlBox();
  627. });
  628. waits(50);
  629. runs(function () {
  630. utils.openContactsPanel();
  631. });
  632. }, converse));
  633. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  634. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  635. var i, children;
  636. var names = [];
  637. spyOn(converse, 'emit');
  638. spyOn(this.rosterview, 'update').andCallThrough();
  639. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  640. var addName = function (idx, item) {
  641. if (!$(item).hasClass('request-actions')) {
  642. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  643. }
  644. };
  645. for (i=0; i<mock.req_names.length; i++) {
  646. this.roster.create({
  647. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  648. subscription: 'none',
  649. ask: null,
  650. requesting: true,
  651. fullname: mock.req_names[i]
  652. });
  653. expect(this.rosterview.update).toHaveBeenCalled();
  654. // When a requesting contact is added, the controlbox must
  655. // be opened.
  656. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  657. }
  658. // Check that they are sorted alphabetically
  659. children = this.rosterview.get('Contact requests').$el.siblings('dd.requesting-xmpp-contact').children('span');
  660. names = [];
  661. children.each(addName);
  662. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  663. }, converse));
  664. it("do not have a header if there aren't any", $.proxy(function () {
  665. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  666. var name = mock.req_names[0];
  667. spyOn(window, 'confirm').andReturn(true);
  668. this.roster.create({
  669. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  670. subscription: 'none',
  671. ask: null,
  672. requesting: true,
  673. fullname: name
  674. });
  675. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(true);
  676. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  677. .siblings('.request-actions')
  678. .find('.decline-xmpp-request').click();
  679. expect(window.confirm).toHaveBeenCalled();
  680. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(false);
  681. }, converse));
  682. it("can be collapsed under their own header", $.proxy(function () {
  683. checkHeaderToggling.apply(this, [this.rosterview.get('Contact requests').$el]);
  684. }, converse));
  685. it("can have their requests accepted by the user", $.proxy(function () {
  686. // TODO: Testing can be more thorough here, the user is
  687. // actually not accepted/authorized because of
  688. // mock_connection.
  689. var name = mock.req_names.sort()[0];
  690. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  691. spyOn(this.connection.roster, 'authorize');
  692. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  693. .siblings('.request-actions')
  694. .find('.accept-xmpp-request').click();
  695. expect(this.connection.roster.authorize).toHaveBeenCalled();
  696. }, converse));
  697. it("can have their requests denied by the user", $.proxy(function () {
  698. this.rosterview.model.reset();
  699. spyOn(converse, 'emit');
  700. spyOn(this.connection.roster, 'unauthorize');
  701. spyOn(window, 'confirm').andReturn(true);
  702. utils.createContacts('requesting').openControlBox();
  703. var name = mock.req_names.sort()[1];
  704. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  705. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  706. .siblings('.request-actions')
  707. .find('.decline-xmpp-request').click();
  708. expect(window.confirm).toHaveBeenCalled();
  709. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  710. // There should now be one less contact
  711. expect(this.roster.length).toEqual(mock.req_names.length-1);
  712. }, converse));
  713. }, converse));
  714. describe("All Contacts", $.proxy(function () {
  715. beforeEach($.proxy(function () {
  716. utils.clearBrowserStorage();
  717. converse.rosterview.model.reset();
  718. utils.createContacts('all').openControlBox();
  719. utils.openContactsPanel();
  720. }, converse));
  721. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  722. var new_attrs, old_attrs, attrs, old_roster;
  723. var num_contacts = this.roster.length;
  724. new_roster = new this.RosterContacts();
  725. // Roster items are yet to be fetched from browserStorage
  726. expect(new_roster.length).toEqual(0);
  727. new_roster.browserStorage = this.roster.browserStorage;
  728. new_roster.fetch();
  729. expect(new_roster.length).toEqual(num_contacts);
  730. // Check that the roster items retrieved from browserStorage
  731. // have the same attributes values as the original ones.
  732. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  733. for (i=0; i<attrs.length; i++) {
  734. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  735. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  736. // Roster items in storage are not necessarily sorted,
  737. // so we have to sort them here to do a proper
  738. // comparison
  739. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  740. }
  741. }, converse));
  742. }, converse));
  743. }, converse, mock, test_utils));
  744. describe("The 'Add Contact' widget", $.proxy(function (mock, test_utils) {
  745. it("opens up an add form when you click on it", $.proxy(function () {
  746. var panel = this.chatboxviews.get('controlbox').contactspanel;
  747. spyOn(panel, 'toggleContactForm').andCallThrough();
  748. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  749. panel.$el.find('a.toggle-xmpp-contact-form').click();
  750. expect(panel.toggleContactForm).toHaveBeenCalled();
  751. // XXX: Awaiting more tests, close it again for now...
  752. panel.$el.find('a.toggle-xmpp-contact-form').click();
  753. }, converse));
  754. }, converse, mock, test_utils));
  755. describe("The Controlbox Tabs", $.proxy(function () {
  756. beforeEach($.proxy(function () {
  757. runs(function () {
  758. test_utils.closeAllChatBoxes();
  759. });
  760. waits(50);
  761. runs(function () {
  762. test_utils.openControlBox();
  763. });
  764. }, converse));
  765. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  766. var cbview = this.chatboxviews.get('controlbox');
  767. var $panels = cbview.$el.find('.controlbox-panes');
  768. expect($panels.children().length).toBe(2);
  769. expect($panels.children().first().attr('id')).toBe('users');
  770. expect($panels.children().first().is(':visible')).toBe(true);
  771. expect($panels.children().last().attr('id')).toBe('chatrooms');
  772. expect($panels.children().last().is(':visible')).toBe(false);
  773. }, converse));
  774. describe("chatrooms panel", $.proxy(function () {
  775. beforeEach($.proxy(function () {
  776. runs(function () {
  777. test_utils.closeAllChatBoxes();
  778. });
  779. waits(50);
  780. runs(function () {
  781. test_utils.openControlBox();
  782. });
  783. }, converse));
  784. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  785. var cbview = this.chatboxviews.get('controlbox');
  786. var $tabs = cbview.$el.find('#controlbox-tabs');
  787. var $panels = cbview.$el.find('.controlbox-panes');
  788. var $contacts = $panels.children().first();
  789. var $chatrooms = $panels.children().last();
  790. spyOn(cbview, 'switchTab').andCallThrough();
  791. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  792. runs(function () {
  793. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  794. });
  795. waits(250);
  796. runs(function () {
  797. expect($contacts.is(':visible')).toBe(false);
  798. expect($chatrooms.is(':visible')).toBe(true);
  799. expect(cbview.switchTab).toHaveBeenCalled();
  800. });
  801. }, converse));
  802. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  803. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  804. var $input = roomspanel.$el.find('input.new-chatroom-name');
  805. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  806. var $server = roomspanel.$el.find('input.new-chatroom-server');
  807. expect($input.length).toBe(1);
  808. expect($server.length).toBe(1);
  809. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  810. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  811. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  812. runs(function () {
  813. $input.val('Lounge');
  814. $nick.val('dummy');
  815. $server.val('muc.localhost');
  816. });
  817. waits('250');
  818. runs(function () {
  819. roomspanel.$el.find('form').submit();
  820. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  821. });
  822. waits('250');
  823. runs($.proxy(function () {
  824. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  825. }, converse));
  826. }, converse));
  827. }, converse));
  828. }, converse, mock, test_utils));
  829. }));