diff options
author | Ted Gould <ted@gould.cx> | 2015-02-09 13:45:21 -0600 |
---|---|---|
committer | Ted Gould <ted@gould.cx> | 2015-02-09 13:45:21 -0600 |
commit | 4d580e2c10e2df20480efc99c0c85367aae4d49e (patch) | |
tree | ad79c8db2452a5175445430ac97ee268e5c3edf4 /tests/notifications-mock.h | |
parent | 9ea7cad48eec56bafb47c7f05f1b04c79c7c3e78 (diff) | |
download | ayatana-indicator-sound-4d580e2c10e2df20480efc99c0c85367aae4d49e.tar.gz ayatana-indicator-sound-4d580e2c10e2df20480efc99c0c85367aae4d49e.tar.bz2 ayatana-indicator-sound-4d580e2c10e2df20480efc99c0c85367aae4d49e.zip |
Support getting the notifications back out
Diffstat (limited to 'tests/notifications-mock.h')
-rw-r--r-- | tests/notifications-mock.h | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/tests/notifications-mock.h b/tests/notifications-mock.h index 48da344..6abe9d8 100644 --- a/tests/notifications-mock.h +++ b/tests/notifications-mock.h @@ -16,12 +16,16 @@ * Authors: * Ted Gould <ted@canonical.com> */ +#include <map> +#include <memory> +#include <type_traits> #include <libdbustest/dbus-test.h> class NotificationsMock { DbusTestDbusMock * mock = nullptr; + DbusTestDbusMockObject * baseobj = nullptr; public: NotificationsMock (std::vector<std::string> capabilities = {"body", "body-markup", "icon-static", "image/svg+xml", "x-canonical-private-synchronous", "x-canonical-append", "x-canonical-private-icon-only", "x-canonical-truncation", "private-synchronous", "append", "private-icon-only", "truncation"}) { @@ -29,13 +33,17 @@ class NotificationsMock dbus_test_task_set_bus(DBUS_TEST_TASK(mock), DBUS_TEST_SERVICE_BUS_SESSION); dbus_test_task_set_name(DBUS_TEST_TASK(mock), "Notify"); - DbusTestDbusMockObject * baseobj =dbus_test_dbus_mock_get_object(mock, "/org/freedesktop/Notifications", "org.freedesktop.Notifications", NULL); + baseobj =dbus_test_dbus_mock_get_object(mock, "/org/freedesktop/Notifications", "org.freedesktop.Notifications", NULL); std::string capspython("ret = "); capspython += vector2py(capabilities); dbus_test_dbus_mock_object_add_method(mock, baseobj, "GetCapabilities", NULL, G_VARIANT_TYPE("as"), capspython.c_str(), NULL); + + dbus_test_dbus_mock_object_add_method(mock, baseobj, + "Notify", NULL, G_VARIANT_TYPE("(susssasa{sv}i)"), + "ret = 10", NULL); } ~NotificationsMock () { @@ -71,4 +79,63 @@ class NotificationsMock operator DbusTestDbusMock* () { return mock; } + + struct Notification { + std::string app_name; + unsigned int replace_id; + std::string app_icon; + std::string summary; + std::string body; + std::vector<std::string> actions; + std::map<std::string, std::shared_ptr<GVariant>> hints; + int timeout; + }; + + std::shared_ptr<GVariant> childGet (GVariant * tuple, gsize index) { + return std::shared_ptr<GVariant>(g_variant_get_child_value(tuple, index), + [](GVariant * v){ if (v != nullptr) g_variant_unref(v); }); + } + + std::vector<Notification> getNotifications (void) { + std::vector<Notification> notifications; + + unsigned int cnt, i; + auto calls = dbus_test_dbus_mock_object_get_method_calls(mock, baseobj, "Notify", &cnt, NULL); + + for (i = 0; i < cnt; i++) { + auto call = calls[i]; + Notification notification; + + notification.app_name = g_variant_get_string(childGet(call.params, 0).get(), nullptr); + notification.replace_id = g_variant_get_uint32(childGet(call.params, 1).get()); + notification.app_icon = g_variant_get_string(childGet(call.params, 2).get(), nullptr); + notification.summary = g_variant_get_string(childGet(call.params, 3).get(), nullptr); + notification.body = g_variant_get_string(childGet(call.params, 4).get(), nullptr); + notification.timeout = g_variant_get_int32(childGet(call.params, 7).get()); + + auto vactions = childGet(call.params, 5); + GVariantIter iactions = {0}; + g_variant_iter_init(&iactions, vactions.get()); + const gchar * action = NULL; + while (g_variant_iter_loop(&iactions, "&s", &action)) { + std::string saction(action); + notification.actions.push_back(saction); + } + + auto vhints = childGet(call.params, 6); + GVariantIter ihints = {0}; + g_variant_iter_init(&ihints, vhints.get()); + const gchar * hint_key = NULL; + GVariant * hint_value = NULL; + while (g_variant_iter_loop(&ihints, "{&sv}", &hint_key, &hint_value)) { + std::string key(hint_key); + std::shared_ptr<GVariant> value(g_variant_ref(hint_value), [](GVariant * v){ if (v != nullptr) g_variant_unref(v); }); + notification.hints[key] = value; + } + + notifications.push_back(notification); + } + + return notifications; + } }; |