use burokku::{ extension::Registry, test::{ EditCommand, ElementState, InputEvent, KeyEvent, Modifiers, MouseButton, PhysicalPosition, WheelPhase, }, Burokku, BurokkuBuilder, BurokkuError, TestApp, TestBackend, }; fn scroll_example() -> BurokkuBuilder { Burokku::builder() .with_backend::() .script(include_str!("../src/app.js")) .extension(|registry: &mut Registry<'_>| { registry.font( include_bytes!("../../../crates/core/testdata/fonts/NotoSans-Regular.ttf") .as_slice(), ); registry.script("quiet-console", "console.log = () => {}")?; Ok(()) }) } async fn center(app: &TestApp, node: &str) -> Result, BurokkuError> { let center: Vec = app .eval(format!( "(() => {{ const r = {node}.getBoundingClientRect(); \ return [r.x + r.width / 2, r.y + r.height / 2]; }})()" )) .await?; Ok(PhysicalPosition::new(center[0], center[1])) } fn click(app: &mut TestApp, position: PhysicalPosition) -> Result<(), BurokkuError> { let window = app.window_id().unwrap(); 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 key as the platform delivers it: the key, then, for Tab, the command /// the platform's dictionary binds it to. fn key(app: &mut TestApp, text: &str) -> Result<(), BurokkuError> { let window = app.window_id().unwrap(); app.send_input_event( window, InputEvent::KeyboardInput(KeyEvent { key_code: 0, key: text.into(), code: "Unidentified".into(), state: ElementState::Pressed, repeat: false, modifiers: Modifiers::default(), }), )?; if text == "Tab" { app.send_input_event( window, InputEvent::Command(EditCommand::FocusNext { backwards: false }), )?; } Ok(()) } #[test] fn the_wheel_scrolls_the_list_and_reports_its_position() -> Result<(), BurokkuError> { scroll_example().run_test(|mut app| async move { app.present().await?; // Each showcased scroller has content to scroll, and the panels fit // the window rather than growing to their content. let overflowing: bool = app .eval( "list.scrollHeight > list.clientHeight \ && outer.scrollHeight > outer.clientHeight \ && strip.scrollWidth > strip.clientWidth \ && ticker.scrollWidth > ticker.clientWidth", ) .await?; assert!(overflowing); let fits: bool = app .eval( "(() => { const panel = nestedPanel.getBoundingClientRect(); \ return panel.x + panel.width <= windowNode.getBoundingClientRect().width; })()", ) .await?; assert!(fits); // The panels share the row equally: `min-width: 0px` lets the nested // panel shrink below its card strip instead of taking its width. let shared: bool = app .eval( "(() => { const list = listPanel.getBoundingClientRect(); \ const nested = nestedPanel.getBoundingClientRect(); \ return list.width > 0 && Math.abs(list.width - nested.width) < 1; })()", ) .await?; assert!(shared); let window = app.window_id().unwrap(); let position = center(&app, "list").await?; app.send_input_event( window, InputEvent::MouseWheel { delta_x: 0.0, delta_y: -120.0, precise: true, phase: WheelPhase::None, position, }, )?; // One task runs the wheel; the `scroll` listener runs on the host's // next turn. app.run_host_turn().await?; assert_eq!(app.eval::("list.scrollTop").await?, 120.0); assert!(app .eval::("listStatus.textContent") .await? .starts_with("scrollTop 120 of ")); Ok(()) }) } #[test] fn buttons_scroll_the_list_by_click_and_keyboard() -> Result<(), BurokkuError> { scroll_example().run_test(|mut app| async move { app.present().await?; // Jumping centers row 42 in the list. let position = center(&app, "buttons['Jump to #42']").await?; click(&mut app, position)?; let centered: bool = app .eval( "(() => { const row = rows[41].getBoundingClientRect(); \ const port = list.getBoundingClientRect(); \ return Math.abs((row.y + row.height / 2) - (port.y + port.height / 2)) < 1; })()", ) .await?; assert!(centered); assert_eq!( app.eval::("focusStatus.textContent").await?, "Focus: \"Jump to #42\" button" ); // Tab moves to the next button; Enter activates it. app.eval::<()>("globalThis.afterJump = list.scrollTop") .await?; key(&mut app, "Tab")?; key(&mut app, "Enter")?; assert_eq!( app.eval::("focusStatus.textContent").await?, "Focus: \"Page down\" button" ); let paged: bool = app .eval( "list.scrollTop === Math.min(afterJump + list.clientHeight, \ list.scrollHeight - list.clientHeight)", ) .await?; assert!(paged); let before = app.eval::("list.scrollTop").await?; key(&mut app, "Home")?; // Home scrolls only from a focused scroller; a button is focused. assert_eq!(app.eval::("list.scrollTop").await?, before); Ok(()) }) } #[test] fn the_focus_readout_names_each_scroller_a_click_focuses() -> Result<(), BurokkuError> { scroll_example().run_test(|mut app| async move { app.present().await?; // A press focuses the scroll container around it, the `overflow: // hidden` ticker as well; the readout names whichever has focus. for scroller in ["list", "outer", "strip", "ticker"] { let position = center(&app, scroller).await?; click(&mut app, position)?; app.eval::<()>("").await?; assert!( app.eval::(format!("app.activeElement === {scroller}")) .await? ); let readout = app.eval::("focusStatus.textContent").await?; assert_ne!(readout, "Focus: null", "{scroller}"); } Ok(()) }) }