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