testcore.cpp 253 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
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
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "testcore.h"

#include <QScopedPointer>
#include <QLatin1String>
34
#include <QGraphicsObject>
35
36
37
38
39
40
41
42

#include <metainfo.h>
#include <model.h>
#include <modelmerger.h>
#include <modelnode.h>
#include <qmlanchors.h>
#include <invalididexception.h>
#include <invalidmodelnodeexception.h>
43
#include <rewritingexception.h>
44
45
#include <nodeinstanceview.h>
#include <nodeinstance.h>
46
#include <subcomponentmanager.h>
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <QDebug>

#include "../testview.h"
#include <variantproperty.h>
#include <abstractproperty.h>
#include <bindingproperty.h>
#include <nodeproperty.h>

#include <nodelistproperty.h>
#include <nodeabstractproperty.h>
#include <componenttextmodifier.h>

#include <bytearraymodifier.h>
#include "testrewriterview.h"

62
63
#include <qmljs/qmljsinterpreter.h>

64
#include <QPlainTextEdit>
65
66
#include <private/qdeclarativestate_p.h>
#include <private/qdeclarativemetatype_p.h>
67
68
69
70
71
72


using namespace QmlDesigner;
#include <cstdio>
#include "../common/statichelpers.cpp"

73
74
75
76
77
78
79
80
81
82
83
84
85
static void initializeMetaTypeSystem(const QString &resourcePath)
{
    const QDir typeFileDir(resourcePath + QLatin1String("/qml-type-descriptions"));
    const QStringList xmlExtensions = QStringList() << QLatin1String("*.xml");
    const QFileInfoList xmlFiles = typeFileDir.entryInfoList(xmlExtensions,
                                                             QDir::Files,
                                                             QDir::Name);

    const QStringList errors = QmlJS::Interpreter::MetaTypeSystem::load(xmlFiles);
    foreach (const QString &error, errors)
        qWarning() << qPrintable(error);
}

86
87
88
89
90
91
92
93
94
95
96
TestCore::TestCore()
    : QObject()
{
}

void TestCore::initTestCase()
{
#ifndef QDEBUG_IN_TESTS
    qInstallMsgHandler(testMessageOutput);
#endif
    Exception::setShouldAssert(false);
97

98
99
100
101
102
103
    initializeMetaTypeSystem(QLatin1String(QTCREATORDIR "/share/qtcreator"));


   // Load plugins

#ifdef Q_OS_MAC
104
    const QString pluginPath = QTCREATORDIR "/bin/Qt Creator.app/Contents/PlugIns/QmlDesigner";
105
106
107
108
109
110
111
#else
    const QString pluginPath = QTCREATORDIR "/lib/qmldesigner";
#endif

    qDebug() << pluginPath;
    Q_ASSERT(QFileInfo(pluginPath).exists());
    MetaInfo::setPluginPaths(QStringList() << pluginPath);
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
}

void TestCore::cleanupTestCase()
{
    MetaInfo::clearGlobal();
}

void TestCore::testModelCreateCoreModel()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> testView(new TestView);
    QVERIFY(testView.data());
    model->attachView(testView.data());

    QVERIFY(testView->rootModelNode().isValid());
    NodeInstanceView *nodeInstanceView = new NodeInstanceView(model.data());
    model->attachView(nodeInstanceView);
    model->detachView(nodeInstanceView);
}

// TODO: this need to e updated for states
void TestCore::loadEmptyCoreModel()
{
    QFile file(":/fx/empty.qml");
    QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));

    QPlainTextEdit textEdit1;
    textEdit1.setPlainText(file.readAll());
142
    NotIndentingTextEditModifier modifier1(&textEdit1);
143
144
145
146
147
148
149
150

    QScopedPointer<Model> model1(Model::create("Qt/Item"));

    QScopedPointer<TestRewriterView> testRewriterView1(new TestRewriterView());
    testRewriterView1->setTextModifier(&modifier1);
    model1->attachView(testRewriterView1.data());

    QPlainTextEdit textEdit2;
151
    textEdit2.setPlainText("import Qt 4.7; Item{}");
152
    NotIndentingTextEditModifier modifier2(&textEdit2);
153
154
155
156
157
158
159
160
161
162
163
164
165
166

    QScopedPointer<Model> model2(Model::create("Qt/item"));

    QScopedPointer<TestRewriterView> testRewriterView2(new TestRewriterView());
    testRewriterView2->setTextModifier(&modifier2);
    model2->attachView(testRewriterView2.data());

    QVERIFY(compareTree(testRewriterView1->rootModelNode(), testRewriterView2->rootModelNode()));
}

void TestCore::testRewriterView()
{
    try {
        QPlainTextEdit textEdit;
167
        textEdit.setPlainText("import Qt 4.7;\n\nItem {\n}\n");
168
        NotIndentingTextEditModifier textModifier(&textEdit);
169
170
171
172
173
174
175
176
177
178
179
180
181
182

        QScopedPointer<Model> model(Model::create("Qt/Item"));
        QVERIFY(model.data());

        QScopedPointer<TestView> view(new TestView);
        QVERIFY(view.data());
        model->attachView(view.data());

        ModelNode rootModelNode(view->rootModelNode());
        QVERIFY(rootModelNode.isValid());
        QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView());
        testRewriterView->setTextModifier(&textModifier);
        model->attachView(testRewriterView.data());

183
        ModelNode childNode(addNodeListChild(rootModelNode, "Qt/Rectangle", 4, 7, "data"));
184
185
186
        QVERIFY(childNode.isValid());
        childNode.setId("childNode");

187
        ModelNode childNode2(addNodeListChild(childNode, "Qt/Rectangle", 4, 7, "data"));
188
        childNode2.setId("childNode2");
189
        ModelNode childNode3(addNodeListChild(childNode2, "Qt/Rectangle", 4, 7, "data"));
190
        childNode3.setId("childNode3");
191
        ModelNode childNode4(addNodeListChild(childNode3, "Qt/Rectangle", 4, 7, "data"));
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
        childNode4.setId("childNode4");

        QVERIFY(childNode.isValid());
        QVERIFY(childNode2.isValid());
        QVERIFY(childNode3.isValid());
        QVERIFY(childNode4.isValid());

        testRewriterView->setModificationGroupActive(true);

        childNode.destroy();

        QVERIFY(!childNode.isValid());
        QVERIFY(!childNode2.isValid());
        QVERIFY(!childNode3.isValid());
        QVERIFY(!childNode4.isValid());

        QVERIFY(testRewriterView->modelToTextMerger()->isNodeScheduledForRemoval(childNode));
        QVERIFY(!testRewriterView->modelToTextMerger()->isNodeScheduledForRemoval(childNode2));
        QVERIFY(!testRewriterView->modelToTextMerger()->isNodeScheduledForRemoval(childNode3));
        QVERIFY(!testRewriterView->modelToTextMerger()->isNodeScheduledForRemoval(childNode4));

        QVERIFY(!rootModelNode.hasProperty(QLatin1String("data")));

        testRewriterView->modelToTextMerger()->applyChanges();

217
        childNode = addNodeListChild(rootModelNode, "Qt/Rectangle", 4, 7, "data");
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        QVERIFY(testRewriterView->modelToTextMerger()->isNodeScheduledForAddition(childNode));

        testRewriterView->modelToTextMerger()->applyChanges();

        childNode.variantProperty("x") = 70;
        childNode.variantProperty("y") = 90;

        QCOMPARE(testRewriterView->modelToTextMerger()->findAddedVariantProperty(childNode.variantProperty("x")).value(), QVariant(70));
        QCOMPARE(testRewriterView->modelToTextMerger()->findAddedVariantProperty(childNode.variantProperty("y")).value(), QVariant(90));

        model->detachView(testRewriterView.data());
    } catch (Exception &e) {
        QFAIL(qPrintable(e.description()));
    }
}

void TestCore::testRewriterErrors()
{
    QPlainTextEdit textEdit;
237
    textEdit.setPlainText("import Qt 4.7;\n\nItem {\n}\n");
238
    NotIndentingTextEditModifier textModifier(&textEdit);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253

    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    ModelNode rootModelNode(view->rootModelNode());
    QVERIFY(rootModelNode.isValid());
    QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView());
    testRewriterView->setTextModifier(&textModifier);
    model->attachView(testRewriterView.data());

    QVERIFY(testRewriterView->errors().isEmpty());
254
    textEdit.setPlainText("import Qt 4.7;\n\nError {\n}\n");
255
256
    QVERIFY(!testRewriterView->errors().isEmpty());

257
    textEdit.setPlainText("import Qt 4.7;\n\nItem {\n}\n");
258
259
260
261
262
    QVERIFY(testRewriterView->errors().isEmpty());
}

