mimeTypeSpec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @author Roeland Jago Douma <roeland@famdouma.nl>
  3. *
  4. * @copyright Copyright (c) 2015, ownCloud, Inc.
  5. * @license AGPL-3.0
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. describe('MimeType tests', function() {
  21. var _files;
  22. var _aliases;
  23. var _theme;
  24. beforeEach(function() {
  25. _files = OC.MimeTypeList.files;
  26. _aliases = OC.MimeTypeList.aliases;
  27. _theme = OC.MimeTypeList.themes.abc;
  28. OC.MimeTypeList.files = ['folder', 'folder-shared', 'folder-external', 'foo-bar', 'foo', 'file'];
  29. OC.MimeTypeList.aliases = {'app/foobar': 'foo/bar'};
  30. OC.MimeTypeList.themes.abc = ['folder'];
  31. });
  32. afterEach(function() {
  33. OC.MimeTypeList.files = _files;
  34. OC.MimeTypeList.aliases = _aliases;
  35. OC.MimeTypeList.themes.abc = _theme;
  36. });
  37. describe('_getFile', function() {
  38. it('returns the correct icon for "dir"', function() {
  39. var res = OC.MimeType._getFile('dir', OC.MimeTypeList.files);
  40. expect(res).toEqual('folder');
  41. });
  42. it('returns the correct icon for "dir-shared"', function() {
  43. var res = OC.MimeType._getFile('dir-shared', OC.MimeTypeList.files);
  44. expect(res).toEqual('folder-shared');
  45. });
  46. it('returns the correct icon for "dir-external"', function() {
  47. var res = OC.MimeType._getFile('dir-external', OC.MimeTypeList.files);
  48. expect(res).toEqual('folder-external');
  49. });
  50. it('returns the correct icon for a mimetype for which we have an icon', function() {
  51. var res = OC.MimeType._getFile('foo/bar', OC.MimeTypeList.files);
  52. expect(res).toEqual('foo-bar');
  53. });
  54. it('returns the correct icon for a mimetype for which we only have a general mimetype icon', function() {
  55. var res = OC.MimeType._getFile('foo/baz', OC.MimeTypeList.files);
  56. expect(res).toEqual('foo');
  57. });
  58. it('return the file mimetype if we have no matching icon but do have a file icon', function() {
  59. var res = OC.MimeType._getFile('foobar', OC.MimeTypeList.files);
  60. expect(res).toEqual('file');
  61. });
  62. it('return null if we do not have a matching icon', function() {
  63. var res = OC.MimeType._getFile('xyz', []);
  64. expect(res).toEqual(null);
  65. });
  66. });
  67. describe('getIconUrl', function() {
  68. describe('no theme', function() {
  69. var _themeFolder;
  70. beforeEach(function() {
  71. _themeFolder = OC.theme.folder;
  72. OC.theme.folder = '';
  73. //Clear mimetypeIcons caches
  74. OC.MimeType._mimeTypeIcons = {};
  75. });
  76. afterEach(function() {
  77. OC.theme.folder = _themeFolder;
  78. });
  79. it('return undefined if the an icon for undefined is requested', function() {
  80. var res = OC.MimeType.getIconUrl(undefined);
  81. expect(res).toEqual(undefined);
  82. });
  83. it('return the url for the mimetype file', function() {
  84. var res = OC.MimeType.getIconUrl('file');
  85. expect(res).toEqual(OC.webroot + '/core/img/filetypes/file.svg');
  86. });
  87. it('test if the cache works correctly', function() {
  88. OC.MimeType._mimeTypeIcons = {};
  89. expect(Object.keys(OC.MimeType._mimeTypeIcons).length).toEqual(0);
  90. var res = OC.MimeType.getIconUrl('dir');
  91. expect(Object.keys(OC.MimeType._mimeTypeIcons).length).toEqual(1);
  92. expect(OC.MimeType._mimeTypeIcons.dir).toEqual(res);
  93. res = OC.MimeType.getIconUrl('dir-shared');
  94. expect(Object.keys(OC.MimeType._mimeTypeIcons).length).toEqual(2);
  95. expect(OC.MimeType._mimeTypeIcons['dir-shared']).toEqual(res);
  96. });
  97. it('test if alaiases are converted correctly', function() {
  98. var res = OC.MimeType.getIconUrl('app/foobar');
  99. expect(res).toEqual(OC.webroot + '/core/img/filetypes/foo-bar.svg');
  100. expect(OC.MimeType._mimeTypeIcons['foo/bar']).toEqual(res);
  101. });
  102. });
  103. describe('themes', function() {
  104. var _themeFolder;
  105. beforeEach(function() {
  106. _themeFolder = OC.theme.folder;
  107. OC.theme.folder = 'abc';
  108. //Clear mimetypeIcons caches
  109. OC.MimeType._mimeTypeIcons = {};
  110. });
  111. afterEach(function() {
  112. OC.theme.folder = _themeFolder;
  113. });
  114. it('test if theme path is used if a theme icon is availble', function() {
  115. var res = OC.MimeType.getIconUrl('dir');
  116. expect(res).toEqual(OC.webroot + '/themes/abc/core/img/filetypes/folder.svg');
  117. });
  118. it('test if we fallback to the default theme if no icon is available in the theme', function() {
  119. var res = OC.MimeType.getIconUrl('dir-shared');
  120. expect(res).toEqual(OC.webroot + '/core/img/filetypes/folder-shared.svg');
  121. });
  122. });
  123. });
  124. });