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
use dioxus_native_core::real_dom::NodeImmutable;
use dioxus_native_core::tree::TreeRef;
use dioxus_native_core::NodeId;
use freya_core::node::get_node_state;
use freya_dom::prelude::{DioxusNode, SafeDOM};
use freya_layout::Layers;
use std::sync::{Arc, Mutex};

pub use freya_core::events::FreyaEvent;
pub use freya_elements::events::mouse::MouseButton;

use crate::test_node::TestNode;

#[derive(Clone)]
pub struct TestUtils {
    pub(crate) sdom: SafeDOM,
    pub(crate) layers: Arc<Mutex<Layers>>,
}

impl TestUtils {
    pub fn sdom(&self) -> &SafeDOM {
        &self.sdom
    }

    pub fn layers(&self) -> &Arc<Mutex<Layers>> {
        &self.layers
    }

    /// Get a Node by the given ID
    pub fn get_node_by_id(&self, node_id: NodeId) -> TestNode {
        let utils = self.clone();

        let dom = self.sdom.get();
        let rdom = dom.rdom();

        let height = rdom.tree_ref().height(node_id).unwrap();
        let children_ids = rdom.tree_ref().children_ids(node_id);
        let node: DioxusNode = rdom.get(node_id).unwrap();

        let state = get_node_state(&node);
        let node_type = node.node_type().clone();

        TestNode {
            node_id,
            utils,
            children_ids,
            height,
            state,
            node_type,
        }
    }
}