| 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 "scene/main/timer.h" |
| 34 |
|
| 35 |
#include "tests/test_macros.h" |
| 36 |
|
| 37 |
namespace TestTimer { |
| 38 |
|
| 39 |
TEST_CASE("[SceneTree][Timer] Check Timer Setters and Getters") { |
| 40 |
Timer *test_timer = memnew(Timer); |
| 41 |
|
| 42 |
SUBCASE("[Timer] Timer set and get wait time") { |
| 43 |
|
| 44 |
CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 1.0)); |
| 45 |
|
| 46 |
test_timer->set_wait_time(50.0); |
| 47 |
CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 50.0)); |
| 48 |
|
| 49 |
test_timer->set_wait_time(42.0); |
| 50 |
CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 42.0)); |
| 51 |
|
| 52 |
|
| 53 |
ERR_PRINT_OFF; |
| 54 |
test_timer->set_wait_time(-22.0); |
| 55 |
ERR_PRINT_ON; |
| 56 |
CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 42.0)); |
| 57 |
|
| 58 |
ERR_PRINT_OFF; |
| 59 |
test_timer->set_wait_time(0.0); |
| 60 |
ERR_PRINT_ON; |
| 61 |
CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 42.0)); |
| 62 |
} |
| 63 |
|
| 64 |
SUBCASE("[Timer] Timer set and get one shot") { |
| 65 |
|
| 66 |
CHECK(test_timer->is_one_shot() == false); |
| 67 |
|
| 68 |
test_timer->set_one_shot(true); |
| 69 |
CHECK(test_timer->is_one_shot() == true); |
| 70 |
|
| 71 |
test_timer->set_one_shot(false); |
| 72 |
CHECK(test_timer->is_one_shot() == false); |
| 73 |
} |
| 74 |
|
| 75 |
SUBCASE("[Timer] Timer set and get autostart") { |
| 76 |
|
| 77 |
CHECK(test_timer->has_autostart() == false); |
| 78 |
|
| 79 |
test_timer->set_autostart(true); |
| 80 |
CHECK(test_timer->has_autostart() == true); |
| 81 |
|
| 82 |
test_timer->set_autostart(false); |
| 83 |
CHECK(test_timer->has_autostart() == false); |
| 84 |
} |
| 85 |
|
| 86 |
SUBCASE("[Timer] Timer start and stop") { |
| 87 |
test_timer->set_autostart(false); |
| 88 |
} |
| 89 |
|
| 90 |
SUBCASE("[Timer] Timer set and get paused") { |
| 91 |
|
| 92 |
CHECK(test_timer->is_paused() == false); |
| 93 |
|
| 94 |
test_timer->set_paused(true); |
| 95 |
CHECK(test_timer->is_paused() == true); |
| 96 |
|
| 97 |
test_timer->set_paused(false); |
| 98 |
CHECK(test_timer->is_paused() == false); |
| 99 |
} |
| 100 |
|
| 101 |
memdelete(test_timer); |
| 102 |
} |
| 103 |
|
| 104 |
TEST_CASE("[SceneTree][Timer] Check Timer Start and Stop") { |
| 105 |
Timer *test_timer = memnew(Timer); |
| 106 |
|
| 107 |
SUBCASE("[Timer] Timer start and stop") { |
| 108 |
SceneTree::get_singleton()->get_root()->add_child(test_timer); |
| 109 |
|
| 110 |
test_timer->start(5.0); |
| 111 |
|
| 112 |
CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 5.0)); |
| 113 |
CHECK(Math::is_equal_approx(test_timer->get_time_left(), 5.0)); |
| 114 |
|
| 115 |
test_timer->start(-2.0); |
| 116 |
|
| 117 |
|
| 118 |
CHECK(Math::is_equal_approx(test_timer->get_wait_time(), 5.0)); |
| 119 |
CHECK(Math::is_equal_approx(test_timer->get_time_left(), 5.0)); |
| 120 |
|
| 121 |
test_timer->stop(); |
| 122 |
CHECK(test_timer->is_processing() == false); |
| 123 |
CHECK(test_timer->has_autostart() == false); |
| 124 |
} |
| 125 |
|
| 126 |
memdelete(test_timer); |
| 127 |
} |
| 128 |
|
| 129 |
TEST_CASE("[SceneTree][Timer] Check Timer process callback") { |
| 130 |
Timer *test_timer = memnew(Timer); |
| 131 |
|
| 132 |
SUBCASE("[Timer] Timer process callback") { |
| 133 |
|
| 134 |
CHECK(test_timer->get_timer_process_callback() == Timer::TimerProcessCallback::TIMER_PROCESS_IDLE); |
| 135 |
|
| 136 |
test_timer->set_timer_process_callback(Timer::TimerProcessCallback::TIMER_PROCESS_PHYSICS); |
| 137 |
CHECK(test_timer->get_timer_process_callback() == Timer::TimerProcessCallback::TIMER_PROCESS_PHYSICS); |
| 138 |
|
| 139 |
test_timer->set_timer_process_callback(Timer::TimerProcessCallback::TIMER_PROCESS_IDLE); |
| 140 |
CHECK(test_timer->get_timer_process_callback() == Timer::TimerProcessCallback::TIMER_PROCESS_IDLE); |
| 141 |
} |
| 142 |
|
| 143 |
memdelete(test_timer); |
| 144 |
} |
| 145 |
|
| 146 |
TEST_CASE("[SceneTree][Timer] Check Timer timeout signal") { |
| 147 |
Timer *test_timer = memnew(Timer); |
| 148 |
SceneTree::get_singleton()->get_root()->add_child(test_timer); |
| 149 |
|
| 150 |
test_timer->set_process(true); |
| 151 |
test_timer->set_physics_process(true); |
| 152 |
|
| 153 |
SUBCASE("[Timer] Timer process timeout signal must be emitted") { |
| 154 |
SIGNAL_WATCH(test_timer, SNAME("timeout")); |
| 155 |
test_timer->start(0.1); |
| 156 |
|
| 157 |
SceneTree::get_singleton()->process(0.2); |
| 158 |
|
| 159 |
Array signal_args = { {} }; |
| 160 |
SIGNAL_CHECK(SNAME("timeout"), signal_args); |
| 161 |
|
| 162 |
SIGNAL_UNWATCH(test_timer, SNAME("timeout")); |
| 163 |
} |
| 164 |
|
| 165 |
SUBCASE("[Timer] Timer process timeout signal must not be emitted") { |
| 166 |
SIGNAL_WATCH(test_timer, SNAME("timeout")); |
| 167 |
test_timer->start(0.1); |
| 168 |
|
| 169 |
SceneTree::get_singleton()->process(0.05); |
| 170 |
|
| 171 |
Array signal_args = { {} }; |
| 172 |
SIGNAL_CHECK_FALSE(SNAME("timeout")); |
| 173 |
|
| 174 |
SIGNAL_UNWATCH(test_timer, SNAME("timeout")); |
| 175 |
} |
| 176 |
|
| 177 |
test_timer->set_timer_process_callback(Timer::TimerProcessCallback::TIMER_PROCESS_PHYSICS); |
| 178 |
|
| 179 |
SUBCASE("[Timer] Timer physics process timeout signal must be emitted") { |
| 180 |
SIGNAL_WATCH(test_timer, SNAME("timeout")); |
| 181 |
test_timer->start(0.1); |
| 182 |
|
| 183 |
SceneTree::get_singleton()->physics_process(0.2); |
| 184 |
|
| 185 |
Array signal_args = { {} }; |
| 186 |
SIGNAL_CHECK(SNAME("timeout"), signal_args); |
| 187 |
|
| 188 |
SIGNAL_UNWATCH(test_timer, SNAME("timeout")); |
| 189 |
} |
| 190 |
|
| 191 |
SUBCASE("[Timer] Timer physics process timeout signal must not be emitted") { |
| 192 |
SIGNAL_WATCH(test_timer, SNAME("timeout")); |
| 193 |
test_timer->start(0.1); |
| 194 |
|
| 195 |
SceneTree::get_singleton()->physics_process(0.05); |
| 196 |
|
| 197 |
Array signal_args = { {} }; |
| 198 |
SIGNAL_CHECK_FALSE(SNAME("timeout")); |
| 199 |
|
| 200 |
SIGNAL_UNWATCH(test_timer, SNAME("timeout")); |
| 201 |
} |
| 202 |
|
| 203 |
memdelete(test_timer); |
| 204 |
} |
| 205 |
|
| 206 |
} |