controlbox.js 59 KB

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