329 lines
15 KiB
C++
329 lines
15 KiB
C++
// CPU-only flexbox layout tests. Build caller-owned Div/Text trees (no System, so the
|
|||
|
|
// OwningPtr never deletes them), run FlexLayout::layoutRoot, assert position/dimensions.
|
||
|
|
#include "gtest/gtest.h"
|
||
|
|
#include "UI/Element.h"
|
||
|
|
#include "UI/Element/Div.h"
|
||
|
|
#include "UI/Element/Text.h"
|
||
|
|
#include "UI/FlexLayout.h"
|
||
|
|
#include "UI/Style/Class.h"
|
||
|
|
#include "UI/Style/Theme.h"
|
||
|
|
|
||
|
|
using namespace Seele;
|
||
|
|
using namespace Seele::UI;
|
||
|
|
|
||
|
|
// style the tree like System::styleRec would (flexbox math runs on the resolved style).
|
||
|
|
// mark the whole subtree dirty first so the Div template classes (applied in
|
||
|
|
// applyStyle, gated on style.dirty/themeChanged) actually run — a freshly built tree
|
||
|
|
// has neither flag set. tests use explicit sizes, so the font is never shaped.
|
||
|
|
static void markAllDirtyRec(Element* el) {
|
||
|
|
if (el == nullptr) return;
|
||
|
|
el->markDirty();
|
||
|
|
for (auto* c : el->getChildren()) markAllDirtyRec(c);
|
||
|
|
}
|
||
|
|
static void resolveStyles(Element* root) {
|
||
|
|
markAllDirtyRec(root);
|
||
|
|
Theme theme{};
|
||
|
|
root->calcStyle(Style{}, theme);
|
||
|
|
}
|
||
|
|
|
||
|
|
// layout a caller-owned tree against `viewport` (the tree is never deleted here).
|
||
|
|
static void layout(Element* root, int vw, int vh) {
|
||
|
|
FlexLayout::layoutRoot(root, UVector2(vw, vh));
|
||
|
|
}
|
||
|
|
|
||
|
|
// (a) row, fixed sizes, gap, justify start/end/center
|
||
|
|
TEST(FlexLayout, RowGapJustify) {
|
||
|
|
Element* a = new Div<W_64, H_32>(Attributes{});
|
||
|
|
Element* b = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, G_8, W_Full, H_Full>({Attributes{}}, {a, b});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 100);
|
||
|
|
// two 64px items + 8px gap, start-justified: x=0 and x=72, y=0, cross stretch?
|
||
|
|
// items have explicit H_32, so no stretch (explicit wins); align default = stretch
|
||
|
|
// but explicit size wins -> y=0, h=32.
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(a->getLayoutDimensions().x, 64.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(b->getLayoutPosition().x, 72.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(b->getLayoutDimensions().y, 32.0f, 0.01f);
|
||
|
|
|
||
|
|
// end-justified: last item flush to the right edge
|
||
|
|
Element* c = new Div<W_64, H_32>(Attributes{});
|
||
|
|
Element* d = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* end = new Div<FlexRow, G_8, Justify_End, W_Full, H_Full>({Attributes{}}, {c, d});
|
||
|
|
resolveStyles(end);
|
||
|
|
layout(end, 300, 100);
|
||
|
|
// total = 64 + 8 + 64 = 136; start at 300 - 136 = 164
|
||
|
|
EXPECT_NEAR(c->getLayoutPosition().x, 164.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(d->getLayoutPosition().x, 164.0f + 72.0f, 0.01f);
|
||
|
|
|
||
|
|
// center-justified
|
||
|
|
Element* e = new Div<W_64, H_32>(Attributes{});
|
||
|
|
Element* f = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* center = new Div<FlexRow, G_8, Justify_Center, W_Full, H_Full>({Attributes{}}, {e, f});
|
||
|
|
resolveStyles(center);
|
||
|
|
layout(center, 300, 100);
|
||
|
|
// free = 300 - 136 = 164; center offset = 82
|
||
|
|
EXPECT_NEAR(e->getLayoutPosition().x, 82.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(f->getLayoutPosition().x, 82.0f + 72.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (b) column: default direction stacks top-to-bottom
|
||
|
|
TEST(FlexLayout, ColumnDefault) {
|
||
|
|
Element* a = new Div<W_64, H_32>(Attributes{});
|
||
|
|
Element* b = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<W_Full, H_Full>({Attributes{}}, {a, b});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 200);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(b->getLayoutPosition().y, 32.0f, 0.01f);
|
||
|
|
// stretch is the default: a W_64 item stays 64 wide (explicit wins); y is packed
|
||
|
|
EXPECT_NEAR(a->getLayoutDimensions().x, 64.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(b->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (c) one child with Grow absorbs all the free space
|
||
|
|
TEST(FlexLayout, GrowAbsorbsFreeSpace) {
|
||
|
|
Element* fixed = new Div<W_48, H_32>(Attributes{});
|
||
|
|
auto* grow = new Div<Grow, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, W_Full, H_Full>({Attributes{}}, {fixed, grow});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 200, 100);
|
||
|
|
EXPECT_NEAR(fixed->getLayoutDimensions().x, 48.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(grow->getLayoutPosition().x, 48.0f, 0.01f);
|
||
|
|
// 200 - 48 free goes to the grower
|
||
|
|
EXPECT_NEAR(grow->getLayoutDimensions().x, 152.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (d) two growers split free space proportionally
|
||
|
|
TEST(FlexLayout, GrowSplitsProportionally) {
|
||
|
|
auto* g1 = new Div<Grow, H_32>(Attributes{}); // weight 1
|
||
|
|
auto* g2 = new Div<Grow_2, H_32>(Attributes{}); // weight 2
|
||
|
|
auto* root = new Div<FlexRow, W_Full, H_Full>({Attributes{}}, {g1, g2});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 100);
|
||
|
|
// 300 total, weights 1:2 -> 100 / 200
|
||
|
|
EXPECT_NEAR(g1->getLayoutDimensions().x, 100.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(g2->getLayoutPosition().x, 100.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(g2->getLayoutDimensions().x, 200.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (e) overflow: a Shrink_0 item keeps its basis; the default-shrink item absorbs the deficit
|
||
|
|
TEST(FlexLayout, ShrinkDeficit) {
|
||
|
|
auto* keep = new Div<Shrink_0, H_32>(Attributes{}); // refuses to shrink
|
||
|
|
keep->setIntrinsicSize(150.0f, 32.0f); // natural main size 150
|
||
|
|
auto* shrink = new Div<H_32>(Attributes{}); // default shrink=1
|
||
|
|
shrink->setIntrinsicSize(150.0f, 32.0f);
|
||
|
|
auto* root = new Div<FlexRow, W_Full, H_Full>({Attributes{}}, {keep, shrink});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 180, 100);
|
||
|
|
// total basis 300 in a 180 box: deficit 120 falls entirely on the shrinker (weight 1 vs 0)
|
||
|
|
EXPECT_NEAR(keep->getLayoutDimensions().x, 150.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(keep->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(shrink->getLayoutPosition().x, 150.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(shrink->getLayoutDimensions().x, 30.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (f) wrap: items that don't fit the line flow onto the next line
|
||
|
|
TEST(FlexLayout, Wrap) {
|
||
|
|
// 150px items in a 200px row with wrap: item 2 can't share line 0 (150+8+150 > 200)
|
||
|
|
Element* a = new Div<W_150, H_32>(Attributes{});
|
||
|
|
Element* b = new Div<W_150, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, Wrap, G_8, W_Full, H_Full>({Attributes{}}, {a, b});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 200, 300);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, 0.0f, 0.01f);
|
||
|
|
// line 1 starts below line 0: line0 cross = 32, + gap 8 -> y = 40
|
||
|
|
EXPECT_NEAR(b->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(b->getLayoutPosition().y, 40.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (g) align-items: start/center/end on the cross axis (items keep their explicit cross size)
|
||
|
|
TEST(FlexLayout, AlignItems) {
|
||
|
|
// start: item at the top of a 200px-tall row
|
||
|
|
{
|
||
|
|
Element* a = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, Items_Start, W_Full, H_Full>({Attributes{}}, {a});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 200);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(a->getLayoutDimensions().y, 32.0f, 0.01f);
|
||
|
|
}
|
||
|
|
// center: item vertically centered
|
||
|
|
{
|
||
|
|
Element* a = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, Items_Center, W_Full, H_Full>({Attributes{}}, {a});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 200);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, (200.0f - 32.0f) * 0.5f, 0.01f);
|
||
|
|
}
|
||
|
|
// end: item at the bottom
|
||
|
|
{
|
||
|
|
Element* a = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, Items_End, W_Full, H_Full>({Attributes{}}, {a});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 200);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, 200.0f - 32.0f, 0.01f);
|
||
|
|
}
|
||
|
|
// stretch: an Auto-height item fills the cross axis (default items is stretch,
|
||
|
|
// so a bare container already does this; assert it explicitly)
|
||
|
|
{
|
||
|
|
auto* a = new Div<W_64>(Attributes{});
|
||
|
|
a->setIntrinsicSize(64.0f, 32.0f);
|
||
|
|
auto* root = new Div<FlexRow, W_Full, H_Full>({Attributes{}}, {a});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 200);
|
||
|
|
EXPECT_NEAR(a->getLayoutDimensions().y, 200.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, 0.0f, 0.01f);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// (h) order reorders flow items (layout order, not DOM order)
|
||
|
|
TEST(FlexLayout, Order) {
|
||
|
|
Element* a = new Div<W_64, H_32, Order_2>(Attributes{});
|
||
|
|
Element* b = new Div<W_64, H_32, Order_1>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, G_8, W_Full, H_Full>({Attributes{}}, {a, b});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 100);
|
||
|
|
// b (order 1) comes before a (order 2)
|
||
|
|
EXPECT_NEAR(b->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().x, 72.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// align-self override: the item's own align beats the container's
|
||
|
|
TEST(FlexLayout, AlignSelfOverride) {
|
||
|
|
Element* a = new Div<W_64, H_32, Self_Center>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, Items_Start, W_Full, H_Full>({Attributes{}}, {a});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 200);
|
||
|
|
// container says start (y=0), the item overrides to center
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, (200.0f - 32.0f) * 0.5f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (i) percent basis: Basis_50Percent resolves against the container's content main
|
||
|
|
TEST(FlexLayout, PercentBasis) {
|
||
|
|
auto* a = new Div<Basis_50Percent, H_32>(Attributes{});
|
||
|
|
auto* b = new Div<Basis_50Percent, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, W_Full, H_Full>({Attributes{}}, {a, b});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 100);
|
||
|
|
// each item's basis = 50% of 300 = 150; no free space, no grow -> stays 150
|
||
|
|
EXPECT_NEAR(a->getLayoutDimensions().x, 150.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(b->getLayoutPosition().x, 150.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(b->getLayoutDimensions().x, 150.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (j) min/max clamps: an explicit size is clamped to [min, max]
|
||
|
|
TEST(FlexLayout, MinMaxClamp) {
|
||
|
|
// maxWidth 64 clamps a 100px explicit width down to 64
|
||
|
|
{
|
||
|
|
Element* a = new Div<W_100, MaxWidthClass<64, DimensionType::Pixel>, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, W_Full, H_Full>({Attributes{}}, {a});
|
||
|
|
resolveStyles(root);
|
||
|
|
layout(root, 300, 100);
|
||
|
|
EXPECT_NEAR(a->getLayoutDimensions().x, 64.0f, 0.01f);
|
||
|
|
}
|
||
|
|
// minWidth 80 clamps a 40px explicit width up to 80. set AFTER resolveStyles:
|
||
|
|
// applyStyle resets non-inherited fields (minWidth etc) to defaults, so a
|
||
|
|
// pre-set minWidth would be clobbered by the re-style. layout reads the style
|
||
|
|
// at layout time, so a post-resolve mutation is honored.
|
||
|
|
{
|
||
|
|
auto* a = new Div<W_40, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, W_Full, H_Full>({Attributes{}}, {a});
|
||
|
|
resolveStyles(root);
|
||
|
|
a->getStyle().minWidth = 80;
|
||
|
|
a->getStyle().minWidthType = DimensionType::Pixel;
|
||
|
|
layout(root, 300, 100);
|
||
|
|
EXPECT_NEAR(a->getLayoutDimensions().x, 80.0f, 0.01f);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// (k) position:relative: offset applied on top of the flow position
|
||
|
|
TEST(FlexLayout, RelativeOffset) {
|
||
|
|
Element* a = new Div<W_64, H_32>(Attributes{});
|
||
|
|
Element* b = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, G_8, W_Full, H_Full>({Attributes{}}, {a, b});
|
||
|
|
resolveStyles(root);
|
||
|
|
// set position/left/top AFTER resolveStyles: applyStyle resets position to
|
||
|
|
// Static and left/top to 0, so pre-set values would be clobbered. layout
|
||
|
|
// reads the style at layout time, so a post-resolve mutation is honored.
|
||
|
|
a->getStyle().position = Position::Relative;
|
||
|
|
a->getStyle().left = 10;
|
||
|
|
a->getStyle().top = 5;
|
||
|
|
layout(root, 300, 100);
|
||
|
|
// a is still in the flow at x=0 but drawn offset by (10, 5); b is unaffected
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().x, 10.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().y, 5.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(b->getLayoutPosition().x, 72.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (l) position:absolute: removed from the flow, positioned from the container's content corner
|
||
|
|
TEST(FlexLayout, AbsolutePosition) {
|
||
|
|
auto* abs = new Div<W_32, H_32>(Attributes{});
|
||
|
|
Element* flow = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, W_Full, H_Full>({Attributes{}}, {abs, flow});
|
||
|
|
resolveStyles(root);
|
||
|
|
// set position/left/top AFTER resolveStyles: applyStyle resets position to
|
||
|
|
// Static and left/top to 0, so pre-set values would be clobbered. layout
|
||
|
|
// reads the style at layout time, so a post-resolve mutation is honored.
|
||
|
|
abs->getStyle().position = Position::Absolute;
|
||
|
|
abs->getStyle().left = 12;
|
||
|
|
abs->getStyle().top = 7;
|
||
|
|
layout(root, 300, 100);
|
||
|
|
// the absolute item is out of the flow: the flow item takes the full line
|
||
|
|
EXPECT_NEAR(flow->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
// absolute is at the container's content corner + (left, top); the container has
|
||
|
|
// no padding, so it's exactly (12, 7) in the container's parent space.
|
||
|
|
EXPECT_NEAR(abs->getLayoutPosition().x, 12.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(abs->getLayoutPosition().y, 7.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (m) nested flex: a Div-in-Div with its own children lays out correctly at both levels
|
||
|
|
TEST(FlexLayout, NestedFlex) {
|
||
|
|
// inner row of two 32px items; outer column stacks it with a sibling
|
||
|
|
Element* in1 = new Div<W_32, H_16>(Attributes{});
|
||
|
|
Element* in2 = new Div<W_32, H_16>(Attributes{});
|
||
|
|
// inner is W_Full (fills the outer's content width) but Auto height:
|
||
|
|
// its cross size falls back to the content measurement (max child = 16).
|
||
|
|
// an explicit H_Full would resolve to 100% of the outer's 100px height,
|
||
|
|
// which is wrong for this test — the inner should size to its children.
|
||
|
|
auto* inner = new Div<FlexRow, W_Full>({Attributes{}}, {in1, in2});
|
||
|
|
auto* sibling = new Div<W_Full, H_24>(Attributes{});
|
||
|
|
auto* outer = new Div<W_Full, H_Full, P_4>({Attributes{}}, {inner, sibling});
|
||
|
|
resolveStyles(outer);
|
||
|
|
layout(outer, 200, 100);
|
||
|
|
// outer has P_4 padding: content origin at (4, 4). inner is W_Full -> fills
|
||
|
|
// the 200-8=192 wide content; its H is Auto -> content height = max child = 16.
|
||
|
|
EXPECT_NEAR(inner->getLayoutPosition().x, 4.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(inner->getLayoutPosition().y, 4.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(inner->getLayoutDimensions().x, 192.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(inner->getLayoutDimensions().y, 16.0f, 0.01f);
|
||
|
|
// sibling stacks below inner: y = 4 + 16 + 0 (no gap) = 20
|
||
|
|
EXPECT_NEAR(sibling->getLayoutPosition().y, 20.0f, 0.01f);
|
||
|
|
// inner row: in1 at x=4+0, in2 at x=4+32
|
||
|
|
EXPECT_NEAR(in1->getLayoutPosition().x, 4.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(in2->getLayoutPosition().x, 36.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|
||
|
|
// (n) hidden: a Hidden element is not laid out and its siblings are not pushed by it
|
||
|
|
TEST(FlexLayout, HiddenSkipsLayout) {
|
||
|
|
Element* a = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* hidden = new Div<W_64, H_32>(Attributes{});
|
||
|
|
Element* c = new Div<W_64, H_32>(Attributes{});
|
||
|
|
auto* root = new Div<FlexRow, G_8, W_Full, H_Full>({Attributes{}}, {a, hidden, c});
|
||
|
|
resolveStyles(root);
|
||
|
|
// set Hidden AFTER resolveStyles: applyStyle resets style=parentStyle, which
|
||
|
|
// would clobber a pre-set display=Hidden. the runtime mutation path is
|
||
|
|
// set-style-field + markDirty, but markDirty triggers a re-style that resets
|
||
|
|
// from parent. so set it after the final re-style, before layout.
|
||
|
|
hidden->getStyle().display = Display::Hidden;
|
||
|
|
layout(root, 300, 100);
|
||
|
|
// a at x=0; the hidden item contributes no footprint and no gap; c at x=72
|
||
|
|
EXPECT_NEAR(a->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(c->getLayoutPosition().x, 72.0f, 0.01f);
|
||
|
|
// the hidden element itself is zeroed
|
||
|
|
EXPECT_NEAR(hidden->getLayoutPosition().x, 0.0f, 0.01f);
|
||
|
|
EXPECT_NEAR(hidden->getLayoutDimensions().x, 0.0f, 0.01f);
|
||
|
|
}
|
||
|
|
|