void TestCore::saveEmptyCoreModel()
{
263
    QList<QDeclarativeError> errors;
264
265
266
267
268
    QFile file(":/fx/empty.qml");
    QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));

    QPlainTextEdit textEdit1;
    textEdit1.setPlainText(file.readAll());
269
    NotIndentingTextEditModifier modifier1(&textEdit1);
270
271
272
273
274
275
276
277
278
279
280
281
282

    QScopedPointer<Model> model1(Model::create("Qt/Item"));

    QScopedPointer<TestRewriterView> testRewriterView1(new TestRewriterView());
    testRewriterView1->setTextModifier(&modifier1);
    model1->attachView(testRewriterView1.data());


    QBuffer buffer;
    buffer.open(QIODevice::ReadWrite | QIODevice::Text);
    modifier1.save(&buffer);

    QPlainTextEdit textEdit2;
283
    textEdit2.setPlainText("import Qt 4.7; Item{}");
284
    NotIndentingTextEditModifier modifier2(&textEdit2);
285
286
287
288
289
290
291
292
293
294
295
296
297

    QScopedPointer<Model> model2(Model::create("Qt/item"));

    QScopedPointer<TestRewriterView> testRewriterView2(new TestRewriterView());
    testRewriterView2->setTextModifier(&modifier2);
    model2->attachView(testRewriterView2.data());

    QVERIFY(compareTree(testRewriterView1->rootModelNode(), testRewriterView2->rootModelNode()));

}

void TestCore::loadAttributesInCoreModel()
{
298
    QList<QDeclarativeError> errors;
299
300
301
302
303
    QFile file(":/fx/attributes.qml");
    QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));

    QPlainTextEdit textEdit1;
    textEdit1.setPlainText(file.readAll());
304
    NotIndentingTextEditModifier modifier1(&textEdit1);
305
306
307
308
309
310
311
312

    QScopedPointer<Model> model1(Model::create("Qt/Item"));

    QScopedPointer<TestRewriterView> testRewriterView1(new TestRewriterView());
    testRewriterView1->setTextModifier(&modifier1);
    model1->attachView(testRewriterView1.data());

    QPlainTextEdit textEdit2;
313
    textEdit2.setPlainText("import Qt 4.7; Item{}");
314
    NotIndentingTextEditModifier modifier2(&textEdit2);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

    QScopedPointer<Model> model2(Model::create("Qt/item"));

    QScopedPointer<TestRewriterView> testRewriterView2(new TestRewriterView());
    testRewriterView2->setTextModifier(&modifier2);
    model2->attachView(testRewriterView2.data());

    ModelNode rootModelNode = testRewriterView2->rootModelNode();

    rootModelNode.setId("theItem");
    rootModelNode.variantProperty("x").setValue(QVariant(300));
    rootModelNode.variantProperty("visible").setValue(true);
    rootModelNode.variantProperty("scale").setValue(0.5);

    QVERIFY(compareTree(testRewriterView1->rootModelNode(), testRewriterView2->rootModelNode()));
}

void TestCore::saveAttributesInCoreModel()
{
    QFile file(":/fx/attributes.qml");
    QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));

    QPlainTextEdit textEdit1;
    textEdit1.setPlainText(file.readAll());
339
    NotIndentingTextEditModifier modifier1(&textEdit1);
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

    QScopedPointer<Model> model1(Model::create("Qt/Item"));

    QScopedPointer<TestRewriterView> testRewriterView1(new TestRewriterView());
    testRewriterView1->setTextModifier(&modifier1);
    model1->attachView(testRewriterView1.data());

    QVERIFY(testRewriterView1->errors().isEmpty());

    QBuffer buffer;
    buffer.open(QIODevice::ReadWrite | QIODevice::Text);
    modifier1.save(&buffer);

    QPlainTextEdit textEdit2;
    textEdit2.setPlainText(buffer.data());
355
    NotIndentingTextEditModifier modifier2(&textEdit2);
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379

    QScopedPointer<Model> model2(Model::create("Qt/Item"));

    QScopedPointer<TestRewriterView> testRewriterView2(new TestRewriterView());
    testRewriterView2->setTextModifier(&modifier2);
    model2->attachView(testRewriterView2.data());

    QVERIFY(testRewriterView2->errors().isEmpty());

    QVERIFY(compareTree(testRewriterView1->rootModelNode(), testRewriterView2->rootModelNode()));
}

void TestCore::testModelCreateRect()
{
     try {

    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    QVERIFY(view->rootModelNode().isValid());
380
    ModelNode childNode = addNodeListChild(view->rootModelNode(), "Qt/Rectangle", 4, 7, "data");
381
382
383
384
385
386
387
    QVERIFY(childNode.isValid());
    QVERIFY(view->rootModelNode().allDirectSubModelNodes().contains(childNode));
    QVERIFY(childNode.parentProperty().parentModelNode() == view->rootModelNode());
    QCOMPARE(childNode.simplifiedTypeName(), QString("Rectangle"));

    QVERIFY(childNode.id().isEmpty());

388
389
    childNode.setId("rect01");
    QCOMPARE(childNode.id(), QString("rect01"));
390
391
392
393
394

    childNode.variantProperty("x") = 100;
    childNode.variantProperty("y") = 100;
    childNode.variantProperty("width") = 100;
    childNode.variantProperty("height") = 100;
395

396
397
398
399
400
401
402
403
404
    QCOMPARE(childNode.propertyNames().count(), 4);
    QCOMPARE(childNode.variantProperty("scale").value(), QVariant());

     } catch (Exception &exception) {
        QFAIL("Exception thrown");
    }

}

405
void TestCore::testRewriterDynamicProperties()
406
{
407
    const QLatin1String qmlString("\n"
408
                                  "import Qt 4.7\n"
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
                                  "\n"
                                  "Rectangle {\n"
                                  "  property int i\n"
                                  "  property int ii: 1\n"
                                  "  property bool b\n"
                                  "  property bool bb: true\n"
                                  "  property double d\n"
                                  "  property double dd: 1.1\n"
                                  "  property real r\n"
                                  "  property real rr: 1.1\n"
                                  "  property string s\n"
                                  "  property string ss: \"hello\"\n"
                                  "  property url u\n"
                                  "  property url uu: \"www\"\n"
                                  "  property color c\n"
                                  "  property color cc: \"#ffffff\"\n"
                                  "  property date t\n"
                                  "  property date tt: \"2000-03-20\"\n"
Kai Koehne's avatar
Kai Koehne committed
427
428
                                  "  property variant v\n"
                                  "  property variant vv: \"Hello\"\n"
429
                                  "}");
430
431

    QPlainTextEdit textEdit1;
432
    textEdit1.setPlainText(qmlString);
433
    NotIndentingTextEditModifier modifier1(&textEdit1);
434
435
436
437
438
439
440

    QScopedPointer<Model> model1(Model::create("Qt/Item"));

    QScopedPointer<TestRewriterView> testRewriterView1(new TestRewriterView());
    testRewriterView1->setTextModifier(&modifier1);
    model1->attachView(testRewriterView1.data());

441
    QVERIFY(testRewriterView1->errors().isEmpty());
442

443
444
445
446
    //
    // text2model
    //
    ModelNode rootModelNode = testRewriterView1->rootModelNode();
Marco Bubke's avatar
Marco Bubke committed
447
    QCOMPARE(rootModelNode.properties().count(), 18);
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
    QVERIFY(rootModelNode.hasVariantProperty("i"));
    QCOMPARE(rootModelNode.variantProperty("i").dynamicTypeName(), QString("int"));
    QCOMPARE(rootModelNode.variantProperty("i").value().type(), QVariant::Int);
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("i").value().toInt(), 0);

    QVERIFY(rootModelNode.hasVariantProperty("ii"));
    QCOMPARE(rootModelNode.variantProperty("ii").dynamicTypeName(), QString("int"));
    QCOMPARE(rootModelNode.variantProperty("ii").value().type(), QVariant::Int);
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("ii").value().toInt(), 1);

    QVERIFY(rootModelNode.hasVariantProperty("b"));
    QCOMPARE(rootModelNode.variantProperty("b").dynamicTypeName(), QString("bool"));
    QCOMPARE(rootModelNode.variantProperty("b").value().type(), QVariant::Bool);
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("b").value().toBool(), false);

    QVERIFY(rootModelNode.hasVariantProperty("bb"));
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("bb").value().toBool(), true);

    QVERIFY(rootModelNode.hasVariantProperty("d"));
    QCOMPARE(rootModelNode.variantProperty("d").dynamicTypeName(), QString("double"));
    QCOMPARE(rootModelNode.variantProperty("d").value().type(), QVariant::Double);
