aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2016-02-10 14:47:39 -0600
committerRobert Tari <robert@tari.in>2021-07-01 02:20:45 +0200
commitd80dca778d22ab21b5832f2d90cb3f08123eed58 (patch)
tree7849afc5cd73926a94ae150bd2f0373c1063f16f /tests
parentb6f3a8d8e4db3f786ec35322dff0724cfc74a2f4 (diff)
downloadayatana-indicator-datetime-d80dca778d22ab21b5832f2d90cb3f08123eed58.tar.gz
ayatana-indicator-datetime-d80dca778d22ab21b5832f2d90cb3f08123eed58.tar.bz2
ayatana-indicator-datetime-d80dca778d22ab21b5832f2d90cb3f08123eed58.zip
in the GLib test Fixture, add helper functions to wait for arbitrary events and bus events
Diffstat (limited to 'tests')
-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;
};