Skip to content
Repos

mologram

C++ 6.8 KB183 lines Opens in site

Directory Raw Download

// M6 people dialogs: contacts, new chat, profile (shared media, members, roles, invite link, leave). SPEC FR-40, FR-41.
#include <fstream>

#include "rig.hpp"
#include "mologram/ui/dialogs/PeopleDialogs.hpp"

using namespace uitest;

TEST_CASE("people dialogs open and fit at 40x15, 80x24 and 160x50", "[ui][dialogs][integration]") {
  struct Case { const char* name; const char* title; std::function<void(Rig&)> open; };
  std::vector<Case> cases = {
      {"ContactsDialog", "Contacts", [](Rig& r) { ContactsDialog::open(r.app.context()); }},
      {"NewChatDialog", "New chat", [](Rig& r) { NewChatDialog::open(r.app.context()); }},
      {"ProfileDialog", "Profile", [](Rig& r) { ProfileDialog::open(r.app.context(), {PeerKind::User, 100}); }},
      {"ContactsPicker", "Pick", [](Rig& r) { ContactsDialog::pick(r.app.context(), "Pick", nullptr); }},
  };
  for (auto& c : cases)
    for (auto sz : {std::make_pair(40, 15), std::make_pair(80, 24), std::make_pair(160, 50)}) {
      Rig r(sz.first, sz.second);
      c.open(r);
      r.settle();
      ttgui::Window* w = r.window(c.name);
      REQUIRE(w != nullptr);
      CHECK(w->bounds.w <= sz.first);
      CHECK(w->bounds.h <= sz.second);
      CHECK(has(r.screen(), c.title));
    }
}

TEST_CASE("contacts: add from a search, block, unblock, import a file, open the chat", "[ui][dialogs][integration]") {
  Rig r;
  ContactsDialog::open(r.app.context());
  ttgui::Window* w = r.window("ContactsDialog");
  REQUIRE(w != nullptr);
  auto* list = r.widgets<ttgui::ListBox>(w)[0];
  CHECK(list->items.empty());  // the fake starts with no contacts
  r.type(r.edit(w, 0), "Carol");
  REQUIRE(!list->items.empty());
  CHECK(has(list->items[0], "Carol"));
  r.click(w, "Add");
  CHECK(has(r.screen(), "Added"));
  r.type(r.edit(w, 0), "");
  REQUIRE(list->items.size() == 1);
  r.click(w, "Block");
  r.type(r.edit(w, 0), "");
  CHECK(has(list->items[0], "(blocked)"));
  r.click(w, "Block");  // toggles
  r.type(r.edit(w, 0), "");
  CHECK(!has(list->items[0], "(blocked)"));

  std::string file = r.tmp.path + "/people.csv";
  { std::ofstream f(file); f << "Dave Example, +31611111111\nbad line\nEve, +31622222222\n"; }
  auto parsed = ContactsDialog::parseImport("Dave, 1\n\nnocomma\nEve , 2 \n");
  REQUIRE(parsed.size() == 2);
  CHECK_EQ(parsed[1].first, std::string("Eve"));
  CHECK_EQ(parsed[1].second, std::string("2"));
  r.type(r.edit(w, 1), file);
  r.click(w, "Import");
  CHECK(has(r.screen(), "Imported 2 contacts"));
  CHECK_EQ(list->items.size(), (size_t)3);
  r.type(r.edit(w, 1), r.tmp.path + "/missing.csv");
  r.click(w, "Import");
  CHECK(has(r.screen(), "Cannot read"));

  list->selected = 0;
  list->onActivated.emit(0);
  r.settle();
  CHECK(!r.hasWindow("ContactsDialog"));
}

TEST_CASE("contacts picker returns the chosen person once", "[ui][dialogs][integration]") {
  Rig r;
  js::Object p;
  p["user"] = js::Value("user:101"); p["name"] = js::Value("Bob");
  r.app.context().client.request("add", "contacts.add", p, [](const RpcResult&) {});
  r.settle();
  int calls = 0;
  Peer got;
  ContactsDialog::pick(r.app.context(), "Pick", [&](const Peer& x) { calls++; got = x; });
  r.settle();
  ttgui::Window* w = r.window("ContactsPicker");
  REQUIRE(w != nullptr);
  r.widgets<ttgui::ListBox>(w)[0]->onActivated.emit(0);
  r.settle();
  CHECK_EQ(calls, 1);
  CHECK_EQ(got.id, (std::int64_t)101);
  CHECK(!r.hasWindow("ContactsPicker"));
}

