slint/api/sixtyfps-cpp/tests/datastructures.cpp
Simon Hausmann 4cbcf2611f Fix recurring C++ timers
* sixtyfps_timer_start needs to *take* the timer id out of the Rust
  timer to avoid that the subsequent drop stops the timer again
* For the Qt event loop, call `timer_event()` once before entering
  QCoreApplication::exec(), to schedule any timers that were started
  beforehand.
* Added a way to quit the event loop gently, in order to use that
  from the C++ unit test.
2021-03-25 20:07:12 +01:00

76 lines
No EOL
1.9 KiB
C++

/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
#include <chrono>
#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"
#include <sixtyfps.h>
SCENARIO("SharedString API")
{
sixtyfps::SharedString str;
REQUIRE(str.empty());
SECTION("Construct from string_view")
{
std::string foo("Foo");
std::string_view foo_view(foo);
str = foo_view;
REQUIRE(str == "Foo");
}
}
TEST_CASE("Basic SharedVector API", "[vector]")
{
sixtyfps::SharedVector<int> vec;
REQUIRE(vec.empty());
SECTION("Initializer list")
{
sixtyfps::SharedVector<int> vec({ 1, 4, 10 });
REQUIRE(vec.size() == 3);
REQUIRE(vec[0] == 1);
REQUIRE(vec[1] == 4);
REQUIRE(vec[2] == 10);
}
}
TEST_CASE("Property Tracker")
{
using namespace sixtyfps;
PropertyTracker tracker1;
PropertyTracker tracker2;
Property<int> prop(42);
auto r = tracker1.evaluate([&]() { return tracker2.evaluate([&]() { return prop.get(); }); });
REQUIRE(r == 42);
prop.set(1);
REQUIRE(tracker2.is_dirty());
REQUIRE(tracker1.is_dirty());
r = tracker1.evaluate(
[&]() { return tracker2.evaluate_as_dependency_root([&]() { return prop.get(); }); });
REQUIRE(r == 1);
prop.set(100);
REQUIRE(tracker2.is_dirty());
REQUIRE(!tracker1.is_dirty());
}
TEST_CASE("C++ Timers")
{
using namespace sixtyfps;
Timer testTimer(std::chrono::milliseconds(16), []() { sixtyfps::quit_event_loop(); });
sixtyfps::run_event_loop();
}