Add tests for drag component

This commit is contained in:
Insality 2022-10-08 19:11:57 +03:00
parent 51dc34613b
commit 9e92cf295b
10 changed files with 167 additions and 12 deletions

View File

@ -155,9 +155,11 @@ end
-- or create your own style
-- @table style
-- @tfield[opt=10] number DRAG_DEADZONE Distance in pixels to start dragging
-- @tfield[opt=false] boolean IS_USE_SCREEN_KOEF If screen aspect ration affects on drag values
function Drag.on_style_change(self, style)
self.style = {}
self.style.DRAG_DEADZONE = style.DRAG_DEADZONE or 10
self.style.IS_USE_SCREEN_KOEF = style.IS_USE_SCREEN_KOEF or false
end
@ -278,11 +280,16 @@ function Drag.on_input(self, action_id, action)
end
if self.is_drag then
local x_koef, y_koef = self._x_koef, self._y_koef
if not self.style.IS_USE_SCREEN_KOEF then
x_koef, y_koef = 1, 1
end
self.on_drag:trigger(self:get_context(),
self.dx * self._x_koef,
self.dy * self._y_koef,
(self.x - self.touch_start_pos.x) * self._x_koef,
(self.y - self.touch_start_pos.y) * self._y_koef)
self.dx * x_koef,
self.dy * y_koef,
(self.x - self.touch_start_pos.x) * x_koef,
(self.y - self.touch_start_pos.y) * y_koef)
end
return self.is_drag

View File