469
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("d").value().toDouble(), 0.0);
470
471
472
473
474

    QVERIFY(rootModelNode.hasVariantProperty("dd"));
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("dd").value().toDouble(), 1.1);

    QVERIFY(rootModelNode.hasVariantProperty("r"));
475
    QCOMPARE(rootModelNode.variantProperty("r").dynamicTypeName(), QString("real"));
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
    QCOMPARE(rootModelNode.variantProperty("r").value().type(), QVariant::Double);
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("r").value().toDouble(), 0.0);

    QVERIFY(rootModelNode.hasVariantProperty("rr"));
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("rr").value().toDouble(), 1.1);

    QVERIFY(rootModelNode.hasVariantProperty("s"));
    QCOMPARE(rootModelNode.variantProperty("s").dynamicTypeName(), QString("string"));
    QCOMPARE(rootModelNode.variantProperty("s").value().type(), QVariant::String);
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("s").value().toString(), QString());

    QVERIFY(rootModelNode.hasVariantProperty("ss"));
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("ss").value().toString(), QString("hello"));

    QVERIFY(rootModelNode.hasVariantProperty("u"));
    QCOMPARE(rootModelNode.variantProperty("u").dynamicTypeName(), QString("url"));
    QCOMPARE(rootModelNode.variantProperty("u").value().type(), QVariant::Url);
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("u").value().toUrl(), QUrl());

    QVERIFY(rootModelNode.hasVariantProperty("uu"));
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("uu").value().toUrl(), QUrl("www"));

    QVERIFY(rootModelNode.hasVariantProperty("c"));
    QCOMPARE(rootModelNode.variantProperty("c").dynamicTypeName(), QString("color"));
    QCOMPARE(rootModelNode.variantProperty("c").value().type(), QVariant::Color);
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("c").value().value<QColor>(), QColor());

    QVERIFY(rootModelNode.hasVariantProperty("cc"));
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("cc").value().value<QColor>(), QColor(255, 255, 255));

    QVERIFY(rootModelNode.hasVariantProperty("t"));
    QCOMPARE(rootModelNode.variantProperty("t").dynamicTypeName(), QString("date"));
508
    QCOMPARE(rootModelNode.variantProperty("t").value().type(), QVariant::Date);
509
510
511
512
513
514
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("t").value().value<QDate>(), QDate());

    QVERIFY(rootModelNode.hasVariantProperty("tt"));
    QCOMPARE(testRewriterView1->rootModelNode().variantProperty("tt").value().value<QDate>(), QDate(2000, 3, 20));

    QVERIFY(rootModelNode.hasVariantProperty("v"));
Kai Koehne's avatar
Kai Koehne committed
515
    QCOMPARE(rootModelNode.variantProperty("v").dynamicTypeName(), QString("variant"));
516
517
    const int type = rootModelNode.variantProperty("v").value().type();
    QCOMPARE(type, QMetaType::type("QVariant"));
518
519

    QVERIFY(rootModelNode.hasVariantProperty("vv"));
520
521
    const QString inThere = testRewriterView1->rootModelNode().variantProperty("vv").value().value<QString>();
    QCOMPARE(inThere, QString("Hello"));
522
523
524

    // test model2text
//    QPlainTextEdit textEdit2;
525
//    textEdit2.setPlainText("import Qt 4.7; Item{}");
526
527
528
529
530
531
532
533
534
535
536
//    NotIndentingTextEditModifier modifier2(&textEdit2);
//
//    QScopedPointer<Model> model2(Model::create("Qt/Item"));
//
//    QScopedPointer<TestRewriterView> testRewriterView2(new TestRewriterView());
//    testRewriterView2->setTextModifier(&modifier2);
//    model2->attachView(testRewriterView2.data());
//
//    testRewriterView2->rootModelNode().variantProperty("pushed").setDynamicTypeNameAndValue("bool", QVariant(false));
//
//    QVERIFY(compareTree(testRewriterView1->rootModelNode(), testRewriterView2->rootModelNode()));
537
538
}

539
540
541
void TestCore::testRewriterGroupedProperties()
{
    const QLatin1String qmlString("\n"
542
                                  "import Qt 4.7\n"
543
544
545
546
547
548
                                  "\n"
                                  "Text {\n"
                                  "  font {\n"
                                  "    pointSize: 10\n"
                                  "    underline: true\n"
                                  "  }\n"
549
                                  "}\n");
550

551
552
553
    QPlainTextEdit textEdit;
    textEdit.setPlainText(qmlString);
    NotIndentingTextEditModifier modifier1(&textEdit);
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568

    QScopedPointer<Model> model1(Model::create("Qt/Text"));

    QScopedPointer<TestRewriterView> testRewriterView1(new TestRewriterView());
    testRewriterView1->setTextModifier(&modifier1);
    model1->attachView(testRewriterView1.data());

    QVERIFY(testRewriterView1->errors().isEmpty());

    //
    // text2model
    //
    ModelNode rootModelNode = testRewriterView1->rootModelNode();
    QCOMPARE(rootModelNode.property(QLatin1String("font.pointSize")).toVariantProperty().value().toDouble(), 10.0);
    QCOMPARE(rootModelNode.property(QLatin1String("font.underline")).toVariantProperty().value().toBool(), true);
569
570
571
572
573
574
575
576
577
578

    rootModelNode.removeProperty(QLatin1String("font.underline"));
    QCOMPARE(rootModelNode.property(QLatin1String("font.pointSize")).toVariantProperty().value().toDouble(), 10.0);
    QVERIFY(!rootModelNode.hasProperty(QLatin1String("font.underline")));

    rootModelNode.variantProperty(QLatin1String("font.pointSize")).setValue(20.0);
    QCOMPARE(rootModelNode.property(QLatin1String("font.pointSize")).toVariantProperty().value().toDouble(), 20.0);

    rootModelNode.removeProperty(QLatin1String("font.pointSize"));
    const QLatin1String expected("\n"
579
                                 "import Qt 4.7\n"
580
581
582
583
584
                                 "\n"
                                 "Text {\n"
                                 "}\n");

    QCOMPARE(textEdit.toPlainText(), expected);
585
586
}

587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
void TestCore::testRewriterPreserveOrder()
{
    const QLatin1String qmlString1("\n"
        "import Qt 4.7\n"
        "\n"
        "Rectangle {\n"
            "width: 640\n"
            "height: 480\n"
            "\n"
            "states: [\n"
                "State {\n"
                    "name: \"State1\"\n"
                "}\n"
            "]\n"
            "\n"
            "Rectangle {\n"
                "id: rectangle1\n"
                "x: 18\n"
                "y: 19\n"
                "width: 100\n"
                "height: 100\n"
            "}\n"
        "}\n");
    const QLatin1String qmlString2("\n"
        "import Qt 4.7\n"
        "\n"
        "Rectangle {\n"
            "width: 640\n"
            "height: 480\n"
            "\n"
            "Rectangle {\n"
                "id: rectangle1\n"
                "x: 18\n"
                "y: 19\n"
                "width: 100\n"
                "height: 100\n"
            "}\n"
            "states: [\n"
                "State {\n"
                    "name: \"State1\"\n"
                "}\n"
            "]\n"
            "\n"
        "}\n");

        {
        QPlainTextEdit textEdit;
        textEdit.setPlainText(qmlString2);
        NotIndentingTextEditModifier modifier(&textEdit);

        QScopedPointer<Model> model(Model::create("Qt/Text"));

        QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView());
        testRewriterView->setTextModifier(&modifier);
        model->attachView(testRewriterView.data());

        QVERIFY(testRewriterView->errors().isEmpty());

        ModelNode rootModelNode = testRewriterView->rootModelNode();
        QVERIFY(rootModelNode.isValid());

        RewriterTransaction transaction = testRewriterView->beginRewriterTransaction();

        ModelNode newModelNode = testRewriterView->createModelNode("Qt/Rectangle", 4, 7);

        newModelNode.setParentProperty(rootModelNode.nodeAbstractProperty("data"));

        newModelNode.setId("rectangle2");
        QCOMPARE(newModelNode.id(), QString("rectangle2"));

        newModelNode.variantProperty("x") = 10;
        newModelNode.variantProperty("y") = 10;
        newModelNode.variantProperty("width") = 100;
        newModelNode.variantProperty("height") = 100;

        transaction.commit();

        QCOMPARE(newModelNode.id(), QString("rectangle2"));
    }

    {
        QPlainTextEdit textEdit;
        textEdit.setPlainText(qmlString1);
        NotIndentingTextEditModifier modifier(&textEdit);

        QScopedPointer<Model> model(Model::create("Qt/Text"));

        QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView());
        testRewriterView->setTextModifier(&modifier);
        model->attachView(testRewriterView.data());

        QVERIFY(testRewriterView->errors().isEmpty());

        ModelNode rootModelNode = testRewriterView->rootModelNode();
        QVERIFY(rootModelNode.isValid());

        RewriterTransaction transaction = testRewriterView->beginRewriterTransaction();

        ModelNode newModelNode = testRewriterView->createModelNode("Qt/Rectangle", 4, 7);

        newModelNode.setParentProperty(rootModelNode.nodeAbstractProperty("data"));

        newModelNode.setId("rectangle2");
        QCOMPARE(newModelNode.id(), QString("rectangle2"));

        newModelNode.variantProperty("x") = 10;
        newModelNode.variantProperty("y") = 10;
        newModelNode.variantProperty("width") = 100;
        newModelNode.variantProperty("height") = 100;

        transaction.commit();

        QCOMPARE(newModelNode.id(), QString("rectangle2"));
    }
}

