summaryrefslogtreecommitdiffstats
path: root/tests/auto/declarative/qmlparser/tst_qmlparser.cpp
blob: 24d3c223a58364bb4b692b3b5c63574989c5d3a2 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <qtest.h>
#include <QtDeclarative/qmlengine.h>
#include <QtDeclarative/qmlcomponent.h>

class MyInterface 
{
public:
    MyInterface() : id(913) {}
    int id;
};

Q_DECLARE_INTERFACE(MyInterface, "com.trolltech.Qt.Test.MyInterface");
QML_DECLARE_INTERFACE(MyInterface);
QML_DEFINE_INTERFACE(MyInterface);

class MyQmlObject : public QObject, public MyInterface
{
    Q_OBJECT
    Q_PROPERTY(int value READ value WRITE setValue)
    Q_PROPERTY(QString readOnlyString READ readOnlyString)
    Q_PROPERTY(bool enabled READ enabled WRITE setEnabled)
    Q_PROPERTY(QRect rect READ rect WRITE setRect)
    Q_PROPERTY(QMatrix matrix READ matrix WRITE setMatrix)  //assumed to be unsupported by QML
    Q_PROPERTY(MyInterface *interface READ interface WRITE setInterface)
    Q_INTERFACES(MyInterface)
public:
    MyQmlObject() : m_value(-1), m_interface(0) {}

    int value() const { return m_value; }
    void setValue(int v) { m_value = v; }

    QString readOnlyString() const { return QLatin1String(""); }

    bool enabled() const { return false; }
    void setEnabled(bool) {}

    QRect rect() const { return QRect(); }
    void setRect(const QRect&) {}

    QMatrix matrix() const { return QMatrix(); }
    void setMatrix(const QMatrix&) {}

    MyInterface *interface() const { return m_interface; }
    void setInterface(MyInterface *iface) { m_interface = iface; }

    Q_CLASSINFO("defaultMethod", "basicSlot()");

public slots:
    void basicSlot() { qWarning("MyQmlObject::basicSlot"); }

signals:
    void basicSignal();

private:
    friend class tst_qmlparser;
    int m_value;
    MyInterface *m_interface;
};

QML_DECLARE_TYPE(MyQmlObject);
QML_DEFINE_TYPE(MyQmlObject,MyQmlObject);

class MyContainer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<QObject*>* children READ children)
    Q_PROPERTY(QList<MyInterface*>* qlistInterfaces READ qlistInterfaces)
    Q_PROPERTY(QmlList<MyInterface*>* qmllistInterfaces READ qmllistInterfaces)
    Q_CLASSINFO("DefaultProperty", "children");
public:
    MyContainer() {}

    QList<QObject*> *children() { return &m_children; }
    QList<MyInterface *> *qlistInterfaces() { return &m_interfaces; }
    QmlList<MyInterface *> *qmllistInterfaces() { return &m_qmlinterfaces; }
    const QmlConcreteList<MyInterface *> &qmllistAccessor() const { return m_qmlinterfaces; }

private:
    QList<QObject*> m_children;
    QList<MyInterface *> m_interfaces;
    QmlConcreteList<MyInterface *> m_qmlinterfaces;
};

QML_DECLARE_TYPE(MyContainer);
QML_DEFINE_TYPE(MyContainer,MyContainer);

class MyDotPropertyObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(MyQmlObject *obj READ obj)
    Q_PROPERTY(MyQmlObject *readWriteObj READ readWriteObj WRITE setReadWriteObj)
public:
    MyDotPropertyObject() : m_rwobj(0), m_ownRWObj(false) {}
    ~MyDotPropertyObject()
    {
        if (m_ownRWObj)
            delete m_rwobj;
    }

    MyQmlObject *obj() { return 0; }

    MyQmlObject *readWriteObj()
    {
        if (!m_rwobj) {
            m_rwobj = new MyQmlObject;
            m_ownRWObj = true;
        }
        return m_rwobj;
    }

    void setReadWriteObj(MyQmlObject *obj)
    {
        if (m_ownRWObj) {
            delete m_rwobj;
            m_ownRWObj = false;
        }

        m_rwobj = obj;
    }

private:
    MyQmlObject *m_rwobj;
    bool m_ownRWObj;
};

