// 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/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(PElement el) { if (el == nullptr) return; el->markDirty(); // children are OwningPtr (move-only) — iterate by const ref, never copy. for (const auto& c : el->getChildren()) markAllDirtyRec(c); } static void resolveStyles(PElement 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(PElement root, int vw, int vh) { FlexLayout::layoutRoot(root, UVector2(vw, vh)); } // (a) row, fixed sizes, gap, justify start/end/center TEST(FlexLayout, RowGapJustify) { OElement a = new Div(Attributes{}); OElement b = new Div(Attributes{}); PElement aRef = a; PElement bRef = b; OElement root = new Div({Attributes{}}, {std::move(a), std::move(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(aRef->getLayoutPosition().x, 0.0f, 0.01f); EXPECT_NEAR(aRef->getLayoutPosition().y, 0.0f, 0.01f); EXPECT_NEAR(aRef->getLayoutDimensions().x, 64.0f, 0.01f); EXPECT_NEAR(bRef->getLayoutPosition().x, 72.0f, 0.01f); EXPECT_NEAR(bRef->getLayoutDimensions().y, 32.0f, 0.01f); // end-justified: last item flush to the right edge OElement c = new Div(Attributes{}); OElement d = new Div(Attributes{}); PElement cRef = c; PElement dRef = d; OElement end = new Div({Attributes{}}, {std::move(c), std::move(d)}); resolveStyles(end); layout(end, 300, 100); // total = 64 + 8 + 64 = 136; start at 300 - 136 = 164 EXPECT_NEAR(cRef->getLayoutPosition().x, 164.0f, 0.01f); EXPECT_NEAR(dRef->getLayoutPosition().x, 164.0f + 72.0f, 0.01f); // center-justified OElement e = new Div(Attributes{}); OElement f = new Div(Attributes{}); PElement eRef = e; PElement fRef = f; OElement center = new Div({Attributes{}}, {std::move(e), std::move(f)}); resolveStyles(center); layout(center, 300, 100); // free = 300 - 136 = 164; center offset = 82 EXPECT_NEAR(eRef->getLayoutPosition().x, 82.0f, 0.01f); EXPECT_NEAR(fRef->getLayoutPosition().x, 82.0f + 72.0f, 0.01f); } // (b) column: default direction stacks top-to-bottom TEST(FlexLayout, ColumnDefault) { OElement a = new Div(Attributes{}); OElement b = new Div(Attributes{}); PElement aRef = a; PElement bRef = b; OElement root = new Div({Attributes{}}, {std::move(a), std::move(b)}); resolveStyles(root); layout(root, 300, 200); EXPECT_NEAR(aRef->getLayoutPosition().y, 0.0f, 0.01f); EXPECT_NEAR(bRef->getLayoutPosition().y, 32.0f, 0.01f); // stretch is the default: a W_64 item stays 64 wide (explicit wins); y is packed EXPECT_NEAR(aRef->getLayoutDimensions().x, 64.0f, 0.01f); EXPECT_NEAR(bRef->getLayoutPosition().x, 0.0f, 0.01f); } // (c) one child with Grow absorbs all the free space TEST(FlexLayout, GrowAbsorbsFreeSpace) { OElement fixed = new Div(Attributes{}); OElement grow = new Div(Attributes{}); PElement fixedRef = fixed; PElement growRef = grow; OElement root = new Div({Attributes{}}, {std::move(fixed), std::move(grow)}); resolveStyles(root); layout(root, 200, 100); EXPECT_NEAR(fixedRef->getLayoutDimensions().x, 48.0f, 0.01f); EXPECT_NEAR(growRef->getLayoutPosition().x, 48.0f, 0.01f); // 200 - 48 free goes to the grower EXPECT_NEAR(growRef->getLayoutDimensions().x, 152.0f, 0.01f); } // (d) two growers split free space proportionally TEST(FlexLayout, GrowSplitsProportionally) { OElement g1 = new Div(Attributes{}); // weight 1 OElement g2 = new Div(Attributes{}); // weight 2 PElement g1Ref = g1; PElement g2Ref = g2; OElement root = new Div({Attributes{}}, {std::move(g1), std::move(g2)}); resolveStyles(root); layout(root, 300, 100); // 300 total, weights 1:2 -> 100 / 200 EXPECT_NEAR(g1Ref->getLayoutDimensions().x, 100.0f, 0.01f); EXPECT_NEAR(g2Ref->getLayoutPosition().x, 100.0f, 0.01f); EXPECT_NEAR(g2Ref->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) { OElement keep = new Div(Attributes{}); // refuses to shrink keep->setIntrinsicSize(150.0f, 32.0f); // natural main size 150 OElement shrink = new Div(Attributes{}); // default shrink=1 shrink->setIntrinsicSize(150.0f, 32.0f); PElement keepRef = keep; PElement shrinkRef = shrink; OElement root = new Div({Attributes{}}, {std::move(keep), std::move(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(keepRef->getLayoutDimensions().x, 150.0f, 0.01f); EXPECT_NEAR(keepRef->getLayoutPosition().x, 0.0f, 0.01f); EXPECT_NEAR(shrinkRef->getLayoutPosition().x, 150.0f, 0.01f); EXPECT_NEAR(shrinkRef->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) OElement a = new Div(Attributes{}); OElement b = new Div(Attributes{}); PElement aRef = a; PElement bRef = b; OElement root = new Div({Attributes{}}, {std::move(a), std::move(b)}); resolveStyles(root); layout(root, 200, 300); EXPECT_NEAR(aRef->getLayoutPosition().x, 0.0f, 0.01f); EXPECT_NEAR(aRef->getLayoutPosition().y, 0.0f, 0.01f); // line 1 starts below line 0: line0 cross = 32, + gap 8 -> y = 40 EXPECT_NEAR(bRef->getLayoutPosition().x, 0.0f, 0.01f); EXPECT_NEAR(bRef->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 { OElement a = new Div(Attributes{}); PElement aRef = a; OElement root = new Div({Attributes{}}, {std::move(a)}); resolveStyles(root); layout(root, 300, 200); EXPECT_NEAR(aRef->getLayoutPosition().y, 0.0f, 0.01f); EXPECT_NEAR(aRef->getLayoutDimensions().y, 32.0f, 0.01f); } // center: item vertically centered { OElement a = new Div(Attributes{}); PElement aRef = a; OElement root = new Div({Attributes{}}, {std::move(a)}); resolveStyles(root); layout(root, 300, 200); EXPECT_NEAR(aRef->getLayoutPosition().y, (200.0f - 32.0f) * 0.5f, 0.01f); } // end: item at the bottom { OElement a = new Div(Attributes{}); PElement aRef = a; OElement root = new Div({Attributes{}}, {std::move(a)}); resolveStyles(root); layout(root, 300, 200); EXPECT_NEAR(aRef->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) { OElement a = new Div(Attributes{}); a->setIntrinsicSize(64.0f, 32.0f); PElement aRef = a; OElement root = new Div({Attributes{}}, {std::move(a)}); resolveStyles(root); layout(root, 300, 200); EXPECT_NEAR(aRef->getLayoutDimensions().y, 200.0f, 0.01f); EXPECT_NEAR(aRef->getLayoutPosition().y, 0.0f, 0.01f); } } // (h) order reorders flow items (layout order, not DOM order) TEST(FlexLayout, Order) { OElement a = new Div(Attributes{}); OElement b = new Div(Attributes{}); PElement aRef = a; PElement bRef = b; OElement root = new Div({Attributes{}}, {std::move(a), std::move(b)}); resolveStyles(root); layout(root, 300, 100); // b (order 1) comes before a (order 2) EXPECT_NEAR(bRef->getLayoutPosition().x, 0.0f, 0.01f); EXPECT_NEAR(aRef->getLayoutPosition().x, 72.0f, 0.01f); } // align-self override: the item's own align beats the container's TEST(FlexLayout, AlignSelfOverride) { OElement a = new Div(Attributes{}); PElement aRef = a; OElement root = new Div({Attributes{}}, {std::move(a)}); resolveStyles(root); layout(root, 300, 200); // container says start (y=0), the item overrides to center EXPECT_NEAR(aRef->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) { OElement a = new Div(Attributes{}); OElement b = new Div(Attributes{}); PElement aRef = a; PElement bRef = b; OElement root = new Div({Attributes{}}, {std::move(a), std::move(b)}); resolveStyles(root); layout(root, 300, 100); // each item's basis = 50% of 300 = 150; no free space, no grow -> stays 150 EXPECT_NEAR(aRef->getLayoutDimensions().x, 150.0f, 0.01f); EXPECT_NEAR(bRef->getLayoutPosition().x, 150.0f, 0.01f); EXPECT_NEAR(bRef->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 { OElement a = new Div, H_32>(Attributes{}); PElement aRef = a; OElement root = new Div({Attributes{}}, {std::move(a)}); resolveStyles(root); layout(root, 300, 100); EXPECT_NEAR(aRef->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. { OElement a = new Div(Attributes{}); PElement aRef = a; OElement root = new Div({Attributes{}}, {std::move(a)}); resolveStyles(root); aRef->getStyle().minWidth = 80; aRef->getStyle().minWidthType = DimensionType::Pixel; layout(root, 300, 100); EXPECT_NEAR(aRef->getLayoutDimensions().x, 80.0f, 0.01f); } } // (k) position:relative: offset applied on top of the flow position TEST(FlexLayout, RelativeOffset) { OElement a = new Div(Attributes{}); OElement b = new Div(Attributes{}); PElement aRef = a; PElement bRef = b; OElement root = new Div({Attributes{}}, {std::move(a), std::move(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. aRef->getStyle().position = Position::Relative; aRef->getStyle().left = 10; aRef->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(aRef->getLayoutPosition().x, 10.0f, 0.01f); EXPECT_NEAR(aRef->getLayoutPosition().y, 5.0f, 0.01f); EXPECT_NEAR(bRef->getLayoutPosition().x, 72.0f, 0.01f); } // (l) position:absolute: removed from the flow, positioned from the container's content corner TEST(FlexLayout, AbsolutePosition) { OElement abs = new Div(Attributes{}); OElement flow = new Div(Attributes{}); PElement absRef = abs; PElement flowRef = flow; OElement root = new Div({Attributes{}}, {std::move(abs), std::move(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. absRef->getStyle().position = Position::Absolute; absRef->getStyle().left = 12; absRef->getStyle().top = 7; layout(root, 300, 100); // the absolute item is out of the flow: the flow item takes the full line EXPECT_NEAR(flowRef->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(absRef->getLayoutPosition().x, 12.0f, 0.01f); EXPECT_NEAR(absRef->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 OElement in1 = new Div(Attributes{}); OElement in2 = new Div(Attributes{}); PElement in1Ref = in1; PElement in2Ref = in2; // 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. OElement inner = new Div({Attributes{}}, {std::move(in1), std::move(in2)}); PElement innerRef = inner; OElement sibling = new Div(Attributes{}); PElement siblingRef = sibling; OElement outer = new Div({Attributes{}}, {std::move(inner), std::move(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(innerRef->getLayoutPosition().x, 4.0f, 0.01f); EXPECT_NEAR(innerRef->getLayoutPosition().y, 4.0f, 0.01f); EXPECT_NEAR(innerRef->getLayoutDimensions().x, 192.0f, 0.01f); EXPECT_NEAR(innerRef->getLayoutDimensions().y, 16.0f, 0.01f); // sibling stacks below inner: y = 4 + 16 + 0 (no gap) = 20 EXPECT_NEAR(siblingRef->getLayoutPosition().y, 20.0f, 0.01f); // inner row: in1 at x=4+0, in2 at x=4+32 EXPECT_NEAR(in1Ref->getLayoutPosition().x, 4.0f, 0.01f); EXPECT_NEAR(in2Ref->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) { OElement a = new Div(Attributes{}); OElement hidden = new Div(Attributes{}); OElement c = new Div(Attributes{}); PElement aRef = a; PElement hiddenRef = hidden; PElement cRef = c; OElement root = new Div({Attributes{}}, {std::move(a), std::move(hidden), std::move(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. hiddenRef->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(aRef->getLayoutPosition().x, 0.0f, 0.01f); EXPECT_NEAR(cRef->getLayoutPosition().x, 72.0f, 0.01f); // the hidden element itself is zeroed EXPECT_NEAR(hiddenRef->getLayoutPosition().x, 0.0f, 0.01f); EXPECT_NEAR(hiddenRef->getLayoutDimensions().x, 0.0f, 0.01f); }