freya_testing/
test_node.rs

1use freya_core::{
2    custom_attributes::CustomAttributeValues,
3    node::NodeState,
4    states::{
5        StyleState,
6        ViewportState,
7    },
8};
9use freya_native_core::{
10    node::NodeType,
11    real_dom::NodeImmutable,
12    NodeId,
13};
14use torin::{
15    geometry::Area,
16    prelude::LayoutNode,
17};
18
19use crate::test_utils::TestUtils;
20
21/// Represents a `Node` in the DOM.
22#[derive(Clone)]
23pub struct TestNode {
24    pub(crate) node_id: NodeId,
25    pub(crate) utils: TestUtils,
26    pub(crate) height: u16,
27    pub(crate) children_ids: Vec<NodeId>,
28    pub(crate) state: NodeState,
29    pub(crate) node_type: NodeType<CustomAttributeValues>,
30}
31
32impl TestNode {
33    /// Get a node by its position in this node children list. Will panic if not found.
34    #[track_caller]
35    pub fn get(&self, child_index: usize) -> Self {
36        self.try_get(child_index)
37            .unwrap_or_else(|| panic!("Child by index {child_index} not found"))
38    }
39
40    /// Get a node by its position in this node children list.
41    #[track_caller]
42    pub fn try_get(&self, child_index: usize) -> Option<Self> {
43        let child_id = self.children_ids.get(child_index)?;
44        let child: TestNode = self.utils.get_node_by_id(*child_id);
45        Some(child)
46    }
47
48    /// Get the Node text
49    pub fn text(&self) -> Option<&str> {
50        self.node_type.text()
51    }
52
53    /// Get the Node state
54    pub fn state(&self) -> &NodeState {
55        &self.state
56    }
57
58    /// Get the Node layout
59    pub fn layout(&self) -> Option<LayoutNode> {
60        self.utils()
61            .sdom()
62            .get()
63            .layout()
64            .get(self.node_id)
65            .cloned()
66    }
67
68    /// Get the Node layout Area
69    pub fn area(&self) -> Option<Area> {
70        self.layout().map(|l| l.area)
71    }
72
73    /// Get the Node style
74    pub fn style(&self) -> StyleState {
75        self.utils
76            .sdom
77            .get()
78            .rdom()
79            .get(self.node_id)
80            .unwrap()
81            .get::<StyleState>()
82            .unwrap()
83            .clone()
84    }
85
86    /// Get a mutable reference to the test utils.
87    pub(crate) fn utils(&self) -> &TestUtils {
88        &self.utils
89    }
90
91    /// Get the NodeId from the parent
92    pub fn parent_id(&self) -> Option<NodeId> {
93        let sdom = self.utils().sdom();
94        let fdom = sdom.get();
95        let dom = fdom.rdom();
96        let node = dom.get(self.node_id).unwrap();
97        node.parent_id()
98    }
99
100    /// Get the Node height in the DOM
101    pub fn dom_height(&self) -> u16 {
102        self.height
103    }
104
105    /// Check if the Node is visible given it's viewports.
106    pub fn is_visible(&self) -> bool {
107        let Some(area) = self.area() else {
108            return false;
109        };
110
111        let sdom = self.utils().sdom();
112        let fdom = sdom.get();
113        let dom = fdom.rdom();
114        let node = dom.get(self.node_id).unwrap();
115        let node_viewports = node.get::<ViewportState>().unwrap();
116
117        let layout = fdom.layout();
118
119        // Skip elements that are completely out of any their parent's viewport
120        for viewport_id in &node_viewports.viewports {
121            let viewport = layout.get(*viewport_id).unwrap().visible_area();
122            if !viewport.intersects(&area) {
123                return false;
124            }
125        }
126
127        true
128    }
129
130    /// Get the IDs of this Node children.
131    pub fn children_ids(&self) -> Vec<NodeId> {
132        self.children_ids.clone()
133    }
134
135    /// Check if this element is text
136    pub fn is_element(&self) -> bool {
137        self.node_type.is_element()
138    }
139
140    /// Check if this element is text
141    pub fn is_text(&self) -> bool {
142        self.node_type.is_text()
143    }
144
145    /// Check if this element is a placeholder
146    pub fn is_placeholder(&self) -> bool {
147        self.node_type.is_placeholder()
148    }
149
150    /// Get a descendant Node of this Node that matches a certain text.
151    pub fn get_by_text(&self, matching_text: &str) -> Option<Self> {
152        self.utils()
153            .get_node_matching_inside_id(self.node_id, |node| {
154                if let NodeType::Text(text) = &*node.node_type() {
155                    matching_text == text
156                } else {
157                    false
158                }
159            })
160            .first()
161            .cloned()
162    }
163}