controlbox.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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.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. var $filter;
  147. var $roster;
  148. runs(function () {
  149. _clearContacts();
  150. converse.roster_groups = true;
  151. utils.createGroupedContacts();
  152. $filter = converse.rosterview.$('.roster-filter');
  153. $roster = converse.rosterview.$roster;
  154. });
  155. waits(350); // Needed, due to debounce
  156. runs(function () {
  157. expect($roster.find('dd:visible').length).toBe(15);
  158. expect($roster.find('dt:visible').length).toBe(5);
  159. $filter.val("candice");
  160. expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
  161. expect($roster.find('dt:visible').length).toBe(5); // ditto
  162. $filter.trigger('keydown');
  163. });
  164. waits(350); // Needed, due to debounce
  165. runs (function () {
  166. expect($roster.find('dd:visible').length).toBe(1);
  167. expect($roster.find('dd:visible').eq(0).text().trim()).toBe('Candice van der Knijff');
  168. expect($roster.find('dt:visible').length).toBe(1);
  169. expect($roster.find('dt:visible').eq(0).text()).toBe('colleagues');
  170. $filter.val("an");
  171. $filter.trigger('keydown');
  172. });
  173. waits(350); // Needed, due to debounce
  174. runs (function () {
  175. expect($roster.find('dd:visible').length).toBe(5);
  176. expect($roster.find('dt:visible').length).toBe(4);
  177. $filter.val("xxx");
  178. $filter.trigger('keydown');
  179. });
  180. waits(350); // Needed, due to debounce
  181. runs (function () {
  182. expect($roster.find('dd:visible').length).toBe(0);
  183. expect($roster.find('dt:visible').length).toBe(0);
  184. $filter.val(""); // Check that contacts are shown again, when the filter string is cleared.
  185. $filter.trigger('keydown');
  186. });
  187. waits(350); // Needed, due to debounce
  188. runs(function () {
  189. expect($roster.find('dd:visible').length).toBe(15);
  190. expect($roster.find('dt:visible').length).toBe(5);
  191. });
  192. converse.roster_groups = false;
  193. });
  194. it("can be used to filter the groups shown", function () {
  195. var $filter;
  196. var $roster;
  197. var $type;
  198. runs(function () {
  199. converse.roster_groups = true;
  200. _clearContacts();
  201. utils.createGroupedContacts();
  202. $filter = converse.rosterview.$('.roster-filter');
  203. $roster = converse.rosterview.$roster;
  204. $type = converse.rosterview.$('.filter-type');
  205. $type.val('groups');
  206. });
  207. waits(350); // Needed, due to debounce
  208. runs(function () {
  209. expect($roster.find('dd:visible').length).toBe(15);
  210. expect($roster.find('dt:visible').length).toBe(5);
  211. $filter.val("colleagues");
  212. expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
  213. expect($roster.find('dt:visible').length).toBe(5); // ditto
  214. $filter.trigger('keydown');
  215. });
  216. waits(350); // Needed, due to debounce
  217. runs (function () {
  218. expect($roster.find('dt:visible').length).toBe(1);
  219. expect($roster.find('dt:visible').eq(0).text()).toBe('colleagues');
  220. // Check that all contacts under the group are shown
  221. expect($roster.find('dt:visible').nextUntil('dt', 'dd:hidden').length).toBe(0);
  222. $filter.val("xxx");
  223. $filter.trigger('keydown');
  224. });
  225. waits(350); // Needed, due to debounce
  226. runs (function () {
  227. expect($roster.find('dt:visible').length).toBe(0);
  228. $filter.val(""); // Check that groups are shown again, when the filter string is cleared.
  229. $filter.trigger('keydown');
  230. });
  231. waits(350); // Needed, due to debounce
  232. runs(function () {
  233. expect($roster.find('dd:visible').length).toBe(15);
  234. expect($roster.find('dt:visible').length).toBe(5);
  235. });
  236. converse.roster_groups = false;
  237. });
  238. it("has a button with which its contents can be cleared", function () {
  239. converse.roster_groups = true;
  240. _clearContacts();
  241. utils.createGroupedContacts();
  242. var $filter = converse.rosterview.$('.roster-filter');
  243. var $roster = converse.rosterview.$roster;
  244. runs (function () {
  245. $filter.val("xxx");
  246. $filter.trigger('keydown');
  247. expect($filter.hasClass("x")).toBeFalsy();
  248. });
  249. waits(350); // Needed, due to debounce
  250. runs (function () {
  251. expect($filter.hasClass("x")).toBeTruthy();
  252. $filter.addClass("onX").click();
  253. expect($filter.val()).toBe("");
  254. });
  255. converse.roster_groups = false;
  256. });
  257. }, converse));
  258. describe("A Roster Group", $.proxy(function () {
  259. beforeEach(function () {
  260. converse.roster_groups = true;
  261. });
  262. afterEach(function () {
  263. converse.roster_groups = false;
  264. });
  265. it("can be used to organize existing contacts", $.proxy(function () {
  266. runs($.proxy(function () {
  267. _clearContacts();
  268. spyOn(converse, 'emit');
  269. spyOn(this.rosterview, 'update').andCallThrough();
  270. converse.rosterview.render();
  271. utils.createContacts('pending');
  272. utils.createContacts('requesting');
  273. utils.createGroupedContacts();
  274. }, this));
  275. waits(50); // Needed, due to debounce
  276. runs($.proxy(function () {
  277. // Check that the groups appear alphabetically and that
  278. // requesting and pending contacts are last.
  279. var group_titles = $.map(this.rosterview.$el.find('dt'), function (o) { return $(o).text().trim(); });
  280. expect(group_titles).toEqual([
  281. "colleagues",
  282. "Family",
  283. "friends & acquaintences",
  284. "ænemies",
  285. "Ungrouped",
  286. "Contact requests",
  287. "Pending contacts"
  288. ]);
  289. // Check that usernames appear alphabetically per group
  290. _.each(_.keys(mock.groups), $.proxy(function (name) {
  291. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  292. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  293. expect(names).toEqual(_.clone(names).sort());
  294. }, converse));
  295. }, this));
  296. }, converse));
  297. it("can share contacts with other roster groups", $.proxy(function () {
  298. var groups = ['colleagues', 'friends'];
  299. runs($.proxy(function () {
  300. _clearContacts();
  301. var i=0, j=0;
  302. spyOn(converse, 'emit');
  303. spyOn(this.rosterview, 'update').andCallThrough();
  304. converse.rosterview.render();
  305. for (i=0; i<mock.cur_names.length; i++) {
  306. this.roster.create({
  307. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  308. subscription: 'both',
  309. ask: null,
  310. groups: groups,
  311. fullname: mock.cur_names[i]
  312. });
  313. }
  314. }, this));
  315. waits(50); // Needed, due to debounce
  316. runs($.proxy(function () {
  317. // Check that usernames appear alphabetically per group
  318. _.each(groups, $.proxy(function (name) {
  319. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  320. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  321. expect(names).toEqual(_.clone(names).sort());
  322. expect(names.length).toEqual(mock.cur_names.length);
  323. }, this));
  324. }, this));
  325. }, converse));
  326. it("remembers whether it is closed or opened", $.proxy(function () {
  327. var i=0, j=0;
  328. var groups = {
  329. 'colleagues': 3,
  330. 'friends & acquaintences': 3,
  331. 'Ungrouped': 2
  332. };
  333. _.each(_.keys(groups), $.proxy(function (name) {
  334. j = i;
  335. for (i=j; i<j+groups[name]; i++) {
  336. this.roster.create({
  337. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  338. subscription: 'both',
  339. ask: null,
  340. groups: name === 'ungrouped'? [] : [name],
  341. fullname: mock.cur_names[i]
  342. });
  343. }
  344. }, converse));
  345. var view = this.rosterview.get('colleagues');
  346. var $toggle = view.$el.find('a.group-toggle');
  347. expect(view.model.get('state')).toBe('opened');
  348. $toggle.click();
  349. expect(view.model.get('state')).toBe('closed');
  350. $toggle.click();
  351. expect(view.model.get('state')).toBe('opened');
  352. }, converse));
  353. }, converse));
  354. describe("Pending Contacts", $.proxy(function () {
  355. function _clearContacts () {
  356. utils.clearBrowserStorage();
  357. converse.rosterview.model.reset();
  358. }
  359. function _addContacts () {
  360. _clearContacts();
  361. // Must be initialized, so that render is called and documentFragment set up.
  362. utils.createContacts('pending').openControlBox().openContactsPanel();
  363. }
  364. it("can be collapsed under their own header", $.proxy(function () {
  365. runs(function () {
  366. _addContacts();
  367. });
  368. waits(50);
  369. runs($.proxy(function () {
  370. checkHeaderToggling.apply(this, [this.rosterview.get('Pending contacts').$el]);
  371. }, this));
  372. }, converse));
  373. it("can be added to the roster", $.proxy(function () {
  374. _clearContacts();
  375. spyOn(converse, 'emit');
  376. spyOn(this.rosterview, 'update').andCallThrough();
  377. runs($.proxy(function () {
  378. this.roster.create({
  379. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  380. subscription: 'none',
  381. ask: 'subscribe',
  382. fullname: mock.pend_names[0]
  383. });
  384. }, converse));
  385. waits(300);
  386. runs($.proxy(function () {
  387. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  388. expect(this.rosterview.update).toHaveBeenCalled();
  389. }, converse));
  390. }, converse));
  391. it("can be removed by the user", $.proxy(function () {
  392. runs($.proxy(function () {
  393. _addContacts();
  394. }, this));
  395. waits(50);
  396. runs($.proxy(function () {
  397. /* FIXME: Monkepatch
  398. * After refactoring the mock connection to use a
  399. * Strophe.Connection object, these tests fail because "remove"
  400. * function in strophe.roster (line 292) gets called and it
  401. * then tries to actually remove the user which is not in the roster...
  402. */
  403. var old_remove = this.connection.roster.remove;
  404. this.connection.roster.remove = function (jid, callback) { callback(); };
  405. var name = mock.pend_names[0];
  406. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  407. spyOn(window, 'confirm').andReturn(true);
  408. spyOn(converse, 'emit');
  409. spyOn(this.connection.roster, 'remove').andCallThrough();
  410. spyOn(this.connection.roster, 'unauthorize');
  411. spyOn(this.rosterview.model, 'remove').andCallThrough();
  412. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  413. .siblings('.remove-xmpp-contact').click();
  414. expect(window.confirm).toHaveBeenCalled();
  415. expect(this.connection.roster.remove).toHaveBeenCalled();
  416. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  417. expect(this.rosterview.model.remove).toHaveBeenCalled();
  418. expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
  419. /* XXX Restore Monkeypatch */
  420. this.connection.roster.remove = old_remove;
  421. }, this));
  422. }, converse));
  423. it("do not have a header if there aren't any", $.proxy(function () {
  424. var name = mock.pend_names[0];
  425. runs($.proxy(function () {
  426. _clearContacts();
  427. }, this));
  428. waits(50);
  429. runs($.proxy(function () {
  430. spyOn(window, 'confirm').andReturn(true);
  431. this.roster.create({
  432. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  433. subscription: 'none',
  434. ask: 'subscribe',
  435. fullname: name
  436. });
  437. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(true);
  438. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  439. .siblings('.remove-xmpp-contact').click();
  440. expect(window.confirm).toHaveBeenCalled();
  441. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(false);
  442. }, this));
  443. }, converse));
  444. it("will lose their own header once the last one has been removed", $.proxy(function () {
  445. _addContacts();
  446. var name;
  447. spyOn(window, 'confirm').andReturn(true);
  448. for (i=0; i<mock.pend_names.length; i++) {
  449. name = mock.pend_names[i];
  450. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  451. .siblings('.remove-xmpp-contact').click();
  452. }
  453. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  454. }, converse));
  455. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  456. _clearContacts();
  457. var i, t;
  458. spyOn(converse, 'emit');
  459. spyOn(this.rosterview, 'update').andCallThrough();
  460. for (i=0; i<mock.pend_names.length; i++) {
  461. this.roster.create({
  462. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  463. subscription: 'none',
  464. ask: 'subscribe',
  465. fullname: mock.pend_names[i]
  466. });
  467. expect(this.rosterview.update).toHaveBeenCalled();
  468. }
  469. // Check that they are sorted alphabetically
  470. t = this.rosterview.get('Pending contacts').$el.siblings('dd.pending-xmpp-contact').find('span').text();
  471. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  472. }, converse));
  473. }, converse));
  474. describe("Existing Contacts", $.proxy(function () {
  475. function _clearContacts () {
  476. utils.clearBrowserStorage();
  477. converse.rosterview.model.reset();
  478. }
  479. var _addContacts = function () {
  480. _clearContacts();
  481. utils.createContacts('current').openControlBox().openContactsPanel();
  482. };
  483. it("can be collapsed under their own header", $.proxy(function () {
  484. runs(function () {
  485. _addContacts();
  486. });
  487. waits(50);
  488. runs($.proxy(function () {
  489. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]);
  490. }, this));
  491. }, converse));
  492. it("will be hidden when appearing under a collapsed group", $.proxy(function () {
  493. _addContacts();
  494. this.rosterview.$el.find('dt.roster-group').find('a.group-toggle').click();
  495. var name = "Max Mustermann";
  496. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  497. converse.roster.create({
  498. ask: null,
  499. fullname: name,
  500. jid: jid,
  501. requesting: false,
  502. subscription: 'both'
  503. });
  504. var view = this.rosterview.get('My contacts').get(jid);
  505. expect(view.$el.is(':visible')).toBe(false);
  506. }, converse));
  507. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  508. runs(function () {
  509. _clearContacts();
  510. });
  511. waits(50);
  512. runs($.proxy(function () {
  513. var i, t;
  514. spyOn(converse, 'emit');
  515. spyOn(this.rosterview, 'update').andCallThrough();
  516. for (i=0; i<mock.cur_names.length; i++) {
  517. this.roster.create({
  518. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  519. subscription: 'both',
  520. ask: null,
  521. fullname: mock.cur_names[i]
  522. });
  523. expect(this.rosterview.update).toHaveBeenCalled();
  524. }
  525. // Check that they are sorted alphabetically
  526. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  527. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  528. }, this));
  529. }, converse));
  530. it("can be removed by the user", $.proxy(function () {
  531. runs(function () {
  532. _addContacts();
  533. });
  534. waits(50);
  535. runs($.proxy(function () {
  536. /* FIXME: Monkepatch
  537. * After refactoring the mock connection to use a
  538. * Strophe.Connection object, these tests fail because "remove"
  539. * function in strophe.roster (line 292) gets called and it
  540. * then tries to actually remove the user which is not in the roster...
  541. */
  542. var old_remove = this.connection.roster.remove;
  543. this.connection.roster.remove = function (jid, callback) { callback(); };
  544. var name = mock.cur_names[0];
  545. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  546. spyOn(window, 'confirm').andReturn(true);
  547. spyOn(converse, 'emit');
  548. spyOn(this.connection.roster, 'remove').andCallThrough();
  549. spyOn(this.connection.roster, 'unauthorize');
  550. spyOn(this.rosterview.model, 'remove').andCallThrough();
  551. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  552. .siblings('.remove-xmpp-contact').click();
  553. expect(window.confirm).toHaveBeenCalled();
  554. expect(this.connection.roster.remove).toHaveBeenCalled();
  555. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  556. expect(this.rosterview.model.remove).toHaveBeenCalled();
  557. expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
  558. /* XXX Restore Monkeypatch */
  559. this.connection.roster.remove = old_remove;
  560. }, this));
  561. }, converse));
  562. it("do not have a header if there aren't any", $.proxy(function () {
  563. var name = mock.cur_names[0];
  564. runs(function () {
  565. _clearContacts();
  566. });
  567. waits(50);
  568. runs($.proxy(function () {
  569. spyOn(window, 'confirm').andReturn(true);
  570. this.roster.create({
  571. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  572. subscription: 'both',
  573. ask: null,
  574. fullname: name
  575. });
  576. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
  577. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  578. .siblings('.remove-xmpp-contact').click();
  579. expect(window.confirm).toHaveBeenCalled();
  580. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  581. }, this));
  582. }, converse));
  583. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  584. runs(function () {
  585. _addContacts();
  586. });
  587. waits(50);
  588. runs($.proxy(function () {
  589. var jid, t;
  590. spyOn(converse, 'emit');
  591. spyOn(this.rosterview, 'update').andCallThrough();
  592. for (i=0; i<mock.cur_names.length; i++) {
  593. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  594. this.roster.get(jid).set('chat_status', 'online');
  595. expect(this.rosterview.update).toHaveBeenCalled();
  596. // Check that they are sorted alphabetically
  597. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  598. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  599. }
  600. }, this));
  601. }, converse));
  602. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  603. runs(function () {
  604. _addContacts();
  605. });
  606. waits(50);
  607. runs($.proxy(function () {
  608. var jid, t;
  609. spyOn(converse, 'emit');
  610. spyOn(this.rosterview, 'update').andCallThrough();
  611. for (i=0; i<mock.cur_names.length; i++) {
  612. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  613. this.roster.get(jid).set('chat_status', 'dnd');
  614. expect(this.rosterview.update).toHaveBeenCalled();
  615. // Check that they are sorted alphabetically
  616. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  617. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  618. }
  619. }, this));
  620. }, converse));
  621. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  622. runs(function () {
  623. _addContacts();
  624. });
  625. waits(50);
  626. runs($.proxy(function () {
  627. var jid, t;
  628. spyOn(converse, 'emit');
  629. spyOn(this.rosterview, 'update').andCallThrough();
  630. for (i=0; i<mock.cur_names.length; i++) {
  631. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  632. this.roster.get(jid).set('chat_status', 'away');
  633. expect(this.rosterview.update).toHaveBeenCalled();
  634. // Check that they are sorted alphabetically
  635. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  636. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  637. }
  638. }, this));
  639. }, converse));
  640. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  641. runs(function () {
  642. _addContacts();
  643. });
  644. waits(50);
  645. runs($.proxy(function () {
  646. var jid, t;
  647. spyOn(converse, 'emit');
  648. spyOn(this.rosterview, 'update').andCallThrough();
  649. for (i=0; i<mock.cur_names.length; i++) {
  650. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  651. this.roster.get(jid).set('chat_status', 'xa');
  652. expect(this.rosterview.update).toHaveBeenCalled();
  653. // Check that they are sorted alphabetically
  654. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  655. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  656. }
  657. }, this));
  658. }, converse));
  659. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  660. runs(function () {
  661. _addContacts();
  662. });
  663. waits(50);
  664. runs($.proxy(function () {
  665. var jid, t;
  666. spyOn(converse, 'emit');
  667. spyOn(this.rosterview, 'update').andCallThrough();
  668. for (i=0; i<mock.cur_names.length; i++) {
  669. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  670. this.roster.get(jid).set('chat_status', 'unavailable');
  671. expect(this.rosterview.update).toHaveBeenCalled();
  672. // Check that they are sorted alphabetically
  673. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  674. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  675. }
  676. }, this));
  677. }, converse));
  678. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  679. runs(function () {
  680. _addContacts();
  681. });
  682. waits(50);
  683. runs($.proxy(function () {
  684. var i;
  685. for (i=0; i<3; i++) {
  686. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  687. this.roster.get(jid).set('chat_status', 'online');
  688. }
  689. for (i=3; i<6; i++) {
  690. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  691. this.roster.get(jid).set('chat_status', 'dnd');
  692. }
  693. for (i=6; i<9; i++) {
  694. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  695. this.roster.get(jid).set('chat_status', 'away');
  696. }
  697. for (i=9; i<12; i++) {
  698. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  699. this.roster.get(jid).set('chat_status', 'xa');
  700. }
  701. for (i=12; i<15; i++) {
  702. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  703. this.roster.get(jid).set('chat_status', 'unavailable');
  704. }
  705. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  706. for (i=0; i<3; i++) {
  707. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  708. }
  709. for (i=3; i<6; i++) {
  710. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  711. }
  712. for (i=6; i<9; i++) {
  713. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  714. }
  715. for (i=9; i<12; i++) {
  716. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  717. }
  718. for (i=12; i<15; i++) {
  719. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  720. }
  721. for (i=15; i<mock.cur_names.length; i++) {
  722. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  723. }
  724. }, this));
  725. }, converse));
  726. }, converse));
  727. describe("Requesting Contacts", $.proxy(function () {
  728. beforeEach($.proxy(function () {
  729. runs(function () {
  730. utils.clearBrowserStorage();
  731. converse.rosterview.model.reset();
  732. utils.createContacts('requesting').openControlBox();
  733. });
  734. waits(50);
  735. runs(function () {
  736. utils.openContactsPanel();
  737. });
  738. }, converse));
  739. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  740. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  741. var i, children;
  742. var names = [];
  743. spyOn(converse, 'emit');
  744. spyOn(this.rosterview, 'update').andCallThrough();
  745. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  746. var addName = function (idx, item) {
  747. if (!$(item).hasClass('request-actions')) {
  748. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  749. }
  750. };
  751. for (i=0; i<mock.req_names.length; i++) {
  752. this.roster.create({
  753. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  754. subscription: 'none',
  755. ask: null,
  756. requesting: true,
  757. fullname: mock.req_names[i]
  758. });
  759. expect(this.rosterview.update).toHaveBeenCalled();
  760. // When a requesting contact is added, the controlbox must
  761. // be opened.
  762. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  763. }
  764. // Check that they are sorted alphabetically
  765. children = this.rosterview.get('Contact requests').$el.siblings('dd.requesting-xmpp-contact').children('span');
  766. names = [];
  767. children.each(addName);
  768. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  769. }, converse));
  770. it("do not have a header if there aren't any", $.proxy(function () {
  771. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  772. var name = mock.req_names[0];
  773. runs($.proxy(function () {
  774. spyOn(window, 'confirm').andReturn(true);
  775. this.roster.create({
  776. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  777. subscription: 'none',
  778. ask: null,
  779. requesting: true,
  780. fullname: name
  781. });
  782. }, this));
  783. waits(50);
  784. runs($.proxy(function () {
  785. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(true);
  786. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  787. .siblings('.request-actions')
  788. .find('.decline-xmpp-request').click();
  789. expect(window.confirm).toHaveBeenCalled();
  790. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(false);
  791. }, this));
  792. }, converse));
  793. it("can be collapsed under their own header", $.proxy(function () {
  794. checkHeaderToggling.apply(this, [this.rosterview.get('Contact requests').$el]);
  795. }, converse));
  796. it("can have their requests accepted by the user", $.proxy(function () {
  797. // TODO: Testing can be more thorough here, the user is
  798. // actually not accepted/authorized because of
  799. // mock_connection.
  800. var name = mock.req_names.sort()[0];
  801. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  802. spyOn(this.connection.roster, 'authorize');
  803. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  804. .siblings('.request-actions')
  805. .find('.accept-xmpp-request').click();
  806. expect(this.connection.roster.authorize).toHaveBeenCalled();
  807. }, converse));
  808. it("can have their requests denied by the user", $.proxy(function () {
  809. this.rosterview.model.reset();
  810. runs($.proxy(function () {
  811. spyOn(converse, 'emit');
  812. spyOn(this.connection.roster, 'unauthorize');
  813. spyOn(window, 'confirm').andReturn(true);
  814. utils.createContacts('requesting').openControlBox();
  815. converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced.
  816. }, this));
  817. waits(50);
  818. runs($.proxy(function () {
  819. var name = mock.req_names.sort()[1];
  820. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  821. .siblings('.request-actions')
  822. .find('.decline-xmpp-request').click();
  823. }, this));
  824. waits(50);
  825. runs($.proxy(function () {
  826. expect(window.confirm).toHaveBeenCalled();
  827. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  828. // There should now be one less contact
  829. expect(this.roster.length).toEqual(mock.req_names.length-1);
  830. }, this));
  831. }, converse));
  832. it("are persisted even if other contacts' change their presence ", $.proxy(function() {
  833. /* This is a regression test.
  834. * https://github.com/jcbrand/converse.js/issues/262
  835. */
  836. this.rosterview.model.reset();
  837. spyOn(this.roster, 'clearCache').andCallThrough();
  838. expect(this.roster.pluck('jid').length).toBe(0);
  839. var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'});
  840. this.connection._dataRecv(test_utils.createRequest(stanza));
  841. expect(this.roster.pluck('jid').length).toBe(1);
  842. expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  843. // Taken from the spec
  844. // http://xmpp.org/rfcs/rfc3921.html#rfc.section.7.3
  845. stanza = $iq({
  846. to: this.connection.jid,
  847. type: 'result',
  848. id: 'roster_1'
  849. }).c('query', {
  850. xmlns: 'jabber:iq:roster',
  851. }).c('item', {
  852. jid: 'romeo@example.net',
  853. name: 'Romeo',
  854. subscription:'both'
  855. }).c('group').t('Friends').up().up()
  856. .c('item', {
  857. jid: 'mercutio@example.org',
  858. name: 'Mercutio',
  859. subscription:'from'
  860. }).c('group').t('Friends').up().up()
  861. .c('item', {
  862. jid: 'benvolio@example.org',
  863. name: 'Benvolio',
  864. subscription:'both'
  865. }).c('group').t('Friends');
  866. this.connection.roster._onReceiveRosterSuccess(null, stanza.tree());
  867. expect(this.roster.clearCache).toHaveBeenCalled();
  868. expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  869. }, converse));
  870. }, converse));
  871. describe("All Contacts", $.proxy(function () {
  872. beforeEach($.proxy(function () {
  873. utils.clearBrowserStorage();
  874. converse.rosterview.model.reset();
  875. utils.createContacts('all').openControlBox();
  876. utils.openContactsPanel();
  877. }, converse));
  878. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  879. var new_attrs, old_attrs, attrs;
  880. var num_contacts = this.roster.length;
  881. new_roster = new this.RosterContacts();
  882. // Roster items are yet to be fetched from browserStorage
  883. expect(new_roster.length).toEqual(0);
  884. new_roster.browserStorage = this.roster.browserStorage;
  885. new_roster.fetch();
  886. expect(new_roster.length).toEqual(num_contacts);
  887. // Check that the roster items retrieved from browserStorage
  888. // have the same attributes values as the original ones.
  889. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  890. for (i=0; i<attrs.length; i++) {
  891. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  892. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  893. // Roster items in storage are not necessarily sorted,
  894. // so we have to sort them here to do a proper
  895. // comparison
  896. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  897. }
  898. }, converse));
  899. }, converse));
  900. }, converse, mock, test_utils));
  901. describe("The 'Add Contact' widget", $.proxy(function (mock, test_utils) {
  902. it("opens up an add form when you click on it", $.proxy(function () {
  903. var panel = this.chatboxviews.get('controlbox').contactspanel;
  904. spyOn(panel, 'toggleContactForm').andCallThrough();
  905. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  906. panel.$el.find('a.toggle-xmpp-contact-form').click();
  907. expect(panel.toggleContactForm).toHaveBeenCalled();
  908. // XXX: Awaiting more tests, close it again for now...
  909. panel.$el.find('a.toggle-xmpp-contact-form').click();
  910. }, converse));
  911. }, converse, mock, test_utils));
  912. describe("The Controlbox Tabs", $.proxy(function () {
  913. beforeEach($.proxy(function () {
  914. runs(function () {
  915. test_utils.closeAllChatBoxes();
  916. });
  917. waits(50);
  918. runs(function () {
  919. test_utils.openControlBox();
  920. });
  921. }, converse));
  922. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  923. var cbview = this.chatboxviews.get('controlbox');
  924. var $panels = cbview.$el.find('.controlbox-panes');
  925. expect($panels.children().length).toBe(2);
  926. expect($panels.children().first().attr('id')).toBe('users');
  927. expect($panels.children().first().is(':visible')).toBe(true);
  928. expect($panels.children().last().attr('id')).toBe('chatrooms');
  929. expect($panels.children().last().is(':visible')).toBe(false);
  930. }, converse));
  931. describe("chatrooms panel", $.proxy(function () {
  932. beforeEach($.proxy(function () {
  933. runs(function () {
  934. test_utils.closeAllChatBoxes();
  935. });
  936. waits(50);
  937. runs(function () {
  938. test_utils.openControlBox();
  939. });
  940. }, converse));
  941. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  942. var cbview = this.chatboxviews.get('controlbox');
  943. var $tabs = cbview.$el.find('#controlbox-tabs');
  944. var $panels = cbview.$el.find('.controlbox-panes');
  945. var $contacts = $panels.children().first();
  946. var $chatrooms = $panels.children().last();
  947. spyOn(cbview, 'switchTab').andCallThrough();
  948. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  949. runs(function () {
  950. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  951. });
  952. waits(250);
  953. runs(function () {
  954. expect($contacts.is(':visible')).toBe(false);
  955. expect($chatrooms.is(':visible')).toBe(true);
  956. expect(cbview.switchTab).toHaveBeenCalled();
  957. });
  958. }, converse));
  959. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  960. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  961. var $input = roomspanel.$el.find('input.new-chatroom-name');
  962. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  963. var $server = roomspanel.$el.find('input.new-chatroom-server');
  964. expect($input.length).toBe(1);
  965. expect($server.length).toBe(1);
  966. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  967. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  968. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  969. runs(function () {
  970. $input.val('Lounge');
  971. $nick.val('dummy');
  972. $server.val('muc.localhost');
  973. });
  974. waits('250');
  975. runs(function () {
  976. roomspanel.$el.find('form').submit();
  977. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  978. });
  979. waits('250');
  980. runs($.proxy(function () {
  981. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  982. }, converse));
  983. }, converse));
  984. }, converse));
  985. }, converse, mock, test_utils));
  986. }));