Add tests for hover component

This commit is contained in:
Insality 2022-10-08 16:11:45 +03:00
parent a87f775ac7
commit 51dc34613b
6 changed files with 140 additions and 1 deletions

View File

@ -99,6 +99,15 @@ function Hover.set_hover(self, state)
end
end
--- Return current hover state. True if touch action was on the node at current time
-- @tparam Hover self @{Hover}
-- @treturn bool The current hovered state
function Hover.is_hovered(self)
return self._is_hovered
end
--- Set mouse hover state
-- @tparam Hover self @{Hover}
-- @tparam bool state The mouse hover state
@ -110,6 +119,14 @@ function Hover.set_mouse_hover(self, state)
end
--- Return current hover state. True if nil action_id (usually desktop mouse) was on the node at current time
-- @tparam Hover self @{Hover}
-- @treturn bool The current hovered state
function Hover.is_mouse_hovered(self)
return self._is_mouse_hovered
end
--- Strict hover click area. Useful for
-- no click events outside stencil node
-- @tparam Hover self @{Hover}

View File

@ -50,4 +50,13 @@ function M.input_empty(x, y)
end
function M.input_empty_action_nil(x, y)
return nil, {
x = x,
y = y,
}
end
return M

View File

@ -11,7 +11,7 @@ end
-- Callback for return value from function
function M.get_function(callback)
local listener = {}
listener.callback = function() if callback then return callback() end end
listener.callback = function(...) if callback then return callback(...) end end
mock.mock(listener)
return function(...) return listener.callback(...) end, listener.callback
end

View File

@ -3,6 +3,7 @@ local deftest = require("deftest.deftest")
local tests = {
-- Test list
require("test.tests.test_button"),
require("test.tests.test_hover"),
}

100
test/tests/test_hover.lua Normal file
View File

@ -0,0 +1,100 @@
local mock_gui = require "deftest.mock.gui"
local mock_time = require("deftest.mock.time")
local mock_input = require("test.helper.mock_input")
local test_helper = require("test.helper.test_helper")
local druid_system = require("druid.druid")
return function()
local druid = nil
local context = test_helper.get_context()
describe("Eva lang", function()
before(function()
mock_gui.mock()
mock_time.mock()
mock_time.set(60)
druid = druid_system.new(context)
end)
after(function()
mock_gui.unmock()
mock_time.unmock()
druid:final(context)
druid = nil
end)
it("Hover should fire callback on touch hover and unhover", function()
local button = mock_gui.add_box("button", 0, 0, 100, 50)
local is_hovered = false
local on_hover, on_hover_mock = test_helper.get_function(function(_, state)
is_hovered = state
end)
local instance = druid:new_hover(button, on_hover)
druid:on_input(mock_input.input_empty(10, 10))
assert(is_hovered == true)
assert(instance:is_hovered() == true)
assert(instance:is_mouse_hovered() == false)
druid:on_input(mock_input.input_empty(-10, 10))
assert(is_hovered == false)
assert(instance:is_hovered() == false)
assert(instance:is_mouse_hovered() == false)
end)
it("Hover should fire callback on mouse hover and unhover", function()
local button = mock_gui.add_box("button", 0, 0, 100, 50)
local is_hovered = false
local on_hover, on_hover_mock = test_helper.get_function(function(_, state)
is_hovered = state
end)
local instance = druid:new_hover(button)
instance.on_mouse_hover:subscribe(on_hover)
druid:on_input(mock_input.input_empty_action_nil(10, 10))
assert(is_hovered == true)
assert(instance:is_hovered() == false)
assert(instance:is_mouse_hovered() == true)
druid:on_input(mock_input.input_empty_action_nil(-10, 10))
assert(is_hovered == false)
assert(instance:is_hovered() == false)
assert(instance:is_mouse_hovered() == false)
end)
it("Should work with click zone", function()
local button = mock_gui.add_box("button", 0, 0, 100, 50)
local zone = mock_gui.add_box("zone", 25, 25, 25, 25)
local on_hover, on_hover_mock = test_helper.get_function()
local instance = druid:new_hover(button, on_hover)
instance:set_click_zone(zone)
druid:on_input(mock_input.input_empty(10, 10))
assert(instance:is_hovered() == false)
druid:on_input(mock_input.input_empty(25, 25))
assert(instance:is_hovered() == true)
druid:on_input(mock_input.input_empty(24, 24))
assert(instance:is_hovered() == false)
end)
it("Hover should have set_enabled function", function()
local button = mock_gui.add_box("button", 0, 0, 100, 50)
local on_hover, on_hover_mock = test_helper.get_function()
local instance = druid:new_hover(button, on_hover)
druid:on_input(mock_input.input_empty(10, 10))
assert(instance:is_hovered() == true)
instance:set_enabled(false)
assert(instance:is_hovered() == false)
druid:on_input(mock_input.input_empty(12, 12))
assert(instance:is_hovered() == false)
instance:set_enabled(true)
druid:on_input(mock_input.input_empty(12, 12))
assert(instance:is_hovered() == true)
druid:on_input(mock_input.input_empty(-10, 10))
assert(instance:is_hovered() == false)
end)
end)
end

View File

@ -1,13 +1,25 @@
local mock_gui = require "deftest.mock.gui"
local mock_time = require("deftest.mock.time")
local mock_input = require("test.helper.mock_input")
local test_helper = require("test.helper.test_helper")
local druid_system = require("druid.druid")
return function()
local druid = nil
local context = test_helper.get_context()
describe("Eva lang", function()
before(function()
mock_gui.mock()
mock_time.mock()
mock_time.set(60)
druid = druid_system.new(context)
end)
after(function()
mock_gui.unmock()
mock_time.unmock()
druid:final(context)
druid = nil
end)
it("Should do something right", function()