1
0

mainfileinfodetailviewSpec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * @copyright 2015 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  6. * @author Michael Jobst <mjobst+github@tecratech.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Raghu Nayyar <hey@raghunayyar.com>
  9. * @author Vincent Petry <vincent@nextcloud.com>
  10. *
  11. * @license AGPL-3.0-or-later
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. describe('OCA.Files.MainFileInfoDetailView tests', function() {
  28. var view, tooltipStub, fileActions, fileList, testFileInfo;
  29. beforeEach(function() {
  30. tooltipStub = sinon.stub($.fn, 'tooltip');
  31. fileActions = new OCA.Files.FileActions();
  32. fileList = new OCA.Files.FileList($('<table></table>'), {
  33. fileActions: fileActions
  34. });
  35. view = new OCA.Files.MainFileInfoDetailView({
  36. fileList: fileList,
  37. fileActions: fileActions
  38. });
  39. testFileInfo = new OCA.Files.FileInfoModel({
  40. id: 5,
  41. name: 'One.txt',
  42. mimetype: 'text/plain',
  43. permissions: 31,
  44. path: '/subdir',
  45. size: 123456789,
  46. etag: 'abcdefg',
  47. mtime: Date.UTC(2015, 6, 17, 1, 2, 0, 0)
  48. });
  49. });
  50. afterEach(function() {
  51. view.remove();
  52. view = undefined;
  53. tooltipStub.restore();
  54. });
  55. describe('rendering', function() {
  56. it('displays basic info', function() {
  57. var clock = sinon.useFakeTimers(Date.UTC(2015, 6, 17, 1, 2, 0, 3));
  58. var dateExpected = OC.Util.formatDate(Date(Date.UTC(2015, 6, 17, 1, 2, 0, 0)));
  59. view.setFileInfo(testFileInfo);
  60. expect(view.$el.find('.fileName h3').text()).toEqual('One.txt');
  61. expect(view.$el.find('.fileName h3').attr('title')).toEqual('One.txt');
  62. expect(view.$el.find('.size').text()).toEqual('117.7 MB');
  63. expect(view.$el.find('.size').attr('title')).toEqual('123456789 bytes');
  64. expect(view.$el.find('.date').text()).toEqual('seconds ago');
  65. expect(view.$el.find('.date').attr('title')).toEqual(dateExpected);
  66. clock.restore();
  67. });
  68. it('displays permalink', function() {
  69. view.setFileInfo(testFileInfo);
  70. expect(view.$el.find('.permalink').attr('href'))
  71. .toEqual(OC.getProtocol() + '://' + OC.getHost() + OC.generateUrl('/f/5'));
  72. });
  73. it('displays favorite icon', function() {
  74. fileActions.registerAction({
  75. name: 'Favorite',
  76. mime: 'all',
  77. permissions: OC.PERMISSION_NONE
  78. });
  79. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  80. view.setFileInfo(testFileInfo);
  81. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(true);
  82. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(false);
  83. testFileInfo.set('tags', []);
  84. view.setFileInfo(testFileInfo);
  85. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(false);
  86. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(true);
  87. });
  88. it('does not display favorite icon if favorite action is not available', function() {
  89. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  90. view.setFileInfo(testFileInfo);
  91. expect(view.$el.find('.action-favorite').length).toEqual(0);
  92. testFileInfo.set('tags', []);
  93. view.setFileInfo(testFileInfo);
  94. expect(view.$el.find('.action-favorite').length).toEqual(0);
  95. });
  96. it('displays mime icon', function() {
  97. // File
  98. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  99. testFileInfo.set('mimetype', 'text/calendar');
  100. view.setFileInfo(testFileInfo);
  101. expect(lazyLoadPreviewStub.calledOnce).toEqual(true);
  102. var previewArgs = lazyLoadPreviewStub.getCall(0).args;
  103. expect(previewArgs[0].mime).toEqual('text/calendar');
  104. expect(previewArgs[0].path).toEqual('/subdir/One.txt');
  105. expect(previewArgs[0].etag).toEqual('abcdefg');
  106. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  107. // returns mime icon first without img parameter
  108. previewArgs[0].callback(
  109. OC.imagePath('core', 'filetypes/text-calendar.svg')
  110. );
  111. // still loading
  112. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  113. // preview loading failed, no prview
  114. previewArgs[0].error();
  115. // loading stopped, the mimetype icon gets displayed
  116. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  117. expect(view.$el.find('.thumbnail').css('background-image'))
  118. .toContain('filetypes/text-calendar.svg');
  119. // Folder
  120. testFileInfo.set('mimetype', 'httpd/unix-directory');
  121. view.setFileInfo(testFileInfo);
  122. expect(view.$el.find('.thumbnail').css('background-image'))
  123. .toContain('filetypes/folder.svg');
  124. lazyLoadPreviewStub.restore();
  125. });
  126. it('uses icon from model if present in model', function() {
  127. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  128. testFileInfo.set('mimetype', 'httpd/unix-directory');
  129. testFileInfo.set('icon', OC.MimeType.getIconUrl('dir-external'));
  130. view.setFileInfo(testFileInfo);
  131. expect(lazyLoadPreviewStub.notCalled).toEqual(true);
  132. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  133. expect(view.$el.find('.thumbnail').css('background-image'))
  134. .toContain('filetypes/folder-external.svg');
  135. lazyLoadPreviewStub.restore();
  136. });
  137. it('displays thumbnail', function() {
  138. var lazyLoadPreviewStub = sinon.stub(fileList, 'lazyLoadPreview');
  139. testFileInfo.set('mimetype', 'text/plain');
  140. view.setFileInfo(testFileInfo);
  141. expect(lazyLoadPreviewStub.calledOnce).toEqual(true);
  142. var previewArgs = lazyLoadPreviewStub.getCall(0).args;
  143. expect(previewArgs[0].mime).toEqual('text/plain');
  144. expect(previewArgs[0].path).toEqual('/subdir/One.txt');
  145. expect(previewArgs[0].etag).toEqual('abcdefg');
  146. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  147. // returns mime icon first without img parameter
  148. previewArgs[0].callback(
  149. OC.imagePath('core', 'filetypes/text-plain.svg')
  150. );
  151. // still loading
  152. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(true);
  153. // return an actual (simulated) image
  154. previewArgs[0].callback(
  155. 'testimage', {
  156. width: 100,
  157. height: 200
  158. }
  159. );
  160. // loading stopped, image got displayed
  161. expect(view.$el.find('.thumbnail').css('background-image'))
  162. .toContain('testimage');
  163. expect(view.$el.find('.thumbnail').hasClass('icon-loading')).toEqual(false);
  164. lazyLoadPreviewStub.restore();
  165. });
  166. it('does not show size if no size available', function() {
  167. testFileInfo.unset('size');
  168. view.setFileInfo(testFileInfo);
  169. expect(view.$el.find('.size').length).toEqual(0);
  170. });
  171. it('renders displayName instead of name if available', function() {
  172. testFileInfo.set('displayName', 'hello.txt');
  173. view.setFileInfo(testFileInfo);
  174. expect(view.$el.find('.fileName h3').text()).toEqual('hello.txt');
  175. expect(view.$el.find('.fileName h3').attr('title')).toEqual('hello.txt');
  176. });
  177. it('rerenders when changes are made on the model', function() {
  178. // Show the "Favorite" icon
  179. fileActions.registerAction({
  180. name: 'Favorite',
  181. mime: 'all',
  182. permissions: OC.PERMISSION_NONE
  183. });
  184. view.setFileInfo(testFileInfo);
  185. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  186. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(true);
  187. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(false);
  188. testFileInfo.set('tags', []);
  189. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(false);
  190. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(true);
  191. });
  192. it('unbinds change listener from model', function() {
  193. // Show the "Favorite" icon
  194. fileActions.registerAction({
  195. name: 'Favorite',
  196. mime: 'all',
  197. permissions: OC.PERMISSION_NONE
  198. });
  199. view.setFileInfo(testFileInfo);
  200. view.setFileInfo(new OCA.Files.FileInfoModel({
  201. id: 999,
  202. name: 'test.txt',
  203. path: '/'
  204. }));
  205. // set value on old model
  206. testFileInfo.set('tags', [OC.TAG_FAVORITE]);
  207. // no change
  208. expect(view.$el.find('.action-favorite > span').hasClass('icon-starred')).toEqual(false);
  209. expect(view.$el.find('.action-favorite > span').hasClass('icon-star')).toEqual(true);
  210. });
  211. });
  212. describe('events', function() {
  213. it('triggers default action when clicking on the thumbnail', function() {
  214. var actionHandler = sinon.stub();
  215. fileActions.registerAction({
  216. name: 'Something',
  217. mime: 'all',
  218. permissions: OC.PERMISSION_READ,
  219. actionHandler: actionHandler
  220. });
  221. fileActions.setDefault('text/plain', 'Something');
  222. view.setFileInfo(testFileInfo);
  223. view.$el.find('.thumbnail').click();
  224. expect(actionHandler.calledOnce).toEqual(true);
  225. expect(actionHandler.getCall(0).args[0]).toEqual('One.txt');
  226. expect(actionHandler.getCall(0).args[1].fileList).toEqual(fileList);
  227. expect(actionHandler.getCall(0).args[1].fileActions).toEqual(fileActions);
  228. expect(actionHandler.getCall(0).args[1].fileInfoModel).toEqual(testFileInfo);
  229. });
  230. it('triggers "Favorite" action when clicking on the star', function() {
  231. var actionHandler = sinon.stub();
  232. fileActions.registerAction({
  233. name: 'Favorite',
  234. mime: 'all',
  235. permissions: OC.PERMISSION_READ,
  236. actionHandler: actionHandler
  237. });
  238. view.setFileInfo(testFileInfo);
  239. view.$el.find('.action-favorite').click();
  240. expect(actionHandler.calledOnce).toEqual(true);
  241. expect(actionHandler.getCall(0).args[0]).toEqual('One.txt');
  242. expect(actionHandler.getCall(0).args[1].fileList).toEqual(fileList);
  243. expect(actionHandler.getCall(0).args[1].fileActions).toEqual(fileActions);
  244. expect(actionHandler.getCall(0).args[1].fileInfoModel).toEqual(testFileInfo);
  245. });
  246. });
  247. });