diff --git a/Cargo.lock b/Cargo.lock index bce11d2cb0b3b..8016618d972aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7107,6 +7107,16 @@ dependencies = [ "rerun", ] +[[package]] +name = "test_ui_wakeup" +version = "0.19.0-alpha.1+dev" +dependencies = [ + "anyhow", + "clap", + "re_log", + "rerun", +] + [[package]] name = "thiserror" version = "1.0.63" diff --git a/crates/viewer/re_viewer/src/app.rs b/crates/viewer/re_viewer/src/app.rs index fae36b6cf3d95..9702e09a408c6 100644 --- a/crates/viewer/re_viewer/src/app.rs +++ b/crates/viewer/re_viewer/src/app.rs @@ -1034,6 +1034,8 @@ impl App { let start = web_time::Instant::now(); while let Some((channel_source, msg)) = self.rx.try_recv() { + re_log::trace!("Received a message from {channel_source:?}"); // Used by `test_ui_wakeup` test app! + let msg = match msg.payload { re_smart_channel::SmartMessagePayload::Msg(msg) => msg, diff --git a/tests/rust/test_ui_wakeup/Cargo.toml b/tests/rust/test_ui_wakeup/Cargo.toml new file mode 100644 index 0000000000000..b70252ac2f0bf --- /dev/null +++ b/tests/rust/test_ui_wakeup/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "test_ui_wakeup" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +publish = false + +[lints] +workspace = true + +[dependencies] +re_log = { workspace = true, features = ["setup"] } +rerun = { path = "../../../crates/top/rerun", features = [ + "clap", + "web_viewer", +] } + +anyhow.workspace = true +clap = { workspace = true, features = ["derive"] } diff --git a/tests/rust/test_ui_wakeup/src/main.rs b/tests/rust/test_ui_wakeup/src/main.rs new file mode 100644 index 0000000000000..491b8121c5acc --- /dev/null +++ b/tests/rust/test_ui_wakeup/src/main.rs @@ -0,0 +1,63 @@ +//! Test that the Rerun Viewer UI wakes up as new messages arrive, +//! even if the viewer is hidden. +//! +//! ## Test setup - build the viewer +//! * `pixi run rerun` +//! * `pixi run rerun-web` +//! +//! ## Test matrix +//! * Run `cargo r -p test_ui_wakeup` and test: +//! * That the viewer wakes up in the background when it's alt-tabbed +//! * That the viewer wakes up when minimized (it should log "Received a message from…") +//! * Run `cargo r -p test_ui_wakeup -- --serve` and test: +//! * The viewer wakes up when browser is alt-tabbed away +//! * Switch to a different browser tab, send a few messages, switch back. The messages should be there +//! (this is not a conclusive test, as the messages might have been received on tab select) + +use std::io::Read as _; + +#[derive(Debug, clap::Parser)] +#[clap(author, version, about)] +struct Args { + #[command(flatten)] + rerun: rerun::clap::RerunArgs, +} + +fn main() -> anyhow::Result<()> { + re_log::setup_logging(); + + use clap::Parser as _; + let args = Args::parse(); + + // This is so that re_viewer logs incoming messages: + let rust_log = "info,re_viewer=trace"; + eprintln!("Setting RUST_LOG={rust_log}"); + std::env::set_var("RUST_LOG", rust_log); + + println!("Starting Viewer…"); + let (rec, _serve_guard) = args.rerun.init("rerun_example_ui_wakeup")?; + + // Wait out some log spam from the viewer starting: + std::thread::sleep(std::time::Duration::from_secs(1)); + + println!("Now put the viewer in the background (alt-tab, minimize, put in background tab, etc"); + + for i in 0..usize::MAX { + println!("Sending message number {i}…"); + rec.log( + "Text", + &rerun::TextDocument::new(format!("This is message number {i}")), + )?; + println!("Press ENTER to send more data to the viewer"); + + wait_from_enter(); + } + + Ok(()) +} + +fn wait_from_enter() { + let _ = std::io::stdin() + .read(&mut [0u8]) + .expect("Failed to read from stdin"); +}