Fix the Rust/C++ Timer API to be more convenient to use

Allow calling restart() on a repeated timer if it has been previously stopped.
This commit is contained in:
Simon Hausmann 2022-01-05 11:15:15 +01:00 committed by Simon Hausmann
parent ced05732a8
commit 083ae5692b
4 changed files with 83 additions and 16 deletions

View file

@ -77,6 +77,57 @@ TEST_CASE("C++ Restart Singleshot Timer")
REQUIRE(timer_was_running);
}
TEST_CASE("C++ Restart Repeated Timer")
{
int timer_triggered = 0;
sixtyfps::Timer timer;
timer.start(sixtyfps::TimerMode::Repeated, std::chrono::milliseconds(30),
[&]() { timer_triggered++; });
REQUIRE(timer_triggered == 0);
bool timer_was_running = false;
sixtyfps::Timer::single_shot(std::chrono::milliseconds(500), [&]() {
timer_was_running = timer.running();
sixtyfps::quit_event_loop();
});
sixtyfps::run_event_loop();
REQUIRE(timer_triggered > 1);
REQUIRE(timer_was_running);
timer_was_running = false;
timer_triggered = 0;
timer.stop();
sixtyfps::Timer::single_shot(std::chrono::milliseconds(500), [&]() {
timer_was_running = timer.running();
sixtyfps::quit_event_loop();
});
sixtyfps::run_event_loop();
REQUIRE(timer_triggered == 0);
REQUIRE(!timer_was_running);
timer_was_running = false;
timer_triggered = 0;
timer.restart();
sixtyfps::Timer::single_shot(std::chrono::milliseconds(500), [&]() {
timer_was_running = timer.running();
sixtyfps::quit_event_loop();
});
sixtyfps::run_event_loop();
REQUIRE(timer_triggered > 1);
REQUIRE(timer_was_running);
}
TEST_CASE("Quit from event")
{
int called = 0;