From 00d4c04919b70f53fd6c6242e59aee631799dc4f Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 9 Feb 2015 10:54:12 -0600 Subject: Basis of expecting eventually a set of test functions --- tests/indicator-fixture.h | 58 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) (limited to 'tests/indicator-fixture.h') diff --git a/tests/indicator-fixture.h b/tests/indicator-fixture.h index a26f61d..2948e6b 100644 --- a/tests/indicator-fixture.h +++ b/tests/indicator-fixture.h @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include @@ -195,6 +197,44 @@ class IndicatorFixture : public ::testing::Test waitForCore(G_OBJECT(group.get()), "action-added"); } + testing::AssertionResult expectEventually (std::function &testfunc) { + auto loop = std::shared_ptr(g_main_loop_new(nullptr, FALSE), [](GMainLoop * loop) { if (loop != nullptr) g_main_loop_unref(loop); }); + + std::promise retpromise; + auto retfuture = retpromise.get_future(); + + /* The core of the idle function as an object so we can use the C++-isms + of attaching the variables and make this code reasonably readable */ + auto idlefunc = [&loop, &retpromise, &testfunc]() -> void { + auto result = testfunc(); + + if (result == false) { + /* TODO: Check time */ + return; + } + + retpromise.set_value(result); + g_main_loop_quit(loop.get()); + }; + + /* Run once to see if we can avoid waiting */ + idlefunc(); + if (retfuture.valid()) { + return retfuture.get(); + } + + auto idlesrc = g_idle_add([](gpointer data) -> gboolean { + auto func = reinterpret_cast *>(data); + (*func)(); + return G_SOURCE_CONTINUE; + }, &idlefunc); + + g_main_loop_run(loop.get()); + g_source_remove(idlesrc); + + return retfuture.get(); + } + protected: void setMenu (const std::string& path) { run->_menu.reset(); @@ -363,7 +403,7 @@ class IndicatorFixture : public ::testing::Test } protected: - testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector menuLocation, const std::string& attribute, GVariant * value) { + testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector menuLocation, const std::string& attribute, GVariant * value) { auto varref = std::shared_ptr(g_variant_ref_sink(value), [](GVariant * varptr) { if (varptr != nullptr) g_variant_unref(varptr); @@ -401,26 +441,36 @@ class IndicatorFixture : public ::testing::Test } } - testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector menuLocation, const std::string& attribute, bool value) { + testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector menuLocation, const std::string& attribute, bool value) { GVariant * var = g_variant_new_boolean(value); return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var); } - testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector menuLocation, const std::string& attribute, std::string value) { + testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector menuLocation, const std::string& attribute, std::string value) { GVariant * var = g_variant_new_string(value.c_str()); return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var); } - testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector menuLocation, const std::string& attribute, const char * value) { + testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector menuLocation, const std::string& attribute, const char * value) { GVariant * var = g_variant_new_string(value); return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var); } + template testing::AssertionResult expectEventuallyMenuAttribute (Args&& ... args) { + std::function func = [&]() { + return expectMenuAttribute(std::forward(args)...); + }; + return expectEventually(func); + } }; + #define EXPECT_MENU_ATTRIB(menu, attrib, value) \ EXPECT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value) +#define EXPECT_EVENTUALLY_MENU_ATTRIB(menu, attrib, value) \ + EXPECT_PRED_FORMAT3(IndicatorFixture::expectEventuallyMenuAttribute, menu, attrib, value) + #define ASSERT_MENU_ATTRIB(menu, attrib, value) \ ASSERT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value) -- cgit v1.2.3