controlbox.js 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. (function (root, factory) {
  2. define(["mock", "converse-core", "test_utils"], factory);
  3. } (this, function (mock, converse, test_utils) {
  4. var _ = converse.env._;
  5. var $ = converse.env.jQuery;
  6. var $pres = converse.env.$pres;
  7. var $iq = converse.env.$iq;
  8. var checkHeaderToggling = function ($header) {
  9. var $toggle = $header.find('a.group-toggle');
  10. expect($header.css('display')).toEqual('block');
  11. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  12. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  13. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  14. $toggle.click();
  15. expect($toggle.hasClass('icon-closed')).toBeTruthy();
  16. expect($toggle.hasClass('icon-opened')).toBeFalsy();
  17. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:hidden').length).toBeTruthy();
  18. $toggle.click();
  19. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  20. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  21. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  22. };
  23. describe("The Control Box", function () {
  24. it("can be opened by clicking a DOM element with class 'toggle-controlbox'", mock.initConverse(function (_converse) {
  25. runs(function () {
  26. // This spec will only pass if the controlbox is not currently
  27. // open yet.
  28. expect($("div#controlbox").is(':visible')).toBe(false);
  29. spyOn(_converse.controlboxtoggle, 'onClick').andCallThrough();
  30. spyOn(_converse.controlboxtoggle, 'showControlBox').andCallThrough();
  31. spyOn(_converse, 'emit');
  32. // Redelegate so that the spies are now registered as the event handlers (specifically for 'onClick')
  33. _converse.controlboxtoggle.delegateEvents();
  34. $('.toggle-controlbox').click();
  35. }.bind(_converse));
  36. waits(50);
  37. runs(function () {
  38. expect(_converse.controlboxtoggle.onClick).toHaveBeenCalled();
  39. expect(_converse.controlboxtoggle.showControlBox).toHaveBeenCalled();
  40. expect(_converse.emit).toHaveBeenCalledWith('controlBoxOpened', jasmine.any(Object));
  41. expect($("div#controlbox").is(':visible')).toBe(true);
  42. }.bind(_converse));
  43. }));
  44. describe("The Status Widget", function () {
  45. it("shows the user's chat status, which is online by default", mock.initConverse(function (_converse) {
  46. test_utils.openControlBox();
  47. var view = _converse.xmppstatusview;
  48. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  49. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online');
  50. }));
  51. it("can be used to set the current user's chat status", mock.initConverse(function (_converse) {
  52. test_utils.openControlBox();
  53. var view = _converse.xmppstatusview;
  54. spyOn(view, 'toggleOptions').andCallThrough();
  55. spyOn(view, 'setStatus').andCallThrough();
  56. spyOn(_converse, 'emit');
  57. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  58. runs(function () {
  59. view.$el.find('a.choose-xmpp-status').click();
  60. expect(view.toggleOptions).toHaveBeenCalled();
  61. });
  62. waits(250);
  63. runs(function () {
  64. spyOn(view, 'updateStatusUI').andCallThrough();
  65. view.initialize(); // Rebind events for spy
  66. $(view.$el.find('.dropdown dd ul li a')[1]).click(); // Change status to "dnd"
  67. expect(view.setStatus).toHaveBeenCalled();
  68. expect(_converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd');
  69. });
  70. waits(250);
  71. runs(function () {
  72. expect(view.updateStatusUI).toHaveBeenCalled();
  73. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false);
  74. expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true);
  75. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy');
  76. });
  77. }));
  78. it("can be used to set a custom status message", mock.initConverse(function (_converse) {
  79. test_utils.openControlBox();
  80. var view = _converse.xmppstatusview;
  81. _converse.xmppstatus.save({'status': 'online'});
  82. spyOn(view, 'setStatusMessage').andCallThrough();
  83. spyOn(view, 'renderStatusChangeForm').andCallThrough();
  84. spyOn(_converse, 'emit');
  85. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  86. view.$el.find('a.change-xmpp-status-message').click();
  87. expect(view.renderStatusChangeForm).toHaveBeenCalled();
  88. // The async testing here is used only to provide time for
  89. // visual feedback
  90. var msg = 'I am happy';
  91. runs (function () {
  92. view.$el.find('form input.custom-xmpp-status').val(msg);
  93. });
  94. waits(250);
  95. runs (function () {
  96. view.$el.find('form#set-custom-xmpp-status').submit();
  97. expect(view.setStatusMessage).toHaveBeenCalled();
  98. expect(_converse.emit).toHaveBeenCalledWith('statusMessageChanged', msg);
  99. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  100. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg);
  101. });
  102. }));
  103. });
  104. });
  105. describe("The Contacts Roster", function () {
  106. describe("The live filter", function () {
  107. it("will only appear when roster contacts flow over the visible area", mock.initConverse(function (_converse) {
  108. var $filter = _converse.rosterview.$('.roster-filter');
  109. var names = mock.cur_names;
  110. runs(function () {
  111. test_utils.openControlBox();
  112. _converse.rosterview.update(); // XXX: Will normally called as event handler
  113. });
  114. waits(5); // Needed, due to debounce
  115. runs(function () {
  116. expect($filter.length).toBe(1);
  117. expect($filter.is(':visible')).toBeFalsy();
  118. for (var i=0; i<names.length; i++) {
  119. _converse.roster.create({
  120. ask: null,
  121. fullname: names[i],
  122. jid: names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  123. requesting: 'false',
  124. subscription: 'both'
  125. });
  126. _converse.rosterview.update(); // XXX: Will normally called as event handler
  127. }
  128. });
  129. waits(5); // Needed, due to debounce
  130. runs(function () {
  131. $filter = _converse.rosterview.$('.roster-filter');
  132. if (_converse.rosterview.$roster.hasScrollBar()) {
  133. expect($filter.is(':visible')).toBeTruthy();
  134. } else {
  135. expect($filter.is(':visible')).toBeFalsy();
  136. }
  137. });
  138. }));
  139. it("can be used to filter the contacts shown", mock.initConverse(function (_converse) {
  140. var $filter;
  141. var $roster;
  142. runs(function () {
  143. _converse.roster_groups = true;
  144. test_utils.openControlBox();
  145. });
  146. waits(50);
  147. runs(function () {
  148. test_utils.createGroupedContacts(_converse);
  149. $filter = _converse.rosterview.$('.roster-filter');
  150. $roster = _converse.rosterview.$roster;
  151. });
  152. waits(5); // Needed, due to debounce in "update" method
  153. runs(function () {
  154. _converse.rosterview.filter_view.delegateEvents();
  155. expect($roster.find('dd:visible').length).toBe(15);
  156. expect($roster.find('dt:visible').length).toBe(5);
  157. $filter.val("candice");
  158. expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
  159. expect($roster.find('dt:visible').length).toBe(5); // ditto
  160. $filter.trigger('keydown');
  161. });
  162. waits(550); // Needed, due to debounce
  163. runs (function () {
  164. expect($roster.find('dd:visible').length).toBe(1);
  165. expect($roster.find('dd:visible').eq(0).text().trim()).toBe('Candice van der Knijff');
  166. expect($roster.find('dt:visible').length).toBe(1);
  167. expect($roster.find('dt:visible').eq(0).text()).toBe('colleagues');
  168. $filter = _converse.rosterview.$('.roster-filter');
  169. $filter.val("an");
  170. $filter.trigger('keydown');
  171. });
  172. waits(550); // Needed, due to debounce
  173. runs (function () {
  174. expect($roster.find('dd:visible').length).toBe(5);
  175. expect($roster.find('dt:visible').length).toBe(4);
  176. $filter = _converse.rosterview.$('.roster-filter');
  177. $filter.val("xxx");
  178. $filter.trigger('keydown');
  179. });
  180. waits(550); // 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 = _converse.rosterview.$('.roster-filter');
  185. $filter.val(""); // Check that contacts are shown again, when the filter string is cleared.
  186. $filter.trigger('keydown');
  187. });
  188. waits(550); // Needed, due to debounce
  189. runs(function () {
  190. expect($roster.find('dd:visible').length).toBe(15);
  191. expect($roster.find('dt:visible').length).toBe(5);
  192. });
  193. _converse.roster_groups = false;
  194. }));
  195. it("can be used to filter the groups shown", mock.initConverse(function (_converse) {
  196. var $filter;
  197. var $roster;
  198. var $type;
  199. runs(function () {
  200. _converse.roster_groups = true;
  201. test_utils.openControlBox();
  202. });
  203. waits(50); // Needed, due to debounce in "update" method
  204. runs(function () {
  205. test_utils.createGroupedContacts(_converse);
  206. _converse.rosterview.filter_view.delegateEvents();
  207. $filter = _converse.rosterview.$('.roster-filter');
  208. $roster = _converse.rosterview.$roster;
  209. $type = _converse.rosterview.$('.filter-type');
  210. $type.val('groups');
  211. });
  212. waits(550); // Needed, due to debounce
  213. runs(function () {
  214. expect($roster.find('dd:visible').length).toBe(15);
  215. expect($roster.find('dt:visible').length).toBe(5);
  216. $filter.val("colleagues");
  217. });
  218. waits(50); // Needed, due to debounce
  219. runs(function () {
  220. expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
  221. expect($roster.find('dt:visible').length).toBe(5); // ditto
  222. $filter.trigger('keydown');
  223. });
  224. waits(550); // Needed, due to debounce
  225. runs (function () {
  226. expect($roster.find('dt:visible').length).toBe(1);
  227. expect($roster.find('dt:visible').eq(0).text()).toBe('colleagues');
  228. // Check that all contacts under the group are shown
  229. expect($roster.find('dt:visible').nextUntil('dt', 'dd:hidden').length).toBe(0);
  230. $filter = _converse.rosterview.$('.roster-filter');
  231. $filter.val("xxx");
  232. $filter.trigger('keydown');
  233. });
  234. waits(550); // Needed, due to debounce
  235. runs (function () {
  236. expect($roster.find('dt:visible').length).toBe(0);
  237. $filter = _converse.rosterview.$('.roster-filter');
  238. $filter.val(""); // Check that groups are shown again, when the filter string is cleared.
  239. $filter.trigger('keydown');
  240. });
  241. waits(550); // Needed, due to debounce
  242. runs(function () {
  243. expect($roster.find('dd:visible').length).toBe(15);
  244. expect($roster.find('dt:visible').length).toBe(5);
  245. });
  246. _converse.roster_groups = false;
  247. }));
  248. it("has a button with which its contents can be cleared", mock.initConverse(function (_converse) {
  249. _converse.roster_groups = true;
  250. test_utils.createGroupedContacts(_converse);
  251. var $filter = _converse.rosterview.$('.roster-filter');
  252. runs (function () {
  253. _converse.rosterview.filter_view.delegateEvents();
  254. $filter.val("xxx");
  255. $filter.trigger('keydown');
  256. expect($filter.hasClass("x")).toBeFalsy();
  257. });
  258. waits(550); // Needed, due to debounce
  259. runs (function () {
  260. $filter = _converse.rosterview.$('.roster-filter');
  261. expect($filter.hasClass("x")).toBeTruthy();
  262. $filter.addClass("onX").click();
  263. expect($filter.val()).toBe("");
  264. });
  265. _converse.roster_groups = false;
  266. }));
  267. it("can be used to filter contacts by their chat state", mock.initConverse(function (_converse) {
  268. var $filter;
  269. var $roster;
  270. _converse.roster_groups = true;
  271. test_utils.createGroupedContacts(_converse);
  272. var jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
  273. _converse.roster.get(jid).set('chat_status', 'online');
  274. runs(function () {
  275. test_utils.openControlBox();
  276. _converse.rosterview.filter_view.delegateEvents();
  277. var $type = _converse.rosterview.$('.filter-type');
  278. $type.val('state').trigger('change');
  279. });
  280. waits(300); // Needed, due to debounce in "update" method
  281. runs(function () {
  282. $filter = _converse.rosterview.$('.state-type');
  283. $roster = _converse.rosterview.$roster;
  284. expect($roster.find('dd:visible').length).toBe(15);
  285. expect($roster.find('dt:visible').length).toBe(5);
  286. $filter.val("online");
  287. expect($roster.find('dd:visible').length).toBe(15); // because no change event yet
  288. expect($roster.find('dt:visible').length).toBe(5); // ditto
  289. $filter.trigger('change');
  290. });
  291. waits(550); // Needed, due to debounce
  292. runs (function () {
  293. expect($roster.find('dd:visible').length).toBe(1);
  294. expect($roster.find('dd:visible').eq(0).text().trim()).toBe('Rinse Sommer');
  295. expect($roster.find('dt:visible').length).toBe(1);
  296. var $type = _converse.rosterview.$('.filter-type');
  297. $type.val('contacts').trigger('change');
  298. _converse.roster_groups = false;
  299. });
  300. }));
  301. });
  302. describe("A Roster Group", function () {
  303. it("can be used to organize existing contacts", mock.initConverse(function (_converse) {
  304. runs(function () {
  305. _converse.roster_groups = true;
  306. spyOn(_converse, 'emit');
  307. spyOn(_converse.rosterview, 'update').andCallThrough();
  308. _converse.rosterview.render();
  309. test_utils.createContacts(_converse, 'pending');
  310. test_utils.createContacts(_converse, 'requesting');
  311. test_utils.createGroupedContacts(_converse);
  312. });
  313. waits(50); // Needed, due to debounce
  314. runs(function () {
  315. // Check that the groups appear alphabetically and that
  316. // requesting and pending contacts are last.
  317. var group_titles = $.map(_converse.rosterview.$el.find('dt'), function (o) { return $(o).text().trim(); });
  318. expect(group_titles).toEqual([
  319. "Contact requests",
  320. "colleagues",
  321. "Family",
  322. "friends & acquaintences",
  323. "ænemies",
  324. "Ungrouped",
  325. "Pending contacts"
  326. ]);
  327. // Check that usernames appear alphabetically per group
  328. _.each(_.keys(mock.groups), function (name) {
  329. var $contacts = _converse.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  330. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  331. expect(names).toEqual(_.clone(names).sort());
  332. });
  333. });
  334. }));
  335. it("can share contacts with other roster groups", mock.initConverse(function (_converse) {
  336. _converse.roster_groups = true;
  337. var groups = ['colleagues', 'friends'];
  338. runs(function () {
  339. var i=0;
  340. spyOn(_converse, 'emit');
  341. spyOn(_converse.rosterview, 'update').andCallThrough();
  342. _converse.rosterview.render();
  343. for (i=0; i<mock.cur_names.length; i++) {
  344. _converse.roster.create({
  345. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  346. subscription: 'both',
  347. ask: null,
  348. groups: groups,
  349. fullname: mock.cur_names[i]
  350. });
  351. }
  352. });
  353. waits(50); // Needed, due to debounce
  354. runs(function () {
  355. // Check that usernames appear alphabetically per group
  356. _.each(groups, function (name) {
  357. var $contacts = _converse.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  358. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  359. expect(names).toEqual(_.clone(names).sort());
  360. expect(names.length).toEqual(mock.cur_names.length);
  361. });
  362. });
  363. }));
  364. it("remembers whether it is closed or opened", mock.initConverse(function (_converse) {
  365. _converse.roster_groups = true;
  366. var i=0, j=0;
  367. var groups = {
  368. 'colleagues': 3,
  369. 'friends & acquaintences': 3,
  370. 'Ungrouped': 2
  371. };
  372. _.each(_.keys(groups), function (name) {
  373. j = i;
  374. for (i=j; i<j+groups[name]; i++) {
  375. _converse.roster.create({
  376. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  377. subscription: 'both',
  378. ask: null,
  379. groups: name === 'ungrouped'? [] : [name],
  380. fullname: mock.cur_names[i]
  381. });
  382. }
  383. });
  384. var view = _converse.rosterview.get('colleagues');
  385. var $toggle = view.$el.find('a.group-toggle');
  386. expect(view.model.get('state')).toBe('opened');
  387. $toggle.click();
  388. expect(view.model.get('state')).toBe('closed');
  389. $toggle.click();
  390. expect(view.model.get('state')).toBe('opened');
  391. }));
  392. });
  393. describe("Pending Contacts", function () {
  394. function _addContacts (_converse) {
  395. // Must be initialized, so that render is called and documentFragment set up.
  396. test_utils.createContacts(_converse, 'pending').openControlBox().openContactsPanel(_converse);
  397. }
  398. it("can be collapsed under their own header", mock.initConverse(function (_converse) {
  399. runs(function () {
  400. _addContacts(_converse);
  401. });
  402. waits(50);
  403. runs(function () {
  404. checkHeaderToggling.apply(_converse, [_converse.rosterview.get('Pending contacts').$el]);
  405. });
  406. }));
  407. it("can be added to the roster", mock.initConverse(function (_converse) {
  408. spyOn(_converse, 'emit');
  409. spyOn(_converse.rosterview, 'update').andCallThrough();
  410. runs(function () {
  411. test_utils.openControlBox();
  412. _converse.roster.create({
  413. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  414. subscription: 'none',
  415. ask: 'subscribe',
  416. fullname: mock.pend_names[0]
  417. });
  418. });
  419. waits(300);
  420. runs(function () {
  421. expect(_converse.rosterview.$el.is(':visible')).toEqual(true);
  422. expect(_converse.rosterview.update).toHaveBeenCalled();
  423. });
  424. }));
  425. it("are shown in the roster when show_only_online_users", mock.initConverse(function (_converse) {
  426. _converse.show_only_online_users = true;
  427. runs(function () {
  428. _addContacts(_converse);
  429. });
  430. waits(50);
  431. spyOn(_converse.rosterview, 'update').andCallThrough();
  432. runs(function () {
  433. expect(_converse.rosterview.$el.is(':visible')).toEqual(true);
  434. expect(_converse.rosterview.update).toHaveBeenCalled();
  435. });
  436. waits(300); // Needed, due to debounce
  437. runs (function () {
  438. expect(_converse.rosterview.$el.find('dd:visible').length).toBe(3);
  439. expect(_converse.rosterview.$el.find('dt:visible').length).toBe(1);
  440. });
  441. _converse.show_only_online_users = false;
  442. }));
  443. it("are shown in the roster when hide_offline_users", mock.initConverse(function (_converse) {
  444. _converse.hide_offline_users = true;
  445. runs(function () {
  446. _addContacts(_converse);
  447. });
  448. waits(50);
  449. spyOn(_converse.rosterview, 'update').andCallThrough();
  450. runs(function () {
  451. expect(_converse.rosterview.$el.is(':visible')).toEqual(true);
  452. expect(_converse.rosterview.update).toHaveBeenCalled();
  453. });
  454. waits(300); // Needed, due to debounce
  455. runs (function () {
  456. expect(_converse.rosterview.$el.find('dd:visible').length).toBe(3);
  457. expect(_converse.rosterview.$el.find('dt:visible').length).toBe(1);
  458. });
  459. _converse.hide_offline_users = false;
  460. }));
  461. it("can be removed by the user", mock.initConverse(function (_converse) {
  462. runs(function () {
  463. _addContacts(_converse);
  464. });
  465. waits(50);
  466. runs(function () {
  467. var name = mock.pend_names[0];
  468. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  469. var contact = _converse.roster.get(jid);
  470. spyOn(window, 'confirm').andReturn(true);
  471. spyOn(contact, 'unauthorize').andCallFake(function () { return contact; });
  472. spyOn(contact, 'removeFromRoster');
  473. spyOn(_converse.connection, 'sendIQ').andCallFake(function (iq, callback) {
  474. if (typeof callback === "function") { return callback(); }
  475. });
  476. _converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  477. .parent().siblings('.remove-xmpp-contact').click();
  478. expect(window.confirm).toHaveBeenCalled();
  479. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  480. expect(contact.removeFromRoster).toHaveBeenCalled();
  481. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  482. expect(_converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
  483. });
  484. }));
  485. it("do not have a header if there aren't any", mock.initConverse(function (_converse) {
  486. runs(function () {
  487. test_utils.openControlBox();
  488. var name = mock.pend_names[0];
  489. _converse.roster.create({
  490. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  491. subscription: 'none',
  492. ask: 'subscribe',
  493. fullname: name
  494. });
  495. });
  496. waits(20);
  497. runs(function () {
  498. spyOn(window, 'confirm').andReturn(true);
  499. spyOn(_converse.connection, 'sendIQ').andCallFake(function (iq, callback) {
  500. if (typeof callback === "function") { return callback(); }
  501. });
  502. expect(_converse.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(true);
  503. _converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  504. .parent().siblings('.remove-xmpp-contact').click();
  505. expect(window.confirm).toHaveBeenCalled();
  506. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  507. expect(_converse.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(false);
  508. });
  509. }));
  510. it("will lose their own header once the last one has been removed", mock.initConverse(function (_converse) {
  511. _addContacts(_converse);
  512. var name;
  513. spyOn(window, 'confirm').andReturn(true);
  514. for (var i=0; i<mock.pend_names.length; i++) {
  515. name = mock.pend_names[i];
  516. _converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  517. .parent().siblings('.remove-xmpp-contact').click();
  518. }
  519. expect(_converse.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  520. }));
  521. it("can be added to the roster and they will be sorted alphabetically", mock.initConverse(function (_converse) {
  522. var i, t;
  523. spyOn(_converse, 'emit');
  524. spyOn(_converse.rosterview, 'update').andCallThrough();
  525. for (i=0; i<mock.pend_names.length; i++) {
  526. _converse.roster.create({
  527. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  528. subscription: 'none',
  529. ask: 'subscribe',
  530. fullname: mock.pend_names[i]
  531. });
  532. expect(_converse.rosterview.update).toHaveBeenCalled();
  533. }
  534. // Check that they are sorted alphabetically
  535. t = _converse.rosterview.get('Pending contacts').$el.siblings('dd.pending-xmpp-contact').find('span').text();
  536. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  537. }));
  538. });
  539. describe("Existing Contacts", function () {
  540. var _addContacts = function (_converse) {
  541. test_utils.createContacts(_converse, 'current').openControlBox().openContactsPanel(_converse);
  542. };
  543. it("can be collapsed under their own header", mock.initConverse(function (_converse) {
  544. runs(function () {
  545. _addContacts(_converse);
  546. });
  547. waits(50);
  548. runs(function () {
  549. checkHeaderToggling.apply(_converse, [_converse.rosterview.$el.find('dt.roster-group')]);
  550. });
  551. }));
  552. it("will be hidden when appearing under a collapsed group", mock.initConverse(function (_converse) {
  553. _converse.roster_groups = false;
  554. _addContacts(_converse);
  555. _converse.rosterview.$el.find('dt.roster-group').find('a.group-toggle').click();
  556. var name = "Max Mustermann";
  557. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  558. _converse.roster.create({
  559. ask: null,
  560. fullname: name,
  561. jid: jid,
  562. requesting: false,
  563. subscription: 'both'
  564. });
  565. var view = _converse.rosterview.get('My contacts').get(jid);
  566. expect(view.$el.is(':visible')).toBe(false);
  567. }));
  568. it("can be added to the roster and they will be sorted alphabetically", mock.initConverse(function (_converse) {
  569. var i, t;
  570. spyOn(_converse.rosterview, 'update').andCallThrough();
  571. runs(function () {
  572. for (i=0; i<mock.cur_names.length; i++) {
  573. _converse.roster.create({
  574. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  575. subscription: 'both',
  576. ask: null,
  577. fullname: mock.cur_names[i]
  578. });
  579. expect(_converse.rosterview.update).toHaveBeenCalled();
  580. }
  581. });
  582. waits(10);
  583. runs(function () {
  584. // Check that they are sorted alphabetically
  585. t = _converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  586. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  587. });
  588. }));
  589. it("can be removed by the user", mock.initConverse(function (_converse) {
  590. runs(function () {
  591. _addContacts(_converse);
  592. });
  593. waits(50);
  594. runs(function () {
  595. var name = mock.cur_names[0];
  596. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  597. var contact = _converse.roster.get(jid);
  598. spyOn(window, 'confirm').andReturn(true);
  599. spyOn(contact, 'removeFromRoster');
  600. spyOn(_converse.connection, 'sendIQ').andCallFake(function (iq, callback) {
  601. if (typeof callback === "function") { return callback(); }
  602. });
  603. _converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  604. .parent().find('.remove-xmpp-contact').click();
  605. expect(window.confirm).toHaveBeenCalled();
  606. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  607. expect(contact.removeFromRoster).toHaveBeenCalled();
  608. expect(_converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
  609. });
  610. }));
  611. it("do not have a header if there aren't any", mock.initConverse(function (_converse) {
  612. var name = mock.cur_names[0];
  613. var contact;
  614. runs(function () {
  615. contact = _converse.roster.create({
  616. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  617. subscription: 'both',
  618. ask: null,
  619. fullname: name
  620. });
  621. });
  622. waits(50);
  623. runs(function () {
  624. spyOn(window, 'confirm').andReturn(true);
  625. spyOn(contact, 'removeFromRoster');
  626. spyOn(_converse.connection, 'sendIQ').andCallFake(function (iq, callback) {
  627. if (typeof callback === "function") { return callback(); }
  628. });
  629. expect(_converse.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
  630. _converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  631. .parent().find('.remove-xmpp-contact').click();
  632. expect(window.confirm).toHaveBeenCalled();
  633. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  634. expect(contact.removeFromRoster).toHaveBeenCalled();
  635. expect(_converse.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  636. });
  637. }));
  638. it("can change their status to online and be sorted alphabetically", mock.initConverse(function (_converse) {
  639. runs(function () {
  640. _addContacts(_converse);
  641. });
  642. waits(50);
  643. runs(function () {
  644. var jid, t;
  645. spyOn(_converse, 'emit');
  646. spyOn(_converse.rosterview, 'update').andCallThrough();
  647. for (var i=0; i<mock.cur_names.length; i++) {
  648. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  649. _converse.roster.get(jid).set('chat_status', 'online');
  650. expect(_converse.rosterview.update).toHaveBeenCalled();
  651. // Check that they are sorted alphabetically
  652. t = _converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  653. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  654. }
  655. });
  656. }));
  657. it("can change their status to busy and be sorted alphabetically", mock.initConverse(function (_converse) {
  658. runs(function () {
  659. _addContacts(_converse);
  660. });
  661. waits(50);
  662. runs(function () {
  663. var jid, t;
  664. spyOn(_converse, 'emit');
  665. spyOn(_converse.rosterview, 'update').andCallThrough();
  666. for (var i=0; i<mock.cur_names.length; i++) {
  667. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  668. _converse.roster.get(jid).set('chat_status', 'dnd');
  669. expect(_converse.rosterview.update).toHaveBeenCalled();
  670. // Check that they are sorted alphabetically
  671. t = _converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  672. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  673. }
  674. });
  675. }));
  676. it("can change their status to away and be sorted alphabetically", mock.initConverse(function (_converse) {
  677. runs(function () {
  678. _addContacts(_converse);
  679. });
  680. waits(50);
  681. runs(function () {
  682. var jid, t;
  683. spyOn(_converse, 'emit');
  684. spyOn(_converse.rosterview, 'update').andCallThrough();
  685. for (var i=0; i<mock.cur_names.length; i++) {
  686. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  687. _converse.roster.get(jid).set('chat_status', 'away');
  688. expect(_converse.rosterview.update).toHaveBeenCalled();
  689. // Check that they are sorted alphabetically
  690. t = _converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  691. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  692. }
  693. });
  694. }));
  695. it("can change their status to xa and be sorted alphabetically", mock.initConverse(function (_converse) {
  696. runs(function () {
  697. _addContacts(_converse);
  698. });
  699. waits(50);
  700. runs(function () {
  701. var jid, t;
  702. spyOn(_converse, 'emit');
  703. spyOn(_converse.rosterview, 'update').andCallThrough();
  704. for (var i=0; i<mock.cur_names.length; i++) {
  705. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  706. _converse.roster.get(jid).set('chat_status', 'xa');
  707. expect(_converse.rosterview.update).toHaveBeenCalled();
  708. // Check that they are sorted alphabetically
  709. t = _converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  710. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  711. }
  712. });
  713. }));
  714. it("can change their status to unavailable and be sorted alphabetically", mock.initConverse(function (_converse) {
  715. runs(function () {
  716. _addContacts(_converse);
  717. });
  718. waits(50);
  719. runs(function () {
  720. var jid, t;
  721. spyOn(_converse, 'emit');
  722. spyOn(_converse.rosterview, 'update').andCallThrough();
  723. for (var i=0; i<mock.cur_names.length; i++) {
  724. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  725. _converse.roster.get(jid).set('chat_status', 'unavailable');
  726. expect(_converse.rosterview.update).toHaveBeenCalled();
  727. // Check that they are sorted alphabetically
  728. t = _converse.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  729. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  730. }
  731. });
  732. }));
  733. it("are ordered according to status: online, busy, away, xa, unavailable, offline", mock.initConverse(function (_converse) {
  734. runs(function () {
  735. _addContacts(_converse);
  736. });
  737. waits(50);
  738. runs(function () {
  739. var i, jid;
  740. for (i=0; i<3; i++) {
  741. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  742. _converse.roster.get(jid).set('chat_status', 'online');
  743. }
  744. for (i=3; i<6; i++) {
  745. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  746. _converse.roster.get(jid).set('chat_status', 'dnd');
  747. }
  748. for (i=6; i<9; i++) {
  749. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  750. _converse.roster.get(jid).set('chat_status', 'away');
  751. }
  752. for (i=9; i<12; i++) {
  753. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  754. _converse.roster.get(jid).set('chat_status', 'xa');
  755. }
  756. for (i=12; i<15; i++) {
  757. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  758. _converse.roster.get(jid).set('chat_status', 'unavailable');
  759. }
  760. var contacts = _converse.rosterview.$el.find('dd.current-xmpp-contact');
  761. for (i=0; i<3; i++) {
  762. expect($(contacts[i]).hasClass('online')).toBeTruthy();
  763. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  764. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  765. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  766. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  767. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  768. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  769. }
  770. for (i=3; i<6; i++) {
  771. expect($(contacts[i]).hasClass('dnd')).toBeTruthy();
  772. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  773. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  774. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  775. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  776. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  777. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  778. }
  779. for (i=6; i<9; i++) {
  780. expect($(contacts[i]).hasClass('away')).toBeTruthy();
  781. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  782. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  783. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  784. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  785. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  786. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  787. }
  788. for (i=9; i<12; i++) {
  789. expect($(contacts[i]).hasClass('xa')).toBeTruthy();
  790. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  791. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  792. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  793. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  794. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  795. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  796. }
  797. for (i=12; i<15; i++) {
  798. expect($(contacts[i]).hasClass('unavailable')).toBeTruthy();
  799. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  800. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  801. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  802. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  803. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  804. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  805. }
  806. for (i=15; i<mock.cur_names.length; i++) {
  807. expect($(contacts[i]).hasClass('offline')).toBeTruthy();
  808. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  809. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  810. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  811. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  812. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  813. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  814. }
  815. });
  816. }));
  817. });
  818. describe("Requesting Contacts", function () {
  819. it("can be added to the roster and they will be sorted alphabetically", mock.initConverse(function (_converse) {
  820. var i, children;
  821. var names = [];
  822. var addName = function (idx, item) {
  823. if (!$(item).hasClass('request-actions')) {
  824. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  825. }
  826. };
  827. runs(function () {
  828. test_utils.openContactsPanel(_converse);
  829. });
  830. waits(250);
  831. runs(function () {
  832. spyOn(_converse, 'emit');
  833. spyOn(_converse.rosterview, 'update').andCallThrough();
  834. spyOn(_converse.controlboxtoggle, 'showControlBox').andCallThrough();
  835. for (i=0; i<mock.req_names.length; i++) {
  836. _converse.roster.create({
  837. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  838. subscription: 'none',
  839. ask: null,
  840. requesting: true,
  841. fullname: mock.req_names[i]
  842. });
  843. }
  844. });
  845. waits(250);
  846. runs(function () {
  847. expect(_converse.rosterview.update).toHaveBeenCalled();
  848. // Check that they are sorted alphabetically
  849. children = _converse.rosterview.get('Contact requests').$el.siblings('dd.requesting-xmpp-contact').find('span');
  850. names = [];
  851. children.each(addName);
  852. expect(names.join('')).toEqual(mock.req_names.slice(0,mock.req_names.length+1).sort().join(''));
  853. });
  854. }));
  855. it("do not have a header if there aren't any", mock.initConverse(function (_converse) {
  856. test_utils.openContactsPanel(_converse);
  857. var name = mock.req_names[0];
  858. runs(function () {
  859. spyOn(window, 'confirm').andReturn(true);
  860. _converse.roster.create({
  861. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  862. subscription: 'none',
  863. ask: null,
  864. requesting: true,
  865. fullname: name
  866. });
  867. });
  868. waits(350);
  869. runs(function () {
  870. expect(_converse.rosterview.get('Contact requests').$el.is(':visible')).toEqual(true);
  871. _converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  872. .parent().siblings('.request-actions')
  873. .find('.decline-xmpp-request').click();
  874. expect(window.confirm).toHaveBeenCalled();
  875. expect(_converse.rosterview.get('Contact requests').$el.is(':visible')).toEqual(false);
  876. });
  877. }));
  878. it("can be collapsed under their own header", mock.initConverse(function (_converse) {
  879. runs(function () {
  880. test_utils.createContacts(_converse, 'requesting').openControlBox();
  881. });
  882. waits(10);
  883. runs(function () {
  884. checkHeaderToggling.apply(_converse, [_converse.rosterview.get('Contact requests').$el]);
  885. });
  886. }));
  887. it("can have their requests accepted by the user", mock.initConverse(function (_converse) {
  888. runs(function () {
  889. test_utils.createContacts(_converse, 'requesting').openControlBox();
  890. });
  891. waits(10);
  892. runs(function () {
  893. // TODO: Testing can be more thorough here, the user is
  894. // actually not accepted/authorized because of
  895. // mock_connection.
  896. var name = mock.req_names.sort()[0];
  897. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  898. var contact = _converse.roster.get(jid);
  899. spyOn(_converse.roster, 'sendContactAddIQ').andCallFake(function (jid, fullname, groups, callback) {
  900. callback();
  901. });
  902. spyOn(contact, 'authorize').andCallFake(function () { return contact; });
  903. _converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  904. .parent().siblings('.request-actions')
  905. .find('.accept-xmpp-request').click();
  906. expect(_converse.roster.sendContactAddIQ).toHaveBeenCalled();
  907. expect(contact.authorize).toHaveBeenCalled();
  908. });
  909. }));
  910. it("can have their requests denied by the user", mock.initConverse(function (_converse) {
  911. runs(function () {
  912. test_utils.createContacts(_converse, 'requesting').openControlBox();
  913. _converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced.
  914. });
  915. waits(50);
  916. runs(function () {
  917. var name = mock.req_names.sort()[1];
  918. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  919. var contact = _converse.roster.get(jid);
  920. spyOn(window, 'confirm').andReturn(true);
  921. spyOn(contact, 'unauthorize').andCallFake(function () { return contact; });
  922. _converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  923. .parent().siblings('.request-actions')
  924. .find('.decline-xmpp-request').click();
  925. expect(window.confirm).toHaveBeenCalled();
  926. expect(contact.unauthorize).toHaveBeenCalled();
  927. // There should now be one less contact
  928. expect(_converse.roster.length).toEqual(mock.req_names.length-1);
  929. });
  930. }));
  931. it("are persisted even if other contacts' change their presence ", mock.initConverse(function (_converse) {
  932. /* This is a regression test.
  933. * https://github.com/jcbrand/_converse.js/issues/262
  934. */
  935. expect(_converse.roster.pluck('jid').length).toBe(0);
  936. var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'});
  937. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  938. expect(_converse.roster.pluck('jid').length).toBe(1);
  939. expect(_.includes(_converse.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  940. // Taken from the spec
  941. // http://xmpp.org/rfcs/rfc3921.html#rfc.section.7.3
  942. stanza = $iq({
  943. to: _converse.connection.jid,
  944. type: 'result',
  945. id: 'roster_1'
  946. }).c('query', {
  947. xmlns: 'jabber:iq:roster',
  948. }).c('item', {
  949. jid: 'romeo@example.net',
  950. name: 'Romeo',
  951. subscription:'both'
  952. }).c('group').t('Friends').up().up()
  953. .c('item', {
  954. jid: 'mercutio@example.org',
  955. name: 'Mercutio',
  956. subscription:'from'
  957. }).c('group').t('Friends').up().up()
  958. .c('item', {
  959. jid: 'benvolio@example.org',
  960. name: 'Benvolio',
  961. subscription:'both'
  962. }).c('group').t('Friends');
  963. _converse.roster.onReceivedFromServer(stanza.tree());
  964. expect(_.includes(_converse.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  965. }));
  966. });
  967. describe("All Contacts", function () {
  968. it("are saved to, and can be retrieved from browserStorage", mock.initConverse(function (_converse) {
  969. test_utils.createContacts(_converse, 'all').openControlBox();
  970. test_utils.openContactsPanel(_converse);
  971. var new_attrs, old_attrs, attrs;
  972. var num_contacts = _converse.roster.length;
  973. var new_roster = new _converse.RosterContacts();
  974. // Roster items are yet to be fetched from browserStorage
  975. expect(new_roster.length).toEqual(0);
  976. new_roster.browserStorage = _converse.roster.browserStorage;
  977. new_roster.fetch();
  978. expect(new_roster.length).toEqual(num_contacts);
  979. // Check that the roster items retrieved from browserStorage
  980. // have the same attributes values as the original ones.
  981. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  982. for (var i=0; i<attrs.length; i++) {
  983. new_attrs = _.map(_.map(new_roster.models, 'attributes'), attrs[i]);
  984. old_attrs = _.map(_.map(_converse.roster.models, 'attributes'), attrs[i]);
  985. // Roster items in storage are not necessarily sorted,
  986. // so we have to sort them here to do a proper
  987. // comparison
  988. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  989. }
  990. }));
  991. it("will show fullname and jid properties on tooltip", mock.initConverse(function (_converse) {
  992. runs(function () {
  993. test_utils.createContacts(_converse, 'all').openControlBox();
  994. test_utils.openContactsPanel(_converse);
  995. });
  996. waits(10);
  997. runs(function () {
  998. var jid, name, i;
  999. for (i=0; i<mock.cur_names.length; i++) {
  1000. name = mock.cur_names[i];
  1001. jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1002. var $dd = _converse.rosterview.$el.find("dd:contains('"+name+"')").children().first();
  1003. var dd_text = $dd.text();
  1004. var dd_title = $dd.attr('title');
  1005. expect(dd_text).toBe(name);
  1006. expect(dd_title).toContain(name);
  1007. expect(dd_title).toContain(jid);
  1008. }
  1009. });
  1010. }));
  1011. });
  1012. });
  1013. describe("The 'Add Contact' widget", function () {
  1014. it("opens up an add form when you click on it", mock.initConverse(function (_converse) {
  1015. var panel = _converse.chatboxviews.get('controlbox').contactspanel;
  1016. spyOn(panel, 'toggleContactForm').andCallThrough();
  1017. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1018. panel.$el.find('a.toggle-xmpp-contact-form').click();
  1019. expect(panel.toggleContactForm).toHaveBeenCalled();
  1020. // XXX: Awaiting more tests, close it again for now...
  1021. panel.$el.find('a.toggle-xmpp-contact-form').click();
  1022. }));
  1023. });
  1024. describe("The Controlbox Tabs", function () {
  1025. it("contains two tabs, 'Contacts' and 'ChatRooms'", mock.initConverse(function (_converse) {
  1026. test_utils.openControlBox();
  1027. var cbview = _converse.chatboxviews.get('controlbox');
  1028. var $panels = cbview.$el.find('.controlbox-panes');
  1029. expect($panels.children().length).toBe(2);
  1030. expect($panels.children().first().attr('id')).toBe('users');
  1031. expect($panels.children().first().is(':visible')).toBe(true);
  1032. expect($panels.children().last().attr('id')).toBe('chatrooms');
  1033. expect($panels.children().last().is(':visible')).toBe(false);
  1034. }));
  1035. it("remembers which tab was open last", mock.initConverse(function (_converse) {
  1036. test_utils.openControlBox();
  1037. var cbview = _converse.chatboxviews.get('controlbox');
  1038. var $tabs = cbview.$el.find('#controlbox-tabs');
  1039. expect(cbview.model.get('active-panel')).toBe('users');
  1040. $tabs.find('li').last().find('a').click();
  1041. expect(cbview.model.get('active-panel')).toBe('chatrooms');
  1042. $tabs.find('li').first().find('a').click();
  1043. expect(cbview.model.get('active-panel')).toBe('users');
  1044. }));
  1045. describe("chatrooms panel", function () {
  1046. it("is opened by clicking the 'Chatrooms' tab", mock.initConverse(function (_converse) {
  1047. test_utils.openControlBox();
  1048. var cbview = _converse.chatboxviews.get('controlbox');
  1049. var $tabs = cbview.$el.find('#controlbox-tabs');
  1050. var $panels = cbview.$el.find('.controlbox-panes');
  1051. var $contacts = $panels.children().first();
  1052. var $chatrooms = $panels.children().last();
  1053. spyOn(cbview, 'switchTab').andCallThrough();
  1054. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1055. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  1056. expect($contacts.is(':visible')).toBe(false);
  1057. expect($chatrooms.is(':visible')).toBe(true);
  1058. expect(cbview.switchTab).toHaveBeenCalled();
  1059. }));
  1060. it("contains a form through which a new chatroom can be created", mock.initConverse(function (_converse) {
  1061. test_utils.openControlBox();
  1062. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  1063. var $input = roomspanel.$el.find('input.new-chatroom-name');
  1064. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  1065. var $server = roomspanel.$el.find('input.new-chatroom-server');
  1066. expect($input.length).toBe(1);
  1067. expect($server.length).toBe(1);
  1068. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  1069. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  1070. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1071. runs(function () {
  1072. $input.val('Lounge');
  1073. $nick.val('dummy');
  1074. $server.val('muc.localhost');
  1075. });
  1076. waits('250');
  1077. runs(function () {
  1078. roomspanel.$el.find('form').submit();
  1079. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  1080. });
  1081. waits('250');
  1082. runs(function () {
  1083. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  1084. });
  1085. }));
  1086. it("can list rooms publically available on the server", mock.initConverse(function (_converse) {
  1087. test_utils.openControlBox();
  1088. var panel = _converse.chatboxviews.get('controlbox').roomspanel;
  1089. panel.$tabs.find('li').last().find('a').click(); // Click the chatrooms tab
  1090. panel.model.set({'muc_domain': 'muc.localhost'}); // Make sure the domain is set
  1091. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  1092. expect($('#available-chatrooms').children('dt').length).toBe(0);
  1093. expect($('#available-chatrooms').children('dd').length).toBe(0);
  1094. var iq = $iq({
  1095. from:'muc.localhost',
  1096. to:'dummy@localhost/pda',
  1097. type:'result'
  1098. }).c('query')
  1099. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  1100. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  1101. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  1102. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  1103. panel.onRoomsFound(iq);
  1104. expect(panel.$('#available-chatrooms').children('dt').length).toBe(1);
  1105. expect(panel.$('#available-chatrooms').children('dt').first().text()).toBe("Rooms on muc.localhost");
  1106. expect(panel.$('#available-chatrooms').children('dd').length).toBe(4);
  1107. }));
  1108. });
  1109. });
  1110. }));