Merge lp:~nick-dedekind/qmenumodel/qdbusactiongroup-variant-conversion into lp:qmenumodel

Proposed by Nick Dedekind on 2014-03-20
Status: Needs review
Proposed branch: lp:~nick-dedekind/qmenumodel/qdbusactiongroup-variant-conversion
Merge into: lp:qmenumodel
Diff against target: 298 lines (+167/-13)
7 files modified
libqmenumodel/src/qdbusactiongroup.cpp (+29/-2)
libqmenumodel/src/qstateaction.cpp (+2/-4)
tests/client/actiongrouptest.cpp (+96/-0)
tests/client/script_actiongrouptest.py (+2/-2)
tests/script/dbusmenuscript.cpp (+16/-0)
tests/script/dbusmenuscript.h (+1/-0)
tests/script/menuscript.py (+21/-5)
To merge this branch: bzr merge lp:~nick-dedekind/qmenumodel/qdbusactiongroup-variant-conversion
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve on 2014-03-20
Lars Karlitski 2014-03-20 Pending
Review via email: mp+211908@code.launchpad.net

Commit message

Action updateState/activate uses parameter types to determine correct variant conversion.

Description of the change

Action updateState/activate uses parameter types to determine correct variant conversion.
Added tests for implicit type conversion.

To post a comment you must log in.
104. By Nick Dedekind on 2014-03-20

Action updateState/activate uses parameter types to determine corect variant conversion

Unmerged revisions

104. By Nick Dedekind on 2014-03-20

