coreSpec.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. describe('Base values', function() {
  23. it('Sets webroots', function() {
  24. expect(OC.webroot).toBeDefined();
  25. expect(OC.appswebroots).toBeDefined();
  26. });
  27. });
  28. describe('basename', function() {
  29. it('Returns the nothing if no file name given', function() {
  30. expect(OC.basename('')).toEqual('');
  31. });
  32. it('Returns the nothing if dir is root', function() {
  33. expect(OC.basename('/')).toEqual('');
  34. });
  35. it('Returns the same name if no path given', function() {
  36. expect(OC.basename('some name.txt')).toEqual('some name.txt');
  37. });
  38. it('Returns the base name if root path given', function() {
  39. expect(OC.basename('/some name.txt')).toEqual('some name.txt');
  40. });
  41. it('Returns the base name if double root path given', function() {
  42. expect(OC.basename('//some name.txt')).toEqual('some name.txt');
  43. });
  44. it('Returns the base name if subdir given without root', function() {
  45. expect(OC.basename('subdir/some name.txt')).toEqual('some name.txt');
  46. });
  47. it('Returns the base name if subdir given with root', function() {
  48. expect(OC.basename('/subdir/some name.txt')).toEqual('some name.txt');
  49. });
  50. it('Returns the base name if subdir given with double root', function() {
  51. expect(OC.basename('//subdir/some name.txt')).toEqual('some name.txt');
  52. });
  53. it('Returns the base name if subdir has dot', function() {
  54. expect(OC.basename('/subdir.dat/some name.txt')).toEqual('some name.txt');
  55. });
  56. it('Returns dot if file name is dot', function() {
  57. expect(OC.basename('/subdir/.')).toEqual('.');
  58. });
  59. // TODO: fix the source to make it work like PHP's basename
  60. it('Returns the dir itself if no file name given', function() {
  61. // TODO: fix the source to make it work like PHP's dirname
  62. // expect(OC.basename('subdir/')).toEqual('subdir');
  63. expect(OC.basename('subdir/')).toEqual('');
  64. });
  65. it('Returns the dir itself if no file name given with root', function() {
  66. // TODO: fix the source to make it work like PHP's dirname
  67. // expect(OC.basename('/subdir/')).toEqual('subdir');
  68. expect(OC.basename('/subdir/')).toEqual('');
  69. });
  70. });
  71. describe('dirname', function() {
  72. it('Returns the nothing if no file name given', function() {
  73. expect(OC.dirname('')).toEqual('');
  74. });
  75. it('Returns the root if dir is root', function() {
  76. // TODO: fix the source to make it work like PHP's dirname
  77. // expect(OC.dirname('/')).toEqual('/');
  78. expect(OC.dirname('/')).toEqual('');
  79. });
  80. it('Returns the root if dir is double root', function() {
  81. // TODO: fix the source to make it work like PHP's dirname
  82. // expect(OC.dirname('//')).toEqual('/');
  83. expect(OC.dirname('//')).toEqual('/'); // oh no...
  84. });
  85. it('Returns dot if dir is dot', function() {
  86. expect(OC.dirname('.')).toEqual('.');
  87. });
  88. it('Returns dot if no root given', function() {
  89. // TODO: fix the source to make it work like PHP's dirname
  90. // expect(OC.dirname('some dir')).toEqual('.');
  91. expect(OC.dirname('some dir')).toEqual('some dir'); // oh no...
  92. });
  93. it('Returns the dir name if file name and root path given', function() {
  94. // TODO: fix the source to make it work like PHP's dirname
  95. // expect(OC.dirname('/some name.txt')).toEqual('/');
  96. expect(OC.dirname('/some name.txt')).toEqual('');
  97. });
  98. it('Returns the dir name if double root path given', function() {
  99. expect(OC.dirname('//some name.txt')).toEqual('/'); // how lucky...
  100. });
  101. it('Returns the dir name if subdir given without root', function() {
  102. expect(OC.dirname('subdir/some name.txt')).toEqual('subdir');
  103. });
  104. it('Returns the dir name if subdir given with root', function() {
  105. expect(OC.dirname('/subdir/some name.txt')).toEqual('/subdir');
  106. });
  107. it('Returns the dir name if subdir given with double root', function() {
  108. // TODO: fix the source to make it work like PHP's dirname
  109. // expect(OC.dirname('//subdir/some name.txt')).toEqual('/subdir');
  110. expect(OC.dirname('//subdir/some name.txt')).toEqual('//subdir'); // oh...
  111. });
  112. it('Returns the dir name if subdir has dot', function() {
  113. expect(OC.dirname('/subdir.dat/some name.txt')).toEqual('/subdir.dat');
  114. });
  115. it('Returns the dir name if file name is dot', function() {
  116. expect(OC.dirname('/subdir/.')).toEqual('/subdir');
  117. });
  118. it('Returns the dir name if no file name given', function() {
  119. expect(OC.dirname('subdir/')).toEqual('subdir');
  120. });
  121. it('Returns the dir name if no file name given with root', function() {
  122. expect(OC.dirname('/subdir/')).toEqual('/subdir');
  123. });
  124. });
  125. describe('escapeHTML', function() {
  126. it('Returns nothing if no string was given', function() {
  127. expect(escapeHTML('')).toEqual('');
  128. });
  129. it('Returns a sanitized string if a string containing HTML is given', function() {
  130. expect(escapeHTML('There needs to be a <script>alert(\"Unit\" + \'test\')</script> for it!')).toEqual('There needs to be a &lt;script&gt;alert(&quot;Unit&quot; + &#039;test&#039;)&lt;/script&gt; for it!');
  131. });
  132. it('Returns the string without modification if no potentially dangerous character is passed.', function() {
  133. expect(escapeHTML('This is a good string without HTML.')).toEqual('This is a good string without HTML.');
  134. });
  135. });
  136. describe('joinPaths', function() {
  137. it('returns empty string with no or empty arguments', function() {
  138. expect(OC.joinPaths()).toEqual('');
  139. expect(OC.joinPaths('')).toEqual('');
  140. expect(OC.joinPaths('', '')).toEqual('');
  141. });
  142. it('returns joined path sections', function() {
  143. expect(OC.joinPaths('abc')).toEqual('abc');
  144. expect(OC.joinPaths('abc', 'def')).toEqual('abc/def');
  145. expect(OC.joinPaths('abc', 'def', 'ghi')).toEqual('abc/def/ghi');
  146. });
  147. it('keeps leading slashes', function() {
  148. expect(OC.joinPaths('/abc')).toEqual('/abc');
  149. expect(OC.joinPaths('/abc', '')).toEqual('/abc');
  150. expect(OC.joinPaths('', '/abc')).toEqual('/abc');
  151. expect(OC.joinPaths('/abc', 'def')).toEqual('/abc/def');
  152. expect(OC.joinPaths('/abc', 'def', 'ghi')).toEqual('/abc/def/ghi');
  153. });
  154. it('keeps trailing slashes', function() {
  155. expect(OC.joinPaths('', 'abc/')).toEqual('abc/');
  156. expect(OC.joinPaths('abc/')).toEqual('abc/');
  157. expect(OC.joinPaths('abc/', '')).toEqual('abc/');
  158. expect(OC.joinPaths('abc', 'def/')).toEqual('abc/def/');
  159. expect(OC.joinPaths('abc', 'def', 'ghi/')).toEqual('abc/def/ghi/');
  160. });
  161. it('splits paths in specified strings and discards extra slashes', function() {
  162. expect(OC.joinPaths('//abc//')).toEqual('/abc/');
  163. expect(OC.joinPaths('//abc//def//')).toEqual('/abc/def/');
  164. expect(OC.joinPaths('//abc//', '//def//')).toEqual('/abc/def/');
  165. expect(OC.joinPaths('//abc//', '//def//', '//ghi//')).toEqual('/abc/def/ghi/');
  166. expect(OC.joinPaths('//abc//def//', '//ghi//jkl/mno/', '//pqr//'))
  167. .toEqual('/abc/def/ghi/jkl/mno/pqr/');
  168. expect(OC.joinPaths('/abc', '/def')).toEqual('/abc/def');
  169. expect(OC.joinPaths('/abc/', '/def')).toEqual('/abc/def');
  170. expect(OC.joinPaths('/abc/', 'def')).toEqual('/abc/def');
  171. });
  172. it('discards empty sections', function() {
  173. expect(OC.joinPaths('abc', '', 'def')).toEqual('abc/def');
  174. });
  175. it('returns root if only slashes', function() {
  176. expect(OC.joinPaths('//')).toEqual('/');
  177. expect(OC.joinPaths('/', '/')).toEqual('/');
  178. expect(OC.joinPaths('/', '//', '/')).toEqual('/');
  179. });
  180. });
  181. describe('filePath', function() {
  182. beforeEach(function() {
  183. OC.webroot = 'http://localhost';
  184. OC.appswebroots['files'] = OC.webroot + '/apps3/files';
  185. });
  186. afterEach(function() {
  187. delete OC.appswebroots['files'];
  188. });
  189. it('Uses a direct link for css and images,' , function() {
  190. expect(OC.filePath('core', 'css', 'style.css')).toEqual('http://localhost/core/css/style.css');
  191. expect(OC.filePath('files', 'css', 'style.css')).toEqual('http://localhost/apps3/files/css/style.css');
  192. expect(OC.filePath('core', 'img', 'image.png')).toEqual('http://localhost/core/img/image.png');
  193. expect(OC.filePath('files', 'img', 'image.png')).toEqual('http://localhost/apps3/files/img/image.png');
  194. });
  195. it('Routes PHP files via index.php,' , function() {
  196. expect(OC.filePath('core', 'ajax', 'test.php')).toEqual('http://localhost/index.php/core/ajax/test.php');
  197. expect(OC.filePath('files', 'ajax', 'test.php')).toEqual('http://localhost/index.php/apps/files/ajax/test.php');
  198. });
  199. });
  200. describe('Link functions', function() {
  201. var TESTAPP = 'testapp';
  202. var TESTAPP_ROOT = OC.webroot + '/appsx/testapp';
  203. beforeEach(function() {
  204. OC.appswebroots[TESTAPP] = TESTAPP_ROOT;
  205. });
  206. afterEach(function() {
  207. // restore original array
  208. delete OC.appswebroots[TESTAPP];
  209. });
  210. it('Generates correct links for core apps', function() {
  211. expect(OC.linkTo('core', 'somefile.php')).toEqual(OC.webroot + '/core/somefile.php');
  212. expect(OC.linkTo('admin', 'somefile.php')).toEqual(OC.webroot + '/admin/somefile.php');
  213. });
  214. it('Generates correct links for regular apps', function() {
  215. expect(OC.linkTo(TESTAPP, 'somefile.php')).toEqual(OC.webroot + '/index.php/apps/' + TESTAPP + '/somefile.php');
  216. });
  217. it('Generates correct remote links', function() {
  218. expect(OC.linkToRemote('webdav')).toEqual(window.location.protocol + '//' + window.location.host + OC.webroot + '/remote.php/webdav');
  219. });
  220. describe('Images', function() {
  221. it('Generates image path with given extension', function() {
  222. var svgSupportStub = sinon.stub(OC.Util, 'hasSVGSupport', function() { return true; });
  223. expect(OC.imagePath('core', 'somefile.jpg')).toEqual(OC.webroot + '/core/img/somefile.jpg');
  224. expect(OC.imagePath(TESTAPP, 'somefile.jpg')).toEqual(TESTAPP_ROOT + '/img/somefile.jpg');
  225. svgSupportStub.restore();
  226. });
  227. it('Generates image path with svg extension when svg support exists', function() {
  228. var svgSupportStub = sinon.stub(OC.Util, 'hasSVGSupport', function() { return true; });
  229. expect(OC.imagePath('core', 'somefile')).toEqual(OC.webroot + '/core/img/somefile.svg');
  230. expect(OC.imagePath(TESTAPP, 'somefile')).toEqual(TESTAPP_ROOT + '/img/somefile.svg');
  231. svgSupportStub.restore();
  232. });
  233. it('Generates image path with png ext when svg support is not available', function() {
  234. var svgSupportStub = sinon.stub(OC.Util, 'hasSVGSupport', function() { return false; });
  235. expect(OC.imagePath('core', 'somefile')).toEqual(OC.webroot + '/core/img/somefile.png');
  236. expect(OC.imagePath(TESTAPP, 'somefile')).toEqual(TESTAPP_ROOT + '/img/somefile.png');
  237. svgSupportStub.restore();
  238. });
  239. });
  240. });
  241. describe('Query string building', function() {
  242. it('Returns empty string when empty params', function() {
  243. expect(OC.buildQueryString()).toEqual('');
  244. expect(OC.buildQueryString({})).toEqual('');
  245. });
  246. it('Encodes regular query strings', function() {
  247. expect(OC.buildQueryString({
  248. a: 'abc',
  249. b: 'def'
  250. })).toEqual('a=abc&b=def');
  251. });
  252. it('Encodes special characters', function() {
  253. expect(OC.buildQueryString({
  254. unicode: '汉字'
  255. })).toEqual('unicode=%E6%B1%89%E5%AD%97');
  256. expect(OC.buildQueryString({
  257. b: 'spaace value',
  258. 'space key': 'normalvalue',
  259. 'slash/this': 'amp&ersand'
  260. })).toEqual('b=spaace%20value&space%20key=normalvalue&slash%2Fthis=amp%26ersand');
  261. });
  262. it('Encodes data types and empty values', function() {
  263. expect(OC.buildQueryString({
  264. 'keywithemptystring': '',
  265. 'keywithnull': null,
  266. 'keywithundefined': null,
  267. something: 'else'
  268. })).toEqual('keywithemptystring=&keywithnull&keywithundefined&something=else');
  269. expect(OC.buildQueryString({
  270. 'booleanfalse': false,
  271. 'booleantrue': true
  272. })).toEqual('booleanfalse=false&booleantrue=true');
  273. expect(OC.buildQueryString({
  274. 'number': 123
  275. })).toEqual('number=123');
  276. });
  277. });
  278. describe('Session heartbeat', function() {
  279. var clock,
  280. oldConfig,
  281. routeStub,
  282. counter;
  283. beforeEach(function() {
  284. clock = sinon.useFakeTimers();
  285. oldConfig = window.oc_config;
  286. routeStub = sinon.stub(OC, 'generateUrl').returns('/heartbeat');
  287. counter = 0;
  288. fakeServer.autoRespond = true;
  289. fakeServer.autoRespondAfter = 0;
  290. fakeServer.respondWith(/\/heartbeat/, function(xhr) {
  291. counter++;
  292. xhr.respond(200, {'Content-Type': 'application/json'}, '{}');
  293. });
  294. });
  295. afterEach(function() {
  296. clock.restore();
  297. /* jshint camelcase: false */
  298. window.oc_config = oldConfig;
  299. routeStub.restore();
  300. $(document).off('ajaxError');
  301. });
  302. it('sends heartbeat half the session lifetime when heartbeat enabled', function() {
  303. /* jshint camelcase: false */
  304. window.oc_config = {
  305. session_keepalive: true,
  306. session_lifetime: 300
  307. };
  308. window.initCore();
  309. expect(routeStub.calledWith('/heartbeat')).toEqual(true);
  310. expect(counter).toEqual(0);
  311. // less than half, still nothing
  312. clock.tick(100 * 1000);
  313. expect(counter).toEqual(0);
  314. // reach past half (160), one call
  315. clock.tick(55 * 1000);
  316. expect(counter).toEqual(1);
  317. // almost there to the next, still one
  318. clock.tick(140 * 1000);
  319. expect(counter).toEqual(1);
  320. // past it, second call
  321. clock.tick(20 * 1000);
  322. expect(counter).toEqual(2);
  323. });
  324. it('does no send heartbeat when heartbeat disabled', function() {
  325. /* jshint camelcase: false */
  326. window.oc_config = {
  327. session_keepalive: false,
  328. session_lifetime: 300
  329. };
  330. window.initCore();
  331. expect(routeStub.notCalled).toEqual(true);
  332. expect(counter).toEqual(0);
  333. clock.tick(1000000);
  334. // still nothing
  335. expect(counter).toEqual(0);
  336. });
  337. it('limits the heartbeat between one minute and one day', function() {
  338. /* jshint camelcase: false */
  339. var setIntervalStub = sinon.stub(window, 'setInterval');
  340. window.oc_config = {
  341. session_keepalive: true,
  342. session_lifetime: 5
  343. };
  344. window.initCore();
  345. expect(setIntervalStub.getCall(0).args[1]).toEqual(60 * 1000);
  346. setIntervalStub.reset();
  347. window.oc_config = {
  348. session_keepalive: true,
  349. session_lifetime: 48 * 3600
  350. };
  351. window.initCore();
  352. expect(setIntervalStub.getCall(0).args[1]).toEqual(24 * 3600 * 1000);
  353. setIntervalStub.restore();
  354. });
  355. });
  356. describe('Parse query string', function() {
  357. it('Parses query string from full URL', function() {
  358. var query = OC.parseQueryString('http://localhost/stuff.php?q=a&b=x');
  359. expect(query).toEqual({q: 'a', b: 'x'});
  360. });
  361. it('Parses query string from query part alone', function() {
  362. var query = OC.parseQueryString('q=a&b=x');
  363. expect(query).toEqual({q: 'a', b: 'x'});
  364. });
  365. it('Returns null hash when empty query', function() {
  366. var query = OC.parseQueryString('');
  367. expect(query).toEqual(null);
  368. });
  369. it('Returns empty hash when empty query with question mark', function() {
  370. var query = OC.parseQueryString('?');
  371. expect(query).toEqual({});
  372. });
  373. it('Decodes regular query strings', function() {
  374. var query = OC.parseQueryString('a=abc&b=def');
  375. expect(query).toEqual({
  376. a: 'abc',
  377. b: 'def'
  378. });
  379. });
  380. it('Ignores empty parts', function() {
  381. var query = OC.parseQueryString('&q=a&&b=x&');
  382. expect(query).toEqual({q: 'a', b: 'x'});
  383. });
  384. it('Ignores lone equal signs', function() {
  385. var query = OC.parseQueryString('&q=a&=&b=x&');
  386. expect(query).toEqual({q: 'a', b: 'x'});
  387. });
  388. it('Includes extra equal signs in value', function() {
  389. var query = OC.parseQueryString('u=a=x&q=a=b');
  390. expect(query).toEqual({u: 'a=x', q: 'a=b'});
  391. });
  392. it('Decodes plus as space', function() {
  393. var query = OC.parseQueryString('space+key=space+value');
  394. expect(query).toEqual({'space key': 'space value'});
  395. });
  396. it('Decodes special characters', function() {
  397. var query = OC.parseQueryString('unicode=%E6%B1%89%E5%AD%97');
  398. expect(query).toEqual({unicode: '汉字'});
  399. query = OC.parseQueryString('b=spaace%20value&space%20key=normalvalue&slash%2Fthis=amp%26ersand');
  400. expect(query).toEqual({
  401. b: 'spaace value',
  402. 'space key': 'normalvalue',
  403. 'slash/this': 'amp&ersand'
  404. });
  405. });
  406. it('Decodes empty values', function() {
  407. var query = OC.parseQueryString('keywithemptystring=&keywithnostring');
  408. expect(query).toEqual({
  409. 'keywithemptystring': '',
  410. 'keywithnostring': null
  411. });
  412. });
  413. it('Does not interpret data types', function() {
  414. var query = OC.parseQueryString('booleanfalse=false&booleantrue=true&number=123');
  415. expect(query).toEqual({
  416. 'booleanfalse': 'false',
  417. 'booleantrue': 'true',
  418. 'number': '123'
  419. });
  420. });
  421. });
  422. describe('Generate Url', function() {
  423. it('returns absolute urls', function() {
  424. expect(OC.generateUrl('heartbeat')).toEqual(OC.webroot + '/index.php/heartbeat');
  425. expect(OC.generateUrl('/heartbeat')).toEqual(OC.webroot + '/index.php/heartbeat');
  426. });
  427. it('substitutes parameters which are escaped by default', function() {
  428. expect(OC.generateUrl('apps/files/download/{file}', {file: '<">ImAnUnescapedString/!'})).toEqual(OC.webroot + '/index.php/apps/files/download/%3C%22%3EImAnUnescapedString%2F!');
  429. });
  430. it('substitutes parameters which can also be unescaped via option flag', function() {
  431. expect(OC.generateUrl('apps/files/download/{file}', {file: 'subfolder/Welcome.txt'}, {escape: false})).toEqual(OC.webroot + '/index.php/apps/files/download/subfolder/Welcome.txt');
  432. });
  433. it('substitutes multiple parameters which are escaped by default', function() {
  434. expect(OC.generateUrl('apps/files/download/{file}/{id}', {file: '<">ImAnUnescapedString/!', id: 5})).toEqual(OC.webroot + '/index.php/apps/files/download/%3C%22%3EImAnUnescapedString%2F!/5');
  435. });
  436. it('substitutes multiple parameters which can also be unescaped via option flag', function() {
  437. expect(OC.generateUrl('apps/files/download/{file}/{id}', {file: 'subfolder/Welcome.txt', id: 5}, {escape: false})).toEqual(OC.webroot + '/index.php/apps/files/download/subfolder/Welcome.txt/5');
  438. });
  439. it('doesnt error out with no params provided', function () {
  440. expect(OC.generateUrl('apps/files/download{file}')).toEqual(OC.webroot + '/index.php/apps/files/download%7Bfile%7D');
  441. });
  442. });
  443. describe('Main menu mobile toggle', function() {
  444. var clock;
  445. var $toggle;
  446. var $navigation;
  447. beforeEach(function() {
  448. clock = sinon.useFakeTimers();
  449. $('#testArea').append('<div id="header">' +
  450. '<a class="menutoggle header-appname-container" href="#">' +
  451. '<h1 class="header-appname"></h1>' +
  452. '<div class="icon-caret"></div>' +
  453. '</a>' +
  454. '</div>' +
  455. '<div id="navigation"></div>');
  456. $toggle = $('#header').find('.menutoggle');
  457. $navigation = $('#navigation');
  458. });
  459. afterEach(function() {
  460. clock.restore();
  461. $(document).off('ajaxError');
  462. });
  463. it('Sets up menu toggle', function() {
  464. window.initCore();
  465. expect($navigation.hasClass('menu')).toEqual(true);
  466. });
  467. it('Clicking menu toggle toggles navigation in', function() {
  468. window.initCore();
  469. $navigation.hide(); // normally done through media query triggered CSS
  470. expect($navigation.is(':visible')).toEqual(false);
  471. $toggle.click();
  472. clock.tick(1 * 1000);
  473. expect($navigation.is(':visible')).toEqual(true);
  474. $toggle.click();
  475. clock.tick(1 * 1000);
  476. expect($navigation.is(':visible')).toEqual(false);
  477. });
  478. });
  479. describe('SVG extension replacement', function() {
  480. var svgSupportStub;
  481. beforeEach(function() {
  482. svgSupportStub = sinon.stub(OC.Util, 'hasSVGSupport');
  483. });
  484. afterEach(function() {
  485. svgSupportStub.restore();
  486. });
  487. it('does not replace svg extension with png when SVG is supported', function() {
  488. svgSupportStub.returns(true);
  489. expect(
  490. OC.Util.replaceSVGIcon('/path/to/myicon.svg?someargs=1')
  491. ).toEqual(
  492. '/path/to/myicon.svg?someargs=1'
  493. );
  494. });
  495. it('replaces svg extension with png when SVG not supported', function() {
  496. svgSupportStub.returns(false);
  497. expect(
  498. OC.Util.replaceSVGIcon('/path/to/myicon.svg?someargs=1')
  499. ).toEqual(
  500. '/path/to/myicon.png?someargs=1'
  501. );
  502. });
  503. });
  504. describe('Util', function() {
  505. describe('humanFileSize', function() {
  506. it('renders file sizes with the correct unit', function() {
  507. var data = [
  508. [0, '0 B'],
  509. ["0", '0 B'],
  510. ["A", 'NaN B'],
  511. [125, '125 B'],
  512. [128000, '125 KB'],
  513. [128000000, '122.1 MB'],
  514. [128000000000, '119.2 GB'],
  515. [128000000000000, '116.4 TB']
  516. ];
  517. for (var i = 0; i < data.length; i++) {
  518. expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]);
  519. }
  520. });
  521. it('renders file sizes with the correct unit for small sizes', function() {
  522. var data = [
  523. [0, '0 KB'],
  524. [125, '< 1 KB'],
  525. [128000, '125 KB'],
  526. [128000000, '122.1 MB'],
  527. [128000000000, '119.2 GB'],
  528. [128000000000000, '116.4 TB']
  529. ];
  530. for (var i = 0; i < data.length; i++) {
  531. expect(OC.Util.humanFileSize(data[i][0], true)).toEqual(data[i][1]);
  532. }
  533. });
  534. });
  535. describe('stripTime', function() {
  536. it('strips time from dates', function() {
  537. expect(OC.Util.stripTime(new Date(2014, 2, 24, 15, 4, 45, 24)))
  538. .toEqual(new Date(2014, 2, 24, 0, 0, 0, 0));
  539. });
  540. });
  541. });
  542. describe('naturalSortCompare', function() {
  543. // cit() will skip tests if running on PhantomJS because it has issues with
  544. // localeCompare(). See https://github.com/ariya/phantomjs/issues/11063
  545. //
  546. // Please make sure to run these tests in Chrome/Firefox manually
  547. // to make sure they run.
  548. var cit = window.isPhantom?xit:it;
  549. // must provide the same results as \OC_Util::naturalSortCompare
  550. it('sorts alphabetically', function() {
  551. var a = [
  552. 'def',
  553. 'xyz',
  554. 'abc'
  555. ];
  556. a.sort(OC.Util.naturalSortCompare);
  557. expect(a).toEqual([
  558. 'abc',
  559. 'def',
  560. 'xyz'
  561. ]);
  562. });
  563. cit('sorts with different casing', function() {
  564. var a = [
  565. 'aaa',
  566. 'bbb',
  567. 'BBB',
  568. 'AAA'
  569. ];
  570. a.sort(OC.Util.naturalSortCompare);
  571. expect(a).toEqual([
  572. 'aaa',
  573. 'AAA',
  574. 'bbb',
  575. 'BBB'
  576. ]);
  577. });
  578. it('sorts with numbers', function() {
  579. var a = [
  580. '124.txt',
  581. 'abc1',
  582. '123.txt',
  583. 'abc',
  584. 'abc2',
  585. 'def (2).txt',
  586. 'ghi 10.txt',
  587. 'abc12',
  588. 'def.txt',
  589. 'def (1).txt',
  590. 'ghi 2.txt',
  591. 'def (10).txt',
  592. 'abc10',
  593. 'def (12).txt',
  594. 'z',
  595. 'ghi.txt',
  596. 'za',
  597. 'ghi 1.txt',
  598. 'ghi 12.txt',
  599. 'zz',
  600. '15.txt',
  601. '15b.txt'
  602. ];
  603. a.sort(OC.Util.naturalSortCompare);
  604. expect(a).toEqual([
  605. '15.txt',
  606. '15b.txt',
  607. '123.txt',
  608. '124.txt',
  609. 'abc',
  610. 'abc1',
  611. 'abc2',
  612. 'abc10',
  613. 'abc12',
  614. 'def.txt',
  615. 'def (1).txt',
  616. 'def (2).txt',
  617. 'def (10).txt',
  618. 'def (12).txt',
  619. 'ghi.txt',
  620. 'ghi 1.txt',
  621. 'ghi 2.txt',
  622. 'ghi 10.txt',
  623. 'ghi 12.txt',
  624. 'z',
  625. 'za',
  626. 'zz'
  627. ]);
  628. });
  629. it('sorts with chinese characters', function() {
  630. var a = [
  631. '十.txt',
  632. '一.txt',
  633. '二.txt',
  634. '十 2.txt',
  635. '三.txt',
  636. '四.txt',
  637. 'abc.txt',
  638. '五.txt',
  639. '七.txt',
  640. '八.txt',
  641. '九.txt',
  642. '六.txt',
  643. '十一.txt',
  644. '波.txt',
  645. '破.txt',
  646. '莫.txt',
  647. '啊.txt',
  648. '123.txt'
  649. ];
  650. a.sort(OC.Util.naturalSortCompare);
  651. expect(a).toEqual([
  652. '123.txt',
  653. 'abc.txt',
  654. '一.txt',
  655. '七.txt',
  656. '三.txt',
  657. '九.txt',
  658. '二.txt',
  659. '五.txt',
  660. '八.txt',
  661. '六.txt',
  662. '十.txt',
  663. '十 2.txt',
  664. '十一.txt',
  665. '啊.txt',
  666. '四.txt',
  667. '波.txt',
  668. '破.txt',
  669. '莫.txt'
  670. ]);
  671. });
  672. cit('sorts with umlauts', function() {
  673. var a = [
  674. 'öh.txt',
  675. 'Äh.txt',
  676. 'oh.txt',
  677. 'Üh 2.txt',
  678. 'Üh.txt',
  679. 'ah.txt',
  680. 'Öh.txt',
  681. 'uh.txt',
  682. 'üh.txt',
  683. 'äh.txt'
  684. ];
  685. a.sort(OC.Util.naturalSortCompare);
  686. expect(a).toEqual([
  687. 'ah.txt',
  688. 'äh.txt',
  689. 'Äh.txt',
  690. 'oh.txt',
  691. 'öh.txt',
  692. 'Öh.txt',
  693. 'uh.txt',
  694. 'üh.txt',
  695. 'Üh.txt',
  696. 'Üh 2.txt'
  697. ]);
  698. });
  699. });
  700. describe('Plugins', function() {
  701. var plugin;
  702. beforeEach(function() {
  703. plugin = {
  704. name: 'Some name',
  705. attach: function(obj) {
  706. obj.attached = true;
  707. },
  708. detach: function(obj) {
  709. obj.attached = false;
  710. }
  711. };
  712. OC.Plugins.register('OC.Test.SomeName', plugin);
  713. });
  714. it('attach plugin to object', function() {
  715. var obj = {something: true};
  716. OC.Plugins.attach('OC.Test.SomeName', obj);
  717. expect(obj.attached).toEqual(true);
  718. OC.Plugins.detach('OC.Test.SomeName', obj);
  719. expect(obj.attached).toEqual(false);
  720. });
  721. it('only call handler for target name', function() {
  722. var obj = {something: true};
  723. OC.Plugins.attach('OC.Test.SomeOtherName', obj);
  724. expect(obj.attached).not.toBeDefined();
  725. OC.Plugins.detach('OC.Test.SomeOtherName', obj);
  726. expect(obj.attached).not.toBeDefined();
  727. });
  728. });
  729. describe('Notifications', function() {
  730. var showSpy;
  731. var showHtmlSpy;
  732. var hideSpy;
  733. var clock;
  734. beforeEach(function() {
  735. clock = sinon.useFakeTimers();
  736. showSpy = sinon.spy(OC.Notification, 'show');
  737. showHtmlSpy = sinon.spy(OC.Notification, 'showHtml');
  738. hideSpy = sinon.spy(OC.Notification, 'hide');
  739. $('#testArea').append('<div id="notification"></div>');
  740. });
  741. afterEach(function() {
  742. showSpy.restore();
  743. showHtmlSpy.restore();
  744. hideSpy.restore();
  745. // jump past animations
  746. clock.tick(10000);
  747. clock.restore();
  748. });
  749. describe('showTemporary', function() {
  750. it('shows a plain text notification with default timeout', function() {
  751. var $row = OC.Notification.showTemporary('My notification test');
  752. expect(showSpy.calledOnce).toEqual(true);
  753. expect(showSpy.firstCall.args[0]).toEqual('My notification test');
  754. expect(showSpy.firstCall.args[1]).toEqual({isHTML: false, timeout: 7});
  755. expect($row).toBeDefined();
  756. expect($row.text()).toEqual('My notification test');
  757. });
  758. it('shows a HTML notification with default timeout', function() {
  759. var $row = OC.Notification.showTemporary('<a>My notification test</a>', { isHTML: true });
  760. expect(showSpy.notCalled).toEqual(true);
  761. expect(showHtmlSpy.calledOnce).toEqual(true);
  762. expect(showHtmlSpy.firstCall.args[0]).toEqual('<a>My notification test</a>');
  763. expect(showHtmlSpy.firstCall.args[1]).toEqual({isHTML: true, timeout: 7});
  764. expect($row).toBeDefined();
  765. expect($row.text()).toEqual('My notification test');
  766. });
  767. it('hides itself after 7 seconds', function() {
  768. var $row = OC.Notification.showTemporary('');
  769. // travel in time +7000 milliseconds
  770. clock.tick(7000);
  771. expect(hideSpy.calledOnce).toEqual(true);
  772. expect(hideSpy.firstCall.args[0]).toEqual($row);
  773. });
  774. });
  775. describe('show', function() {
  776. it('hides itself after a given time', function() {
  777. OC.Notification.show('', { timeout: 10 });
  778. // travel in time +9 seconds
  779. clock.tick(9000);
  780. expect(hideSpy.notCalled).toEqual(true);
  781. // travel in time +1 seconds
  782. clock.tick(1000);
  783. expect(hideSpy.calledOnce).toEqual(true);
  784. });
  785. it('does not hide itself after a given time if a timeout of 0 is defined', function() {
  786. OC.Notification.show('', { timeout: 0 });
  787. // travel in time +1000 seconds
  788. clock.tick(1000000);
  789. expect(hideSpy.notCalled).toEqual(true);
  790. });
  791. it('does not hide itself if no timeout given to show', function() {
  792. OC.Notification.show('');
  793. // travel in time +1000 seconds
  794. clock.tick(1000000);
  795. expect(hideSpy.notCalled).toEqual(true);
  796. });
  797. });
  798. it('cumulates several notifications', function() {
  799. var $row1 = OC.Notification.showTemporary('One');
  800. var $row2 = OC.Notification.showTemporary('Two', {timeout: 2});
  801. var $row3 = OC.Notification.showTemporary('Three');
  802. var $el = $('#notification');
  803. var $rows = $el.find('.row');
  804. expect($rows.length).toEqual(3);
  805. expect($rows.eq(0).is($row1)).toEqual(true);
  806. expect($rows.eq(1).is($row2)).toEqual(true);
  807. expect($rows.eq(2).is($row3)).toEqual(true);
  808. clock.tick(3000);
  809. $rows = $el.find('.row');
  810. expect($rows.length).toEqual(2);
  811. expect($rows.eq(0).is($row1)).toEqual(true);
  812. expect($rows.eq(1).is($row3)).toEqual(true);
  813. });
  814. it('shows close button for error types', function() {
  815. var $row = OC.Notification.showTemporary('One');
  816. var $rowError = OC.Notification.showTemporary('Two', {type: 'error'});
  817. expect($row.find('.close').length).toEqual(0);
  818. expect($rowError.find('.close').length).toEqual(1);
  819. // after clicking, row is gone
  820. $rowError.find('.close').click();
  821. var $rows = $('#notification').find('.row');
  822. expect($rows.length).toEqual(1);
  823. expect($rows.eq(0).is($row)).toEqual(true);
  824. });
  825. it('fades out the last notification but not the other ones', function() {
  826. var fadeOutStub = sinon.stub($.fn, 'fadeOut');
  827. var $row1 = OC.Notification.show('One', {type: 'error'});
  828. var $row2 = OC.Notification.show('Two', {type: 'error'});
  829. OC.Notification.showTemporary('Three', {timeout: 2});
  830. var $el = $('#notification');
  831. var $rows = $el.find('.row');
  832. expect($rows.length).toEqual(3);
  833. clock.tick(3000);
  834. $rows = $el.find('.row');
  835. expect($rows.length).toEqual(2);
  836. $row1.find('.close').click();
  837. clock.tick(1000);
  838. expect(fadeOutStub.notCalled).toEqual(true);
  839. $row2.find('.close').click();
  840. clock.tick(1000);
  841. expect(fadeOutStub.calledOnce).toEqual(true);
  842. expect($el.is(':empty')).toEqual(false);
  843. fadeOutStub.yield();
  844. expect($el.is(':empty')).toEqual(true);
  845. fadeOutStub.restore();
  846. });
  847. it('hides the first notification when calling hide without arguments', function() {
  848. var $row1 = OC.Notification.show('One');
  849. var $row2 = OC.Notification.show('Two');
  850. var $el = $('#notification');
  851. var $rows = $el.find('.row');
  852. expect($rows.length).toEqual(2);
  853. OC.Notification.hide();
  854. $rows = $el.find('.row');
  855. expect($rows.length).toEqual(1);
  856. expect($rows.eq(0).is($row2)).toEqual(true);
  857. });
  858. it('hides the given notification when calling hide with argument', function() {
  859. var $row1 = OC.Notification.show('One');
  860. var $row2 = OC.Notification.show('Two');
  861. var $el = $('#notification');
  862. var $rows = $el.find('.row');
  863. expect($rows.length).toEqual(2);
  864. OC.Notification.hide($row2);
  865. $rows = $el.find('.row');
  866. expect($rows.length).toEqual(1);
  867. expect($rows.eq(0).is($row1)).toEqual(true);
  868. });
  869. });
  870. describe('global ajax errors', function() {
  871. var reloadStub, ajaxErrorStub;
  872. beforeEach(function() {
  873. reloadStub = sinon.stub(OC, 'reload');
  874. // unstub the error processing method
  875. ajaxErrorStub = OC._processAjaxError;
  876. ajaxErrorStub.restore();
  877. window.initCore();
  878. });
  879. afterEach(function() {
  880. reloadStub.restore();
  881. $(document).off('ajaxError');
  882. });
  883. it('reloads current page in case of auth error', function () {
  884. var dataProvider = [
  885. [200, false],
  886. [400, false],
  887. [401, true],
  888. [302, true],
  889. [307, true]
  890. ];
  891. for (var i = 0; i < dataProvider.length; i++) {
  892. var xhr = { status: dataProvider[i][0] };
  893. var expectedCall = dataProvider[i][1];
  894. reloadStub.reset();
  895. $(document).trigger(new $.Event('ajaxError'), xhr);
  896. if (expectedCall) {
  897. expect(reloadStub.calledOnce).toEqual(true);
  898. } else {
  899. expect(reloadStub.notCalled).toEqual(true);
  900. }
  901. }
  902. });
  903. })
  904. });