mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 22:31:14 +00:00
Complete the C++ Timer API
With this patch it matches the Rust API, with start(), restart(), running() and a default constructor.
This commit is contained in:
parent
3d64be7fb6
commit
f557a27556
3 changed files with 134 additions and 9 deletions
|
@ -7,7 +7,7 @@
|
|||
#include <sixtyfps.h>
|
||||
#include <thread>
|
||||
|
||||
TEST_CASE("C++ Timers")
|
||||
TEST_CASE("C++ Singleshot Timers")
|
||||
{
|
||||
using namespace sixtyfps;
|
||||
int called = 0;
|
||||
|
@ -20,6 +20,62 @@ TEST_CASE("C++ Timers")
|
|||
REQUIRE(called == 10);
|
||||
}
|
||||
|
||||
TEST_CASE("C++ 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);
|
||||
}
|
||||
|
||||
TEST_CASE("C++ Restart Singleshot Timer")
|
||||
{
|
||||
int timer_triggered = 0;
|
||||
sixtyfps::Timer timer;
|
||||
|
||||
timer.start(sixtyfps::TimerMode::SingleShot, 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_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")
|
||||
{
|
||||
|
@ -33,7 +89,6 @@ TEST_CASE("Quit from event")
|
|||
REQUIRE(called == 10);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Event from thread")
|
||||
{
|
||||
std::atomic<int> called = 0;
|
||||
|
@ -50,15 +105,13 @@ TEST_CASE("Event from thread")
|
|||
t.join();
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Blocking Event from thread")
|
||||
{
|
||||
std::atomic<int> called = 0;
|
||||
auto t = std::thread([&] {
|
||||
// test returning a, unique_ptr because it is movable-only
|
||||
std::unique_ptr foo = sixtyfps::blocking_invoke_from_event_loop([&] {
|
||||
return std::make_unique<int>(42);
|
||||
});
|
||||
std::unique_ptr foo = sixtyfps::blocking_invoke_from_event_loop(
|
||||
[&] { return std::make_unique<int>(42); });
|
||||
called = *foo;
|
||||
int xxx = 123;
|
||||
sixtyfps::blocking_invoke_from_event_loop([&] {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue