aboutsummaryrefslogtreecommitdiff
path: root/tests/client/convertertest.cpp
blob: 933ed2f70f6a3bf43a5c8098e593daa8dd5e3e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright 2012 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Renato Araujo Oliveira Filho <renato@canonical.com>
 */

extern "C" {
#include <glib.h>
}

#include "converter.h"

#include <QObject>
#include <QtTest>
#include <QDebug>

class ConverterTest : public QObject
{
    Q_OBJECT

private:
    bool compare(const QVariant &qv, const GVariantType *type)
    {
        bool result;
        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()
                       << "Result:" << g_variant_type_peek_string (g_variant_get_type(gv))
                       << "Expected:"<< g_variant_type_peek_string (type);
        }
        g_variant_unref(gv);
        return result;
    }
    bool compare(GVariant *gv, const QVariant::Type type)
    {
        g_variant_ref_sink(gv);
        const QVariant& qv = Converter::toQVariant(gv);
        bool result = (qv.type() == type);
        if (!result) {
            qWarning() << "types are different: GVariant:" << g_variant_type_peek_string (g_variant_get_type(gv))
                       << "Result:" << qv.type()
                       << "Expected:"<< type;
        }
        g_variant_unref(gv);
        return result;
    }
    bool compare(GVariant *gv, const QMetaType::Type type)
    {
        return compare(gv, (QVariant::Type) type);
    }
    bool compareWithSchema(const QVariant &qv, const QString strType)
    {
        GVariantType* expected_type;
        expected_type = g_variant_type_new(strType.toUtf8().data());

        bool result;
        GVariant *gv = Converter::toGVariantWithSchema(qv, strType.toUtf8().data());
        result = g_variant_type_equal(g_variant_get_type(gv), expected_type);
        if (!result) {
            qWarning() << "types are different: QVariant:" << qv.typeName()
                       << "Result:" << (const char*) g_variant_get_type(gv)
                       << "Expected:"<< (const char*) expected_type;
        }
        g_variant_unref(gv);
        return result;
    }

