fileactionsSpec.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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" class="list-container view-grid"><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. var notifyUpdateListenersSpy = sinon.spy(fileList.fileActions, '_notifyUpdateListeners');
  279. $tr.find('.action-test').click();
  280. expect(actionStub.calledOnce).toEqual(true);
  281. expect(actionStub.getCall(0).args[0]).toEqual('testName.txt');
  282. var context = actionStub.getCall(0).args[1];
  283. expect(context.$file.is($tr)).toEqual(true);
  284. expect(context.fileList).toBeDefined();
  285. expect(context.fileActions).toBeDefined();
  286. expect(context.dir).toEqual('/subdir');
  287. expect(context.fileInfoModel.get('name')).toEqual('testName.txt');
  288. expect(notifyUpdateListenersSpy.calledTwice).toEqual(true);
  289. expect(notifyUpdateListenersSpy.calledBefore(actionStub)).toEqual(true);
  290. expect(notifyUpdateListenersSpy.calledAfter(actionStub)).toEqual(true);
  291. expect(notifyUpdateListenersSpy.getCall(0).args[0]).toEqual('beforeTriggerAction');
  292. expect(notifyUpdateListenersSpy.getCall(0).args[1]).toEqual({
  293. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  294. fileName: 'testName.txt',
  295. context: context
  296. });
  297. expect(notifyUpdateListenersSpy.getCall(1).args[0]).toEqual('afterTriggerAction');
  298. expect(notifyUpdateListenersSpy.getCall(1).args[1]).toEqual({
  299. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  300. fileName: 'testName.txt',
  301. context: context
  302. });
  303. // when data-path is defined
  304. actionStub.reset();
  305. $tr.attr('data-path', '/somepath');
  306. $tr.find('.action-test').click();
  307. context = actionStub.getCall(0).args[1];
  308. expect(context.dir).toEqual('/somepath');
  309. });
  310. it('also triggers action handler when calling triggerAction()', function() {
  311. var notifyUpdateListenersSpy = sinon.spy(fileList.fileActions, '_notifyUpdateListeners');
  312. var model = new OCA.Files.FileInfoModel({
  313. id: 1,
  314. name: 'Test.txt',
  315. path: '/subdir',
  316. mime: 'text/plain',
  317. permissions: 31
  318. });
  319. fileActions.triggerAction('Test', model, fileList);
  320. expect(actionStub.calledOnce).toEqual(true);
  321. expect(actionStub.getCall(0).args[0]).toEqual('Test.txt');
  322. expect(actionStub.getCall(0).args[1].fileList).toEqual(fileList);
  323. expect(actionStub.getCall(0).args[1].fileActions).toEqual(fileActions);
  324. expect(actionStub.getCall(0).args[1].fileInfoModel).toEqual(model);
  325. expect(notifyUpdateListenersSpy.calledTwice).toEqual(true);
  326. expect(notifyUpdateListenersSpy.calledBefore(actionStub)).toEqual(true);
  327. expect(notifyUpdateListenersSpy.calledAfter(actionStub)).toEqual(true);
  328. expect(notifyUpdateListenersSpy.getCall(0).args[0]).toEqual('beforeTriggerAction');
  329. expect(notifyUpdateListenersSpy.getCall(0).args[1]).toEqual({
  330. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  331. fileName: 'Test.txt',
  332. context: {
  333. fileActions: fileActions,
  334. fileInfoModel: model,
  335. dir: '/subdir',
  336. fileList: fileList,
  337. $file: fileList.findFileEl('Test.txt')
  338. }
  339. });
  340. expect(notifyUpdateListenersSpy.getCall(1).args[0]).toEqual('afterTriggerAction');
  341. expect(notifyUpdateListenersSpy.getCall(1).args[1]).toEqual({
  342. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  343. fileName: 'Test.txt',
  344. context: {
  345. fileActions: fileActions,
  346. fileInfoModel: model,
  347. dir: '/subdir',
  348. fileList: fileList,
  349. $file: fileList.findFileEl('Test.txt')
  350. }
  351. });
  352. });
  353. it('triggers listener events when invoked directly', function() {
  354. var context = {fileActions: new OCA.Files.FileActions()}
  355. var notifyUpdateListenersSpy = sinon.spy(context.fileActions, '_notifyUpdateListeners');
  356. var testAction = fileActions.get('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'];
  357. testAction('Test.txt', context);
  358. expect(actionStub.calledOnce).toEqual(true);
  359. expect(actionStub.getCall(0).args[0]).toEqual('Test.txt');
  360. expect(actionStub.getCall(0).args[1]).toBe(context);
  361. expect(notifyUpdateListenersSpy.calledTwice).toEqual(true);
  362. expect(notifyUpdateListenersSpy.calledBefore(actionStub)).toEqual(true);
  363. expect(notifyUpdateListenersSpy.calledAfter(actionStub)).toEqual(true);
  364. expect(notifyUpdateListenersSpy.getCall(0).args[0]).toEqual('beforeTriggerAction');
  365. expect(notifyUpdateListenersSpy.getCall(0).args[1]).toEqual({
  366. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  367. fileName: 'Test.txt',
  368. context: context
  369. });
  370. expect(notifyUpdateListenersSpy.getCall(1).args[0]).toEqual('afterTriggerAction');
  371. expect(notifyUpdateListenersSpy.getCall(1).args[1]).toEqual({
  372. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  373. fileName: 'Test.txt',
  374. context: context
  375. });
  376. }),
  377. describe('actions menu', function() {
  378. it('shows actions menu inside row when clicking the menu trigger', function() {
  379. expect($tr.find('td.filename .fileActionsMenu').length).toEqual(0);
  380. $tr.find('.action-menu').click();
  381. expect($tr.find('td.filename .fileActionsMenu').length).toEqual(1);
  382. });
  383. it('shows highlight on current row', function() {
  384. $tr.find('.action-menu').click();
  385. expect($tr.hasClass('mouseOver')).toEqual(true);
  386. });
  387. it('cleans up after hiding', function() {
  388. var slideUpStub = sinon.stub($.fn, 'slideUp');
  389. $tr.find('.action-menu').click();
  390. expect($tr.find('.fileActionsMenu').length).toEqual(1);
  391. OC.hideMenus();
  392. // sliding animation
  393. expect(slideUpStub.calledOnce).toEqual(true);
  394. slideUpStub.getCall(0).args[1]();
  395. expect($tr.hasClass('mouseOver')).toEqual(false);
  396. expect($tr.find('.fileActionsMenu').length).toEqual(0);
  397. });
  398. });
  399. });
  400. describe('custom rendering', function() {
  401. var $tr;
  402. beforeEach(function() {
  403. var fileData = {
  404. id: 18,
  405. type: 'file',
  406. name: 'testName.txt',
  407. mimetype: 'text/plain',
  408. size: '1234',
  409. etag: 'a01234c',
  410. mtime: '123456'
  411. };
  412. $tr = fileList.add(fileData);
  413. });
  414. it('regular function', function() {
  415. var actionStub = sinon.stub();
  416. fileActions.registerAction({
  417. name: 'Test',
  418. displayName: '',
  419. mime: 'all',
  420. type: OCA.Files.FileActions.TYPE_INLINE,
  421. permissions: OC.PERMISSION_READ,
  422. render: function(actionSpec, isDefault, context) {
  423. expect(actionSpec.name).toEqual('Test');
  424. expect(actionSpec.displayName).toEqual('');
  425. expect(actionSpec.permissions).toEqual(OC.PERMISSION_READ);
  426. expect(actionSpec.mime).toEqual('all');
  427. expect(isDefault).toEqual(false);
  428. expect(context.fileList).toEqual(fileList);
  429. expect(context.$file[0]).toEqual($tr[0]);
  430. var $customEl = $('<a class="action action-test" href="#"><span>blabli</span><span>blabla</span></a>');
  431. $tr.find('td:first').append($customEl);
  432. return $customEl;
  433. },
  434. actionHandler: actionStub
  435. });
  436. fileActions.display($tr.find('td.filename'), true, fileList);
  437. var $actionEl = $tr.find('td:first .action-test');
  438. expect($actionEl.length).toEqual(1);
  439. expect($actionEl.hasClass('action')).toEqual(true);
  440. $actionEl.click();
  441. expect(actionStub.calledOnce).toEqual(true);
  442. expect(actionStub.getCall(0).args[0]).toEqual('testName.txt');
  443. });
  444. });
  445. describe('merging', function() {
  446. var $tr;
  447. beforeEach(function() {
  448. var fileData = {
  449. id: 18,
  450. type: 'file',
  451. name: 'testName.txt',
  452. path: '/anotherpath/there',
  453. mimetype: 'text/plain',
  454. size: '1234',
  455. etag: 'a01234c',
  456. mtime: '123456'
  457. };
  458. $tr = fileList.add(fileData);
  459. });
  460. afterEach(function() {
  461. $tr = null;
  462. });
  463. it('copies all actions to target file actions', function() {
  464. var actions1 = new OCA.Files.FileActions();
  465. var actions2 = new OCA.Files.FileActions();
  466. var actionStub1 = sinon.stub();
  467. var actionStub2 = sinon.stub();
  468. actions1.registerAction({
  469. name: 'Test',
  470. type: OCA.Files.FileActions.TYPE_INLINE,
  471. mime: 'all',
  472. permissions: OC.PERMISSION_READ,
  473. icon: OC.imagePath('core', 'actions/test'),
  474. actionHandler: actionStub1
  475. });
  476. actions2.registerAction({
  477. name: 'Test2',
  478. type: OCA.Files.FileActions.TYPE_INLINE,
  479. mime: 'all',
  480. permissions: OC.PERMISSION_READ,
  481. icon: OC.imagePath('core', 'actions/test'),
  482. actionHandler: actionStub2
  483. });
  484. actions2.merge(actions1);
  485. actions2.display($tr.find('td.filename'), true, fileList);
  486. expect($tr.find('.action-test').length).toEqual(1);
  487. expect($tr.find('.action-test2').length).toEqual(1);
  488. $tr.find('.action-test').click();
  489. expect(actionStub1.calledOnce).toEqual(true);
  490. expect(actionStub2.notCalled).toEqual(true);
  491. actionStub1.reset();
  492. $tr.find('.action-test2').click();
  493. expect(actionStub1.notCalled).toEqual(true);
  494. expect(actionStub2.calledOnce).toEqual(true);
  495. });
  496. it('overrides existing actions on merge', function() {
  497. var actions1 = new OCA.Files.FileActions();
  498. var actions2 = new OCA.Files.FileActions();
  499. var actionStub1 = sinon.stub();
  500. var actionStub2 = sinon.stub();
  501. actions1.registerAction({
  502. name: 'Test',
  503. type: OCA.Files.FileActions.TYPE_INLINE,
  504. mime: 'all',
  505. permissions: OC.PERMISSION_READ,
  506. icon: OC.imagePath('core', 'actions/test'),
  507. actionHandler: actionStub1
  508. });
  509. actions2.registerAction({
  510. name: 'Test', // override
  511. mime: 'all',
  512. type: OCA.Files.FileActions.TYPE_INLINE,
  513. permissions: OC.PERMISSION_READ,
  514. icon: OC.imagePath('core', 'actions/test'),
  515. actionHandler: actionStub2
  516. });
  517. actions1.merge(actions2);
  518. actions1.display($tr.find('td.filename'), true, fileList);
  519. expect($tr.find('.action-test').length).toEqual(1);
  520. $tr.find('.action-test').click();
  521. expect(actionStub1.notCalled).toEqual(true);
  522. expect(actionStub2.calledOnce).toEqual(true);
  523. });
  524. it('overrides existing action when calling register after merge', function() {
  525. var actions1 = new OCA.Files.FileActions();
  526. var actions2 = new OCA.Files.FileActions();
  527. var actionStub1 = sinon.stub();
  528. var actionStub2 = sinon.stub();
  529. actions1.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: actionStub1
  536. });
  537. actions1.merge(actions2);
  538. // late override
  539. actions1.registerAction({
  540. mime: 'all',
  541. name: 'Test', // override
  542. type: OCA.Files.FileActions.TYPE_INLINE,
  543. permissions: OC.PERMISSION_READ,
  544. icon: OC.imagePath('core', 'actions/test'),
  545. actionHandler: actionStub2
  546. });
  547. actions1.display($tr.find('td.filename'), true, fileList);
  548. expect($tr.find('.action-test').length).toEqual(1);
  549. $tr.find('.action-test').click();
  550. expect(actionStub1.notCalled).toEqual(true);
  551. expect(actionStub2.calledOnce).toEqual(true);
  552. });
  553. it('leaves original file actions untouched (clean copy)', function() {
  554. var actions1 = new OCA.Files.FileActions();
  555. var actions2 = new OCA.Files.FileActions();
  556. var actionStub1 = sinon.stub();
  557. var actionStub2 = sinon.stub();
  558. actions1.registerAction({
  559. mime: 'all',
  560. name: 'Test',
  561. type: OCA.Files.FileActions.TYPE_INLINE,
  562. permissions: OC.PERMISSION_READ,
  563. icon: OC.imagePath('core', 'actions/test'),
  564. actionHandler: actionStub1
  565. });
  566. // copy the Test action to actions2
  567. actions2.merge(actions1);
  568. // late override
  569. actions2.registerAction({
  570. mime: 'all',
  571. name: 'Test', // override
  572. type: OCA.Files.FileActions.TYPE_INLINE,
  573. permissions: OC.PERMISSION_READ,
  574. icon: OC.imagePath('core', 'actions/test'),
  575. actionHandler: actionStub2
  576. });
  577. // check if original actions still call the correct handler
  578. actions1.display($tr.find('td.filename'), true, fileList);
  579. expect($tr.find('.action-test').length).toEqual(1);
  580. $tr.find('.action-test').click();
  581. expect(actionStub1.calledOnce).toEqual(true);
  582. expect(actionStub2.notCalled).toEqual(true);
  583. });
  584. });
  585. describe('events', function() {
  586. var clock;
  587. beforeEach(function() {
  588. clock = sinon.useFakeTimers();
  589. });
  590. afterEach(function() {
  591. clock.restore();
  592. });
  593. it('notifies update event handlers once after multiple changes', function() {
  594. var actionStub = sinon.stub();
  595. var handler = sinon.stub();
  596. fileActions.on('registerAction', handler);
  597. fileActions.registerAction({
  598. mime: 'all',
  599. name: 'Test',
  600. type: OCA.Files.FileActions.TYPE_INLINE,
  601. permissions: OC.PERMISSION_READ,
  602. icon: OC.imagePath('core', 'actions/test'),
  603. actionHandler: actionStub
  604. });
  605. fileActions.registerAction({
  606. mime: 'all',
  607. name: 'Test2',
  608. permissions: OC.PERMISSION_READ,
  609. icon: OC.imagePath('core', 'actions/test'),
  610. actionHandler: actionStub
  611. });
  612. expect(handler.calledTwice).toEqual(true);
  613. });
  614. it('does not notifies update event handlers after unregistering', function() {
  615. var actionStub = sinon.stub();
  616. var handler = sinon.stub();
  617. fileActions.on('registerAction', handler);
  618. fileActions.off('registerAction', handler);
  619. fileActions.registerAction({
  620. mime: 'all',
  621. name: 'Test',
  622. type: OCA.Files.FileActions.TYPE_INLINE,
  623. permissions: OC.PERMISSION_READ,
  624. icon: OC.imagePath('core', 'actions/test'),
  625. actionHandler: actionStub
  626. });
  627. fileActions.registerAction({
  628. mime: 'all',
  629. name: 'Test2',
  630. type: OCA.Files.FileActions.TYPE_INLINE,
  631. permissions: OC.PERMISSION_READ,
  632. icon: OC.imagePath('core', 'actions/test'),
  633. actionHandler: actionStub
  634. });
  635. expect(handler.notCalled).toEqual(true);
  636. });
  637. });
  638. describe('default actions', function() {
  639. describe('download', function() {
  640. it('redirects to URL and sets busy state to list', function() {
  641. var handleDownloadStub = sinon.stub(OCA.Files.Files, 'handleDownload');
  642. var busyStub = sinon.stub(fileList, 'showFileBusyState');
  643. var fileData = {
  644. id: 18,
  645. type: 'file',
  646. name: 'testName.txt',
  647. mimetype: 'text/plain',
  648. size: '1234',
  649. etag: 'a01234c',
  650. mtime: '123456',
  651. permissions: OC.PERMISSION_READ | OC.PERMISSION_UPDATE
  652. };
  653. // note: FileActions.display() is called implicitly
  654. fileList.add(fileData);
  655. var model = fileList.getModelForFile('testName.txt');
  656. fileActions.registerDefaultActions();
  657. fileActions.triggerAction('Download', model, fileList);
  658. expect(busyStub.calledOnce).toEqual(true);
  659. expect(busyStub.calledWith('testName.txt', true)).toEqual(true);
  660. expect(handleDownloadStub.calledOnce).toEqual(true);
  661. expect(handleDownloadStub.getCall(0).args[0]).toEqual(
  662. OC.getRootPath() + '/remote.php/webdav/subdir/testName.txt'
  663. );
  664. busyStub.reset();
  665. handleDownloadStub.yield();
  666. expect(busyStub.calledOnce).toEqual(true);
  667. expect(busyStub.calledWith('testName.txt', false)).toEqual(true);
  668. busyStub.restore();
  669. handleDownloadStub.restore();
  670. });
  671. });
  672. });
  673. describe('download spinner', function() {
  674. var FileActions = OCA.Files.FileActions;
  675. var $el;
  676. beforeEach(function() {
  677. $el = $('<a href="#"><span class="icon icon-download"></span><span>Download</span></a>');
  678. });
  679. it('replaces download icon with spinner', function() {
  680. FileActions.updateFileActionSpinner($el, true);
  681. expect($el.find('.icon.icon-loading-small').length).toEqual(1);
  682. expect($el.find('.icon.icon-download').hasClass('hidden')).toEqual(true);
  683. });
  684. it('replaces spinner back with download icon with spinner', function() {
  685. FileActions.updateFileActionSpinner($el, true);
  686. FileActions.updateFileActionSpinner($el, false);
  687. expect($el.find('.icon.icon-loading-small').length).toEqual(0);
  688. expect($el.find('.icon.icon-download').hasClass('hidden')).toEqual(false);
  689. });
  690. });
  691. });