coreSpec.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2014 ownCloud Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('Core base tests', function() {
  7. var debounceStub
  8. beforeEach(function() {
  9. debounceStub = sinon.stub(_, 'debounce').callsFake(function(callback) {
  10. return function() {
  11. // defer instead of debounce, to make it work with clock
  12. _.defer(callback);
  13. };
  14. });
  15. });
  16. afterEach(function() {
  17. // many tests call window.initCore so need to unregister global events
  18. // ideally in the future we'll need a window.unloadCore() function
  19. $(document).off('ajaxError.main');
  20. $(document).off('unload.main');
  21. $(document).off('beforeunload.main');
  22. OC._userIsNavigatingAway = false;
  23. OC._reloadCalled = false;
  24. debounceStub.restore();
  25. });
  26. describe('Base values', function() {
  27. it('Sets webroots', function() {
  28. expect(OC.getRootPath()).toBeDefined();
  29. expect(window._oc_appswebroots).toBeDefined();
  30. });
  31. });
  32. describe('filePath', function() {
  33. beforeEach(function() {
  34. window._oc_webroot = 'http://localhost';
  35. window._oc_appswebroots.files = OC.getRootPath() + '/apps3/files';
  36. });
  37. afterEach(function() {
  38. delete window._oc_appswebroots.files;
  39. });
  40. it('Uses a direct link for css and images,' , function() {
  41. expect(OC.filePath('core', 'css', 'style.css')).toEqual('http://localhost/core/css/style.css');
  42. expect(OC.filePath('files', 'css', 'style.css')).toEqual('http://localhost/apps3/files/css/style.css');
  43. expect(OC.filePath('core', 'img', 'image.png')).toEqual('http://localhost/core/img/image.png');
  44. expect(OC.filePath('files', 'img', 'image.png')).toEqual('http://localhost/apps3/files/img/image.png');
  45. });
  46. it('Routes PHP files via index.php,' , function() {
  47. expect(OC.filePath('core', 'ajax', 'test.php')).toEqual('http://localhost/index.php/core/ajax/test.php');
  48. expect(OC.filePath('files', 'ajax', 'test.php')).toEqual('http://localhost/index.php/apps/files/ajax/test.php');
  49. });
  50. });
  51. describe('Link functions', function() {
  52. var TESTAPP = 'testapp';
  53. var TESTAPP_ROOT = OC.getRootPath() + '/appsx/testapp';
  54. beforeEach(function() {
  55. window._oc_appswebroots[TESTAPP] = TESTAPP_ROOT;
  56. });
  57. afterEach(function() {
  58. // restore original array
  59. delete window._oc_appswebroots[TESTAPP];
  60. });
  61. it('Generates correct links for core apps', function() {
  62. expect(OC.linkTo('core', 'somefile.php')).toEqual(OC.getRootPath() + '/core/somefile.php');
  63. expect(OC.linkTo('admin', 'somefile.php')).toEqual(OC.getRootPath() + '/admin/somefile.php');
  64. });
  65. it('Generates correct links for regular apps', function() {
  66. expect(OC.linkTo(TESTAPP, 'somefile.php')).toEqual(OC.getRootPath() + '/index.php/apps/' + TESTAPP + '/somefile.php');
  67. });
  68. it('Generates correct remote links', function() {
  69. expect(OC.linkToRemote('webdav')).toEqual(window.location.protocol + '//' + window.location.host + OC.getRootPath() + '/remote.php/webdav');
  70. });
  71. describe('Images', function() {
  72. it('Generates image path with given extension', function() {
  73. expect(OC.imagePath('core', 'somefile.jpg')).toEqual(OC.getRootPath() + '/core/img/somefile.jpg');
  74. expect(OC.imagePath(TESTAPP, 'somefile.jpg')).toEqual(TESTAPP_ROOT + '/img/somefile.jpg');
  75. });
  76. it('Generates image path with svg extension', function() {
  77. expect(OC.imagePath('core', 'somefile')).toEqual(OC.getRootPath() + '/core/img/somefile.svg');
  78. expect(OC.imagePath(TESTAPP, 'somefile')).toEqual(TESTAPP_ROOT + '/img/somefile.svg');
  79. });
  80. });
  81. });
  82. describe('Query string building', function() {
  83. it('Returns empty string when empty params', function() {
  84. expect(OC.buildQueryString()).toEqual('');
  85. expect(OC.buildQueryString({})).toEqual('');
  86. });
  87. it('Encodes regular query strings', function() {
  88. expect(OC.buildQueryString({
  89. a: 'abc',
  90. b: 'def'
  91. })).toEqual('a=abc&b=def');
  92. });
  93. it('Encodes special characters', function() {
  94. expect(OC.buildQueryString({
  95. unicode: '汉字'
  96. })).toEqual('unicode=%E6%B1%89%E5%AD%97');
  97. expect(OC.buildQueryString({
  98. b: 'spaace value',
  99. 'space key': 'normalvalue',
  100. 'slash/this': 'amp&ersand'
  101. })).toEqual('b=spaace%20value&space%20key=normalvalue&slash%2Fthis=amp%26ersand');
  102. });
  103. it('Encodes data types and empty values', function() {
  104. expect(OC.buildQueryString({
  105. 'keywithemptystring': '',
  106. 'keywithnull': null,
  107. 'keywithundefined': null,
  108. something: 'else'
  109. })).toEqual('keywithemptystring=&keywithnull&keywithundefined&something=else');
  110. expect(OC.buildQueryString({
  111. 'booleanfalse': false,
  112. 'booleantrue': true
  113. })).toEqual('booleanfalse=false&booleantrue=true');
  114. expect(OC.buildQueryString({
  115. 'number': 123
  116. })).toEqual('number=123');
  117. });
  118. });
  119. describe('Session heartbeat', function() {
  120. var clock,
  121. oldConfig,
  122. counter;
  123. beforeEach(function() {
  124. clock = sinon.useFakeTimers();
  125. oldConfig = OC.config;
  126. counter = 0;
  127. fakeServer.autoRespond = true;
  128. fakeServer.autoRespondAfter = 0;
  129. fakeServer.respondWith(/\/csrftoken/, function(xhr) {
  130. counter++;
  131. xhr.respond(200, {'Content-Type': 'application/json'}, '{"token": "pgBEsb3MzTb1ZPd2mfDZbQ6/0j3OrXHMEZrghHcOkg8=:3khw5PSa+wKQVo4f26exFD3nplud9ECjJ8/Y5zk5/k4="}');
  132. });
  133. $(document).off('ajaxComplete'); // ignore previously registered heartbeats
  134. });
  135. afterEach(function() {
  136. clock.restore();
  137. /* jshint camelcase: false */
  138. OC.config = oldConfig;
  139. $(document).off('ajaxError');
  140. $(document).off('ajaxComplete');
  141. });
  142. it('sends heartbeat half the session lifetime when heartbeat enabled', function() {
  143. /* jshint camelcase: false */
  144. OC.config = {
  145. session_keepalive: true,
  146. session_lifetime: 300
  147. };
  148. window.initCore();
  149. expect(counter).toEqual(0);
  150. // less than half, still nothing
  151. clock.tick(100 * 1000);
  152. expect(counter).toEqual(0);
  153. // reach past half (160), one call
  154. clock.tick(55 * 1000);
  155. expect(counter).toEqual(1);
  156. // almost there to the next, still one
  157. clock.tick(140 * 1000);
  158. expect(counter).toEqual(1);
  159. // past it, second call
  160. clock.tick(20 * 1000);
  161. expect(counter).toEqual(2);
  162. });
  163. it('does not send heartbeat when heartbeat disabled', function() {
  164. /* jshint camelcase: false */
  165. OC.config = {
  166. session_keepalive: false,
  167. session_lifetime: 300
  168. };
  169. window.initCore();
  170. expect(counter).toEqual(0);
  171. clock.tick(1000000);
  172. // still nothing
  173. expect(counter).toEqual(0);
  174. });
  175. it('limits the heartbeat between one minute and one day', function() {
  176. /* jshint camelcase: false */
  177. var setIntervalStub = sinon.stub(window, 'setInterval');
  178. OC.config = {
  179. session_keepalive: true,
  180. session_lifetime: 5
  181. };
  182. window.initCore();
  183. expect(setIntervalStub.getCall(0).args[1]).toEqual(60 * 1000);
  184. setIntervalStub.reset();
  185. OC.config = {
  186. session_keepalive: true,
  187. session_lifetime: 48 * 3600
  188. };
  189. window.initCore();
  190. expect(setIntervalStub.getCall(0).args[1]).toEqual(24 * 3600 * 1000);
  191. setIntervalStub.restore();
  192. });
  193. });
  194. describe('Parse query string', function() {
  195. it('Parses query string from full URL', function() {
  196. var query = OC.parseQueryString('http://localhost/stuff.php?q=a&b=x');
  197. expect(query).toEqual({q: 'a', b: 'x'});
  198. });
  199. it('Parses query string from query part alone', function() {
  200. var query = OC.parseQueryString('q=a&b=x');
  201. expect(query).toEqual({q: 'a', b: 'x'});
  202. });
  203. it('Returns null hash when empty query', function() {
  204. var query = OC.parseQueryString('');
  205. expect(query).toEqual(null);
  206. });
  207. it('Returns empty hash when empty query with question mark', function() {
  208. var query = OC.parseQueryString('?');
  209. expect(query).toEqual({});
  210. });
  211. it('Decodes regular query strings', function() {
  212. var query = OC.parseQueryString('a=abc&b=def');
  213. expect(query).toEqual({
  214. a: 'abc',
  215. b: 'def'
  216. });
  217. });
  218. it('Ignores empty parts', function() {
  219. var query = OC.parseQueryString('&q=a&&b=x&');
  220. expect(query).toEqual({q: 'a', b: 'x'});
  221. });
  222. it('Ignores lone equal signs', function() {
  223. var query = OC.parseQueryString('&q=a&=&b=x&');
  224. expect(query).toEqual({q: 'a', b: 'x'});
  225. });
  226. it('Includes extra equal signs in value', function() {
  227. var query = OC.parseQueryString('u=a=x&q=a=b');
  228. expect(query).toEqual({u: 'a=x', q: 'a=b'});
  229. });
  230. it('Decodes plus as space', function() {
  231. var query = OC.parseQueryString('space+key=space+value');
  232. expect(query).toEqual({'space key': 'space value'});
  233. });
  234. it('Decodes special characters', function() {
  235. var query = OC.parseQueryString('unicode=%E6%B1%89%E5%AD%97');
  236. expect(query).toEqual({unicode: '汉字'});
  237. query = OC.parseQueryString('b=spaace%20value&space%20key=normalvalue&slash%2Fthis=amp%26ersand');
  238. expect(query).toEqual({
  239. b: 'spaace value',
  240. 'space key': 'normalvalue',
  241. 'slash/this': 'amp&ersand'
  242. });
  243. });
  244. it('Decodes empty values', function() {
  245. var query = OC.parseQueryString('keywithemptystring=&keywithnostring');
  246. expect(query).toEqual({
  247. 'keywithemptystring': '',
  248. 'keywithnostring': null
  249. });
  250. });
  251. it('Does not interpret data types', function() {
  252. var query = OC.parseQueryString('booleanfalse=false&booleantrue=true&number=123');
  253. expect(query).toEqual({
  254. 'booleanfalse': 'false',
  255. 'booleantrue': 'true',
  256. 'number': '123'
  257. });
  258. });
  259. });
  260. describe('Generate Url', function() {
  261. it('returns absolute urls', function() {
  262. expect(OC.generateUrl('csrftoken')).toEqual(OC.getRootPath() + '/index.php/csrftoken');
  263. expect(OC.generateUrl('/csrftoken')).toEqual(OC.getRootPath() + '/index.php/csrftoken');
  264. });
  265. it('substitutes parameters which are escaped by default', function() {
  266. expect(OC.generateUrl('apps/files/download/{file}', {file: '<">ImAnUnescapedString/!'})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/%3C%22%3EImAnUnescapedString%2F!');
  267. });
  268. it('substitutes parameters which can also be unescaped via option flag', function() {
  269. expect(OC.generateUrl('apps/files/download/{file}', {file: 'subfolder/Welcome.txt'}, {escape: false})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/subfolder/Welcome.txt');
  270. });
  271. it('substitutes multiple parameters which are escaped by default', function() {
  272. expect(OC.generateUrl('apps/files/download/{file}/{id}', {file: '<">ImAnUnescapedString/!', id: 5})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/%3C%22%3EImAnUnescapedString%2F!/5');
  273. });
  274. it('substitutes multiple parameters which can also be unescaped via option flag', function() {
  275. expect(OC.generateUrl('apps/files/download/{file}/{id}', {file: 'subfolder/Welcome.txt', id: 5}, {escape: false})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/subfolder/Welcome.txt/5');
  276. });
  277. it('doesnt error out with no params provided', function () {
  278. expect(OC.generateUrl('apps/files/download{file}')).toEqual(OC.getRootPath() + '/index.php/apps/files/download%7Bfile%7D');
  279. });
  280. });
  281. describe('Util', function() {
  282. describe('computerFileSize', function() {
  283. it('correctly parses file sizes from a human readable formated string', function() {
  284. var data = [
  285. ['125', 125],
  286. ['125.25', 125],
  287. ['125.25B', 125],
  288. ['125.25 B', 125],
  289. ['0 B', 0],
  290. ['99999999999999999999999999999999999999999999 B', 99999999999999999999999999999999999999999999],
  291. ['0 MB', 0],
  292. ['0 kB', 0],
  293. ['0kB', 0],
  294. ['125 B', 125],
  295. ['125b', 125],
  296. ['125 KB', 128000],
  297. ['125kb', 128000],
  298. ['122.1 MB', 128031130],
  299. ['122.1mb', 128031130],
  300. ['119.2 GB', 127990025421],
  301. ['119.2gb', 127990025421],
  302. ['116.4 TB', 127983153473126],
  303. ['116.4tb', 127983153473126],
  304. ['8776656778888777655.4tb', 9.650036181387265e+30],
  305. [1234, null],
  306. [-1234, null],
  307. ['-1234 B', null],
  308. ['B', null],
  309. ['40/0', null],
  310. ['40,30 kb', null],
  311. [' 122.1 MB ', 128031130],
  312. ['122.1 MB ', 128031130],
  313. [' 122.1 MB ', 128031130],
  314. [' 122.1 MB ', 128031130],
  315. ['122.1 MB ', 128031130],
  316. [' 125', 125],
  317. [' 125 ', 125],
  318. ];
  319. for (var i = 0; i < data.length; i++) {
  320. expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]);
  321. }
  322. });
  323. it('returns null if the parameter is not a string', function() {
  324. expect(OC.Util.computerFileSize(NaN)).toEqual(null);
  325. expect(OC.Util.computerFileSize(125)).toEqual(null);
  326. });
  327. it('returns null if the string is unparsable', function() {
  328. expect(OC.Util.computerFileSize('')).toEqual(null);
  329. expect(OC.Util.computerFileSize('foobar')).toEqual(null);
  330. });
  331. });
  332. describe('stripTime', function() {
  333. it('strips time from dates', function() {
  334. expect(OC.Util.stripTime(new Date(2014, 2, 24, 15, 4, 45, 24)))
  335. .toEqual(new Date(2014, 2, 24, 0, 0, 0, 0));
  336. });
  337. });
  338. });
  339. describe('naturalSortCompare', function() {
  340. // cit() will skip tests if running on PhantomJS because it has issues with
  341. // localeCompare(). See https://github.com/ariya/phantomjs/issues/11063
  342. //
  343. // Please make sure to run these tests in Chrome/Firefox manually
  344. // to make sure they run.
  345. var cit = window.isPhantom?xit:it;
  346. // must provide the same results as \OC_Util::naturalSortCompare
  347. it('sorts alphabetically', function() {
  348. var a = [
  349. 'def',
  350. 'xyz',
  351. 'abc'
  352. ];
  353. a.sort(OC.Util.naturalSortCompare);
  354. expect(a).toEqual([
  355. 'abc',
  356. 'def',
  357. 'xyz'
  358. ]);
  359. });
  360. cit('sorts with different casing', function() {
  361. var a = [
  362. 'aaa',
  363. 'bbb',
  364. 'BBB',
  365. 'AAA'
  366. ];
  367. a.sort(OC.Util.naturalSortCompare);
  368. expect(a).toEqual([
  369. 'aaa',
  370. 'AAA',
  371. 'bbb',
  372. 'BBB'
  373. ]);
  374. });
  375. it('sorts with numbers', function() {
  376. var a = [
  377. '124.txt',
  378. 'abc1',
  379. '123.txt',
  380. 'abc',
  381. 'abc2',
  382. 'def (2).txt',
  383. 'ghi 10.txt',
  384. 'abc12',
  385. 'def.txt',
  386. 'def (1).txt',
  387. 'ghi 2.txt',
  388. 'def (10).txt',
  389. 'abc10',
  390. 'def (12).txt',
  391. 'z',
  392. 'ghi.txt',
  393. 'za',
  394. 'ghi 1.txt',
  395. 'ghi 12.txt',
  396. 'zz',
  397. '15.txt',
  398. '15b.txt'
  399. ];
  400. a.sort(OC.Util.naturalSortCompare);
  401. expect(a).toEqual([
  402. '15.txt',
  403. '15b.txt',
  404. '123.txt',
  405. '124.txt',
  406. 'abc',
  407. 'abc1',
  408. 'abc2',
  409. 'abc10',
  410. 'abc12',
  411. 'def.txt',
  412. 'def (1).txt',
  413. 'def (2).txt',
  414. 'def (10).txt',
  415. 'def (12).txt',
  416. 'ghi.txt',
  417. 'ghi 1.txt',
  418. 'ghi 2.txt',
  419. 'ghi 10.txt',
  420. 'ghi 12.txt',
  421. 'z',
  422. 'za',
  423. 'zz'
  424. ]);
  425. });
  426. it('sorts with chinese characters', function() {
  427. var a = [
  428. '十.txt',
  429. '一.txt',
  430. '二.txt',
  431. '十 2.txt',
  432. '三.txt',
  433. '四.txt',
  434. 'abc.txt',
  435. '五.txt',
  436. '七.txt',
  437. '八.txt',
  438. '九.txt',
  439. '六.txt',
  440. '十一.txt',
  441. '波.txt',
  442. '破.txt',
  443. '莫.txt',
  444. '啊.txt',
  445. '123.txt'
  446. ];
  447. a.sort(OC.Util.naturalSortCompare);
  448. expect(a).toEqual([
  449. '123.txt',
  450. 'abc.txt',
  451. '一.txt',
  452. '七.txt',
  453. '三.txt',
  454. '九.txt',
  455. '二.txt',
  456. '五.txt',
  457. '八.txt',
  458. '六.txt',
  459. '十.txt',
  460. '十 2.txt',
  461. '十一.txt',
  462. '啊.txt',
  463. '四.txt',
  464. '波.txt',
  465. '破.txt',
  466. '莫.txt'
  467. ]);
  468. });
  469. cit('sorts with umlauts', function() {
  470. var a = [
  471. 'öh.txt',
  472. 'Äh.txt',
  473. 'oh.txt',
  474. 'Üh 2.txt',
  475. 'Üh.txt',
  476. 'ah.txt',
  477. 'Öh.txt',
  478. 'uh.txt',
  479. 'üh.txt',
  480. 'äh.txt'
  481. ];
  482. a.sort(OC.Util.naturalSortCompare);
  483. expect(a).toEqual([
  484. 'ah.txt',
  485. 'äh.txt',
  486. 'Äh.txt',
  487. 'oh.txt',
  488. 'öh.txt',
  489. 'Öh.txt',
  490. 'uh.txt',
  491. 'üh.txt',
  492. 'Üh.txt',
  493. 'Üh 2.txt'
  494. ]);
  495. });
  496. });
  497. describe('Plugins', function() {
  498. var plugin;
  499. beforeEach(function() {
  500. plugin = {
  501. name: 'Some name',
  502. attach: function(obj) {
  503. obj.attached = true;
  504. },
  505. detach: function(obj) {
  506. obj.attached = false;
  507. }
  508. };
  509. OC.Plugins.register('OC.Test.SomeName', plugin);
  510. });
  511. it('attach plugin to object', function() {
  512. var obj = {something: true};
  513. OC.Plugins.attach('OC.Test.SomeName', obj);
  514. expect(obj.attached).toEqual(true);
  515. OC.Plugins.detach('OC.Test.SomeName', obj);
  516. expect(obj.attached).toEqual(false);
  517. });
  518. it('only call handler for target name', function() {
  519. var obj = {something: true};
  520. OC.Plugins.attach('OC.Test.SomeOtherName', obj);
  521. expect(obj.attached).not.toBeDefined();
  522. OC.Plugins.detach('OC.Test.SomeOtherName', obj);
  523. expect(obj.attached).not.toBeDefined();
  524. });
  525. });
  526. describe('Notifications', function() {
  527. var showHtmlSpy;
  528. var clock;
  529. /**
  530. * Returns the HTML or plain text of the given notification row.
  531. *
  532. * This is needed to ignore the close button that is added to the
  533. * notification row after the text.
  534. */
  535. var getNotificationText = function($node) {
  536. return $node.contents()[0].outerHTML ||
  537. $node.contents()[0].nodeValue;
  538. }
  539. beforeEach(function() {
  540. clock = sinon.useFakeTimers();
  541. });
  542. afterEach(function() {
  543. // jump past animations
  544. clock.tick(10000);
  545. clock.restore();
  546. $('body .toastify').remove();
  547. });
  548. describe('showTemporary', function() {
  549. it('shows a plain text notification with default timeout', function() {
  550. OC.Notification.showTemporary('My notification test');
  551. var $row = $('body .toastify');
  552. expect($row.length).toEqual(1);
  553. expect(getNotificationText($row)).toEqual('My notification test');
  554. });
  555. it('shows a HTML notification with default timeout', function() {
  556. OC.Notification.showTemporary('<a>My notification test</a>', { isHTML: true });
  557. var $row = $('body .toastify');
  558. expect($row.length).toEqual(1);
  559. expect(getNotificationText($row)).toEqual('<a>My notification test</a>');
  560. });
  561. it('hides itself after 7 seconds', function() {
  562. OC.Notification.showTemporary('');
  563. var $row = $('body .toastify');
  564. expect($row.length).toEqual(1);
  565. // travel in time +7000 milliseconds
  566. clock.tick(7500);
  567. $row = $('body .toastify');
  568. expect($row.length).toEqual(0);
  569. });
  570. it('hides itself after a given time', function() {
  571. OC.Notification.showTemporary('', {timeout: 10000});
  572. var $row = $('body .toastify');
  573. expect($row.length).toEqual(1);
  574. // travel in time +7000 milliseconds
  575. clock.tick(7500);
  576. $row = $('body .toastify');
  577. expect($row.length).toEqual(1);
  578. // travel in time another 4000 milliseconds
  579. clock.tick(4000);
  580. $row = $('body .toastify');
  581. expect($row.length).toEqual(0);
  582. });
  583. });
  584. describe('show', function() {
  585. it('hides itself after a given time', function() {
  586. OC.Notification.show('', {timeout: 10000});
  587. var $row = $('body .toastify');
  588. expect($row.length).toEqual(1);
  589. clock.tick(11500);
  590. $row = $('body .toastify');
  591. expect($row.length).toEqual(0);
  592. });
  593. it('does not hide itself if no timeout given to show', function() {
  594. OC.Notification.show('');
  595. var $row = $('body .toastify');
  596. expect($row.length).toEqual(1);
  597. // travel in time +1000 seconds
  598. clock.tick(1000000);
  599. $row = $('body .toastify');
  600. expect($row.length).toEqual(1);
  601. });
  602. });
  603. describe('showHtml', function() {
  604. it('hides itself after a given time', function() {
  605. OC.Notification.showHtml('<p></p>', {timeout: 10000});
  606. var $row = $('body .toastify');
  607. expect($row.length).toEqual(1);
  608. clock.tick(11500);
  609. $row = $('body .toastify');
  610. expect($row.length).toEqual(0);
  611. });
  612. it('does not hide itself if no timeout given to show', function() {
  613. OC.Notification.showHtml('<p></p>');
  614. var $row = $('body .toastify');
  615. expect($row.length).toEqual(1);
  616. // travel in time +1000 seconds
  617. clock.tick(1000000);
  618. $row = $('body .toastify');
  619. expect($row.length).toEqual(1);
  620. });
  621. });
  622. describe('hide', function() {
  623. it('hides a temporary notification before its timeout expires', function() {
  624. var hideCallback = sinon.spy();
  625. var notification = OC.Notification.showTemporary('');
  626. var $row = $('body .toastify');
  627. expect($row.length).toEqual(1);
  628. OC.Notification.hide(notification, hideCallback);
  629. // Give time to the hide animation to finish
  630. clock.tick(1000);
  631. $row = $('body .toastify');
  632. expect($row.length).toEqual(0);
  633. expect(hideCallback.calledOnce).toEqual(true);
  634. });
  635. it('hides a notification before its timeout expires', function() {
  636. var hideCallback = sinon.spy();
  637. var notification = OC.Notification.show('', {timeout: 10000});
  638. var $row = $('body .toastify');
  639. expect($row.length).toEqual(1);
  640. OC.Notification.hide(notification, hideCallback);
  641. // Give time to the hide animation to finish
  642. clock.tick(1000);
  643. $row = $('body .toastify');
  644. expect($row.length).toEqual(0);
  645. expect(hideCallback.calledOnce).toEqual(true);
  646. });
  647. it('hides a notification without timeout', function() {
  648. var hideCallback = sinon.spy();
  649. var notification = OC.Notification.show('');
  650. var $row = $('body .toastify');
  651. expect($row.length).toEqual(1);
  652. OC.Notification.hide(notification, hideCallback);
  653. // Give time to the hide animation to finish
  654. clock.tick(1000);
  655. $row = $('body .toastify');
  656. expect($row.length).toEqual(0);
  657. expect(hideCallback.calledOnce).toEqual(true);
  658. });
  659. });
  660. it('cumulates several notifications', function() {
  661. var $row1 = OC.Notification.showTemporary('One');
  662. var $row2 = OC.Notification.showTemporary('Two', {timeout: 2000});
  663. var $row3 = OC.Notification.showTemporary('Three');
  664. var $el = $('body');
  665. var $rows = $el.find('.toastify');
  666. expect($rows.length).toEqual(3);
  667. expect($rows.eq(0).is($row3)).toEqual(true);
  668. expect($rows.eq(1).is($row2)).toEqual(true);
  669. expect($rows.eq(2).is($row1)).toEqual(true);
  670. clock.tick(3000);
  671. $rows = $el.find('.toastify');
  672. expect($rows.length).toEqual(2);
  673. expect($rows.eq(0).is($row3)).toEqual(true);
  674. expect($rows.eq(1).is($row1)).toEqual(true);
  675. });
  676. it('hides the given notification when calling hide with argument', function() {
  677. var $row1 = OC.Notification.show('One');
  678. var $row2 = OC.Notification.show('Two');
  679. var $el = $('body');
  680. var $rows = $el.find('.toastify');
  681. expect($rows.length).toEqual(2);
  682. OC.Notification.hide($row2);
  683. clock.tick(3000);
  684. $rows = $el.find('.toastify');
  685. expect($rows.length).toEqual(1);
  686. expect($rows.eq(0).is($row1)).toEqual(true);
  687. });
  688. });
  689. describe('global ajax errors', function() {
  690. var reloadStub, ajaxErrorStub, clock;
  691. var notificationStub;
  692. var waitTimeMs = 6500;
  693. var oldCurrentUser;
  694. beforeEach(function() {
  695. oldCurrentUser = OC.currentUser;
  696. OC.currentUser = 'dummy';
  697. clock = sinon.useFakeTimers();
  698. reloadStub = sinon.stub(OC, 'reload');
  699. document.head.dataset.user = 'dummy'
  700. notificationStub = sinon.stub(OC.Notification, 'show');
  701. // unstub the error processing method
  702. ajaxErrorStub = OC._processAjaxError;
  703. ajaxErrorStub.restore();
  704. window.initCore();
  705. });
  706. afterEach(function() {
  707. OC.currentUser = oldCurrentUser;
  708. reloadStub.restore();
  709. notificationStub.restore();
  710. clock.restore();
  711. });
  712. it('does not reload the page if the user was navigating away', function() {
  713. var xhr = { status: 0 };
  714. OC._userIsNavigatingAway = true;
  715. clock.tick(100);
  716. $(document).trigger(new $.Event('ajaxError'), xhr);
  717. clock.tick(waitTimeMs);
  718. expect(reloadStub.notCalled).toEqual(true);
  719. });
  720. it('shows a temporary notification if the connection is lost', function() {
  721. var xhr = { status: 0 };
  722. spyOn(OC, '_ajaxConnectionLostHandler');
  723. $(document).trigger(new $.Event('ajaxError'), xhr);
  724. clock.tick(101);
  725. expect(OC._ajaxConnectionLostHandler.calls.count()).toBe(1);
  726. });
  727. });
  728. describe('Snapper', function() {
  729. var snapConstructorStub;
  730. var snapperStub;
  731. var clock;
  732. beforeEach(function() {
  733. snapConstructorStub = sinon.stub(window, 'Snap');
  734. snapperStub = {};
  735. snapperStub.enable = sinon.stub();
  736. snapperStub.disable = sinon.stub();
  737. snapperStub.close = sinon.stub();
  738. snapperStub.on = sinon.stub();
  739. snapperStub.state = sinon.stub().returns({
  740. state: sinon.stub()
  741. });
  742. snapConstructorStub.returns(snapperStub);
  743. clock = sinon.useFakeTimers();
  744. // _.now could have been set to Date.now before Sinon replaced it
  745. // with a fake version, so _.now must be stubbed to ensure that the
  746. // fake Date.now will be called instead of the original one.
  747. _.now = sinon.stub(_, 'now').callsFake(function() {
  748. return new Date().getTime();
  749. });
  750. $('#testArea').append('<div id="app-navigation">The navigation bar</div><div id="app-content">Content</div>');
  751. });
  752. afterEach(function() {
  753. snapConstructorStub.restore();
  754. clock.restore();
  755. _.now.restore();
  756. // Remove the event handler for the resize event added to the window
  757. // due to calling window.initCore() when there is an #app-navigation
  758. // element.
  759. $(window).off('resize');
  760. viewport.reset();
  761. });
  762. it('is enabled on a narrow screen', function() {
  763. viewport.set(480);
  764. window.initCore();
  765. expect(snapConstructorStub.calledOnce).toBe(true);
  766. expect(snapperStub.enable.calledOnce).toBe(true);
  767. expect(snapperStub.disable.called).toBe(false);
  768. });
  769. it('is disabled when disallowing the gesture on a narrow screen', function() {
  770. viewport.set(480);
  771. window.initCore();
  772. expect(snapperStub.enable.calledOnce).toBe(true);
  773. expect(snapperStub.disable.called).toBe(false);
  774. expect(snapperStub.close.called).toBe(false);
  775. OC.disallowNavigationBarSlideGesture();
  776. expect(snapperStub.enable.calledOnce).toBe(true);
  777. expect(snapperStub.disable.calledOnce).toBe(true);
  778. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  779. expect(snapperStub.close.called).toBe(false);
  780. });
  781. it('is not disabled again when disallowing the gesture twice on a narrow screen', function() {
  782. viewport.set(480);
  783. window.initCore();
  784. expect(snapperStub.enable.calledOnce).toBe(true);
  785. expect(snapperStub.disable.called).toBe(false);
  786. expect(snapperStub.close.called).toBe(false);
  787. OC.disallowNavigationBarSlideGesture();
  788. expect(snapperStub.enable.calledOnce).toBe(true);
  789. expect(snapperStub.disable.calledOnce).toBe(true);
  790. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  791. expect(snapperStub.close.called).toBe(false);
  792. OC.disallowNavigationBarSlideGesture();
  793. expect(snapperStub.enable.calledOnce).toBe(true);
  794. expect(snapperStub.disable.calledOnce).toBe(true);
  795. expect(snapperStub.close.called).toBe(false);
  796. });
  797. it('is enabled when allowing the gesture after disallowing it on a narrow screen', function() {
  798. viewport.set(480);
  799. window.initCore();
  800. expect(snapperStub.enable.calledOnce).toBe(true);
  801. expect(snapperStub.disable.called).toBe(false);
  802. expect(snapperStub.close.called).toBe(false);
  803. OC.disallowNavigationBarSlideGesture();
  804. expect(snapperStub.enable.calledOnce).toBe(true);
  805. expect(snapperStub.disable.calledOnce).toBe(true);
  806. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  807. expect(snapperStub.close.called).toBe(false);
  808. OC.allowNavigationBarSlideGesture();
  809. expect(snapperStub.enable.calledTwice).toBe(true);
  810. expect(snapperStub.disable.calledOnce).toBe(true);
  811. expect(snapperStub.close.called).toBe(false);
  812. });
  813. it('is not enabled again when allowing the gesture twice after disallowing it on a narrow screen', function() {
  814. viewport.set(480);
  815. window.initCore();
  816. expect(snapperStub.enable.calledOnce).toBe(true);
  817. expect(snapperStub.disable.called).toBe(false);
  818. expect(snapperStub.close.called).toBe(false);
  819. OC.disallowNavigationBarSlideGesture();
  820. expect(snapperStub.enable.calledOnce).toBe(true);
  821. expect(snapperStub.disable.calledOnce).toBe(true);
  822. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  823. expect(snapperStub.close.called).toBe(false);
  824. OC.allowNavigationBarSlideGesture();
  825. expect(snapperStub.enable.calledTwice).toBe(true);
  826. expect(snapperStub.disable.calledOnce).toBe(true);
  827. expect(snapperStub.close.called).toBe(false);
  828. OC.allowNavigationBarSlideGesture();
  829. expect(snapperStub.enable.calledTwice).toBe(true);
  830. expect(snapperStub.disable.calledOnce).toBe(true);
  831. expect(snapperStub.close.called).toBe(false);
  832. });
  833. it('is disabled on a wide screen', function() {
  834. viewport.set(1280);
  835. window.initCore();
  836. expect(snapConstructorStub.calledOnce).toBe(true);
  837. expect(snapperStub.enable.called).toBe(false);
  838. expect(snapperStub.disable.calledOnce).toBe(true);
  839. });
  840. it('is not disabled again when disallowing the gesture on a wide screen', function() {
  841. viewport.set(1280);
  842. window.initCore();
  843. expect(snapperStub.enable.called).toBe(false);
  844. expect(snapperStub.disable.calledOnce).toBe(true);
  845. expect(snapperStub.close.calledOnce).toBe(true);
  846. OC.disallowNavigationBarSlideGesture();
  847. expect(snapperStub.enable.called).toBe(false);
  848. expect(snapperStub.disable.calledOnce).toBe(true);
  849. expect(snapperStub.close.calledOnce).toBe(true);
  850. });
  851. it('is not enabled when allowing the gesture after disallowing it on a wide screen', function() {
  852. viewport.set(1280);
  853. window.initCore();
  854. expect(snapperStub.enable.called).toBe(false);
  855. expect(snapperStub.disable.calledOnce).toBe(true);
  856. expect(snapperStub.close.calledOnce).toBe(true);
  857. OC.disallowNavigationBarSlideGesture();
  858. expect(snapperStub.enable.called).toBe(false);
  859. expect(snapperStub.disable.calledOnce).toBe(true);
  860. expect(snapperStub.close.calledOnce).toBe(true);
  861. OC.allowNavigationBarSlideGesture();
  862. expect(snapperStub.enable.called).toBe(false);
  863. expect(snapperStub.disable.calledOnce).toBe(true);
  864. expect(snapperStub.close.calledOnce).toBe(true);
  865. });
  866. it('is enabled when resizing to a narrow screen', function() {
  867. viewport.set(1280);
  868. window.initCore();
  869. expect(snapperStub.enable.called).toBe(false);
  870. expect(snapperStub.disable.calledOnce).toBe(true);
  871. viewport.set(480);
  872. // Setting the viewport width does not automatically trigger a
  873. // resize.
  874. $(window).resize();
  875. // The resize handler is debounced to be executed a few milliseconds
  876. // after the resize event.
  877. clock.tick(1000);
  878. expect(snapperStub.enable.calledOnce).toBe(true);
  879. expect(snapperStub.disable.calledOnce).toBe(true);
  880. });
  881. it('is not enabled when resizing to a narrow screen after disallowing the gesture', function() {
  882. viewport.set(1280);
  883. window.initCore();
  884. expect(snapperStub.enable.called).toBe(false);
  885. expect(snapperStub.disable.calledOnce).toBe(true);
  886. OC.disallowNavigationBarSlideGesture();
  887. expect(snapperStub.enable.called).toBe(false);
  888. expect(snapperStub.disable.calledOnce).toBe(true);
  889. viewport.set(480);
  890. // Setting the viewport width does not automatically trigger a
  891. // resize.
  892. $(window).resize();
  893. // The resize handler is debounced to be executed a few milliseconds
  894. // after the resize event.
  895. clock.tick(1000);
  896. expect(snapperStub.enable.called).toBe(false);
  897. expect(snapperStub.disable.calledOnce).toBe(true);
  898. });
  899. it('is enabled when resizing to a narrow screen after disallowing the gesture and allowing it', function() {
  900. viewport.set(1280);
  901. window.initCore();
  902. expect(snapperStub.enable.called).toBe(false);
  903. expect(snapperStub.disable.calledOnce).toBe(true);
  904. OC.disallowNavigationBarSlideGesture();
  905. expect(snapperStub.enable.called).toBe(false);
  906. expect(snapperStub.disable.calledOnce).toBe(true);
  907. OC.allowNavigationBarSlideGesture();
  908. expect(snapperStub.enable.called).toBe(false);
  909. expect(snapperStub.disable.calledOnce).toBe(true);
  910. viewport.set(480);
  911. // Setting the viewport width does not automatically trigger a
  912. // resize.
  913. $(window).resize();
  914. // The resize handler is debounced to be executed a few milliseconds
  915. // after the resize event.
  916. clock.tick(1000);
  917. expect(snapperStub.enable.calledOnce).toBe(true);
  918. expect(snapperStub.disable.calledOnce).toBe(true);
  919. });
  920. it('is enabled when allowing the gesture after disallowing it and resizing to a narrow screen', function() {
  921. viewport.set(1280);
  922. window.initCore();
  923. expect(snapperStub.enable.called).toBe(false);
  924. expect(snapperStub.disable.calledOnce).toBe(true);
  925. OC.disallowNavigationBarSlideGesture();
  926. expect(snapperStub.enable.called).toBe(false);
  927. expect(snapperStub.disable.calledOnce).toBe(true);
  928. viewport.set(480);
  929. // Setting the viewport width does not automatically trigger a
  930. // resize.
  931. $(window).resize();
  932. // The resize handler is debounced to be executed a few milliseconds
  933. // after the resize event.
  934. clock.tick(1000);
  935. expect(snapperStub.enable.called).toBe(false);
  936. expect(snapperStub.disable.calledOnce).toBe(true);
  937. OC.allowNavigationBarSlideGesture();
  938. expect(snapperStub.enable.calledOnce).toBe(true);
  939. expect(snapperStub.disable.calledOnce).toBe(true);
  940. });
  941. it('is disabled when disallowing the gesture after disallowing it, resizing to a narrow screen and allowing it', function() {
  942. viewport.set(1280);
  943. window.initCore();
  944. expect(snapperStub.enable.called).toBe(false);
  945. expect(snapperStub.disable.calledOnce).toBe(true);
  946. OC.disallowNavigationBarSlideGesture();
  947. expect(snapperStub.enable.called).toBe(false);
  948. expect(snapperStub.disable.calledOnce).toBe(true);
  949. viewport.set(480);
  950. // Setting the viewport width does not automatically trigger a
  951. // resize.
  952. $(window).resize();
  953. // The resize handler is debounced to be executed a few milliseconds
  954. // after the resize event.
  955. clock.tick(1000);
  956. expect(snapperStub.enable.called).toBe(false);
  957. expect(snapperStub.disable.calledOnce).toBe(true);
  958. OC.allowNavigationBarSlideGesture();
  959. expect(snapperStub.enable.calledOnce).toBe(true);
  960. expect(snapperStub.disable.calledOnce).toBe(true);
  961. OC.disallowNavigationBarSlideGesture();
  962. expect(snapperStub.enable.calledOnce).toBe(true);
  963. expect(snapperStub.disable.calledTwice).toBe(true);
  964. expect(snapperStub.disable.getCall(1).calledWithExactly(true)).toBe(true);
  965. });
  966. it('is disabled when resizing to a wide screen', function() {
  967. viewport.set(480);
  968. window.initCore();
  969. expect(snapperStub.enable.calledOnce).toBe(true);
  970. expect(snapperStub.disable.called).toBe(false);
  971. expect(snapperStub.close.called).toBe(false);
  972. viewport.set(1280);
  973. // Setting the viewport width does not automatically trigger a
  974. // resize.
  975. $(window).resize();
  976. // The resize handler is debounced to be executed a few milliseconds
  977. // after the resize event.
  978. clock.tick(1000);
  979. expect(snapperStub.enable.calledOnce).toBe(true);
  980. expect(snapperStub.disable.calledOnce).toBe(true);
  981. expect(snapperStub.close.calledOnce).toBe(true);
  982. });
  983. it('is not disabled again when disallowing the gesture after resizing to a wide screen', function() {
  984. viewport.set(480);
  985. window.initCore();
  986. expect(snapperStub.enable.calledOnce).toBe(true);
  987. expect(snapperStub.disable.called).toBe(false);
  988. expect(snapperStub.close.called).toBe(false);
  989. viewport.set(1280);
  990. // Setting the viewport width does not automatically trigger a
  991. // resize.
  992. $(window).resize();
  993. // The resize handler is debounced to be executed a few milliseconds
  994. // after the resize event.
  995. clock.tick(1000);
  996. expect(snapperStub.enable.calledOnce).toBe(true);
  997. expect(snapperStub.disable.calledOnce).toBe(true);
  998. expect(snapperStub.close.calledOnce).toBe(true);
  999. OC.disallowNavigationBarSlideGesture();
  1000. expect(snapperStub.enable.calledOnce).toBe(true);
  1001. expect(snapperStub.disable.calledOnce).toBe(true);
  1002. expect(snapperStub.close.calledOnce).toBe(true);
  1003. });
  1004. it('is not enabled when allowing the gesture after disallowing it, resizing to a narrow screen and resizing to a wide screen', function() {
  1005. viewport.set(1280);
  1006. window.initCore();
  1007. expect(snapperStub.enable.called).toBe(false);
  1008. expect(snapperStub.disable.calledOnce).toBe(true);
  1009. expect(snapperStub.close.calledOnce).toBe(true);
  1010. OC.disallowNavigationBarSlideGesture();
  1011. expect(snapperStub.enable.called).toBe(false);
  1012. expect(snapperStub.disable.calledOnce).toBe(true);
  1013. expect(snapperStub.close.calledOnce).toBe(true);
  1014. viewport.set(480);
  1015. // Setting the viewport width does not automatically trigger a
  1016. // resize.
  1017. $(window).resize();
  1018. // The resize handler is debounced to be executed a few milliseconds
  1019. // after the resize event.
  1020. clock.tick(1000);
  1021. expect(snapperStub.enable.called).toBe(false);
  1022. expect(snapperStub.disable.calledOnce).toBe(true);
  1023. expect(snapperStub.close.calledOnce).toBe(true);
  1024. viewport.set(1280);
  1025. $(window).resize();
  1026. clock.tick(1000);
  1027. expect(snapperStub.enable.called).toBe(false);
  1028. expect(snapperStub.disable.calledTwice).toBe(true);
  1029. expect(snapperStub.close.calledTwice).toBe(true);
  1030. OC.allowNavigationBarSlideGesture();
  1031. expect(snapperStub.enable.called).toBe(false);
  1032. expect(snapperStub.disable.calledTwice).toBe(true);
  1033. expect(snapperStub.close.calledTwice).toBe(true);
  1034. });
  1035. });
  1036. });