Skip to main content

script/dom/testing/
servotestutils.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5// check-tidy: no specs after this line
6
7use backtrace::Backtrace;
8use js::context::{JSContext, NoGC};
9use layout_api::ReflowPhasesRun;
10use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
11use script_bindings::domstring::DOMString;
12use script_bindings::root::DomRoot;
13use servo_base::Epoch;
14use time::Duration;
15
16use crate::dom::bindings::codegen::Bindings::ServoTestUtilsBinding::ServoTestUtilsMethods;
17use crate::dom::globalscope::GlobalScope;
18use crate::dom::layoutresult::LayoutResult;
19use crate::dom::types::AccessibilityUpdateResult;
20
21pub(crate) struct ServoTestUtils;
22
23impl ServoTestUtilsMethods<crate::DomTypeHolder> for ServoTestUtils {
24    fn AdvanceClock(no_gc: &NoGC, global: &GlobalScope, ms: i32) {
25        global
26            .as_window()
27            .advance_animation_clock(no_gc, Duration::milliseconds(ms as i64));
28    }
29
30    #[expect(unsafe_code)]
31    fn CrashHard(_: &GlobalScope) {
32        unsafe { std::ptr::null_mut::<i32>().write(42) }
33    }
34
35    fn ForceLayout(cx: &mut JSContext, global: &GlobalScope) -> DomRoot<LayoutResult> {
36        let (phases_run, statistics) = global.as_window().Document().update_the_rendering(cx);
37
38        let mut phases = Vec::new();
39        if phases_run.contains(ReflowPhasesRun::RanLayout) {
40            phases.push(DOMString::from_static("RanLayout"))
41        }
42        if phases_run.contains(ReflowPhasesRun::BuiltStackingContextTree) {
43            phases.push(DOMString::from_static("BuiltStackingContextTree"))
44        }
45        if phases_run.contains(ReflowPhasesRun::BuiltDisplayList) {
46            phases.push(DOMString::from_static("BuiltDisplayList"))
47        }
48        if phases_run.contains(ReflowPhasesRun::UpdatedScrollNodeOffset) {
49            phases.push(DOMString::from_static("UpdatedScrollNodeOffset"))
50        }
51        if phases_run.contains(ReflowPhasesRun::UpdatedImageData) {
52            phases.push(DOMString::from_static("UpdatedImageData"))
53        }
54
55        LayoutResult::new(
56            cx,
57            global,
58            phases,
59            statistics.rebuilt_fragment_count,
60            statistics.restyle_fragment_count,
61            statistics.only_descendants_changed_count,
62        )
63    }
64
65    fn Js_backtrace(_: &GlobalScope) {
66        println!("Current JS stack:");
67        let rust_stack = Backtrace::new();
68        println!("Current Rust stack:\n{:?}", rust_stack);
69    }
70
71    fn Panic(_: &GlobalScope) {
72        panic!("explicit panic from script")
73    }
74
75    fn EnsureAccessibilityActive(global: &GlobalScope) {
76        let window = global.as_window();
77        window
78            .layout()
79            .set_accessibility_active(true, Epoch::default());
80    }
81
82    fn ForceAccessibilityUpdate(
83        cx: &mut JSContext,
84        global: &GlobalScope,
85    ) -> DomRoot<AccessibilityUpdateResult> {
86        let window = global.as_window();
87        window.layout().set_force_accessibility_update_if_active();
88        let (_, statistics) = window.Document().update_the_rendering(cx);
89
90        AccessibilityUpdateResult::new(
91            cx,
92            global,
93            statistics.nodes_updated_from_dom,
94            statistics.nodes_updated_from_tree,
95            statistics.nodes_updated_bounds,
96            statistics.nodes_in_tree_update,
97        )
98    }
99}