Skip to content
Repos

DevPlaceApp

C++ 6.5 KB198 lines Opens in site

Directory Raw Download

#include <SDL3/SDL.h>

#include <cstdlib>

#include "TestHarness.hpp"
#include "screens/components/DevPlaceTextEditor.hpp"
#include "screens/components/EmojiPicker.hpp"
#include "screens/components/SelectableTextPopover.hpp"
#include "ui/Overlays.hpp"
#include "ui/Palette.hpp"
#include "ui/TextEngine.hpp"

using namespace dp;
using namespace dp::ui;

namespace {

void prepare() {
  TextEngine::instance().init();
  TextEngine::instance().setScale(1.0f);
  Env::reset();
  Appearance::setDark(true);
  Overlays::instance().clear();
}

void layoutIn(const ViewPtr& v, float w, float h) {
  v->assignIds(0, 0);
  v->measure(Proposal{w, h});
  v->place({0, 0, w, h});
}

// Places the view at the size it picks for the proposal (not the whole proposal).
Size layoutFit(const ViewPtr& v, float w, float h) {
  v->assignIds(0, 0);
  const Size s = v->measure(Proposal{w, h});
  v->place({0, 0, s.w, s.h});
  return s;
}

View* findLabel(View* v, const std::string& label) {
  if (v->label() == label) return v;
  for (const ViewPtr& c : v->children()) {
    if (View* f = findLabel(c.get(), label)) return f;
  }
  return nullptr;
}

// Same routing as Host::mouseDown / mouseUp for a tap at the center of the labeled view.
bool tapLabel(const ViewPtr& root, const std::string& label) {
  View* target = findLabel(root.get(), label);
  if (!target) return false;
  const Point p = target->bounds().center();
  std::vector<View*> path;
  root->hits(p, path);
  for (View* v : path) {
    if (v->acceptsPress()) {
      v->onTap(p);
      return true;
    }
  }
  return false;
}

void maybeSave(const Canvas& c, const char* name) {
  const char* dir = std::getenv("DEVPLACE_TEST_PNG_DIR");
  if (!dir || !*dir) return;
  CHECK(c.savePng(std::string(dir) + "/" + name));
}

ViewPtr overlayRoot(Size window, Canvas* canvas) {
  UiState::instance().now += 1.0;  // animations (fade, slide) run on UiState::now
  ViewPtr root = Overlays::instance().view(window);
  layoutIn(root, window.w, window.h);
  if (canvas) {
    canvas->resize(static_cast<int>(window.w), static_cast<int>(window.h), 1.0f);
    canvas->clear(Palette::bg2());
    root->draw(*canvas);
  }
  return root;
}

}  // namespace

DP_TEST(compose_popover_long_press_copy_and_quote) {
  prepare();
  std::string inserted;
  QuoteComposer quote{true, [&](const std::string& q) { inserted += q; }};
  ViewPtr host = SelectableTextPopover(Text("rendered"), "raw **source** text", quote);
  layoutIn(host, 300, 60);
  CHECK(!Overlays::instance().any());
  host->onLongPress({10, 10});
  CHECK(Overlays::instance().any());

  Canvas canvas;
  ViewPtr root = overlayRoot({430, 400}, &canvas);
  maybeSave(canvas, "popover.png");
  CHECK(tapLabel(root, "Select first word"));
  root = overlayRoot({430, 400}, nullptr);
  CHECK(tapLabel(root, "Insert as quote"));
  CHECK_EQ(inserted, std::string("> raw\n\n"));  // first word selected, then quoted
  CHECK(!Overlays::instance().any());            // closes after the action

  host->onLongPress({10, 10});
  root = overlayRoot({430, 400}, nullptr);
  CHECK(tapLabel(root, "Select all text"));
  root = overlayRoot({430, 400}, nullptr);
  CHECK(tapLabel(root, "Copy to clipboard"));
  CHECK(!Overlays::instance().any());
  char* clip = SDL_GetClipboardText();
  const std::string copied = clip ? clip : "";
  SDL_free(clip);
  CHECK(copied.empty() || copied == "raw **source** text");  // headless clipboard may be unavailable
}

