SOLVED - Is it possible to get mouse_get_position?
Use case: Testing menu interactions, currently requires a non-deterministic await
Currently on GD4 branch I ended up with this, that works, but the wait for a single frame seems rather non-deterministic.
func test_open_settings_menu() -> void:
var settings_button_pos = button_settings.get_global_position()
_sender.mouse_set_position(settings_button_pos, settings_button_pos)
await wait_frames(1)
_sender.mouse_left_button_down(settings_button_pos, settings_button_pos)
_sender.mouse_left_button_up(settings_button_pos, settings_button_pos)
await wait_frames(1)
assert_eq(main_menu.visible,false)
assert_eq(settings_menu.visible,true)
It would be nice if we could have something like await wait_until(assert_eq(_sender.mouse_get_position,settings_button_pos)) to wait until the mouse is in the correct place. Would something like that be possible?
There is some stuff in the InputSender that makes this a little cleaner, but the "waits" are necessary. You can string all your commands together and there is wait_frames and wait_secs built into the sender. There's also hold_for which can save you a "button up". You can then wait for the idle signal on the sender.
Some waits can be skipped in different scenarios (such as no wait between your button down/up). But if you want something like just_pressed you have to use waits (I'm pretty sure).
Here's another way to write that test without using any direct calls to wait_frames.
func test_open_settings_menu() -> void:
var settings_button_pos = button_settings.get_global_position()
_sender.mouse_set_position(settings_button_pos, settings_button_pos)\
.wait_frames(1)\
.mouse_left_button_down(settings_button_pos, settings_button_pos)\
.hold_for('1f')\ # holds the button down for one frame, for illustration purposes, no button up needed
.wait_frames(1)
await _sender.idle
assert_eq(main_menu.visible,false)
assert_eq(settings_menu.visible,true)
I've also been working on some additional helpers that I haven't released yet. In the near future you will be able to do something like:
func test_open_settings_menu() -> void:
await _sender.mouse_left_click_on(button_settings).idle
assert_eq(main_menu.visible,false)
assert_eq(settings_menu.visible,true)
Ohhh that chained version looks real nice!
Seems like it did not like .hold_for('1f') but was working fine with .hold_for(0.1) as number rather than string.
Also great call on the waiting for sender to be idle, didn't think of doing that.
While here, is the assert_eq instant assert or is it a waiting assert? (And if its instant, can I make it waiting?)
Odd. hold_for is supposed to take both string (a number and then 's' for seconds or 'f' for frames) or a number of seconds. I got a little to tricky with it and tried to make it too versatile, it might be broke or needed more frames. The next release will deprecate hold_for and is replaced by hold_seconds or hold_frames (hold_for will still work, just generate a deprecation warning).
None of the asserts do any awaiting of their own. So the assert executes immediately. In this test's case though, the await prevents it from running until the sender is idle.
If you want to have a look, this is what I got so far
Scene: MainMenu.zip (it has a jpeg as background.jpg stored under /src/Screens/MainMenu/assets, feel free to stick anything you want in it or remove the node all together)
test_main_menu.gd
extends GutTest
var TestScene = load("res://src/Screens/MainMenu/Main.tscn")
var test_scene = null
var main_menu = null
var settings_menu = null
var button_play = null
var button_settings = null
var button_quit = null
var button_settings_back = null
var _sender = null
func after_each():
_sender.release_all()
_sender.clear()
func before_each() -> void:
test_scene = partial_double(TestScene).instantiate()
add_child_autofree(test_scene)
_sender = InputSender.new(Input)
main_menu = test_scene.get_node("CanvasLayer/MainMenu")
settings_menu = test_scene.get_node("CanvasLayer/SettingsMenu")
watch_signals(main_menu)
watch_signals(settings_menu)
await wait_for_signal(main_menu.ready, 0.1, "main_menu.ready")
await wait_for_signal(settings_menu.ready, 0.1, "settings_menu.ready")
button_play = main_menu.get_node("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/ButtonPlay")
button_settings = main_menu.get_node("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/ButtonSettings")
button_quit = main_menu.get_node("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/ButtonQuit")
button_settings_back = (settings_menu.get_node("CenterContainer/PanelContainer/MarginContainer/ScrollContainer/VBoxContainer/ButtonSettingsBack"))
watch_signals(button_play)
watch_signals(button_settings)
watch_signals(button_quit)
watch_signals(button_settings_back)
await wait_for_signal(button_play.ready, 0.1, "button_play.ready")
await wait_for_signal(button_settings.ready, 0.1, "button_settings.ready")
await wait_for_signal(button_quit.ready, 0.1, "button_quit.ready")
await wait_for_signal(button_settings_back.ready, 0.1, "button_settings_back.ready")
func test_main_menu_load() -> void:
assert_eq(main_menu.visible, true)
assert_eq(settings_menu.visible, false)
func test_open_settings_menu_seconds() -> void:
var settings_button_pos = button_settings.get_global_position()
_sender.mouse_set_position(settings_button_pos, settings_button_pos)\
.wait_frames(1)\
.mouse_left_button_down(settings_button_pos, settings_button_pos)\
.hold_for(0.1)\
.wait_frames(1)
await _sender.idle
assert_eq(main_menu.visible, false)
assert_eq(settings_menu.visible, true)
func test_open_settings_menu_frames() -> void:
var settings_button_pos = button_settings.get_global_position()
_sender.mouse_set_position(settings_button_pos, settings_button_pos)\
.wait_frames(1)\
.mouse_left_button_down(settings_button_pos, settings_button_pos)\
.hold_for('1f')\
.wait_frames(1)
await _sender.idle
assert_eq(main_menu.visible, false)
assert_eq(settings_menu.visible, true)
Output
res://test/Menu/test_main_menu.gd.
* test_main_menu_load
-- Awaiting signal "ready" or for 0.1 second(s) -- main_menu.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- settings_menu.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_play.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_settings.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_quit.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_settings_back.ready
* test_open_settings_menu_seconds
-- Awaiting signal "ready" or for 0.1 second(s) -- main_menu.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- settings_menu.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_play.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_settings.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_quit.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_settings_back.ready
* test_open_settings_menu_frames
-- Awaiting signal "ready" or for 0.1 second(s) -- main_menu.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- settings_menu.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_play.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_settings.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_quit.ready
-- Awaiting signal "ready" or for 0.1 second(s) -- button_settings_back.ready
[Failed]: [true] expected to equal [false]:
at line 77
[Failed]: [false] expected to equal [true]:
at line 78
4/6 passed.
Until I started waiting for the ready signals from different buttons, it was fairly "racey" with how long you needed to wait before things could happen, but with your suggestion of hold, its now much reduced and seems stable :)
But yeah hold_frames is a really good idea, its more consistent for sure.
More frames?
What happens if you wait more frames hold_for('10f')? Does it pass?
Timing Issues
Until I started waiting for the ready signals from different buttons, it was fairly "racey" with how long you needed to wait before things could happen, but with your suggestion of hold, its now much reduced and seems stable :)
In the _ready of Main.tscn or any of its children are you
- creating any of the Nodes under test via code?
- calling anything deferred?
- calling methods that should have an
await?
I am pretty sure that the 6 awaits in before_each are never getting the signal and you end up waiting the full .1 seconds, which ends up being enough to sort things out. Adding a single await wait_seconds(.6) should have the same effect.
_ready should be called after all children have been added to the tree (_init is called top down and _ready is called bottom up). Since you are able to use get_node on test_scene that should mean that all the children of test_scene are "ready". I'm not 100% sure when the ready signal is emitted though, but all calls to _ready for test_scene and all of its children should be complete by the time add_child_autofree completes.
Finally
I absolutely love seeing people using InputSender, but if you are only checking that you are handling the button pressed correctly then the following is a better test.
func test_open_settings_menu_seconds() -> void:
button_settings.pressed.emit()
assert_eq(main_menu.visible, false)
assert_eq(settings_menu.visible, true)
If all of your buttons are standard buttons then it is overkill to test whether or not they work with the mouse (a minor case of "testing the framework"). Getting a reference to the button and making it emit the signal is enough to test that you are handling the button press correctly (ensures everything is wired and that your event handler works as expected since you should not be calling event handlers directly from code anywhere, even in tests).
If you are doing end to end testing and trying to verify that the button is in the right place and able to receive clicks (visible and enabled) then using InputSender is the right way to go. Also, if you made a button and are handling button down and button up events and emitting your own "pressed" signal based on complex situations then InputSender is the right way to go (though I would have all those tests in the button's test script and still emit the signal here).
Ok, I'm replicating the issue now and seeing inconsistent behavior (thanks for the zip). I'll have more time to look at this later.
In my testing though, the emit approach seems to be working consistently.
I am pretty sure that the 6 awaits in before_each are never getting the signal and you end up waiting the full .1 seconds, which ends up being enough to sort things out. Adding a single await wait_seconds(.6) should have the same effect.
You are right, removed all awaits wait_for_signal and turns out I don't need it if I am not interacting with the mouse at all (using your suggestion of pressing the button via button itself instead of via mouse cursor - didn't actually realise that can be done like that).
In the end doing something like await wait_for_signal(test_scene.ready, 10, "test_scene.ready") actually sits and waits for the entire 10 seconds. So you are right, this ready call is doing nothing... How come it doesn't cause a fail?
So this all can be simplified to
func before_each() -> void:
test_scene = partial_double(TestScene).instantiate()
add_child_autofree(test_scene)
main_menu = test_scene.get_node("CanvasLayer/MainMenu")
settings_menu = test_scene.get_node("CanvasLayer/SettingsMenu")
button_play = main_menu.get_node("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/ButtonPlay")
button_settings = main_menu.get_node("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/ButtonSettings")
button_quit = main_menu.get_node("CenterContainer/PanelContainer/MarginContainer/VBoxContainer/ButtonQuit")
button_settings_back = (settings_menu.get_node("CenterContainer/PanelContainer/MarginContainer/ScrollContainer/VBoxContainer/ButtonSettingsBack"))
func test_main_menu_load() -> void:
assert_eq(main_menu.visible, true)
assert_eq(settings_menu.visible, false)
func test_open_settings_menu() -> void:
button_settings.pressed.emit()
await wait_frames(1)
assert_eq(main_menu.visible, false)
assert_eq(settings_menu.visible, true)
And seems to work consistently, checked using delays that the menu actually visually goes to the settings screen. Is there any kind of screenshot functionality? Like capture screenshot at a certain point in test? Would be great for manual debugging if visually the right thing happens.
I think you could put these informations in the DOCS.
It would help a lot beginners, I was searching these information for three days already.
Thank you you put here!
Information 1
There is some stuff in the
InputSenderthat makes this a little cleaner, but the "waits" are necessary. You can string all your commands together and there iswait_framesandwait_secsbuilt into the sender. There's alsohold_forwhich can save you a "button up". You can then wait for theidlesignal on the sender.Some waits can be skipped in different scenarios (such as no wait between your button down/up). But if you want something like
just_pressedyou have to use waits (I'm pretty sure).Here's another way to write that test without using any direct calls to
wait_frames.func test_open_settings_menu() -> void: var settings_button_pos = button_settings.get_global_position() _sender.mouse_set_position(settings_button_pos, settings_button_pos)\ .wait_frames(1)\ .mouse_left_button_down(settings_button_pos, settings_button_pos)\ .hold_for('1f')\ # holds the button down for one frame, for illustration purposes, no button up needed .wait_frames(1) await _sender.idle assert_eq(main_menu.visible,false) assert_eq(settings_menu.visible,true)
Information 2
I absolutely love seeing people using
InputSender, but if you are only checking that you are handling the button pressed correctly then the following is a better test.func test_open_settings_menu_seconds() -> void: button_settings.pressed.emit() assert_eq(main_menu.visible, false) assert_eq(settings_menu.visible, true)If all of your buttons are standard buttons then it is overkill to test whether or not they work with the mouse (a minor case of "testing the framework"). Getting a reference to
I believe the second information is more simple to use, and draw attention that also exists button_up and button_down could help more people.