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 +++++++++++++++++++++++++++++++++++++++++++---- tests/indicator-test.cc | 2 +- 2 files changed, 55 insertions(+), 5 deletions(-) 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) diff --git a/tests/indicator-test.cc b/tests/indicator-test.cc index 31494d4..07d5f91 100644 --- a/tests/indicator-test.cc +++ b/tests/indicator-test.cc @@ -58,7 +58,7 @@ protected: TEST_F(IndicatorTest, PhoneMenu) { setMenu("/com/canonical/indicator/sound/phone"); - EXPECT_MENU_ATTRIB({0}, "action", "indicator.root"); + EXPECT_EVENTUALLY_MENU_ATTRIB(std::vector({0}), "action", "indicator.root"); EXPECT_MENU_ATTRIB({0}, "x-canonical-type", "com.canonical.indicator.root"); EXPECT_MENU_ATTRIB({0}, "x-canonical-scroll-action", "indicator.scroll"); EXPECT_MENU_ATTRIB({0}, "x-canonical-secondary-action", "indicator.mute"); -- cgit v1.2.3 From 38b5f26e19facc5571723d122f4fa97eea8ba395 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 9 Feb 2015 11:25:52 -0600 Subject: Adding time checking in --- tests/indicator-fixture.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/indicator-fixture.h b/tests/indicator-fixture.h index 2948e6b..e16d254 100644 --- a/tests/indicator-fixture.h +++ b/tests/indicator-fixture.h @@ -33,6 +33,8 @@ class IndicatorFixture : public ::testing::Test std::string _indicatorPath; std::string _indicatorAddress; std::vector> _mocks; + protected: + std::chrono::milliseconds _eventuallyTime; class PerRunData { public: @@ -118,6 +120,7 @@ class IndicatorFixture : public ::testing::Test const std::string& addr) : _indicatorPath(path) , _indicatorAddress(addr) + , _eventuallyTime(std::chrono::seconds(5)) { }; @@ -202,14 +205,15 @@ class IndicatorFixture : public ::testing::Test std::promise retpromise; auto retfuture = retpromise.get_future(); + auto start = std::chrono::steady_clock::now(); /* 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 idlefunc = [&loop, &retpromise, &testfunc, &start, this]() -> void { auto result = testfunc(); + g_debug("Idle Ran"); - if (result == false) { - /* TODO: Check time */ + if (result == false && _eventuallyTime > (std::chrono::steady_clock::now() - start)) { return; } -- cgit v1.2.3 From 52dafd6b57679d35d7bdedda447da37ebdf4493a Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 9 Feb 2015 11:49:28 -0600 Subject: Fixes the timeout --- tests/indicator-fixture.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/indicator-fixture.h b/tests/indicator-fixture.h index e16d254..c269be5 100644 --- a/tests/indicator-fixture.h +++ b/tests/indicator-fixture.h @@ -36,6 +36,7 @@ class IndicatorFixture : public ::testing::Test protected: std::chrono::milliseconds _eventuallyTime; + private: class PerRunData { public: /* We're private in the fixture but other than that we don't care, @@ -209,9 +210,8 @@ class IndicatorFixture : public ::testing::Test /* 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, &start, this]() -> void { + std::function idlefunc = [&loop, &retpromise, &testfunc, &start, this]() -> void { auto result = testfunc(); - g_debug("Idle Ran"); if (result == false && _eventuallyTime > (std::chrono::steady_clock::now() - start)) { return; @@ -221,12 +221,6 @@ class IndicatorFixture : public ::testing::Test 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)(); -- cgit v1.2.3 From fe57009ba87b4cd9403cd219c26cc9487ac5d276 Mon Sep 17 00:00:00 2001 From: Ted Gould Date: Mon, 9 Feb 2015 12:03:29 -0600 Subject: Bringing the eventually to all the test types --- tests/indicator-fixture.h | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/tests/indicator-fixture.h b/tests/indicator-fixture.h index c269be5..e725a50 100644 --- a/tests/indicator-fixture.h +++ b/tests/indicator-fixture.h @@ -272,6 +272,13 @@ class IndicatorFixture : public ::testing::Test return result; } + template testing::AssertionResult expectEventuallyActionStateExists (Args&& ... args) { + std::function func = [&]() { + return expectActionStateExists(std::forward(args)...); + }; + return expectEventually(func); + } + testing::AssertionResult expectActionStateType (const char * nameStr, const char * typeStr, const std::string& name, const GVariantType * type) { auto atype = g_action_group_get_action_state_type(run->_actions.get(), name.c_str()); bool same = false; @@ -294,6 +301,13 @@ class IndicatorFixture : public ::testing::Test return result; } + template testing::AssertionResult expectEventuallyActionStateType (Args&& ... args) { + std::function func = [&]() { + return expectActionStateType(std::forward(args)...); + }; + return expectEventually(func); + } + testing::AssertionResult expectActionStateIs (const char * nameStr, const char * valueStr, const std::string& name, GVariant * value) { auto varref = std::shared_ptr(g_variant_ref_sink(value), [](GVariant * varptr) { if (varptr != nullptr) @@ -358,6 +372,13 @@ class IndicatorFixture : public ::testing::Test return expectActionStateIs(nameStr, valueStr, name, var); } + template testing::AssertionResult expectEventuallyActionStateIs (Args&& ... args) { + std::function func = [&]() { + return expectActionStateIs(std::forward(args)...); + }; + return expectEventually(func); + } + private: std::shared_ptr getMenuAttributeVal (int location, std::shared_ptr& menu, const std::string& attribute, std::shared_ptr& value) { @@ -462,6 +483,9 @@ class IndicatorFixture : public ::testing::Test } }; +/* Menu Attrib */ +#define ASSERT_MENU_ATTRIB(menu, attrib, value) \ + ASSERT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value) #define EXPECT_MENU_ATTRIB(menu, attrib, value) \ EXPECT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value) @@ -469,24 +493,32 @@ class IndicatorFixture : public ::testing::Test #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) - +/* Action Exists */ #define ASSERT_ACTION_EXISTS(action) \ ASSERT_PRED_FORMAT1(IndicatorFixture::expectActionExists, action) #define EXPECT_ACTION_EXISTS(action) \ EXPECT_PRED_FORMAT1(IndicatorFixture::expectActionExists, action) -#define EXPECT_ACTION_STATE(action, value) \ - EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value) +#define EXPECT_EVENTUALLY_ACTION_EXISTS(action) \ + EXPECT_PRED_FORMAT1(IndicatorFixture::expectEventuallyActionExists, action) +/* Action State */ #define ASSERT_ACTION_STATE(action, value) \ ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value) -#define EXPECT_ACTION_STATE_TYPE(action, type) \ - EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type) +#define EXPECT_ACTION_STATE(action, value) \ + EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value) + +#define EXPECT_EVENTUALLY_ACTION_STATE(action, value) \ + EXPECT_PRED_FORMAT2(IndicatorFixture::expectEventuallyActionStateIs, action, value) +/* Action State Type */ #define ASSERT_ACTION_STATE_TYPE(action, type) \ ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type) +#define EXPECT_ACTION_STATE_TYPE(action, type) \ + EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type) + +#define EXPECT_EVENTUALLY_ACTION_STATE_TYPE(action, type) \ + EXPECT_PRED_FORMAT2(IndicatorFixture::expectEventuallyActionStateType, action, type) -- cgit v1.2.3