// M6 settings, login, accounts, emoji, language table, preferences and themes. SPEC FR-01..04, FR-50, FR-60..66.
#include <fstream>
#include "rig.hpp"
#include "mologram/ActionRegistry.hpp"
#include "mologram/ui/ThemeCatalog.hpp"
#include "mologram/ui/dialogs/EmojiDialog.hpp"
#include "mologram/ui/dialogs/LoginDialog.hpp"
#include "mologram/ui/dialogs/SettingsDialog.hpp"
using namespace uitest;
// ------------------------------------------------------------------ language table, preferences, themes
TEST_CASE("language table: every key exists in every language, unknown keys and languages are safe", "[ui][settings][unit]") {
Strings s;
CHECK_EQ(s.tr("title.search"), std::string("Search"));
CHECK(s.set("nl"));
CHECK_EQ(s.tr("title.search"), std::string("Zoeken"));
CHECK(s.set("de"));
CHECK_EQ(s.tr("btn.cancel"), std::string("Abbrechen"));
CHECK(!s.set("xx"));
CHECK_EQ(s.language(), std::string("de"));
CHECK_EQ(s.tr("no.such.key"), std::string("no.such.key"));
for (const auto& key : Strings::keys())
for (const auto& lang : Strings::languages()) CHECK(Strings::has(lang, key));
CHECK_EQ(Strings::languageName("nl"), std::string("Nederlands"));
}
TEST_CASE("preferences: atomic save and load, corrupt file rejected, recent emoji unique and newest first", "[ui][settings][unit]") {
TempDir d;
std::string path = d.path + "/sub/prefs.json";
{
PrefsStore p(path);
p.setStr("theme", "night");
p.setKeybinding("quit", "Ctrl+Z");
p.addRecentEmoji("a"); p.addRecentEmoji("b"); p.addRecentEmoji("a");
p.setWindows({{"user:100", 1, 2, 30, 10}, {"chat:5", 3, 4, 40, 12}});
std::string err;
REQUIRE(p.save(err));
CHECK(!std::filesystem::exists(path + ".tmp"));
}
PrefsStore q(path);
std::string err;
REQUIRE(q.load(err));
CHECK_EQ(q.str("theme"), std::string("night"));
CHECK_EQ(q.keybindings().at("quit"), std::string("Ctrl+Z"));
auto rec = q.recentEmoji();
REQUIRE(rec.size() == 2);
CHECK_EQ(rec[0], std::string("a"));
auto w = q.windows();
REQUIRE(w.size() == 2);
CHECK_EQ(w[1].peer, std::string("chat:5"));
CHECK_EQ(w[1].h, 12);
{ std::ofstream f(path); f << "{ not json"; }
PrefsStore bad(path);
CHECK(!bad.load(err));
CHECK(has(err, "rejected"));
q.clearKeybindings();
CHECK(q.keybindings().empty());
}
TEST_CASE("themes change the colours and fall back to defaults for unknown names", "[ui][settings][unit]") {
ttgui::Config c = ttgui::Config::defaults();
auto grey = c.windowColor("grey");
CHECK(ThemeCatalog::apply(c, "night"));
CHECK(!(c.windowColor("grey").bg == grey.bg));
CHECK(ThemeCatalog::apply(c, "contrast"));
CHECK(ThemeCatalog::apply(c, "default"));
CHECK(c.windowColor("grey").bg == grey.bg);
CHECK(!ThemeCatalog::apply(c, "nope"));
CHECK(c.windowColor("grey").bg == grey.bg);
CHECK_EQ(ThemeCatalog::names().size(), (size_t)3);
}
TEST_CASE("preferences saved earlier are applied at startup: language, theme and key bindings", "[ui][settings][integration]") {
Rig r(120, 34, "", "", true, R"({"language":"nl","theme":"night","keybindings":{"quit":"Ctrl+Z"}})");
CHECK_EQ(r.app.context().tr.language(), std::string("nl"));
CHECK_EQ(r.app.config().keyFor("quit"), std::string("Ctrl+Z"));
bool dutch = false;
for (auto& m : r.app.menuBar()->menus) if (m == "Bericht") dutch = true;
CHECK(dutch);
r.app.app().running = true;
r.press("Ctrl+Z");
CHECK(!r.app.app().running);
}
// ------------------------------------------------------------------ settings dialog
TEST_CASE("settings dialog opens every section at 40x15, 80x24 and 160x50", "[ui][settings][integration]") {
for (auto sz : {std::make_pair(40, 15), std::make_pair(80, 24), std::make_pair(160, 50)})
for (int section = 0; section < (int)SettingsDialog::sections().size(); section++) {
Rig r(sz.first, sz.second);
SettingsDialog::open(r.app.context(), section);
r.settle();
ttgui::Window* w = r.window("SettingsDialog");
REQUIRE(w != nullptr);
CHECK(w->bounds.w <= sz.first);
CHECK(w->bounds.h <= sz.second);
CHECK(has(r.screen(), "Settings"));
}
}
TEST_CASE("settings: profile edit, privacy cycling, sessions and notifications reach the service", "[ui][settings][integration]") {
Rig r;
SettingsDialog::open(r.app.context(), 0);
r.settle();
ttgui::Window* w = r.window("SettingsDialog");
REQUIRE(w != nullptr);
auto edits = r.widgets<ttgui::EditBox>(w);
REQUIRE(edits.size() >= 4);
CHECK_EQ(edits[0]->text, std::string("Fake")); // account.profile name "Fake Account" split into first and last
CHECK_EQ(edits[1]->text, std::string("Account"));
r.type(edits[2], "newname");
r.click(w, "Save");
CHECK(has(r.screen(), "Saved"));
auto combo = r.widgets<ttgui::ComboBox>(w)[0];
combo->onSelected.emit(1); // Privacy
r.settle();
auto list = r.widgets<ttgui::ListBox>(w)[0];
REQUIRE(list->items.size() == SettingsDialog::privacyKeys().size());
CHECK(has(list->items[0], "Last seen: everybody"));
list->onActivated.emit(0);
r.settle();
CHECK(has(list->items[0], "Last seen: contacts"));
CHECK_EQ(SettingsDialog::nextRule("nobody"), std::string("everybody"));
combo->onSelected.emit(2); // Sessions
r.settle();
auto tables = r.widgets<ttgui::Table>(w);
REQUIRE(!tables.empty());
REQUIRE(tables[0]->rows.size() == 1);
CHECK_EQ(tables[0]->rows[0][1], std::string("yes"));
r.click(w, "Terminate");
CHECK(has(r.screen(), "Session ended"));
combo->onSelected.emit(3); // Notifications
r.settle();
auto checks = r.widgets<ttgui::CheckBox>(w);
REQUIRE(checks.size() >= 3);
checks[1]->checked = true;
checks[1]->onChanged.emit(true);
r.settle();
bool groupsMuted = false;
r.app.context().client.request("n", "account.notifications", {}, [&](const RpcResult& res) { groupsMuted = js::getBool(*res.value.find("groups"), "muted"); });
r.settle();
CHECK(groupsMuted);
}
TEST_CASE("settings: Premium-only privacy keys three ways", "[ui][settings][premium][integration]") {
Rig r;
SettingsDialog::open(r.app.context(), 1);
r.settle();
ttgui::Window* w = r.window("SettingsDialog");
REQUIRE(w != nullptr);
auto list = r.widgets<ttgui::ListBox>(w)[0];
size_t voice = 0;
const auto& keys = SettingsDialog::privacyKeys();
for (size_t i = 0; i < keys.size(); i++) if (keys[i].id == "voice_messages") voice = i;
CHECK(has(list->items[voice], "[P]") || has(list->items[voice], "\xF0\x9F\x94\x92"));
// not Premium: the calm panel, nothing changes
r.svc.setPremium(mologram::PremiumState::NotPremium);
r.settle();
list->onActivated.emit((int)voice);
r.settle();
CHECK(r.hasWindow("PremiumPanel"));
CHECK(has(list->items[voice], "everybody"));
r.app.app().wm.closeWindow(r.window("PremiumPanel"));
r.settle();
// Premium: the rule changes
r.svc.setPremium(mologram::PremiumState::Premium);
r.settle();
CHECK(!has(list->items[voice], "[P]"));
list->onActivated.emit((int)voice);
r.settle();
CHECK(has(list->items[voice], "contacts"));
// Premium lost midway: the server refuses, the state flips, the value stays as it was
r.svc.revokePremiumSilently();
list->onActivated.emit((int)voice);
r.settle();
CHECK(!r.app.premium().has());
CHECK(has(list->items[voice], "contacts"));
}
TEST_CASE("settings: theme and language apply at once and are remembered", "[ui][settings][integration]") {
Rig r;
SettingsDialog::open(r.app.context(), 4);
r.settle();
ttgui::Window* w = r.window("SettingsDialog");
REQUIRE(w != nullptr);
auto combos = r.widgets<ttgui::ComboBox>(w);
REQUIRE(combos.size() >= 3); // section, theme, language
auto before = r.app.app().cfg.windowColor("grey").bg;
combos[1]->onSelected.emit(1); // night
CHECK(!(r.app.app().cfg.windowColor("grey").bg == before));
CHECK_EQ(r.app.prefs().str("theme"), std::string("night"));
combos[2]->onSelected.emit(1); // nl
r.settle();
CHECK_EQ(r.app.context().tr.language(), std::string("nl"));
bool dutch = false;
for (auto& m : r.app.menuBar()->menus) if (m == "Venster") dutch = true;
CHECK(dutch);
PrefsStore reread(r.tmp.path + "/prefs.json");
std::string err;
REQUIRE(reread.load(err));
CHECK_EQ(reread.str("language"), std::string("nl"));
CHECK_EQ(reread.str("theme"), std::string("night"));
}
TEST_CASE("settings: key binding editor rebinds, rejects conflicts, saves, and resets", "[ui][settings][integration]") {
Rig r;
SettingsDialog::open(r.app.context(), 5);
r.settle();
ttgui::Window* w = r.window("SettingsDialog");
REQUIRE(w != nullptr);
auto* table = r.widgets<ttgui::Table>(w)[0];
size_t quit = 0, help = 0;
for (size_t i = 0; i < mologram::ActionRegistry::all().size(); i++) {
if (mologram::ActionRegistry::all()[i].id == "quit") quit = i;
if (mologram::ActionRegistry::all()[i].id == "help") help = i;
}
table->selected = (int)quit;
r.click(w, "Rebind");
REQUIRE((bool)r.app.context().keyCapture);
CHECK(has(r.screen(), "Press the new key for quit"));
ttgui::KeyEventData k = r.key("Ctrl+Z");
r.app.app().running = true;
ttgui::Event ev = ttgui::Event::makeKey(k);
r.app.app().dispatch(ev); // the capture takes the key press
CHECK(!(bool)r.app.context().keyCapture);
CHECK_EQ(r.app.config().keyFor("quit"), std::string("Ctrl+Z"));
CHECK(r.app.app().running); // the key press was consumed by the editor, it did not quit
r.press("Ctrl+Z");
CHECK(!r.app.app().running); // and it quits now
table->selected = (int)help;
r.click(w, "Rebind");
ttgui::Event ev2 = ttgui::Event::makeKey(r.key("Ctrl+Z"));
r.app.app().dispatch(ev2);
CHECK_EQ(r.app.config().keyFor("help"), std::string("F1")); // refused: already used by quit
CHECK(has(r.screen(), "already used by quit"));
r.click(w, "Rebind");
ttgui::Event esc = ttgui::Event::makeKey(r.key("Escape"));
r.app.app().dispatch(esc);
CHECK(has(r.screen(), "Cancelled"));
PrefsStore reread(r.tmp.path + "/prefs.json");
std::string err;
REQUIRE(reread.load(err));
CHECK_EQ(reread.keybindings().at("quit"), std::string("Ctrl+Z"));
r.click(w, "Reset all");
CHECK_EQ(r.app.config().keyFor("quit"), std::string("Ctrl+Q"));
CHECK(r.app.prefs().keybindings().empty());
CHECK_EQ(SettingsDialog::conflictFor(r.app.config(), "help", "Ctrl+Q"), std::string("quit"));
CHECK_EQ(SettingsDialog::conflictFor(r.app.config(), "fmt_link", "Ctrl+K"), std::string("")); // the allowed shared pair (D-08)
}
TEST_CASE("settings Premium section: explains a feature without Premium, nothing happens with it", "[ui][settings][premium][integration]") {
Rig r;
SettingsDialog::open(r.app.context(), 6);
r.settle();
ttgui::Window* w = r.window("SettingsDialog");
REQUIRE(w != nullptr);
CHECK(has(r.screen(), "not active"));
r.click(w, "Use selected feature");
CHECK(r.hasWindow("PremiumPanel"));
r.app.app().wm.closeWindow(r.window("PremiumPanel"));
r.svc.setPremium(mologram::PremiumState::Premium);
r.settle();
SettingsDialog::open(r.app.context(), 6);
r.settle();
CHECK(has(r.screen(), "active"));
ttgui::Window* w2 = r.window("SettingsDialog");
r.click(w2, "Use selected feature");
CHECK(!r.hasWindow("PremiumPanel"));
}
// ------------------------------------------------------------------ login, accounts
TEST_CASE("login dialog opens in every state at 40x15, 80x24 and 160x50", "[ui][login][integration]") {
for (const char* state : {"need_phone", "need_code", "need_password", "qr"})
for (auto sz : {std::make_pair(40, 15), std::make_pair(80, 24), std::make_pair(160, 50)}) {
Rig r(sz.first, sz.second);
LoginDialog::open(r.app.context(), state, "");
r.settle();
ttgui::Window* w = r.window("LoginDialog");
REQUIRE(w != nullptr);
CHECK(w->bounds.w <= sz.first);
CHECK(w->bounds.h <= sz.second);
}
}
TEST_CASE("login: phone, wrong code with the server text, resend, 2FA password, ready", "[ui][login][integration]") {
Rig r(100, 30, "", "", false);
REQUIRE(r.hasWindow("LoginDialog")); // the app asks the service and finds it logged out
ttgui::Window* w = r.window("LoginDialog");
r.type(r.edit(w), "+31600000000");
r.click(w, "Next");
w = r.window("LoginDialog");
REQUIRE(w != nullptr);
CHECK(has(r.screen(), "Login code"));
r.type(r.edit(w), "00000");
r.click(w, "Next");
w = r.window("LoginDialog");
CHECK(has(r.screen(), "PHONE_CODE_INVALID")); // the server's text, unchanged
r.click(w, "Resend code");
w = r.window("LoginDialog");
CHECK(has(r.screen(), "A new code was sent"));
r.type(r.edit(w), "12345");
r.click(w, "Next");
w = r.window("LoginDialog");
CHECK(has(r.screen(), "Password"));
r.type(r.edit(w), "wrong");
r.click(w, "Next");
w = r.window("LoginDialog");
CHECK(has(r.screen(), "PASSWORD_HASH_INVALID"));
r.type(r.edit(w), "secret");
r.click(w, "Next");
CHECK(!r.hasWindow("LoginDialog"));
bool ready = false;
r.app.context().client.request("s", "auth.state", {}, [&](const RpcResult& res) { ready = js::getStr(res.value, "state") == "ready"; });
r.settle();
CHECK(ready);
}
TEST_CASE("login by QR code shows the token and finishes when the service reports ready", "[ui][login][integration]") {
Rig r(100, 40, "", "", false);
ttgui::Window* w = r.window("LoginDialog");
REQUIRE(w != nullptr);
r.click(w, "QR code");
w = r.window("LoginDialog");
REQUIRE(w != nullptr);
auto qrs = r.widgets<ttgui::QrWidget>(w);
REQUIRE(qrs.size() == 1);
CHECK_EQ(qrs[0]->text, std::string("tg://login?token=FAKE"));
CHECK(qrs[0]->ok());
REQUIRE((bool)r.app.context().onAuthState);
r.app.context().onAuthState("ready");
r.settle();
CHECK(!r.hasWindow("LoginDialog"));
// a phone button goes back
Rig r2(100, 40, "", "", false);
ttgui::Window* w2 = r2.window("LoginDialog");
r2.click(w2, "QR code");
r2.click(r2.window("LoginDialog"), "Phone");
CHECK(r2.hasWindow("LoginDialog"));
}
TEST_CASE("accounts: profiles are found by their session files; switch, add and log out", "[ui][login][integration]") {
Rig r;
{ std::ofstream a(r.tmp.path + "/work.session"); std::ofstream b(r.tmp.path + "/personal.session"); std::ofstream c(r.tmp.path + "/main.session.lock"); }
auto names = AccountsDialog::accounts(r.app.config());
REQUIRE(names.size() == 3);
CHECK_EQ(names[0], std::string("main"));
CHECK_EQ(names[1], std::string("personal"));
CHECK_EQ(names[2], std::string("work"));
AccountsDialog::open(r.app.context());
ttgui::Window* w = r.window("AccountsDialog");
REQUIRE(w != nullptr);
auto* list = r.widgets<ttgui::ListBox>(w)[0];
list->selected = 2;
r.app.app().running = true;
r.click(w, "Switch");
CHECK_EQ(r.app.restartAccount(), std::string("work"));
CHECK(!r.app.app().running);
Rig r2;
AccountsDialog::open(r2.app.context());
ttgui::Window* w2 = r2.window("AccountsDialog");
r2.type(r2.edit(w2), "newone");
r2.click(w2, "Add");
CHECK_EQ(r2.app.restartAccount(), std::string("newone"));
Rig r3;
AccountsDialog::open(r3.app.context());
r3.click(r3.window("AccountsDialog"), "Log out");
CHECK(r3.hasWindow("LoginDialog"));
CHECK(has(r3.screen(), "Logged out"));
}
TEST_CASE("account profiles get their own session and socket paths", "[ui][login][unit]") {
Config c = Config::fromText(R"({"data_dir":"/d","account":"work"})");
CHECK_EQ(c.str("session_path"), std::string("/d/work.session"));
CHECK(has(c.str("socket_path"), "mologram-work.sock"));
Config main = Config::fromText(R"({"data_dir":"/d"})");
CHECK(has(main.str("session_path"), "main.session"));
Config explicitPath = Config::fromText(R"({"data_dir":"/d","account":"work","session_path":"/x/s"})");
CHECK_EQ(explicitPath.str("session_path"), std::string("/x/s"));
main.setAccount("home");
CHECK_EQ(main.str("session_path"), std::string("/d/home.session"));
}
// ------------------------------------------------------------------ emoji
TEST_CASE("emoji dialog opens at 40x15, 80x24 and 160x50", "[ui][emoji][integration]") {
for (auto sz : {std::make_pair(40, 15), std::make_pair(80, 24), std::make_pair(160, 50)}) {
Rig r(sz.first, sz.second);
r.openAlice();
EmojiDialog::open(r.app.context());
r.settle();
ttgui::Window* w = r.window("EmojiDialog");
REQUIRE(w != nullptr);
CHECK(w->bounds.w <= sz.first);
CHECK(w->bounds.h <= sz.second);
CHECK(has(r.screen(), "Emoji"));
}
}
TEST_CASE("emoji: pick inserts and is remembered, stickers send, custom emoji are Premium (three ways)", "[ui][emoji][premium][integration]") {
Rig r;
r.openAlice();
EmojiDialog::open(r.app.context());
r.settle();
ttgui::Window* w = r.window("EmojiDialog");
REQUIRE(w != nullptr);
auto pickers = r.widgets<ttgui::EmojiPicker>(w);
REQUIRE(pickers.size() == 1);
pickers[0]->onPick.emit("\xF0\x9F\x91\x8D");
CHECK_EQ(r.composer().editor->getText(), std::string("\xF0\x9F\x91\x8D"));
auto recents = r.app.prefs().recentEmoji();
REQUIRE(!recents.empty());
CHECK_EQ(recents.front(), std::string("\xF0\x9F\x91\x8D"));
auto combo = r.widgets<ttgui::ComboBox>(w)[0];
auto list = r.widgets<ttgui::ListBox>(w)[0];
combo->onSelected.emit(1); // Recent
r.settle();
REQUIRE(list->items.size() == 1);
CHECK(list->visible);
CHECK(!pickers[0]->visible);
combo->onSelected.emit(2); // Stickers
r.settle();
REQUIRE(list->items.size() == 6);
list->onActivated.emit(0);
r.settle();
CHECK(r.svc.history({PeerKind::User, 100}).back().media.kind == MediaKind::Sticker);
auto search = r.widgets<ttgui::EditBox>(w)[1];
r.type(search, "love");
CHECK_EQ(list->items.size(), (size_t)1);
// custom emoji: NotPremium -> panel; Premium -> inserted and sent; lost midway -> refused, text comes back
r.type(search, "");
combo->onSelected.emit(3);
r.settle();
REQUIRE(list->items.size() == 6);
r.composer().editor->setText("");
r.svc.setPremium(mologram::PremiumState::NotPremium);
r.settle();
list->onActivated.emit(0);
r.settle();
CHECK(r.hasWindow("PremiumPanel"));
CHECK(r.composer().editor->getText().empty());
r.app.app().wm.closeWindow(r.window("PremiumPanel"));
r.svc.setPremium(mologram::PremiumState::Premium);
r.settle();
list->onActivated.emit(0);
r.settle();
CHECK_EQ(r.composer().editor->getText(), std::string("\xE2\xAD\x90"));
size_t before = r.svc.history({PeerKind::User, 100}).size();
r.composer().doSend();
r.settle();
CHECK_EQ(r.svc.history({PeerKind::User, 100}).size(), before + 1);
list->onActivated.emit(1);
r.settle();
r.svc.revokePremiumSilently();
r.composer().doSend();
r.settle();
CHECK_EQ(r.svc.history({PeerKind::User, 100}).size(), before + 1); // refused
CHECK(!r.app.premium().has());
CHECK(r.hasWindow("PremiumPanel"));
CHECK_EQ(r.composer().editor->getText(), std::string("\xE2\xAD\x90")); // nothing lost
}