1
0

fileactionsSpec.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /**
  2. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  6. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Vincent Chan <plus.vincchan@gmail.com>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0-or-later
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. describe('OCA.Files.FileActions tests', function() {
  29. var fileList, fileActions, clock;
  30. beforeEach(function() {
  31. clock = sinon.useFakeTimers();
  32. // init horrible parameters
  33. var $body = $('#testArea');
  34. $body.append('<input type="hidden" id="permissions" value="31"></input>');
  35. $body.append('<table class="files-filestable list-container view-grid"><tbody class="files-fileList"></tbody></table>');
  36. // dummy files table
  37. fileActions = new OCA.Files.FileActions();
  38. fileActions.registerAction({
  39. name: 'Testdropdown',
  40. displayName: 'Testdropdowndisplay',
  41. mime: 'all',
  42. permissions: OC.PERMISSION_READ,
  43. icon: function () {
  44. return OC.imagePath('core', 'actions/download');
  45. }
  46. });
  47. fileActions.registerAction({
  48. name: 'Testinline',
  49. displayName: 'Testinlinedisplay',
  50. type: OCA.Files.FileActions.TYPE_INLINE,
  51. mime: 'all',
  52. permissions: OC.PERMISSION_READ
  53. });
  54. fileActions.registerAction({
  55. name: 'Testdefault',
  56. displayName: 'Testdefaultdisplay',
  57. mime: 'all',
  58. permissions: OC.PERMISSION_READ
  59. });
  60. fileActions.setDefault('all', 'Testdefault');
  61. fileList = new OCA.Files.FileList($body, {
  62. fileActions: fileActions
  63. });
  64. fileList.changeDirectory('/subdir', false, true);
  65. });
  66. afterEach(function() {
  67. fileActions = null;
  68. fileList.destroy();
  69. fileList = undefined;
  70. clock.restore();
  71. $('#permissions, .files-filestable').remove();
  72. });
  73. it('calling clear() clears file actions', function() {
  74. fileActions.clear();
  75. expect(fileActions.actions).toEqual({});
  76. expect(fileActions.defaults).toEqual({});
  77. expect(fileActions.icons).toEqual({});
  78. expect(fileActions.currentFile).toBe(null);
  79. });
  80. describe('displaying actions', function() {
  81. var $tr;
  82. beforeEach(function() {
  83. var fileData = {
  84. id: 18,
  85. type: 'file',
  86. name: 'testName.txt',
  87. mimetype: 'text/plain',
  88. size: '1234',
  89. etag: 'a01234c',
  90. mtime: '123456',
  91. permissions: OC.PERMISSION_READ | OC.PERMISSION_UPDATE
  92. };
  93. // note: FileActions.display() is called implicitly
  94. $tr = fileList.add(fileData);
  95. });
  96. it('renders inline file actions', function() {
  97. // actions defined after call
  98. expect($tr.find('.action.action-testinline').length).toEqual(1);
  99. expect($tr.find('.action.action-testinline').attr('data-action')).toEqual('Testinline');
  100. });
  101. it('does not render dropdown actions', function() {
  102. expect($tr.find('.action.action-testdropdown').length).toEqual(0);
  103. });
  104. it('does not render default action', function() {
  105. expect($tr.find('.action.action-testdefault').length).toEqual(0);
  106. });
  107. it('replaces file actions when displayed twice', function() {
  108. fileActions.display($tr.find('td.filename'), true, fileList);
  109. fileActions.display($tr.find('td.filename'), true, fileList);
  110. expect($tr.find('.action.action-testinline').length).toEqual(1);
  111. });
  112. it('renders actions menu trigger', function() {
  113. expect($tr.find('.action.action-menu').length).toEqual(1);
  114. expect($tr.find('.action.action-menu').attr('data-action')).toEqual('menu');
  115. });
  116. it('only renders actions relevant to the mime type', function() {
  117. fileActions.registerAction({
  118. name: 'Match',
  119. displayName: 'MatchDisplay',
  120. type: OCA.Files.FileActions.TYPE_INLINE,
  121. mime: 'text/plain',
  122. permissions: OC.PERMISSION_READ
  123. });
  124. fileActions.registerAction({
  125. name: 'Nomatch',
  126. displayName: 'NoMatchDisplay',
  127. type: OCA.Files.FileActions.TYPE_INLINE,
  128. mime: 'application/octet-stream',
  129. permissions: OC.PERMISSION_READ
  130. });
  131. fileActions.display($tr.find('td.filename'), true, fileList);
  132. expect($tr.find('.action.action-match').length).toEqual(1);
  133. expect($tr.find('.action.action-nomatch').length).toEqual(0);
  134. });
  135. it('only renders actions relevant to the permissions', function() {
  136. fileActions.registerAction({
  137. name: 'Match',
  138. displayName: 'MatchDisplay',
  139. type: OCA.Files.FileActions.TYPE_INLINE,
  140. mime: 'text/plain',
  141. permissions: OC.PERMISSION_UPDATE
  142. });
  143. fileActions.registerAction({
  144. name: 'Nomatch',
  145. displayName: 'NoMatchDisplay',
  146. type: OCA.Files.FileActions.TYPE_INLINE,
  147. mime: 'text/plain',
  148. permissions: OC.PERMISSION_DELETE
  149. });
  150. fileActions.display($tr.find('td.filename'), true, fileList);
  151. expect($tr.find('.action.action-match').length).toEqual(1);
  152. expect($tr.find('.action.action-nomatch').length).toEqual(0);
  153. });
  154. it('display inline icon with image path', function() {
  155. fileActions.registerAction({
  156. name: 'Icon',
  157. displayName: 'IconDisplay',
  158. type: OCA.Files.FileActions.TYPE_INLINE,
  159. mime: 'all',
  160. icon: OC.imagePath('core', 'actions/icon'),
  161. permissions: OC.PERMISSION_READ
  162. });
  163. fileActions.registerAction({
  164. name: 'NoIcon',
  165. displayName: 'NoIconDisplay',
  166. type: OCA.Files.FileActions.TYPE_INLINE,
  167. mime: 'all',
  168. permissions: OC.PERMISSION_READ
  169. });
  170. fileActions.display($tr.find('td.filename'), true, fileList);
  171. expect($tr.find('.action.action-icon').length).toEqual(1);
  172. expect($tr.find('.action.action-icon').find('img').length).toEqual(1);
  173. expect($tr.find('.action.action-icon').find('img').eq(0).attr('src')).toEqual(OC.imagePath('core', 'actions/icon'));
  174. expect($tr.find('.action.action-noicon').length).toEqual(1);
  175. expect($tr.find('.action.action-noicon').find('img').length).toEqual(0);
  176. });
  177. it('display alt text on inline icon with image path', function() {
  178. fileActions.registerAction({
  179. name: 'IconAltText',
  180. displayName: 'IconAltTextDisplay',
  181. type: OCA.Files.FileActions.TYPE_INLINE,
  182. mime: 'all',
  183. icon: OC.imagePath('core', 'actions/iconAltText'),
  184. altText: 'alt icon text',
  185. permissions: OC.PERMISSION_READ
  186. });
  187. fileActions.registerAction({
  188. name: 'IconNoAltText',
  189. displayName: 'IconNoAltTextDisplay',
  190. type: OCA.Files.FileActions.TYPE_INLINE,
  191. mime: 'all',
  192. icon: OC.imagePath('core', 'actions/iconNoAltText'),
  193. permissions: OC.PERMISSION_READ
  194. });
  195. fileActions.display($tr.find('td.filename'), true, fileList);
  196. expect($tr.find('.action.action-iconalttext').length).toEqual(1);
  197. expect($tr.find('.action.action-iconalttext').find('img').length).toEqual(1);
  198. expect($tr.find('.action.action-iconalttext').find('img').eq(0).attr('alt')).toEqual('alt icon text');
  199. expect($tr.find('.action.action-iconnoalttext').length).toEqual(1);
  200. expect($tr.find('.action.action-iconnoalttext').find('img').length).toEqual(1);
  201. expect($tr.find('.action.action-iconnoalttext').find('img').eq(0).attr('alt')).toEqual('');
  202. });
  203. it('display inline icon with iconClass', function() {
  204. fileActions.registerAction({
  205. name: 'Icon',
  206. displayName: 'IconDisplay',
  207. type: OCA.Files.FileActions.TYPE_INLINE,
  208. mime: 'all',
  209. iconClass: 'icon-test',
  210. permissions: OC.PERMISSION_READ
  211. });
  212. fileActions.registerAction({
  213. name: 'NoIcon',
  214. displayName: 'NoIconDisplay',
  215. type: OCA.Files.FileActions.TYPE_INLINE,
  216. mime: 'all',
  217. permissions: OC.PERMISSION_READ
  218. });
  219. fileActions.display($tr.find('td.filename'), true, fileList);
  220. expect($tr.find('.action.action-icon').length).toEqual(1);
  221. expect($tr.find('.action.action-icon').find('.icon').length).toEqual(1);
  222. expect($tr.find('.action.action-icon').find('.icon').hasClass('icon-test')).toEqual(true);
  223. expect($tr.find('.action.action-noicon').length).toEqual(1);
  224. expect($tr.find('.action.action-noicon').find('.icon').length).toEqual(0);
  225. });
  226. it('display alt text on inline icon with iconClass when no display name exists', function() {
  227. fileActions.registerAction({
  228. name: 'IconAltText',
  229. displayName: '',
  230. type: OCA.Files.FileActions.TYPE_INLINE,
  231. mime: 'all',
  232. iconClass: 'icon-alttext',
  233. altText: 'alt icon text',
  234. permissions: OC.PERMISSION_READ
  235. });
  236. fileActions.registerAction({
  237. name: 'IconNoAltText',
  238. displayName: 'IconNoAltTextDisplay',
  239. type: OCA.Files.FileActions.TYPE_INLINE,
  240. mime: 'all',
  241. altText: 'useless alt text',
  242. iconClass: 'icon-noalttext',
  243. permissions: OC.PERMISSION_READ
  244. });
  245. fileActions.display($tr.find('td.filename'), true, fileList);
  246. expect($tr.find('.action.action-iconalttext').length).toEqual(1);
  247. expect($tr.find('.action.action-iconalttext').find('.icon').length).toEqual(1);
  248. expect($tr.find('.action.action-iconalttext').find('.hidden-visually').text()).toEqual('alt icon text');
  249. expect($tr.find('.action.action-iconnoalttext').length).toEqual(1);
  250. expect($tr.find('.action.action-iconnoalttext').find('.icon').length).toEqual(1);
  251. expect($tr.find('.action.action-iconnoalttext').find('.hidden-visually').length).toEqual(0);
  252. });
  253. });
  254. describe('action handler', function() {
  255. var actionStub, $tr, clock;
  256. beforeEach(function() {
  257. clock = sinon.useFakeTimers();
  258. var fileData = {
  259. id: 18,
  260. type: 'file',
  261. name: 'testName.txt',
  262. mimetype: 'text/plain',
  263. size: '1234',
  264. etag: 'a01234c',
  265. mtime: '123456'
  266. };
  267. actionStub = sinon.stub();
  268. fileActions.registerAction({
  269. name: 'Test',
  270. type: OCA.Files.FileActions.TYPE_INLINE,
  271. mime: 'all',
  272. icon: OC.imagePath('core', 'actions/test'),
  273. permissions: OC.PERMISSION_READ,
  274. actionHandler: actionStub
  275. });
  276. $tr = fileList.add(fileData);
  277. });
  278. afterEach(function() {
  279. OC.hideMenus();
  280. // jump past animations
  281. clock.tick(1000);
  282. clock.restore();
  283. });
  284. it('passes context to action handler', function() {
  285. var notifyUpdateListenersSpy = sinon.spy(fileList.fileActions, '_notifyUpdateListeners');
  286. expect($tr.length).toEqual(1);
  287. expect($tr.find('.action-test').length).toEqual(1);
  288. $tr.find('.action-test').click();
  289. expect(actionStub.calledOnce).toEqual(true);
  290. expect(actionStub.getCall(0).args[0]).toEqual('testName.txt');
  291. var context = actionStub.getCall(0).args[1];
  292. expect(context.$file.is($tr)).toEqual(true);
  293. expect(context.fileList).toBeDefined();
  294. expect(context.fileActions).toBeDefined();
  295. expect(context.dir).toEqual('/subdir');
  296. expect(context.fileInfoModel.get('name')).toEqual('testName.txt');
  297. expect(notifyUpdateListenersSpy.calledTwice).toEqual(true);
  298. expect(notifyUpdateListenersSpy.calledBefore(actionStub)).toEqual(true);
  299. expect(notifyUpdateListenersSpy.calledAfter(actionStub)).toEqual(true);
  300. expect(notifyUpdateListenersSpy.getCall(0).args[0]).toEqual('beforeTriggerAction');
  301. expect(notifyUpdateListenersSpy.getCall(0).args[1]).toEqual({
  302. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  303. fileName: 'testName.txt',
  304. context: context
  305. });
  306. expect(notifyUpdateListenersSpy.getCall(1).args[0]).toEqual('afterTriggerAction');
  307. expect(notifyUpdateListenersSpy.getCall(1).args[1]).toEqual({
  308. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  309. fileName: 'testName.txt',
  310. context: context
  311. });
  312. // when data-path is defined
  313. actionStub.reset();
  314. $tr.attr('data-path', '/somepath');
  315. $tr.find('.action-test').click();
  316. context = actionStub.getCall(0).args[1];
  317. expect(context.dir).toEqual('/somepath');
  318. });
  319. it('also triggers action handler when calling triggerAction()', function() {
  320. var notifyUpdateListenersSpy = sinon.spy(fileList.fileActions, '_notifyUpdateListeners');
  321. var model = new OCA.Files.FileInfoModel({
  322. id: 1,
  323. name: 'Test.txt',
  324. path: '/subdir',
  325. mime: 'text/plain',
  326. permissions: 31
  327. });
  328. fileActions.triggerAction('Test', model, fileList);
  329. expect(actionStub.calledOnce).toEqual(true);
  330. expect(actionStub.getCall(0).args[0]).toEqual('Test.txt');
  331. expect(actionStub.getCall(0).args[1].fileList).toEqual(fileList);
  332. expect(actionStub.getCall(0).args[1].fileActions).toEqual(fileActions);
  333. expect(actionStub.getCall(0).args[1].fileInfoModel).toEqual(model);
  334. expect(notifyUpdateListenersSpy.calledTwice).toEqual(true);
  335. expect(notifyUpdateListenersSpy.calledBefore(actionStub)).toEqual(true);
  336. expect(notifyUpdateListenersSpy.calledAfter(actionStub)).toEqual(true);
  337. expect(notifyUpdateListenersSpy.getCall(0).args[0]).toEqual('beforeTriggerAction');
  338. expect(notifyUpdateListenersSpy.getCall(0).args[1]).toEqual({
  339. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  340. fileName: 'Test.txt',
  341. context: {
  342. fileActions: fileActions,
  343. fileInfoModel: model,
  344. dir: '/subdir',
  345. fileList: fileList,
  346. $file: fileList.findFileEl('Test.txt')
  347. }
  348. });
  349. expect(notifyUpdateListenersSpy.getCall(1).args[0]).toEqual('afterTriggerAction');
  350. expect(notifyUpdateListenersSpy.getCall(1).args[1]).toEqual({
  351. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  352. fileName: 'Test.txt',
  353. context: {
  354. fileActions: fileActions,
  355. fileInfoModel: model,
  356. dir: '/subdir',
  357. fileList: fileList,
  358. $file: fileList.findFileEl('Test.txt')
  359. }
  360. });
  361. });
  362. it('triggers listener events when invoked directly', function() {
  363. var context = {fileActions: new OCA.Files.FileActions()}
  364. var notifyUpdateListenersSpy = sinon.spy(context.fileActions, '_notifyUpdateListeners');
  365. var testAction = fileActions.get('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'];
  366. testAction('Test.txt', context);
  367. expect(actionStub.calledOnce).toEqual(true);
  368. expect(actionStub.getCall(0).args[0]).toEqual('Test.txt');
  369. expect(actionStub.getCall(0).args[1]).toBe(context);
  370. expect(notifyUpdateListenersSpy.calledTwice).toEqual(true);
  371. expect(notifyUpdateListenersSpy.calledBefore(actionStub)).toEqual(true);
  372. expect(notifyUpdateListenersSpy.calledAfter(actionStub)).toEqual(true);
  373. expect(notifyUpdateListenersSpy.getCall(0).args[0]).toEqual('beforeTriggerAction');
  374. expect(notifyUpdateListenersSpy.getCall(0).args[1]).toEqual({
  375. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  376. fileName: 'Test.txt',
  377. context: context
  378. });
  379. expect(notifyUpdateListenersSpy.getCall(1).args[0]).toEqual('afterTriggerAction');
  380. expect(notifyUpdateListenersSpy.getCall(1).args[1]).toEqual({
  381. action: fileActions.getActions('all', OCA.Files.FileActions.TYPE_INLINE, OC.PERMISSION_READ)['Test'],
  382. fileName: 'Test.txt',
  383. context: context
  384. });
  385. }),
  386. describe('actions menu', function() {
  387. it('shows actions menu inside row when clicking the menu trigger', function() {
  388. expect($tr.find('td.filename .fileActionsMenu').length).toEqual(0);
  389. $tr.find('.action-menu').click();
  390. expect($tr.find('td.filename .fileActionsMenu').length).toEqual(1);
  391. });
  392. it('shows highlight on current row', function() {
  393. $tr.find('.action-menu').click();
  394. expect($tr.hasClass('mouseOver')).toEqual(true);
  395. });
  396. it('cleans up after hiding', function() {
  397. var slideUpStub = sinon.stub($.fn, 'slideUp');
  398. $tr.find('.action-menu').click();
  399. expect($tr.find('.fileActionsMenu').length).toEqual(1);
  400. OC.hideMenus();
  401. // sliding animation
  402. expect(slideUpStub.calledOnce).toEqual(true);
  403. slideUpStub.getCall(0).args[1]();
  404. expect($tr.hasClass('mouseOver')).toEqual(false);
  405. expect($tr.find('.fileActionsMenu').length).toEqual(0);
  406. });
  407. });
  408. });
  409. describe('custom rendering', function() {
  410. var $tr;
  411. beforeEach(function() {
  412. var fileData = {
  413. id: 18,
  414. type: 'file',
  415. name: 'testName.txt',
  416. mimetype: 'text/plain',
  417. size: '1234',
  418. etag: 'a01234c',
  419. mtime: '123456'
  420. };
  421. $tr = fileList.add(fileData);
  422. });
  423. it('regular function', function() {
  424. var actionStub = sinon.stub();
  425. fileActions.registerAction({
  426. name: 'Test',
  427. displayName: '',
  428. mime: 'all',
  429. type: OCA.Files.FileActions.TYPE_INLINE,
  430. permissions: OC.PERMISSION_READ,
  431. render: function(actionSpec, isDefault, context) {
  432. expect(actionSpec.name).toEqual('Test');
  433. expect(actionSpec.displayName).toEqual('');
  434. expect(actionSpec.permissions).toEqual(OC.PERMISSION_READ);
  435. expect(actionSpec.mime).toEqual('all');
  436. expect(isDefault).toEqual(false);
  437. expect(context.fileList).toEqual(fileList);
  438. expect(context.$file[0]).toEqual($tr[0]);
  439. var $customEl = $('<a class="action action-test" href="#"><span>blabli</span><span>blabla</span></a>');
  440. $tr.find('td:first').append($customEl);
  441. return $customEl;
  442. },
  443. actionHandler: actionStub
  444. });
  445. fileActions.display($tr.find('td.filename'), true, fileList);
  446. var $actionEl = $tr.find('td:first .action-test');
  447. expect($actionEl.length).toEqual(1);
  448. expect($actionEl.hasClass('action')).toEqual(true);
  449. $actionEl.click();
  450. expect(actionStub.calledOnce).toEqual(true);
  451. expect(actionStub.getCall(0).args[0]).toEqual('testName.txt');
  452. });
  453. });
  454. describe('merging', function() {
  455. var $tr;
  456. beforeEach(function() {
  457. var fileData = {
  458. id: 18,
  459. type: 'file',
  460. name: 'testName.txt',
  461. path: '/anotherpath/there',
  462. mimetype: 'text/plain',
  463. size: '1234',
  464. etag: 'a01234c',
  465. mtime: '123456'
  466. };
  467. $tr = fileList.add(fileData);
  468. });
  469. afterEach(function() {
  470. $tr = null;
  471. });
  472. it('copies all actions to target file actions', function() {
  473. var actions1 = new OCA.Files.FileActions();
  474. var actions2 = new OCA.Files.FileActions();
  475. var actionStub1 = sinon.stub();
  476. var actionStub2 = sinon.stub();
  477. actions1.registerAction({
  478. name: 'Test',
  479. type: OCA.Files.FileActions.TYPE_INLINE,
  480. mime: 'all',
  481. permissions: OC.PERMISSION_READ,
  482. icon: OC.imagePath('core', 'actions/test'),
  483. actionHandler: actionStub1
  484. });
  485. actions2.registerAction({
  486. name: 'Test2',
  487. type: OCA.Files.FileActions.TYPE_INLINE,
  488. mime: 'all',
  489. permissions: OC.PERMISSION_READ,
  490. icon: OC.imagePath('core', 'actions/test'),
  491. actionHandler: actionStub2
  492. });
  493. actions2.merge(actions1);
  494. actions2.display($tr.find('td.filename'), true, fileList);
  495. expect($tr.find('.action-test').length).toEqual(1);
  496. expect($tr.find('.action-test2').length).toEqual(1);
  497. $tr.find('.action-test').click();
  498. expect(actionStub1.calledOnce).toEqual(true);
  499. expect(actionStub2.notCalled).toEqual(true);
  500. actionStub1.reset();
  501. $tr.find('.action-test2').click();
  502. expect(actionStub1.notCalled).toEqual(true);
  503. expect(actionStub2.calledOnce).toEqual(true);
  504. });
  505. it('overrides existing actions on merge', function() {
  506. var actions1 = new OCA.Files.FileActions();
  507. var actions2 = new OCA.Files.FileActions();
  508. var actionStub1 = sinon.stub();
  509. var actionStub2 = sinon.stub();
  510. actions1.registerAction({
  511. name: 'Test',
  512. type: OCA.Files.FileActions.TYPE_INLINE,
  513. mime: 'all',
  514. permissions: OC.PERMISSION_READ,
  515. icon: OC.imagePath('core', 'actions/test'),
  516. actionHandler: actionStub1
  517. });
  518. actions2.registerAction({
  519. name: 'Test', // override
  520. mime: 'all',
  521. type: OCA.Files.FileActions.TYPE_INLINE,
  522. permissions: OC.PERMISSION_READ,
  523. icon: OC.imagePath('core', 'actions/test'),
  524. actionHandler: actionStub2
  525. });
  526. actions1.merge(actions2);
  527. actions1.display($tr.find('td.filename'), true, fileList);
  528. expect($tr.find('.action-test').length).toEqual(1);
  529. $tr.find('.action-test').click();
  530. expect(actionStub1.notCalled).toEqual(true);
  531. expect(actionStub2.calledOnce).toEqual(true);
  532. });
  533. it('overrides existing action when calling register after merge', function() {
  534. var actions1 = new OCA.Files.FileActions();
  535. var actions2 = new OCA.Files.FileActions();
  536. var actionStub1 = sinon.stub();
  537. var actionStub2 = sinon.stub();
  538. actions1.registerAction({
  539. mime: 'all',
  540. name: 'Test',
  541. type: OCA.Files.FileActions.TYPE_INLINE,
  542. permissions: OC.PERMISSION_READ,
  543. icon: OC.imagePath('core', 'actions/test'),
  544. actionHandler: actionStub1
  545. });
  546. actions1.merge(actions2);
  547. // late override
  548. actions1.registerAction({
  549. mime: 'all',
  550. name: 'Test', // override
  551. type: OCA.Files.FileActions.TYPE_INLINE,
  552. permissions: OC.PERMISSION_READ,
  553. icon: OC.imagePath('core', 'actions/test'),
  554. actionHandler: actionStub2
  555. });
  556. actions1.display($tr.find('td.filename'), true, fileList);
  557. expect($tr.find('.action-test').length).toEqual(1);
  558. $tr.find('.action-test').click();
  559. expect(actionStub1.notCalled).toEqual(true);
  560. expect(actionStub2.calledOnce).toEqual(true);
  561. });
  562. it('leaves original file actions untouched (clean copy)', function() {
  563. var actions1 = new OCA.Files.FileActions();
  564. var actions2 = new OCA.Files.FileActions();
  565. var actionStub1 = sinon.stub();
  566. var actionStub2 = sinon.stub();
  567. actions1.registerAction({
  568. mime: 'all',
  569. name: 'Test',
  570. type: OCA.Files.FileActions.TYPE_INLINE,
  571. permissions: OC.PERMISSION_READ,
  572. icon: OC.imagePath('core', 'actions/test'),
  573. actionHandler: actionStub1
  574. });
  575. // copy the Test action to actions2
  576. actions2.merge(actions1);
  577. // late override
  578. actions2.registerAction({
  579. mime: 'all',
  580. name: 'Test', // override
  581. type: OCA.Files.FileActions.TYPE_INLINE,
  582. permissions: OC.PERMISSION_READ,
  583. icon: OC.imagePath('core', 'actions/test'),
  584. actionHandler: actionStub2
  585. });
  586. // check if original actions still call the correct handler
  587. actions1.display($tr.find('td.filename'), true, fileList);
  588. expect($tr.find('.action-test').length).toEqual(1);
  589. $tr.find('.action-test').click();
  590. expect(actionStub1.calledOnce).toEqual(true);
  591. expect(actionStub2.notCalled).toEqual(true);
  592. });
  593. });
  594. describe('events', function() {
  595. var clock;
  596. beforeEach(function() {
  597. clock = sinon.useFakeTimers();
  598. });
  599. afterEach(function() {
  600. clock.restore();
  601. });
  602. it('notifies update event handlers once after multiple changes', function() {
  603. var actionStub = sinon.stub();
  604. var handler = sinon.stub();
  605. fileActions.on('registerAction', handler);
  606. fileActions.registerAction({
  607. mime: 'all',
  608. name: 'Test',
  609. type: OCA.Files.FileActions.TYPE_INLINE,
  610. permissions: OC.PERMISSION_READ,
  611. icon: OC.imagePath('core', 'actions/test'),
  612. actionHandler: actionStub
  613. });
  614. fileActions.registerAction({
  615. mime: 'all',
  616. name: 'Test2',
  617. permissions: OC.PERMISSION_READ,
  618. icon: OC.imagePath('core', 'actions/test'),
  619. actionHandler: actionStub
  620. });
  621. expect(handler.calledTwice).toEqual(true);
  622. });
  623. it('does not notifies update event handlers after unregistering', function() {
  624. var actionStub = sinon.stub();
  625. var handler = sinon.stub();
  626. fileActions.on('registerAction', handler);
  627. fileActions.off('registerAction', handler);
  628. fileActions.registerAction({
  629. mime: 'all',
  630. name: 'Test',
  631. type: OCA.Files.FileActions.TYPE_INLINE,
  632. permissions: OC.PERMISSION_READ,
  633. icon: OC.imagePath('core', 'actions/test'),
  634. actionHandler: actionStub
  635. });
  636. fileActions.registerAction({
  637. mime: 'all',
  638. name: 'Test2',
  639. type: OCA.Files.FileActions.TYPE_INLINE,
  640. permissions: OC.PERMISSION_READ,
  641. icon: OC.imagePath('core', 'actions/test'),
  642. actionHandler: actionStub
  643. });
  644. expect(handler.notCalled).toEqual(true);
  645. });
  646. });
  647. describe('default actions', function() {
  648. describe('download', function() {
  649. it('redirects to URL and sets busy state to list', function() {
  650. var handleDownloadStub = sinon.stub(OCA.Files.Files, 'handleDownload');
  651. var busyStub = sinon.stub(fileList, 'showFileBusyState');
  652. var fileData = {
  653. id: 18,
  654. type: 'file',
  655. name: 'testName.txt',
  656. mimetype: 'text/plain',
  657. size: '1234',
  658. etag: 'a01234c',
  659. mtime: '123456',
  660. permissions: OC.PERMISSION_READ | OC.PERMISSION_UPDATE
  661. };
  662. // note: FileActions.display() is called implicitly
  663. fileList.add(fileData);
  664. var model = fileList.getModelForFile('testName.txt');
  665. fileActions.registerDefaultActions();
  666. fileActions.triggerAction('Download', model, fileList);
  667. expect(busyStub.calledOnce).toEqual(true);
  668. expect(busyStub.calledWith('testName.txt', true)).toEqual(true);
  669. expect(handleDownloadStub.calledOnce).toEqual(true);
  670. expect(handleDownloadStub.getCall(0).args[0]).toEqual(
  671. OC.getRootPath() + '/remote.php/webdav/subdir/testName.txt'
  672. );
  673. busyStub.reset();
  674. handleDownloadStub.yield();
  675. expect(busyStub.calledOnce).toEqual(true);
  676. expect(busyStub.calledWith('testName.txt', false)).toEqual(true);
  677. busyStub.restore();
  678. handleDownloadStub.restore();
  679. });
  680. });
  681. });
  682. describe('download spinner', function() {
  683. var FileActions = OCA.Files.FileActions;
  684. var $el;
  685. beforeEach(function() {
  686. $el = $('<a href="#"><span class="icon icon-download"></span><span>Download</span></a>');
  687. });
  688. it('replaces download icon with spinner', function() {
  689. FileActions.updateFileActionSpinner($el, true);
  690. expect($el.find('.icon.icon-loading-small').length).toEqual(1);
  691. expect($el.find('.icon.icon-download').hasClass('hidden')).toEqual(true);
  692. });
  693. it('replaces spinner back with download icon with spinner', function() {
  694. FileActions.updateFileActionSpinner($el, true);
  695. FileActions.updateFileActionSpinner($el, false);
  696. expect($el.find('.icon.icon-loading-small').length).toEqual(0);
  697. expect($el.find('.icon.icon-download').hasClass('hidden')).toEqual(false);
  698. });
  699. });
  700. });