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