aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRenato Araujo Oliveira Filho <renato.filho@canonical.com>2012-10-17 15:10:49 +0000
committerTarmac <>2012-10-17 15:10:49 +0000
commitd46ee24446b49d8ef00f9f0aef44ee863966f245 (patch)
tree6c13049fc74b0c44d2bbe28dfd1dc212dad1970c
parentced307a448aeed3a20d1a1e4bdcbf4dcbe4b1e46 (diff)
parentcdd910c7a326af6d87acc6df1c90016c8e30cbc7 (diff)
downloadqmenumodel-d46ee24446b49d8ef00f9f0aef44ee863966f245.tar.gz
qmenumodel-d46ee24446b49d8ef00f9f0aef44ee863966f245.tar.bz2
qmenumodel-d46ee24446b49d8ef00f9f0aef44ee863966f245.zip
Implemented conversion for QVariantMap;
Created unit test for QVariantMap conversion;. Approved by Olivier Tilloy, PS Jenkins bot.
-rw-r--r--libqmenumodel/src/converter.cpp28
-rw-r--r--tests/client/convertertest.cpp6
-rw-r--r--tests/client/modeltest.cpp10
-rwxr-xr-xtests/client/script_modeltest.py9
4 files changed, 50 insertions, 3 deletions
diff --git a/libqmenumodel/src/converter.cpp b/libqmenumodel/src/converter.cpp
index 452f68c..c2b8fb1 100644
--- a/libqmenumodel/src/converter.cpp
+++ b/libqmenumodel/src/converter.cpp
@@ -38,6 +38,19 @@ QVariant Converter::toQVariant(GVariant *value)
gsize size = 0;
const gchar *v = g_variant_get_string(value, &size);
result.setValue(QString::fromLatin1(v, size));
+ } else if (g_variant_type_equal(type, G_VARIANT_TYPE_VARDICT)) {
+ GVariantIter iter;
+ GVariant *vvalue;
+ gchar *key;
+ QVariantMap qmap;
+
+ g_variant_iter_init (&iter, value);
+ while (g_variant_iter_loop (&iter, "{sv}", &key, &vvalue))
+ {
+ qmap.insert(QString::fromLatin1(key), toQVariant(vvalue));
+ }
+
+ result.setValue(qmap);
} else {
qWarning() << "Unsupported GVariant value";
}
@@ -109,6 +122,21 @@ GVariant* Converter::toGVariant(const QVariant &value)
case QVariant::UInt:
result = g_variant_new_uint32(value.toUInt());
break;
+ case QVariant::Map:
+ {
+ GVariantBuilder *b;
+ GVariant *dict;
+
+ b = g_variant_builder_new(G_VARIANT_TYPE_VARDICT);
+ QMapIterator<QString, QVariant> i(value.toMap());
+ while (i.hasNext()) {
+ i.next();
+ g_variant_builder_add(b, "{sv}", i.key().toUtf8().data(), toGVariant(i.value()));
+ }
+ result = g_variant_builder_end(b);
+ g_variant_builder_unref(b);
+ break;
+ }
default:
result = ::toGVariant(value.typeName(), value);
}
diff --git a/tests/client/convertertest.cpp b/tests/client/convertertest.cpp
index 72b6a87..633ecf1 100644
--- a/tests/client/convertertest.cpp
+++ b/tests/client/convertertest.cpp
@@ -35,7 +35,7 @@ private:
bool compare(const QVariant &qv, const GVariantType *type)
{
bool result;
- GVariant *gv = Converter::toGVariant(qv);
+ GVariant *gv = Converter::toGVariant(qv);
result = g_variant_type_equal(g_variant_get_type(gv), type);
if (!result) {
qWarning() << "types are different: QVariant:" << qv.typeName()
@@ -48,7 +48,6 @@ private:
private Q_SLOTS:
-
/*
* Test converter QVariant to GVariant
*/
@@ -86,6 +85,9 @@ private Q_SLOTS:
// ByteArray
QVERIFY(compare(QVariant(QByteArray("42")), G_VARIANT_TYPE_BYTESTRING));
+
+ // Map
+ QVERIFY(compare(QVariantMap(), G_VARIANT_TYPE_VARDICT));
}
};
diff --git a/tests/client/modeltest.cpp b/tests/client/modeltest.cpp
index 03629ca..1c2eeca 100644
--- a/tests/client/modeltest.cpp
+++ b/tests/client/modeltest.cpp
@@ -202,6 +202,16 @@ private Q_SLOTS:
v = extra["string"];
QCOMPARE(v.type(), QVariant::String);
QCOMPARE(v.toString(), QString("42"));
+
+ // Map
+ v = extra["map"];
+ QVariantMap map;
+ map.insert("int64", QVariant::fromValue<long>(-42));
+ map.insert("string", "42");
+ map.insert("double", 42.42);
+
+ QCOMPARE(v.type(), QVariant::Map);
+ QCOMPARE(v.toMap(), map);
}
/*
diff --git a/tests/client/script_modeltest.py b/tests/client/script_modeltest.py
index db4e28b..95df442 100755
--- a/tests/client/script_modeltest.py
+++ b/tests/client/script_modeltest.py
@@ -5,6 +5,12 @@ from gi.repository import GLib
from menuscript import Script, ActionList, MENU_OBJECT_PATH
al = ActionList(MENU_OBJECT_PATH)
+
+# create map
+pmap = {'int64' : GLib.Variant('x', -42),
+ 'string': GLib.Variant('s', u'42'),
+ 'double': GLib.Variant('d', 42.42)}
+
al.appendItem("Menu0", "Menu0Act", None, None, {'x-boolean' : GLib.Variant('b', True),
'x-byte' : GLib.Variant('y', 42),
'x-int16' : GLib.Variant('n', -42),
@@ -14,7 +20,8 @@ al.appendItem("Menu0", "Menu0Act", None, None, {'x-boolean' : GLib.Variant('b',
'x-int64' : GLib.Variant('x', -42),
'x-uint64' : GLib.Variant('t', 42),
'x-double' : GLib.Variant('d', 42.42),
- 'x-string' : GLib.Variant('s', u'42')
+ 'x-string' : GLib.Variant('s', u'42'),
+ 'x-map' : GLib.Variant('a{sv}', pmap),
})
al.appendItem("Menu1", "Menu1Act")