2014-08-23 21:57:52 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2014 Uli Schlachter
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local object = require("gears.object")
|
2015-06-14 16:03:29 +02:00
|
|
|
local matrix_equals = require("gears.matrix").equals
|
2015-06-14 15:57:33 +02:00
|
|
|
local base = require("wibox.widget.base")
|
2014-08-23 21:57:52 +02:00
|
|
|
local say = require("say")
|
2015-02-18 12:49:21 +01:00
|
|
|
local assert = require("luassert")
|
2015-09-17 15:01:50 +02:00
|
|
|
local no_parent = base.no_parent_I_know_what_I_am_doing
|
2014-08-23 21:57:52 +02:00
|
|
|
|
2020-03-01 01:47:20 +01:00
|
|
|
_G.awesome.api_level = 9999
|
|
|
|
|
2014-08-23 21:57:52 +02:00
|
|
|
-- {{{ Own widget-based assertions
|
|
|
|
local function widget_fit(state, arguments)
|
|
|
|
if #arguments ~= 3 then
|
2015-06-14 16:03:29 +02:00
|
|
|
error("Have " .. #arguments .. " arguments, but need 3")
|
2014-08-23 21:57:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local widget = arguments[1]
|
|
|
|
local given = arguments[2]
|
|
|
|
local expected = arguments[3]
|
2015-09-17 15:01:50 +02:00
|
|
|
local w, h = base.fit_widget(no_parent, { "fake context" }, widget, given[1], given[2])
|
2014-08-23 21:57:52 +02:00
|
|
|
|
|
|
|
local fits = expected[1] == w and expected[2] == h
|
|
|
|
if state.mod == fits then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
-- For proper error message, mess with the arguments
|
|
|
|
arguments[1] = given[1]
|
|
|
|
arguments[2] = given[2]
|
|
|
|
arguments[3] = expected[1]
|
|
|
|
arguments[4] = expected[2]
|
|
|
|
arguments[5] = w
|
|
|
|
arguments[6] = h
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
say:set("assertion.widget_fit.positive", "Offering (%s, %s) to widget and expected (%s, %s), but got (%s, %s)")
|
|
|
|
assert:register("assertion", "widget_fit", widget_fit, "assertion.widget_fit.positive", "assertion.widget_fit.positive")
|
2015-06-14 16:03:29 +02:00
|
|
|
|
2022-01-08 00:23:19 +01:00
|
|
|
local function times(char, len)
|
|
|
|
local chars = {}
|
|
|
|
for _ = 1, len do
|
|
|
|
table.insert(chars, char)
|
|
|
|
end
|
|
|
|
return table.concat(chars, "")
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Pad the `str` with `len` amount of characters `char`.
|
|
|
|
-- When `is_after == true`, the padding will be appended.
|
|
|
|
-- This allows exceeding the limit of 99 in `string.format`.
|
|
|
|
local function pad_string(str, char, len, is_after)
|
|
|
|
len = len - #str
|
|
|
|
local pad = times(char, len)
|
|
|
|
|
|
|
|
if is_after == true then
|
|
|
|
return str .. pad
|
|
|
|
else
|
|
|
|
return pad .. str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-21 23:51:22 +01:00
|
|
|
local function draw_layout_comparison(actual, expected)
|
|
|
|
local lines = {
|
|
|
|
"Widget layout does not match:\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
local left, right = {}, {}
|
2022-03-15 23:02:42 +01:00
|
|
|
local too_small = false
|
|
|
|
local too_big = false
|
2021-12-21 23:51:22 +01:00
|
|
|
|
|
|
|
local function wrap_line(width, x, line)
|
|
|
|
-- Two columns occupied by the `|` characters
|
|
|
|
local buffer = width - 2 - #line
|
|
|
|
local buffer_left = math.floor(buffer / 2)
|
|
|
|
local buffer_right = buffer - buffer_left
|
|
|
|
|
|
|
|
return string.format(
|
|
|
|
"%s|%s%s%s|",
|
|
|
|
times(" ", x), times(" ", buffer_left), line, times(" ", buffer_right)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function draw_widget(widget)
|
2022-03-15 23:02:42 +01:00
|
|
|
-- We need a minimum size to render the data in.
|
|
|
|
-- One line/column on all sides for the boundary.
|
|
|
|
-- Three lines, 12 columns for the text.
|
|
|
|
local min_size = { 14, 5 }
|
|
|
|
|
|
|
|
if widget._width < min_size[1] or widget._height < min_size[2] then
|
|
|
|
too_small = true
|
|
|
|
elseif widget._width > 25 or widget._height > 25 then
|
|
|
|
too_big = true
|
|
|
|
end
|
|
|
|
|
|
|
|
local width = math.max(min_size[1], widget._width)
|
|
|
|
local height = math.max(min_size[2], widget._height)
|
2021-12-21 23:51:22 +01:00
|
|
|
local x = widget._matrix.x0
|
2022-03-15 23:02:42 +01:00
|
|
|
local y = widget._matrix.y0
|
|
|
|
|
|
|
|
local buffer_top = math.floor(height / 2)
|
|
|
|
local buffer_bottom = height - buffer_top
|
|
|
|
|
|
|
|
local line_start = x + 1
|
|
|
|
local lines_index = y + 1
|
|
|
|
local widget_lines = {}
|
|
|
|
widget_lines[lines_index] = pad_string(times("-", width), " ", line_start)
|
2021-12-21 23:51:22 +01:00
|
|
|
|
|
|
|
for _ = 1, buffer_top do
|
2022-03-15 23:02:42 +01:00
|
|
|
lines_index = lines_index + 1
|
|
|
|
widget_lines[lines_index] = pad_string(
|
|
|
|
wrap_line(width, x, " "),
|
|
|
|
" ",
|
|
|
|
line_start
|
|
|
|
)
|
2021-12-21 23:51:22 +01:00
|
|
|
end
|
|
|
|
|
2022-03-15 23:02:42 +01:00
|
|
|
lines_index = lines_index + 1
|
|
|
|
widget_lines[lines_index] = pad_string(
|
|
|
|
wrap_line(width, x, widget._name or "Widget"),
|
|
|
|
" ",
|
|
|
|
line_start
|
|
|
|
)
|
|
|
|
|
|
|
|
lines_index = lines_index + 1
|
|
|
|
widget_lines[lines_index] = pad_string(
|
|
|
|
wrap_line(width, x, string.format(
|
|
|
|
"Size %d,%d",
|
|
|
|
widget._width, widget._height
|
|
|
|
)),
|
|
|
|
" ",
|
|
|
|
line_start
|
|
|
|
)
|
|
|
|
|
|
|
|
lines_index = lines_index + 1
|
|
|
|
widget_lines[lines_index] = pad_string(
|
|
|
|
wrap_line(width, x, string.format(
|
|
|
|
"Pos %d,%d",
|
|
|
|
widget._matrix.x0, widget._matrix.y0
|
|
|
|
)),
|
|
|
|
" ",
|
|
|
|
line_start
|
|
|
|
)
|
2021-12-21 23:51:22 +01:00
|
|
|
|
|
|
|
for _ = 1, buffer_bottom do
|
2022-03-15 23:02:42 +01:00
|
|
|
lines_index = lines_index + 1
|
|
|
|
widget_lines[lines_index] = pad_string(
|
|
|
|
wrap_line(width, x, " "),
|
|
|
|
" ",
|
|
|
|
line_start
|
|
|
|
)
|
2021-12-21 23:51:22 +01:00
|
|
|
end
|
|
|
|
|
2022-03-15 23:02:42 +01:00
|
|
|
lines_index = lines_index + 1
|
|
|
|
widget_lines[lines_index] = pad_string(times("-", width), " ", line_start)
|
2021-12-21 23:51:22 +01:00
|
|
|
|
2022-03-15 23:02:42 +01:00
|
|
|
return widget_lines, lines_index
|
2021-12-21 23:51:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local max_len = 0
|
2022-03-15 23:02:42 +01:00
|
|
|
local left_len = 0
|
|
|
|
local right_len = 0
|
2021-12-21 23:51:22 +01:00
|
|
|
|
|
|
|
for _, widget in ipairs(actual) do
|
2022-03-15 23:02:42 +01:00
|
|
|
local widget_lines, last_index = draw_widget(widget)
|
|
|
|
left_len = math.max(left_len, last_index)
|
|
|
|
|
|
|
|
for i, line in pairs(widget_lines) do
|
2021-12-21 23:51:22 +01:00
|
|
|
max_len = math.max(max_len, #line)
|
2022-03-15 23:02:42 +01:00
|
|
|
left[i] = line
|
2021-12-21 23:51:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, widget in ipairs(expected) do
|
2022-03-15 23:02:42 +01:00
|
|
|
local widget_lines, last_index = draw_widget(widget)
|
|
|
|
right_len = math.max(right_len, last_index)
|
|
|
|
|
|
|
|
for i, line in pairs(widget_lines) do
|
|
|
|
right_len = right_len + 1
|
|
|
|
right[i] = line
|
2021-12-21 23:51:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-15 23:02:42 +01:00
|
|
|
if too_small then
|
|
|
|
table.insert(lines,
|
|
|
|
"!!! Widget size is too small for proper drawing. Please " ..
|
|
|
|
"adjust the test case to use widgets with a dimension " ..
|
|
|
|
"of at least (14, 5)\n"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if too_big then
|
|
|
|
table.insert(lines,
|
|
|
|
"!!! Widget size is too big for proper drawing. Please " ..
|
|
|
|
"adjust the test case to keep widgets below a dimension " ..
|
|
|
|
"of (25, 25)\n"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-01-08 00:23:19 +01:00
|
|
|
table.insert(
|
|
|
|
lines,
|
|
|
|
pad_string("Actual:", " ", max_len, true) .. " Expected:"
|
|
|
|
)
|
2021-12-21 23:51:22 +01:00
|
|
|
|
2022-03-15 23:02:42 +01:00
|
|
|
for i = 1, math.max(left_len, right_len) do
|
2022-01-08 00:23:19 +01:00
|
|
|
table.insert(
|
|
|
|
lines,
|
|
|
|
pad_string(left[i] or "", " ", max_len, true) .. " " .. (right[i] or "")
|
|
|
|
)
|
2021-12-21 23:51:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Force one additional newline. Due to how the error message has to be set
|
|
|
|
-- up, we want to force `say`'s trailing quote into a new line.
|
|
|
|
table.insert(lines, "")
|
|
|
|
|
2022-03-15 23:02:42 +01:00
|
|
|
local rendered = table.concat(lines, "\n")
|
|
|
|
return rendered:gsub("%s*$", "")
|
2021-12-21 23:51:22 +01:00
|
|
|
end
|
|
|
|
|
2015-06-14 16:03:29 +02:00
|
|
|
local function widget_layout(state, arguments)
|
|
|
|
if #arguments ~= 3 then
|
|
|
|
error("Have " .. #arguments .. " arguments, but need 3")
|
|
|
|
end
|
|
|
|
|
|
|
|
local widget = arguments[1]
|
2021-12-21 23:51:22 +01:00
|
|
|
local width_actual, height_actual = arguments[2][1], arguments[2][2]
|
2015-06-14 16:03:29 +02:00
|
|
|
local expected = arguments[3]
|
|
|
|
|
2021-12-21 23:51:22 +01:00
|
|
|
if not widget.layout then
|
|
|
|
arguments[1] = "Method `layout` is missing. Is this even a widget?"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local children = widget:layout({"fake context"}, width_actual, height_actual)
|
|
|
|
|
|
|
|
if type(children) ~= "table" then
|
|
|
|
arguments[1] = string.format(
|
|
|
|
"Expected table from widget:layout(), got %s",
|
|
|
|
type(children)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local num_children = #children
|
|
|
|
|
|
|
|
local fits = num_children == #expected
|
|
|
|
-- If the numbers don't match, we can skip checking them individually
|
|
|
|
if fits then
|
|
|
|
for i = 1, num_children do
|
2016-02-07 13:10:21 +01:00
|
|
|
local child, exp = children[i], expected[i]
|
|
|
|
if child._widget ~= exp._widget or
|
|
|
|
child._width ~= exp._width or
|
|
|
|
child._height ~= exp._height or
|
|
|
|
not matrix_equals(child._matrix, exp._matrix) then
|
2015-06-14 16:03:29 +02:00
|
|
|
fits = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if state.mod == fits then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-12-21 23:51:22 +01:00
|
|
|
arguments[1] = draw_layout_comparison(children, expected)
|
2015-06-14 16:03:29 +02:00
|
|
|
return false
|
|
|
|
end
|
2021-12-21 23:51:22 +01:00
|
|
|
|
|
|
|
-- Hack the API by using the first argument as arbitrary string.
|
|
|
|
-- This gives actual flexibility in the error message.
|
|
|
|
say:set("assertion.widget_layout.positive", "%s")
|
2017-03-03 23:11:06 +01:00
|
|
|
assert:register("assertion",
|
|
|
|
"widget_layout",
|
|
|
|
widget_layout,
|
|
|
|
"assertion.widget_layout.positive",
|
|
|
|
"assertion.widget_layout.positive")
|
2014-08-23 21:57:52 +02:00
|
|
|
-- }}}
|
|
|
|
|
2017-06-08 16:59:51 +02:00
|
|
|
local function test_container(container)
|
|
|
|
local w1 = base.empty_widget()
|
|
|
|
|
|
|
|
assert.is.same({}, container:get_children())
|
|
|
|
|
|
|
|
container:set_widget(w1)
|
|
|
|
assert.is.same({ w1 }, container:get_children())
|
|
|
|
|
|
|
|
container:set_widget(nil)
|
|
|
|
assert.is.same({}, container:get_children())
|
|
|
|
|
|
|
|
container:set_widget(w1)
|
|
|
|
assert.is.same({ w1 }, container:get_children())
|
|
|
|
|
|
|
|
if container.reset then
|
|
|
|
container:reset()
|
|
|
|
assert.is.same({}, container:get_children())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-23 21:57:52 +02:00
|
|
|
return {
|
|
|
|
widget_stub = function(width, height)
|
|
|
|
local w = object()
|
2016-05-26 06:52:11 +02:00
|
|
|
w._private = {}
|
2016-02-27 14:01:31 +01:00
|
|
|
w.is_widget = true
|
2016-05-26 06:52:11 +02:00
|
|
|
w._private.visible = true
|
|
|
|
w._private.opacity = 1
|
2015-09-06 10:16:52 +02:00
|
|
|
if width or height then
|
|
|
|
w.fit = function()
|
|
|
|
return width or 10, height or 10
|
|
|
|
end
|
2014-08-23 21:57:52 +02:00
|
|
|
end
|
2016-05-31 05:12:39 +02:00
|
|
|
w._private.widget_caches = {}
|
2017-03-11 13:55:57 +01:00
|
|
|
w:connect_signal("widget::layout_changed", function()
|
|
|
|
-- TODO: This is not completely correct, since our parent's caches
|
|
|
|
-- are not cleared. For the time being, tests just have to handle
|
|
|
|
-- this clearing-part themselves.
|
|
|
|
w._private.widget_caches = {}
|
|
|
|
end)
|
2014-08-23 21:57:52 +02:00
|
|
|
|
|
|
|
return w
|
|
|
|
end,
|
2017-06-08 16:59:51 +02:00
|
|
|
|
|
|
|
test_container = test_container,
|
2014-08-23 21:57:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|