703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
void TestCore::testRewriterForGradientMagic()
{
    const QLatin1String qmlString("\n"
                                  "import Qt 4.7\n"
                                  "\n"
                                  "Rectangle {\n"
                                  "    id: root\n"
                                  "    x: 10;\n"
                                  "    y: 10;\n"
                                  "    Rectangle {\n"
                                  "        id: rectangle1\n"
                                  "        x: 10;\n"
                                  "        y: 10;\n"
                                  "    }\n"
                                  "    Rectangle {\n"
                                  "        id: rectangle2\n"
                                  "        x: 100;\n"
                                  "        y: 100;\n"
                                  "        anchors.fill: root\n"
                                  "    }\n"
                                  "    Rectangle {\n"
                                  "        id: rectangle3\n"
                                  "        x: 140;\n"
                                  "        y: 180;\n"
                                  "        gradient: Gradient {\n"
                                  "             GradientStop {\n"
                                  "                 position: 0\n"
                                  "                 color: \"white\"\n"
                                  "             }\n"
                                  "             GradientStop {\n"
                                  "                  position: 1\n"
                                  "                  color: \"black\"\n"
                                  "             }\n"
                                  "        }\n"
                                  "    }\n"
                                  "}");

Thomas Hartmann's avatar
Thomas Hartmann committed
740
741
    QSKIP("See BAUHAUS-674", SkipAll);

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
    QPlainTextEdit textEdit;
    textEdit.setPlainText(qmlString);
    NotIndentingTextEditModifier modifier(&textEdit);

    QScopedPointer<Model> model(Model::create("Qt/Text"));

    QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView());
    testRewriterView->setTextModifier(&modifier);
    model->attachView(testRewriterView.data());

    QVERIFY(testRewriterView->errors().isEmpty());

    ModelNode rootModelNode = testRewriterView->rootModelNode();
    QVERIFY(rootModelNode.isValid());

    ModelNode myRect = testRewriterView->modelNodeForId(QLatin1String("rectangle3"));
    QVERIFY(myRect.isValid());
    myRect.variantProperty("rotation") = QVariant(45);
    QVERIFY(myRect.isValid());

    QScopedPointer<Model> model1(Model::create("Qt/Item", 4, 7));
    QVERIFY(model1.data());

    QScopedPointer<TestView> view1(new TestView);
    model1->attachView(view1.data());

    QScopedPointer<TestRewriterView> testRewriterView1(new TestRewriterView());
    QPlainTextEdit textEdit1;
    textEdit1.setPlainText("import Qt 4.7; Item {}");
    NotIndentingTextEditModifier modifier1(&textEdit1);

    testRewriterView1->setTextModifier(&modifier1);
    model1->attachView(testRewriterView1.data());

    QVERIFY(testRewriterView1->errors().isEmpty());

    RewriterTransaction transaction(view1->beginRewriterTransaction());

    ModelMerger merger(view1.data());

    QVERIFY(myRect.isValid());
    merger.replaceModel(myRect);
    transaction.commit();
}

787
788
void TestCore::loadSubItems()
{
789
    QFile file(QString(QTCREATORDIR) + "/tests/auto/qml/qmldesigner/data/fx/topitem.qml");
790
791
792
793
    QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));

    QPlainTextEdit textEdit1;
    textEdit1.setPlainText(file.readAll());
794
    NotIndentingTextEditModifier modifier1(&textEdit1);
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825

    QScopedPointer<Model> model1(Model::create("Qt/Item"));

    QScopedPointer<TestRewriterView> testRewriterView1(new TestRewriterView());
    testRewriterView1->setTextModifier(&modifier1);
    model1->attachView(testRewriterView1.data());
}

void TestCore::createInvalidCoreModel()
{
    QScopedPointer<Model> invalidModel(Model::create("ItemSUX"));
    //QVERIFY(!invalidModel.data()); //#no direct ype checking in model atm

    QScopedPointer<Model> invalidModel2(Model::create("InvalidNode"));
    //QVERIFY(!invalidModel2.data());
}

void TestCore::testModelCreateSubNode()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    QList<TestView::MethodCall> expectedCalls;
    expectedCalls << TestView::MethodCall("modelAttached", QStringList() << QString::number(reinterpret_cast<long>(model.data())));
    QCOMPARE(view->methodCalls(), expectedCalls);

    QVERIFY(view->rootModelNode().isValid());
826
    ModelNode childNode = addNodeListChild(view->rootModelNode(), "Qt/Rectangle", 4, 7, "data");
827
828
829
830
831
832
833
834
835
836
    QVERIFY(childNode.isValid());
    QVERIFY(view->rootModelNode().allDirectSubModelNodes().contains(childNode));
    QVERIFY(childNode.parentProperty().parentModelNode() == view->rootModelNode());
    QCOMPARE(childNode.simplifiedTypeName(), QString("Rectangle"));

    expectedCalls << TestView::MethodCall("nodeCreated", QStringList() << "");
    expectedCalls << TestView::MethodCall("nodeReparented", QStringList() << "" << "data" << "" << "PropertiesAdded");
    QCOMPARE(view->methodCalls(), expectedCalls);

    QVERIFY(childNode.id().isEmpty());
837
838
    childNode.setId("blah");
    QCOMPARE(childNode.id(), QString("blah"));
839

840
    expectedCalls << TestView::MethodCall("nodeIdChanged", QStringList() << "blah" << "blah" << "");
841
842
843
844
845
846
847
848
849
    QCOMPARE(view->methodCalls(), expectedCalls);

    try {
        childNode.setId("invalid id");
        QFAIL("Setting an invalid id does not throw an excxeption");
    } catch (Exception &exception) {
        QCOMPARE(exception.type(), QString("InvalidIdException"));
    }

850
    QCOMPARE(childNode.id(), QString("blah"));
851
    QCOMPARE(view->methodCalls(), expectedCalls);
852
853
854

    childNode.setId(QString());
    QVERIFY(childNode.id().isEmpty());
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
}


void TestCore::testTypicalRewriterOperations()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    ModelNode rootModelNode = view->rootModelNode();
    QCOMPARE(rootModelNode.allDirectSubModelNodes().count(), 0);

    QVERIFY(rootModelNode.property("test").isValid());
    QVERIFY(!rootModelNode.property("test").isVariantProperty());
    QVERIFY(!rootModelNode.property("test").isBindingProperty());

    QVERIFY(rootModelNode.variantProperty("test").isValid());
    QVERIFY(!rootModelNode.hasProperty("test"));

    rootModelNode.variantProperty("test") = 70;

    QVERIFY(rootModelNode.hasProperty("test"));
    QVERIFY(rootModelNode.property("test").isVariantProperty());
    QCOMPARE(rootModelNode.variantProperty("test").value(), QVariant(70));

    rootModelNode.bindingProperty("test") = "parent.x";
    QVERIFY(!rootModelNode.property("test").isVariantProperty());
    QVERIFY(rootModelNode.property("test").isBindingProperty());

    QCOMPARE(rootModelNode.bindingProperty("test").expression(), QString("parent.x"));

Erik Verbruggen's avatar
Erik Verbruggen committed
889
    ModelNode childNode(addNodeListChild(rootModelNode, "Qt/Rectangle", 4 ,6, "data"));
890
891
892
893
894
    rootModelNode.nodeListProperty("test").reparentHere(childNode);
    QCOMPARE(childNode.parentProperty(), rootModelNode.nodeAbstractProperty("test"));
    QVERIFY(rootModelNode.property("test").isNodeAbstractProperty());
    QVERIFY(rootModelNode.property("test").isNodeListProperty());
    QVERIFY(!rootModelNode.property("test").isBindingProperty());
895
    QVERIFY(childNode.parentProperty().isNodeListProperty());
