Skip to content

Commit

Permalink
Generated tests: only assert scroll width/height for nodes that are s…
Browse files Browse the repository at this point in the history
…croll containers (#726)

* Fix documentation of scroll_height method

* Only assert scroll width/height for scrollable nodes

* Add "end" padding to computed content size
  • Loading branch information
nicoburns authored Oct 22, 2024
1 parent 0153947 commit 73cf7fc
Show file tree
Hide file tree
Showing 1,023 changed files with 17,068 additions and 168,154 deletions.
39 changes: 27 additions & 12 deletions scripts/gentest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ fn generate_test(name: impl AsRef<str>, description: &Value) -> TokenStream {
fn generate_assertions(ident: &str, node: &Value, use_rounding: bool) -> TokenStream {
let layout = if use_rounding { &node["smartRoundedLayout"] } else { &node["unroundedLayout"] };

fn is_scrollable(overflow: &Value) -> bool {
match overflow {
Value::String(ref value) => matches!(value.as_ref(), "hidden" | "scroll" | "auto"),
_ => false,
}
}
let is_scroll_container = is_scrollable(&node["style"]["overflowX"]) || is_scrollable(&node["style"]["overflowY"]);

let read_f32 = |s: &str| layout[s].as_f64().unwrap() as f32;
let read_naive_f32 = |s: &str| node["naivelyRoundedLayout"][s].as_f64().unwrap() as f32;
let width = read_f32("width");
Expand All @@ -260,33 +268,40 @@ fn generate_assertions(ident: &str, node: &Value, use_rounding: bool) -> TokenSt

let ident = Ident::new(ident, Span::call_site());

// The scrollWidth reading from chrome is only accurate if the node is scroll container. So only assert in that case.
// TODO: accurately test content size in the non-scroll-container case.
let scroll_assertions = if is_scroll_container {
quote!(
#[cfg(feature = "content_size")]
assert_eq!(layout.scroll_width(), #scroll_width, "scroll_width of node {:?}. Expected {}. Actual {}", #ident, #scroll_width, layout.scroll_width());
#[cfg(feature = "content_size")]
assert_eq!(layout.scroll_height(), #scroll_height, "scroll_height of node {:?}. Expected {}. Actual {}", #ident, #scroll_height, layout.scroll_height());
)
} else {
quote!()
};

if use_rounding {
quote!(
#[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
let layout @ Layout { size, location, .. } = taffy.layout(#ident).unwrap();
let layout = taffy.layout(#ident).unwrap();
let Layout { size, location, .. } = layout;
assert_eq!(size.width, #width, "width of node {:?}. Expected {}. Actual {}", #ident, #width, size.width);
assert_eq!(size.height, #height, "height of node {:?}. Expected {}. Actual {}", #ident, #height, size.height);
assert_eq!(location.x, #x, "x of node {:?}. Expected {}. Actual {}", #ident, #x, location.x);
assert_eq!(location.y, #y, "y of node {:?}. Expected {}. Actual {}", #ident, #y, location.y);
#[cfg(feature = "content_size")]
assert_eq!(layout.scroll_width(), #scroll_width, "scroll_width of node {:?}. Expected {}. Actual {}", #ident, #scroll_width, layout.scroll_width());
#[cfg(feature = "content_size")]
assert_eq!(layout.scroll_height(), #scroll_height, "scroll_height of node {:?}. Expected {}. Actual {}", #ident, #scroll_height, layout.scroll_height());
#scroll_assertions

#children
)
} else {
quote!(
#[cfg_attr(not(feature = "content_size"), allow(unused_variables))]
let layout @ Layout { size, location, .. } = taffy.layout(#ident).unwrap();
let layout = taffy.layout(#ident).unwrap();
let Layout { size, location, .. } = layout;
assert!((size.width - #width).abs() < 0.1, "width of node {:?}. Expected {}. Actual {}", #ident, #width, size.width);
assert!((size.height - #height).abs() < 0.1, "height of node {:?}. Expected {}. Actual {}", #ident, #height, size.height);
assert!((location.x - #x).abs() < 0.1, "x of node {:?}. Expected {}. Actual {}", #ident, #x, location.x);
assert!((location.y - #y).abs() < 0.1, "y of node {:?}. Expected {}. Actual {}", #ident, #y, location.y);
#[cfg(feature = "content_size")]
assert!((layout.scroll_width() - #scroll_width).abs() < 0.1, "scroll_width of node {:?}. Expected {}. Actual {}", #ident, #scroll_width, layout.scroll_width());
#[cfg(feature = "content_size")]
assert!((layout.scroll_height() - #scroll_height).abs() < 0.1, "scroll_height of node {:?}. Expected {}. Actual {}", #ident, #scroll_height, layout.scroll_height());
#scroll_assertions

#children
)
Expand Down
3 changes: 3 additions & 0 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1999,6 +1999,9 @@ fn final_layout_pass(
}
}

content_size.width += constants.content_box_inset.right - constants.border.right - constants.scrollbar_gutter.x;
content_size.height += constants.content_box_inset.bottom - constants.border.bottom - constants.scrollbar_gutter.y;

content_size
}

Expand Down
4 changes: 2 additions & 2 deletions src/tree/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ impl Layout {
)
}

/// Return the scroll width of the node.
/// The scroll width is the difference between the width and the content width, floored at zero
/// Return the scroll height of the node.
/// The scroll height is the difference between the height and the content height, floored at zero
pub fn scroll_height(&self) -> f32 {
f32_max(
0.0,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 8 additions & 80 deletions tests/generated/block/block_absolute_aspect_ratio_fill_height.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 73cf7fc

Please sign in to comment.