test_input_event.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #pragma once
  31. #include "core/input/input_event.h"
  32. #include "core/math/rect2.h"
  33. #include "core/os/memory.h"
  34. #include "core/variant/array.h"
  35. #include "tests/test_macros.h"
  36. namespace TestInputEvent {
  37. TEST_CASE("[InputEvent] Signal is emitted when device is changed") {
  38. Ref<InputEventKey> input_event;
  39. input_event.instantiate();
  40. SIGNAL_WATCH(*input_event, CoreStringName(changed));
  41. Array empty_args = { {} };
  42. input_event->set_device(1);
  43. SIGNAL_CHECK("changed", empty_args);
  44. CHECK(input_event->get_device() == 1);
  45. SIGNAL_UNWATCH(*input_event, CoreStringName(changed));
  46. }
  47. TEST_CASE("[InputEvent] Test accumulate") {
  48. Ref<InputEventMouseMotion> iemm1, iemm2;
  49. Ref<InputEventKey> iek;
  50. iemm1.instantiate(), iemm2.instantiate();
  51. iek.instantiate();
  52. iemm1->set_button_mask(MouseButtonMask::LEFT);
  53. CHECK_FALSE(iemm1->accumulate(iemm2));
  54. iemm2->set_button_mask(MouseButtonMask::LEFT);
  55. CHECK(iemm1->accumulate(iemm2));
  56. CHECK_FALSE(iemm1->accumulate(iek));
  57. CHECK_FALSE(iemm2->accumulate(iek));
  58. }
  59. TEST_CASE("[InputEvent][SceneTree] Test methods that interact with the InputMap") {
  60. const String mock_action = "mock_action";
  61. Ref<InputEventJoypadMotion> iejm;
  62. iejm.instantiate();
  63. InputMap::get_singleton()->add_action(mock_action, 0.5);
  64. InputMap::get_singleton()->action_add_event(mock_action, iejm);
  65. CHECK(iejm->is_action_type());
  66. CHECK(iejm->is_action(mock_action));
  67. CHECK(iejm->is_action_released(mock_action));
  68. CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.0f));
  69. iejm->set_axis_value(0.8f);
  70. // Since deadzone is 0.5, action_strength grows linearly from 0.5 to 1.0.
  71. CHECK(Math::is_equal_approx(iejm->get_action_strength(mock_action), 0.6f));
  72. CHECK(Math::is_equal_approx(iejm->get_action_raw_strength(mock_action), 0.8f));
  73. CHECK(iejm->is_action_pressed(mock_action));
  74. InputMap::get_singleton()->erase_action(mock_action);
  75. }
  76. TEST_CASE("[InputEvent] Test xformed_by") {
  77. Ref<InputEventMouseMotion> iemm1;
  78. iemm1.instantiate();
  79. iemm1->set_position(Vector2(0.0f, 0.0f));
  80. Transform2D transform;
  81. transform = transform.translated(Vector2(2.0f, 3.0f));
  82. Ref<InputEventMouseMotion> iemm2 = iemm1->xformed_by(transform);
  83. CHECK(iemm2->get_position().is_equal_approx(Vector2(2.0f, 3.0f)));
  84. }
  85. } // namespace TestInputEvent