mirror of
https://github.com/Insality/druid
synced 2025-06-27 10:27:48 +02:00
Add initial Druid tests
This commit is contained in:
parent
cb5df2b5bc
commit
a6de0128bd
@ -45,8 +45,12 @@ globals = {
|
||||
"debug",
|
||||
"timer",
|
||||
"window",
|
||||
"buffer",
|
||||
"buffer",
|
||||
"resource",
|
||||
"defos",
|
||||
"html5",
|
||||
"describe",
|
||||
"before",
|
||||
"after",
|
||||
"it",
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
22
test/helper/mock_input.lua
Normal file
22
test/helper/mock_input.lua
Normal file
@ -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
|
18
test/helper/test_helper.lua
Normal file
18
test/helper/test_helper.lua
Normal file
@ -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
|
@ -1,7 +1,8 @@
|
||||
local deftest = require "deftest.deftest"
|
||||
local deftest = require("deftest.deftest")
|
||||
|
||||
local tests = {
|
||||
-- Test list
|
||||
require("test.tests.test_button"),
|
||||
}
|
||||
|
||||
|
||||
|
129
test/tests/test_button.lua
Normal file
129
test/tests/test_button.lua
Normal file
@ -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
|
17
test/tests/test_template.lua
Normal file
17
test/tests/test_template.lua
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user