controlbox.js 61 KB

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