fileactionsSpec.js 26 KB

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