Testing a Rust and QML App
Test QML and native Rust types deterministically on a virtual
clock using ljos test, App::build, TestWindow, and render_png.
After reading this page, you will be able to verify your QML files and native type dump, run QML
TestCase suites on a virtual clock, write headless Rust unit and integration tests against Engine and App::build, drive custom visual items and accessibility trees with TestWindow, and render deterministic PNG screenshots.Before running tests,
ljos check compiles a QML directory and reports compiler errors, warnings and static lints, exiting with a non-zero status if any file fails to compile:In a Rust application that compiles QML ahead of time, keep a test in
tests/types.rs that verifies ljostypes.json matches your register function:Run Cargo with
LJOS_TYPES=write whenever you change a native type's properties, signals, methods or enums, and commit the updated ljostypes.json:If you want a Rust test to compile a directory of QML files and assert that there are zero warnings or errors without printing to standard error, enable the
dev-qml feature in [dev-dependencies] and call ljos::dev::compile_quiet(&dirs, Some(&engine)). It returns the compiled MapSource along with every Diagnostic; MapSource::keys() and MapSource::failures() list the components that succeeded and those that failed.ljos test runs QML TestCase components in tst_*.qml files on a virtual clock without opening a platform window. Timers, animations and wait() calls advance virtual time immediately without wall-clock sleeps, and the exit status is the number of failed tests:Pass
--filter <NAME> (which may be repeated) to run only a specific test case, function or data tag, and --import <DIR> to add a module import path used only by tests.A
tst_*.qml file uses import QtTest:You test native Rust types through the same
Engine interface that QML uses. For a full application configured with ljos::App, calling App::build instead of App::run constructs the Engine and instantiates the root component headlessly so a test can inspect properties and invoke methods.For focused integration tests of a native module against inline QML, register your types on
ljos::engine(), compile the QML snippet with ljos_compiler and EngineOracle, and assert on the resulting object state:Headless tests run against isolated, in-process stand-ins for every platform service:
Home directory isolation.
Platform resolves standard directories through PathEnv::for_process, which automatically gives every Rust test binary (any binary under target/*/deps, or any run guarded by LJOS_GUARD_HOME) a fresh temporary directory for HOME and XDG paths. Settings and LocalStorage in tests never read or write the developer's real home directory.Service stand-ins. A headless
Engine uses an in-memory clipboard, records URLs passed to Qt.openUrlExternally and badge counts passed to Platform::set_badge_number without touching the desktop, fails network fetches with FetchError::NoNetwork unless set_fetcher is called, and uses NoSensors unless the test installs FakeSensors. Without a display host, Screen reports a single 800×800 screen and Qt.application is active.When you write a custom visual item with
ljos::item or want to test keyboard, pointer, wheel and IME input along with accessibility, enable the testing feature on ljos and use ljos::testing::TestWindow:Virtual clock and input.
TestWindow hosts a scene without a platform window, delivers key presses, pointer moves and clicks, wheel scrolling and IME composition, and resizes the scene with resize on the virtual clock.Waiting on compute pool tasks. If an item under test spawns work on the compute pool with
ljos::task::spawn, TestWindow::advance waits for outstanding task replies before advancing time. If the background work waits on a mock server on the test thread, register a callback with serve_while_waiting (backed by Platform::finish_with) so the test thread can serve requests while waiting.Accessibility tree inspection. Interactive elements are addressed by role and accessible name rather than coordinates. Call
set_accessibility_active on TestWindow to attach an accessibility observer, and inspect the resulting AccessibleNode tree with accessibility_tree, accessible_node and accessible_item. Each node reports its role word, computed name, description, state words (such as "checked" or "disabled") and focused, while ljos::item's child_items, overlay, map_to_scene and scene_rect let you verify geometry.With the
render feature enabled on ljos, a Rust test can render the scene returned by App::build to a PNG image:1.
Install
ljos::render::Picture before creating the root so the root's timers and animations run on the virtual clock.2.
Call
ljos::render::set_fonts to select the bundled font (so text renders identically on every machine) or the system's fonts.3.
Call
ljos::render::render_png(&engine, root, (width, height), settle_ms) to advance the virtual clock by settle_ms milliseconds and render the scene to a PNG.This is the same renderer that powers the
ljos render CLI command.