test_input_event.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**************************************************************************/
  2. /* test_input_event.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_INPUT_EVENT_H
  31. #define TEST_INPUT_EVENT_H
  32. #include "core/input/input_event.h"
  33. #include "core/math/rect2.h"
  34. #include "core/os/memory.h"
  35. #include "core/variant/array.h"
  36. #include "tests/test_macros.h"
  37. namespace TestInputEvent {
  38. TEST_CASE("[InputEvent] Signal is emitted when device is changed") {
  39. Ref<InputEventKey> input_event;
  40. input_event.instantiate();
  41. SIGNAL_WATCH(*input_event, CoreStringName(changed));
  42. Array args1;
  43. Array empty_args;
  44. empty_args.push_back(args1);
  45. input_event->set_device(1);
  46. SIGNAL_CHECK("changed", empty_args);
  47. CHECK(input_event->get_device() == 1);
  48. SIGNAL_UNWATCH(*input_event, CoreStringName(changed));
  49. }
  50. TEST_CASE("[InputEvent] Test accumulate") {
  51. Ref<InputEventMouseMotion> iemm1, iemm2;
  52. Ref<InputEventKey> iek;
  53. iemm1.instantiate(), iemm2.instantiate();
  54. iek.instantiate();
  55. iemm1->set_button_mask(MouseButtonMask::LEFT);
  56. CHECK_FALSE(iemm1->accumulate(iemm2));
  57. iemm2->set_button_mask(MouseButtonMask::LEFT);
  58. CHECK(iemm1->accumulate(iemm2));
  59. CHECK_FALSE(iemm1->accumulate(iek));
  60. CHECK_FALSE(iemm2->accumulate(iek));
  61. }
  62. TEST_CASE("[InputEvent][SceneTree] Test methods that interact with the InputMap") {
  63. const String mock_action = "mock_action";
  64. Ref<InputEventJoypadMotion> iejm;
  65. iejm.instantiate();
  66. InputMap::get_singleton()->add_action(mock_action, 0.5);
  67. InputMap::get_singleton()->action_add_event(mock_action, iejm);
  68. CHECK(iejm->is_action_type());
  69. CHECK(iejm->is_action(mock_action));
  70. CHECK(iejm->is_action_released(mock_action));
  71. CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.0f));
  72. iejm->set_axis_value(0.8f);
  73. // Since deadzone is 0.5, action_strength grows linearly from 0.5 to 1.0.
  74. CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.6f));
  75. CHECK(Math::is_equal_approx(iejm->get_action_raw_strength(mock_action), 0.8f));
  76. CHECK(iejm->is_action_pressed(mock_action));
  77. InputMap::get_singleton()->erase_action(mock_action);
  78. }
  79. TEST_CASE("[InputEvent] Test xformed_by") {
  80. Ref<InputEventMouseMotion> iemm1;
  81. iemm1.instantiate();
  82. iemm1->set_position(Vector2(0.0f, 0.0f));
  83. Transform2D transform;
  84. transform = transform.translated(Vector2(2.0f, 3.0f));
  85. Ref<InputEventMouseMotion> iemm2 = iemm1->xformed_by(transform);
  86. CHECK(iemm2->get_position().is_equal_approx(Vector2(2.0f, 3.0f)));
  87. }
  88. } // namespace TestInputEvent
  89. #endif // TEST_INPUT_EVENT_H