Skip to content
Repos

DevPlaceApp

C++ 5.3 KB132 lines Opens in site

Directory Raw Download

#include <cstdlib>
#include <string>

#include "SocialTestSupport.hpp"
#include "screens/components/MessageBubble.hpp"
#include "screens/components/TypingIndicator.hpp"
#include "screens/conversation/ComposerRow.hpp"
#include "screens/conversation/ConversationScreen.hpp"
#include "spec/SocialSpec.hpp"
#include "ui/Palette.hpp"
#include "ui/TextEngine.hpp"

using namespace dp;
using namespace dp::test;
using namespace dp::ui;

namespace {

void prepare(bool dark = true) {
  TextEngine::instance().init();
  TextEngine::instance().setScale(1.0f);
  Appearance::setDark(dark);
  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;
}

// Writes a PNG when SOCIAL_SHOT_DIR is set (visual checks); never required by the assertions.
void maybeShot(ViewPtr root, float w, float h, const std::string& name, bool dark = true) {
  const char* dir = std::getenv("SOCIAL_SHOT_DIR");
  if (!dir) return;
  Appearance::setDark(dark);
  Canvas canvas;
  canvas.resize(static_cast<int>(w), static_cast<int>(h), 1.0f);
  Env::reset();
  root->assignIds(0, 0);
  root->measure(Proposal{w, h});
  root->place({0, 0, w, h});
  canvas.clear(Palette::bg2());
  root->draw(canvas);
  canvas.savePng(std::string(dir) + "/" + name + ".png");
}

// First descendant whose width dropped below `fullWidth` (the bubble inside the full width row).
View* narrowest(View* v, float fullWidth) {
  while (v->bounds().w >= fullWidth && !v->children().empty()) v = v->children()[0].get();
  return v;
}

Message sample(bool mine, const std::string& text, bool read = false) {
  Message m;
  m.data.id = mine ? "m-mine" : "m-other";
  m.data.content = text;
  m.data.read = read;
  m.data.createdAt = DateCodec::now() - std::chrono::minutes(3);
  m.isMine = mine;
  m.sender = MockFixtures::alice();
  return m;
}

}  // namespace

DP_TEST(social_render_typing_indicator_metrics) {
  prepare();
  ViewPtr v = TypingIndicatorView("alice");
  const Size s = layoutIn(v, 300, 100);
  // 3 dots of 5 + 2 gaps of 3 = 21, padding 10 each side; height 5 + 2 * 8
  CHECK(std::abs(s.w - 41.0f) < 0.6f);
  CHECK(std::abs(s.h - 21.0f) < 0.6f);
  maybeShot(TypingIndicatorView("alice")->padding(20), 120, 70, "typing-dark");
}

DP_TEST(social_render_bubble_alignment_and_width_cap) {
  prepare();
  ViewPtr mine = MessageBubbleView(sample(true, "short", true), 252);
  ViewPtr theirs = MessageBubbleView(sample(false, std::string(400, 'w') + " " + std::string(300, 'v')), 252);
  layoutIn(mine, 400, 800);
  layoutIn(theirs, 400, 800);
  CHECK(mine->bounds().w <= 400.5f);
  // The bubble itself sits at the trailing edge for own messages and at the leading edge for the others.
  View* mineBubble = narrowest(mine.get(), 399);
  View* theirBubble = narrowest(theirs.get(), 399);
  CHECK(mineBubble->bounds().right() >= 399.5f);
  CHECK(mineBubble->bounds().w < 200);  // hugs the short text
  CHECK(theirBubble->bounds().w <= 252.5f);
  CHECK(theirBubble->bounds().x <= 0.5f);
  maybeShot(VStack({MessageBubbleView(sample(true, "Looks great", true), 252), MessageBubbleView(sample(false, "Pretty good!"), 252)}, 12)->padding(12), 400, 200, "bubbles-dark");
  maybeShot(VStack({MessageBubbleView(sample(true, "Looks great", true), 252), MessageBubbleView(sample(false, "Pretty good!"), 252)}, 12)->padding(12), 400, 200, "bubbles-light", false);
}

DP_TEST(social_render_composer_row_aligns_send_button_with_field_bottom) {
  prepare();
  ViewPtr field = RectangleShape(Color::rgb(0x112233))->frame(std::nullopt, 40);
  ViewPtr below = RectangleShape(Color::rgb(0x332211))->frame(std::nullopt, 15);
  ViewPtr send = RectangleShape(Color::rgb(0xFF0000))->frame(36, 36);
  ViewPtr row = ComposerRow(field, below, send);
  const Size s = layoutIn(row, 300, 200);
  CHECK(std::abs(s.h - (40 + 4 + 15)) < 0.1f);
  CHECK(std::abs(send->bounds().bottom() - field->bounds().bottom()) < 0.1f);  // aligned to the field, not the column
  CHECK(std::abs(send->bounds().right() - 300) < 0.1f);
  CHECK(std::abs(field->bounds().w - (300 - 36 - 8)) < 0.1f);
  // A taller trailing view grows the row above the field bottom.
  ViewPtr tall = RectangleShape(Color::rgb(0xFF0000))->frame(36, 60);
  ViewPtr row2 = ComposerRow(RectangleShape(Color::rgb(1))->frame(std::nullopt, 40), RectangleShape(Color::rgb(2))->frame(std::nullopt, 15), tall);
  const Size s2 = layoutIn(row2, 300, 200);
  CHECK(std::abs(s2.h - (60 + 4 + 15)) < 0.1f);
}

DP_TEST(social_render_conversation_screen_states) {
  SignedInMock mock;
  prepare();
  ConversationScreen screen(MockFixtures::alice());
  bool loaded = false;
  screen.model().load([&] { loaded = true; });
  CHECK(pumpUntil([&] { return loaded; }));
  CHECK_EQ(screen.model().messages().size(), static_cast<std::size_t>(8));
  CHECK(std::abs(screen.bubbleMaxWidth() - spec::SocialMetrics::kBubbleDefaultWidth) < 0.1f);  // width unknown yet
  ViewPtr body = screen.body();
  layoutIn(body, 430, 800);
  layoutIn(screen.body(), 430, 800);  // second frame sees the measured width
  CHECK(std::abs(screen.bubbleMaxWidth() - (430 - 24 - 48)) < 0.6f);
  maybeShot(screen.body(), 430, 800, "conversation-dark");
  // Typing indicator overlay appears while the other user types.
  screen.model().handle(ChatSocketEvent::typing(MockFixtures::alice().id));
  CHECK(screen.model().isOtherTyping());
  maybeShot(screen.body(), 430, 800, "conversation-typing-dark");
}
51.1ms