Skip to main content

yew_e2e/test_driver/
wheel.rs

1//! Turning the wheel over an element.
2
3use anyhow::Result;
4use serde_json::json;
5
6use super::finite;
7use crate::{TestDriver, TestElement};
8
9impl TestDriver {
10    /// **Turn the wheel and report whether anything consumed it.**
11    ///
12    /// Dispatches the same event as [`Self::wheel`], and returns `true` when a
13    /// listener called `preventDefault()` on it.
14    pub async fn wheel_consumed(&self, element: &TestElement, delta: f64) -> Result<bool> {
15        tracing::trace!(delta, "Turning the wheel and asking who took it");
16
17        let delta = finite("a wheel delta", delta)?;
18
19        self.turn_wheel(element, json!({ "deltaY": delta }), false)
20            .await
21    }
22
23    /// Turn the wheel over an element: a cancelable, bubbling `wheel` event
24    /// with `deltaY` set to `delta` (positive scrolls down), dispatched on it.
25    ///
26    /// The event is synthesized in the page rather than sent through
27    /// WebDriver, so it reaches the element's listeners but the browser does
28    /// not scroll anything by itself. Returns once the event is dispatched;
29    /// see [`Self::wheel_consumed`] to learn whether anything handled it.
30    pub async fn wheel(&self, element: &TestElement, delta: f64) -> Result<()> {
31        tracing::trace!(delta, "Turning the wheel over an element");
32
33        let delta = finite("a wheel delta", delta)?;
34
35        self.turn_wheel(element, json!({ "deltaY": delta }), false)
36            .await?;
37        Ok(())
38    }
39
40    /// **Turn the wheel with `alt` held**, and `shift` too where asked.
41    ///
42    /// The pointer is put at the middle of the element rather than left at the
43    /// origin a fresh `WheelEvent` carries: this zoom holds still whatever is
44    /// under the pointer, so a notch at `clientY` zero anchors on the top edge
45    /// and walks what is shown straight out of the window.
46    pub async fn wheel_alt(&self, element: &TestElement, delta: f64, shift: bool) -> Result<()> {
47        tracing::trace!(delta, shift, "Turning the wheel with alt held");
48
49        let delta = finite("a wheel delta", delta)?;
50
51        let init = json!({ "deltaY": delta, "altKey": true, "shiftKey": shift });
52        self.turn_wheel(element, init, true).await?;
53        Ok(())
54    }
55
56    /// **Turn the wheel over an element and say whether the page took it.**
57    ///
58    /// Both axes and `ctrl` are given, because that is what tells one turn from
59    /// another where an element answers for more than one of them, and the
60    /// pointer is put in the middle of the element so a zoom that holds still
61    /// whatever is under it has something to hold.
62    ///
63    /// What comes back is whether the default was prevented, which is the only
64    /// thing that separates a gesture the window answered from one it answered
65    /// *and* let fall through to scroll the box it landed in.
66    pub async fn wheel_over(
67        &self,
68        element: &TestElement,
69        across: f64,
70        down: f64,
71        ctrl: bool,
72    ) -> Result<bool> {
73        tracing::trace!(across, down, ctrl, "Turning the wheel over both axes");
74
75        let across = finite("a sideways wheel delta", across)?;
76        let down = finite("a wheel delta", down)?;
77
78        let init = json!({ "deltaX": across, "deltaY": down, "ctrlKey": ctrl });
79        self.turn_wheel(element, init, true).await
80    }
81
82    /// **Turn the wheel sideways**, reading as `deltaX`: the gesture a trackpad
83    /// makes to pan, and not the one `wheel` makes.
84    pub async fn wheel_across(&self, element: &TestElement, delta: f64) -> Result<()> {
85        tracing::trace!(delta, "Turning the wheel sideways over an element");
86
87        let delta = finite("a sideways wheel delta", delta)?;
88
89        self.turn_wheel(element, json!({ "deltaX": delta }), false)
90            .await?;
91        Ok(())
92    }
93
94    /// **Dispatch one `wheel` event on `element`**, cancelable and bubbling,
95    /// with what `init` sets on it, and say whether a listener prevented its
96    /// default. Where `centred`, the pointer is put in the middle of the
97    /// element rather than left at the origin a fresh `WheelEvent` carries.
98    async fn turn_wheel(
99        &self,
100        element: &TestElement,
101        init: serde_json::Value,
102        centred: bool,
103    ) -> Result<bool> {
104        let script = "const [element, init, centred] = arguments; \
105             if (centred) { \
106                 const box = element.getBoundingClientRect(); \
107                 init.clientX = box.left + box.width / 2; \
108                 init.clientY = box.top + box.height / 2; \
109             } \
110             init.bubbles = true; \
111             init.cancelable = true; \
112             return !element.dispatchEvent(new WheelEvent('wheel', init));";
113
114        let taken = self
115            .inner
116            .execute(script, vec![element.inner.to_json()?, init, json!(centred)])
117            .await?;
118
119        Ok(taken.json().as_bool().unwrap_or(false))
120    }
121}