Skip to content

Commit

Permalink
test reordering of nodes in torin
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Dec 19, 2024
1 parent 8ad9a2d commit 0fe95a9
Showing 1 changed file with 121 additions and 5 deletions.
126 changes: 121 additions & 5 deletions crates/torin/tests/other.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use rustc_hash::{
FxHashMap,
FxHashSet,
};
#[cfg(test)]
use rustc_hash::FxHashMap;
use torin::{
prelude::*,
test_utils::*,
Expand Down Expand Up @@ -414,3 +410,123 @@ pub fn deep_tree() {

assert_eq!(layout.get_root_candidate(), RootNodeCandidate::None);
}

#[test]
pub fn node_reordering() {
let (mut layout, mut measurer) = test_utils();

let mut mocked_dom = TestingDOM::default();
mocked_dom.add(
0,
None,
vec![1, 2],
Node::from_size_and_direction(
Size::Pixels(Length::new(200.0)),
Size::Pixels(Length::new(200.0)),
DirectionMode::Vertical,
),
);
mocked_dom.add(
1,
Some(0),
vec![],
Node::from_size_and_direction(
Size::Pixels(Length::new(200.0)),
Size::Pixels(Length::new(100.0)),
DirectionMode::Vertical,
),
);
mocked_dom.add(
2,
Some(0),
vec![],
Node::from_size_and_direction(
Size::Pixels(Length::new(200.0)),
Size::Pixels(Length::new(100.0)),
DirectionMode::Vertical,
),
);

layout.measure(
0,
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(1000.0, 1000.0)),
&mut measurer,
&mut mocked_dom,
);

assert_eq!(
layout.get(0).unwrap().area,
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(200.0, 200.0)),
);

assert_eq!(
layout.get(1).unwrap().area,
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(200.0, 100.0)),
);

assert_eq!(
layout.get(2).unwrap().area,
Rect::new(Point2D::new(0.0, 100.0), Size2D::new(200.0, 100.0)),
);

layout.invalidate_with_reason(1, DirtyReason::Reorder);
layout.invalidate_with_reason(2, DirtyReason::Reorder);

mocked_dom.add(
0,
None,
vec![2, 1],
Node::from_size_and_direction(
Size::Pixels(Length::new(200.0)),
Size::Pixels(Length::new(200.0)),
DirectionMode::Vertical,
),
);

layout.find_best_root(&mut mocked_dom);

layout.measure(
0,
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(1000.0, 1000.0)),
&mut measurer,
&mut mocked_dom,
);

assert_eq!(
layout.get(2).unwrap().area,
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(200.0, 100.0)),
);

assert_eq!(
layout.get(1).unwrap().area,
Rect::new(Point2D::new(0.0, 100.0), Size2D::new(200.0, 100.0)),
);

// This will not cause the desired output expected by the user as we are not properly invalidating those reordered nodes
layout.invalidate_with_reason(1, DirtyReason::None);

mocked_dom.add(
0,
None,
vec![1, 2],
Node::from_size_and_direction(
Size::Pixels(Length::new(200.0)),
Size::Pixels(Length::new(200.0)),
DirectionMode::Vertical,
),
);

layout.find_best_root(&mut mocked_dom);

// That is why these nodes still have the same positions as before

assert_eq!(
layout.get(2).unwrap().area,
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(200.0, 100.0)),
);

assert_eq!(
layout.get(1).unwrap().area,
Rect::new(Point2D::new(0.0, 100.0), Size2D::new(200.0, 100.0)),
);
}

0 comments on commit 0fe95a9

Please sign in to comment.