test_timer.h 7.2 KB

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