diff --git a/lib/menubar/utils.lua b/lib/menubar/utils.lua index c27280b4..2713cc72 100644 --- a/lib/menubar/utils.lua +++ b/lib/menubar/utils.lua @@ -128,19 +128,7 @@ local all_icon_sizes = { } --- List of supported icon formats. -local icon_formats = { "png", "xpm", "svg" } - ---- Check whether the icon format is supported. --- @param icon_file Filename of the icon. --- @return true if format is supported, false otherwise. -local function is_format_supported(icon_file) - for _, f in ipairs(icon_formats) do - if icon_file:match('%.' .. f) then - return true - end - end - return false -end +local supported_icon_formats = { png = 1, xpm = 2, svg = 3 } local icon_lookup_path = nil --- Get a list of icon lookup paths. @@ -226,21 +214,23 @@ function utils.lookup_icon_uncached(icon_file) return false end - if icon_file:sub(1, 1) == '/' and is_format_supported(icon_file) then + local icon_file_ext = icon_file:match(".*%.(.-)$") + if icon_file:sub(1, 1) == '/' and supported_icon_formats[icon_file_ext] then -- If the path to the icon is absolute and its format is -- supported, do not perform a lookup. return gfs.file_readable(icon_file) and icon_file or nil else for _, directory in ipairs(get_icon_lookup_path()) do - if is_format_supported(icon_file) and - gfs.file_readable(directory .. "/" .. icon_file) then - return directory .. "/" .. icon_file + local directory_file = directory .. "/" .. icon_file + if supported_icon_formats[icon_file_ext] and + gfs.file_readable(directory_file) then + return directory_file else -- Icon is probably specified without path and format, -- like 'firefox'. Try to add supported extensions to -- it and see if such file exists. - for _, format in ipairs(icon_formats) do - local possible_file = directory .. "/" .. icon_file .. "." .. format + for format, _ in pairs(supported_icon_formats) do + local possible_file = directory_file .. "." .. format if gfs.file_readable(possible_file) then return possible_file end diff --git a/spec/menubar/home/.icons/icon6.xpm b/spec/menubar/home/.icons/icon6.xpm new file mode 100644 index 00000000..7017a0fe --- /dev/null +++ b/spec/menubar/home/.icons/icon6.xpm @@ -0,0 +1 @@ +../../../../icons/awesome16.png diff --git a/spec/menubar/home/.icons/icon7.svg b/spec/menubar/home/.icons/icon7.svg new file mode 100644 index 00000000..7017a0fe --- /dev/null +++ b/spec/menubar/home/.icons/icon7.svg @@ -0,0 +1 @@ +../../../../icons/awesome16.png diff --git a/spec/menubar/usr/share/icon5.png b/spec/menubar/usr/share/icon5.png new file mode 100644 index 00000000..7017a0fe --- /dev/null +++ b/spec/menubar/usr/share/icon5.png @@ -0,0 +1 @@ +../../../../icons/awesome16.png diff --git a/spec/menubar/usr/share/icon6.xpm b/spec/menubar/usr/share/icon6.xpm new file mode 100644 index 00000000..7017a0fe --- /dev/null +++ b/spec/menubar/usr/share/icon6.xpm @@ -0,0 +1 @@ +../../../../icons/awesome16.png diff --git a/spec/menubar/usr/share/icon7.svg b/spec/menubar/usr/share/icon7.svg new file mode 100644 index 00000000..7017a0fe --- /dev/null +++ b/spec/menubar/usr/share/icon7.svg @@ -0,0 +1 @@ +../../../../icons/awesome16.png diff --git a/spec/menubar/utils_spec.lua b/spec/menubar/utils_spec.lua index da4c8000..aca4c575 100644 --- a/spec/menubar/utils_spec.lua +++ b/spec/menubar/utils_spec.lua @@ -6,9 +6,12 @@ local utils = require("menubar.utils") local theme = require("beautiful") local glib = require("lgi").GLib +local gfs = require("gears.filesystem") describe("menubar.utils lookup_icon_uncached", function() local shimmed = {} + local gfs_shim_dir_readable + local gfs_shim_file_readable local icon_theme local function assert_found_in_path(icon, path) @@ -23,13 +26,18 @@ describe("menubar.utils lookup_icon_uncached", function() glib[name] = function() return retval end end - shim('get_home_dir', root .. "/home") - shim('get_user_data_dir', root .. "/home/.local/share") + shim('get_home_dir', "/home") + shim('get_user_data_dir', "/home/.local/share") shim('get_system_data_dirs', { - root .. "/usr/local/share", - root .. "/usr/share" + "/usr/local/share", + "/usr/share" }) + gfs_shim_dir_readable = gfs.dir_readable + gfs.dir_readable = function(path) return gfs_shim_dir_readable(root..path) end + gfs_shim_file_readable = gfs.file_readable + gfs.file_readable = function(filename) return gfs_shim_file_readable(root..filename) end + icon_theme = theme.icon_theme theme.icon_theme = 'awesome' end) @@ -38,6 +46,8 @@ describe("menubar.utils lookup_icon_uncached", function() for name, func in pairs(shimmed) do glib[name] = func end + gfs.dir_readable = gfs_shim_dir_readable + gfs.file_readable = gfs_shim_file_readable theme.icon_theme = icon_theme end) @@ -65,6 +75,66 @@ describe("menubar.utils lookup_icon_uncached", function() assert_found_in_path('awesome', '/.icons/awesome/64x64/apps/awesome.png') assert_found_in_path('awesome2', '/.icons/awesome/scalable/apps/awesome2.png') end) + +-- Check for no argument :: if not icon_file or icon_file == "" then + it('no icon specified', function() + assert.is_false(utils.lookup_icon_uncached()) + assert.is_false(utils.lookup_icon_uncached(nil)) -- Same as above? + assert.is_false(utils.lookup_icon_uncached(false)) + assert.is_false(utils.lookup_icon_uncached('')) + end) + +-- Check for icon not in search path :: if icon_file:sub(1, 1) == '/' and supported_icon_formats[icon_file_ext] then + it('finds icons even those not in the search paths when full path specified', function() + + -- Shimmed icon base directories contain the following icons: + -- + -- usr/share/icon5.png + -- usr/share/icon6.xpm + -- usr/share/icon7.svg + + assert_found_in_path('/usr/share/icon5.png', '/usr/share/icon5.png') + assert_found_in_path('/usr/share/icon6.xpm', '/usr/share/icon6.xpm') + assert_found_in_path('/usr/share/icon7.svg', '/usr/share/icon7.svg') + + assert.is_same(nil, utils.lookup_icon_uncached('/.png')) -- supported file does not exist in location + assert.is_same(nil, utils.lookup_icon_uncached('/blah/icon6.png')) -- supported file does not exist in location + end) + +-- Check icon with specified extension matching :: if supported_icon_formats[icon_file_ext] and + it('finds icons with specified supported extension in search path', function() + + -- Shimmed icon base directories contain the following icons: + -- + -- .icons/icon5.png + -- .icons/icon6.xpm + -- .icons/icon7.svg + + assert_found_in_path('icon5.png', '/.icons/icon5.png') + assert_found_in_path('icon6.xpm', '/.icons/icon6.xpm') + assert_found_in_path('icon7.svg', '/.icons/icon7.svg') + + -- Will fail as file does not exist (but extension is supported) + assert.is_false(utils.lookup_icon_uncached('icon8.png')) + end) + +-- Check icon with no (or invalid) extension matching :: if NOT supported_icon_formats[icon_file_ext] + it('finds icons without specified path or extension', function() + + -- Shimmed icon base directories contain the following icons: + -- + -- .icons/icon6.xpm + -- .icons/icon7.svg + + assert_found_in_path('icon6', '/.icons/icon6.xpm') -- Similar to original tests and testing xpm extension + assert_found_in_path('icon7', '/.icons/icon7.svg') -- Similar to original tests and testing svg extension + + assert.is_false(utils.lookup_icon_uncached('/png')) -- supported file does not exist in given location + assert.is_false(utils.lookup_icon_uncached('.png')) -- file does not exist + assert.is_false(utils.lookup_icon_uncached('png')) -- file does not exist + assert.is_false(utils.lookup_icon_uncached('icon9')) -- file does not exist + end) +--]] end) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80