controlbox.js 47 KB

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