QML_DECLARE_TYPE(MyDotPropertyObject);
QML_DEFINE_TYPE(MyDotPropertyObject,MyDotPropertyObject);

class tst_qmlparser : public QObject
{
    Q_OBJECT
public:
    tst_qmlparser() {}

private slots:
    void simpleObject();
    void simpleContainer();
    void unregisteredObject();
    void nonexistantProperty();
    void unsupportedProperty();
    //void setProperties();
    void wrongType();
    void readOnly();
    //void dotProperties();
    void nullDotProperty();
    void fakeDotProperty();
    //void readWriteDotProperty();
    void emptyInput();
    void missingObject();
    void invalidXML();
    void duplicateIDs();
    void invalidID();
    void interfaceProperty();
    void interfaceQmlList();
    void interfaceQList();
    void cannotAssignBindingToSignal();
    void assignObjectToSignal();

private:
    QmlEngine engine;
};

void tst_qmlparser::simpleObject()
{
    QmlComponent component(&engine, "<MyQmlObject/>");
    QObject *object = component.create();
    QVERIFY(object != 0);
}

void tst_qmlparser::simpleContainer()
{
    QmlComponent component(&engine, "<MyContainer>\n<MyQmlObject/>\n<MyQmlObject/>\n</MyContainer>");
    MyContainer *container= qobject_cast<MyContainer*>(component.create());
    QVERIFY(container != 0);
    QCOMPARE(container->children()->count(),2);
}

void tst_qmlparser::unregisteredObject()
{
    QmlComponent component(&engine, "<UnRegisteredObject/>", QUrl("myprogram.qml"));
    QTest::ignoreMessage(QtWarningMsg, "Unable to create object of type 'UnRegisteredObject' @myprogram.qml:1");
    QObject *object = component.create();
    QVERIFY(object == 0);
}

void tst_qmlparser::nonexistantProperty()
{
    //NOTE: these first 3 should all have the same error message
    {
        QmlComponent component(&engine, "<MyQmlObject something=\"24\"/>");
        QTest::ignoreMessage(QtWarningMsg, "Unknown property 'something' @<unspecified file>:1");
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

    {
        QmlComponent component(&engine, "<MyQmlObject>\n<something>24</something>\n</MyQmlObject>");
        QTest::ignoreMessage(QtWarningMsg, "Unknown property 'something' @<unspecified file>:2");
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

    //non-existant using binding
    {
        QmlComponent component(&engine, "<MyQmlObject something=\"{1}\"/>");
        QTest::ignoreMessage(QtWarningMsg, "Unknown property 'something' @<unspecified file>:1");
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

    {
        QmlComponent component(&engine, "<MyQmlObject><something></something></MyQmlObject>");
        QObject *object = component.create();
        QVERIFY(object != 0);
    }

    //non-existant value-type default property
    {
        QmlComponent component(&engine, "<MyQmlObject>\n24\n</MyQmlObject>");
        QTest::ignoreMessage(QtWarningMsg, "Unable to resolve default property @<unspecified file>:3");
        // XXX would 2 be a better line number in this case? Message should also be improved.
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

    //non-existant object-type default property
    {
        QmlComponent component(&engine, "<MyQmlObject>\n<MyQmlObject/>\n</MyQmlObject>");
        QTest::ignoreMessage(QtWarningMsg, "Unable to assign to non-existant property  @<unspecified file>:2");
        // XXX Message needs to be improved (and should be closer to value-type message).
        QObject *object = component.create();
        QVERIFY(object == 0);
    }
}

void tst_qmlparser::unsupportedProperty()
{
    QTest::ignoreMessage(QtWarningMsg, "Property 'matrix' is of an unknown type @<unspecified file>:1");
    QmlComponent component(&engine, "<MyQmlObject matrix=\"1,0,0,0,1,0,0,0,1\"/>");
    MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create());
    QVERIFY(object == 0);
}

void tst_qmlparser::wrongType()
{
    //string for int
    {
        QTest::ignoreMessage(QtWarningMsg, "Can't assign value 'hello' to property 'value' @<unspecified file>:1");
        QmlComponent component(&engine, "<MyQmlObject value=\"hello\"/>");
        MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create());
        QVERIFY(object == 0);
    }

    //int for bool
    {
        QTest::ignoreMessage(QtWarningMsg, "Can't assign value '5' to property 'enabled' @<unspecified file>:1");
        QmlComponent component(&engine, "<MyQmlObject enabled=\"5\"/>");
        MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create());
        QVERIFY(object == 0);
    }

    //bad format for rect
    {
        QTest::ignoreMessage(QtWarningMsg, "Can't assign value '5,5x10' to property 'rect' @<unspecified file>:1");
        QmlComponent component(&engine, "<MyQmlObject rect=\"5,5x10\"/>");
        MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create());
        QVERIFY(object == 0);
    }

    //TODO: more types (including float-to-int, complex types, etc)
}

