tests: Add an helper "before and after" method for the layouts

This will avoid a lot of copy paste as the
remove/set/swap/insert/add/remove_widgets/swap_widgets code
is identical beside the method name.
This commit is contained in:
Emmanuel Lepage Vallee 2016-08-21 02:57:32 -04:00
parent 280973c9cb
commit 6990cc15dc
1 changed files with 59 additions and 3 deletions

View File

@ -5,14 +5,15 @@ local wibox = require( "wibox" )
local surface = require( "gears.surface" )
local color = require( "gears.color" )
local beautiful = require( "beautiful" )
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
-- Create a generic rectangle widget to show layout disposition
local function generic_widget(text)
local function generic_widget(text, col)
return {
{
{
draw = function(_, _, cr, width, height)
cr:set_source(color(beautiful.bg_normal))
cr:set_source(color(col or beautiful.bg_normal))
cr:set_line_width(3)
cr:rectangle(0, 0, width, height)
cr:fill_preserve()
@ -34,8 +35,63 @@ local function generic_widget(text)
}
end
local names = {
"first" ,
"second" ,
"third" ,
"fourth" ,
"fifth" ,
"sixth" ,
"seventh",
"eighth" ,
"ninth" ,
}
-- Generic template to create "before and after" for layout mutators
local function generic_before_after(layout, layout_args, count, method, method_args)
local ls = {}
-- Create the layouts
for i=1, 2 do
local args = {layout = layout}
-- In case the layout change the array (it is technically not forbidden)
for k,v in pairs(layout_args) do
args[k] = v
end
local l = wibox.layout(args)
for j=1, count or 3 do
l:add(wibox.widget(generic_widget(names[j] or "N/A")))
end
ls[i] = l
end
-- Mutate
if type(method) == "function" then
method(ls[2], unpack(method_args or {}))
else
ls[2][method](ls[2], unpack(method_args or {}))
end
return wibox.layout {
{
markup = "<b>Before:</b>",
widget = wibox.widget.textbox,
},
ls[1],
{
markup = "<b>After:</b>",
widget = wibox.widget.textbox,
},
ls[2],
layout = wibox.layout.fixed.vertical
}
end
-- Let the test request a size and file format
local widget, w, h = loadfile(file_path)(generic_widget)
local widget, w, h = loadfile(file_path)(generic_widget, generic_before_after)
-- Emulate the event loop for 10 iterations
for _ = 1, 10 do