Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qquickanchors / tst_qquickanchors.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QSignalSpy>
43 #include <private/qquickitem_p.h>
44 #include <QtDeclarative/qdeclarativeengine.h>
45 #include <QtDeclarative/qdeclarativecomponent.h>
46 #include <QtQuick/qquickview.h>
47 #include <QtQuick/private/qquickrectangle_p.h>
48 #include <QtQuick/private/qquicktext_p.h>
49 #include <QtQuick/private/qquickanchors_p_p.h>
50 #include <QtQuick/private/qquickitem_p.h>
51 #include "../../shared/util.h"
52
53 Q_DECLARE_METATYPE(QQuickAnchors::Anchor)
54 Q_DECLARE_METATYPE(QQuickAnchorLine::AnchorLine)
55
56 class tst_qquickanchors : public QDeclarativeDataTest
57 {
58     Q_OBJECT
59 public:
60     tst_qquickanchors() {}
61
62 private slots:
63     void basicAnchors();
64     void basicAnchorsRTL();
65     void loops();
66     void illegalSets();
67     void illegalSets_data();
68     void reset();
69     void reset_data();
70     void resetConvenience();
71     void nullItem();
72     void nullItem_data();
73     void crash1();
74     void centerIn();
75     void centerInRTL();
76     void centerInRotation();
77     void hvCenter();
78     void hvCenterRTL();
79     void fill();
80     void fillRTL();
81     void margins();
82     void marginsRTL();
83 };
84
85 /*
86    Find an item with the specified objectName.
87 */
88 template<typename T>
89 T *findItem(QQuickItem *parent, const QString &objectName)
90 {
91     if (!parent)
92         return 0;
93
94     const QMetaObject &mo = T::staticMetaObject;
95     //qDebug() << parent->QQuickItem::children().count() << "children";
96     for (int i = 0; i < parent->childItems().count(); ++i) {
97         QQuickItem *item = qobject_cast<QQuickItem*>(parent->childItems().at(i));
98         if (!item)
99             continue;
100         //qDebug() << "try" << item;
101         if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName))
102             return static_cast<T*>(item);
103         item = findItem<T>(item, objectName);
104         if (item)
105             return static_cast<T*>(item);
106     }
107
108     return 0;
109 }
110
111 void tst_qquickanchors::basicAnchors()
112 {
113     QQuickView *view = new QQuickView;
114     view->setSource(testFileUrl("anchors.qml"));
115
116     qApp->processEvents();
117
118     //sibling horizontal
119     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect1"))->x(), 26.0);
120     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect2"))->x(), 122.0);
121     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect3"))->x(), 74.0);
122     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect4"))->x(), 16.0);
123     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect5"))->x(), 112.0);
124     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect6"))->x(), 64.0);
125
126     //parent horizontal
127     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect7"))->x(), 0.0);
128     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect8"))->x(), 240.0);
129     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect9"))->x(), 120.0);
130     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect10"))->x(), -10.0);
131     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect11"))->x(), 230.0);
132     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect12"))->x(), 110.0);
133
134     //vertical
135     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect13"))->y(), 20.0);
136     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect14"))->y(), 155.0);
137
138     //stretch
139     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect15"))->x(), 26.0);
140     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect15"))->width(), 96.0);
141     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect16"))->x(), 26.0);
142     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect16"))->width(), 192.0);
143     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect17"))->x(), -70.0);
144     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect17"))->width(), 192.0);
145
146     //vertical stretch
147     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect18"))->y(), 20.0);
148     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect18"))->height(), 40.0);
149
150     //more parent horizontal
151     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect19"))->x(), 115.0);
152     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect20"))->x(), 235.0);
153     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect21"))->x(), -5.0);
154
155     //centerIn
156     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect22"))->x(), 69.0);
157     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect22"))->y(), 5.0);
158
159      //margins
160     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect23"))->x(), 31.0);
161     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect23"))->y(), 5.0);
162     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect23"))->width(), 86.0);
163     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect23"))->height(), 10.0);
164
165     // offsets
166     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect24"))->x(), 26.0);
167     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect25"))->y(), 60.0);
168     QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect26"))->y(), 5.0);
169
170     //baseline
171     QQuickText *text1 = findItem<QQuickText>(view->rootObject(), QLatin1String("text1"));
172     QQuickText *text2 = findItem<QQuickText>(view->rootObject(), QLatin1String("text2"));
173     QCOMPARE(text1->y(), text2->y());
174
175     delete view;
176 }
177
178 QQuickItem* childItem(QQuickItem *parentItem, const char * itemString) {
179     return findItem<QQuickItem>(parentItem, QLatin1String(itemString));
180 }
181
182 qreal offsetMasterRTL(QQuickItem *rootItem, const char * itemString) {
183     QQuickItem* masterItem = findItem<QQuickItem>(rootItem,  QLatin1String("masterRect"));
184     return masterItem->width()+2*masterItem->x()-findItem<QQuickItem>(rootItem,  QLatin1String(itemString))->width();
185 }
186
187 qreal offsetParentRTL(QQuickItem *rootItem, const char * itemString) {
188     return rootItem->width()+2*rootItem->x()-findItem<QQuickItem>(rootItem,  QLatin1String(itemString))->width();
189 }
190
191 void mirrorAnchors(QQuickItem *item) {
192     QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
193     itemPrivate->setLayoutMirror(true);
194 }
195
196 void tst_qquickanchors::basicAnchorsRTL()
197 {
198     QQuickView *view = new QQuickView;
199     view->setSource(testFileUrl("anchors.qml"));
200
201     qApp->processEvents();
202
203     QQuickItem* rootItem = qobject_cast<QQuickItem*>(view->rootObject());
204     foreach (QObject *child, rootItem->children()) {
205         bool mirrored = QQuickItemPrivate::get(qobject_cast<QQuickItem*>(child))->anchors()->property("mirrored").toBool();
206         QCOMPARE(mirrored, false);
207     }
208
209     foreach (QObject *child, rootItem->children())
210         mirrorAnchors(qobject_cast<QQuickItem*>(child));
211
212     foreach (QObject *child, rootItem->children()) {
213         bool mirrored = QQuickItemPrivate::get(qobject_cast<QQuickItem*>(child))->anchors()->property("mirrored").toBool();
214         QCOMPARE(mirrored, true);
215     }
216
217     //sibling horizontal
218     QCOMPARE(childItem(rootItem, "rect1")->x(), offsetMasterRTL(rootItem, "rect1")-26.0);
219     QCOMPARE(childItem(rootItem, "rect2")->x(), offsetMasterRTL(rootItem, "rect2")-122.0);
220     QCOMPARE(childItem(rootItem, "rect3")->x(), offsetMasterRTL(rootItem, "rect3")-74.0);
221     QCOMPARE(childItem(rootItem, "rect4")->x(), offsetMasterRTL(rootItem, "rect4")-16.0);
222     QCOMPARE(childItem(rootItem, "rect5")->x(), offsetMasterRTL(rootItem, "rect5")-112.0);
223     QCOMPARE(childItem(rootItem, "rect6")->x(), offsetMasterRTL(rootItem, "rect6")-64.0);
224
225     //parent horizontal
226     QCOMPARE(childItem(rootItem, "rect7")->x(), offsetParentRTL(rootItem, "rect7")-0.0);
227     QCOMPARE(childItem(rootItem, "rect8")->x(), offsetParentRTL(rootItem, "rect8")-240.0);
228     QCOMPARE(childItem(rootItem, "rect9")->x(), offsetParentRTL(rootItem, "rect9")-120.0);
229     QCOMPARE(childItem(rootItem, "rect10")->x(), offsetParentRTL(rootItem, "rect10")+10.0);
230     QCOMPARE(childItem(rootItem, "rect11")->x(), offsetParentRTL(rootItem, "rect11")-230.0);
231     QCOMPARE(childItem(rootItem, "rect12")->x(), offsetParentRTL(rootItem, "rect12")-110.0);
232
233     //vertical
234     QCOMPARE(childItem(rootItem, "rect13")->y(), 20.0);
235     QCOMPARE(childItem(rootItem, "rect14")->y(), 155.0);
236
237     //stretch
238     QCOMPARE(childItem(rootItem, "rect15")->x(), offsetMasterRTL(rootItem, "rect15")-26.0);
239     QCOMPARE(childItem(rootItem, "rect15")->width(), 96.0);
240     QCOMPARE(childItem(rootItem, "rect16")->x(), offsetMasterRTL(rootItem, "rect16")-26.0);
241     QCOMPARE(childItem(rootItem, "rect16")->width(), 192.0);
242     QCOMPARE(childItem(rootItem, "rect17")->x(), offsetMasterRTL(rootItem, "rect17")+70.0);
243     QCOMPARE(childItem(rootItem, "rect17")->width(), 192.0);
244
245     //vertical stretch
246     QCOMPARE(childItem(rootItem, "rect18")->y(), 20.0);
247     QCOMPARE(childItem(rootItem, "rect18")->height(), 40.0);
248
249     //more parent horizontal
250     QCOMPARE(childItem(rootItem, "rect19")->x(), offsetParentRTL(rootItem, "rect19")-115.0);
251     QCOMPARE(childItem(rootItem, "rect20")->x(), offsetParentRTL(rootItem, "rect20")-235.0);
252     QCOMPARE(childItem(rootItem, "rect21")->x(), offsetParentRTL(rootItem, "rect21")+5.0);
253
254     //centerIn
255     QCOMPARE(childItem(rootItem, "rect22")->x(), offsetMasterRTL(rootItem, "rect22")-69.0);
256     QCOMPARE(childItem(rootItem, "rect22")->y(), 5.0);
257
258      //margins
259     QCOMPARE(childItem(rootItem, "rect23")->x(), offsetMasterRTL(rootItem, "rect23")-31.0);
260     QCOMPARE(childItem(rootItem, "rect23")->y(), 5.0);
261     QCOMPARE(childItem(rootItem, "rect23")->width(), 86.0);
262     QCOMPARE(childItem(rootItem, "rect23")->height(), 10.0);
263
264     // offsets
265     QCOMPARE(childItem(rootItem, "rect24")->x(), offsetMasterRTL(rootItem, "rect24")-26.0);
266     QCOMPARE(childItem(rootItem, "rect25")->y(), 60.0);
267     QCOMPARE(childItem(rootItem, "rect26")->y(), 5.0);
268
269     //baseline
270     QQuickText *text1 = findItem<QQuickText>(rootItem, QLatin1String("text1"));
271     QQuickText *text2 = findItem<QQuickText>(rootItem, QLatin1String("text2"));
272     QCOMPARE(text1->y(), text2->y());
273
274     delete view;
275 }
276
277 // mostly testing that we don't crash
278 void tst_qquickanchors::loops()
279 {
280     {
281         QUrl source(testFileUrl("loop1.qml"));
282
283         QString expect = source.toString() + ":6:5: QML Text: Possible anchor loop detected on horizontal anchor.";
284         QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
285         QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
286
287         QQuickView *view = new QQuickView;
288         view->setSource(source);
289         qApp->processEvents();
290
291         delete view;
292     }
293
294     {
295         QUrl source(testFileUrl("loop2.qml"));
296
297         QString expect = source.toString() + ":8:3: QML Image: Possible anchor loop detected on horizontal anchor.";
298         QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
299
300         QQuickView *view = new QQuickView;
301         view->setSource(source);
302         qApp->processEvents();
303
304         delete view;
305     }
306 }
307
308 void tst_qquickanchors::illegalSets()
309 {
310     QFETCH(QString, qml);
311     QFETCH(QString, warning);
312
313     QTest::ignoreMessage(QtWarningMsg, warning.toLatin1());
314
315     QDeclarativeEngine engine;
316     QDeclarativeComponent component(&engine);
317     component.setData(QByteArray("import QtQuick 1.0\n" + qml.toUtf8()), QUrl::fromLocalFile(""));
318     if (!component.isReady())
319         qWarning() << "Test errors:" << component.errors();
320     QVERIFY(component.isReady());
321     QObject *o = component.create();
322     delete o;
323 }
324
325 void tst_qquickanchors::illegalSets_data()
326 {
327     QTest::addColumn<QString>("qml");
328     QTest::addColumn<QString>("warning");
329
330     QTest::newRow("H - too many anchors")
331         << "Rectangle { id: rect; Rectangle { anchors.left: rect.left; anchors.right: rect.right; anchors.horizontalCenter: rect.horizontalCenter } }"
332         << "file::2:23: QML Rectangle: Cannot specify left, right, and hcenter anchors.";
333
334     foreach (const QString &side, QStringList() << "left" << "right") {
335         QTest::newRow("H - anchor to V")
336             << QString("Rectangle { Rectangle { anchors.%1: parent.top } }").arg(side)
337             << "file::2:13: QML Rectangle: Cannot anchor a horizontal edge to a vertical edge.";
338
339         QTest::newRow("H - anchor to non parent/sibling")
340             << QString("Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.%1: rect.%1 } }").arg(side)
341             << "file::2:45: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.";
342
343         QTest::newRow("H - anchor to self")
344             << QString("Rectangle { id: rect; anchors.%1: rect.%1 }").arg(side)
345             << "file::2:1: QML Rectangle: Cannot anchor item to self.";
346     }
347
348
349     QTest::newRow("V - too many anchors")
350         << "Rectangle { id: rect; Rectangle { anchors.top: rect.top; anchors.bottom: rect.bottom; anchors.verticalCenter: rect.verticalCenter } }"
351         << "file::2:23: QML Rectangle: Cannot specify top, bottom, and vcenter anchors.";
352
353     QTest::newRow("V - too many anchors with baseline")
354         << "Rectangle { Text { id: text1; text: \"Hello\" } Text { anchors.baseline: text1.baseline; anchors.top: text1.top; } }"
355         << "file::2:47: QML Text: Baseline anchor cannot be used in conjunction with top, bottom, or vcenter anchors.";
356
357     foreach (const QString &side, QStringList() << "top" << "bottom" << "baseline") {
358
359         QTest::newRow("V - anchor to H")
360             << QString("Rectangle { Rectangle { anchors.%1: parent.left } }").arg(side)
361             << "file::2:13: QML Rectangle: Cannot anchor a vertical edge to a horizontal edge.";
362
363         QTest::newRow("V - anchor to non parent/sibling")
364             << QString("Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.%1: rect.%1 } }").arg(side)
365             << "file::2:45: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.";
366
367         QTest::newRow("V - anchor to self")
368             << QString("Rectangle { id: rect; anchors.%1: rect.%1 }").arg(side)
369             << "file::2:1: QML Rectangle: Cannot anchor item to self.";
370     }
371
372
373     QTest::newRow("centerIn - anchor to non parent/sibling")
374         << "Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.centerIn: rect} }"
375         << "file::2:45: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.";
376
377
378     QTest::newRow("fill - anchor to non parent/sibling")
379         << "Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.fill: rect} }"
380         << "file::2:45: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.";
381 }
382
383 void tst_qquickanchors::reset()
384 {
385     QFETCH(QString, side);
386     QFETCH(QQuickAnchorLine::AnchorLine, anchorLine);
387     QFETCH(QQuickAnchors::Anchor, usedAnchor);
388
389     QQuickItem *baseItem = new QQuickItem;
390
391     QQuickAnchorLine anchor;
392     anchor.item = baseItem;
393     anchor.anchorLine = anchorLine;
394
395     QQuickItem *item = new QQuickItem;
396     QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
397
398     const QMetaObject *meta = itemPrivate->anchors()->metaObject();
399     QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
400
401     QVERIFY(p.write(itemPrivate->anchors(), qVariantFromValue(anchor)));
402     QCOMPARE(itemPrivate->anchors()->usedAnchors().testFlag(usedAnchor), true);
403
404     QVERIFY(p.reset(itemPrivate->anchors()));
405     QCOMPARE(itemPrivate->anchors()->usedAnchors().testFlag(usedAnchor), false);
406
407     delete item;
408     delete baseItem;
409 }
410
411 void tst_qquickanchors::reset_data()
412 {
413     QTest::addColumn<QString>("side");
414     QTest::addColumn<QQuickAnchorLine::AnchorLine>("anchorLine");
415     QTest::addColumn<QQuickAnchors::Anchor>("usedAnchor");
416
417     QTest::newRow("left") << "left" << QQuickAnchorLine::Left << QQuickAnchors::LeftAnchor;
418     QTest::newRow("top") << "top" << QQuickAnchorLine::Top << QQuickAnchors::TopAnchor;
419     QTest::newRow("right") << "right" << QQuickAnchorLine::Right << QQuickAnchors::RightAnchor;
420     QTest::newRow("bottom") << "bottom" << QQuickAnchorLine::Bottom << QQuickAnchors::BottomAnchor;
421
422     QTest::newRow("hcenter") << "horizontalCenter" << QQuickAnchorLine::HCenter << QQuickAnchors::HCenterAnchor;
423     QTest::newRow("vcenter") << "verticalCenter" << QQuickAnchorLine::VCenter << QQuickAnchors::VCenterAnchor;
424     QTest::newRow("baseline") << "baseline" << QQuickAnchorLine::Baseline << QQuickAnchors::BaselineAnchor;
425 }
426
427 void tst_qquickanchors::resetConvenience()
428 {
429     QQuickItem *baseItem = new QQuickItem;
430     QQuickItem *item = new QQuickItem;
431     QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
432
433     //fill
434     itemPrivate->anchors()->setFill(baseItem);
435     QVERIFY(itemPrivate->anchors()->fill() == baseItem);
436     itemPrivate->anchors()->resetFill();
437     QVERIFY(itemPrivate->anchors()->fill() == 0);
438
439     //centerIn
440     itemPrivate->anchors()->setCenterIn(baseItem);
441     QVERIFY(itemPrivate->anchors()->centerIn() == baseItem);
442     itemPrivate->anchors()->resetCenterIn();
443     QVERIFY(itemPrivate->anchors()->centerIn() == 0);
444
445     delete item;
446     delete baseItem;
447 }
448
449 void tst_qquickanchors::nullItem()
450 {
451     QFETCH(QString, side);
452
453     QQuickAnchorLine anchor;
454     QQuickItem *item = new QQuickItem;
455     QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
456
457     const QMetaObject *meta = itemPrivate->anchors()->metaObject();
458     QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
459
460     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML Item: Cannot anchor to a null item.");
461     QVERIFY(p.write(itemPrivate->anchors(), qVariantFromValue(anchor)));
462
463     delete item;
464 }
465
466 void tst_qquickanchors::nullItem_data()
467 {
468     QTest::addColumn<QString>("side");
469
470     QTest::newRow("left") << "left";
471     QTest::newRow("top") << "top";
472     QTest::newRow("right") << "right";
473     QTest::newRow("bottom") << "bottom";
474
475     QTest::newRow("hcenter") << "horizontalCenter";
476     QTest::newRow("vcenter") << "verticalCenter";
477     QTest::newRow("baseline") << "baseline";
478 }
479
480 //QTBUG-5428
481 void tst_qquickanchors::crash1()
482 {
483     QUrl source(testFileUrl("crash1.qml"));
484
485     QString expect = source.toString() + ":3:1: QML Column: Cannot specify top, bottom, verticalCenter, fill or centerIn anchors for items inside Column. Column will not function.";
486
487     QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
488
489     QQuickView *view = new QQuickView(source);
490     qApp->processEvents();
491
492     delete view;
493 }
494
495 void tst_qquickanchors::fill()
496 {
497     QQuickView *view = new QQuickView(testFileUrl("fill.qml"));
498
499     qApp->processEvents();
500     QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("filler"));
501     QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
502     QCOMPARE(rect->x(), 0.0 + 10.0);
503     QCOMPARE(rect->y(), 0.0 + 30.0);
504     QCOMPARE(rect->width(), 200.0 - 10.0 - 20.0);
505     QCOMPARE(rect->height(), 200.0 - 30.0 - 40.0);
506     //Alter Offsets (tests QTBUG-6631)
507     rectPrivate->anchors()->setLeftMargin(20.0);
508     rectPrivate->anchors()->setRightMargin(0.0);
509     rectPrivate->anchors()->setBottomMargin(0.0);
510     rectPrivate->anchors()->setTopMargin(10.0);
511     QCOMPARE(rect->x(), 0.0 + 20.0);
512     QCOMPARE(rect->y(), 0.0 + 10.0);
513     QCOMPARE(rect->width(), 200.0 - 20.0);
514     QCOMPARE(rect->height(), 200.0 - 10.0);
515
516     delete view;
517 }
518
519 void tst_qquickanchors::fillRTL()
520 {
521     QQuickView *view = new QQuickView(testFileUrl("fill.qml"));
522
523     qApp->processEvents();
524     QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("filler"));
525     QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
526     mirrorAnchors(rect);
527
528     QCOMPARE(rect->x(), 0.0 + 20.0);
529     QCOMPARE(rect->y(), 0.0 + 30.0);
530     QCOMPARE(rect->width(), 200.0 - 10.0 - 20.0);
531     QCOMPARE(rect->height(), 200.0 - 30.0 - 40.0);
532     //Alter Offsets (tests QTBUG-6631)
533     rectPrivate->anchors()->setLeftMargin(20.0);
534     rectPrivate->anchors()->setRightMargin(0.0);
535     rectPrivate->anchors()->setBottomMargin(0.0);
536     rectPrivate->anchors()->setTopMargin(10.0);
537     QCOMPARE(rect->x(), 0.0 + 0.0);
538     QCOMPARE(rect->y(), 0.0 + 10.0);
539     QCOMPARE(rect->width(), 200.0 - 20.0);
540     QCOMPARE(rect->height(), 200.0 - 10.0);
541
542     delete view;
543 }
544
545 void tst_qquickanchors::centerIn()
546 {
547     QQuickView *view = new QQuickView(testFileUrl("centerin.qml"));
548
549     qApp->processEvents();
550     QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
551     QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
552
553     QCOMPARE(rect->x(), 75.0 + 10);
554     QCOMPARE(rect->y(), 75.0 + 30);
555     //Alter Offsets (tests QTBUG-6631)
556     rectPrivate->anchors()->setHorizontalCenterOffset(-20.0);
557     rectPrivate->anchors()->setVerticalCenterOffset(-10.0);
558     QCOMPARE(rect->x(), 75.0 - 20.0);
559     QCOMPARE(rect->y(), 75.0 - 10.0);
560
561     delete view;
562 }
563
564 void tst_qquickanchors::centerInRTL()
565 {
566     QQuickView *view = new QQuickView(testFileUrl("centerin.qml"));
567
568     qApp->processEvents();
569     QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
570     QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
571     mirrorAnchors(rect);
572
573     QCOMPARE(rect->x(), 75.0 - 10);
574     QCOMPARE(rect->y(), 75.0 + 30);
575     //Alter Offsets (tests QTBUG-6631)
576     rectPrivate->anchors()->setHorizontalCenterOffset(-20.0);
577     rectPrivate->anchors()->setVerticalCenterOffset(-10.0);
578     QCOMPARE(rect->x(), 75.0 + 20.0);
579     QCOMPARE(rect->y(), 75.0 - 10.0);
580
581     delete view;
582 }
583
584 //QTBUG-12441
585 void tst_qquickanchors::centerInRotation()
586 {
587     QQuickView *view = new QQuickView(testFileUrl("centerinRotation.qml"));
588
589     qApp->processEvents();
590     QQuickRectangle* outer = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("outer"));
591     QQuickRectangle* inner = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("inner"));
592
593     QEXPECT_FAIL("", "QTBUG-12441", Abort);
594     QCOMPARE(outer->x(), qreal(49.5));
595     QCOMPARE(outer->y(), qreal(49.5));
596     QCOMPARE(inner->x(), qreal(25.5));
597     QCOMPARE(inner->y(), qreal(25.5));
598
599     delete view;
600 }
601
602 void tst_qquickanchors::hvCenter()
603 {
604     QQuickView *view = new QQuickView(testFileUrl("hvCenter.qml"));
605
606     qApp->processEvents();
607     QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
608     QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
609
610     // test QTBUG-10999
611     QCOMPARE(rect->x(), 10.0);
612     QCOMPARE(rect->y(), 19.0);
613
614     rectPrivate->anchors()->setHorizontalCenterOffset(-5.0);
615     rectPrivate->anchors()->setVerticalCenterOffset(5.0);
616     QCOMPARE(rect->x(), 10.0 - 5.0);
617     QCOMPARE(rect->y(), 19.0 + 5.0);
618
619     delete view;
620 }
621
622 void tst_qquickanchors::hvCenterRTL()
623 {
624     QQuickView *view = new QQuickView(testFileUrl("hvCenter.qml"));
625
626     qApp->processEvents();
627     QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
628     QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
629     mirrorAnchors(rect);
630
631     // test QTBUG-10999
632     QCOMPARE(rect->x(), 10.0);
633     QCOMPARE(rect->y(), 19.0);
634
635     rectPrivate->anchors()->setHorizontalCenterOffset(-5.0);
636     rectPrivate->anchors()->setVerticalCenterOffset(5.0);
637     QCOMPARE(rect->x(), 10.0 + 5.0);
638     QCOMPARE(rect->y(), 19.0 + 5.0);
639
640     delete view;
641 }
642 void tst_qquickanchors::margins()
643 {
644     QQuickView *view = new QQuickView(testFileUrl("margins.qml"));
645
646     qApp->processEvents();
647     QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("filler"));
648     QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
649     QCOMPARE(rect->x(), 5.0);
650     QCOMPARE(rect->y(), 6.0);
651     QCOMPARE(rect->width(), 200.0 - 5.0 - 10.0);
652     QCOMPARE(rect->height(), 200.0 - 6.0 - 10.0);
653
654     rectPrivate->anchors()->setTopMargin(0.0);
655     rectPrivate->anchors()->setMargins(20.0);
656
657     QCOMPARE(rect->x(), 5.0);
658     QCOMPARE(rect->y(), 20.0);
659     QCOMPARE(rect->width(), 200.0 - 5.0 - 20.0);
660     QCOMPARE(rect->height(), 200.0 - 20.0 - 20.0);
661
662     delete view;
663 }
664
665 void tst_qquickanchors::marginsRTL()
666 {
667     QQuickView *view = new QQuickView(testFileUrl("margins.qml"));
668
669     QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("filler"));
670     QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
671     mirrorAnchors(rect);
672
673     QCOMPARE(rect->x(), 10.0);
674     QCOMPARE(rect->y(), 6.0);
675     QCOMPARE(rect->width(), 200.0 - 5.0 - 10.0);
676     QCOMPARE(rect->height(), 200.0 - 6.0 - 10.0);
677
678     rectPrivate->anchors()->setTopMargin(0.0);
679     rectPrivate->anchors()->setMargins(20.0);
680
681     QCOMPARE(rect->x(), 20.0);
682     QCOMPARE(rect->y(), 20.0);
683     QCOMPARE(rect->width(), 200.0 - 5.0 - 20.0);
684     QCOMPARE(rect->height(), 200.0 - 20.0 - 20.0);
685
686     delete view;
687 }
688
689
690 QTEST_MAIN(tst_qquickanchors)
691
692 #include "tst_qquickanchors.moc"