-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,427 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ pub mod prelude { | |
pub use crate::torin::*; | ||
pub use crate::values::prelude::*; | ||
} | ||
|
||
pub mod test_utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::prelude::*; | ||
use std::collections::HashMap; | ||
|
||
pub struct TestingMeasurer; | ||
|
||
impl LayoutMeasurer<usize> for TestingMeasurer { | ||
fn measure( | ||
&mut self, | ||
_node_id: usize, | ||
_node: &Node, | ||
_area: &Area, | ||
_parent_size: &Area, | ||
_available_parent_area: &Area, | ||
) -> Option<Area> { | ||
None | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct TestingDOM { | ||
mapper: HashMap<usize, (Option<usize>, Vec<usize>, u16, Node)>, | ||
} | ||
|
||
impl TestingDOM { | ||
pub fn add(&mut self, node_id: usize, parent: Option<usize>, children: Vec<usize>, node: Node) { | ||
let depth = parent.map(|p| self.mapper.get(&p).unwrap().2).unwrap_or(0) + 1; | ||
self.mapper.insert(node_id, (parent, children, depth, node)); | ||
} | ||
|
||
pub fn set_node(&mut self, node_id: usize, node: Node) { | ||
self.mapper.get_mut(&node_id).unwrap().3 = node; | ||
} | ||
|
||
pub fn remove(&mut self, node_id: usize) { | ||
let node = self.mapper.get(&node_id).unwrap().clone(); | ||
|
||
if let Some((_, parent_children, _, _)) = node.0.and_then(|p| self.mapper.get_mut(&p)) { | ||
parent_children.retain(|c| *c != node_id); | ||
} | ||
|
||
self.mapper.remove(&node_id); | ||
|
||
for child in node.1 { | ||
self.remove(child); | ||
} | ||
} | ||
} | ||
|
||
impl DOMAdapter<usize> for TestingDOM { | ||
fn children_of(&mut self, node_id: &usize) -> Vec<usize> { | ||
self.mapper | ||
.get(node_id) | ||
.map(|c| c.1.clone()) | ||
.unwrap_or_default() | ||
} | ||
|
||
fn parent_of(&self, node_id: &usize) -> Option<usize> { | ||
self.mapper.get(node_id).and_then(|c| c.0) | ||
} | ||
|
||
fn height(&self, node_id: &usize) -> Option<u16> { | ||
self.mapper.get(node_id).map(|c| c.2) | ||
} | ||
|
||
fn get_node(&self, node_id: &usize) -> Option<Node> { | ||
self.mapper.get(node_id).map(|c| c.3.clone()) | ||
} | ||
|
||
fn is_node_valid(&mut self, _node_id: &usize) -> bool { | ||
true | ||
} | ||
|
||
fn closest_common_parent(&self, node_id_a: &usize, _node_id_b: &usize) -> Option<usize> { | ||
Some(self.parent_of(node_id_a).unwrap_or(*node_id_a)) | ||
} | ||
} | ||
|
||
pub fn test_utils() -> (Torin<usize>, Option<TestingMeasurer>) { | ||
let layout = Torin::<usize>::new(); | ||
let measurer = Some(TestingMeasurer); | ||
|
||
(layout, measurer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
use torin::{prelude::*, test_utils::*}; | ||
|
||
#[test] | ||
pub fn display_horizontal() { | ||
let (mut layout, mut measurer) = test_utils(); | ||
|
||
let mut mocked_dom = TestingDOM::default(); | ||
mocked_dom.add( | ||
0, | ||
None, | ||
vec![1], | ||
Node::from_size_and_alignments_and_direction( | ||
Size::Pixels(Length::new(200.0)), | ||
Size::Pixels(Length::new(200.0)), | ||
Alignment::Center, | ||
Alignment::Center, | ||
DirectionMode::Horizontal, | ||
), | ||
); | ||
mocked_dom.add( | ||
1, | ||
Some(0), | ||
vec![], | ||
Node::from_size_and_direction( | ||
Size::Pixels(Length::new(100.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(50.0, 50.0), Size2D::new(100.0, 100.0)), | ||
); | ||
} | ||
|
||
#[test] | ||
pub fn display_vertical_with_inner_children() { | ||
let (mut layout, mut measurer) = test_utils(); | ||
|
||
let mut mocked_dom = TestingDOM::default(); | ||
mocked_dom.add( | ||
0, | ||
None, | ||
vec![1], | ||
Node::from_size_and_alignments_and_direction( | ||
Size::Pixels(Length::new(200.0)), | ||
Size::Pixels(Length::new(200.0)), | ||
Alignment::Center, | ||
Alignment::Center, | ||
DirectionMode::Vertical, | ||
), | ||
); | ||
mocked_dom.add( | ||
1, | ||
Some(0), | ||
vec![2], | ||
Node::from_size_and_padding( | ||
Size::Pixels(Length::new(100.0)), | ||
Size::Pixels(Length::new(100.0)), | ||
Gaps::new(5.0, 5.0, 5.0, 5.0), | ||
), | ||
); | ||
mocked_dom.add( | ||
2, | ||
Some(1), | ||
vec![], | ||
Node::from_size_and_direction( | ||
Size::Percentage(Length::new(100.0)), | ||
Size::Percentage(Length::new(100.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(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(50.0, 50.0), Size2D::new(100.0, 100.0)), | ||
); | ||
|
||
assert_eq!( | ||
layout.get(2).unwrap().area, | ||
Rect::new(Point2D::new(55.0, 55.0), Size2D::new(90.0, 90.0)), | ||
); | ||
} | ||
|
||
#[test] | ||
pub fn double_center_alignment() { | ||
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_alignments_and_direction( | ||
Size::Percentage(Length::new(100.0)), | ||
Size::Percentage(Length::new(100.0)), | ||
Alignment::Center, | ||
Alignment::Center, | ||
DirectionMode::Vertical, | ||
), | ||
); | ||
mocked_dom.add( | ||
1, | ||
Some(0), | ||
vec![], | ||
Node::from_size_and_direction( | ||
Size::Pixels(Length::new(200.0)), | ||
Size::Pixels(Length::new(200.0)), | ||
DirectionMode::Vertical, | ||
), | ||
); | ||
mocked_dom.add( | ||
2, | ||
Some(0), | ||
vec![], | ||
Node::from_size_and_direction( | ||
Size::Pixels(Length::new(300.0)), | ||
Size::Pixels(Length::new(300.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(1).unwrap().area, | ||
Rect::new(Point2D::new(400.0, 250.0), Size2D::new(200.0, 200.0)), | ||
); | ||
|
||
assert_eq!( | ||
layout.get(2).unwrap().area, | ||
Rect::new(Point2D::new(350.0, 450.0), Size2D::new(300.0, 300.0)), | ||
); | ||
} | ||
|
||
#[test] | ||
pub fn double_end_alignment() { | ||
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_alignments_and_direction( | ||
Size::Percentage(Length::new(100.0)), | ||
Size::Percentage(Length::new(100.0)), | ||
Alignment::End, | ||
Alignment::End, | ||
DirectionMode::Vertical, | ||
), | ||
); | ||
mocked_dom.add( | ||
1, | ||
Some(0), | ||
vec![], | ||
Node::from_size_and_direction( | ||
Size::Pixels(Length::new(200.0)), | ||
Size::Pixels(Length::new(200.0)), | ||
DirectionMode::Vertical, | ||
), | ||
); | ||
mocked_dom.add( | ||
2, | ||
Some(0), | ||
vec![], | ||
Node::from_size_and_direction( | ||
Size::Pixels(Length::new(300.0)), | ||
Size::Pixels(Length::new(300.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(1).unwrap().area, | ||
Rect::new(Point2D::new(800.0, 500.0), Size2D::new(200.0, 200.0)), | ||
); | ||
|
||
assert_eq!( | ||
layout.get(2).unwrap().area, | ||
Rect::new(Point2D::new(700.0, 700.0), Size2D::new(300.0, 300.0)), | ||
); | ||
} | ||
|
||
#[test] | ||
pub fn unsized_alignment() { | ||
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_alignments_and_direction_and_padding( | ||
Size::Inner, | ||
Size::Inner, | ||
Alignment::Center, | ||
Alignment::End, | ||
DirectionMode::Horizontal, | ||
Gaps::new(15.0, 15.0, 15.0, 15.0), | ||
), | ||
); | ||
mocked_dom.add( | ||
1, | ||
Some(0), | ||
vec![], | ||
Node::from_size_and_direction( | ||
Size::Pixels(Length::new(100.0)), | ||
Size::Pixels(Length::new(50.0)), | ||
DirectionMode::Vertical, | ||
), | ||
); | ||
mocked_dom.add( | ||
2, | ||
Some(0), | ||
vec![], | ||
Node::from_size_and_direction_and_margin( | ||
Size::Pixels(Length::new(150.0)), | ||
Size::Pixels(Length::new(80.0)), | ||
DirectionMode::Vertical, | ||
Gaps::new(10.0, 50.0, 20.0, 0.0), | ||
), | ||
); | ||
|
||
layout.measure( | ||
0, | ||
Rect::new(Point2D::new(0.0, 0.0), Size2D::new(350.0, 190.0)), | ||
&mut measurer, | ||
&mut mocked_dom, | ||
); | ||
|
||
assert_eq!( | ||
layout.get(1).unwrap().visible_area(), | ||
Rect::new(Point2D::new(15.0, 75.0), Size2D::new(100.0, 50.0)), | ||
); | ||
|
||
assert_eq!( | ||
layout.get(2).unwrap().visible_area(), | ||
Rect::new(Point2D::new(115.0, 25.0), Size2D::new(150.0, 80.0)), | ||
); | ||
} |
Oops, something went wrong.