2015-08-12 11:53:01 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2015 Uli Schlachter
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local hierarchy = require("wibox.hierarchy")
|
|
|
|
|
2015-09-16 13:57:38 +02:00
|
|
|
local Region = require("lgi").cairo.Region
|
2015-08-12 11:53:01 +02:00
|
|
|
local matrix = require("gears.matrix")
|
2015-09-06 10:00:27 +02:00
|
|
|
local utils = require("wibox.test_utils")
|
2015-08-12 11:53:01 +02:00
|
|
|
|
|
|
|
local function make_widget(children)
|
2015-09-06 10:00:27 +02:00
|
|
|
local result = utils.widget_stub()
|
2015-08-12 11:53:01 +02:00
|
|
|
result.layout = function()
|
|
|
|
return children
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2016-02-07 13:10:21 +01:00
|
|
|
local function make_child(widget, width, height, mat)
|
|
|
|
return { _widget = widget, _width = width, _height = height, _matrix = mat }
|
2015-08-12 11:53:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe("wibox.hierarchy", function()
|
|
|
|
describe("Accessor functions", function()
|
|
|
|
local widget, instance
|
|
|
|
before_each(function()
|
|
|
|
local function nop() end
|
|
|
|
local context = {}
|
|
|
|
widget = make_widget(nil)
|
|
|
|
instance = hierarchy.new(context, widget, 10, 20, nop, nop)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_widget", function()
|
|
|
|
assert.is.equal(instance:get_widget(), widget)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_matrix_to_parent", function()
|
2015-09-16 13:57:38 +02:00
|
|
|
assert.is.equal(matrix.identity, instance:get_matrix_to_parent())
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_matrix_to_device", function()
|
2015-09-16 13:57:38 +02:00
|
|
|
assert.is.equal(matrix.identity, instance:get_matrix_to_device())
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_matrix_from_parent", function()
|
2015-09-16 13:57:38 +02:00
|
|
|
assert.is.equal(matrix.identity, instance:get_matrix_from_parent())
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_matrix_from_device", function()
|
2015-09-16 13:57:38 +02:00
|
|
|
assert.is.equal(matrix.identity, instance:get_matrix_from_device())
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_draw_extents", function()
|
|
|
|
assert.is.same({ instance:get_draw_extents() }, { 0, 0, 10, 20 })
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_size", function()
|
|
|
|
assert.is.same({ instance:get_size() }, { 10, 20 })
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_children", function()
|
|
|
|
assert.is.same(instance:get_children(), {})
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("disconnect works", function()
|
|
|
|
local child = make_widget(nil)
|
|
|
|
local parent = make_widget({
|
2015-09-16 13:57:38 +02:00
|
|
|
make_child(child, 2, 5, matrix.create_translate(10, 0))
|
2015-08-12 11:53:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
local extra_arg = {}
|
|
|
|
local child_redraws, child_layouts = 0, 0
|
|
|
|
local parent_redraws, parent_layouts = 0, 0
|
|
|
|
local function redraw(arg, extra)
|
|
|
|
assert.is.equal(extra_arg, extra)
|
|
|
|
if arg:get_widget() == child then
|
|
|
|
child_redraws = child_redraws + 1
|
|
|
|
elseif arg:get_widget() == parent then
|
|
|
|
parent_redraws = parent_redraws + 1
|
|
|
|
else
|
|
|
|
error("Unknown widget")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local function layout(arg, extra)
|
|
|
|
assert.is.equal(extra_arg, extra)
|
|
|
|
if arg:get_widget() == child then
|
|
|
|
child_layouts = child_layouts + 1
|
|
|
|
elseif arg:get_widget() == parent then
|
|
|
|
parent_layouts = parent_layouts + 1
|
|
|
|
else
|
|
|
|
error("Unknown widget")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local context = {}
|
2016-02-07 13:10:21 +01:00
|
|
|
local instance = hierarchy.new(context, parent, 15, 20, redraw, layout, extra_arg) -- luacheck: no unused
|
2015-08-12 11:53:01 +02:00
|
|
|
|
|
|
|
-- There should be a connection
|
|
|
|
parent:emit_signal("widget::redraw_needed")
|
|
|
|
assert.is.same({ 0, 0, 1, 0 }, { child_redraws, child_layouts, parent_redraws, parent_layouts })
|
|
|
|
child:emit_signal("widget::redraw_needed")
|
|
|
|
assert.is.same({ 1, 0, 1, 0 }, { child_redraws, child_layouts, parent_redraws, parent_layouts })
|
|
|
|
child:emit_signal("widget::layout_changed")
|
|
|
|
assert.is.same({ 1, 1, 1, 0 }, { child_redraws, child_layouts, parent_redraws, parent_layouts })
|
|
|
|
parent:emit_signal("widget::layout_changed")
|
|
|
|
assert.is.same({ 1, 1, 1, 1 }, { child_redraws, child_layouts, parent_redraws, parent_layouts })
|
|
|
|
|
|
|
|
-- Garbage-collect the hierarchy
|
|
|
|
instance = nil
|
|
|
|
collectgarbage("collect")
|
|
|
|
|
|
|
|
-- No connections should be left
|
|
|
|
parent:emit_signal("widget::redraw_needed")
|
|
|
|
child:emit_signal("widget::redraw_needed")
|
|
|
|
child:emit_signal("widget::layout_changed")
|
|
|
|
parent:emit_signal("widget::layout_changed")
|
|
|
|
assert.is.same({ 1, 1, 1, 1 }, { child_redraws, child_layouts, parent_redraws, parent_layouts })
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe("children", function()
|
|
|
|
local child, intermediate, parent
|
|
|
|
local hierarchy_child, hierarchy_intermediate, hierarchy_parent
|
|
|
|
before_each(function()
|
|
|
|
child = make_widget(nil)
|
|
|
|
intermediate = make_widget({
|
2015-09-16 13:57:38 +02:00
|
|
|
make_child(child, 10, 20, matrix.create_translate(0, 5))
|
2015-08-12 11:53:01 +02:00
|
|
|
})
|
|
|
|
parent = make_widget({
|
2015-09-16 13:57:38 +02:00
|
|
|
make_child(intermediate, 5, 2, matrix.create_translate(4, 0))
|
2015-08-12 11:53:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
local function nop() end
|
|
|
|
local context = {}
|
|
|
|
hierarchy_parent = hierarchy.new(context, parent, 15, 16, nop, nop)
|
|
|
|
|
|
|
|
-- This also tests get_children
|
|
|
|
local children = hierarchy_parent:get_children()
|
|
|
|
assert.is.equal(#children, 1)
|
|
|
|
hierarchy_intermediate = children[1]
|
|
|
|
|
2016-02-07 13:10:21 +01:00
|
|
|
children = hierarchy_intermediate:get_children()
|
2015-08-12 11:53:01 +02:00
|
|
|
assert.is.equal(#children, 1)
|
|
|
|
hierarchy_child = children[1]
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_widget", function()
|
|
|
|
assert.is.equal(hierarchy_child:get_widget(), child)
|
|
|
|
assert.is.equal(hierarchy_intermediate:get_widget(), intermediate)
|
|
|
|
assert.is.equal(hierarchy_parent:get_widget(), parent)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_matrix_to_parent", function()
|
2015-09-16 13:57:38 +02:00
|
|
|
assert.is.equal(hierarchy_child:get_matrix_to_parent(), matrix.create_translate(0, 5))
|
|
|
|
assert.is.equal(hierarchy_intermediate:get_matrix_to_parent(), matrix.create_translate(4, 0))
|
|
|
|
assert.is.equal(hierarchy_parent:get_matrix_to_parent(), matrix.identity)
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_matrix_to_device", function()
|
2015-09-16 13:57:38 +02:00
|
|
|
assert.is.equal(hierarchy_child:get_matrix_to_device(), matrix.create_translate(4, 5))
|
|
|
|
assert.is.equal(hierarchy_intermediate:get_matrix_to_device(), matrix.create_translate(4, 0))
|
|
|
|
assert.is.equal(hierarchy_parent:get_matrix_to_device(), matrix.identity)
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_matrix_from_parent", function()
|
2015-09-16 13:57:38 +02:00
|
|
|
assert.is.equal(hierarchy_child:get_matrix_from_parent(), matrix.create_translate(0, -5))
|
|
|
|
assert.is.equal(hierarchy_intermediate:get_matrix_from_parent(), matrix.create_translate(-4, 0))
|
|
|
|
assert.is.equal(hierarchy_parent:get_matrix_from_parent(), matrix.identity)
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_matrix_from_device", function()
|
2015-09-16 13:57:38 +02:00
|
|
|
assert.is.equal(hierarchy_child:get_matrix_from_device(), matrix.create_translate(-4, -5))
|
|
|
|
assert.is.equal(hierarchy_intermediate:get_matrix_from_device(), matrix.create_translate(-4, 0))
|
|
|
|
assert.is.equal(hierarchy_parent:get_matrix_from_device(), matrix.identity)
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_draw_extents", function()
|
|
|
|
assert.is.same({ hierarchy_child:get_draw_extents() }, { 0, 0, 10, 20 })
|
|
|
|
assert.is.same({ hierarchy_intermediate:get_draw_extents() }, { 0, 0, 10, 25 })
|
|
|
|
assert.is.same({ hierarchy_parent:get_draw_extents() }, { 0, 0, 15, 25 })
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("get_size", function()
|
|
|
|
assert.is.same({ hierarchy_child:get_size() }, { 10, 20 })
|
|
|
|
assert.is.same({ hierarchy_intermediate:get_size() }, { 5, 2 })
|
|
|
|
assert.is.same({ hierarchy_parent:get_size() }, { 15, 16 })
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2015-09-19 13:57:18 +02:00
|
|
|
describe("update", function()
|
2015-08-12 11:53:01 +02:00
|
|
|
local child, intermediate, parent
|
|
|
|
local instance
|
|
|
|
local function nop() end
|
|
|
|
before_each(function()
|
|
|
|
child = make_widget(nil)
|
|
|
|
intermediate = make_widget({
|
2015-09-16 13:57:38 +02:00
|
|
|
make_child(child, 10, 20, matrix.create_translate(0, 5))
|
2015-08-12 11:53:01 +02:00
|
|
|
})
|
|
|
|
parent = make_widget({
|
2015-09-16 13:57:38 +02:00
|
|
|
make_child(intermediate, 5, 2, matrix.create_translate(4, 0))
|
2015-08-12 11:53:01 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
local context = {}
|
|
|
|
instance = hierarchy.new(context, parent, 15, 16, nop, nop)
|
|
|
|
end)
|
|
|
|
|
2015-09-19 13:57:18 +02:00
|
|
|
it("No difference 1", function()
|
|
|
|
local region = instance:update(context, parent, 15, 16)
|
2015-08-12 11:53:01 +02:00
|
|
|
assert.is.equal(region:num_rectangles(), 0)
|
|
|
|
end)
|
|
|
|
|
2015-09-19 13:57:18 +02:00
|
|
|
it("No difference 2", function()
|
|
|
|
local region1 = Region.create()
|
|
|
|
local region2 = instance:update(context, parent, 15, 16, region1)
|
|
|
|
assert.is.equal(region1, region2)
|
|
|
|
assert.is.equal(region2:num_rectangles(), 0)
|
|
|
|
end)
|
|
|
|
|
2015-08-12 11:53:01 +02:00
|
|
|
it("child moved", function()
|
2015-09-19 13:57:18 +02:00
|
|
|
-- Clear caches and change result of intermediate
|
2015-08-12 11:53:01 +02:00
|
|
|
intermediate.layout = function()
|
2015-09-16 13:57:38 +02:00
|
|
|
return { make_child(child, 10, 20, matrix.create_translate(0, 4)) }
|
2015-08-12 11:53:01 +02:00
|
|
|
end
|
2015-09-19 13:57:18 +02:00
|
|
|
intermediate:emit_signal("widget::layout_changed")
|
|
|
|
|
|
|
|
local region = instance:update(context, parent, 15, 16)
|
2015-08-12 11:53:01 +02:00
|
|
|
assert.is.equal(region:num_rectangles(), 1)
|
|
|
|
local rect = region:get_rectangle(0)
|
|
|
|
-- The widget drew to 4, 5, 10, 20 before and 4, 4, 10, 20 after
|
|
|
|
assert.is.same({ rect.x, rect.y, rect.width, rect.height }, { 4, 4, 10, 21 })
|
|
|
|
end)
|
2015-09-19 13:57:18 +02:00
|
|
|
|
|
|
|
it("child disappears", function()
|
|
|
|
-- Clear caches and change result of intermediate
|
|
|
|
intermediate.layout = function() end
|
|
|
|
intermediate:emit_signal("widget::layout_changed")
|
|
|
|
|
|
|
|
local region = instance:update(context, parent, 15, 16)
|
|
|
|
assert.is.equal(region:num_rectangles(), 1)
|
|
|
|
local rect = region:get_rectangle(0)
|
|
|
|
-- The child was drawn to 4, 5, 10, 20
|
|
|
|
assert.is.same({ rect.x, rect.y, rect.width, rect.height }, { 4, 5, 10, 20 })
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("widget changed", function()
|
|
|
|
-- Clear caches and change result of parent
|
|
|
|
local new_intermediate = make_widget({
|
|
|
|
make_child(child, 10, 20, matrix.create_translate(0, 5))
|
|
|
|
})
|
|
|
|
parent.layout = function()
|
|
|
|
return { make_child(new_intermediate, 5, 2, matrix.create_translate(4, 0)) }
|
|
|
|
end
|
|
|
|
parent:emit_signal("widget::layout_changed")
|
|
|
|
|
|
|
|
local region = instance:update(context, parent, 15, 16)
|
|
|
|
assert.is.equal(region:num_rectangles(), 1)
|
|
|
|
local rect = region:get_rectangle(0)
|
|
|
|
-- Intermediate drew to 4, 0, 5, 2 (and so does new_intermediate)
|
|
|
|
assert.is.same({ rect.x, rect.y, rect.width, rect.height }, { 4, 0, 5, 2 })
|
|
|
|
end)
|
2015-08-12 11:53:01 +02:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|