| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/input/input_event.h" |
| 34 |
#include "core/math/rect2.h" |
| 35 |
#include "core/os/memory.h" |
| 36 |
#include "core/variant/array.h" |
| 37 |
|
| 38 |
#include "tests/test_macros.h" |
| 39 |
|
| 40 |
namespace TestInputEvent { |
| 41 |
TEST_CASE("[InputEvent] Signal is emitted when device is changed") { |
| 42 |
Ref<InputEventKey> input_event; |
| 43 |
input_event.instantiate(); |
| 44 |
|
| 45 |
SIGNAL_WATCH(*input_event, CoreStringName(changed)); |
| 46 |
|
| 47 |
Array empty_args = { {} }; |
| 48 |
input_event->set_device(1); |
| 49 |
|
| 50 |
SIGNAL_CHECK("changed", empty_args); |
| 51 |
CHECK(input_event->get_device() == 1); |
| 52 |
|
| 53 |
SIGNAL_UNWATCH(*input_event, CoreStringName(changed)); |
| 54 |
} |
| 55 |
|
| 56 |
TEST_CASE("[InputEvent] Test accumulate") { |
| 57 |
Ref<InputEventMouseMotion> iemm1, iemm2; |
| 58 |
Ref<InputEventKey> iek; |
| 59 |
|
| 60 |
iemm1.instantiate(), iemm2.instantiate(); |
| 61 |
iek.instantiate(); |
| 62 |
|
| 63 |
iemm1->set_button_mask(MouseButtonMask::LEFT); |
| 64 |
|
| 65 |
CHECK_FALSE(iemm1->accumulate(iemm2)); |
| 66 |
|
| 67 |
iemm2->set_button_mask(MouseButtonMask::LEFT); |
| 68 |
|
| 69 |
CHECK(iemm1->accumulate(iemm2)); |
| 70 |
|
| 71 |
CHECK_FALSE(iemm1->accumulate(iek)); |
| 72 |
CHECK_FALSE(iemm2->accumulate(iek)); |
| 73 |
} |
| 74 |
|
| 75 |
TEST_CASE("[InputEvent][SceneTree] Test methods that interact with the InputMap") { |
| 76 |
const String mock_action = "mock_action"; |
| 77 |
Ref<InputEventJoypadMotion> iejm; |
| 78 |
iejm.instantiate(); |
| 79 |
|
| 80 |
InputMap::get_singleton()->add_action(mock_action, 0.5); |
| 81 |
InputMap::get_singleton()->action_add_event(mock_action, iejm); |
| 82 |
|
| 83 |
CHECK(iejm->is_action_type()); |
| 84 |
CHECK(iejm->is_action(mock_action)); |
| 85 |
|
| 86 |
CHECK(iejm->is_action_released(mock_action)); |
| 87 |
CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.0f)); |
| 88 |
|
| 89 |
iejm->set_axis_value(0.8f); |
| 90 |
|
| 91 |
CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.6f)); |
| 92 |
CHECK(Math::is_equal_approx(iejm->get_action_raw_strength(mock_action), 0.8f)); |
| 93 |
CHECK(iejm->is_action_pressed(mock_action)); |
| 94 |
|
| 95 |
InputMap::get_singleton()->erase_action(mock_action); |
| 96 |
} |
| 97 |
|
| 98 |
TEST_CASE("[InputEvent] Test xformed_by") { |
| 99 |
Ref<InputEventMouseMotion> iemm1; |
| 100 |
iemm1.instantiate(); |
| 101 |
|
| 102 |
iemm1->set_position(Vector2(0.0f, 0.0f)); |
| 103 |
Transform2D transform; |
| 104 |
transform = transform.translated(Vector2(2.0f, 3.0f)); |
| 105 |
|
| 106 |
Ref<InputEventMouseMotion> iemm2 = iemm1->xformed_by(transform); |
| 107 |
|
| 108 |
CHECK(iemm2->get_position().is_equal_approx(Vector2(2.0f, 3.0f))); |
| 109 |
} |
| 110 |
} |