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
1=== modified file 'tests/indicator-fixture.h'
2--- tests/indicator-fixture.h 2015-01-30 02:57:16 +0000
3+++ tests/indicator-fixture.h 2015-02-09 18:07:05 +0000
4@@ -20,6 +20,8 @@
5 #include <memory>
6 #include <algorithm>
7 #include <string>
8+#include <functional>
9+#include <future>
10
11 #include <gtest/gtest.h>
12 #include <gio/gio.h>
13@@ -31,7 +33,10 @@
14 std::string _indicatorPath;
15 std::string _indicatorAddress;
16 std::vector<std::shared_ptr<DbusTestTask>> _mocks;
17+ protected:
18+ std::chrono::milliseconds _eventuallyTime;
19
20+ private:
21 class PerRunData {
22 public:
23 /* We're private in the fixture but other than that we don't care,
24@@ -116,6 +121,7 @@
25 const std::string& addr)
26 : _indicatorPath(path)
27 , _indicatorAddress(addr)
28+ , _eventuallyTime(std::chrono::seconds(5))
29 {
30 };
31
32@@ -195,6 +201,38 @@
33 waitForCore(G_OBJECT(group.get()), "action-added");
34 }
35
36+ testing::AssertionResult expectEventually (std::function<testing::AssertionResult(void)> &testfunc) {
37+ auto loop = std::shared_ptr<GMainLoop>(g_main_loop_new(nullptr, FALSE), [](GMainLoop * loop) { if (loop != nullptr) g_main_loop_unref(loop); });
38+
39+ std::promise<testing::AssertionResult> retpromise;
40+ auto retfuture = retpromise.get_future();
41+ auto start = std::chrono::steady_clock::now();
42+
43+ /* The core of the idle function as an object so we can use the C++-isms
44+ of attaching the variables and make this code reasonably readable */
45+ std::function<void(void)> idlefunc = [&loop, &retpromise, &testfunc, &start, this]() -> void {
46+ auto result = testfunc();
47+
48+ if (result == false && _eventuallyTime > (std::chrono::steady_clock::now() - start)) {
49+ return;
50+ }
51+
52+ retpromise.set_value(result);
53+ g_main_loop_quit(loop.get());
54+ };
55+
56+ auto idlesrc = g_idle_add([](gpointer data) -> gboolean {
57+ auto func = reinterpret_cast<std::function<void(void)> *>(data);
58+ (*func)();
59+ return G_SOURCE_CONTINUE;
60+ }, &idlefunc);
61+
62+ g_main_loop_run(loop.get());
63+ g_source_remove(idlesrc);
64+
65+ return retfuture.get();
66+ }
67+
68 protected:
69 void setMenu (const std::string& path) {
70 run->_menu.reset();
71@@ -234,6 +272,13 @@
72 return result;
73 }
74
75+ template <typename... Args> testing::AssertionResult expectEventuallyActionStateExists (Args&& ... args) {
76+ std::function<testing::AssertionResult(void)> func = [&]() {
77+ return expectActionStateExists(std::forward<Args>(args)...);
78+ };
79+ return expectEventually(func);
80+ }
81+
82 testing::AssertionResult expectActionStateType (const char * nameStr, const char * typeStr, const std::string& name, const GVariantType * type) {
83 auto atype = g_action_group_get_action_state_type(run->_actions.get(), name.c_str());
84 bool same = false;
85@@ -256,6 +301,13 @@
86 return result;
87 }
88
89+ template <typename... Args> testing::AssertionResult expectEventuallyActionStateType (Args&& ... args) {
90+ std::function<testing::AssertionResult(void)> func = [&]() {
91+ return expectActionStateType(std::forward<Args>(args)...);
92+ };
93+ return expectEventually(func);
94+ }
95+
96 testing::AssertionResult expectActionStateIs (const char * nameStr, const char * valueStr, const std::string& name, GVariant * value) {
97 auto varref = std::shared_ptr<GVariant>(g_variant_ref_sink(value), [](GVariant * varptr) {
98 if (varptr != nullptr)
99@@ -320,6 +372,13 @@
100 return expectActionStateIs(nameStr, valueStr, name, var);
101 }
102
103+ template <typename... Args> testing::AssertionResult expectEventuallyActionStateIs (Args&& ... args) {
104+ std::function<testing::AssertionResult(void)> func = [&]() {
105+ return expectActionStateIs(std::forward<Args>(args)...);
106+ };
107+ return expectEventually(func);
108+ }
109+
110
111 private:
112 std::shared_ptr<GVariant> getMenuAttributeVal (int location, std::shared_ptr<GMenuModel>& menu, const std::string& attribute, std::shared_ptr<GVariant>& value) {
113@@ -363,7 +422,7 @@
114 }
115
116 protected:
117- testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, GVariant * value) {
118+ testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, GVariant * value) {
119 auto varref = std::shared_ptr<GVariant>(g_variant_ref_sink(value), [](GVariant * varptr) {
120 if (varptr != nullptr)
121 g_variant_unref(varptr);
122@@ -401,44 +460,65 @@
123 }
124 }
125
126- testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, bool value) {
127+ testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, bool value) {
128 GVariant * var = g_variant_new_boolean(value);
129 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);
130 }
131
132- testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, std::string value) {
133+ testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, std::string value) {
134 GVariant * var = g_variant_new_string(value.c_str());
135 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);
136 }
137
138- testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const gchar * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, const char * value) {
139+ testing::AssertionResult expectMenuAttribute (const char * menuLocationStr, const char * attributeStr, const char * valueStr, const std::vector<int> menuLocation, const std::string& attribute, const char * value) {
140 GVariant * var = g_variant_new_string(value);
141 return expectMenuAttribute(menuLocationStr, attributeStr, valueStr, menuLocation, attribute, var);
142 }
143
144+ template <typename... Args> testing::AssertionResult expectEventuallyMenuAttribute (Args&& ... args) {
145+ std::function<testing::AssertionResult(void)> func = [&]() {
146+ return expectMenuAttribute(std::forward<Args>(args)...);
147+ };
148+ return expectEventually(func);
149+ }
150 };
151
152+/* Menu Attrib */
153+#define ASSERT_MENU_ATTRIB(menu, attrib, value) \
154+ ASSERT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value)
155+
156 #define EXPECT_MENU_ATTRIB(menu, attrib, value) \
157 EXPECT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value)
158
159-#define ASSERT_MENU_ATTRIB(menu, attrib, value) \
160- ASSERT_PRED_FORMAT3(IndicatorFixture::expectMenuAttribute, menu, attrib, value)
161+#define EXPECT_EVENTUALLY_MENU_ATTRIB(menu, attrib, value) \
162+ EXPECT_PRED_FORMAT3(IndicatorFixture::expectEventuallyMenuAttribute, menu, attrib, value)
163
164+/* Action Exists */
165 #define ASSERT_ACTION_EXISTS(action) \
166 ASSERT_PRED_FORMAT1(IndicatorFixture::expectActionExists, action)
167
168 #define EXPECT_ACTION_EXISTS(action) \
169 EXPECT_PRED_FORMAT1(IndicatorFixture::expectActionExists, action)
170
171+#define EXPECT_EVENTUALLY_ACTION_EXISTS(action) \
172+ EXPECT_PRED_FORMAT1(IndicatorFixture::expectEventuallyActionExists, action)
173+
174+/* Action State */
175+#define ASSERT_ACTION_STATE(action, value) \
176+ ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value)
177+
178 #define EXPECT_ACTION_STATE(action, value) \
179 EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value)
180
181-#define ASSERT_ACTION_STATE(action, value) \
182- ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateIs, action, value)
183+#define EXPECT_EVENTUALLY_ACTION_STATE(action, value) \
184+ EXPECT_PRED_FORMAT2(IndicatorFixture::expectEventuallyActionStateIs, action, value)
185+
186+/* Action State Type */
187+#define ASSERT_ACTION_STATE_TYPE(action, type) \
188+ ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type)
189
190 #define EXPECT_ACTION_STATE_TYPE(action, type) \
191 EXPECT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type)
192
193-#define ASSERT_ACTION_STATE_TYPE(action, type) \
194- ASSERT_PRED_FORMAT2(IndicatorFixture::expectActionStateType, action, type)
195-
196+#define EXPECT_EVENTUALLY_ACTION_STATE_TYPE(action, type) \
197+ EXPECT_PRED_FORMAT2(IndicatorFixture::expectEventuallyActionStateType, action, type)
198
199=== modified file 'tests/indicator-test.cc'
200--- tests/indicator-test.cc 2015-02-04 17:43:08 +0000
201+++ tests/indicator-test.cc 2015-02-09 18:07:05 +0000
202@@ -61,7 +61,7 @@
203 TEST_F(IndicatorTest, PhoneMenu) {
204 setMenu("/com/canonical/indicator/sound/phone");
205
206- EXPECT_MENU_ATTRIB({0}, "action", "indicator.root");
207+ EXPECT_EVENTUALLY_MENU_ATTRIB(std::vector<int>({0}), "action", "indicator.root");
208 EXPECT_MENU_ATTRIB({0}, "x-canonical-type", "com.canonical.indicator.root");
209 EXPECT_MENU_ATTRIB({0}, "x-canonical-scroll-action", "indicator.scroll");
210 EXPECT_MENU_ATTRIB({0}, "x-canonical-secondary-action", "indicator.mute");

Subscribers

People subscribed via source and target branches