From d92be1cfa735c01567ffe1dd843cdc7d4d8e9290 Mon Sep 17 00:00:00 2001 From: Insality Date: Sun, 11 Sep 2022 14:19:07 +0300 Subject: [PATCH] Update button tests --- test/helper/mock_input.lua | 14 ++++++++++++++ test/tests/test_button.lua | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/test/helper/mock_input.lua b/test/helper/mock_input.lua index 900f9b8..7362b8d 100644 --- a/test/helper/mock_input.lua +++ b/test/helper/mock_input.lua @@ -19,6 +19,20 @@ function M.click_released(x, y) end +function M.key_pressed(key_id) + return hash(key_id), { + pressed = true + } +end + + +function M.key_released(key_id) + return hash(key_id), { + released = true + } +end + + function M.click_repeated(x, y) return hash("touch"), { repeated = true, diff --git a/test/tests/test_button.lua b/test/tests/test_button.lua index ee5ca9e..d27178d 100644 --- a/test/tests/test_button.lua +++ b/test/tests/test_button.lua @@ -211,5 +211,33 @@ return function() assert(check_function_true_mock.calls == 1) assert(failure_function_mock.calls == 1) end) + + it("Should have key trigger", function() + local button = mock_gui.add_box("button", 0, 0, 100, 50) + local button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + instance:set_key_trigger("key_a") + druid:on_input(mock_input.key_pressed("key_a")) + druid:on_input(mock_input.key_released("key_a")) + assert(on_click_mock.calls == 1) + assert(instance:get_key_trigger() == hash("key_a")) + 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 button_params = {} + local on_click, on_click_mock = test_helper.get_function() + local instance = druid:new_button(button, on_click, button_params) + instance:set_click_zone(zone) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(10, 10)) + assert(on_click_mock.calls == 0) + + druid:on_input(mock_input.click_pressed(25, 25)) + druid:on_input(mock_input.click_released(25, 25)) + assert(on_click_mock.calls == 1) + end) end) end