Action updateState/activate uses parameter types to determine corect variant conversion

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libqmenumodel/src/qdbusactiongroup.cpp'
2--- libqmenumodel/src/qdbusactiongroup.cpp 2013-08-09 08:01:43 +0000
3+++ libqmenumodel/src/qdbusactiongroup.cpp 2014-03-20 11:46:21 +0000
4@@ -213,14 +213,41 @@
5 void QDBusActionGroup::updateActionState(const QString &name, const QVariant &state)
6 {
7 if (m_actionGroup != NULL) {
8- g_action_group_change_action_state(m_actionGroup, name.toUtf8().data(), Converter::toGVariant(state));
9+ GVariant* action_state;
10+ QByteArray nameBytes = name.toUtf8();
11+
12+ action_state = g_action_group_get_action_state(m_actionGroup, nameBytes.data());
13+ if (action_state) {
14+ g_action_group_change_action_state(m_actionGroup,
15+ nameBytes.data(),
16+ Converter::toGVariantWithSchema(state, g_variant_get_type_string(action_state)));
17+
18+ g_variant_unref(action_state);
19+ } else {
20+ g_action_group_change_action_state(m_actionGroup, nameBytes.data(), Converter::toGVariant(state));
21+ }
22 }
23 }
24
25 void QDBusActionGroup::activateAction(const QString &name, const QVariant &parameter)
26 {
27 if (m_actionGroup != NULL) {
28- g_action_group_activate_action(m_actionGroup, name.toUtf8().data(), Converter::toGVariant(parameter));
29+ const GVariantType* parameter_type;
30+ QByteArray nameBytes = name.toUtf8();
31+
32+ parameter_type = g_action_group_get_action_parameter_type(m_actionGroup, nameBytes.data());
33+ if (parameter_type) {
34+ gchar* parameter_type_string;
35+ parameter_type_string = g_variant_type_dup_string(parameter_type);
36+
37+ g_action_group_activate_action(m_actionGroup,
38+ nameBytes.data(),
39+ Converter::toGVariantWithSchema(parameter, parameter_type_string));
40+
41+ g_free(parameter_type_string);
42+ } else {
43+ g_action_group_activate_action(m_actionGroup, nameBytes.data(), Converter::toGVariant(parameter));
44+ }
45 }
46 }
47
48
49=== modified file 'libqmenumodel/src/qstateaction.cpp'
50--- libqmenumodel/src/qstateaction.cpp 2014-02-28 12:10:02 +0000
51+++ libqmenumodel/src/qstateaction.cpp 2014-03-20 11:46:21 +0000
52@@ -93,9 +93,7 @@
53 */
54 void QStateAction::updateState(const QVariant &state)
55 {
56- QVariant v = state;
57- if (v.convert(m_state.type()))
58- m_group->updateActionState(m_name, v);
59+ m_group->updateActionState(m_name, state);
60 }
61
62 /*!
63@@ -126,7 +124,7 @@
64 void QStateAction::setState(const QVariant &state)
65 {
66 QVariant v = state;
67- if (!m_state.isValid() || (v.convert(m_state.type()) && v != m_state)) {
68+ if ((m_state.isValid() != state.isValid()) || (v.convert(m_state.type()) && v != m_state)) {
69 m_state = v;
70 Q_EMIT stateChanged(m_state);
71 }
72
73=== modified file 'tests/client/actiongrouptest.cpp'
74--- tests/client/actiongrouptest.cpp 2012-11-23 13:22:04 +0000
75+++ tests/client/actiongrouptest.cpp 2014-03-20 11:46:21 +0000
76@@ -154,6 +154,101 @@
77 }
78
79 /*
80+ * Test if Action::trigger active the action over DBus
81+ * using incorrect type (variant should be converted)
82+ */
83+ void testActiveActionIncorrectType()
84+ {
85+ // start model
86+ m_model.start();
87+ m_actionGroup.start();
88+
89+ // Make menu available
90+ m_script.publishMenu();
91+ m_script.walk(2);
92+
93+ // Get Action
94+ QVariant action = m_model.data(m_model.index(1, 0), QMenuModel::Action);
95+ QVERIFY(action.isValid());
96+ QStateAction *act = m_actionGroup.action(action.toString());
97+ QVERIFY(act);
98+
99+ // test action name
100+ QCOMPARE(act->property("name").toString(), QString("Menu1Act"));
101+
102+ act->activate(QVariant(42.0));
103+ // wait for dbus propagation
104+ QTest::qWait(500);
105+
106+ QPair<QString, QVariant> result = m_script.popActivatedAction();
107+ QCOMPARE(result.first, QString("Menu1Act"));
108+ QCOMPARE(result.second.toString(), QString("42"));
109+ }
110+
111+ /*
112+ * Test updating state over DBus
113+ */
114+ void testUpdateActionState()
115+ {
116+ // start model
117+ m_model.start();
118+ m_actionGroup.start();
119+
120+ // Make menu available
121+ m_script.publishMenu();
122+ m_script.walk(2);
123+
124+ // Get Action
125+ QVariant action = m_model.data(m_model.index(1, 0), QMenuModel::Action);
126+ QVERIFY(action.isValid());
127+ QStateAction *act = m_actionGroup.action(action.toString());
128+ QVERIFY(act);
129+
130+ // test action name
131+ QCOMPARE(act->property("name").toString(), QString("Menu1Act"));
132+
133+ act->updateState(QVariant("Testing1"));
134+ // wait for dbus propagation
135+ QTest::qWait(500);
136+
137+ QPair<QString, QVariant> result = m_script.popActionStateChange();
138+ QCOMPARE(result.first, QString("Menu1Act"));
139+ QCOMPARE(result.second.toString(), QString("Testing1"));
140+ }
141+
142+ /*
143+ * Test updating state over DBus
144+ * using incorrect type (variant should be converted)
145+ */
146+ void testUpdateActionStateIncorrectType()
147+ {
148+ // start model
149+ m_model.start();
150+ m_actionGroup.start();
151+
152+ // Make menu available
153+ m_script.publishMenu();
154+ m_script.walk(2);
155+
156+ // Get Action
157+ QVariant action = m_model.data(m_model.index(1, 0), QMenuModel::Action);
158+ QVERIFY(action.isValid());
159+ QStateAction *act = m_actionGroup.action(action.toString());
160+ QVERIFY(act);
161+
162+ // test action name
163+ QCOMPARE(act->property("name").toString(), QString("Menu1Act"));
164+
165+ act->updateState(QVariant(22));
166+ // wait for dbus propagation
167+ QTest::qWait(500);
168+
169+ QPair<QString, QVariant> result = m_script.popActionStateChange();
170+ QCOMPARE(result.first, QString("Menu1Act"));
171+ QCOMPARE(result.second.toString(), QString("22"));
172+ }
173+
174+ /*
175 * Test if Action became invalid after desappear from DBus
176 */
177 void testRemoveAction()
178@@ -201,6 +296,7 @@
179
180 // Action appear
181 QVERIFY(act->isValid());
182+ QVERIFY(act->state().isValid());
183 }
184 };
185
186
187=== modified file 'tests/client/script_actiongrouptest.py'
188--- tests/client/script_actiongrouptest.py 2013-01-31 04:22:06 +0000
189+++ tests/client/script_actiongrouptest.py 2014-03-20 11:46:21 +0000
190@@ -19,8 +19,8 @@
191 from gi._gi import variant_type_from_string
192
193 al = ActionList(MENU_OBJECT_PATH)
194-al.appendItem("Menu0", "Menu0Act", actionStateType=variant_type_from_string('s'))
195-al.appendItem("Menu1", "Menu1Act", actionStateType=variant_type_from_string('s'))
196+al.appendItem("Menu0", "Menu0Act", actionStateType=variant_type_from_string('s'), actionState=GLib.Variant('s', 'Menu0State'))
197+al.appendItem("Menu1", "Menu1Act", actionStateType=variant_type_from_string('s'), actionState=GLib.Variant('s', 'Menu0State'))
198 al.removeItem("1", "Menu1Act")
199
200 t = Script.create(al)
201
202=== modified file 'tests/script/dbusmenuscript.cpp'
203--- tests/script/dbusmenuscript.cpp 2012-11-23 13:22:04 +0000
204+++ tests/script/dbusmenuscript.cpp 2014-03-20 11:46:21 +0000
205@@ -109,3 +109,19 @@
206 return qMakePair(QString(), QVariant());
207 }
208
209+QPair<QString, QVariant> DBusMenuScript::popActionStateChange()
210+{
211+ if (m_script) {
212+ QDBusMessage reply = m_script->call("popActionStateChange");
213+ if (reply.arguments().count() > 0) {
214+ QVariant value;
215+ QString name = reply.arguments()[0].toString();
216+ if (reply.arguments().count() > 1) {
217+ value = reply.arguments()[1];
218+ }
219+ return qMakePair(name, value);
220+ }
221+ }
222+
223+ return qMakePair(QString(), QVariant());
224+}
225
226=== modified file 'tests/script/dbusmenuscript.h'
227--- tests/script/dbusmenuscript.h 2012-11-23 13:22:04 +0000
228+++ tests/script/dbusmenuscript.h 2014-03-20 11:46:21 +0000
229@@ -48,6 +48,7 @@
230 void unpublishMenu();
231
232 QPair<QString, QVariant> popActivatedAction();
233+ QPair<QString, QVariant> popActionStateChange();
234
235 private:
236 QDBusInterface *m_script;
237
238=== modified file 'tests/script/menuscript.py'
239--- tests/script/menuscript.py 2013-01-31 04:22:06 +0000
240+++ tests/script/menuscript.py 2014-03-20 11:46:21 +0000
241@@ -74,6 +74,11 @@
242 def popActivatedAction(self):
243 return self._list._activatedActions.pop(0)
244
245+ @dbus.service.method(dbus_interface=INTERFACE_NAME,
246+ in_signature='', out_signature='ss')
247+ def popActionStateChange(self):
248+ return self._list._actionStateChanges.pop(0)
249+
250 @staticmethod
251 def create(aList):
252 global bus
253@@ -110,9 +115,15 @@
254 parent.append_item(item)
255
256 # Action
257- act = Gio.SimpleAction.new(self._kargs['actionName'], self._kargs['actionStateType'])
258- act.connect('activate', self._list._onActionActivated)
259- self._list._rootAction.insert(act)
260+ if self._kargs['actionState'] == None:
261+ act = Gio.SimpleAction.new(self._kargs['actionName'], self._kargs['actionStateType'])
262+ act.connect('activate', self._list._onActionActivated)
263+ self._list._rootAction.insert(act)
264+ else:
265+ act = Gio.SimpleAction.new_stateful(self._kargs['actionName'], self._kargs['actionStateType'], self._kargs['actionState'])
266+ act.connect('activate', self._list._onActionActivated)
267+ act.connect('change-state', self._list._onActionStateChanged)
268+ self._list._rootAction.insert(act)
269
270 elif self._kargs['link'] == 'section':
271 section = Gio.Menu()
272@@ -150,15 +161,17 @@
273 self._root = None
274 self._rootAction = None
275 self._activatedActions = []
276+ self._actionStateChanges = []
277
278- def appendItem(self, label, actionName, link=None, parentId=None, properties=None, actionStateType=None):
279+ def appendItem(self, label, actionName, link=None, parentId=None, properties=None, actionStateType=None, actionState=None):
280 self._actions.append(Action(self, 'append',
281 parentId=parentId,
282 label=label,
283 actionName=actionName,
284 link=link,
285 properties=properties,
286- actionStateType=actionStateType))
287+ actionStateType=actionStateType,
288+ actionState=actionState))
289
290 def removeItem(self, menuId, actionName=None):
291 self._actions.append(Action(self, 'remove',
292@@ -229,3 +242,6 @@
293
294 def _onActionActivated(self, action, parameter):
295 self._activatedActions.append((action.get_name(), parameter.get_string()))
296+
297+ def _onActionStateChanged(self, action, value):
298+ self._actionStateChanges.append((action.get_name(), value.get_string()))

Subscribers

People subscribed via source and target branches