controlbox.js 63 KB

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