896
897
898
899
900
901
902
903
904
905
906
907
908
    QCOMPARE(childNode, childNode.parentProperty().toNodeListProperty().toModelNodeList().first());
    QCOMPARE(rootModelNode, childNode.parentProperty().parentModelNode());
    QCOMPARE(childNode.parentProperty().name(), QString("test"));

    QVERIFY(!rootModelNode.property("test").isVariantProperty());
    rootModelNode.variantProperty("test") = 90;
    QVERIFY(rootModelNode.property("test").isVariantProperty());
    QCOMPARE(rootModelNode.variantProperty("test").value(), QVariant(90));

}

void TestCore::testBasicStates()
{
909
    char qmlString[] = "import Qt 4.7\n"
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
                       "Rectangle {\n"
                       "id: root;\n"
                            "Rectangle {\n"
                                "id: rect1;\n"
                            "}\n"
                            "Rectangle {\n"
                                "id: rect2;\n"
                            "}\n"
                            "states: [\n"
                                "State {\n"
                                    "name: \"state1\"\n"
                                    "PropertyChanges {\n"
                                        "target: rect1\n"
                                    "}\n"
                                    "PropertyChanges {\n"
                                        "target: rect2\n"
                                    "}\n"
                                "}\n"
                                ","
                                "State {\n"
                                    "name: \"state2\"\n"
                                    "PropertyChanges {\n"
                                        "target: rect1\n"
                                    "}\n"
                                    "PropertyChanges {\n"
                                        "target: rect2;\n"
                                        "x: 10;\n"
                                    "}\n"
                                "}\n"
                            "]\n"
                       "}\n";

    Exception::setShouldAssert(true);

    QPlainTextEdit textEdit;
    textEdit.setPlainText(qmlString);
946
    NotIndentingTextEditModifier textModifier(&textEdit);
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014

    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    ModelNode rootModelNode(view->rootModelNode());
    QVERIFY(rootModelNode.isValid());
    QCOMPARE(rootModelNode.type(), QString("Qt/Item"));
    QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView());
    testRewriterView->setTextModifier(&textModifier);

    model->attachView(testRewriterView.data());
    QVERIFY(rootModelNode.isValid());
    QCOMPARE(rootModelNode.type(), QString("Qt/Rectangle"));

    QVERIFY(rootModelNode.hasProperty("data"));

    QVERIFY(rootModelNode.property("data").isNodeListProperty());

    QCOMPARE(rootModelNode.nodeListProperty("data").toModelNodeList().count(), 2);

    ModelNode rect1 = rootModelNode.nodeListProperty("data").toModelNodeList().first();
    ModelNode rect2 = rootModelNode.nodeListProperty("data").toModelNodeList().last();

    QVERIFY(QmlItemNode(rect1).isValid());
    QVERIFY(QmlItemNode(rect2).isValid());

    QVERIFY(QmlItemNode(rootModelNode).isValid());

    QCOMPARE(QmlItemNode(rootModelNode).states().allStates().count(), 2);
    QCOMPARE(QmlItemNode(rootModelNode).states().names().count(), 2);
    QCOMPARE(QmlItemNode(rootModelNode).states().names().first(), QString("state1"));
    QCOMPARE(QmlItemNode(rootModelNode).states().names().last(), QString("state2"));

    //
    // QmlModelState API tests
    //
    QmlModelState state1 = QmlItemNode(rootModelNode).states().state("state1");
    QmlModelState state2 = QmlItemNode(rootModelNode).states().state("state2");

    QVERIFY(state1.isValid());
    QVERIFY(state2.isValid());

    QCOMPARE(state1.propertyChanges().count(), 2);
    QCOMPARE(state2.propertyChanges().count(), 2);

    QVERIFY(!state1.hasPropertyChanges(rootModelNode));

    QVERIFY(state1.propertyChanges(rect1).isValid());
    QVERIFY(state1.propertyChanges(rect2).isValid());

    state1.propertyChanges(rect2).modelNode().hasProperty("x");

    QCOMPARE(QmlItemNode(rect1).allAffectingStates().count(), 2);
    QCOMPARE(QmlItemNode(rect2).allAffectingStates().count(), 2);
    QCOMPARE(QmlItemNode(rootModelNode).allAffectingStates().count(), 0);
    QCOMPARE(QmlItemNode(rect1).allAffectingStatesOperations().count(), 2);
    QCOMPARE(QmlItemNode(rect2).allAffectingStatesOperations().count(), 2);

    //
    // check real state2 object
    //

    NodeInstance state2Instance = view->instanceForModelNode(state2.modelNode());
    QVERIFY(state2Instance.isValid());
1015
1016
    QDeclarativeState *stateObject = qobject_cast<QDeclarativeState*>(const_cast<QObject*>(state2Instance.testHandle()));
    QDeclarativeListProperty<QDeclarativeStateOperation> changesList = stateObject->changes();
1017
1018
1019
    QCOMPARE(changesList.count(&changesList), 2);
    QCOMPARE(changesList.at(&changesList, 0)->actions().size(), 0);
    QCOMPARE(changesList.at(&changesList, 1)->actions().size(), 1);
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078


    //
    // actual state switching
    //

    // base state
    QCOMPARE(view->currentState(), view->baseState());
    NodeInstance rect2Instance = view->instanceForModelNode(rect2);
    QVERIFY(rect2Instance.isValid());
    QCOMPARE(rect2Instance.property("x").toInt(), 0);

    int expectedViewMethodCount = view->methodCalls().count();

    // base state-> state2
    view->setCurrentState(state2);
    QCOMPARE(view->currentState(), state2);
    QCOMPARE(view->methodCalls().size(), ++expectedViewMethodCount);
    QCOMPARE(view->methodCalls().last(), TestView::MethodCall("stateChanged", QStringList() << "state2" << QString()));
    QCOMPARE(rect2Instance.property("x").toInt(), 10);

    // state2 -> state1
    view->setCurrentState(state1);
    QCOMPARE(view->currentState(), state1);
    QCOMPARE(view->methodCalls().size(), ++expectedViewMethodCount);
    QCOMPARE(view->methodCalls().last(), TestView::MethodCall("stateChanged", QStringList() << "state1" << "state2"));
    QCOMPARE(rect2Instance.property("x").toInt(), 0);

    // state1 -> baseState
    view->setCurrentState(view->baseState());
    QCOMPARE(view->currentState(), view->baseState());
    QCOMPARE(view->methodCalls().size(), ++expectedViewMethodCount);
    QCOMPARE(view->methodCalls().last(), TestView::MethodCall("stateChanged", QStringList() << QString() << "state1"));
    QCOMPARE(rect2Instance.property("x").toInt(), 0);
}

void TestCore::testModelBasicOperations()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    ModelNode rootModelNode = view->rootModelNode();
    QCOMPARE(rootModelNode.allDirectSubModelNodes().count(), 0);

    rootModelNode.variantProperty("width").setValue(10);
    rootModelNode.variantProperty("height").setValue(10);

    QCOMPARE(rootModelNode.variantProperty("width").value().toInt(), 10);
    QCOMPARE(rootModelNode.variantProperty("height").value().toInt(), 10);

    QVERIFY(rootModelNode.hasProperty("width"));
    rootModelNode.removeProperty("width");
    QVERIFY(!rootModelNode.hasProperty("width"));

    QVERIFY(!rootModelNode.hasProperty("children"));
1079
1080
    ModelNode childNode1(addNodeListChild(rootModelNode, "Qt/Rectangle", 4, 7, "children"));
    ModelNode childNode2(addNodeListChild(rootModelNode, "Qt/Rectangle", 4, 7, "data"));
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127

    QVERIFY(childNode1.isValid());
    QVERIFY(childNode2.isValid());

    QVERIFY(childNode1.parentProperty().parentModelNode().isValid());
    QVERIFY(childNode2.parentProperty().parentModelNode().isValid());

    QVERIFY(rootModelNode.hasProperty("children"));
    childNode2.setParentProperty(rootModelNode, "children");
    QVERIFY(!rootModelNode.hasProperty("data"));
    QCOMPARE(childNode2.parentProperty().parentModelNode(), rootModelNode);
    QVERIFY(rootModelNode.hasProperty("children"));
    childNode2.setParentProperty(rootModelNode, "data");
    childNode1.setParentProperty(rootModelNode, "data");
    QCOMPARE(childNode2.parentProperty().parentModelNode(), rootModelNode);
    QVERIFY(!rootModelNode.hasProperty("children"));
    QVERIFY(rootModelNode.hasProperty("data"));

    QVERIFY(childNode2.isValid());
    QVERIFY(childNode2.parentProperty().parentModelNode().isValid());
    QCOMPARE(childNode2.parentProperty().parentModelNode(), rootModelNode);

    childNode1.setParentProperty(rootModelNode, "children");
    rootModelNode.removeProperty("data");
    QVERIFY(!childNode2.isValid());
    QVERIFY(!rootModelNode.hasProperty("data"));

    QVERIFY(childNode1.isValid());
    rootModelNode.nodeProperty("front").setModelNode(childNode1);
    QVERIFY(rootModelNode.hasProperty("front"));
    QCOMPARE(childNode1.parentProperty().parentModelNode(), rootModelNode);
    rootModelNode.removeProperty("front");
    QVERIFY(!childNode1.isValid());
}

