diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2015-06-03 14:52:00 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2015-07-06 16:32:10 +0200 |
commit | c8145cdbd6160788569d00fbc267abd08ae019c9 (patch) | |
tree | 9757cbbf9ef2fd42074522c7b85124427f447224 /core/js/tests | |
parent | 14eef434fff78bf39a926b1f27220b9ad1ebb833 (diff) | |
download | nextcloud-server-c8145cdbd6160788569d00fbc267abd08ae019c9.tar.gz nextcloud-server-c8145cdbd6160788569d00fbc267abd08ae019c9.zip |
Javascript mimetype icon resolver
This makes it possible to retrieve the icon for mimetypes in javascript.
It makes no additional queries to the server to retrieve the mimetype.
* config/mimetypealiases.json added
* mimetype.js: this is where the logic resides to convert from mimetype
to icon url
* mimetypelist.js: generated file with a list of mimetype mapping (aliases)
and the list of icon files
* ./occ maintenance:mimetypesjs : new command for occ to gernerate
mimetypes.js
* unit tests updated and still work
* javascript tests added
* theming support
* folder of the theme is now present in javascript (OC.theme.folder)
Diffstat (limited to 'core/js/tests')
-rw-r--r-- | core/js/tests/specs/mimeTypeSpec.js | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/core/js/tests/specs/mimeTypeSpec.js b/core/js/tests/specs/mimeTypeSpec.js new file mode 100644 index 00000000000..182941de1a9 --- /dev/null +++ b/core/js/tests/specs/mimeTypeSpec.js @@ -0,0 +1,151 @@ +/** + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +describe('MimeType tests', function() { + var _files; + var _aliases; + var _theme; + + beforeEach(function() { + _files = OC.MimeTypeList.files; + _aliases = OC.MimeTypeList.aliases; + _theme = OC.MimeTypeList.themes['abc']; + + OC.MimeTypeList.files = ['folder', 'folder-shared', 'folder-external', 'foo-bar', 'foo', 'file']; + OC.MimeTypeList.aliases = {'app/foobar': 'foo/bar'}; + OC.MimeTypeList.themes['abc'] = ['folder']; + }); + + afterEach(function() { + OC.MimeTypeList.files = _files; + OC.MimeTypeList.aliases = _aliases; + OC.MimeTypeList.themes['abc'] = _theme; + }); + + describe('_getFile', function() { + + it('returns the correct icon for "dir"', function() { + var res = OC.MimeType._getFile('dir', OC.MimeTypeList.files); + expect(res).toEqual('folder'); + }); + + it('returns the correct icon for "dir-shared"', function() { + var res = OC.MimeType._getFile('dir-shared', OC.MimeTypeList.files); + expect(res).toEqual('folder-shared'); + }); + + it('returns the correct icon for "dir-external"', function() { + var res = OC.MimeType._getFile('dir-external', OC.MimeTypeList.files); + expect(res).toEqual('folder-external'); + }); + + it('returns the correct icon for a mimetype for which we have an icon', function() { + var res = OC.MimeType._getFile('foo/bar', OC.MimeTypeList.files); + expect(res).toEqual('foo-bar'); + }); + + it('returns the correct icon for a mimetype for which we only have a general mimetype icon', function() { + var res = OC.MimeType._getFile('foo/baz', OC.MimeTypeList.files); + expect(res).toEqual('foo'); + }); + + it('return the file mimetype if we have no matching icon but do have a file icon', function() { + var res = OC.MimeType._getFile('foobar', OC.MimeTypeList.files); + expect(res).toEqual('file'); + }); + + it('return null if we do not have a matching icon', function() { + var res = OC.MimeType._getFile('xyz', []); + expect(res).toEqual(null); + }); + }); + + describe('getIconUrl', function() { + + describe('no theme', function() { + var _themeFolder; + + beforeEach(function() { + _themeFolder = OC.theme.folder; + OC.theme.folder = ''; + //Clear mimetypeIcons caches + OC.MimeType._mimeTypeIcons = {}; + }); + + afterEach(function() { + OC.theme.folder = _themeFolder; + }); + + it('return undefined if the an icon for undefined is requested', function() { + var res = OC.MimeType.getIconUrl(undefined); + expect(res).toEqual(undefined); + }); + + it('return the url for the mimetype file', function() { + var res = OC.MimeType.getIconUrl('file'); + expect(res).toEqual(OC.webroot + '/core/img/filetypes/file.svg'); + }); + + it('test if the cache works correctly', function() { + OC.MimeType._mimeTypeIcons = {}; + expect(Object.keys(OC.MimeType._mimeTypeIcons).length).toEqual(0); + + var res = OC.MimeType.getIconUrl('dir'); + expect(Object.keys(OC.MimeType._mimeTypeIcons).length).toEqual(1); + expect(OC.MimeType._mimeTypeIcons['dir']).toEqual(res); + + var res = OC.MimeType.getIconUrl('dir-shared'); + expect(Object.keys(OC.MimeType._mimeTypeIcons).length).toEqual(2); + expect(OC.MimeType._mimeTypeIcons['dir-shared']).toEqual(res); + }); + + it('test if alaiases are converted correctly', function() { + var res = OC.MimeType.getIconUrl('app/foobar'); + expect(res).toEqual(OC.webroot + '/core/img/filetypes/foo-bar.svg'); + expect(OC.MimeType._mimeTypeIcons['foo/bar']).toEqual(res); + }); + }); + + describe('themes', function() { + var _themeFolder; + + beforeEach(function() { + _themeFolder = OC.theme.folder; + OC.theme.folder = 'abc'; + //Clear mimetypeIcons caches + OC.MimeType._mimeTypeIcons = {}; + }); + + afterEach(function() { + OC.theme.folder = _themeFolder; + }); + + it('test if theme path is used if a theme icon is availble', function() { + var res = OC.MimeType.getIconUrl('dir'); + expect(res).toEqual(OC.webroot + '/themes/abc/core/img/filetypes/folder.svg'); + }); + + it('test if we fallback to the default theme if no icon is available in the theme', function() { + var res = OC.MimeType.getIconUrl('dir-shared'); + expect(res).toEqual(OC.webroot + '/core/img/filetypes/folder-shared.svg'); + }); + }); + }); +}); |