// Spec: INV-posts 5.3 and 5.2: what is measured is what is placed (rows must not overlap their successors).
#include <cmath>
#include "TestHarness.hpp"
#include "screens/comments/CommentEditor.hpp"
#include "api/mock/MockFixtures.hpp"
#include "screens/AppContext.hpp"
#include "screens/comments/CommentView.hpp"
#include "screens/feed/FeedPostView.hpp"
#include "screens/comments/CommentsView.hpp"
#include "store/AppState.hpp"
#include "ui/Palette.hpp"
#include "ui/TextEngine.hpp"
using namespace dp;
using namespace ui;
DP_TEST(posts_layout_comment_editor_measured_equals_placed) {
TextEngine::instance().init();
TextEngine::instance().setScale(1.0f);
Env::reset();
CommentEditor editor("layout-test");
std::string text = "Great write-up, thanks for sharing!";
CommentEditorProps props;
props.text = Binding{[&] { return text; }, [&](const std::string& v) { text = v; }};
props.initialHeight = 21;
ViewPtr marker = RectangleShape(Palette::red())->frame(100, 10);
ViewPtr column = VStack({editor.view(props), marker}, 0, HAlign::Leading);
column->assignIds(1, 0);
const Size size = column->measure(Proposal::width(398));
column->place({0, 0, size.w, size.h});
const Rect& m = marker->bounds();
CHECK(m.y + m.h <= size.h + 0.01f);
// the editor view ends before the marker starts
const Rect& e = column->children()[0]->bounds();
CHECK(e.y + e.h <= m.y + 0.01f);
}
DP_TEST(posts_layout_comments_list_rows_do_not_overlap_next_section) {
TextEngine::instance().init();
TextEngine::instance().setScale(1.0f);
Env::reset();
const std::vector<Comment> comments = MockFixtures::comments();
CommentsSession session;
CommentsProps props;
props.comments = &comments;
props.session = &session;
props.showsDividers = true;
session.beginFrame();
ViewPtr marker = RectangleShape(Palette::red())->frame(100, 10);
ViewPtr list = CommentsView(props);
ViewPtr column = VStack({list, marker}, 0, HAlign::Leading);
column->assignIds(1, 0);
const Size size = column->measure(Proposal::width(398));
column->place({0, 0, size.w, size.h});
float bottomOfRows = 0;
for (const ViewPtr& row : list->children()) {
bottomOfRows = std::max(bottomOfRows, row->bounds().bottom());
}
CHECK(bottomOfRows <= marker->bounds().y + 0.01f);
session.endFrame();
}
DP_TEST(posts_layout_feed_card_rows_are_inflation_free) {
TextEngine::instance().init();
TextEngine::instance().setScale(1.0f);
Env::reset();
const std::vector<Post> posts = MockFixtures::posts();
CommentsSession session;
FeedPostContext ctx;
ctx.session = &session;
for (const Post& post : posts) {
session.beginFrame();
ViewPtr marker = RectangleShape(Palette::red())->frame(100, 10);
ViewPtr card = FeedPostView(post, ctx);
ViewPtr column = VStack({card, marker}, 0, HAlign::Leading);
column->assignIds(1, 0);
const Size size = column->measure(Proposal::width(398));
column->place({0, 0, size.w, size.h});
// the card is as tall as it measured, so the next card starts where this one ends
CHECK(std::fabs(card->bounds().h - card->measure(Proposal::width(398)).h) < 0.01f);
CHECK(card->bounds().bottom() <= marker->bounds().y + 0.01f);
session.endFrame();
}
}