You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

coreSpec.js 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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('basename', function() {
  38. it('Returns the nothing if no file name given', function() {
  39. expect(OC.basename('')).toEqual('');
  40. });
  41. it('Returns the nothing if dir is root', function() {
  42. expect(OC.basename('/')).toEqual('');
  43. });
  44. it('Returns the same name if no path given', function() {
  45. expect(OC.basename('some name.txt')).toEqual('some name.txt');
  46. });
  47. it('Returns the base name if root path given', function() {
  48. expect(OC.basename('/some name.txt')).toEqual('some name.txt');
  49. });
  50. it('Returns the base name if double root path given', function() {
  51. expect(OC.basename('//some name.txt')).toEqual('some name.txt');
  52. });
  53. it('Returns the base name if subdir given without root', function() {
  54. expect(OC.basename('subdir/some name.txt')).toEqual('some name.txt');
  55. });
  56. it('Returns the base name if subdir given with root', function() {
  57. expect(OC.basename('/subdir/some name.txt')).toEqual('some name.txt');
  58. });
  59. it('Returns the base name if subdir given with double root', function() {
  60. expect(OC.basename('//subdir/some name.txt')).toEqual('some name.txt');
  61. });
  62. it('Returns the base name if subdir has dot', function() {
  63. expect(OC.basename('/subdir.dat/some name.txt')).toEqual('some name.txt');
  64. });
  65. it('Returns dot if file name is dot', function() {
  66. expect(OC.basename('/subdir/.')).toEqual('.');
  67. });
  68. // TODO: fix the source to make it work like PHP's basename
  69. it('Returns the dir itself if no file name given', function() {
  70. // TODO: fix the source to make it work like PHP's dirname
  71. // expect(OC.basename('subdir/')).toEqual('subdir');
  72. expect(OC.basename('subdir/')).toEqual('');
  73. });
  74. it('Returns the dir itself if no file name given with root', function() {
  75. // TODO: fix the source to make it work like PHP's dirname
  76. // expect(OC.basename('/subdir/')).toEqual('subdir');
  77. expect(OC.basename('/subdir/')).toEqual('');
  78. });
  79. });
  80. describe('dirname', function() {
  81. it('Returns the nothing if no file name given', function() {
  82. expect(OC.dirname('')).toEqual('');
  83. });
  84. it('Returns the root if dir is root', function() {
  85. // TODO: fix the source to make it work like PHP's dirname
  86. // expect(OC.dirname('/')).toEqual('/');
  87. expect(OC.dirname('/')).toEqual('');
  88. });
  89. it('Returns the root if dir is double root', function() {
  90. // TODO: fix the source to make it work like PHP's dirname
  91. // expect(OC.dirname('//')).toEqual('/');
  92. expect(OC.dirname('//')).toEqual('/'); // oh no...
  93. });
  94. it('Returns dot if dir is dot', function() {
  95. expect(OC.dirname('.')).toEqual('.');
  96. });
  97. it('Returns dot if no root given', function() {
  98. // TODO: fix the source to make it work like PHP's dirname
  99. // expect(OC.dirname('some dir')).toEqual('.');
  100. expect(OC.dirname('some dir')).toEqual('some dir'); // oh no...
  101. });
  102. it('Returns the dir name if file name and root path given', function() {
  103. // TODO: fix the source to make it work like PHP's dirname
  104. // expect(OC.dirname('/some name.txt')).toEqual('/');
  105. expect(OC.dirname('/some name.txt')).toEqual('');
  106. });
  107. it('Returns the dir name if double root path given', function() {
  108. expect(OC.dirname('//some name.txt')).toEqual('/'); // how lucky...
  109. });
  110. it('Returns the dir name if subdir given without root', function() {
  111. expect(OC.dirname('subdir/some name.txt')).toEqual('subdir');
  112. });
  113. it('Returns the dir name if subdir given with root', function() {
  114. expect(OC.dirname('/subdir/some name.txt')).toEqual('/subdir');
  115. });
  116. it('Returns the dir name if subdir given with double root', function() {
  117. // TODO: fix the source to make it work like PHP's dirname
  118. // expect(OC.dirname('//subdir/some name.txt')).toEqual('/subdir');
  119. expect(OC.dirname('//subdir/some name.txt')).toEqual('//subdir'); // oh...
  120. });
  121. it('Returns the dir name if subdir has dot', function() {
  122. expect(OC.dirname('/subdir.dat/some name.txt')).toEqual('/subdir.dat');
  123. });
  124. it('Returns the dir name if file name is dot', function() {
  125. expect(OC.dirname('/subdir/.')).toEqual('/subdir');
  126. });
  127. it('Returns the dir name if no file name given', function() {
  128. expect(OC.dirname('subdir/')).toEqual('subdir');
  129. });
  130. it('Returns the dir name if no file name given with root', function() {
  131. expect(OC.dirname('/subdir/')).toEqual('/subdir');
  132. });
  133. });
  134. describe('joinPaths', function() {
  135. it('returns empty string with no or empty arguments', function() {
  136. expect(OC.joinPaths()).toEqual('');
  137. expect(OC.joinPaths('')).toEqual('');
  138. expect(OC.joinPaths('', '')).toEqual('');
  139. });
  140. it('returns joined path sections', function() {
  141. expect(OC.joinPaths('abc')).toEqual('abc');
  142. expect(OC.joinPaths('abc', 'def')).toEqual('abc/def');
  143. expect(OC.joinPaths('abc', 'def', 'ghi')).toEqual('abc/def/ghi');
  144. });
  145. it('keeps leading slashes', function() {
  146. expect(OC.joinPaths('/abc')).toEqual('/abc');
  147. expect(OC.joinPaths('/abc', '')).toEqual('/abc');
  148. expect(OC.joinPaths('', '/abc')).toEqual('/abc');
  149. expect(OC.joinPaths('/abc', 'def')).toEqual('/abc/def');
  150. expect(OC.joinPaths('/abc', 'def', 'ghi')).toEqual('/abc/def/ghi');
  151. });
  152. it('keeps trailing slashes', function() {
  153. expect(OC.joinPaths('', 'abc/')).toEqual('abc/');
  154. expect(OC.joinPaths('abc/')).toEqual('abc/');
  155. expect(OC.joinPaths('abc/', '')).toEqual('abc/');
  156. expect(OC.joinPaths('abc', 'def/')).toEqual('abc/def/');
  157. expect(OC.joinPaths('abc', 'def', 'ghi/')).toEqual('abc/def/ghi/');
  158. });
  159. it('splits paths in specified strings and discards extra slashes', function() {
  160. expect(OC.joinPaths('//abc//')).toEqual('/abc/');
  161. expect(OC.joinPaths('//abc//def//')).toEqual('/abc/def/');
  162. expect(OC.joinPaths('//abc//', '//def//')).toEqual('/abc/def/');
  163. expect(OC.joinPaths('//abc//', '//def//', '//ghi//')).toEqual('/abc/def/ghi/');
  164. expect(OC.joinPaths('//abc//def//', '//ghi//jkl/mno/', '//pqr//'))
  165. .toEqual('/abc/def/ghi/jkl/mno/pqr/');
  166. expect(OC.joinPaths('/abc', '/def')).toEqual('/abc/def');
  167. expect(OC.joinPaths('/abc/', '/def')).toEqual('/abc/def');
  168. expect(OC.joinPaths('/abc/', 'def')).toEqual('/abc/def');
  169. });
  170. it('discards empty sections', function() {
  171. expect(OC.joinPaths('abc', '', 'def')).toEqual('abc/def');
  172. });
  173. it('returns root if only slashes', function() {
  174. expect(OC.joinPaths('//')).toEqual('/');
  175. expect(OC.joinPaths('/', '/')).toEqual('/');
  176. expect(OC.joinPaths('/', '//', '/')).toEqual('/');
  177. });
  178. });
  179. describe('isSamePath', function() {
  180. it('recognizes empty paths are equal', function() {
  181. expect(OC.isSamePath('', '')).toEqual(true);
  182. expect(OC.isSamePath('/', '')).toEqual(true);
  183. expect(OC.isSamePath('//', '')).toEqual(true);
  184. expect(OC.isSamePath('/', '/')).toEqual(true);
  185. expect(OC.isSamePath('/', '//')).toEqual(true);
  186. });
  187. it('recognizes path with single sections as equal regardless of extra slashes', function() {
  188. expect(OC.isSamePath('abc', 'abc')).toEqual(true);
  189. expect(OC.isSamePath('/abc', 'abc')).toEqual(true);
  190. expect(OC.isSamePath('//abc', 'abc')).toEqual(true);
  191. expect(OC.isSamePath('abc', '/abc')).toEqual(true);
  192. expect(OC.isSamePath('abc/', 'abc')).toEqual(true);
  193. expect(OC.isSamePath('abc/', 'abc/')).toEqual(true);
  194. expect(OC.isSamePath('/abc/', 'abc/')).toEqual(true);
  195. expect(OC.isSamePath('/abc/', '/abc/')).toEqual(true);
  196. expect(OC.isSamePath('//abc/', '/abc/')).toEqual(true);
  197. expect(OC.isSamePath('//abc//', '/abc/')).toEqual(true);
  198. expect(OC.isSamePath('abc', 'def')).toEqual(false);
  199. expect(OC.isSamePath('/abc', 'def')).toEqual(false);
  200. expect(OC.isSamePath('//abc', 'def')).toEqual(false);
  201. expect(OC.isSamePath('abc', '/def')).toEqual(false);
  202. expect(OC.isSamePath('abc/', 'def')).toEqual(false);
  203. expect(OC.isSamePath('abc/', 'def/')).toEqual(false);
  204. expect(OC.isSamePath('/abc/', 'def/')).toEqual(false);
  205. expect(OC.isSamePath('/abc/', '/def/')).toEqual(false);
  206. expect(OC.isSamePath('//abc/', '/def/')).toEqual(false);
  207. expect(OC.isSamePath('//abc//', '/def/')).toEqual(false);
  208. });
  209. it('recognizes path with multiple sections as equal regardless of extra slashes', function() {
  210. expect(OC.isSamePath('abc/def', 'abc/def')).toEqual(true);
  211. expect(OC.isSamePath('/abc/def', 'abc/def')).toEqual(true);
  212. expect(OC.isSamePath('abc/def', '/abc/def')).toEqual(true);
  213. expect(OC.isSamePath('abc/def/', '/abc/def/')).toEqual(true);
  214. expect(OC.isSamePath('/abc/def/', '/abc/def/')).toEqual(true);
  215. expect(OC.isSamePath('/abc/def/', 'abc/def/')).toEqual(true);
  216. expect(OC.isSamePath('//abc/def/', 'abc/def/')).toEqual(true);
  217. expect(OC.isSamePath('//abc/def//', 'abc/def/')).toEqual(true);
  218. expect(OC.isSamePath('abc/def', 'abc/ghi')).toEqual(false);
  219. expect(OC.isSamePath('/abc/def', 'abc/ghi')).toEqual(false);
  220. expect(OC.isSamePath('abc/def', '/abc/ghi')).toEqual(false);
  221. expect(OC.isSamePath('abc/def/', '/abc/ghi/')).toEqual(false);
  222. expect(OC.isSamePath('/abc/def/', '/abc/ghi/')).toEqual(false);
  223. expect(OC.isSamePath('/abc/def/', 'abc/ghi/')).toEqual(false);
  224. expect(OC.isSamePath('//abc/def/', 'abc/ghi/')).toEqual(false);
  225. expect(OC.isSamePath('//abc/def//', 'abc/ghi/')).toEqual(false);
  226. });
  227. it('recognizes path entries with dot', function() {
  228. expect(OC.isSamePath('.', '')).toEqual(true);
  229. expect(OC.isSamePath('.', '.')).toEqual(true);
  230. expect(OC.isSamePath('.', '/')).toEqual(true);
  231. expect(OC.isSamePath('/.', '/')).toEqual(true);
  232. expect(OC.isSamePath('/./', '/')).toEqual(true);
  233. expect(OC.isSamePath('/./', '/.')).toEqual(true);
  234. expect(OC.isSamePath('/./', '/./')).toEqual(true);
  235. expect(OC.isSamePath('/./', '/./')).toEqual(true);
  236. expect(OC.isSamePath('a/./b', 'a/b')).toEqual(true);
  237. expect(OC.isSamePath('a/b/.', 'a/b')).toEqual(true);
  238. expect(OC.isSamePath('./a/b', 'a/b')).toEqual(true);
  239. });
  240. });
  241. describe('filePath', function() {
  242. beforeEach(function() {
  243. OC.webroot = 'http://localhost';
  244. OC.appswebroots.files = OC.getRootPath() + '/apps3/files';
  245. });
  246. afterEach(function() {
  247. delete OC.appswebroots.files;
  248. });
  249. it('Uses a direct link for css and images,' , function() {
  250. expect(OC.filePath('core', 'css', 'style.css')).toEqual('http://localhost/core/css/style.css');
  251. expect(OC.filePath('files', 'css', 'style.css')).toEqual('http://localhost/apps3/files/css/style.css');
  252. expect(OC.filePath('core', 'img', 'image.png')).toEqual('http://localhost/core/img/image.png');
  253. expect(OC.filePath('files', 'img', 'image.png')).toEqual('http://localhost/apps3/files/img/image.png');
  254. });
  255. it('Routes PHP files via index.php,' , function() {
  256. expect(OC.filePath('core', 'ajax', 'test.php')).toEqual('http://localhost/index.php/core/ajax/test.php');
  257. expect(OC.filePath('files', 'ajax', 'test.php')).toEqual('http://localhost/index.php/apps/files/ajax/test.php');
  258. });
  259. });
  260. describe('getCanonicalLocale', function() {
  261. var oldLocale;
  262. beforeEach(function() {
  263. oldLocale = $('html').data('locale')
  264. });
  265. afterEach(function() {
  266. $('html').data('locale', oldLocale)
  267. });
  268. it("Returns primary locales as is", function() {
  269. $('html').data('locale', 'de')
  270. expect(OC.getCanonicalLocale()).toEqual('de');
  271. $('html').data('locale', 'zu')
  272. expect(OC.getCanonicalLocale()).toEqual('zu');
  273. });
  274. it("Returns extended locales with hyphens", function() {
  275. $('html').data('locale', 'az_Cyrl_AZ')
  276. expect(OC.getCanonicalLocale()).toEqual('az-Cyrl-AZ');
  277. $('html').data('locale', 'de_DE')
  278. expect(OC.getCanonicalLocale()).toEqual('de-DE');
  279. });
  280. });
  281. describe('Link functions', function() {
  282. var TESTAPP = 'testapp';
  283. var TESTAPP_ROOT = OC.getRootPath() + '/appsx/testapp';
  284. beforeEach(function() {
  285. OC.appswebroots[TESTAPP] = TESTAPP_ROOT;
  286. });
  287. afterEach(function() {
  288. // restore original array
  289. delete OC.appswebroots[TESTAPP];
  290. });
  291. it('Generates correct links for core apps', function() {
  292. expect(OC.linkTo('core', 'somefile.php')).toEqual(OC.getRootPath() + '/core/somefile.php');
  293. expect(OC.linkTo('admin', 'somefile.php')).toEqual(OC.getRootPath() + '/admin/somefile.php');
  294. });
  295. it('Generates correct links for regular apps', function() {
  296. expect(OC.linkTo(TESTAPP, 'somefile.php')).toEqual(OC.getRootPath() + '/index.php/apps/' + TESTAPP + '/somefile.php');
  297. });
  298. it('Generates correct remote links', function() {
  299. expect(OC.linkToRemote('webdav')).toEqual(window.location.protocol + '//' + window.location.host + OC.getRootPath() + '/remote.php/webdav');
  300. });
  301. describe('Images', function() {
  302. it('Generates image path with given extension', function() {
  303. expect(OC.imagePath('core', 'somefile.jpg')).toEqual(OC.getRootPath() + '/core/img/somefile.jpg');
  304. expect(OC.imagePath(TESTAPP, 'somefile.jpg')).toEqual(TESTAPP_ROOT + '/img/somefile.jpg');
  305. });
  306. it('Generates image path with svg extension', function() {
  307. expect(OC.imagePath('core', 'somefile')).toEqual(OC.getRootPath() + '/core/img/somefile.svg');
  308. expect(OC.imagePath(TESTAPP, 'somefile')).toEqual(TESTAPP_ROOT + '/img/somefile.svg');
  309. });
  310. });
  311. });
  312. describe('Query string building', function() {
  313. it('Returns empty string when empty params', function() {
  314. expect(OC.buildQueryString()).toEqual('');
  315. expect(OC.buildQueryString({})).toEqual('');
  316. });
  317. it('Encodes regular query strings', function() {
  318. expect(OC.buildQueryString({
  319. a: 'abc',
  320. b: 'def'
  321. })).toEqual('a=abc&b=def');
  322. });
  323. it('Encodes special characters', function() {
  324. expect(OC.buildQueryString({
  325. unicode: '汉字'
  326. })).toEqual('unicode=%E6%B1%89%E5%AD%97');
  327. expect(OC.buildQueryString({
  328. b: 'spaace value',
  329. 'space key': 'normalvalue',
  330. 'slash/this': 'amp&ersand'
  331. })).toEqual('b=spaace%20value&space%20key=normalvalue&slash%2Fthis=amp%26ersand');
  332. });
  333. it('Encodes data types and empty values', function() {
  334. expect(OC.buildQueryString({
  335. 'keywithemptystring': '',
  336. 'keywithnull': null,
  337. 'keywithundefined': null,
  338. something: 'else'
  339. })).toEqual('keywithemptystring=&keywithnull&keywithundefined&something=else');
  340. expect(OC.buildQueryString({
  341. 'booleanfalse': false,
  342. 'booleantrue': true
  343. })).toEqual('booleanfalse=false&booleantrue=true');
  344. expect(OC.buildQueryString({
  345. 'number': 123
  346. })).toEqual('number=123');
  347. });
  348. });
  349. describe('Session heartbeat', function() {
  350. var clock,
  351. oldConfig,
  352. counter;
  353. beforeEach(function() {
  354. clock = sinon.useFakeTimers();
  355. oldConfig = OC.config;
  356. counter = 0;
  357. fakeServer.autoRespond = true;
  358. fakeServer.autoRespondAfter = 0;
  359. fakeServer.respondWith(/\/csrftoken/, function(xhr) {
  360. counter++;
  361. xhr.respond(200, {'Content-Type': 'application/json'}, '{"token": "pgBEsb3MzTb1ZPd2mfDZbQ6/0j3OrXHMEZrghHcOkg8=:3khw5PSa+wKQVo4f26exFD3nplud9ECjJ8/Y5zk5/k4="}');
  362. });
  363. $(document).off('ajaxComplete'); // ignore previously registered heartbeats
  364. });
  365. afterEach(function() {
  366. clock.restore();
  367. /* jshint camelcase: false */
  368. OC.config = oldConfig;
  369. $(document).off('ajaxError');
  370. $(document).off('ajaxComplete');
  371. });
  372. it('sends heartbeat half the session lifetime when heartbeat enabled', function() {
  373. /* jshint camelcase: false */
  374. OC.config = {
  375. session_keepalive: true,
  376. session_lifetime: 300
  377. };
  378. window.initCore();
  379. expect(counter).toEqual(0);
  380. // less than half, still nothing
  381. clock.tick(100 * 1000);
  382. expect(counter).toEqual(0);
  383. // reach past half (160), one call
  384. clock.tick(55 * 1000);
  385. expect(counter).toEqual(1);
  386. // almost there to the next, still one
  387. clock.tick(140 * 1000);
  388. expect(counter).toEqual(1);
  389. // past it, second call
  390. clock.tick(20 * 1000);
  391. expect(counter).toEqual(2);
  392. });
  393. it('does not send heartbeat when heartbeat disabled', function() {
  394. /* jshint camelcase: false */
  395. OC.config = {
  396. session_keepalive: false,
  397. session_lifetime: 300
  398. };
  399. window.initCore();
  400. expect(counter).toEqual(0);
  401. clock.tick(1000000);
  402. // still nothing
  403. expect(counter).toEqual(0);
  404. });
  405. it('limits the heartbeat between one minute and one day', function() {
  406. /* jshint camelcase: false */
  407. var setIntervalStub = sinon.stub(window, 'setInterval');
  408. OC.config = {
  409. session_keepalive: true,
  410. session_lifetime: 5
  411. };
  412. window.initCore();
  413. expect(setIntervalStub.getCall(0).args[1]).toEqual(60 * 1000);
  414. setIntervalStub.reset();
  415. OC.config = {
  416. session_keepalive: true,
  417. session_lifetime: 48 * 3600
  418. };
  419. window.initCore();
  420. expect(setIntervalStub.getCall(0).args[1]).toEqual(24 * 3600 * 1000);
  421. setIntervalStub.restore();
  422. });
  423. });
  424. describe('Parse query string', function() {
  425. it('Parses query string from full URL', function() {
  426. var query = OC.parseQueryString('http://localhost/stuff.php?q=a&b=x');
  427. expect(query).toEqual({q: 'a', b: 'x'});
  428. });
  429. it('Parses query string from query part alone', function() {
  430. var query = OC.parseQueryString('q=a&b=x');
  431. expect(query).toEqual({q: 'a', b: 'x'});
  432. });
  433. it('Returns null hash when empty query', function() {
  434. var query = OC.parseQueryString('');
  435. expect(query).toEqual(null);
  436. });
  437. it('Returns empty hash when empty query with question mark', function() {
  438. var query = OC.parseQueryString('?');
  439. expect(query).toEqual({});
  440. });
  441. it('Decodes regular query strings', function() {
  442. var query = OC.parseQueryString('a=abc&b=def');
  443. expect(query).toEqual({
  444. a: 'abc',
  445. b: 'def'
  446. });
  447. });
  448. it('Ignores empty parts', function() {
  449. var query = OC.parseQueryString('&q=a&&b=x&');
  450. expect(query).toEqual({q: 'a', b: 'x'});
  451. });
  452. it('Ignores lone equal signs', function() {
  453. var query = OC.parseQueryString('&q=a&=&b=x&');
  454. expect(query).toEqual({q: 'a', b: 'x'});
  455. });
  456. it('Includes extra equal signs in value', function() {
  457. var query = OC.parseQueryString('u=a=x&q=a=b');
  458. expect(query).toEqual({u: 'a=x', q: 'a=b'});
  459. });
  460. it('Decodes plus as space', function() {
  461. var query = OC.parseQueryString('space+key=space+value');
  462. expect(query).toEqual({'space key': 'space value'});
  463. });
  464. it('Decodes special characters', function() {
  465. var query = OC.parseQueryString('unicode=%E6%B1%89%E5%AD%97');
  466. expect(query).toEqual({unicode: '汉字'});
  467. query = OC.parseQueryString('b=spaace%20value&space%20key=normalvalue&slash%2Fthis=amp%26ersand');
  468. expect(query).toEqual({
  469. b: 'spaace value',
  470. 'space key': 'normalvalue',
  471. 'slash/this': 'amp&ersand'
  472. });
  473. });
  474. it('Decodes empty values', function() {
  475. var query = OC.parseQueryString('keywithemptystring=&keywithnostring');
  476. expect(query).toEqual({
  477. 'keywithemptystring': '',
  478. 'keywithnostring': null
  479. });
  480. });
  481. it('Does not interpret data types', function() {
  482. var query = OC.parseQueryString('booleanfalse=false&booleantrue=true&number=123');
  483. expect(query).toEqual({
  484. 'booleanfalse': 'false',
  485. 'booleantrue': 'true',
  486. 'number': '123'
  487. });
  488. });
  489. });
  490. describe('Generate Url', function() {
  491. it('returns absolute urls', function() {
  492. expect(OC.generateUrl('csrftoken')).toEqual(OC.getRootPath() + '/index.php/csrftoken');
  493. expect(OC.generateUrl('/csrftoken')).toEqual(OC.getRootPath() + '/index.php/csrftoken');
  494. });
  495. it('substitutes parameters which are escaped by default', function() {
  496. expect(OC.generateUrl('apps/files/download/{file}', {file: '<">ImAnUnescapedString/!'})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/%3C%22%3EImAnUnescapedString%2F!');
  497. });
  498. it('substitutes parameters which can also be unescaped via option flag', function() {
  499. expect(OC.generateUrl('apps/files/download/{file}', {file: 'subfolder/Welcome.txt'}, {escape: false})).toEqual(OC.getRootPath() + '/index.php/apps/files/download/subfolder/Welcome.txt');
  500. });
  501. it('substitutes multiple parameters which are escaped by default', function() {
  502. 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');
  503. });
  504. it('substitutes multiple parameters which can also be unescaped via option flag', function() {
  505. 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');
  506. });
  507. it('doesnt error out with no params provided', function () {
  508. expect(OC.generateUrl('apps/files/download{file}')).toEqual(OC.getRootPath() + '/index.php/apps/files/download%7Bfile%7D');
  509. });
  510. });
  511. describe('Main menu mobile toggle', function() {
  512. var clock;
  513. var $toggle;
  514. var $navigation;
  515. beforeEach(function() {
  516. jQuery.fx.off = true;
  517. clock = sinon.useFakeTimers();
  518. $('#testArea').append('<div id="header">' +
  519. '<a class="menutoggle header-appname-container" href="#">' +
  520. '<h1 class="header-appname"></h1>' +
  521. '<div class="icon-caret"></div>' +
  522. '</a>' +
  523. '</div>' +
  524. '<div id="navigation"></div>');
  525. $toggle = $('#header').find('.menutoggle');
  526. $navigation = $('#navigation');
  527. });
  528. afterEach(function() {
  529. jQuery.fx.off = false;
  530. clock.restore();
  531. $(document).off('ajaxError');
  532. });
  533. it('Sets up menu toggle', function() {
  534. window.initCore();
  535. expect($navigation.hasClass('menu')).toEqual(true);
  536. });
  537. it('Clicking menu toggle toggles navigation in', function() {
  538. window.initCore();
  539. // fore show more apps icon since otherwise it would be hidden since no icons are available
  540. clock.tick(1 * 1000);
  541. $('#more-apps').show();
  542. expect($navigation.is(':visible')).toEqual(false);
  543. $toggle.click();
  544. clock.tick(1 * 1000);
  545. expect($navigation.is(':visible')).toEqual(true);
  546. $toggle.click();
  547. clock.tick(1 * 1000);
  548. expect($navigation.is(':visible')).toEqual(false);
  549. });
  550. });
  551. describe('Util', function() {
  552. var locale;
  553. var localeStub;
  554. beforeEach(function() {
  555. locale = OC.getCanonicalLocale();
  556. localeStub = sinon.stub(OC, 'getCanonicalLocale');
  557. localeStub.returns(locale);
  558. });
  559. afterEach(function() {
  560. localeStub.restore();
  561. });
  562. describe('humanFileSize', function() {
  563. // cit() will skip tests if toLocaleString() is not supported.
  564. // See https://github.com/ariya/phantomjs/issues/12581
  565. //
  566. // Please run these tests in Chrome/Firefox manually.
  567. var cit = 4.2.toLocaleString("de") !== "4,2" ? xit : it;
  568. it('renders file sizes with the correct unit', function() {
  569. var data = [
  570. [0, '0 B'],
  571. ["0", '0 B'],
  572. ["A", 'NaN B'],
  573. [125, '125 B'],
  574. [128000, '125 KB'],
  575. [128000000, '122.1 MB'],
  576. [128000000000, '119.2 GB'],
  577. [128000000000000, '116.4 TB']
  578. ];
  579. for (var i = 0; i < data.length; i++) {
  580. expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]);
  581. }
  582. });
  583. it('renders file sizes with the correct unit for small sizes', function() {
  584. var data = [
  585. [0, '0 KB'],
  586. [125, '< 1 KB'],
  587. [128000, '125 KB'],
  588. [128000000, '122.1 MB'],
  589. [128000000000, '119.2 GB'],
  590. [128000000000000, '116.4 TB']
  591. ];
  592. for (var i = 0; i < data.length; i++) {
  593. expect(OC.Util.humanFileSize(data[i][0], true)).toEqual(data[i][1]);
  594. }
  595. });
  596. cit('renders file sizes with the correct locale', function() {
  597. localeStub.returns("de");
  598. var data = [
  599. [0, '0 B'],
  600. ["0", '0 B'],
  601. ["A", 'NaN B'],
  602. [125, '125 B'],
  603. [128000, '125 KB'],
  604. [128000000, '122,1 MB'],
  605. [128000000000, '119,2 GB'],
  606. [128000000000000, '116,4 TB']
  607. ];
  608. for (var i = 0; i < data.length; i++) {
  609. expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]);
  610. }
  611. });
  612. });
  613. describe('computerFileSize', function() {
  614. it('correctly parses file sizes from a human readable formated string', function() {
  615. var data = [
  616. ['125', 125],
  617. ['125.25', 125],
  618. ['125.25B', 125],
  619. ['125.25 B', 125],
  620. ['0 B', 0],
  621. ['99999999999999999999999999999999999999999999 B', 99999999999999999999999999999999999999999999],
  622. ['0 MB', 0],
  623. ['0 kB', 0],
  624. ['0kB', 0],
  625. ['125 B', 125],
  626. ['125b', 125],
  627. ['125 KB', 128000],
  628. ['125kb', 128000],
  629. ['122.1 MB', 128031130],
  630. ['122.1mb', 128031130],
  631. ['119.2 GB', 127990025421],
  632. ['119.2gb', 127990025421],
  633. ['116.4 TB', 127983153473126],
  634. ['116.4tb', 127983153473126],
  635. ['8776656778888777655.4tb', 9.650036181387265e+30],
  636. [1234, null],
  637. [-1234, null],
  638. ['-1234 B', null],
  639. ['B', null],
  640. ['40/0', null],
  641. ['40,30 kb', null],
  642. [' 122.1 MB ', 128031130],
  643. ['122.1 MB ', 128031130],
  644. [' 122.1 MB ', 128031130],
  645. [' 122.1 MB ', 128031130],
  646. ['122.1 MB ', 128031130],
  647. [' 125', 125],
  648. [' 125 ', 125],
  649. ];
  650. for (var i = 0; i < data.length; i++) {
  651. expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]);
  652. }
  653. });
  654. it('returns null if the parameter is not a string', function() {
  655. expect(OC.Util.computerFileSize(NaN)).toEqual(null);
  656. expect(OC.Util.computerFileSize(125)).toEqual(null);
  657. });
  658. it('returns null if the string is unparsable', function() {
  659. expect(OC.Util.computerFileSize('')).toEqual(null);
  660. expect(OC.Util.computerFileSize('foobar')).toEqual(null);
  661. });
  662. });
  663. describe('stripTime', function() {
  664. it('strips time from dates', function() {
  665. expect(OC.Util.stripTime(new Date(2014, 2, 24, 15, 4, 45, 24)))
  666. .toEqual(new Date(2014, 2, 24, 0, 0, 0, 0));
  667. });
  668. });
  669. });
  670. describe('naturalSortCompare', function() {
  671. // cit() will skip tests if running on PhantomJS because it has issues with
  672. // localeCompare(). See https://github.com/ariya/phantomjs/issues/11063
  673. //
  674. // Please make sure to run these tests in Chrome/Firefox manually
  675. // to make sure they run.
  676. var cit = window.isPhantom?xit:it;
  677. // must provide the same results as \OC_Util::naturalSortCompare
  678. it('sorts alphabetically', function() {
  679. var a = [
  680. 'def',
  681. 'xyz',
  682. 'abc'
  683. ];
  684. a.sort(OC.Util.naturalSortCompare);
  685. expect(a).toEqual([
  686. 'abc',
  687. 'def',
  688. 'xyz'
  689. ]);
  690. });
  691. cit('sorts with different casing', function() {
  692. var a = [
  693. 'aaa',
  694. 'bbb',
  695. 'BBB',
  696. 'AAA'
  697. ];
  698. a.sort(OC.Util.naturalSortCompare);
  699. expect(a).toEqual([
  700. 'aaa',
  701. 'AAA',
  702. 'bbb',
  703. 'BBB'
  704. ]);
  705. });
  706. it('sorts with numbers', function() {
  707. var a = [
  708. '124.txt',
  709. 'abc1',
  710. '123.txt',
  711. 'abc',
  712. 'abc2',
  713. 'def (2).txt',
  714. 'ghi 10.txt',
  715. 'abc12',
  716. 'def.txt',
  717. 'def (1).txt',
  718. 'ghi 2.txt',
  719. 'def (10).txt',
  720. 'abc10',
  721. 'def (12).txt',
  722. 'z',
  723. 'ghi.txt',
  724. 'za',
  725. 'ghi 1.txt',
  726. 'ghi 12.txt',
  727. 'zz',
  728. '15.txt',
  729. '15b.txt'
  730. ];
  731. a.sort(OC.Util.naturalSortCompare);
  732. expect(a).toEqual([
  733. '15.txt',
  734. '15b.txt',
  735. '123.txt',
  736. '124.txt',
  737. 'abc',
  738. 'abc1',
  739. 'abc2',
  740. 'abc10',
  741. 'abc12',
  742. 'def.txt',
  743. 'def (1).txt',
  744. 'def (2).txt',
  745. 'def (10).txt',
  746. 'def (12).txt',
  747. 'ghi.txt',
  748. 'ghi 1.txt',
  749. 'ghi 2.txt',
  750. 'ghi 10.txt',
  751. 'ghi 12.txt',
  752. 'z',
  753. 'za',
  754. 'zz'
  755. ]);
  756. });
  757. it('sorts with chinese characters', function() {
  758. var a = [
  759. '十.txt',
  760. '一.txt',
  761. '二.txt',
  762. '十 2.txt',
  763. '三.txt',
  764. '四.txt',
  765. 'abc.txt',
  766. '五.txt',
  767. '七.txt',
  768. '八.txt',
  769. '九.txt',
  770. '六.txt',
  771. '十一.txt',
  772. '波.txt',
  773. '破.txt',
  774. '莫.txt',
  775. '啊.txt',
  776. '123.txt'
  777. ];
  778. a.sort(OC.Util.naturalSortCompare);
  779. expect(a).toEqual([
  780. '123.txt',
  781. 'abc.txt',
  782. '一.txt',
  783. '七.txt',
  784. '三.txt',
  785. '九.txt',
  786. '二.txt',
  787. '五.txt',
  788. '八.txt',
  789. '六.txt',
  790. '十.txt',
  791. '十 2.txt',
  792. '十一.txt',
  793. '啊.txt',
  794. '四.txt',
  795. '波.txt',
  796. '破.txt',
  797. '莫.txt'
  798. ]);
  799. });
  800. cit('sorts with umlauts', function() {
  801. var a = [
  802. 'öh.txt',
  803. 'Äh.txt',
  804. 'oh.txt',
  805. 'Üh 2.txt',
  806. 'Üh.txt',
  807. 'ah.txt',
  808. 'Öh.txt',
  809. 'uh.txt',
  810. 'üh.txt',
  811. 'äh.txt'
  812. ];
  813. a.sort(OC.Util.naturalSortCompare);
  814. expect(a).toEqual([
  815. 'ah.txt',
  816. 'äh.txt',
  817. 'Äh.txt',
  818. 'oh.txt',
  819. 'öh.txt',
  820. 'Öh.txt',
  821. 'uh.txt',
  822. 'üh.txt',
  823. 'Üh.txt',
  824. 'Üh 2.txt'
  825. ]);
  826. });
  827. });
  828. describe('Plugins', function() {
  829. var plugin;
  830. beforeEach(function() {
  831. plugin = {
  832. name: 'Some name',
  833. attach: function(obj) {
  834. obj.attached = true;
  835. },
  836. detach: function(obj) {
  837. obj.attached = false;
  838. }
  839. };
  840. OC.Plugins.register('OC.Test.SomeName', plugin);
  841. });
  842. it('attach plugin to object', function() {
  843. var obj = {something: true};
  844. OC.Plugins.attach('OC.Test.SomeName', obj);
  845. expect(obj.attached).toEqual(true);
  846. OC.Plugins.detach('OC.Test.SomeName', obj);
  847. expect(obj.attached).toEqual(false);
  848. });
  849. it('only call handler for target name', function() {
  850. var obj = {something: true};
  851. OC.Plugins.attach('OC.Test.SomeOtherName', obj);
  852. expect(obj.attached).not.toBeDefined();
  853. OC.Plugins.detach('OC.Test.SomeOtherName', obj);
  854. expect(obj.attached).not.toBeDefined();
  855. });
  856. });
  857. describe('Notifications', function() {
  858. var showSpy;
  859. var showHtmlSpy;
  860. var clock;
  861. /**
  862. * Returns the HTML or plain text of the given notification row.
  863. *
  864. * This is needed to ignore the close button that is added to the
  865. * notification row after the text.
  866. */
  867. var getNotificationText = function($node) {
  868. return $node.contents()[0].outerHTML ||
  869. $node.contents()[0].nodeValue;
  870. }
  871. beforeEach(function() {
  872. clock = sinon.useFakeTimers();
  873. showSpy = sinon.spy(OCP.Toast, 'message');
  874. $('#testArea').append('<div id="content"></div>');
  875. });
  876. afterEach(function() {
  877. showSpy.restore();
  878. // jump past animations
  879. clock.tick(10000);
  880. clock.restore();
  881. $('#testArea .toastify').remove();
  882. });
  883. describe('showTemporary', function() {
  884. it('shows a plain text notification with default timeout', function() {
  885. OC.Notification.showTemporary('My notification test');
  886. expect(showSpy.calledOnce).toEqual(true);
  887. expect(showSpy.firstCall.args[0]).toEqual('My notification test');
  888. //expect(showSpy.firstCall.args[1]).toEqual({isHTML: false, timeout: 7});
  889. var $row = $('#testArea .toastify');
  890. expect($row.length).toEqual(1);
  891. expect(getNotificationText($row)).toEqual('My notification test');
  892. });
  893. it('shows a HTML notification with default timeout', function() {
  894. OC.Notification.showTemporary('<a>My notification test</a>', { isHTML: true });
  895. expect(showSpy.calledOnce).toEqual(true);
  896. expect(showSpy.firstCall.args[0]).toEqual('<a>My notification test</a>');
  897. expect(showSpy.firstCall.args[1].isHTML).toEqual(true)
  898. var $row = $('#testArea .toastify');
  899. expect($row.length).toEqual(1);
  900. expect(getNotificationText($row)).toEqual('<a>My notification test</a>');
  901. });
  902. it('hides itself after 7 seconds', function() {
  903. OC.Notification.showTemporary('');
  904. var $row = $('#testArea .toastify');
  905. expect($row.length).toEqual(1);
  906. // travel in time +7000 milliseconds
  907. clock.tick(7500);
  908. $row = $('#testArea .toastify');
  909. expect($row.length).toEqual(0);
  910. });
  911. it('hides itself after a given time', function() {
  912. OC.Notification.showTemporary('', {timeout: 10});
  913. var $row = $('#testArea .toastify');
  914. expect($row.length).toEqual(1);
  915. // travel in time +7000 milliseconds
  916. clock.tick(7500);
  917. $row = $('#testArea .toastify');
  918. expect($row.length).toEqual(1);
  919. // travel in time another 4000 milliseconds
  920. clock.tick(4000);
  921. $row = $('#testArea .toastify');
  922. expect($row.length).toEqual(0);
  923. });
  924. });
  925. describe('show', function() {
  926. it('hides itself after a given time', function() {
  927. OC.Notification.show('', {timeout: 10});
  928. var $row = $('#testArea .toastify');
  929. expect($row.length).toEqual(1);
  930. clock.tick(11500);
  931. $row = $('#testArea .toastify');
  932. expect($row.length).toEqual(0);
  933. });
  934. it('does not hide itself if no timeout given to show', function() {
  935. OC.Notification.show('');
  936. var $row = $('#testArea .toastify');
  937. expect($row.length).toEqual(1);
  938. // travel in time +1000 seconds
  939. clock.tick(1000000);
  940. $row = $('#testArea .toastify');
  941. expect($row.length).toEqual(1);
  942. });
  943. });
  944. describe('showHtml', function() {
  945. it('hides itself after a given time', function() {
  946. OC.Notification.showHtml('<p></p>', {timeout: 10});
  947. var $row = $('#testArea .toastify');
  948. expect($row.length).toEqual(1);
  949. clock.tick(11500);
  950. $row = $('#testArea .toastify');
  951. expect($row.length).toEqual(0);
  952. });
  953. it('does not hide itself if no timeout given to show', function() {
  954. OC.Notification.showHtml('<p></p>');
  955. var $row = $('#testArea .toastify');
  956. expect($row.length).toEqual(1);
  957. // travel in time +1000 seconds
  958. clock.tick(1000000);
  959. $row = $('#testArea .toastify');
  960. expect($row.length).toEqual(1);
  961. });
  962. });
  963. describe('hide', function() {
  964. it('hides a temporary notification before its timeout expires', function() {
  965. var hideCallback = sinon.spy();
  966. var notification = OC.Notification.showTemporary('');
  967. var $row = $('#testArea .toastify');
  968. expect($row.length).toEqual(1);
  969. OC.Notification.hide(notification, hideCallback);
  970. // Give time to the hide animation to finish
  971. clock.tick(1000);
  972. $row = $('#testArea .toastify');
  973. expect($row.length).toEqual(0);
  974. expect(hideCallback.calledOnce).toEqual(true);
  975. });
  976. it('hides a notification before its timeout expires', function() {
  977. var hideCallback = sinon.spy();
  978. var notification = OC.Notification.show('', {timeout: 10});
  979. var $row = $('#testArea .toastify');
  980. expect($row.length).toEqual(1);
  981. OC.Notification.hide(notification, hideCallback);
  982. // Give time to the hide animation to finish
  983. clock.tick(1000);
  984. $row = $('#testArea .toastify');
  985. expect($row.length).toEqual(0);
  986. expect(hideCallback.calledOnce).toEqual(true);
  987. });
  988. it('hides a notification without timeout', function() {
  989. var hideCallback = sinon.spy();
  990. var notification = OC.Notification.show('');
  991. var $row = $('#testArea .toastify');
  992. expect($row.length).toEqual(1);
  993. OC.Notification.hide(notification, hideCallback);
  994. // Give time to the hide animation to finish
  995. clock.tick(1000);
  996. $row = $('#testArea .toastify');
  997. expect($row.length).toEqual(0);
  998. expect(hideCallback.calledOnce).toEqual(true);
  999. });
  1000. });
  1001. it('cumulates several notifications', function() {
  1002. var $row1 = OC.Notification.showTemporary('One');
  1003. var $row2 = OC.Notification.showTemporary('Two', {timeout: 2});
  1004. var $row3 = OC.Notification.showTemporary('Three');
  1005. var $el = $('#testArea');
  1006. var $rows = $el.find('.toastify');
  1007. expect($rows.length).toEqual(3);
  1008. expect($rows.eq(0).is($row3)).toEqual(true);
  1009. expect($rows.eq(1).is($row2)).toEqual(true);
  1010. expect($rows.eq(2).is($row1)).toEqual(true);
  1011. clock.tick(3000);
  1012. $rows = $el.find('.toastify');
  1013. expect($rows.length).toEqual(2);
  1014. expect($rows.eq(0).is($row3)).toEqual(true);
  1015. expect($rows.eq(1).is($row1)).toEqual(true);
  1016. });
  1017. it('hides the given notification when calling hide with argument', function() {
  1018. var $row1 = OC.Notification.show('One');
  1019. var $row2 = OC.Notification.show('Two');
  1020. var $el = $('#testArea');
  1021. var $rows = $el.find('.toastify');
  1022. expect($rows.length).toEqual(2);
  1023. OC.Notification.hide($row2);
  1024. clock.tick(3000);
  1025. $rows = $el.find('.toastify');
  1026. expect($rows.length).toEqual(1);
  1027. expect($rows.eq(0).is($row1)).toEqual(true);
  1028. });
  1029. });
  1030. describe('global ajax errors', function() {
  1031. var reloadStub, ajaxErrorStub, clock;
  1032. var notificationStub;
  1033. var waitTimeMs = 6500;
  1034. var oldCurrentUser;
  1035. beforeEach(function() {
  1036. oldCurrentUser = OC.currentUser;
  1037. OC.currentUser = 'dummy';
  1038. clock = sinon.useFakeTimers();
  1039. reloadStub = sinon.stub(OC, 'reload');
  1040. notificationStub = sinon.stub(OC.Notification, 'show');
  1041. // unstub the error processing method
  1042. ajaxErrorStub = OC._processAjaxError;
  1043. ajaxErrorStub.restore();
  1044. window.initCore();
  1045. });
  1046. afterEach(function() {
  1047. OC.currentUser = oldCurrentUser;
  1048. reloadStub.restore();
  1049. notificationStub.restore();
  1050. clock.restore();
  1051. });
  1052. it('reloads current page in case of auth error', function() {
  1053. var dataProvider = [
  1054. [200, false],
  1055. [400, false],
  1056. [0, false],
  1057. [401, true],
  1058. [302, true],
  1059. [303, true],
  1060. [307, true]
  1061. ];
  1062. for (var i = 0; i < dataProvider.length; i++) {
  1063. var xhr = { status: dataProvider[i][0] };
  1064. var expectedCall = dataProvider[i][1];
  1065. reloadStub.reset();
  1066. OC._reloadCalled = false;
  1067. $(document).trigger(new $.Event('ajaxError'), xhr);
  1068. // trigger timers
  1069. clock.tick(waitTimeMs);
  1070. if (expectedCall) {
  1071. expect(reloadStub.calledOnce).toEqual(true);
  1072. } else {
  1073. expect(reloadStub.notCalled).toEqual(true);
  1074. }
  1075. }
  1076. });
  1077. it('reload only called once in case of auth error', function() {
  1078. var xhr = { status: 401 };
  1079. $(document).trigger(new $.Event('ajaxError'), xhr);
  1080. $(document).trigger(new $.Event('ajaxError'), xhr);
  1081. // trigger timers
  1082. clock.tick(waitTimeMs);
  1083. expect(reloadStub.calledOnce).toEqual(true);
  1084. });
  1085. it('does not reload the page if the user was navigating away', function() {
  1086. var xhr = { status: 0 };
  1087. OC._userIsNavigatingAway = true;
  1088. clock.tick(100);
  1089. $(document).trigger(new $.Event('ajaxError'), xhr);
  1090. clock.tick(waitTimeMs);
  1091. expect(reloadStub.notCalled).toEqual(true);
  1092. });
  1093. it('displays notification', function() {
  1094. var xhr = { status: 401 };
  1095. notificationUpdateStub = sinon.stub(OC.Notification, 'showUpdate');
  1096. $(document).trigger(new $.Event('ajaxError'), xhr);
  1097. clock.tick(waitTimeMs);
  1098. expect(notificationUpdateStub.notCalled).toEqual(false);
  1099. });
  1100. it('shows a temporary notification if the connection is lost', function() {
  1101. var xhr = { status: 0 };
  1102. spyOn(OC, '_ajaxConnectionLostHandler');
  1103. $(document).trigger(new $.Event('ajaxError'), xhr);
  1104. clock.tick(101);
  1105. expect(OC._ajaxConnectionLostHandler.calls.count()).toBe(1);
  1106. });
  1107. });
  1108. describe('Snapper', function() {
  1109. var snapConstructorStub;
  1110. var snapperStub;
  1111. var clock;
  1112. beforeEach(function() {
  1113. snapConstructorStub = sinon.stub(window, 'Snap');
  1114. snapperStub = {};
  1115. snapperStub.enable = sinon.stub();
  1116. snapperStub.disable = sinon.stub();
  1117. snapperStub.close = sinon.stub();
  1118. snapConstructorStub.returns(snapperStub);
  1119. clock = sinon.useFakeTimers();
  1120. // _.now could have been set to Date.now before Sinon replaced it
  1121. // with a fake version, so _.now must be stubbed to ensure that the
  1122. // fake Date.now will be called instead of the original one.
  1123. _.now = sinon.stub(_, 'now').callsFake(function() {
  1124. return new Date().getTime();
  1125. });
  1126. $('#testArea').append('<div id="app-navigation">The navigation bar</div><div id="app-content">Content</div>');
  1127. });
  1128. afterEach(function() {
  1129. snapConstructorStub.restore();
  1130. clock.restore();
  1131. _.now.restore();
  1132. // Remove the event handler for the resize event added to the window
  1133. // due to calling window.initCore() when there is an #app-navigation
  1134. // element.
  1135. $(window).off('resize');
  1136. viewport.reset();
  1137. });
  1138. it('is enabled on a narrow screen', function() {
  1139. viewport.set(480);
  1140. window.initCore();
  1141. expect(snapConstructorStub.calledOnce).toBe(true);
  1142. expect(snapperStub.enable.calledOnce).toBe(true);
  1143. expect(snapperStub.disable.called).toBe(false);
  1144. });
  1145. it('is disabled when disallowing the gesture on a narrow screen', function() {
  1146. viewport.set(480);
  1147. window.initCore();
  1148. expect(snapperStub.enable.calledOnce).toBe(true);
  1149. expect(snapperStub.disable.called).toBe(false);
  1150. expect(snapperStub.close.called).toBe(false);
  1151. OC.disallowNavigationBarSlideGesture();
  1152. expect(snapperStub.enable.calledOnce).toBe(true);
  1153. expect(snapperStub.disable.calledOnce).toBe(true);
  1154. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  1155. expect(snapperStub.close.called).toBe(false);
  1156. });
  1157. it('is not disabled again when disallowing the gesture twice on a narrow screen', function() {
  1158. viewport.set(480);
  1159. window.initCore();
  1160. expect(snapperStub.enable.calledOnce).toBe(true);
  1161. expect(snapperStub.disable.called).toBe(false);
  1162. expect(snapperStub.close.called).toBe(false);
  1163. OC.disallowNavigationBarSlideGesture();
  1164. expect(snapperStub.enable.calledOnce).toBe(true);
  1165. expect(snapperStub.disable.calledOnce).toBe(true);
  1166. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  1167. expect(snapperStub.close.called).toBe(false);
  1168. OC.disallowNavigationBarSlideGesture();
  1169. expect(snapperStub.enable.calledOnce).toBe(true);
  1170. expect(snapperStub.disable.calledOnce).toBe(true);
  1171. expect(snapperStub.close.called).toBe(false);
  1172. });
  1173. it('is enabled when allowing the gesture after disallowing it on a narrow screen', function() {
  1174. viewport.set(480);
  1175. window.initCore();
  1176. expect(snapperStub.enable.calledOnce).toBe(true);
  1177. expect(snapperStub.disable.called).toBe(false);
  1178. expect(snapperStub.close.called).toBe(false);
  1179. OC.disallowNavigationBarSlideGesture();
  1180. expect(snapperStub.enable.calledOnce).toBe(true);
  1181. expect(snapperStub.disable.calledOnce).toBe(true);
  1182. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  1183. expect(snapperStub.close.called).toBe(false);
  1184. OC.allowNavigationBarSlideGesture();
  1185. expect(snapperStub.enable.calledTwice).toBe(true);
  1186. expect(snapperStub.disable.calledOnce).toBe(true);
  1187. expect(snapperStub.close.called).toBe(false);
  1188. });
  1189. it('is not enabled again when allowing the gesture twice after disallowing it on a narrow screen', function() {
  1190. viewport.set(480);
  1191. window.initCore();
  1192. expect(snapperStub.enable.calledOnce).toBe(true);
  1193. expect(snapperStub.disable.called).toBe(false);
  1194. expect(snapperStub.close.called).toBe(false);
  1195. OC.disallowNavigationBarSlideGesture();
  1196. expect(snapperStub.enable.calledOnce).toBe(true);
  1197. expect(snapperStub.disable.calledOnce).toBe(true);
  1198. expect(snapperStub.disable.alwaysCalledWithExactly(true)).toBe(true);
  1199. expect(snapperStub.close.called).toBe(false);
  1200. OC.allowNavigationBarSlideGesture();
  1201. expect(snapperStub.enable.calledTwice).toBe(true);
  1202. expect(snapperStub.disable.calledOnce).toBe(true);
  1203. expect(snapperStub.close.called).toBe(false);
  1204. OC.allowNavigationBarSlideGesture();
  1205. expect(snapperStub.enable.calledTwice).toBe(true);
  1206. expect(snapperStub.disable.calledOnce).toBe(true);
  1207. expect(snapperStub.close.called).toBe(false);
  1208. });
  1209. it('is disabled on a wide screen', function() {
  1210. viewport.set(1280);
  1211. window.initCore();
  1212. expect(snapConstructorStub.calledOnce).toBe(true);
  1213. expect(snapperStub.enable.called).toBe(false);
  1214. expect(snapperStub.disable.calledOnce).toBe(true);
  1215. });
  1216. it('is not disabled again when disallowing the gesture on a wide screen', function() {
  1217. viewport.set(1280);
  1218. window.initCore();
  1219. expect(snapperStub.enable.called).toBe(false);
  1220. expect(snapperStub.disable.calledOnce).toBe(true);
  1221. expect(snapperStub.close.calledOnce).toBe(true);
  1222. OC.disallowNavigationBarSlideGesture();
  1223. expect(snapperStub.enable.called).toBe(false);
  1224. expect(snapperStub.disable.calledOnce).toBe(true);
  1225. expect(snapperStub.close.calledOnce).toBe(true);
  1226. });
  1227. it('is not enabled when allowing the gesture after disallowing it on a wide screen', function() {
  1228. viewport.set(1280);
  1229. window.initCore();
  1230. expect(snapperStub.enable.called).toBe(false);
  1231. expect(snapperStub.disable.calledOnce).toBe(true);
  1232. expect(snapperStub.close.calledOnce).toBe(true);
  1233. OC.disallowNavigationBarSlideGesture();
  1234. expect(snapperStub.enable.called).toBe(false);
  1235. expect(snapperStub.disable.calledOnce).toBe(true);
  1236. expect(snapperStub.close.calledOnce).toBe(true);
  1237. OC.allowNavigationBarSlideGesture();
  1238. expect(snapperStub.enable.called).toBe(false);
  1239. expect(snapperStub.disable.calledOnce).toBe(true);
  1240. expect(snapperStub.close.calledOnce).toBe(true);
  1241. });
  1242. it('is enabled when resizing to a narrow screen', function() {
  1243. viewport.set(1280);
  1244. window.initCore();
  1245. expect(snapperStub.enable.called).toBe(false);
  1246. expect(snapperStub.disable.calledOnce).toBe(true);
  1247. viewport.set(480);
  1248. // Setting the viewport width does not automatically trigger a
  1249. // resize.
  1250. $(window).resize();
  1251. // The resize handler is debounced to be executed a few milliseconds
  1252. // after the resize event.
  1253. clock.tick(1000);
  1254. expect(snapperStub.enable.calledOnce).toBe(true);
  1255. expect(snapperStub.disable.calledOnce).toBe(true);
  1256. });
  1257. it('is not enabled when resizing to a narrow screen after disallowing the gesture', function() {
  1258. viewport.set(1280);
  1259. window.initCore();
  1260. expect(snapperStub.enable.called).toBe(false);
  1261. expect(snapperStub.disable.calledOnce).toBe(true);
  1262. OC.disallowNavigationBarSlideGesture();
  1263. expect(snapperStub.enable.called).toBe(false);
  1264. expect(snapperStub.disable.calledOnce).toBe(true);
  1265. viewport.set(480);
  1266. // Setting the viewport width does not automatically trigger a
  1267. // resize.
  1268. $(window).resize();
  1269. // The resize handler is debounced to be executed a few milliseconds
  1270. // after the resize event.
  1271. clock.tick(1000);
  1272. expect(snapperStub.enable.called).toBe(false);
  1273. expect(snapperStub.disable.calledOnce).toBe(true);
  1274. });
  1275. it('is enabled when resizing to a narrow screen after disallowing the gesture and allowing it', function() {
  1276. viewport.set(1280);
  1277. window.initCore();
  1278. expect(snapperStub.enable.called).toBe(false);
  1279. expect(snapperStub.disable.calledOnce).toBe(true);
  1280. OC.disallowNavigationBarSlideGesture();
  1281. expect(snapperStub.enable.called).toBe(false);
  1282. expect(snapperStub.disable.calledOnce).toBe(true);
  1283. OC.allowNavigationBarSlideGesture();
  1284. expect(snapperStub.enable.called).toBe(false);
  1285. expect(snapperStub.disable.calledOnce).toBe(true);
  1286. viewport.set(480);
  1287. // Setting the viewport width does not automatically trigger a
  1288. // resize.
  1289. $(window).resize();
  1290. // The resize handler is debounced to be executed a few milliseconds
  1291. // after the resize event.
  1292. clock.tick(1000);
  1293. expect(snapperStub.enable.calledOnce).toBe(true);
  1294. expect(snapperStub.disable.calledOnce).toBe(true);
  1295. });
  1296. it('is enabled when allowing the gesture after disallowing it and resizing to a narrow screen', function() {
  1297. viewport.set(1280);
  1298. window.initCore();
  1299. expect(snapperStub.enable.called).toBe(false);
  1300. expect(snapperStub.disable.calledOnce).toBe(true);
  1301. OC.disallowNavigationBarSlideGesture();
  1302. expect(snapperStub.enable.called).toBe(false);
  1303. expect(snapperStub.disable.calledOnce).toBe(true);
  1304. viewport.set(480);
  1305. // Setting the viewport width does not automatically trigger a
  1306. // resize.
  1307. $(window).resize();
  1308. // The resize handler is debounced to be executed a few milliseconds
  1309. // after the resize event.
  1310. clock.tick(1000);
  1311. expect(snapperStub.enable.called).toBe(false);
  1312. expect(snapperStub.disable.calledOnce).toBe(true);
  1313. OC.allowNavigationBarSlideGesture();
  1314. expect(snapperStub.enable.calledOnce).toBe(true);
  1315. expect(snapperStub.disable.calledOnce).toBe(true);
  1316. });
  1317. it('is disabled when disallowing the gesture after disallowing it, resizing to a narrow screen and allowing it', function() {
  1318. viewport.set(1280);
  1319. window.initCore();
  1320. expect(snapperStub.enable.called).toBe(false);
  1321. expect(snapperStub.disable.calledOnce).toBe(true);
  1322. OC.disallowNavigationBarSlideGesture();
  1323. expect(snapperStub.enable.called).toBe(false);
  1324. expect(snapperStub.disable.calledOnce).toBe(true);
  1325. viewport.set(480);
  1326. // Setting the viewport width does not automatically trigger a
  1327. // resize.
  1328. $(window).resize();
  1329. // The resize handler is debounced to be executed a few milliseconds
  1330. // after the resize event.
  1331. clock.tick(1000);
  1332. expect(snapperStub.enable.called).toBe(false);
  1333. expect(snapperStub.disable.calledOnce).toBe(true);
  1334. OC.allowNavigationBarSlideGesture();
  1335. expect(snapperStub.enable.calledOnce).toBe(true);
  1336. expect(snapperStub.disable.calledOnce).toBe(true);
  1337. OC.disallowNavigationBarSlideGesture();
  1338. expect(snapperStub.enable.calledOnce).toBe(true);
  1339. expect(snapperStub.disable.calledTwice).toBe(true);
  1340. expect(snapperStub.disable.getCall(1).calledWithExactly(true)).toBe(true);
  1341. });
  1342. it('is disabled when resizing to a wide screen', function() {
  1343. viewport.set(480);
  1344. window.initCore();
  1345. expect(snapperStub.enable.calledOnce).toBe(true);
  1346. expect(snapperStub.disable.called).toBe(false);
  1347. expect(snapperStub.close.called).toBe(false);
  1348. viewport.set(1280);
  1349. // Setting the viewport width does not automatically trigger a
  1350. // resize.
  1351. $(window).resize();
  1352. // The resize handler is debounced to be executed a few milliseconds
  1353. // after the resize event.
  1354. clock.tick(1000);
  1355. expect(snapperStub.enable.calledOnce).toBe(true);
  1356. expect(snapperStub.disable.calledOnce).toBe(true);
  1357. expect(snapperStub.close.calledOnce).toBe(true);
  1358. });
  1359. it('is not disabled again when disallowing the gesture after resizing to a wide screen', function() {
  1360. viewport.set(480);
  1361. window.initCore();
  1362. expect(snapperStub.enable.calledOnce).toBe(true);
  1363. expect(snapperStub.disable.called).toBe(false);
  1364. expect(snapperStub.close.called).toBe(false);
  1365. viewport.set(1280);
  1366. // Setting the viewport width does not automatically trigger a
  1367. // resize.
  1368. $(window).resize();
  1369. // The resize handler is debounced to be executed a few milliseconds
  1370. // after the resize event.
  1371. clock.tick(1000);
  1372. expect(snapperStub.enable.calledOnce).toBe(true);
  1373. expect(snapperStub.disable.calledOnce).toBe(true);
  1374. expect(snapperStub.close.calledOnce).toBe(true);
  1375. OC.disallowNavigationBarSlideGesture();
  1376. expect(snapperStub.enable.calledOnce).toBe(true);
  1377. expect(snapperStub.disable.calledOnce).toBe(true);
  1378. expect(snapperStub.close.calledOnce).toBe(true);
  1379. });
  1380. it('is not enabled when allowing the gesture after disallowing it, resizing to a narrow screen and resizing to a wide screen', function() {
  1381. viewport.set(1280);
  1382. window.initCore();
  1383. expect(snapperStub.enable.called).toBe(false);
  1384. expect(snapperStub.disable.calledOnce).toBe(true);
  1385. expect(snapperStub.close.calledOnce).toBe(true);
  1386. OC.disallowNavigationBarSlideGesture();
  1387. expect(snapperStub.enable.called).toBe(false);
  1388. expect(snapperStub.disable.calledOnce).toBe(true);
  1389. expect(snapperStub.close.calledOnce).toBe(true);
  1390. viewport.set(480);
  1391. // Setting the viewport width does not automatically trigger a
  1392. // resize.
  1393. $(window).resize();
  1394. // The resize handler is debounced to be executed a few milliseconds
  1395. // after the resize event.
  1396. clock.tick(1000);
  1397. expect(snapperStub.enable.called).toBe(false);
  1398. expect(snapperStub.disable.calledOnce).toBe(true);
  1399. expect(snapperStub.close.calledOnce).toBe(true);
  1400. viewport.set(1280);
  1401. $(window).resize();
  1402. clock.tick(1000);
  1403. expect(snapperStub.enable.called).toBe(false);
  1404. expect(snapperStub.disable.calledTwice).toBe(true);
  1405. expect(snapperStub.close.calledTwice).toBe(true);
  1406. OC.allowNavigationBarSlideGesture();
  1407. expect(snapperStub.enable.called).toBe(false);
  1408. expect(snapperStub.disable.calledTwice).toBe(true);
  1409. expect(snapperStub.close.calledTwice).toBe(true);
  1410. });
  1411. });
  1412. describe('Requires password confirmation', function () {
  1413. var stubMomentNow;
  1414. var stubJsPageLoadTime;
  1415. afterEach(function () {
  1416. delete window.nc_pageLoad;
  1417. delete window.nc_lastLogin;
  1418. delete window.backendAllowsPasswordConfirmation;
  1419. stubMomentNow.restore();
  1420. stubJsPageLoadTime.restore();
  1421. });
  1422. it('should not show the password confirmation dialog when server time is earlier than local time', function () {
  1423. // add server variables
  1424. window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 1, 15, 0).getTime() / 1000);
  1425. window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 1, 0, 0).getTime() / 1000);
  1426. window.backendAllowsPasswordConfirmation = true;
  1427. stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime());
  1428. stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 20, 0).getTime());
  1429. expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeFalsy();
  1430. });
  1431. it('should show the password confirmation dialog when server time is earlier than local time', function () {
  1432. // add server variables
  1433. window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 1, 15, 0).getTime() / 1000);
  1434. window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 1, 0, 0).getTime() / 1000);
  1435. window.backendAllowsPasswordConfirmation = true;
  1436. stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime());
  1437. stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 31, 0).getTime());
  1438. expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeTruthy();
  1439. });
  1440. it('should not show the password confirmation dialog when server time is later than local time', function () {
  1441. // add server variables
  1442. window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 23, 15, 0).getTime() / 1000);
  1443. window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 23, 0, 0).getTime() / 1000);
  1444. window.backendAllowsPasswordConfirmation = true;
  1445. stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime());
  1446. stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 20, 0).getTime());
  1447. expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeFalsy();
  1448. });
  1449. it('should show the password confirmation dialog when server time is later than local time', function () {
  1450. // add server variables
  1451. window.nc_pageLoad = parseInt(new Date(2018, 0, 3, 23, 15, 0).getTime() / 1000);
  1452. window.nc_lastLogin = parseInt(new Date(2018, 0, 3, 23, 0, 0).getTime() / 1000);
  1453. window.backendAllowsPasswordConfirmation = true;
  1454. stubJsPageLoadTime = sinon.stub(OC.PasswordConfirmation, 'pageLoadTime').value(new Date(2018, 0, 3, 12, 15, 0).getTime());
  1455. stubMomentNow = sinon.stub(moment, 'now').returns(new Date(2018, 0, 3, 12, 31, 0).getTime());
  1456. expect(OC.PasswordConfirmation.requiresPasswordConfirmation()).toBeTruthy();
  1457. });
  1458. });
  1459. });