Merge lp:~ted/indicator-sound/expect-eventually into lp:indicator-sound/15.04

Proposed by Ted Gould
Status: Merged
Approved by: Ted Gould
Approved revision: 509
Merged at revision: 477
Proposed branch: lp:~ted/indicator-sound/expect-eventually
Merge into: lp:indicator-sound/15.04
Diff against target: 210 lines (+92/-12)
2 files modified
tests/indicator-fixture.h (+91/-11)
tests/indicator-test.cc (+1/-1)
To merge this branch: bzr merge lp:~ted/indicator-sound/expect-eventually
Reviewer Review Type Date Requested Status
Jussi Pakkanen (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+249099@code.launchpad.net

Commit message

Add eventually functions to indicator fixture

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Jussi Pakkanen (jpakkane) wrote :

This warps your brain but in a good way.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'tests/indicator-fixture.h'
--- tests/indicator-fixture.h 2015-01-30 02:57:16 +0000
+++ tests/indicator-fixture.h 2015-02-09 18:07:05 +0000
@@ -20,6 +20,8 @@
20#include <memory>20#include <memory>
21#include <algorithm>21#include <algorithm>
22#include <string>22#include <string>
23#include <functional>
24#include <future>
2325
24#include <gtest/gtest.h>26#include <gtest/gtest.h>
25#include <gio/gio.h>27#include <gio/gio.h>
@@ -31,7 +33,10 @@
31 std::string _indicatorPath;33 std::string _indicatorPath;
32 std::string _indicatorAddress;34 std::string _indicatorAddress;
33 std::vector<std::shared_ptr<DbusTestTask>> _mocks;35 std::vector<std::shared_ptr<DbusTestTask>> _mocks;
36 protected:
37 std::chrono::milliseconds _eventuallyTime;
3438
39 private:
35 class PerRunData {40 class PerRunData {
36 public:41 public:
37 /* We're private in the fixture but other than that we don't care,42 /* We're private in the fixture but other than that we don't care,
@@ -116,6 +121,7 @@
116 const std::string& addr)121 const std::string& addr)
117 : _indicatorPath(path)122 : _indicatorPath(path)
118 , _indicatorAddress(addr)123 , _indicatorAddress(addr)
124 , _eventuallyTime(std::chrono::seconds(5))
119 {125 {
120 };126 };
121127
@@ -195,6 +201,38 @@
195 waitForCore(G_OBJECT(group.get()), "action-added");201 waitForCore(G_OBJECT(group.get()), "action-added");
196 }202 }
197203
204 testing::AssertionResult expectEventually (std::function<testing::AssertionResult(void)> &testfunc) {
205 auto loop = std::shared_ptr<GMainLoop>(g_main_loop_new(nullptr, FALSE), [](GMainLoop * loop) { if (loop != nullptr) g_main_loop_unref(loop); });
206
207 std::promise<testing::AssertionResult> retpromise;
208 auto retfuture = retpromise.get_future();
209 auto start = std::chrono::steady_clock::now();
210
211 /* The core of the idle function as an object so we can use the C++-isms
212 of attaching the variables and make this code reasonably readable */
213 std::function<void(void)> idlefunc = [&loop, &retpromise, &testfunc, &start, this]() -> void {
214 auto result = testfunc();
215
216 if (result == false && _eventuallyTime > (std::chrono::steady_clock::now() - start)) {
217 return;
218 }
219
220 retpromise.set_value(result);
221 g_main_loop_quit(loop.get());
222 };
223
224 auto idlesrc = g_idle_add([](gpointer data) -> gboolean {
225 auto func = reinterpret_cast<std::function<void(void)> *>(data);
226 (*func)();
227 return G_SOURCE_CONTINUE;
228 }, &idlefunc);
229
230 g_main_loop_run(loop.get());
231 g_source_remove(idlesrc);
232
233 return retfuture.get();
234 }
235
198 protected:236 protected:
199 void setMenu (const std::string& path) {237 void setMenu (const std::string& path) {
200 run->_menu.reset();238 run->_menu.reset();
@@ -234,6 +272,13 @@
234 return result;272 return result;
235 }273 }
236274
275 template <typename... Args> testing::AssertionResult expectEventuallyActionStateExists (Args&& ... args) {
276 std::function<testing::AssertionResult(void)> func = [&]() {
277 return expectActionStateExists(std::forward<Args>(args)...);
278 };
279 return expectEventually(func);
280 }
281
237 testing::AssertionResult expectActionStateType (const char * nameStr, const char * typeStr, const std::string& name, const GVariantType * type) {282 testing::AssertionResult expectActionStateType (const char * nameStr, const char * typeStr, const std::string& name, const GVariantType * type) {
238 auto atype = g_action_group_get_action_state_type(run->_actions.get(), name.c_str());283 auto atype = g_action_group_get_action_state_type(run->_actions.get(), name.c_str());
239 bool same = false;284 bool same = false;
@@ -256,6 +301,13 @@
256 return result;301 return result;
257 }302 }
258303
304 template <typename... Args> testing::AssertionResult expectEventuallyActionStateType (Args&& ... args) {
305 std::function<testing::AssertionResult(void)> func = [&]() {
306 return expectActionStateType(std::forward<Args>(args)...);
307 };
308 return expectEventually(func);
309 }
310
259 testing::AssertionResult expectActionStateIs (const char * nameStr, const char * valueStr, const std::string& name, GVariant * value) {311 testing::AssertionResult expectActionStateIs (const char * nameStr, const char * valueStr, const std::string& name, GVariant * value) {
260 auto varref = std::shared_ptr<GVariant>(g_variant_ref_sink(value), [](GVariant * varptr) {312 auto varref = std::shared_ptr<GVariant>(g_variant_ref_sink(value), [](GVariant * varptr) {
261 if (varptr != nullptr)313 if (varptr != nullptr)
@@ -320,6 +372,13 @@
320 return expectActionStateIs(nameStr, valueStr, name, var);372 return expectActionStateIs(nameStr, valueStr, name, var);
321 }373 }
322374
375 template <typename... Args> testing::AssertionResult expectEventuallyActionStateIs (Args&& ... args) {
376 std::function<testing::AssertionResult(void)> func = [&]() {
377 return expectActionStateIs(std::forward<Args>(args)...);
378 };
379 return expectEventually(func);
380 }
381
323382
324 private:383 private:
325 std::shared_ptr<GVariant> getMenuAttributeVal (int location, std::shared_ptr<GMenuModel>& menu, const std::string& attribute, std::shared_ptr<GVariant>& value) {384 std::shared_ptr<GVariant> getMenuAttributeVal (int location, std::shared_ptr<GMenuModel>& menu, const std::string& attribute, std::shared_ptr<GVariant>& value) {
@@ -363,7 +422,7 @@
363 }422 }
364423
365 protected:424 protected:
366 testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, GVariant * value) {425 testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, GVariant * value) {
367 auto varref = std::shared_ptr<GVariant>(g_variant_ref_sink(value), [](GVariant * varptr) {426 auto varref = std::shared_ptr<GVariant>(g_variant_ref_sink(value), [](GVariant * varptr) {
368 if (varptr != nullptr)427 if (varptr != nullptr)
369 g_variant_unref(varptr);428 g_variant_unref(varptr);
@@ -401,44 +460,65 @@
401 }460 }
402 }461 }
403462
404 testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, bool value) {463 testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, bool value) {
405 GVariant * var = g_variant_new_boolean(value);464 GVariant * var = g_variant_new_boolean(value);
406 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);465 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);
407 }466 }
408467
409 testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, std::string value) {468 testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, std::string value) {
410 GVariant * var = g_variant_new_string(value.c_str());469 GVariant * var = g_variant_new_string(value.c_str());
411 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);470 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);
412 }471 }
413472
414 testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, const char * value) {473 testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, const char * value) {
415 GVariant * var = g_variant_new_string(value);474 GVariant * var = g_variant_new_string(value);
416 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);475 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);
417 }476 }
418477
478 template <typename... Args> testing::AssertionResult expectEventuallyMenuAttribute (Args&& ... args) {
479 std::function<testing::AssertionResult(void)> func = [&]() {
480 return expectMenuAttribute(std::forward<Args>(args)...);
481 };
482 return expectEventually(func);
483 }
419};484};
420485
486/* Menu Attrib */
487#define ASSERT_MENU_ATTRIB(menu, attrib, value) \
488 ASSERT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value)
489
421#define EXPECT_MENU_ATTRIB(menu, attrib, value) \490#define EXPECT_MENU_ATTRIB(menu, attrib, value) \
422 EXPECT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value)491 EXPECT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value)
423492
424#define ASSERT_MENU_ATTRIB(menu, attrib, value) \493#define EXPECT_EVENTUALLY_MENU_ATTRIB(menu, attrib, value) \
425 ASSERT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value)494 EXPECT_PRED_FORMAT3(IndicatorFixture::expectEventuallyMenuAttribute, menu, attrib, value)
426495
496/* Action Exists */
427#define ASSERT_ACTION_EXISTS(action) \497#define ASSERT_ACTION_EXISTS(action) \
428 ASSERT_PRED_FORMAT1(IndicatorFixture::expectActionExists, action)498 ASSERT_PRED_FORMAT1(IndicatorFixture::expectActionExists, action)
429499
430#define EXPECT_ACTION_EXISTS(action) \500#define EXPECT_ACTION_EXISTS(action) \
431 EXPECT_PRED_FORMAT1(IndicatorFixture::expectActionExists, action)501 EXPECT_PRED_FORMAT1(IndicatorFixture::expectActionExists, action)
432502
503#define EXPECT_EVENTUALLY_ACTION_EXISTS(action) \
504 EXPECT_PRED_FORMAT1(IndicatorFixture::expectEventuallyActionExists, action)
505
506/* Action State */
507#define ASSERT_ACTION_STATE(action, value) \
508 ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value)
509
433#define EXPECT_ACTION_STATE(action, value) \510#define EXPECT_ACTION_STATE(action, value) \
434 EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value)511 EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value)
435512
436#define ASSERT_ACTION_STATE(action, value) \513#define EXPECT_EVENTUALLY_ACTION_STATE(action, value) \
437 ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value)514 EXPECT_PRED_FORMAT2(IndicatorFixture::expectEventuallyActionStateIs, action, value)
515
516/* Action State Type */
517#define ASSERT_ACTION_STATE_TYPE(action, type) \
518 ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type)
438519
439#define EXPECT_ACTION_STATE_TYPE(action, type) \520#define EXPECT_ACTION_STATE_TYPE(action, type) \
440 EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type)521 EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type)
441522
442#define ASSERT_ACTION_STATE_TYPE(action, type) \523#define EXPECT_EVENTUALLY_ACTION_STATE_TYPE(action, type) \
443 ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type)524 EXPECT_PRED_FORMAT2(IndicatorFixture::expectEventuallyActionStateType, action, type)
444
445525
=== modified file 'tests/indicator-test.cc'
--- tests/indicator-test.cc 2015-02-04 17:43:08 +0000
+++ tests/indicator-test.cc 2015-02-09 18:07:05 +0000
@@ -61,7 +61,7 @@
61TEST_F(IndicatorTest, PhoneMenu) {61TEST_F(IndicatorTest, PhoneMenu) {
62 setMenu("/com/canonical/indicator/sound/phone");62 setMenu("/com/canonical/indicator/sound/phone");
6363
64 EXPECT_MENU_ATTRIB({0}, "action", "indicator.root");64 EXPECT_EVENTUALLY_MENU_ATTRIB(std::vector<int>({0}), "action", "indicator.root");
65 EXPECT_MENU_ATTRIB({0}, "x-canonical-type", "com.canonical.indicator.root");65 EXPECT_MENU_ATTRIB({0}, "x-canonical-type", "com.canonical.indicator.root");
66 EXPECT_MENU_ATTRIB({0}, "x-canonical-scroll-action", "indicator.scroll");66 EXPECT_MENU_ATTRIB({0}, "x-canonical-scroll-action", "indicator.scroll");
67 EXPECT_MENU_ATTRIB({0}, "x-canonical-secondary-action", "indicator.mute");67 EXPECT_MENU_ATTRIB({0}, "x-canonical-secondary-action", "indicator.mute");

Subscribers

People subscribed via source and target branches