Skip to content
Repos

DevPlaceApp

C++ 1.5 KB56 lines Opens in site

Directory Raw Download

#include "TestHarness.hpp"
#include "ui/TextEngine.hpp"
#include "ui/Views.hpp"

using namespace dp::ui;

namespace {

void prepare() {
  TextEngine::instance().init();
  TextEngine::instance().setScale(1.0f);
  Env::reset();
}

Size layoutIn(ViewPtr v, float w, float h) {
  v->assignIds(0, 0);
  const Size s = v->measure(Proposal{w, h});
  v->place({0, 0, w, h});
  return s;
}

}  // namespace

DP_TEST(layout_hstack_spacer_pushes_trailing_to_edge) {
  prepare();
  ViewPtr a = Text("A");
  ViewPtr b = Text("B");
  ViewPtr row = HStack({a, Spacer(), b}, 8);
  layoutIn(row, 300, 100);
  CHECK(b->bounds().right() <= 300.01f);
  CHECK(b->bounds().right() >= 299.0f);
  CHECK(a->bounds().x <= 0.01f);
}

DP_TEST(layout_text_does_not_wrap_when_it_fits_exactly) {
  prepare();
  ViewPtr dark = Text("Dark");
  ViewPtr icon = Icon("chevron.up.chevron.down", 13);
  ViewPtr inner = HStack({dark, icon}, 4);
  ViewPtr label = Text("Appearance")->maxWidth(Alignment::leading());
  ViewPtr row = HStack({label, inner}, 8);
  ViewPtr wrapped = row->frame(366, std::nullopt);
  layoutIn(wrapped, 366, 100);
  const Size natural = dark->measure(Proposal::ideal());
  CHECK(dark->bounds().w + 0.06f >= natural.w);
  CHECK(dark->bounds().h < 30);  // one line
}

DP_TEST(layout_vstack_wraps_long_text_to_width) {
  prepare();
  ViewPtr t = Text("The quick brown fox jumps over the lazy dog and keeps running far away");
  ViewPtr col = VStack({t}, 0, HAlign::Leading);
  const Size s = layoutIn(col, 120, 400);
  CHECK(s.w <= 120.01f);
  CHECK(s.h > 40);
}
19.4ms