TEST_CASE("new chat dialog creates a group with the chosen members", "[ui][dialogs][integration]") {
  CHECK_EQ(NewChatDialog::kindFor(0), std::string("group"));
  CHECK_EQ(NewChatDialog::kindFor(1), std::string("channel"));
  CHECK_EQ(NewChatDialog::kindFor(2), std::string("forum"));
  Rig r;
  for (const char* u : {"user:100", "user:101"}) {
    js::Object p;
    p["user"] = js::Value(u); p["name"] = js::Value("x");
    r.app.context().client.request("add", "contacts.add", p, [](const RpcResult&) {});
  }
  r.settle();
  NewChatDialog::open(r.app.context());
  r.settle();
  ttgui::Window* w = r.window("NewChatDialog");
  REQUIRE(w != nullptr);
  auto* list = r.widgets<ttgui::ListBox>(w)[0];
  REQUIRE(list->items.size() == 2);
  list->onActivated.emit(0);
  CHECK(has(list->items[0], "[x]"));
  r.click(w, "Create");
  CHECK(r.hasWindow("NewChatDialog"));  // a title is required
  CHECK(has(r.screen(), "Give the chat a title"));
  r.type(r.edit(w), "Team");
  r.click(w, "Create");
  CHECK(!r.hasWindow("NewChatDialog"));
  bool found = false;
  for (auto& d : r.svc.dialogs()) if (d.name == "Team") found = true;
  CHECK(found);
  r.settle();
  bool listed = false;
  for (auto& row : r.app.shell().allChats) if (row.name == "Team") listed = true;
  CHECK(listed);  // the chat list was reloaded
}

TEST_CASE("profile dialog: info, shared media sections, members with roles, invite link, member management", "[ui][dialogs][integration]") {
  Rig r;
  ProfileDialog::open(r.app.context(), {PeerKind::Chat, 200});
  r.settle();
  ttgui::Window* w = r.window("ProfileDialog");
  REQUIRE(w != nullptr);
  auto* list = r.widgets<ttgui::ListBox>(w)[0];
  auto* section = r.widgets<ttgui::ComboBox>(w)[0];
  REQUIRE(list->items.size() >= 5);
  CHECK(has(list->items[0], "Family"));
  CHECK_EQ(ProfileDialog::sections().size(), (size_t)5);
  for (int i = 1; i <= 3; i++) { section->onSelected.emit(i); r.settle(); CHECK(!list->items.empty()); }  // media, files, links
  section->onSelected.emit(4);
  r.settle();
  REQUIRE(list->items.size() == 4);
  CHECK(has(list->items[0], "(owner)"));
  CHECK(has(list->items[1], "(admin)"));

  list->selected = 3;
  r.click(w, "Make admin");
  CHECK(has(r.screen(), "Admin rights granted"));
  r.click(w, "Remove member");
  CHECK(has(r.screen(), "Member removed"));
  section->onSelected.emit(4);
  r.settle();
  CHECK_EQ(list->items.size(), (size_t)3);

  std::string copied;
  r.app.controller().copyToClipboard = [&](const std::string& s) { copied = s; };
  r.click(w, "Invite link");
  CHECK(has(copied, "t.me/"));

  r.click(w, "Add member");
  r.settle();
  CHECK(r.hasWindow("ContactsPicker"));

  r.click(w, "Leave");
  r.settle();
  CHECK(!r.hasWindow("ProfileDialog"));
  for (auto& d : r.svc.dialogs()) CHECK(d.peer.id != 200);
}

TEST_CASE("profile dialog of a person: block and open chat; the info section shows flags", "[ui][dialogs][integration]") {
  Rig r;
  ProfileDialog::open(r.app.context(), {PeerKind::User, 103});
  r.settle();
  ttgui::Window* w = r.window("ProfileDialog");
  REQUIRE(w != nullptr);
  auto* list = r.widgets<ttgui::ListBox>(w)[0];
  bool bot = false;
  for (auto& it : list->items) if (has(it, "bot")) bot = true;
  CHECK(bot);
  CHECK(r.button(w, "Invite link") == nullptr);  // people have no invite link
  r.click(w, "Block");
  CHECK(has(r.screen(), "Blocked"));
  r.click(w, "Open chat");
  r.settle();
  CHECK_EQ(r.app.controller().current().id, (std::int64_t)103);
  CHECK(!r.hasWindow("ProfileDialog"));
}
33.3ms