@ -56,6 +56,7 @@ M["button"] = {
M["drag"] = {
DRAG_DEADZONE = 10, -- Size in pixels of drag deadzone
IS_USE_SCREEN_KOEF = true,
}

View File

@ -1,5 +1,5 @@
[bootstrap]
main_collection = /example/example.collectionc
main_collection = /test/test.collectionc
[script]
shared_state = 1

View File

@ -4,7 +4,7 @@ embedded_instances {
id: "test"
data: "components {\n"
" id: \"test\"\n"
" component: \"/test/test.script\"\n"
" component: \"/test/test.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
@ -16,6 +16,8 @@ embedded_instances {
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" property_decls {\n"
" }\n"
"}\n"
""
position {

10
test/test.gui Normal file
View File

@ -0,0 +1,10 @@
script: "/test/test.gui_script"
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

View File

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

View File

@ -8,7 +8,7 @@ local druid_system = require("druid.druid")
return function()
local druid = nil
local context = test_helper.get_context()
describe("Druid Button", function()
describe("Button Component", function()
before(function()
mock_gui.mock()
mock_time.mock()

134
test/tests/test_drag.lua Normal file
View File

@ -0,0 +1,134 @@
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()
local function create_drag_instance(on_drag)
local button = mock_gui.add_box("button", 0, 0, 20, 20)
local instance = druid:new_drag(button, on_drag)
instance.style.IS_USE_SCREEN_KOEF = false
instance.style.DRAG_DEADZONE = 4
return instance
end
describe("Drag component", 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 call drag callback on node", function()
local on_drag, on_drag_mock = test_helper.get_function()
local instance = create_drag_instance(on_drag)
druid:on_input(mock_input.click_pressed(10, 10))
assert(instance.is_touch == true)
druid:on_input(mock_input.input_empty(12, 10))
assert(on_drag_mock.calls == 0)
druid:on_input(mock_input.input_empty(14, 10))
assert(on_drag_mock.calls == 1)
assert(on_drag_mock.params[2] == 2) -- From the last input dx
assert(on_drag_mock.params[3] == 0)
assert(on_drag_mock.params[4] == 4) -- Total X from drag start point
assert(on_drag_mock.params[5] == 0)
end)
it("Should call all included events", function()
local on_drag, on_drag_mock = test_helper.get_function()
local instance = create_drag_instance(on_drag)
local on_touch_start, on_touch_start_mock = test_helper.get_function()
instance.on_touch_start:subscribe(on_touch_start)
local on_touch_end, on_touch_end_mock = test_helper.get_function()
instance.on_touch_end:subscribe(on_touch_end)
local on_drag_start, on_drag_start_mock = test_helper.get_function()
instance.on_drag_start:subscribe(on_drag_start)
local on_drag_end, on_drag_end_mock = test_helper.get_function()
instance.on_drag_end:subscribe(on_drag_end)
assert(on_touch_start_mock.calls == 0)
druid:on_input(mock_input.click_pressed(10, 10))
assert(on_touch_start_mock.calls == 1)
assert(on_touch_end_mock.calls == 0)
druid:on_input(mock_input.click_released(12, 10))
assert(on_touch_end_mock.calls == 1)
druid:on_input(mock_input.click_pressed(10, 10))
assert(on_drag_start_mock.calls == 0)
druid:on_input(mock_input.input_empty(15, 10))
assert(on_drag_start_mock.calls == 1)
assert(on_drag_mock.calls == 1)
assert(on_drag_end_mock.calls == 0)
druid:on_input(mock_input.click_released(15, 10))
assert(on_drag_end_mock.calls == 1)
end)
it("Should work with set_enabled", function()
local on_drag, on_drag_mock = test_helper.get_function()
local instance = create_drag_instance(on_drag)
instance:set_enabled(false)
assert(instance:is_enabled() == false)
druid:on_input(mock_input.click_pressed(10, 10))
assert(instance.is_touch == false)
druid:on_input(mock_input.input_empty(12, 10))
assert(on_drag_mock.calls == 0)
druid:on_input(mock_input.input_empty(15, 10))
assert(on_drag_mock.calls == 0)
instance:set_enabled(true)
assert(instance:is_enabled() == true)
druid:on_input(mock_input.click_pressed(10, 10))
assert(instance.is_touch == true)
druid:on_input(mock_input.input_empty(12, 10))
assert(on_drag_mock.calls == 0)
druid:on_input(mock_input.input_empty(15, 10))
assert(on_drag_mock.calls == 1)
end)
it("Should work with click zone", function()
local on_drag, on_drag_mock = test_helper.get_function()
local instance = create_drag_instance(on_drag)
local zone = mock_gui.add_box("zone", 10, 10, 10, 10)
instance:set_click_zone(zone)
druid:on_input(mock_input.click_pressed(5, 5))
assert(instance.is_touch == false)
druid:on_input(mock_input.input_empty(10, 5))
assert(on_drag_mock.calls == 0)
druid:on_input(mock_input.input_empty(15, 10))
assert(on_drag_mock.calls == 0)
druid:on_input(mock_input.click_pressed(15, 15))
assert(instance.is_touch == true)
druid:on_input(mock_input.input_empty(20, 15))
assert(on_drag_mock.calls == 1)
end)
end)
end

View File

@ -7,7 +7,7 @@ local druid_system = require("druid.druid")
return function()
local druid = nil
local context = test_helper.get_context()
describe("Eva lang", function()
describe("Hover component", function()
before(function()
mock_gui.mock()
mock_time.mock()
@ -22,7 +22,7 @@ return function()
druid = nil
end)
it("Hover should fire callback on touch hover and unhover", function()
it("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)
@ -40,7 +40,7 @@ return function()
assert(instance:is_mouse_hovered() == false)
end)
it("Hover should fire callback on mouse hover and unhover", function()
it("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)
@ -76,7 +76,7 @@ return function()
assert(instance:is_hovered() == false)
end)
it("Hover should have set_enabled function", function()
it("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)

View File

@ -7,7 +7,7 @@ local druid_system = require("druid.druid")
return function()
local druid = nil
local context = test_helper.get_context()
describe("Eva lang", function()
describe("Template component", function()
before(function()
mock_gui.mock()
mock_time.mock()