Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114use burokku::{ extension::Registry, runtime::rquickjs::Ctx, test::{ElementState, InputEvent, MouseButton, PhysicalPosition}, Burokku, BurokkuError, TestApp, TestBackend,};
/// Declares `findText(content)`, the `text` element whose text is `content`.const FIND_TEXT: &str = r#"function findText(content) { const pending = [app]; while (pending.length > 0) { const node = pending.pop(); if (node.localName === "text" && node.textContent === content) return node; pending.push(...node.childNodes); } return null;}"#;
#[test]fn buttons_and_toggle_update_the_rendered_counter() -> Result<(), BurokkuError> { Burokku::builder() .with_backend::<TestBackend>() .script(include_str!("../dist/app.js")) .extension(|registry: &mut Registry<'_>| { registry.font( include_bytes!("../../../crates/core/testdata/fonts/NotoSans-Regular.ttf") .as_slice(), ); // Keep the example's automatic increments outside this ten-second test. registry.install(|context: &Ctx<'_>| { context .globals() .set("__BUROKKU_COUNTER_INTERVAL_MS__", 60_000) }); Ok(()) }) .run_test(|mut app| async move { wait_for_first_render(&app).await?; app.present().await?; assert_eq!(app.window_title().as_deref(), Some("React counter — 0")); assert!(has_text(&app, "0").await?);
for (label, count) in [("+ Increase", 1), ("+ Increase", 2), ("− Decrease", 1)] { click_text(&mut app, label).await?; app.present().await?; assert_eq!( app.window_title().unwrap(), format!("React counter — {count}") ); assert!(has_text(&app, &count.to_string()).await?); }
assert!(has_text(&app, "Running").await?); click_text(&mut app, "Count every second").await?; app.present().await?; assert!(has_text(&app, "Paused").await?); assert!(!has_text(&app, "Running").await?); Ok(()) })}
/// React renders the first time on a scheduler task after the script, which/// the host's queue does not order against, so wait for the window it renders.async fn wait_for_first_render(app: &TestApp) -> Result<(), BurokkuError> { for _ in 0..1000 { if app.eval::<bool>("app.firstChild !== null").await? { return Ok(()); } } Err(BurokkuError::Host("React rendered no window".into()))}
/// Whether a `text` element in the app has exactly `content` as its text.async fn has_text(app: &TestApp, content: &str) -> Result<bool, BurokkuError> { app.eval(format!( "{FIND_TEXT}\nfindText({}) !== null", js_string(content) )) .await}
/// Presses and releases the primary button over the `text` element whose text/// is `label`; the click bubbles to the control around it.async fn click_text(app: &mut TestApp, label: &str) -> Result<(), BurokkuError> { let center: Vec<f64> = app .eval(format!( "{FIND_TEXT}\n(() => {{ const r = findText({}).getBoundingClientRect(); \ return [r.x + r.width / 2, r.y + r.height / 2]; }})()", js_string(label) )) .await?; let window = app.window_id().unwrap(); let position = PhysicalPosition::new(center[0], center[1]); for (state, buttons) in [(ElementState::Pressed, 1), (ElementState::Released, 0)] { app.send_input_event( window, InputEvent::MouseInput { state, button: MouseButton::Left, position, buttons, }, )?; } Ok(())}
/// A JavaScript string literal for `value`.fn js_string(value: &str) -> String { format!("{value:?}")}