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 dom_struct::dom_struct;
9use layout_api::ReflowPhasesRun;
10use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
11use script_bindings::domstring::DOMString;
12use script_bindings::reflector::Reflector;
13use script_bindings::root::DomRoot;
14use script_bindings::script_runtime::CanGc;
15use time::Duration;
16
17use crate::dom::bindings::codegen::Bindings::ServoTestUtilsBinding::ServoTestUtilsMethods;
18use crate::dom::globalscope::GlobalScope;
19use crate::dom::layoutresult::LayoutResult;
20
21#[dom_struct]
22pub(crate) struct ServoTestUtils {
23    reflector_: Reflector,
24}
25
26impl ServoTestUtilsMethods<crate::DomTypeHolder> for ServoTestUtils {
27    fn AdvanceClock(global: &GlobalScope, ms: i32) {
28        global
29            .as_window()
30            .advance_animation_clock(Duration::milliseconds(ms as i64));
31    }
32
33    #[expect(unsafe_code)]
34    fn CrashHard(_: &GlobalScope) {
35        unsafe { std::ptr::null_mut::<i32>().write(42) }
36    }
37
38    fn ForceLayout(global: &GlobalScope, can_gc: CanGc) -> DomRoot<LayoutResult> {
39        let (phases_run, statistics) = global.as_window().Document().update_the_rendering();
40
41        let mut phases = Vec::new();
42        if phases_run.contains(ReflowPhasesRun::RanLayout) {
43            phases.push(DOMString::from("RanLayout"))
44        }
45        if phases_run.contains(ReflowPhasesRun::CalculatedOverflow) {
46            phases.push(DOMString::from("CalculatedOverflow"))
47        }
48        if phases_run.contains(ReflowPhasesRun::BuiltStackingContextTree) {
49            phases.push(DOMString::from("BuiltStackingContextTree"))
50        }
51        if phases_run.contains(ReflowPhasesRun::BuiltDisplayList) {
52            phases.push(DOMString::from("BuiltDisplayList"))
53        }
54        if phases_run.contains(ReflowPhasesRun::UpdatedScrollNodeOffset) {
55            phases.push(DOMString::from("UpdatedScrollNodeOffset"))
56        }
57        if phases_run.contains(ReflowPhasesRun::UpdatedImageData) {
58            phases.push(DOMString::from("UpdatedImageData"))
59        }
60
61        LayoutResult::new(
62            global,
63            phases,
64            statistics.rebuilt_fragment_count,
65            statistics.restyle_fragment_count,
66            can_gc,
67        )
68    }
69
70    fn Js_backtrace(_: &GlobalScope) {
71        println!("Current JS stack:");
72        let rust_stack = Backtrace::new();
73        println!("Current Rust stack:\n{:?}", rust_stack);
74    }
75
76    fn Panic(_: &GlobalScope) {
77        panic!("explicit panic from script")
78    }
79}