Debian Codegraph @

tests/scene/test_timer.h

raw symbols SCIP
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
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 // check default
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 // wait time remains unchanged if we attempt to set it negative or zero
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 // check default
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 // check default
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 // check default
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 // the wait time and time left remains unchanged when started with a negative start time
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 // check default
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 } // namespace TestTimer