diff --git a/druid/base/hover.lua b/druid/base/hover.lua index 05aa0ac..00323b4 100644 --- a/druid/base/hover.lua +++ b/druid/base/hover.lua @@ -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} diff --git a/test/helper/mock_input.lua b/test/helper/mock_input.lua index 7362b8d..27e575c 100644 --- a/test/helper/mock_input.lua +++ b/test/helper/mock_input.lua @@ -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 diff --git a/test/helper/test_helper.lua b/test/helper/test_helper.lua index 7623cf6..a728a0d 100644 --- a/test/helper/test_helper.lua +++ b/test/helper/test_helper.lua @@ -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 diff --git a/test/test.script b/test/test.script index c65c475..10e8d03 100644 --- a/test/test.script +++ b/test/test.script @@ -3,6 +3,7 @@ local deftest = require("deftest.deftest") local tests = { -- Test list require("test.tests.test_button"), + require("test.tests.test_hover"), } diff --git a/test/tests/test_hover.lua b/test/tests/test_hover.lua new file mode 100644 index 0000000..24d0f7e --- /dev/null +++ b/test/tests/test_hover.lua @@ -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 diff --git a/test/tests/test_template.lua b/test/tests/test_template.lua index f9cc5ef..b81690a 100644 --- a/test/tests/test_template.lua +++ b/test/tests/test_template.lua @@ -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()