imsd-messages/tests/Portal/main.cpp

60 lines
2.5 KiB
C++
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// lint-disable-file no-char-pointer
// The desktop portal's side of the bus layer: where a request's answer
// arrives, and the options the Background request carries.
#include <gio/gio.h>
import std;
import ImsdMessages.Bus;
using namespace ImsdMessages;
namespace {
std::int32_t failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
std::string LookupString(GVariant* dict, const char* key) {
const gchar* value = nullptr;
return g_variant_lookup(dict, key, "&s", &value) && value ? std::string(value) : std::string("<absent>");
}
std::optional<bool> LookupBool(GVariant* dict, const char* key) {
gboolean value = FALSE;
if (!g_variant_lookup(dict, key, "b", &value)) return std::nullopt;
return value != FALSE;
}
}
int main() {
Check(Bus::PortalRequestPath(":1.42", "imsd_messages_7") == "/org/freedesktop/portal/desktop/request/1_42/imsd_messages_7", "a unique name loses its ':' and its '.' becomes '_'");
Check(Bus::PortalRequestPath(":1.2345", "t") == "/org/freedesktop/portal/desktop/request/1_2345/t", "every digit of the connection number stays");
const std::vector<std::string> commandline = { "imsd-messages", "--daemon" };
GVariant* options = g_variant_ref_sink(Bus::BackgroundOptions("tok", "to receive texts", commandline));
Check(g_variant_is_of_type(options, G_VARIANT_TYPE("a{sv}")), "the options are an a{sv}");
Check(LookupString(options, "handle_token") == "tok", "handle_token is the token");
Check(LookupString(options, "reason") == "to receive texts", "reason is passed through");
Check(LookupBool(options, "autostart") == true, "autostart is asked for");
Check(LookupBool(options, "dbus-activatable") == false, "the autostart entry runs the command line, not D-Bus activation");
GVariant* cmd = g_variant_lookup_value(options, "commandline", G_VARIANT_TYPE("as"));
std::vector<std::string> got;
if (cmd) {
gsize n = 0;
const gchar** strv = g_variant_get_strv(cmd, &n);
for (gsize i = 0; i < n; ++i) got.emplace_back(strv[i]);
g_free(static_cast<gpointer>(strv));
g_variant_unref(cmd);
}
Check(got == commandline, "commandline is the daemon's argv, in order");
g_variant_unref(options);
if (failures) std::println("{} check(s) failed", failures);
return failures ? 1 : 0;
}