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