aboutsummaryrefslogtreecommitdiff
path: root/tests/glib-fixture.h
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2016-02-10 14:47:39 -0600
committerMike Gabriel <mike.gabriel@das-netzwerkteam.de>2021-08-29 14:35:04 +0200
commitf81f91eaa0889aac1b3b9b8c9eb95691c1c7a5df (patch)
treed56ed94731970eafad4957a52ebfffaa93eba263 /tests/glib-fixture.h
parent295b91b74cfb0226b01c6f47467a3af1a626cd6a (diff)
downloadayatana-indicator-datetime-f81f91eaa0889aac1b3b9b8c9eb95691c1c7a5df.tar.gz
ayatana-indicator-datetime-f81f91eaa0889aac1b3b9b8c9eb95691c1c7a5df.tar.bz2
ayatana-indicator-datetime-f81f91eaa0889aac1b3b9b8c9eb95691c1c7a5df.zip
in the GLib test Fixture, add helper functions to wait for arbitrary events and bus events
Diffstat (limited to 'tests/glib-fixture.h')
-rw-r--r--tests/glib-fixture.h88
1 files changed, 88 insertions, 0 deletions
diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h
index 4d309e6..efc8d17 100644
--- a/tests/glib-fixture.h
+++ b/tests/glib-fixture.h
@@ -20,7 +20,9 @@
#ifndef INDICATOR_DATETIME_TESTS_GLIB_FIXTURE_H
#define INDICATOR_DATETIME_TESTS_GLIB_FIXTURE_H
+#include <functional> // std::function
#include <map>
+#include <memory> // std::shared_ptr
#include <glib.h>
#include <glib/gstdio.h>
@@ -109,6 +111,92 @@ class GlibFixture : public ::testing::Test
g_source_remove(id);
}
+ bool wait_for(std::function<bool()> test_function, guint timeout_msec=1000)
+ {
+ auto timer = std::shared_ptr<GTimer>(g_timer_new(), [](GTimer* t){g_timer_destroy(t);});
+ const auto timeout_sec = timeout_msec / 1000.0;
+ for (;;) {
+ if (test_function())
+ return true;
+ //g_message("%f ... %f", g_timer_elapsed(timer.get(), nullptr), timeout_sec);
+ if (g_timer_elapsed(timer.get(), nullptr) >= timeout_sec)
+ return false;
+ wait_msec();
+ }
+ }
+
+ bool wait_for_name_owned(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ struct Data {
+ GMainLoop* loop = nullptr;
+ bool owned = false;
+ };
+ Data data;
+
+ auto on_name_appeared = [](GDBusConnection* /*connection*/,
+ const gchar* /*name_*/,
+ const gchar* name_owner,
+ gpointer gdata)
+ {
+ if (name_owner == nullptr)
+ return;
+ auto tmp = static_cast<Data*>(gdata);
+ tmp->owned = true;
+ g_main_loop_quit(tmp->loop);
+ };
+
+ const auto timeout_id = g_timeout_add(timeout_msec, wait_msec__timeout, loop);
+ data.loop = loop;
+ const auto watch_id = g_bus_watch_name_on_connection(connection,
+ name,
+ flags,
+ on_name_appeared,
+ nullptr, /* name_vanished */
+ &data,
+ nullptr); /* user_data_free_func */
+ g_main_loop_run(loop);
+
+ g_bus_unwatch_name(watch_id);
+ g_source_remove(timeout_id);
+
+ return data.owned;
+ }
+
+ void EXPECT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ EXPECT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void EXPECT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ EXPECT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void ASSERT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ ASSERT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void ASSERT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ ASSERT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
GMainLoop * loop;
};