diff --git a/.luacheckrc b/.luacheckrc index 82ec63c..063fc09 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -45,8 +45,12 @@ globals = { "debug", "timer", "window", - "buffer", + "buffer", "resource", "defos", "html5", + "describe", + "before", + "after", + "it", } diff --git a/docs_md/changelog.md b/docs_md/changelog.md index a442d6b..3f66067 100644 --- a/docs_md/changelog.md +++ b/docs_md/changelog.md @@ -445,4 +445,4 @@ And yeah, the new **Druid** logo is here! - **#189** [System] Add optional flag to `component:set_input_priority` to mark it as temporary. It will reset to default input priority after the `component:reset_input_priority` - **#204** [System] Fix: wrong code example link, if open example from direct URL - **#202** [System] Enabled stencil check to true by default. To disable this, use `druid.no_stencil_check` in game.project settings -- [Examples] Add layout, layout fit, progres bar, data list + component examples +- [Examples] Add layout, layout fit, progress bar, data list + component examples diff --git a/druid/base/button.lua b/druid/base/button.lua index 61c4db2..aa66aba 100755 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -145,7 +145,7 @@ local function on_button_release(self) self.can_action = false local time = socket.gettime() - local is_long_click = (time - self.last_pressed_time) > self.style.LONGTAP_TIME + local is_long_click = (time - self.last_pressed_time) >= self.style.LONGTAP_TIME is_long_click = is_long_click and self.on_long_click:is_exist() local is_double_click = (time - self.last_released_time) < self.style.DOUBLETAP_TIME diff --git a/test/helper/mock_input.lua b/test/helper/mock_input.lua new file mode 100644 index 0000000..d4c30cd --- /dev/null +++ b/test/helper/mock_input.lua @@ -0,0 +1,22 @@ +local M = {} + + +function M.click_pressed(x, y) + return hash("touch"), { + pressed = true, + x = x, + y = y, + } +end + + +function M.click_released(x, y) + return hash("touch"), { + released = true, + x = x, + y = y, + } +end + + +return M diff --git a/test/helper/test_helper.lua b/test/helper/test_helper.lua new file mode 100644 index 0000000..3960654 --- /dev/null +++ b/test/helper/test_helper.lua @@ -0,0 +1,18 @@ +local mock = require("deftest.mock.mock") + +local M = {} + +-- Userdata type instead of script self +function M.get_context() + return vmath.vector() +end + + +function M.get_function() + local listener = {} + listener.callback = function() end + mock.mock(listener) + return function(...) listener.callback(...) end, listener.callback +end + +return M diff --git a/test/test.script b/test/test.script index b2eb07f..c65c475 100644 --- a/test/test.script +++ b/test/test.script @@ -1,7 +1,8 @@ -local deftest = require "deftest.deftest" +local deftest = require("deftest.deftest") local tests = { -- Test list + require("test.tests.test_button"), } diff --git a/test/tests/test_button.lua b/test/tests/test_button.lua new file mode 100644 index 0000000..12bdeb2 --- /dev/null +++ b/test/tests/test_button.lua @@ -0,0 +1,129 @@ +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("Druid Button", 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 usual click", 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) + local is_clicked_pressed = druid:on_input(mock_input.click_pressed(10, 10)) + local is_clicked_released = druid:on_input(mock_input.click_released(20, 10)) + + assert(is_clicked_pressed) + assert(is_clicked_released) + assert(on_click_mock.calls == 1) + assert(on_click_mock.params[1] == context) + assert(on_click_mock.params[2] == button_params) + assert(on_click_mock.params[3] == instance) + end) + + it("Should do long click if exists", 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 on_long_click, on_long_click_mock = test_helper.get_function() + + local instance = druid:new_button(button, on_click, button_params) + instance.on_long_click:subscribe(on_long_click) + druid:on_input(mock_input.click_pressed(10, 10)) + mock_time.elapse(0.3) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + assert(on_long_click_mock.calls == 0) + + druid:on_input(mock_input.click_pressed(10, 10)) + mock_time.elapse(0.5) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + assert(on_long_click_mock.calls == 1) + assert(on_long_click_mock.params[1] == context) + assert(on_long_click_mock.params[2] == button_params) + assert(on_long_click_mock.params[3] == instance) + end) + + it("Should do not long click if not exists", 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() + druid:new_button(button, on_click, button_params) + druid:on_input(mock_input.click_pressed(10, 10)) + mock_time.elapse(0.5) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + end) + + it("Should do double click if exists", 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 on_double_click, on_double_click_mock = test_helper.get_function() + + local instance = druid:new_button(button, on_click, button_params) + instance.on_double_click:subscribe(on_double_click) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + mock_time.elapse(0.1) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 1) + assert(on_double_click_mock.calls == 1) + assert(on_double_click_mock.params[1] == context) + assert(on_double_click_mock.params[2] == button_params) + assert(on_double_click_mock.params[3] == instance) + + mock_time.elapse(1) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + mock_time.elapse(0.5) -- The double click should not register, big time between clicks + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + print(on_click_mock.calls, on_double_click_mock.calls) + assert(on_click_mock.calls == 3) + assert(on_double_click_mock.calls == 1) + end) + + it("Should do not double click if not exists", 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() + druid:new_button(button, on_click, button_params) + + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + mock_time.elapse(0.1) + druid:on_input(mock_input.click_pressed(10, 10)) + druid:on_input(mock_input.click_released(20, 10)) + + assert(on_click_mock.calls == 2) + end) + end) +end diff --git a/test/tests/test_template.lua b/test/tests/test_template.lua new file mode 100644 index 0000000..f9cc5ef --- /dev/null +++ b/test/tests/test_template.lua @@ -0,0 +1,17 @@ +local mock_gui = require "deftest.mock.gui" + +return function() + describe("Eva lang", function() + before(function() + mock_gui.mock() + end) + + after(function() + mock_gui.unmock() + end) + + it("Should do something right", function() + assert(2 == 1 + 1) + end) + end) +end