controlbox.js 71 KB

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