void tst_qmlparser::readOnly()
{
    {
        QTest::ignoreMessage(QtWarningMsg, "Can't assign value 'hello' to property 'readOnlyString' because 'readOnlyString' is read-only @<unspecified file>:1");
        QmlComponent component(&engine, "<MyQmlObject readOnlyString=\"hello\"/>");
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

    {
        QTest::ignoreMessage(QtWarningMsg, "Can't assign a binding to property 'readOnlyString' because 'readOnlyString' is read-only @<unspecified file>:1");
        QmlComponent component(&engine, "<MyQmlObject readOnlyString=\"{'hello'}\"/>");
        QObject *object = component.create();
        QVERIFY(object == 0);
    }
}

void tst_qmlparser::nullDotProperty()
{
    QTest::ignoreMessage(QtWarningMsg, "Can't set properties on 'obj' because it is null @<unspecified file>:1");
    QmlComponent component(&engine, "<MyDotPropertyObject obj.value=\"1\"/>");
    QObject *object = component.create();
    QVERIFY(object == 0);
}

void tst_qmlparser::fakeDotProperty()
{
    QTest::ignoreMessage(QtWarningMsg, "Can't set properties on 'value' because it isn't a known object type @<unspecified file>:1");
    QmlComponent component(&engine, "<MyQmlObject value.something=\"hello\"/>");
    QObject *object = component.create();
    QVERIFY(object == 0);
}

//XXX need to define correct behavior first
/*void tst_qmlparser::readWriteDotProperty()
{
    QmlComponent component(&engine, "<MyDotPropertyObject readWriteObj.value=\"1\"/>");
    MyDotPropertyObject *object = qobject_cast<MyDotPropertyObject*>(component.create());
    QVERIFY(object != 0);
    QCOMPARE(object->readWriteObj()->value(),1);

    {
        QmlComponent component(&engine, "<MyContainer><MyQmlObject id=\"Obj\" value=\"1\"/><MyDotPropertyObject readWriteObj=\"{Obj}\"/></MyContainer>");
        MyContainer *object = qobject_cast<MyContainer*>(component.create());
        QVERIFY(object != 0);
        MyDotPropertyObject *dpo = qobject_cast<MyDotPropertyObject *>(object->children()->at(1));
        QCOMPARE(dpo->readWriteObj()->value(),1);
    }
}*/

void tst_qmlparser::emptyInput()
{
    QTest::ignoreMessage(QtCriticalMsg, "No Qml was specified for parsing.");
    QTest::ignoreMessage(QtWarningMsg, "Can't compile because of earlier errors @<unspecified file>:-1");
    QmlComponent component(&engine, "");
    QObject *object = component.create();
    QVERIFY(object == 0);
}

void tst_qmlparser::missingObject()
{
    QTest::ignoreMessage(QtCriticalMsg, "Can't have a property with no object @<unspecified file>:1");
    QTest::ignoreMessage(QtWarningMsg, "Can't compile because of earlier errors @<unspecified file>:-1");
    QmlComponent component(&engine, "<something/>");
    QObject *object = component.create();
    QVERIFY(object == 0);
}


void tst_qmlparser::invalidXML()
{
    //extra stuff on end
    {
        QTest::ignoreMessage(QtCriticalMsg, "Extra content at end of document. @myprogram.qml:1");
        QTest::ignoreMessage(QtWarningMsg, "Can't compile because of earlier errors @myprogram.qml:-1");
        QmlComponent component(&engine, "<MyQmlObject/><something/>", QUrl("myprogram.qml"));
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

    //mismatched tags
    {
        QTest::ignoreMessage(QtCriticalMsg, "Opening and ending tag mismatch. @myprogram.qml:2");
        QTest::ignoreMessage(QtWarningMsg, "Can't compile because of earlier errors @myprogram.qml:-1");
        QmlComponent component(&engine, "<MyQmlObject>\n</MyContainer>", QUrl("myprogram.qml"));
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

    {
        QTest::ignoreMessage(QtCriticalMsg, "Expected '>' or '/', but got '<'. @myprogram.qml:1");
        QTest::ignoreMessage(QtWarningMsg, "Can't compile because of earlier errors @myprogram.qml:-1");
        QmlComponent component(&engine, "<MyQmlObject < />", QUrl("myprogram.qml"));
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

    {
        QTest::ignoreMessage(QtCriticalMsg, "Premature end of document. @myprogram.qml:1");
        QTest::ignoreMessage(QtWarningMsg, "Can't compile because of earlier errors @myprogram.qml:-1");
        QmlComponent component(&engine, "<MyQmlObject something=\" />", QUrl("myprogram.qml"));
        QObject *object = component.create();
        QVERIFY(object == 0);
    }

}

void tst_qmlparser::duplicateIDs()
{
    QTest::ignoreMessage(QtWarningMsg, "An id (\"MyID\") is not unique within its scope. @<unspecified file>:3");
    QmlComponent component(&engine, "<MyContainer>\n<MyQmlObject id=\"MyID\"/>\n<MyQmlObject id=\"MyID\"/>\n</MyContainer>");
    QObject *object = component.create();
    QVERIFY(object == 0);
}

void tst_qmlparser::invalidID()
{
    QTest::ignoreMessage(QtWarningMsg, "'1' is not a valid id @<unspecified file>:1");
    QmlComponent component(&engine, "<MyQmlObject id=\"1\"/>");
    QObject *object = component.create();
    QVERIFY(object == 0);
}

void tst_qmlparser::interfaceProperty()
{
    QmlComponent component(&engine, "<MyQmlObject><interface>\n<MyQmlObject/></interface>\n</MyQmlObject>");
    MyQmlObject *object = qobject_cast<MyQmlObject*>(component.create());
    QVERIFY(object != 0);
    QVERIFY(object->interface());
    QVERIFY(object->interface()->id == 913);
}

void tst_qmlparser::interfaceQmlList()
{
    QmlComponent component(&engine, "<MyContainer><qmllistInterfaces>\n<MyQmlObject/>\n<MyQmlObject/>\n</qmllistInterfaces>\n</MyContainer>");
    MyContainer *container= qobject_cast<MyContainer*>(component.create());
    QVERIFY(container != 0);
    QVERIFY(container->qmllistAccessor().count() == 2);
    for(int ii = 0; ii < 2; ++ii)
        QVERIFY(container->qmllistAccessor().at(ii)->id == 913);
}

void tst_qmlparser::interfaceQList()
{
    QmlComponent component(&engine, "<MyContainer><qlistInterfaces>\n<MyQmlObject/>\n<MyQmlObject/>\n</qlistInterfaces>\n</MyContainer>");
    MyContainer *container= qobject_cast<MyContainer*>(component.create());
    QVERIFY(container != 0);
    QVERIFY(container->qlistInterfaces()->count() == 2);
    for(int ii = 0; ii < 2; ++ii)
        QVERIFY(container->qlistInterfaces()->at(ii)->id == 913);
}

void tst_qmlparser::cannotAssignBindingToSignal()
{
    QTest::ignoreMessage(QtWarningMsg, "Cannot assign binding to signal property @<unspecified file>:1");
    QmlComponent component(&engine, "<MyQmlObject onBasicSignal=\"{print(1921)}\" />");
    MyContainer *container= qobject_cast<MyContainer*>(component.create());
    QVERIFY(container == 0);
}

void tst_qmlparser::assignObjectToSignal()
{
    QmlComponent component(&engine, "<MyQmlObject><onBasicSignal><MyQmlObject /></onBasicSignal></MyQmlObject>");
    MyQmlObject *object = qobject_cast<MyQmlObject *>(component.create());
    QVERIFY(object != 0);
    QTest::ignoreMessage(QtWarningMsg, "MyQmlObject::basicSlot");
    emit object->basicSignal();
}

QTEST_MAIN(tst_qmlparser)

#include "tst_qmlparser.moc"