void TestCore::testModelResolveIds()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    ModelNode rootNode = view->rootModelNode();
    rootNode.setId("rootNode");

1128
    ModelNode childNode1(addNodeListChild(rootNode, "Qt/Rectangle", 4, 7, "children"));
1129

1130
    ModelNode childNode2(addNodeListChild(childNode1, "Qt/Rectangle", 4, 7, "children"));
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
    childNode2.setId("childNode2");
    childNode2.bindingProperty("test").setExpression("parent.parent");

    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), rootNode);
    childNode1.setId("childNode1");
    childNode2.bindingProperty("test").setExpression("childNode1.parent");
    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), rootNode);
    childNode2.bindingProperty("test").setExpression("rootNode");
    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), rootNode);

1141
    ModelNode childNode3(addNodeListChild(childNode2, "Qt/Rectangle", 4, 7, "children"));
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
    childNode3.setId("childNode3");
    childNode2.nodeProperty("front").setModelNode(childNode3);
    childNode2.bindingProperty("test").setExpression("childNode3.parent");
    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), childNode2);
    childNode2.bindingProperty("test").setExpression("childNode3.parent.parent");
    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), childNode1);
    childNode2.bindingProperty("test").setExpression("childNode3.parent.parent.parent");
    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), rootNode);
    childNode2.bindingProperty("test").setExpression("childNode3");
    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), childNode3);
    childNode2.bindingProperty("test").setExpression("front");
    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), childNode3);
    childNode2.bindingProperty("test").setExpression("back");
    QVERIFY(!childNode2.bindingProperty("test").resolveToModelNode().isValid());
    childNode2.bindingProperty("test").setExpression("childNode3.parent.front");
    QCOMPARE(childNode2.bindingProperty("test").resolveToModelNode(), childNode3);

    childNode2.variantProperty("x") = 10;
    QCOMPARE(childNode2.variantProperty("x").value().toInt(), 10);

    childNode2.bindingProperty("test").setExpression("childNode3.parent.x");
    QVERIFY(childNode2.bindingProperty("test").resolveToProperty().isVariantProperty());
    QCOMPARE(childNode2.bindingProperty("test").resolveToProperty().toVariantProperty().value().toInt(), 10);

    childNode2.bindingProperty("test").setExpression("childNode3.parent.test");
    QVERIFY(childNode2.bindingProperty("test").resolveToProperty().isBindingProperty());
    QCOMPARE(childNode2.bindingProperty("test").resolveToProperty().toBindingProperty().expression(), QString("childNode3.parent.test"));
}

void TestCore::testModelNodeListProperty()
{
    //
    // Test NodeListProperty API
    //
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    ModelNode rootNode = view->rootModelNode();

    //
    // Item {}
    //
    NodeListProperty rootChildren = rootNode.nodeListProperty("children");
    QVERIFY(!rootNode.hasProperty("children"));
    QVERIFY(rootChildren.isValid());
    QVERIFY(!rootChildren.isNodeListProperty());
    QVERIFY(rootChildren.isEmpty());

1194
    ModelNode rectNode = view->createModelNode("Qt/Rectangle", 4, 7);
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
    rootChildren.reparentHere(rectNode);

    //
    // Item { children: [ Rectangle {} ] }
    //
    QVERIFY(rootNode.hasProperty("children"));
    QVERIFY(rootChildren.isValid());
    QVERIFY(rootChildren.isNodeListProperty());
    QVERIFY(!rootChildren.isEmpty());

1205
    ModelNode mouseAreaNode = view->createModelNode("Qt/Item", 4, 7);
1206
    NodeListProperty rectChildren = rectNode.nodeListProperty("children");
1207
    rectChildren.reparentHere(mouseAreaNode);
1208
1209

    //
1210
    // Item { children: [ Rectangle { children : [ MouseArea {} ] } ] }
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
    //
    QVERIFY(rectNode.hasProperty("children"));
    QVERIFY(rectChildren.isValid());
    QVERIFY(rectChildren.isNodeListProperty());
    QVERIFY(!rectChildren.isEmpty());

    rectNode.destroy();

    //
    // Item { }
    //
    QVERIFY(!rectNode.isValid());
1223
    QVERIFY(!mouseAreaNode.isValid());
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
    QVERIFY(!rootNode.hasProperty("children"));
    QVERIFY(rootChildren.isValid());
    QVERIFY(!rootChildren.isNodeListProperty());
    QVERIFY(rootChildren.isEmpty());
    QVERIFY(!rectChildren.isValid());
}

void TestCore::testBasicOperationsWithView()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    NodeInstanceView *nodeInstanceView = new NodeInstanceView(model.data());
    model->attachView(nodeInstanceView);

    ModelNode rootModelNode = view->rootModelNode();
    QCOMPARE(rootModelNode.allDirectSubModelNodes().count(), 0);
    NodeInstance rootInstance = nodeInstanceView->instanceForNode(rootModelNode);

1247
1248
1249
    // no width, height specified implicitly is set to 100x100
    QCOMPARE(rootInstance.size().width(), 100.0);
    QCOMPARE(rootInstance.size().height(), 100.0);
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264

    QVERIFY(rootInstance.isValid());
    QVERIFY(rootInstance.isQmlGraphicsItem());

    QVERIFY(rootModelNode.isValid());

    rootModelNode.variantProperty("width").setValue(10);
    rootModelNode.variantProperty("height").setValue(10);

    QCOMPARE(rootModelNode.variantProperty("width").value().toInt(), 10);
    QCOMPARE(rootModelNode.variantProperty("height").value().toInt(), 10);

    QCOMPARE(rootInstance.size().width(), 10.0);
    QCOMPARE(rootInstance.size().height(), 10.0);

1265
1266
    ModelNode childNode(addNodeListChild(rootModelNode, "Qt/Rectangle", 4, 7, "data"));
    ModelNode childNode2(addNodeListChild(childNode, "Qt/Rectangle", 4, 7, "data"));
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
    QVERIFY(childNode2.parentProperty().parentModelNode() == childNode);

    QVERIFY(childNode.isValid());

    {
        NodeInstance childInstance2 = nodeInstanceView->instanceForNode(childNode2);
        NodeInstance childInstance = nodeInstanceView->instanceForNode(childNode);

        QVERIFY(childInstance.isValid());
        QVERIFY(childInstance.isQmlGraphicsItem());
1277
1278
        QVERIFY(qobject_cast<QGraphicsObject*>(childInstance2.testHandle())->parentItem()->toGraphicsObject() == childInstance.testHandle());
        QVERIFY(qobject_cast<QGraphicsObject*>(childInstance.testHandle())->parentItem()->toGraphicsObject() == rootInstance.testHandle());
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
        QCOMPARE(childInstance.size().width(), 0.0);
        QCOMPARE(childInstance.size().height(), 0.0);


        childNode.variantProperty("width").setValue(100);
        childNode.variantProperty("height").setValue(100);

        QCOMPARE(childNode.variantProperty("width").value().toInt(), 100);
        QCOMPARE(childNode.variantProperty("height").value().toInt(), 100);

        QCOMPARE(childInstance.size().width(), 100.0);
        QCOMPARE(childInstance.size().height(), 100.0);

        childNode.destroy();
        QVERIFY(!childNode.isValid());
        QVERIFY(!childNode2.isValid());
        QVERIFY(childInstance.testHandle() == 0);
        QVERIFY(childInstance2.testHandle() == 0);
        QVERIFY(!childInstance.isValid());
        QVERIFY(!childInstance2.isValid());
    }

1301
    childNode = addNodeListChild(rootModelNode, "Qt/Image", 4, 7, "data");
1302
1303
    QVERIFY(childNode.isValid());
    QCOMPARE(childNode.type(), QString("Qt/Image"));
