test_timer.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**************************************************************************/
  2. /* test_timer.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 "scene/main/timer.h"
  32. #include "tests/test_macros.h"
  33. namespace TestTimer {
  34. TEST_CASE("[SceneTree][Timer] Check Timer Setters and Getters") {
  35. Timer *test_timer = memnew(Timer);
  36. SUBCASE("[Timer] Timer set and get wait time") {
  37. // check default
  38. CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 1.0));
  39. test_timer->set_wait_time(50.0);
  40. CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 50.0));
  41. test_timer->set_wait_time(42.0);
  42. CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 42.0));
  43. // wait time remains unchanged if we attempt to set it negative or zero
  44. ERR_PRINT_OFF;
  45. test_timer->set_wait_time(-22.0);
  46. ERR_PRINT_ON;
  47. CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 42.0));
  48. ERR_PRINT_OFF;
  49. test_timer->set_wait_time(0.0);
  50. ERR_PRINT_ON;
  51. CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 42.0));
  52. }
  53. SUBCASE("[Timer] Timer set and get one shot") {
  54. // check default
  55. CHECK(test_timer->is_one_shot() == false);
  56. test_timer->set_one_shot(true);
  57. CHECK(test_timer->is_one_shot() == true);
  58. test_timer->set_one_shot(false);
  59. CHECK(test_timer->is_one_shot() == false);
  60. }
  61. SUBCASE("[Timer] Timer set and get autostart") {
  62. // check default
  63. CHECK(test_timer->has_autostart() == false);
  64. test_timer->set_autostart(true);
  65. CHECK(test_timer->has_autostart() == true);
  66. test_timer->set_autostart(false);
  67. CHECK(test_timer->has_autostart() == false);
  68. }
  69. SUBCASE("[Timer] Timer start and stop") {
  70. test_timer->set_autostart(false);
  71. }
  72. SUBCASE("[Timer] Timer set and get paused") {
  73. // check default
  74. CHECK(test_timer->is_paused() == false);
  75. test_timer->set_paused(true);
  76. CHECK(test_timer->is_paused() == true);
  77. test_timer->set_paused(false);
  78. CHECK(test_timer->is_paused() == false);
  79. }
  80. memdelete(test_timer);
  81. }
  82. TEST_CASE("[SceneTree][Timer] Check Timer Start and Stop") {
  83. Timer *test_timer = memnew(Timer);
  84. SUBCASE("[Timer] Timer start and stop") {
  85. SceneTree::get_singleton()->get_root()->add_child(test_timer);
  86. test_timer->start(5.0);
  87. CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 5.0));
  88. CHECK(Math::is_equal_approx(test_timer->get_time_left(), 5.0));
  89. test_timer->start(-2.0);
  90. // the wait time and time left remains unchanged when started with a negative start time
  91. CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 5.0));
  92. CHECK(Math::is_equal_approx(test_timer->get_time_left(), 5.0));
  93. test_timer->stop();
  94. CHECK(test_timer->is_processing() == false);
  95. CHECK(test_timer->has_autostart() == false);
  96. }
  97. memdelete(test_timer);
  98. }
  99. TEST_CASE("[SceneTree][Timer] Check Timer process callback") {
  100. Timer *test_timer = memnew(Timer);
  101. SUBCASE("[Timer] Timer process callback") {
  102. // check default
  103. CHECK(test_timer->get_timer_process_callback() == Timer::TimerProcessCallback::TIMER_PROCESS_IDLE);
  104. test_timer->set_timer_process_callback(Timer::TimerProcessCallback::TIMER_PROCESS_PHYSICS);
  105. CHECK(test_timer->get_timer_process_callback() == Timer::TimerProcessCallback::TIMER_PROCESS_PHYSICS);
  106. test_timer->set_timer_process_callback(Timer::TimerProcessCallback::TIMER_PROCESS_IDLE);
  107. CHECK(test_timer->get_timer_process_callback() == Timer::TimerProcessCallback::TIMER_PROCESS_IDLE);
  108. }
  109. memdelete(test_timer);
  110. }
  111. TEST_CASE("[SceneTree][Timer] Check Timer timeout signal") {
  112. Timer *test_timer = memnew(Timer);
  113. SceneTree::get_singleton()->get_root()->add_child(test_timer);
  114. test_timer->set_process(true);
  115. test_timer->set_physics_process(true);
  116. SUBCASE("[Timer] Timer process timeout signal must be emitted") {
  117. SIGNAL_WATCH(test_timer, SNAME("timeout"));
  118. test_timer->start(0.1);
  119. SceneTree::get_singleton()->process(0.2);
  120. Array signal_args = { {} };
  121. SIGNAL_CHECK(SNAME("timeout"), signal_args);
  122. SIGNAL_UNWATCH(test_timer, SNAME("timeout"));
  123. }
  124. SUBCASE("[Timer] Timer process timeout signal must not be emitted") {
  125. SIGNAL_WATCH(test_timer, SNAME("timeout"));
  126. test_timer->start(0.1);
  127. SceneTree::get_singleton()->process(0.05);
  128. Array signal_args = { {} };
  129. SIGNAL_CHECK_FALSE(SNAME("timeout"));
  130. SIGNAL_UNWATCH(test_timer, SNAME("timeout"));
  131. }
  132. test_timer->set_timer_process_callback(Timer::TimerProcessCallback::TIMER_PROCESS_PHYSICS);
  133. SUBCASE("[Timer] Timer physics process timeout signal must be emitted") {
  134. SIGNAL_WATCH(test_timer, SNAME("timeout"));
  135. test_timer->start(0.1);
  136. SceneTree::get_singleton()->physics_process(0.2);
  137. Array signal_args = { {} };
  138. SIGNAL_CHECK(SNAME("timeout"), signal_args);
  139. SIGNAL_UNWATCH(test_timer, SNAME("timeout"));
  140. }
  141. SUBCASE("[Timer] Timer physics process timeout signal must not be emitted") {
  142. SIGNAL_WATCH(test_timer, SNAME("timeout"));
  143. test_timer->start(0.1);
  144. SceneTree::get_singleton()->physics_process(0.05);
  145. Array signal_args = { {} };
  146. SIGNAL_CHECK_FALSE(SNAME("timeout"));
  147. SIGNAL_UNWATCH(test_timer, SNAME("timeout"));
  148. }
  149. memdelete(test_timer);
  150. }
  151. } // namespace TestTimer