DP_TEST(compose_popover_hides_quote_button_when_inactive) {
  prepare();
  ViewPtr host = SelectableTextPopover(Text("x"), "some text", QuoteComposer{});
  layoutIn(host, 300, 60);
  host->onLongPress({1, 1});
  ViewPtr root = overlayRoot({430, 400}, nullptr);
  CHECK(findLabel(root.get(), "Copy to clipboard") != nullptr);
  CHECK(findLabel(root.get(), "Insert as quote") == nullptr);
  Overlays::instance().clear();
}

DP_TEST(compose_editor_keeps_focus_and_resizes_with_the_band) {
  prepare();
  std::string text;
  Binding b{[&] { return text; }, [&](const std::string& v) { text = v; }};
  auto handle = std::make_shared<TextInputHandle>();
  DevPlaceTextEditorOptions o;
  o.placeholder = "Say";
  o.initialLineCount = 3;
  o.handle = handle;
  auto build = [&] {
    ViewPtr v = DevPlaceTextEditor(b, o);
    layoutFit(v, 300, 800);
    return v;
  };
  ViewPtr v = build();
  const float h0 = v->bounds().h;
  CHECK(h0 > DevPlaceTextEditorMetrics::singleLineHeight());
  const std::uint64_t idBefore = handle->id;
  CHECK(idBefore != 0);
  v = build();
  handle->focus();
  std::vector<View*> interior;
  v->hits({20, 20}, interior);
  CHECK(!interior.empty());
  CHECK(interior.front()->isTextInput());  // the text input, not the resize band, owns the interior
  // The input id must not change when the placeholder disappears.
  text = "hello";
  v = build();
  CHECK_EQ(handle->id, idBefore);

  // Drag in the bottom band grows the height; the views are rebuilt every frame, so find the target again each time.
  auto bandTarget = [&](const ViewPtr& root, float y) {
    std::vector<View*> path;
    root->hits({100, y}, path);
    for (View* p : path) if (p->acceptsPress()) return p;
    return static_cast<View*>(nullptr);
  };
  View* press = bandTarget(v, h0 - 5);
  CHECK(press != nullptr);
  CHECK(press->wantsDrag());
  press->onDragBegin({100, h0 - 5});
  press->onDragMove({100, h0 + 45}, {0, 50});
  press->onDragEnd({100, h0 + 45});
  v = build();
  const float grown = v->bounds().h;
  CHECK(grown > h0 + 40);
  press = bandTarget(v, grown - 5);
  CHECK(press != nullptr);
  press->onDragBegin({100, grown - 5});
  press->onDragMove({100, -500}, {0, -500});  // far upward: clamped to one line
  press->onDragEnd({100, -500});
  v = build();
  CHECK_EQ(v->bounds().h, DevPlaceTextEditorMetrics::singleLineHeight());
}

DP_TEST(compose_emoji_picker_view_lays_out_all_categories) {
  prepare();
  auto model = std::make_shared<EmojiPickerModel>();
  std::string picked;
  ViewPtr v = EmojiPickerView(model, [&](const std::string& e) { picked = e; }, nullptr);
  layoutIn(v, 430, 800);
  CHECK_EQ(model->sectionIds.size(), static_cast<std::size_t>(9));  // no recents in a fresh store
  CHECK(model->sectionOffsets[1] > model->sectionOffsets[0]);
  CHECK(findLabel(v.get(), "Flags") != nullptr);                     // category bar button label
  model->search = "grinning";
  v = EmojiPickerView(model, [&](const std::string& e) { picked = e; }, nullptr);
  layoutIn(v, 430, 800);
  CHECK(findLabel(v.get(), "Flags") == nullptr);                     // no bar while searching
  model->search = "zzzzqqq";
  v = EmojiPickerView(model, nullptr, nullptr);
  layoutIn(v, 430, 800);
  CHECK(findLabel(v.get(), "No emoji found") != nullptr);
}
19.1ms