coreSpec.js 42 KB

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