Skip to content

Commit 23605a3

Browse files
committed
use exact measurements in tests
1 parent f16799d commit 23605a3

1 file changed

Lines changed: 28 additions & 17 deletions

File tree

tests/hidden_tiles.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ impl Behavior<&'static str> for TestBehavior {
2828

2929
const VIEWPORT: egui::Vec2 = egui::vec2(800.0, 600.0);
3030

31+
/// What `TestBehavior` leaves between two tiles, since it keeps the default `Behavior::gap_width`.
32+
const GAP_WIDTH: f32 = 1.0;
33+
3134
/// Two panes side by side, each wrapped in its own tab container by `all_panes_must_have_tabs`.
3235
fn harness() -> egui_kittest::Harness<'static, Tree<&'static str>> {
3336
let mut tiles = Tiles::default();
@@ -56,14 +59,22 @@ fn pane(tree: &Tree<&'static str>, name: &str) -> Option<TileId> {
5659
.map(|(tile_id, _)| *tile_id)
5760
}
5861

59-
/// The width the pane's slot takes up in the root container, tab bar and all.
60-
fn slot_width(tree: &Tree<&'static str>, name: &str) -> f32 {
62+
/// The rect the pane's slot in the root container was laid out in, tab bar and all,
63+
/// or `None` if it was never laid out.
64+
fn slot_rect(tree: &Tree<&'static str>, name: &str) -> Option<egui::Rect> {
6165
let pane_id = pane(tree, name).expect("the pane should still be in the tree");
6266
let slot = tree
6367
.tiles
6468
.parent_of(pane_id)
6569
.expect("`all_panes_must_have_tabs` should have given the pane a tab container");
66-
tree.tiles.rect(slot).map_or(0.0, |rect| rect.width())
70+
tree.tiles.rect(slot)
71+
}
72+
73+
/// The width the pane's slot takes up in the root container, tab bar and all.
74+
fn slot_width(tree: &Tree<&'static str>, name: &str) -> f32 {
75+
slot_rect(tree, name)
76+
.expect("the slot should have been laid out")
77+
.width()
6778
}
6879

6980
/// The width the whole tree was laid out in.
@@ -79,34 +90,34 @@ fn hiding_a_pane_gives_its_space_to_its_sibling() {
7990
let mut harness = harness();
8091

8192
let whole_width = tree_width(harness.state());
82-
let split_width = slot_width(harness.state(), "b");
83-
assert!(
84-
split_width < whole_width / 2.0 + 1.0,
85-
"the two panes should share the width to begin with, but `b` got {split_width} \
86-
out of {whole_width}"
93+
let split_width = (whole_width - GAP_WIDTH) / 2.0;
94+
assert_eq!(
95+
slot_width(harness.state(), "b"),
96+
split_width,
97+
"the two panes should split the {whole_width} the tree was laid out in"
8798
);
8899

89100
let hidden = pane(harness.state(), "a").expect("pane `a`");
90101
harness.state_mut().set_visible(hidden, false);
91102
harness.run();
92103

93-
let full_width = slot_width(harness.state(), "b");
94-
assert!(
95-
full_width > whole_width - 1.0,
96-
"`b` is the only thing left to show, so it should have the whole width of \
97-
{whole_width}, but got {full_width}"
104+
assert_eq!(
105+
slot_width(harness.state(), "b"),
106+
whole_width,
107+
"`b` is the only thing left to show, so it should have the whole width"
98108
);
99109
assert_eq!(
100-
slot_width(harness.state(), "a"),
101-
0.0,
110+
slot_rect(harness.state(), "a"),
111+
None,
102112
"the tab container around the hidden pane should not be laid out"
103113
);
104114

105115
harness.state_mut().set_visible(hidden, true);
106116
harness.run();
107117

108-
assert!(
109-
(slot_width(harness.state(), "b") - split_width).abs() < 1.0,
118+
assert_eq!(
119+
slot_width(harness.state(), "b"),
120+
split_width,
110121
"showing the pane again should restore the split"
111122
);
112123
}

0 commit comments

Comments
 (0)