1304
    childNode2 = addNodeListChild(childNode, "Qt/Rectangle", 4, 7, "data");
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
    QVERIFY(childNode2.isValid());
    childNode2.setParentProperty(rootModelNode, "data");
    QVERIFY(childNode2.isValid());

    {
        NodeInstance childInstance2 = nodeInstanceView->instanceForNode(childNode2);
        NodeInstance childInstance = nodeInstanceView->instanceForNode(childNode);

        QVERIFY(childInstance.isValid());
        QVERIFY(childInstance.isQmlGraphicsItem());
1315
1316
        QVERIFY(qobject_cast<QGraphicsObject*>(childInstance2.testHandle())->parentItem()->toGraphicsObject() == rootInstance.testHandle());
        QVERIFY(qobject_cast<QGraphicsObject*>(childInstance.testHandle())->parentItem()->toGraphicsObject() == rootInstance.testHandle());
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
        QCOMPARE(childInstance.size().width(), 0.0);
        QCOMPARE(childInstance.size().height(), 0.0);

        QCOMPARE(rootModelNode, childNode2.parentProperty().parentModelNode());

        childNode.variantProperty("width").setValue(20);
        QCOMPARE(childInstance.property("width").toInt(), 20);

        QCOMPARE(childNode.variantProperty("width").value().toInt(), 20);
        childNode.removeProperty("width");
        QVERIFY(!childNode.hasProperty("width"));

        rootModelNode.removeProperty("data");
        QVERIFY(!childNode.isValid());
        QVERIFY(!childNode2.isValid());
        QVERIFY(childInstance.testHandle() == 0);
        QVERIFY(childInstance2.testHandle() == 0);
        QVERIFY(!childInstance.isValid());
        QVERIFY(!childInstance2.isValid());
    }

    model->detachView(nodeInstanceView);
}

void TestCore::testQmlModelView()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QmlModelView *view = new TestView(model.data());
    QVERIFY(view);
    model->attachView(view);
    QVERIFY(view->model());
    QVERIFY(view->rootQmlObjectNode().isValid());
    QVERIFY(!view->rootQmlObjectNode().hasNodeParent());
    QVERIFY(!view->rootQmlObjectNode().hasInstanceParent());
    QVERIFY(view->rootQmlObjectNode().isRootModelNode());

    PropertyListType propertyList;
    propertyList.append(qMakePair(QString("x"), QVariant(20)));
    propertyList.append(qMakePair(QString("y"), QVariant(20)));
    propertyList.append(qMakePair(QString("width"), QVariant(20)));
    propertyList.append(qMakePair(QString("height"), QVariant(20)));

1361
    QmlObjectNode node1 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376

    QVERIFY(node1.isValid());
    QVERIFY(!node1.hasNodeParent());
    QVERIFY(!node1.hasInstanceParent());

    QCOMPARE(node1.instanceValue("x").toInt(), 20);
    QCOMPARE(node1.instanceValue("y").toInt(), 20);

    node1.setParentProperty(view->rootQmlObjectNode().nodeAbstractProperty("children"));

    QVERIFY(node1.hasNodeParent());
    QVERIFY(node1.hasInstanceParent());
    QVERIFY(node1.instanceParent() == view->rootQmlObjectNode());


1377
    QmlObjectNode node2 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404

    QVERIFY(node2.isValid());
    QVERIFY(!node2.hasNodeParent());
    QVERIFY(!node2.hasInstanceParent());

    node2.setParentProperty(view->rootQmlObjectNode().nodeAbstractProperty("children"));

    QVERIFY(node2.hasNodeParent());
    QVERIFY(node2.hasInstanceParent());
    QVERIFY(node2.instanceParent() == view->rootQmlObjectNode());


    node2.setParentProperty(node1.nodeAbstractProperty("children"));

    QVERIFY(node2.hasNodeParent());
    QVERIFY(node2.hasInstanceParent());
    QVERIFY(node2.instanceParent() == node1);

        node2.setParentProperty(view->rootQmlObjectNode().nodeAbstractProperty("children"));

    QCOMPARE(node1.instanceValue("x").toInt(), 20);

    node1.setVariantProperty("x", 2);

    QCOMPARE(node1.instanceValue("x").toInt(), 2);


1405
1406
1407
1408
1409
1410
    QmlObjectNode node3 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
    QmlObjectNode node4 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
    QmlObjectNode node5 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
    QmlObjectNode node6 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
    QmlObjectNode node7 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
    QmlObjectNode node8 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437

    node3.setParentProperty(node2.nodeAbstractProperty("children"));
    node4.setParentProperty(node3.nodeAbstractProperty("children"));
    node5.setParentProperty(node4.nodeAbstractProperty("children"));
    node6.setParentProperty(node5.nodeAbstractProperty("children"));
    node7.setParentProperty(node6.nodeAbstractProperty("children"));
    node8.setParentProperty(node7.nodeAbstractProperty("children"));

    QCOMPARE(node2.nodeAbstractProperty("children").allSubNodes().count(), 6);
    QVERIFY(node8.isValid());
    node3.destroy();
    QVERIFY(!node8.isValid());
    QCOMPARE(node2.nodeAbstractProperty("children").allSubNodes().count(), 0);

    node1.setId("node1");

    node2.setVariantProperty("x", 20);
    node2.setBindingProperty("x", "node1.x");
    QCOMPARE(node2.instanceValue("x").toInt(), 2);
    node1.setVariantProperty("x", 10);
    QCOMPARE(node2.instanceValue("x").toInt(), 10);

    node1.destroy();
    QVERIFY(!node1.isValid());

    QCOMPARE(node2.instanceValue("x").toInt(), 10); // is this right? or should it be a invalid qvariant?

1438
    node1 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
1439
1440
1441
1442
    node1.setId("node1");

    QCOMPARE(node2.instanceValue("x").toInt(), 20);

1443
    node3 = view->createQmlObjectNode("Qt/Rectangle", 4, 7, propertyList);
1444
1445
1446
1447
1448
1449
    node3.setParentProperty(node2.nodeAbstractProperty("children"));
    QCOMPARE(node3.instanceValue("width").toInt(), 20);
    node3.setVariantProperty("width", 0);
    QCOMPARE(node3.instanceValue("width").toInt(), 0);

    QCOMPARE(node3.instanceValue("x").toInt(), 20);
1450
    QVERIFY(!QDeclarativeMetaType::toQObject(node3.instanceValue("anchors.fill")));
1451
    node3.setBindingProperty("anchors.fill", "parent");
1452
    QVERIFY(QDeclarativeMetaType::toQObject(node3.instanceValue("anchors.fill")));
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
    QCOMPARE(node3.instanceValue("x").toInt(), 0);
    QCOMPARE(node3.instanceValue("width").toInt(), 20);

    node3.setParentProperty(node1.nodeAbstractProperty("children"));
    node1.setVariantProperty("width", 50);
    node2.setVariantProperty("width", 100);
    QCOMPARE(node3.instanceValue("width").toInt(), 50);

}

void TestCore::testModelRemoveNode()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    NodeInstanceView *nodeInstanceView = new NodeInstanceView(model.data());
    model->attachView(nodeInstanceView);

    QCOMPARE(view->rootModelNode().allDirectSubModelNodes().count(), 0);


1478
    ModelNode childNode = addNodeListChild(view->rootModelNode(), "Qt/Rectangle", 4, 7, "data");
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
    QVERIFY(childNode.isValid());
    QCOMPARE(view->rootModelNode().allDirectSubModelNodes().count(), 1);
    QVERIFY(view->rootModelNode().allDirectSubModelNodes().contains(childNode));
    QVERIFY(childNode.parentProperty().parentModelNode() == view->rootModelNode());

    {
        NodeInstance childInstance = nodeInstanceView->instanceForNode(childNode);
        QVERIFY(childInstance.isValid());
        QVERIFY(childInstance.parent() == nodeInstanceView->instanceForNode(view->rootModelNode()));
    }

1490
    ModelNode subChildNode = addNodeListChild(childNode, "Qt/Rectangle", 4, 7, "data");
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
    QVERIFY(subChildNode.isValid());
    QCOMPARE(childNode.allDirectSubModelNodes().count(), 1);
    QVERIFY(childNode.allDirectSubModelNodes().contains(subChildNode));
    QVERIFY(subChildNode.parentProperty().parentModelNode() == childNode);

    {
        NodeInstance subChildInstance = nodeInstanceView->instanceForNode(subChildNode);
        QVERIFY(subChildInstance.isValid());
        QVERIFY(subChildInstance.parent() == nodeInstanceView->instanceForNode(childNode));
    }

    childNode.destroy();

    QVERIFY(!nodeInstanceView->hasInstanceForNode(childNode));

    QCOMPARE(view->rootModelNode().allDirectSubModelNodes().count(), 0);
    QVERIFY(!view->rootModelNode().allDirectSubModelNodes().contains(childNode));
    QVERIFY(!childNode.isValid());
    QVERIFY(!subChildNode.isValid());

    try {
        view->rootModelNode().destroy();
        QFAIL("remove the rootModelNode should be throw a exception");
    } catch (Exception &exception) {
        QCOMPARE(exception.type(), QString("InvalidArgumentException"));
    }

    QVERIFY(view->rootModelNode().isValid());

    // delete node not in hierarchy
1521
    childNode = view->createModelNode("Qt/Item", 4, 7);
1522
1523
1524
1525
1526
1527
1528
1529
    childNode.destroy();

    model->detachView(nodeInstanceView);
}

void TestCore::reparentingNode()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
1530