private Q_SLOTS:

    /*
     * Test converter QVariant to GVariant
     */

    void testBooleanToGVariant()
    {
        // Boolean
        QVERIFY(compare(QVariant(true), G_VARIANT_TYPE_BOOLEAN));
    }

    void testByteToGVariant()
    {
        // Byte
        QVERIFY(compare(QVariant::fromValue<uchar>(42), G_VARIANT_TYPE_BYTE));
    }

    void testInt16ToGVariant()
    {
        // Int16
        QVERIFY(compare(QVariant::fromValue<short>(-42), G_VARIANT_TYPE_INT16));
    }

    void testUInt16ToGVariant()
    {
        // UInt16
        QVERIFY(compare(QVariant::fromValue<ushort>(42), G_VARIANT_TYPE_UINT16));
    }

    void testInt32ToGVariant()
    {
        // Int32
        QVERIFY(compare(QVariant(-42), G_VARIANT_TYPE_INT32));
    }

    void testUInt32ToGVariant()
    {
        // UInt32
        QVERIFY(compare(QVariant((uint)42), G_VARIANT_TYPE_UINT32));
    }

    void testInt64ToGVariant()
    {
        // Int64
        QVERIFY(compare(QVariant::fromValue<long>(-42), G_VARIANT_TYPE_INT64));
    }

    void testUInt64ToGVariant()
    {
        // UInt64
        QVERIFY(compare(QVariant::fromValue<ulong>(42), G_VARIANT_TYPE_UINT64));
    }

    void testQlongLongToGVariant()
    {
        // Int64
        QVERIFY(compare(QVariant::fromValue<qlonglong>(-42), G_VARIANT_TYPE_INT64));
    }

    void testUQlongLongToGVariant()
    {
        // UInt64
        QVERIFY(compare(QVariant::fromValue<qulonglong>(42), G_VARIANT_TYPE_UINT64));
    }

    void testDoubleToGVariant()
    {
        // Double
        QVERIFY(compare(QVariant((double)42.42), G_VARIANT_TYPE_DOUBLE));
    }

    void testStringToGVariant()
    {
        // String
        QVERIFY(compare(QVariant(QString("42")), G_VARIANT_TYPE_STRING));
    }

    void testByteArrayToGVariant()
    {
        // ByteArray
        QVERIFY(compare(QVariant(QByteArray("42")), G_VARIANT_TYPE_BYTESTRING));
    }

    void testByteArrayListToGVariant()
    {
        // ByteArrayList
        QVariant result;
        result.setValue(QByteArrayList({"42", "53"}));
        QVERIFY(compare(result, G_VARIANT_TYPE_BYTESTRING_ARRAY));
    }

    void testMapToGVariant()
    {
        // Map
        QVERIFY(compare(QVariantMap({{"fooBar", 0xdeadbeef}}), G_VARIANT_TYPE_VARDICT));
    }

    void testBooleanToQVariant()
    {
        // Boolean
        QVERIFY(compare(g_variant_new_boolean(TRUE), QVariant::Bool));
    }

    void testByteToQVariant()
    {
        // Byte
        QVERIFY(compare(g_variant_new_byte(53), QMetaType::UChar));
    }

    void testInt16ToQVariant()
    {
        // Int16
        QVERIFY(compare(g_variant_new_int16(-53), QMetaType::Short));
    }

    void testUInt16ToQVariant()
    {
        // UInt16
        QVERIFY(compare(g_variant_new_uint16(53), QMetaType::UShort));
    }

    void testInt32ToQVariant()
    {
        // Int32
        QVERIFY(compare(g_variant_new_int32(-53), QVariant::Int));
    }

    void testUInt32ToQVariant()
    {
        // UInt32
        QVERIFY(compare(g_variant_new_uint32(53), QVariant::UInt));
    }

    void testInt64ToQVariant()
    {
        // Int64
        QVERIFY(compare(g_variant_new_int64(-53), QVariant::LongLong));
    }

    void testUInt64ToQVariant()
    {
        // UInt64
        QVERIFY(compare(g_variant_new_uint64(53), QVariant::ULongLong));
    }

    void testDoubleToQVariant()
    {
        // Double
        QVERIFY(compare(g_variant_new_double(53.53), QVariant::Double));
    }

    void testStringToQVariant()
    {
        // String
        QVERIFY(compare(g_variant_new_string("53"), QVariant::String));
    }

    void testByteArrayToQVariant()
    {
        // ByteArray
        QVERIFY(compare(g_variant_new_bytestring("53"), QVariant::ByteArray));
    }

    void testByteArrayListToQVariant()
    {
        // ByteArrayList
        const gchar * byteArray[] = {"42", "53", NULL};
        QVERIFY(compare(g_variant_new_bytestring_array(byteArray, -1), QMetaType::QByteArrayList));
    }

    void testTupleConversion()
    {
        QVariantList qTuple;
        qTuple << 1 << "2" << 3.3;

        GVariant *gTuple = Converter::toGVariant(qTuple);
        QVERIFY(g_variant_type_is_tuple(g_variant_get_type(gTuple)));
        QCOMPARE(g_variant_n_children(gTuple), (gsize)3);

        GVariant *v = g_variant_get_child_value(gTuple, 0);
        int v0 = g_variant_get_int32(v);
        QCOMPARE(v0, 1);
        g_variant_unref(v);

        v = g_variant_get_child_value(gTuple, 1);
        const gchar *v1 = g_variant_get_string(v, NULL);
        QCOMPARE(QString(v1), QString("2"));
        g_variant_unref(v);

        v = g_variant_get_child_value(gTuple, 2);
        gdouble v2 = g_variant_get_double(v);
        QCOMPARE(v2, 3.3);
        g_variant_unref(v);

        g_variant_unref(gTuple);
    }

    void testSchemaConvert()
    {
        // convert to integer
        compareWithSchema(QVariant::fromValue<int>(1), "i");
        compareWithSchema(QVariant::fromValue<double>(1.1), "i");

        // convert to integer
        compareWithSchema(QVariant::fromValue<bool>(true), "b");
        compareWithSchema(QVariant::fromValue<int>(1), "b");

        // convert to double
        compareWithSchema(QVariant::fromValue<double>(1.0), "d");
        compareWithSchema(QVariant::fromValue<int>(1), "d");

        // convert to string
        compareWithSchema(QVariant::fromValue<int>(1), "s");
        compareWithSchema(QVariant::fromValue<double>(1.1), "s");

        // convert to tuple
        compareWithSchema(QVariantList() << QVariant::fromValue<bool>(true) << QVariant::fromValue<int>(1) << QVariant::fromValue<int>(1) << QVariant::fromValue<QString>("test1"), "(bdis)");

        // convert to array
        compareWithSchema(QVariantList() << QVariant::fromValue<int>(1) << QVariant::fromValue<int>(1), "ad");
        compareWithSchema(QVariantList() << QVariant::fromValue<QString>("test1") << QVariant::fromValue<QString>("test2"), "as");

        // convert to array of tuple
        QVariantList si1(QVariantList() << QVariant::fromValue<QString>("test1") << QVariant::fromValue<int>(1));
        QVariantList si2(QVariantList() << QVariant::fromValue<QString>("test1") << QVariant::fromValue<int>(1));
        compareWithSchema(QVariantList() << QVariant::fromValue(si1) << QVariant::fromValue(si2), "a(sd)");

        // convert to vardict
        QVariantMap map;
        map["test1"] = QVariant::fromValue<int>(1);
        map["test2"] = QVariant::fromValue<double>(1);
        compareWithSchema(map, "a{sv}");
    }

};

QTEST_MAIN(ConverterTest)

#include "convertertest.moc"