controlbox.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. /* FIXME: Monkepatch
  367. * After refactoring the mock connection to use a
  368. * Strophe.Connection object, these tests fail because "remove"
  369. * function in strophe.roster (line 292) gets called and it
  370. * then tries to actually remove the user which is not in the roster...
  371. */
  372. var old_remove = this.connection.roster.remove;
  373. this.connection.roster.remove = function (jid, callback) { callback(); };
  374. _addContacts();
  375. var name = mock.pend_names[0];
  376. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  377. spyOn(window, 'confirm').andReturn(true);
  378. spyOn(converse, 'emit');
  379. spyOn(this.connection.roster, 'remove').andCallThrough();
  380. spyOn(this.connection.roster, 'unauthorize');
  381. spyOn(this.rosterview.model, 'remove').andCallThrough();
  382. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  383. .siblings('.remove-xmpp-contact').click();
  384. expect(window.confirm).toHaveBeenCalled();
  385. expect(this.connection.roster.remove).toHaveBeenCalled();
  386. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  387. expect(this.rosterview.model.remove).toHaveBeenCalled();
  388. expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
  389. /* XXX Restore Monkeypatch */
  390. this.connection.roster.remove = old_remove;
  391. }, converse));
  392. it("do not have a header if there aren't any", $.proxy(function () {
  393. var name = mock.pend_names[0];
  394. _clearContacts();
  395. spyOn(window, 'confirm').andReturn(true);
  396. this.roster.create({
  397. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  398. subscription: 'none',
  399. ask: 'subscribe',
  400. fullname: name
  401. });
  402. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(true);
  403. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  404. .siblings('.remove-xmpp-contact').click();
  405. expect(window.confirm).toHaveBeenCalled();
  406. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(false);
  407. }, converse));
  408. it("will lose their own header once the last one has been removed", $.proxy(function () {
  409. _addContacts();
  410. var name;
  411. spyOn(window, 'confirm').andReturn(true);
  412. for (i=0; i<mock.pend_names.length; i++) {
  413. name = mock.pend_names[i];
  414. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  415. .siblings('.remove-xmpp-contact').click();
  416. }
  417. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  418. }, converse));
  419. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  420. _clearContacts();
  421. var i, t;
  422. spyOn(converse, 'emit');
  423. spyOn(this.rosterview, 'update').andCallThrough();
  424. for (i=0; i<mock.pend_names.length; i++) {
  425. this.roster.create({
  426. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  427. subscription: 'none',
  428. ask: 'subscribe',
  429. fullname: mock.pend_names[i]
  430. });
  431. expect(this.rosterview.update).toHaveBeenCalled();
  432. }
  433. // Check that they are sorted alphabetically
  434. t = this.rosterview.get('Pending contacts').$el.siblings('dd.pending-xmpp-contact').find('span').text();
  435. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  436. }, converse));
  437. }, converse));
  438. describe("Existing Contacts", $.proxy(function () {
  439. function _clearContacts () {
  440. utils.clearBrowserStorage();
  441. converse.rosterview.model.reset();
  442. }
  443. var _addContacts = function () {
  444. _clearContacts();
  445. utils.createContacts('current').openControlBox().openContactsPanel();
  446. };
  447. it("can be collapsed under their own header", $.proxy(function () {
  448. _addContacts();
  449. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]);
  450. }, converse));
  451. it("will be hidden when appearing under a collapsed group", $.proxy(function () {
  452. _addContacts();
  453. this.rosterview.$el.find('dt.roster-group').find('a.group-toggle').click();
  454. var name = "Max Mustermann";
  455. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  456. converse.roster.create({
  457. ask: null,
  458. fullname: name,
  459. jid: jid,
  460. requesting: false,
  461. subscription: 'both'
  462. });
  463. var view = this.rosterview.get('My contacts').get(jid);
  464. expect(view.$el.is(':visible')).toBe(false);
  465. }, converse));
  466. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  467. _clearContacts();
  468. var i, t;
  469. spyOn(converse, 'emit');
  470. spyOn(this.rosterview, 'update').andCallThrough();
  471. for (i=0; i<mock.cur_names.length; i++) {
  472. this.roster.create({
  473. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  474. subscription: 'both',
  475. ask: null,
  476. fullname: mock.cur_names[i]
  477. });
  478. expect(this.rosterview.update).toHaveBeenCalled();
  479. }
  480. // Check that they are sorted alphabetically
  481. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  482. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  483. }, converse));
  484. it("can be removed by the user", $.proxy(function () {
  485. _addContacts();
  486. var name = mock.cur_names[0];
  487. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  488. spyOn(window, 'confirm').andReturn(true);
  489. spyOn(converse, 'emit');
  490. spyOn(this.connection.roster, 'remove').andCallThrough();
  491. spyOn(this.connection.roster, 'unauthorize');
  492. spyOn(this.rosterview.model, 'remove').andCallThrough();
  493. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  494. .siblings('.remove-xmpp-contact').click();
  495. expect(window.confirm).toHaveBeenCalled();
  496. expect(this.connection.roster.remove).toHaveBeenCalled();
  497. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  498. expect(this.rosterview.model.remove).toHaveBeenCalled();
  499. expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
  500. }, converse));
  501. it("do not have a header if there aren't any", $.proxy(function () {
  502. var name = mock.cur_names[0];
  503. _clearContacts();
  504. spyOn(window, 'confirm').andReturn(true);
  505. this.roster.create({
  506. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  507. subscription: 'both',
  508. ask: null,
  509. fullname: name
  510. });
  511. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
  512. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  513. .siblings('.remove-xmpp-contact').click();
  514. expect(window.confirm).toHaveBeenCalled();
  515. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  516. }, converse));
  517. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  518. _addContacts();
  519. var jid, t;
  520. spyOn(converse, 'emit');
  521. spyOn(this.rosterview, 'update').andCallThrough();
  522. for (i=0; i<mock.cur_names.length; i++) {
  523. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  524. this.roster.get(jid).set('chat_status', 'online');
  525. expect(this.rosterview.update).toHaveBeenCalled();
  526. // Check that they are sorted alphabetically
  527. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  528. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  529. }
  530. }, converse));
  531. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  532. _addContacts();
  533. var jid, t;
  534. spyOn(converse, 'emit');
  535. spyOn(this.rosterview, 'update').andCallThrough();
  536. for (i=0; i<mock.cur_names.length; i++) {
  537. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  538. this.roster.get(jid).set('chat_status', 'dnd');
  539. expect(this.rosterview.update).toHaveBeenCalled();
  540. // Check that they are sorted alphabetically
  541. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  542. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  543. }
  544. }, converse));
  545. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  546. _addContacts();
  547. var jid, t;
  548. spyOn(converse, 'emit');
  549. spyOn(this.rosterview, 'update').andCallThrough();
  550. for (i=0; i<mock.cur_names.length; i++) {
  551. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  552. this.roster.get(jid).set('chat_status', 'away');
  553. expect(this.rosterview.update).toHaveBeenCalled();
  554. // Check that they are sorted alphabetically
  555. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  556. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  557. }
  558. }, converse));
  559. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  560. _addContacts();
  561. var jid, t;
  562. spyOn(converse, 'emit');
  563. spyOn(this.rosterview, 'update').andCallThrough();
  564. for (i=0; i<mock.cur_names.length; i++) {
  565. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  566. this.roster.get(jid).set('chat_status', 'xa');
  567. expect(this.rosterview.update).toHaveBeenCalled();
  568. // Check that they are sorted alphabetically
  569. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  570. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  571. }
  572. }, converse));
  573. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  574. _addContacts();
  575. var jid, t;
  576. spyOn(converse, 'emit');
  577. spyOn(this.rosterview, 'update').andCallThrough();
  578. for (i=0; i<mock.cur_names.length; i++) {
  579. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  580. this.roster.get(jid).set('chat_status', 'unavailable');
  581. expect(this.rosterview.update).toHaveBeenCalled();
  582. // Check that they are sorted alphabetically
  583. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  584. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  585. }
  586. }, converse));
  587. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  588. _addContacts();
  589. var i;
  590. for (i=0; i<3; i++) {
  591. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  592. this.roster.get(jid).set('chat_status', 'online');
  593. }
  594. for (i=3; i<6; i++) {
  595. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  596. this.roster.get(jid).set('chat_status', 'dnd');
  597. }
  598. for (i=6; i<9; i++) {
  599. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  600. this.roster.get(jid).set('chat_status', 'away');
  601. }
  602. for (i=9; i<12; i++) {
  603. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  604. this.roster.get(jid).set('chat_status', 'xa');
  605. }
  606. for (i=12; i<15; i++) {
  607. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  608. this.roster.get(jid).set('chat_status', 'unavailable');
  609. }
  610. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  611. for (i=0; i<3; i++) {
  612. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  613. }
  614. for (i=3; i<6; i++) {
  615. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  616. }
  617. for (i=6; i<9; i++) {
  618. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  619. }
  620. for (i=9; i<12; i++) {
  621. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  622. }
  623. for (i=12; i<15; i++) {
  624. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  625. }
  626. for (i=15; i<mock.cur_names.length; i++) {
  627. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  628. }
  629. }, converse));
  630. }, converse));
  631. describe("Requesting Contacts", $.proxy(function () {
  632. beforeEach($.proxy(function () {
  633. runs(function () {
  634. utils.clearBrowserStorage();
  635. converse.rosterview.model.reset();
  636. utils.createContacts('requesting').openControlBox();
  637. });
  638. waits(50);
  639. runs(function () {
  640. utils.openContactsPanel();
  641. });
  642. }, converse));
  643. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  644. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  645. var i, children;
  646. var names = [];
  647. spyOn(converse, 'emit');
  648. spyOn(this.rosterview, 'update').andCallThrough();
  649. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  650. var addName = function (idx, item) {
  651. if (!$(item).hasClass('request-actions')) {
  652. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  653. }
  654. };
  655. for (i=0; i<mock.req_names.length; i++) {
  656. this.roster.create({
  657. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  658. subscription: 'none',
  659. ask: null,
  660. requesting: true,
  661. fullname: mock.req_names[i]
  662. });
  663. expect(this.rosterview.update).toHaveBeenCalled();
  664. // When a requesting contact is added, the controlbox must
  665. // be opened.
  666. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  667. }
  668. // Check that they are sorted alphabetically
  669. children = this.rosterview.get('Contact requests').$el.siblings('dd.requesting-xmpp-contact').children('span');
  670. names = [];
  671. children.each(addName);
  672. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  673. }, converse));
  674. it("do not have a header if there aren't any", $.proxy(function () {
  675. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  676. var name = mock.req_names[0];
  677. spyOn(window, 'confirm').andReturn(true);
  678. this.roster.create({
  679. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  680. subscription: 'none',
  681. ask: null,
  682. requesting: true,
  683. fullname: name
  684. });
  685. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(true);
  686. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  687. .siblings('.request-actions')
  688. .find('.decline-xmpp-request').click();
  689. expect(window.confirm).toHaveBeenCalled();
  690. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(false);
  691. }, converse));
  692. it("can be collapsed under their own header", $.proxy(function () {
  693. checkHeaderToggling.apply(this, [this.rosterview.get('Contact requests').$el]);
  694. }, converse));
  695. it("can have their requests accepted by the user", $.proxy(function () {
  696. // TODO: Testing can be more thorough here, the user is
  697. // actually not accepted/authorized because of
  698. // mock_connection.
  699. var name = mock.req_names.sort()[0];
  700. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  701. spyOn(this.connection.roster, 'authorize');
  702. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  703. .siblings('.request-actions')
  704. .find('.accept-xmpp-request').click();
  705. expect(this.connection.roster.authorize).toHaveBeenCalled();
  706. }, converse));
  707. it("can have their requests denied by the user", $.proxy(function () {
  708. this.rosterview.model.reset();
  709. spyOn(converse, 'emit');
  710. spyOn(this.connection.roster, 'unauthorize');
  711. spyOn(window, 'confirm').andReturn(true);
  712. utils.createContacts('requesting').openControlBox();
  713. var name = mock.req_names.sort()[1];
  714. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  715. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  716. .siblings('.request-actions')
  717. .find('.decline-xmpp-request').click();
  718. expect(window.confirm).toHaveBeenCalled();
  719. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  720. // There should now be one less contact
  721. expect(this.roster.length).toEqual(mock.req_names.length-1);
  722. }, converse));
  723. it("are persisted even if other contacts' change their presence ", $.proxy(function() {
  724. /* This is a regression test.
  725. * https://github.com/jcbrand/converse.js/issues/262
  726. */
  727. this.rosterview.model.reset();
  728. spyOn(this.roster, 'clearCache').andCallThrough();
  729. expect(this.roster.pluck('jid').length).toBe(0);
  730. var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'});
  731. this.connection._dataRecv(test_utils.createRequest(stanza));
  732. expect(this.roster.pluck('jid').length).toBe(1);
  733. expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  734. // Taken from the spec
  735. // http://xmpp.org/rfcs/rfc3921.html#rfc.section.7.3
  736. stanza = $iq({
  737. to: this.connection.jid,
  738. type: 'result',
  739. id: 'roster_1'
  740. }).c('query', {
  741. xmlns: 'jabber:iq:roster',
  742. }).c('item', {
  743. jid: 'romeo@example.net',
  744. name: 'Romeo',
  745. subscription:'both'
  746. }).c('group').t('Friends').up().up()
  747. .c('item', {
  748. jid: 'mercutio@example.org',
  749. name: 'Mercutio',
  750. subscription:'from'
  751. }).c('group').t('Friends').up().up()
  752. .c('item', {
  753. jid: 'benvolio@example.org',
  754. name: 'Benvolio',
  755. subscription:'both'
  756. }).c('group').t('Friends');
  757. this.connection.roster._onReceiveRosterSuccess(null, stanza.tree());
  758. expect(this.roster.clearCache).toHaveBeenCalled();
  759. expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  760. }, converse));
  761. }, converse));
  762. describe("All Contacts", $.proxy(function () {
  763. beforeEach($.proxy(function () {
  764. utils.clearBrowserStorage();
  765. converse.rosterview.model.reset();
  766. utils.createContacts('all').openControlBox();
  767. utils.openContactsPanel();
  768. }, converse));
  769. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  770. var new_attrs, old_attrs, attrs;
  771. var num_contacts = this.roster.length;
  772. new_roster = new this.RosterContacts();
  773. // Roster items are yet to be fetched from browserStorage
  774. expect(new_roster.length).toEqual(0);
  775. new_roster.browserStorage = this.roster.browserStorage;
  776. new_roster.fetch();
  777. expect(new_roster.length).toEqual(num_contacts);
  778. // Check that the roster items retrieved from browserStorage
  779. // have the same attributes values as the original ones.
  780. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  781. for (i=0; i<attrs.length; i++) {
  782. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  783. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  784. // Roster items in storage are not necessarily sorted,
  785. // so we have to sort them here to do a proper
  786. // comparison
  787. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  788. }
  789. }, converse));
  790. }, converse));
  791. }, converse, mock, test_utils));
  792. describe("The 'Add Contact' widget", $.proxy(function (mock, test_utils) {
  793. it("opens up an add form when you click on it", $.proxy(function () {
  794. var panel = this.chatboxviews.get('controlbox').contactspanel;
  795. spyOn(panel, 'toggleContactForm').andCallThrough();
  796. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  797. panel.$el.find('a.toggle-xmpp-contact-form').click();
  798. expect(panel.toggleContactForm).toHaveBeenCalled();
  799. // XXX: Awaiting more tests, close it again for now...
  800. panel.$el.find('a.toggle-xmpp-contact-form').click();
  801. }, converse));
  802. }, converse, mock, test_utils));
  803. describe("The Controlbox Tabs", $.proxy(function () {
  804. beforeEach($.proxy(function () {
  805. runs(function () {
  806. test_utils.closeAllChatBoxes();
  807. });
  808. waits(50);
  809. runs(function () {
  810. test_utils.openControlBox();
  811. });
  812. }, converse));
  813. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  814. var cbview = this.chatboxviews.get('controlbox');
  815. var $panels = cbview.$el.find('.controlbox-panes');
  816. expect($panels.children().length).toBe(2);
  817. expect($panels.children().first().attr('id')).toBe('users');
  818. expect($panels.children().first().is(':visible')).toBe(true);
  819. expect($panels.children().last().attr('id')).toBe('chatrooms');
  820. expect($panels.children().last().is(':visible')).toBe(false);
  821. }, converse));
  822. describe("chatrooms panel", $.proxy(function () {
  823. beforeEach($.proxy(function () {
  824. runs(function () {
  825. test_utils.closeAllChatBoxes();
  826. });
  827. waits(50);
  828. runs(function () {
  829. test_utils.openControlBox();
  830. });
  831. }, converse));
  832. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  833. var cbview = this.chatboxviews.get('controlbox');
  834. var $tabs = cbview.$el.find('#controlbox-tabs');
  835. var $panels = cbview.$el.find('.controlbox-panes');
  836. var $contacts = $panels.children().first();
  837. var $chatrooms = $panels.children().last();
  838. spyOn(cbview, 'switchTab').andCallThrough();
  839. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  840. runs(function () {
  841. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  842. });
  843. waits(250);
  844. runs(function () {
  845. expect($contacts.is(':visible')).toBe(false);
  846. expect($chatrooms.is(':visible')).toBe(true);
  847. expect(cbview.switchTab).toHaveBeenCalled();
  848. });
  849. }, converse));
  850. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  851. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  852. var $input = roomspanel.$el.find('input.new-chatroom-name');
  853. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  854. var $server = roomspanel.$el.find('input.new-chatroom-server');
  855. expect($input.length).toBe(1);
  856. expect($server.length).toBe(1);
  857. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  858. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  859. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  860. runs(function () {
  861. $input.val('Lounge');
  862. $nick.val('dummy');
  863. $server.val('muc.localhost');
  864. });
  865. waits('250');
  866. runs(function () {
  867. roomspanel.$el.find('form').submit();
  868. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  869. });
  870. waits('250');
  871. runs($.proxy(function () {
  872. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  873. }, converse));
  874. }, converse));
  875. }, converse));
  876. }, converse, mock, test_utils));
  877. }));