1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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"
54 Q_DECLARE_METATYPE(QQuickAnchors::Anchor)
55 Q_DECLARE_METATYPE(QQuickAnchorLine::AnchorLine)
57 using namespace QQuickVisualTestUtil;
59 class tst_qquickanchors : public QDeclarativeDataTest
63 tst_qquickanchors() {}
67 void basicAnchorsRTL();
70 void illegalSets_data();
73 void resetConvenience();
79 void centerInRotation();
88 void tst_qquickanchors::basicAnchors()
90 QQuickView *view = new QQuickView;
91 view->setSource(testFileUrl("anchors.qml"));
93 qApp->processEvents();
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);
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);
112 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect13"))->y(), 20.0);
113 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect14"))->y(), 155.0);
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);
124 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect18"))->y(), 20.0);
125 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect18"))->height(), 40.0);
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);
133 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect22"))->x(), 69.0);
134 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect22"))->y(), 5.0);
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);
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);
148 QQuickText *text1 = findItem<QQuickText>(view->rootObject(), QLatin1String("text1"));
149 QQuickText *text2 = findItem<QQuickText>(view->rootObject(), QLatin1String("text2"));
150 QCOMPARE(text1->y(), text2->y());
155 QQuickItem* childItem(QQuickItem *parentItem, const char * itemString) {
156 return findItem<QQuickItem>(parentItem, QLatin1String(itemString));
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();
164 qreal offsetParentRTL(QQuickItem *rootItem, const char * itemString) {
165 return rootItem->width()+2*rootItem->x()-findItem<QQuickItem>(rootItem, QLatin1String(itemString))->width();
168 void mirrorAnchors(QQuickItem *item) {
169 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
170 itemPrivate->setLayoutMirror(true);
173 void tst_qquickanchors::basicAnchorsRTL()
175 QQuickView *view = new QQuickView;
176 view->setSource(testFileUrl("anchors.qml"));
178 qApp->processEvents();
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);
186 foreach (QObject *child, rootItem->children())
187 mirrorAnchors(qobject_cast<QQuickItem*>(child));
189 foreach (QObject *child, rootItem->children()) {
190 bool mirrored = QQuickItemPrivate::get(qobject_cast<QQuickItem*>(child))->anchors()->property("mirrored").toBool();
191 QCOMPARE(mirrored, true);
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);
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);
211 QCOMPARE(childItem(rootItem, "rect13")->y(), 20.0);
212 QCOMPARE(childItem(rootItem, "rect14")->y(), 155.0);
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);
223 QCOMPARE(childItem(rootItem, "rect18")->y(), 20.0);
224 QCOMPARE(childItem(rootItem, "rect18")->height(), 40.0);
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);
232 QCOMPARE(childItem(rootItem, "rect22")->x(), offsetMasterRTL(rootItem, "rect22")-69.0);
233 QCOMPARE(childItem(rootItem, "rect22")->y(), 5.0);
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);
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);
247 QQuickText *text1 = findItem<QQuickText>(rootItem, QLatin1String("text1"));
248 QQuickText *text2 = findItem<QQuickText>(rootItem, QLatin1String("text2"));
249 QCOMPARE(text1->y(), text2->y());
254 // mostly testing that we don't crash
255 void tst_qquickanchors::loops()
258 QUrl source(testFileUrl("loop1.qml"));
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());
264 QQuickView *view = new QQuickView;
265 view->setSource(source);
266 qApp->processEvents();
272 QUrl source(testFileUrl("loop2.qml"));
274 QString expect = source.toString() + ":8:3: QML Image: Possible anchor loop detected on horizontal anchor.";
275 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
277 QQuickView *view = new QQuickView;
278 view->setSource(source);
279 qApp->processEvents();
285 void tst_qquickanchors::illegalSets()
287 QFETCH(QString, qml);
288 QFETCH(QString, warning);
290 QTest::ignoreMessage(QtWarningMsg, warning.toLatin1());
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();
302 void tst_qquickanchors::illegalSets_data()
304 QTest::addColumn<QString>("qml");
305 QTest::addColumn<QString>("warning");
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.";
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.";
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.";
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.";
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.";
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.";
334 foreach (const QString &side, QStringList() << "top" << "bottom" << "baseline") {
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.";
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.";
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.";
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.";
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.";
360 void tst_qquickanchors::reset()
362 QFETCH(QString, side);
363 QFETCH(QQuickAnchorLine::AnchorLine, anchorLine);
364 QFETCH(QQuickAnchors::Anchor, usedAnchor);
366 QQuickItem *baseItem = new QQuickItem;
368 QQuickAnchorLine anchor;
369 anchor.item = baseItem;
370 anchor.anchorLine = anchorLine;
372 QQuickItem *item = new QQuickItem;
373 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
375 const QMetaObject *meta = itemPrivate->anchors()->metaObject();
376 QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
378 QVERIFY(p.write(itemPrivate->anchors(), qVariantFromValue(anchor)));
379 QCOMPARE(itemPrivate->anchors()->usedAnchors().testFlag(usedAnchor), true);
381 QVERIFY(p.reset(itemPrivate->anchors()));
382 QCOMPARE(itemPrivate->anchors()->usedAnchors().testFlag(usedAnchor), false);
388 void tst_qquickanchors::reset_data()
390 QTest::addColumn<QString>("side");
391 QTest::addColumn<QQuickAnchorLine::AnchorLine>("anchorLine");
392 QTest::addColumn<QQuickAnchors::Anchor>("usedAnchor");
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;
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;
404 void tst_qquickanchors::resetConvenience()
406 QQuickItem *baseItem = new QQuickItem;
407 QQuickItem *item = new QQuickItem;
408 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
411 itemPrivate->anchors()->setFill(baseItem);
412 QVERIFY(itemPrivate->anchors()->fill() == baseItem);
413 itemPrivate->anchors()->resetFill();
414 QVERIFY(itemPrivate->anchors()->fill() == 0);
417 itemPrivate->anchors()->setCenterIn(baseItem);
418 QVERIFY(itemPrivate->anchors()->centerIn() == baseItem);
419 itemPrivate->anchors()->resetCenterIn();
420 QVERIFY(itemPrivate->anchors()->centerIn() == 0);
426 void tst_qquickanchors::nullItem()
428 QFETCH(QString, side);
430 QQuickAnchorLine anchor;
431 QQuickItem *item = new QQuickItem;
432 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
434 const QMetaObject *meta = itemPrivate->anchors()->metaObject();
435 QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
437 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML Item: Cannot anchor to a null item.");
438 QVERIFY(p.write(itemPrivate->anchors(), qVariantFromValue(anchor)));
443 void tst_qquickanchors::nullItem_data()
445 QTest::addColumn<QString>("side");
447 QTest::newRow("left") << "left";
448 QTest::newRow("top") << "top";
449 QTest::newRow("right") << "right";
450 QTest::newRow("bottom") << "bottom";
452 QTest::newRow("hcenter") << "horizontalCenter";
453 QTest::newRow("vcenter") << "verticalCenter";
454 QTest::newRow("baseline") << "baseline";
458 void tst_qquickanchors::crash1()
460 QUrl source(testFileUrl("crash1.qml"));
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.";
464 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
466 QQuickView *view = new QQuickView(source);
467 qApp->processEvents();
472 void tst_qquickanchors::fill()
474 QQuickView *view = new QQuickView(testFileUrl("fill.qml"));
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);
496 void tst_qquickanchors::fillRTL()
498 QQuickView *view = new QQuickView(testFileUrl("fill.qml"));
500 qApp->processEvents();
501 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("filler"));
502 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
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);
522 void tst_qquickanchors::centerIn()
524 QQuickView *view = new QQuickView(testFileUrl("centerin.qml"));
526 qApp->processEvents();
527 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
528 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
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);
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);
546 void tst_qquickanchors::centerInRTL()
548 QQuickView *view = new QQuickView(testFileUrl("centerin.qml"));
550 qApp->processEvents();
551 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
552 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
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);
567 void tst_qquickanchors::centerInRotation()
569 QQuickView *view = new QQuickView(testFileUrl("centerinRotation.qml"));
571 qApp->processEvents();
572 QQuickRectangle* outer = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("outer"));
573 QQuickRectangle* inner = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("inner"));
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));
583 void tst_qquickanchors::hvCenter()
585 QQuickView *view = new QQuickView(testFileUrl("hvCenter.qml"));
587 qApp->processEvents();
588 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
589 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
592 QCOMPARE(rect->x(), 10.0);
593 QCOMPARE(rect->y(), 19.0);
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);
603 void tst_qquickanchors::hvCenterRTL()
605 QQuickView *view = new QQuickView(testFileUrl("hvCenter.qml"));
607 qApp->processEvents();
608 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
609 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
613 QCOMPARE(rect->x(), 10.0);
614 QCOMPARE(rect->y(), 19.0);
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);
623 void tst_qquickanchors::margins()
625 QQuickView *view = new QQuickView(testFileUrl("margins.qml"));
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);
635 rectPrivate->anchors()->setTopMargin(0.0);
636 rectPrivate->anchors()->setMargins(20.0);
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);
646 void tst_qquickanchors::marginsRTL()
648 QQuickView *view = new QQuickView(testFileUrl("margins.qml"));
650 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("filler"));
651 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
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);
659 rectPrivate->anchors()->setTopMargin(0.0);
660 rectPrivate->anchors()->setMargins(20.0);
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);
671 QTEST_MAIN(tst_qquickanchors)
673 #include "tst_qquickanchors.moc"