Truly independent web browser
Compositor+Tests/LibWeb: Test smooth wheel steps on a controlled clock
sideshowbarker1cdbdd4bce92
diff --git a/Services/Compositor/Rust/src/context_state/scrolling.rs b/Services/Compositor/Rust/src/context_state/scrolling.rsindex d9a0cdcc803..9d0f8604bf9 100644--- a/Services/Compositor/Rust/src/context_state/scrolling.rs+++ b/Services/Compositor/Rust/src/context_state/scrolling.rs@@ -1094,3 +1094,229 @@ impl ContextState {None}}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::host::{FfiCompositorCoreHost, FfiOverlayLabel};+ use libcompositing_rust::display_list::commands::{CompositorContextId, UniqueNodeId};+ use libcompositing_rust::display_list::storage::DisplayListStorage;+ use libcompositing_rust::node_slot_id::NodeSlotId;+ use libcompositing_rust::visual_context::{+ ScrollData, SpatialData, TransformData, TransformDataRole, VISUAL_VIEWPORT_NODE_INDEX, VisualContextTree,+ scroll_state::NO_SCROLL_STATE_SLOT,+ };+ use libgfx_rust::FloatMatrix4x4;+ use libipc_rust::cpp_connection::RawAttachment;+ use libipc_rust::wire::{Decoder, EndpointMessage, decode_message_header};+ use std::cell::RefCell;+ use std::ffi::c_void;+ use std::sync::Arc;++ unsafe extern "C" fn record_completions(+ host: *mut c_void,+ _: u64,+ bytes: ak::Span<'_, u8>,+ attachments: ak::Span<'_, RawAttachment>,+ ) {+ assert!(attachments.as_slice().is_empty());+ let mut decoder = Decoder::new(bytes.as_slice(), []);+ let header = decode_message_header(&mut decoder).unwrap();+ type Updates = compositor_web_content_client::messages::AsyncScrollUpdates;+ if header == (Updates::ENDPOINT_MAGIC, Updates::MESSAGE_ID) {+ decoder.decode::<CompositorContextId>().unwrap();+ let updates = decoder.decode::<PendingAsyncScrollUpdates>().unwrap();+ // SAFETY: The fixture retains the receiver until after its context and host are dropped.+ let completed = unsafe { &*host.cast::<RefCell<Vec<AsyncScrollOperationID>>>() };+ completed.borrow_mut().extend(updates.completed_operation_ids);+ }+ }++ unsafe extern "C" fn set_timer(_: *mut c_void, _: u64, _: ContextTimer, _: i64) {}+ unsafe extern "C" fn cancel_timer(_: *mut c_void, _: u64, _: ContextTimer) {}+ unsafe extern "C" fn shape_overlay_label(+ _: *mut c_void,+ _: u64,+ _: bool,+ _: ak::Span<'_, u8>,+ _: f64,+ _: ak::Span<'_, u8>,+ _: *mut c_void,+ _: unsafe extern "C" fn(*mut c_void, *const FfiOverlayLabel<'_>),+ ) {+ }++ struct WheelTest {+ context: ContextState,+ node_index: SpatialNodeIndex,+ completed_operations: Rc<RefCell<Vec<AsyncScrollOperationID>>>,+ }++ impl WheelTest {+ fn new(kind: AsyncScrollNodeKind) -> Self {+ let completed_operations = Rc::new(RefCell::new(Vec::<AsyncScrollOperationID>::new()));+ // SAFETY: The receiver outlives the host, and the callbacks never reenter the context.+ let host = unsafe {+ Host::new(FfiCompositorCoreHost {+ host: Rc::as_ptr(&completed_operations).cast_mut().cast(),+ post_message_to_web_content: record_completions,+ post_message_to_render_clock_of_web_content: record_completions,+ post_message_to_presentation: record_completions,+ set_timer,+ cancel_timer,+ shape_overlay_label,+ })+ };+ let mut context = ContextState::new(+ CompositorContextId(1),+ Some(1),+ WebContentClientId(1),+ Rc::new(host),+ Rc::new(WheelGestureIdAllocator::default()),+ false,+ );+ let mut tree = VisualContextTree::create(TransformData {+ matrix: FloatMatrix4x4::identity(),+ origin: FloatPoint::default(),+ sorting_context_root_index: None,+ flattens_inherited_transform: false,+ role: TransformDataRole::CssTransform,+ synthetic_plane: false,+ establishes_sorting_context: false,+ bounds: Default::default(),+ });+ let node_index = tree.append_spatial(+ SpatialData::Scroll(ScrollData {+ state_slot: NO_SCROLL_STATE_SLOT,+ owner_paintable: NodeSlotId::INVALID,+ }),+ VISUAL_VIEWPORT_NODE_INDEX,+ );+ let tree = AccumulatedVisualContextTree::from_arc(Arc::new(tree));+ context.scroll_state_snapshot.set_node_count(tree.spatial_node_count());+ let display_list = DisplayList::new(+ DisplayListStorage::empty(),+ 1,+ tree.structural_epoch(),+ None,+ None,+ Default::default(),+ );+ context+ .scene+ .install(Rc::new(display_list), tree, MonotonicTime::default());+ context.async_scroll_tree.set_state(AsyncScrollingState {+ document_id: Some(UniqueNodeId(1)),+ scroll_nodes: vec![AsyncScrollNode {+ scroll_node_index: node_index,+ stable_node_id: AsyncScrollNodeStableID {+ node_id: UniqueNodeId(1),+ kind,+ pseudo_element_type: 0,+ },+ parent_scroll_node_index: None,+ scrollport_rect: IntRect::new(0, 0, 100, 100),+ min_scroll_offset: FloatPoint::default(),+ max_scroll_offset: FloatPoint { x: 0.0, y: 1000.0 },+ is_viewport: kind == AsyncScrollNodeKind::Viewport,+ can_be_wheel_scrolled_horizontally: false,+ can_be_wheel_scrolled_vertically: true,+ chains_scroll_to_ancestors_horizontally: false,+ chains_scroll_to_ancestors_vertically: false,+ shows_overscroll_effect_horizontally: false,+ shows_overscroll_effect_vertically: false,+ }],+ ..Default::default()+ });+ Self {+ context,+ node_index,+ completed_operations,+ }+ }++ fn step(&mut self, now: MonotonicTime) -> AsyncScrollOperationID {+ self.context+ .scroll_node_by_wheel_delta(+ self.node_index,+ FloatPoint { x: 0.0, y: 120.0 },+ WheelDeltaPrecision::Discrete,+ ScrollGesturePhase::None,+ AsyncScrollOperationTracking::Yes,+ IntRect::new(0, 0, 100, 100),+ ScrollChaining::None,+ now,+ )+ .outcome+ .operation_id+ .unwrap()+ }++ fn offset(&self) -> FloatPoint {+ self.context+ .async_scroll_tree+ .scroll_offset_for_node(self.node_index, &self.context.scroll_state_snapshot)+ .unwrap()+ }+ }++ #[test]+ fn discrete_wheel_step_animates_until_200ms() {+ for kind in [AsyncScrollNodeKind::Viewport, AsyncScrollNodeKind::Element] {+ let mut test = WheelTest::new(kind);+ let start = MonotonicTime::from_nanoseconds(1_000_000_000);+ let operation = test.step(start);+ assert_eq!(test.offset(), FloatPoint::default());+ assert!(test.completed_operations.borrow().is_empty());++ test.context+ .advance_smooth_scroll_animations(start + Duration::from_milliseconds(100));+ let offset = test.offset();+ assert_eq!(offset.x, 0.0);+ assert!(offset.y > 0.0 && offset.y < 120.0);+ assert!(test.completed_operations.borrow().is_empty());++ test.context+ .advance_smooth_scroll_animations(start + Duration::from_milliseconds(200));+ assert_eq!(test.offset(), FloatPoint { x: 0.0, y: 120.0 });+ assert_eq!(test.completed_operations.borrow().as_slice(), &[operation]);+ assert!(test.context.smooth_scroll_animations.is_empty());+ }+ }++ #[test]+ fn discrete_wheel_step_completes_on_a_delayed_first_sample() {+ for kind in [AsyncScrollNodeKind::Viewport, AsyncScrollNodeKind::Element] {+ let mut test = WheelTest::new(kind);+ let start = MonotonicTime::from_nanoseconds(1_000_000_000);+ let operation = test.step(start);++ // A delayed compositor tick can finish a smooth scroll without presenting an intermediate offset.+ test.context+ .advance_smooth_scroll_animations(start + Duration::from_milliseconds(250));+ assert_eq!(test.offset(), FloatPoint { x: 0.0, y: 120.0 });+ assert_eq!(test.completed_operations.borrow().as_slice(), &[operation]);+ assert!(test.context.smooth_scroll_animations.is_empty());+ }+ }++ #[test]+ fn discrete_wheel_steps_accumulate_while_animating() {+ for kind in [AsyncScrollNodeKind::Viewport, AsyncScrollNodeKind::Element] {+ let mut test = WheelTest::new(kind);+ let start = MonotonicTime::from_nanoseconds(1_000_000_000);+ let operation = test.step(start);+ let second_step_time = start + Duration::from_milliseconds(50);+ test.context.advance_smooth_scroll_animations(second_step_time);+ assert!(test.offset().y > 0.0 && test.offset().y < 120.0);+ assert_eq!(test.step(second_step_time), operation);+ assert!(test.completed_operations.borrow().is_empty());++ test.context+ .advance_smooth_scroll_animations(second_step_time + Duration::from_milliseconds(200));+ assert_eq!(test.offset(), FloatPoint { x: 0.0, y: 240.0 });+ assert_eq!(test.completed_operations.borrow().as_slice(), &[operation]);+ assert!(test.context.smooth_scroll_animations.is_empty());+ }+ }+}diff --git a/Tests/LibWeb/Text/expected/async-scrolling/wheel-steps-scroll-smoothly.txt b/Tests/LibWeb/Text/expected/async-scrolling/wheel-steps-scroll-smoothly.txtindex 317913e4adf..0bb58473a0f 100644--- a/Tests/LibWeb/Text/expected/async-scrolling/wheel-steps-scroll-smoothly.txt+++ b/Tests/LibWeb/Text/expected/async-scrolling/wheel-steps-scroll-smoothly.txt@@ -1,4 +1,6 @@-a step animated the scroll container: true-a step animated the viewport: true-two steps scrolled twice as far: true-two steps back and one forward scrolled one step back: true+a step scrolled the scroll container: true+two more steps scrolled the scroll container to 360: true+two steps back and one forward scrolled the scroll container to 240: true+a step scrolled the viewport: true+two more steps scrolled the viewport to 360: true+two steps back and one forward scrolled the viewport to 240: truediff --git a/Tests/LibWeb/Text/input/async-scrolling/wheel-steps-scroll-smoothly.html b/Tests/LibWeb/Text/input/async-scrolling/wheel-steps-scroll-smoothly.htmlindex 2e77ef4da6e..45f96e1a1d0 100644--- a/Tests/LibWeb/Text/input/async-scrolling/wheel-steps-scroll-smoothly.html+++ b/Tests/LibWeb/Text/input/async-scrolling/wheel-steps-scroll-smoothly.html@@ -10,7 +10,8 @@await waitForCondition(() => compositorScrollsWheelStepAt(50, 50, 0, 120) && compositorScrollsWheelStepAt(400, 400, 0, 120));- // A step that jumps fires one scroll event. A step that animates fires one per frame.+ // Scroll updates and events can coalesce, so their count cannot establish smoothness.+ // The compositor's Rust tests check intermediate offsets with controlled timestamps.const scrollEventsUntilScrollend = async (target, x, y, deltas) => {let scrollEventCount = 0;const countScrollEvent = () => ++scrollEventCount;@@ -24,16 +25,21 @@};const scrollerStepEvents = await scrollEventsUntilScrollend(scroller, 50, 50, [120]);- println(`a step animated the scroll container: ${scrollerStepEvents > 1 && scroller.scrollTop > 0}`);+ println(`a step scrolled the scroll container: ${scrollerStepEvents > 0 && scroller.scrollTop === 120}`);++ const scrollerForwardEvents = await scrollEventsUntilScrollend(scroller, 50, 50, [120, 120]);+ println(`two more steps scrolled the scroll container to 360: ${scrollerForwardEvents > 0 && scroller.scrollTop === 360}`);++ const scrollerBackEvents = await scrollEventsUntilScrollend(scroller, 50, 50, [-120, -120, 120]);+ println(`two steps back and one forward scrolled the scroll container to 240: ${scrollerBackEvents > 0 && scroller.scrollTop === 240}`);const viewportStepEvents = await scrollEventsUntilScrollend(document, 400, 400, [120]);- const stepDistance = window.scrollY;- println(`a step animated the viewport: ${viewportStepEvents > 1 && stepDistance > 0}`);+ println(`a step scrolled the viewport: ${viewportStepEvents > 0 && window.scrollY === 120}`);- await scrollEventsUntilScrollend(document, 400, 400, [120, 120]);- println(`two steps scrolled twice as far: ${window.scrollY === stepDistance * 3}`);+ const viewportForwardEvents = await scrollEventsUntilScrollend(document, 400, 400, [120, 120]);+ println(`two more steps scrolled the viewport to 360: ${viewportForwardEvents > 0 && window.scrollY === 360}`);- await scrollEventsUntilScrollend(document, 400, 400, [-120, -120, 120]);- println(`two steps back and one forward scrolled one step back: ${window.scrollY === stepDistance * 2}`);+ const viewportBackEvents = await scrollEventsUntilScrollend(document, 400, 400, [-120, -120, 120]);+ println(`two steps back and one forward scrolled the viewport to 240: ${viewportBackEvents > 0 && window.scrollY === 240}`);});</script>