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.

fileactionsSpec.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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('OCA.Files.FileActions tests', function() {
  22. var fileList, fileActions, clock;
  23. beforeEach(function() {
  24. clock = sinon.useFakeTimers();
  25. // init horrible parameters
  26. var $body = $('#testArea');
  27. $body.append('<input type="hidden" id="dir" value="/subdir"></input>');
  28. $body.append('<input type="hidden" id="permissions" value="31"></input>');
  29. $body.append('<table id="filestable"><tbody id="fileList"></tbody></table>');
  30. // dummy files table
  31. fileActions = new OCA.Files.FileActions();
  32. fileActions.registerAction({
  33. name: 'Testdropdown',
  34. displayName: 'Testdropdowndisplay',
  35. mime: 'all',
  36. permissions: OC.PERMISSION_READ,
  37. icon: function () {
  38. return OC.imagePath('core', 'actions/download');
  39. }
  40. });
  41. fileActions.registerAction({
  42. name: 'Testinline',
  43. displayName: 'Testinlinedisplay',
  44. type: OCA.Files.FileActions.TYPE_INLINE,
  45. mime: 'all',
  46. permissions: OC.PERMISSION_READ
  47. });
  48. fileActions.registerAction({
  49. name: 'Testdefault',
  50. displayName: 'Testdefaultdisplay',
  51. mime: 'all',
  52. permissions: OC.PERMISSION_READ
  53. });
  54. fileActions.setDefault('all', 'Testdefault');
  55. fileList = new OCA.Files.FileList($body, {
  56. fileActions: fileActions
  57. });
  58. });
  59. afterEach(function() {
  60. fileActions = null;
  61. fileList.destroy();
  62. fileList = undefined;
  63. clock.restore();
  64. $('#dir, #permissions, #filestable').remove();
  65. });
  66. it('calling clear() clears file actions', function() {
  67. fileActions.clear();
  68. expect(fileActions.actions).toEqual({});
  69. expect(fileActions.defaults).toEqual({});
  70. expect(fileActions.icons).toEqual({});
  71. expect(fileActions.currentFile).toBe(null);
  72. });
  73. describe('displaying actions', function() {
  74. var $tr;
  75. beforeEach(function() {
  76. var fileData = {
  77. id: 18,
  78. type: 'file',
  79. name: 'testName.txt',
  80. mimetype: 'text/plain',
  81. size: '1234',
  82. etag: 'a01234c',
  83. mtime: '123456',
  84. permissions: OC.PERMISSION_READ | OC.PERMISSION_UPDATE
  85. };
  86. // note: FileActions.display() is called implicitly
  87. $tr = fileList.add(fileData);
  88. });
  89. it('renders inline file actions', function() {
  90. // actions defined after call
  91. expect($tr.find('.action.action-testinline').length).toEqual(1);
  92. expect($tr.find('.action.action-testinline').attr('data-action')).toEqual('Testinline');
  93. });
  94. it('does not render dropdown actions', function() {
  95. expect($tr.find('.action.action-testdropdown').length).toEqual(0);
  96. });
  97. it('does not render default action', function() {
  98. expect($tr.find('.action.action-testdefault').length).toEqual(0);
  99. });
  100. it('replaces file actions when displayed twice', function() {
  101. fileActions.display($tr.find('td.filename'), true, fileList);
  102. fileActions.display($tr.find('td.filename'), true, fileList);
  103. expect($tr.find('.action.action-testinline').length).toEqual(1);
  104. });
  105. it('renders actions menu trigger', function() {
  106. expect($tr.find('.action.action-menu').length).toEqual(1);
  107. expect($tr.find('.action.action-menu').attr('data-action')).toEqual('menu');
  108. });
  109. it('only renders actions relevant to the mime type', function() {
  110. fileActions.registerAction({
  111. name: 'Match',
  112. displayName: 'MatchDisplay',
  113. type: OCA.Files.FileActions.TYPE_INLINE,
  114. mime: 'text/plain',
  115. permissions: OC.PERMISSION_READ
  116. });
  117. fileActions.registerAction({
  118. name: 'Nomatch',
  119. displayName: 'NoMatchDisplay',
  120. type: OCA.Files.FileActions.TYPE_INLINE,
  121. mime: 'application/octet-stream',
  122. permissions: OC.PERMISSION_READ
  123. });
  124. fileActions.display($tr.find('td.filename'), true, fileList);
  125. expect($tr.find('.action.action-match').length).toEqual(1);
  126. expect($tr.find('.action.action-nomatch').length).toEqual(0);
  127. });
  128. it('only renders actions relevant to the permissions', function() {
  129. fileActions.registerAction({
  130. name: 'Match',
  131. displayName: 'MatchDisplay',
  132. type: OCA.Files.FileActions.TYPE_INLINE,
  133. mime: 'text/plain',
  134. permissions: OC.PERMISSION_UPDATE
  135. });
  136. fileActions.registerAction({
  137. name: 'Nomatch',
  138. displayName: 'NoMatchDisplay',
  139. type: OCA.Files.FileActions.TYPE_INLINE,
  140. mime: 'text/plain',
  141. permissions: OC.PERMISSION_DELETE
  142. });
  143. fileActions.display($tr.find('td.filename'), true, fileList);
  144. expect($tr.find('.action.action-match').length).toEqual(1);
  145. expect($tr.find('.action.action-nomatch').length).toEqual(0);
  146. });
  147. it('display inline icon with image path', function() {
  148. fileActions.registerAction({
  149. name: 'Icon',
  150. displayName: 'IconDisplay',
  151. type: OCA.Files.FileActions.TYPE_INLINE,
  152. mime: 'all',
  153. icon: OC.imagePath('core', 'actions/icon'),
  154. permissions: OC.PERMISSION_READ
  155. });
  156. fileActions.registerAction({
  157. name: 'NoIcon',
  158. displayName: 'NoIconDisplay',
  159. type: OCA.Files.FileActions.TYPE_INLINE,
  160. mime: 'all',
  161. permissions: OC.PERMISSION_READ
  162. });
  163. fileActions.display($tr.find('td.filename'), true, fileList);
  164. expect($tr.find('.action.action-icon').length).toEqual(1);
  165. expect($tr.find('.action.action-icon').find('img').length).toEqual(1);
  166. expect($tr.find('.action.action-icon').find('img').eq(0).attr('src')).toEqual(OC.imagePath('core', 'actions/icon'));
  167. expect($tr.find('.action.action-noicon').length).toEqual(1);
  168. expect($tr.find('.action.action-noicon').find('img').length).toEqual(0);
  169. });
  170. it('display alt text on inline icon with image path', function() {
  171. fileActions.registerAction({
  172. name: 'IconAltText',
  173. displayName: 'IconAltTextDisplay',
  174. type: OCA.Files.FileActions.TYPE_INLINE,
  175. mime: 'all',
  176. icon: OC.imagePath('core', 'actions/iconAltText'),
  177. altText: 'alt icon text',
  178. permissions: OC.PERMISSION_READ
  179. });
  180. fileActions.registerAction({
  181. name: 'IconNoAltText',
  182. displayName: 'IconNoAltTextDisplay',
  183. type: OCA.Files.FileActions.TYPE_INLINE,
  184. mime: 'all',
  185. icon: OC.imagePath('core', 'actions/iconNoAltText'),
  186. permissions: OC.PERMISSION_READ
  187. });
  188. fileActions.display($tr.find('td.filename'), true, fileList);
  189. expect($tr.find('.action.action-iconalttext').length).toEqual(1);
  190. expect($tr.find('.action.action-iconalttext').find('img').length).toEqual(1);
  191. expect($tr.find('.action.action-iconalttext').find('img').eq(0).attr('alt')).toEqual('alt icon text');
  192. expect($tr.find('.action.action-iconnoalttext').length).toEqual(1);
  193. expect($tr.find('.action.action-iconnoalttext').find('img').length).toEqual(1);
  194. expect($tr.find('.action.action-iconnoalttext').find('img').eq(0).attr('alt')).toEqual('');
  195. });
  196. it('display inline icon with iconClass', function() {
  197. fileActions.registerAction({
  198. name: 'Icon',
  199. displayName: 'IconDisplay',
  200. type: OCA.Files.FileActions.TYPE_INLINE,
  201. mime: 'all',
  202. iconClass: 'icon-test',
  203. permissions: OC.PERMISSION_READ
  204. });
  205. fileActions.registerAction({
  206. name: 'NoIcon',
  207. displayName: 'NoIconDisplay',
  208. type: OCA.Files.FileActions.TYPE_INLINE,
  209. mime: 'all',
  210. permissions: OC.PERMISSION_READ
  211. });
  212. fileActions.display($tr.find('td.filename'), true, fileList);
  213. expect($tr.find('.action.action-icon').length).toEqual(1);
  214. expect($tr.find('.action.action-icon').find('.icon').length).toEqual(1);
  215. expect($tr.find('.action.action-icon').find('.icon').hasClass('icon-test')).toEqual(true);
  216. expect($tr.find('.action.action-noicon').length).toEqual(1);
  217. expect($tr.find('.action.action-noicon').find('.icon').length).toEqual(0);
  218. });
  219. it('display alt text on inline icon with iconClass when no display name exists', function() {
  220. fileActions.registerAction({
  221. name: 'IconAltText',
  222. displayName: '',
  223. type: OCA.Files.FileActions.TYPE_INLINE,
  224. mime: 'all',
  225. iconClass: 'icon-alttext',
  226. altText: 'alt icon text',
  227. permissions: OC.PERMISSION_READ
  228. });
  229. fileActions.registerAction({
  230. name: 'IconNoAltText',
  231. displayName: 'IconNoAltTextDisplay',
  232. type: OCA.Files.FileActions.TYPE_INLINE,
  233. mime: 'all',
  234. altText: 'useless alt text',
  235. iconClass: 'icon-noalttext',
  236. permissions: OC.PERMISSION_READ
  237. });
  238. fileActions.display($tr.find('td.filename'), true, fileList);
  239. expect($tr.find('.action.action-iconalttext').length).toEqual(1);
  240. expect($tr.find('.action.action-iconalttext').find('.icon').length).toEqual(1);
  241. expect($tr.find('.action.action-iconalttext').find('.hidden-visually').text()).toEqual('alt icon text');
  242. expect($tr.find('.action.action-iconnoalttext').length).toEqual(1);
  243. expect($tr.find('.action.action-iconnoalttext').find('.icon').length).toEqual(1);
  244. expect($tr.find('.action.action-iconnoalttext').find('.hidden-visually').length).toEqual(0);
  245. });
  246. });
  247. describe('action handler', function() {
  248. var actionStub, $tr, clock;
  249. beforeEach(function() {
  250. clock = sinon.useFakeTimers();
  251. var fileData = {
  252. id: 18,
  253. type: 'file',
  254. name: 'testName.txt',
  255. mimetype: 'text/plain',
  256. size: '1234',
  257. etag: 'a01234c',
  258. mtime: '123456'
  259. };
  260. actionStub = sinon.stub();
  261. fileActions.registerAction({
  262. name: 'Test',
  263. type: OCA.Files.FileActions.TYPE_INLINE,
  264. mime: 'all',
  265. icon: OC.imagePath('core', 'actions/test'),
  266. permissions: OC.PERMISSION_READ,
  267. actionHandler: actionStub
  268. });
  269. $tr = fileList.add(fileData);
  270. });
  271. afterEach(function() {
  272. OC.hideMenus();
  273. // jump past animations
  274. clock.tick(1000);
  275. clock.restore();
  276. });
  277. it('passes context to action handler', function() {
  278. $tr.find('.action-test').click();
  279. expect(actionStub.calledOnce).toEqual(true);
  280. expect(actionStub.getCall(0).args[0]).toEqual('testName.txt');
  281. var context = actionStub.getCall(0).args[1];
  282. expect(context.$file.is($tr)).toEqual(true);
  283. expect(context.fileList).toBeDefined();
  284. expect(context.fileActions).toBeDefined();
  285. expect(context.dir).toEqual('/subdir');
  286. expect(context.fileInfoModel.get('name')).toEqual('testName.txt');
  287. // when data-path is defined
  288. actionStub.reset();
  289. $tr.attr('data-path', '/somepath');
  290. $tr.find('.action-test').click();
  291. context = actionStub.getCall(0).args[1];
  292. expect(context.dir).toEqual('/somepath');
  293. });
  294. it('also triggers action handler when calling triggerAction()', function() {
  295. var model = new OCA.Files.FileInfoModel({
  296. id: 1,
  297. name: 'Test.txt',
  298. path: '/subdir',
  299. mime: 'text/plain',
  300. permissions: 31
  301. });
  302. fileActions.triggerAction('Test', model, fileList);
  303. expect(actionStub.calledOnce).toEqual(true);
  304. expect(actionStub.getCall(0).args[0]).toEqual('Test.txt');
  305. expect(actionStub.getCall(0).args[1].fileList).toEqual(fileList);
  306. expect(actionStub.getCall(0).args[1].fileActions).toEqual(fileActions);
  307. expect(actionStub.getCall(0).args[1].fileInfoModel).toEqual(model);
  308. });
  309. describe('actions menu', function() {
  310. it('shows actions menu inside row when clicking the menu trigger', function() {
  311. expect($tr.find('td.filename .fileActionsMenu').length).toEqual(0);
  312. $tr.find('.action-menu').click();
  313. expect($tr.find('td.filename .fileActionsMenu').length).toEqual(1);
  314. });
  315. it('shows highlight on current row', function() {
  316. $tr.find('.action-menu').click();
  317. expect($tr.hasClass('mouseOver')).toEqual(true);
  318. });
  319. it('cleans up after hiding', function() {
  320. var slideUpStub = sinon.stub($.fn, 'slideUp');
  321. $tr.find('.action-menu').click();
  322. expect($tr.find('.fileActionsMenu').length).toEqual(1);
  323. OC.hideMenus();
  324. // sliding animation
  325. expect(slideUpStub.calledOnce).toEqual(true);
  326. slideUpStub.getCall(0).args[1]();
  327. expect($tr.hasClass('mouseOver')).toEqual(false);
  328. expect($tr.find('.fileActionsMenu').length).toEqual(0);
  329. });
  330. });
  331. });
  332. describe('custom rendering', function() {
  333. var $tr;
  334. beforeEach(function() {
  335. var fileData = {
  336. id: 18,
  337. type: 'file',
  338. name: 'testName.txt',
  339. mimetype: 'text/plain',
  340. size: '1234',
  341. etag: 'a01234c',
  342. mtime: '123456'
  343. };
  344. $tr = fileList.add(fileData);
  345. });
  346. it('regular function', function() {
  347. var actionStub = sinon.stub();
  348. fileActions.registerAction({
  349. name: 'Test',
  350. displayName: '',
  351. mime: 'all',
  352. type: OCA.Files.FileActions.TYPE_INLINE,
  353. permissions: OC.PERMISSION_READ,
  354. render: function(actionSpec, isDefault, context) {
  355. expect(actionSpec.name).toEqual('Test');
  356. expect(actionSpec.displayName).toEqual('');
  357. expect(actionSpec.permissions).toEqual(OC.PERMISSION_READ);
  358. expect(actionSpec.mime).toEqual('all');
  359. expect(isDefault).toEqual(false);
  360. expect(context.fileList).toEqual(fileList);
  361. expect(context.$file[0]).toEqual($tr[0]);
  362. var $customEl = $('<a class="action action-test" href="#"><span>blabli</span><span>blabla</span></a>');
  363. $tr.find('td:first').append($customEl);
  364. return $customEl;
  365. },
  366. actionHandler: actionStub
  367. });
  368. fileActions.display($tr.find('td.filename'), true, fileList);
  369. var $actionEl = $tr.find('td:first .action-test');
  370. expect($actionEl.length).toEqual(1);
  371. expect($actionEl.hasClass('action')).toEqual(true);
  372. $actionEl.click();
  373. expect(actionStub.calledOnce).toEqual(true);
  374. expect(actionStub.getCall(0).args[0]).toEqual('testName.txt');
  375. });
  376. });
  377. describe('merging', function() {
  378. var $tr;
  379. beforeEach(function() {
  380. var fileData = {
  381. id: 18,
  382. type: 'file',
  383. name: 'testName.txt',
  384. path: '/anotherpath/there',
  385. mimetype: 'text/plain',
  386. size: '1234',
  387. etag: 'a01234c',
  388. mtime: '123456'
  389. };
  390. $tr = fileList.add(fileData);
  391. });
  392. afterEach(function() {
  393. $tr = null;
  394. });
  395. it('copies all actions to target file actions', function() {
  396. var actions1 = new OCA.Files.FileActions();
  397. var actions2 = new OCA.Files.FileActions();
  398. var actionStub1 = sinon.stub();
  399. var actionStub2 = sinon.stub();
  400. actions1.registerAction({
  401. name: 'Test',
  402. type: OCA.Files.FileActions.TYPE_INLINE,
  403. mime: 'all',
  404. permissions: OC.PERMISSION_READ,
  405. icon: OC.imagePath('core', 'actions/test'),
  406. actionHandler: actionStub1
  407. });
  408. actions2.registerAction({
  409. name: 'Test2',
  410. type: OCA.Files.FileActions.TYPE_INLINE,
  411. mime: 'all',
  412. permissions: OC.PERMISSION_READ,
  413. icon: OC.imagePath('core', 'actions/test'),
  414. actionHandler: actionStub2
  415. });
  416. actions2.merge(actions1);
  417. actions2.display($tr.find('td.filename'), true, fileList);
  418. expect($tr.find('.action-test').length).toEqual(1);
  419. expect($tr.find('.action-test2').length).toEqual(1);
  420. $tr.find('.action-test').click();
  421. expect(actionStub1.calledOnce).toEqual(true);
  422. expect(actionStub2.notCalled).toEqual(true);
  423. actionStub1.reset();
  424. $tr.find('.action-test2').click();
  425. expect(actionStub1.notCalled).toEqual(true);
  426. expect(actionStub2.calledOnce).toEqual(true);
  427. });
  428. it('overrides existing actions on merge', function() {
  429. var actions1 = new OCA.Files.FileActions();
  430. var actions2 = new OCA.Files.FileActions();
  431. var actionStub1 = sinon.stub();
  432. var actionStub2 = sinon.stub();
  433. actions1.registerAction({
  434. name: 'Test',
  435. type: OCA.Files.FileActions.TYPE_INLINE,
  436. mime: 'all',
  437. permissions: OC.PERMISSION_READ,
  438. icon: OC.imagePath('core', 'actions/test'),
  439. actionHandler: actionStub1
  440. });
  441. actions2.registerAction({
  442. name: 'Test', // override
  443. mime: 'all',
  444. type: OCA.Files.FileActions.TYPE_INLINE,
  445. permissions: OC.PERMISSION_READ,
  446. icon: OC.imagePath('core', 'actions/test'),
  447. actionHandler: actionStub2
  448. });
  449. actions1.merge(actions2);
  450. actions1.display($tr.find('td.filename'), true, fileList);
  451. expect($tr.find('.action-test').length).toEqual(1);
  452. $tr.find('.action-test').click();
  453. expect(actionStub1.notCalled).toEqual(true);
  454. expect(actionStub2.calledOnce).toEqual(true);
  455. });
  456. it('overrides existing action when calling register after merge', function() {
  457. var actions1 = new OCA.Files.FileActions();
  458. var actions2 = new OCA.Files.FileActions();
  459. var actionStub1 = sinon.stub();
  460. var actionStub2 = sinon.stub();
  461. actions1.registerAction({
  462. mime: 'all',
  463. name: 'Test',
  464. type: OCA.Files.FileActions.TYPE_INLINE,
  465. permissions: OC.PERMISSION_READ,
  466. icon: OC.imagePath('core', 'actions/test'),
  467. actionHandler: actionStub1
  468. });
  469. actions1.merge(actions2);
  470. // late override
  471. actions1.registerAction({
  472. mime: 'all',
  473. name: 'Test', // override
  474. type: OCA.Files.FileActions.TYPE_INLINE,
  475. permissions: OC.PERMISSION_READ,
  476. icon: OC.imagePath('core', 'actions/test'),
  477. actionHandler: actionStub2
  478. });
  479. actions1.display($tr.find('td.filename'), true, fileList);
  480. expect($tr.find('.action-test').length).toEqual(1);
  481. $tr.find('.action-test').click();
  482. expect(actionStub1.notCalled).toEqual(true);
  483. expect(actionStub2.calledOnce).toEqual(true);
  484. });
  485. it('leaves original file actions untouched (clean copy)', function() {
  486. var actions1 = new OCA.Files.FileActions();
  487. var actions2 = new OCA.Files.FileActions();
  488. var actionStub1 = sinon.stub();
  489. var actionStub2 = sinon.stub();
  490. actions1.registerAction({
  491. mime: 'all',
  492. name: 'Test',
  493. type: OCA.Files.FileActions.TYPE_INLINE,
  494. permissions: OC.PERMISSION_READ,
  495. icon: OC.imagePath('core', 'actions/test'),
  496. actionHandler: actionStub1
  497. });
  498. // copy the Test action to actions2
  499. actions2.merge(actions1);
  500. // late override
  501. actions2.registerAction({
  502. mime: 'all',
  503. name: 'Test', // override
  504. type: OCA.Files.FileActions.TYPE_INLINE,
  505. permissions: OC.PERMISSION_READ,
  506. icon: OC.imagePath('core', 'actions/test'),
  507. actionHandler: actionStub2
  508. });
  509. // check if original actions still call the correct handler
  510. actions1.display($tr.find('td.filename'), true, fileList);
  511. expect($tr.find('.action-test').length).toEqual(1);
  512. $tr.find('.action-test').click();
  513. expect(actionStub1.calledOnce).toEqual(true);
  514. expect(actionStub2.notCalled).toEqual(true);
  515. });
  516. });
  517. describe('events', function() {
  518. var clock;
  519. beforeEach(function() {
  520. clock = sinon.useFakeTimers();
  521. });
  522. afterEach(function() {
  523. clock.restore();
  524. });
  525. it('notifies update event handlers once after multiple changes', function() {
  526. var actionStub = sinon.stub();
  527. var handler = sinon.stub();
  528. fileActions.on('registerAction', handler);
  529. fileActions.registerAction({
  530. mime: 'all',
  531. name: 'Test',
  532. type: OCA.Files.FileActions.TYPE_INLINE,
  533. permissions: OC.PERMISSION_READ,
  534. icon: OC.imagePath('core', 'actions/test'),
  535. actionHandler: actionStub
  536. });
  537. fileActions.registerAction({
  538. mime: 'all',
  539. name: 'Test2',
  540. permissions: OC.PERMISSION_READ,
  541. icon: OC.imagePath('core', 'actions/test'),
  542. actionHandler: actionStub
  543. });
  544. expect(handler.calledTwice).toEqual(true);
  545. });
  546. it('does not notifies update event handlers after unregistering', function() {
  547. var actionStub = sinon.stub();
  548. var handler = sinon.stub();
  549. fileActions.on('registerAction', handler);
  550. fileActions.off('registerAction', handler);
  551. fileActions.registerAction({
  552. mime: 'all',
  553. name: 'Test',
  554. type: OCA.Files.FileActions.TYPE_INLINE,
  555. permissions: OC.PERMISSION_READ,
  556. icon: OC.imagePath('core', 'actions/test'),
  557. actionHandler: actionStub
  558. });
  559. fileActions.registerAction({
  560. mime: 'all',
  561. name: 'Test2',
  562. type: OCA.Files.FileActions.TYPE_INLINE,
  563. permissions: OC.PERMISSION_READ,
  564. icon: OC.imagePath('core', 'actions/test'),
  565. actionHandler: actionStub
  566. });
  567. expect(handler.notCalled).toEqual(true);
  568. });
  569. });
  570. describe('default actions', function() {
  571. describe('download', function() {
  572. it('redirects to URL and sets busy state to list', function() {
  573. var handleDownloadStub = sinon.stub(OCA.Files.Files, 'handleDownload');
  574. var busyStub = sinon.stub(fileList, 'showFileBusyState');
  575. var fileData = {
  576. id: 18,
  577. type: 'file',
  578. name: 'testName.txt',
  579. mimetype: 'text/plain',
  580. size: '1234',
  581. etag: 'a01234c',
  582. mtime: '123456',
  583. permissions: OC.PERMISSION_READ | OC.PERMISSION_UPDATE
  584. };
  585. // note: FileActions.display() is called implicitly
  586. fileList.add(fileData);
  587. var model = fileList.getModelForFile('testName.txt');
  588. fileActions.registerDefaultActions();
  589. fileActions.triggerAction('Download', model, fileList);
  590. expect(busyStub.calledOnce).toEqual(true);
  591. expect(busyStub.calledWith('testName.txt', true)).toEqual(true);
  592. expect(handleDownloadStub.calledOnce).toEqual(true);
  593. expect(handleDownloadStub.getCall(0).args[0]).toEqual(
  594. OC.webroot + '/remote.php/webdav/subdir/testName.txt'
  595. );
  596. busyStub.reset();
  597. handleDownloadStub.yield();
  598. expect(busyStub.calledOnce).toEqual(true);
  599. expect(busyStub.calledWith('testName.txt', false)).toEqual(true);
  600. busyStub.restore();
  601. handleDownloadStub.restore();
  602. });
  603. });
  604. });
  605. describe('download spinner', function() {
  606. var FileActions = OCA.Files.FileActions;
  607. var $el;
  608. beforeEach(function() {
  609. $el = $('<a href="#"><span class="icon icon-download"></span><span>Download</span></a>');
  610. });
  611. it('replaces download icon with spinner', function() {
  612. FileActions.updateFileActionSpinner($el, true);
  613. expect($el.find('.icon.icon-loading-small').length).toEqual(1);
  614. expect($el.find('.icon.icon-download').hasClass('hidden')).toEqual(true);
  615. });
  616. it('replaces spinner back with download icon with spinner', function() {
  617. FileActions.updateFileActionSpinner($el, true);
  618. FileActions.updateFileActionSpinner($el, false);
  619. expect($el.find('.icon.icon-loading-small').length).toEqual(0);
  620. expect($el.find('.icon.icon-download').hasClass('hidden')).toEqual(false);
  621. });
  622. });
  623. });