1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    ModelNode rootModelNode = view->rootModelNode();
    QVERIFY(rootModelNode.isValid());
    rootModelNode.setId("rootModelNode");
    QCOMPARE(rootModelNode.id(), QString("rootModelNode"));


    NodeInstanceView *nodeInstanceView = new NodeInstanceView(model.data());
    model->attachView(nodeInstanceView);

1546
    ModelNode childNode = addNodeListChild(rootModelNode, "Qt/Rectangle", 4, 7, "data");
1547
1548
1549
1550
1551
1552
1553
1554
1555
    QCOMPARE(childNode.parentProperty().parentModelNode(), rootModelNode);
    QVERIFY(rootModelNode.allDirectSubModelNodes().contains(childNode));

    {
        NodeInstance childInstance = nodeInstanceView->instanceForNode(childNode);
        QVERIFY(childInstance.isValid());
        QVERIFY(childInstance.parent() == nodeInstanceView->instanceForNode(view->rootModelNode()));
    }

1556
    ModelNode childNode2 = addNodeListChild(rootModelNode, "Qt/Item", 4, 7, "data");
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
    QCOMPARE(childNode2.parentProperty().parentModelNode(), rootModelNode);
    QVERIFY(rootModelNode.allDirectSubModelNodes().contains(childNode2));

    {
        NodeInstance childIstance2 = nodeInstanceView->instanceForNode(childNode2);
        QVERIFY(childIstance2.isValid());
        QVERIFY(childIstance2.parent() == nodeInstanceView->instanceForNode(view->rootModelNode()));
    }

    childNode.setParentProperty(childNode2, "data");

    QCOMPARE(childNode.parentProperty().parentModelNode(), childNode2);
    QVERIFY(childNode2.allDirectSubModelNodes().contains(childNode));
    QVERIFY(!rootModelNode.allDirectSubModelNodes().contains(childNode));
    QVERIFY(rootModelNode.allDirectSubModelNodes().contains(childNode2));

    {
        NodeInstance childIstance = nodeInstanceView->instanceForNode(childNode);
        QVERIFY(childIstance.isValid());
        QVERIFY(childIstance.parent() == nodeInstanceView->instanceForNode(childNode2));
    }

    childNode2.setParentProperty(rootModelNode, "data");
    QCOMPARE(childNode2.parentProperty().parentModelNode(), rootModelNode);
    QVERIFY(rootModelNode.allDirectSubModelNodes().contains(childNode2));

    {
        NodeInstance childIstance2 = nodeInstanceView->instanceForNode(childNode2);
        QVERIFY(childIstance2.isValid());
        QVERIFY(childIstance2.parent() == nodeInstanceView->instanceForNode(rootModelNode));
    }

    QCOMPARE(childNode.parentProperty().parentModelNode(), childNode2);

    model->detachView(nodeInstanceView);
}

void TestCore::reparentingNodeLikeDragAndDrop()
{
    QPlainTextEdit textEdit;
1597
    textEdit.setPlainText("import Qt 4.7;\n\nItem {\n}\n");
1598
    NotIndentingTextEditModifier textModifier(&textEdit);
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615

    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestRewriterView> testRewriterView(new TestRewriterView());
    testRewriterView->setTextModifier(&textModifier);
    model->attachView(testRewriterView.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    NodeInstanceView *nodeInstanceView = new NodeInstanceView(model.data());
    model->attachView(nodeInstanceView);
    view->rootModelNode().setId("rootModelNode");
    QCOMPARE(view->rootModelNode().id(), QString("rootModelNode"));

1616
    ModelNode rectNode = addNodeListChild(view->rootModelNode(), "Qt/Rectangle", 4, 7, "data");
1617
    rectNode.setId("rect_1");
1618
1619
1620
1621
1622
1623
1624
    rectNode.variantProperty("x").setValue(20);
    rectNode.variantProperty("y").setValue(30);
    rectNode.variantProperty("width").setValue(40);
    rectNode.variantProperty("height").setValue(50);

    RewriterTransaction transaction(view->beginRewriterTransaction());

1625
    ModelNode textNode = addNodeListChild(view->rootModelNode(), "Qt/Text", 4, 7, "data");
1626
1627
1628
    QCOMPARE(textNode.parentProperty().parentModelNode(), view->rootModelNode());
    QVERIFY(view->rootModelNode().allDirectSubModelNodes().contains(textNode));

1629
    textNode.setId("rext_1");
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
    textNode.variantProperty("x").setValue(10);
    textNode.variantProperty("y").setValue(10);
    textNode.variantProperty("width").setValue(50);
    textNode.variantProperty("height").setValue(20);

    textNode.variantProperty("x").setValue(30);
    textNode.variantProperty("y").setValue(30);

    {
        NodeInstance textInstance = nodeInstanceView->instanceForNode(textNode);
        QVERIFY(textInstance.isValid());
        QVERIFY(textInstance.parent() == nodeInstanceView->instanceForNode(view->rootModelNode()));
        QCOMPARE(textInstance.position().x(), 30.0);
        QCOMPARE(textInstance.position().y(), 30.0);
        QCOMPARE(textInstance.size().width(), 50.0);
        QCOMPARE(textInstance.size().height(), 20.0);
    }

    textNode.setParentProperty(rectNode, "data");
    QCOMPARE(textNode.parentProperty().parentModelNode(), rectNode);
    QVERIFY(rectNode.allDirectSubModelNodes().contains(textNode));

    {
        NodeInstance textInstance = nodeInstanceView->instanceForNode(textNode);
        QVERIFY(textInstance.isValid());
        QVERIFY(textInstance.parent() == nodeInstanceView->instanceForNode(rectNode));
        QCOMPARE(textInstance.position().x(), 30.0);
        QCOMPARE(textInstance.position().y(), 30.0);
        QCOMPARE(textInstance.size().width(), 50.0);
        QCOMPARE(textInstance.size().height(), 20.0);
    }

    textNode.setParentProperty(view->rootModelNode(), "data");
    QCOMPARE(textNode.parentProperty().parentModelNode(), view->rootModelNode());
    QVERIFY(view->rootModelNode().allDirectSubModelNodes().contains(textNode));

    {
        NodeInstance textInstance = nodeInstanceView->instanceForNode(textNode);
        QVERIFY(textInstance.isValid());
        QVERIFY(textInstance.parent() == nodeInstanceView->instanceForNode(view->rootModelNode()));
        QCOMPARE(textInstance.position().x(), 30.0);
        QCOMPARE(textInstance.position().y(), 30.0);
        QCOMPARE(textInstance.size().width(), 50.0);
        QCOMPARE(textInstance.size().height(), 20.0);
    }

    textNode.setParentProperty(rectNode, "data");
    QCOMPARE(textNode.parentProperty().parentModelNode(), rectNode);
    QVERIFY(rectNode.allDirectSubModelNodes().contains(textNode));

    {
        NodeInstance textInstance = nodeInstanceView->instanceForNode(textNode);
        QVERIFY(textInstance.isValid());
        QVERIFY(textInstance.parent() == nodeInstanceView->instanceForNode(rectNode));
        QCOMPARE(textInstance.position().x(), 30.0);
        QCOMPARE(textInstance.position().y(), 30.0);
        QCOMPARE(textInstance.size().width(), 50.0);
        QCOMPARE(textInstance.size().height(), 20.0);
    }

    {
        NodeInstance textInstance = nodeInstanceView->instanceForNode(textNode);
        QVERIFY(textInstance.isValid());
        QVERIFY(textInstance.parent() == nodeInstanceView->instanceForNode(rectNode));
        QCOMPARE(textInstance.position().x(), 30.0);
        QCOMPARE(textInstance.position().y(), 30.0);
        QCOMPARE(textInstance.size().width(), 50.0);
        QCOMPARE(textInstance.size().height(), 20.0);
    }

    transaction.commit();

    if(!textNode.isValid())
        QFAIL("textnode node is not valid anymore");
    else
        QCOMPARE(textNode.parentProperty().parentModelNode(), rectNode);
    QVERIFY(rectNode.allDirectSubModelNodes().contains(textNode));

    model->detachView(nodeInstanceView);
}

void TestCore::testModelReorderSiblings()
{
    QScopedPointer<Model> model(Model::create("Qt/Item"));
    QVERIFY(model.data());

    QScopedPointer<TestView> view(new TestView);
    QVERIFY(view.data());
    model->attachView(view.data());

    NodeInstanceView *nodeInstanceView = new NodeInstanceView(model.data());
    model->attachView(nodeInstanceView);

    ModelNode rootModelNode = view->rootModelNode();
    QVERIFY(rootModelNode.isValid());

1726
    ModelNode a = addNodeListChild(rootModelNode, "Qt/Rectangle", 4, 7,<