1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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"
53 Q_DECLARE_METATYPE(QQuickAnchors::Anchor)
54 Q_DECLARE_METATYPE(QQuickAnchorLine::AnchorLine)
56 class tst_qquickanchors : public QDeclarativeDataTest
60 tst_qquickanchors() {}
64 void basicAnchorsRTL();
67 void illegalSets_data();
70 void resetConvenience();
76 void centerInRotation();
86 Find an item with the specified objectName.
89 T *findItem(QQuickItem *parent, const QString &objectName)
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));
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);
105 return static_cast<T*>(item);
111 void tst_qquickanchors::basicAnchors()
113 QQuickView *view = new QQuickView;
114 view->setSource(testFileUrl("anchors.qml"));
116 qApp->processEvents();
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);
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);
135 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect13"))->y(), 20.0);
136 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect14"))->y(), 155.0);
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);
147 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect18"))->y(), 20.0);
148 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect18"))->height(), 40.0);
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);
156 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect22"))->x(), 69.0);
157 QCOMPARE(findItem<QQuickRectangle>(view->rootObject(), QLatin1String("rect22"))->y(), 5.0);
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);
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);
171 QQuickText *text1 = findItem<QQuickText>(view->rootObject(), QLatin1String("text1"));
172 QQuickText *text2 = findItem<QQuickText>(view->rootObject(), QLatin1String("text2"));
173 QCOMPARE(text1->y(), text2->y());
178 QQuickItem* childItem(QQuickItem *parentItem, const char * itemString) {
179 return findItem<QQuickItem>(parentItem, QLatin1String(itemString));
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();
187 qreal offsetParentRTL(QQuickItem *rootItem, const char * itemString) {
188 return rootItem->width()+2*rootItem->x()-findItem<QQuickItem>(rootItem, QLatin1String(itemString))->width();
191 void mirrorAnchors(QQuickItem *item) {
192 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
193 itemPrivate->setLayoutMirror(true);
196 void tst_qquickanchors::basicAnchorsRTL()
198 QQuickView *view = new QQuickView;
199 view->setSource(testFileUrl("anchors.qml"));
201 qApp->processEvents();
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);
209 foreach (QObject *child, rootItem->children())
210 mirrorAnchors(qobject_cast<QQuickItem*>(child));
212 foreach (QObject *child, rootItem->children()) {
213 bool mirrored = QQuickItemPrivate::get(qobject_cast<QQuickItem*>(child))->anchors()->property("mirrored").toBool();
214 QCOMPARE(mirrored, true);
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);
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);
234 QCOMPARE(childItem(rootItem, "rect13")->y(), 20.0);
235 QCOMPARE(childItem(rootItem, "rect14")->y(), 155.0);
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);
246 QCOMPARE(childItem(rootItem, "rect18")->y(), 20.0);
247 QCOMPARE(childItem(rootItem, "rect18")->height(), 40.0);
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);
255 QCOMPARE(childItem(rootItem, "rect22")->x(), offsetMasterRTL(rootItem, "rect22")-69.0);
256 QCOMPARE(childItem(rootItem, "rect22")->y(), 5.0);
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);
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);
270 QQuickText *text1 = findItem<QQuickText>(rootItem, QLatin1String("text1"));
271 QQuickText *text2 = findItem<QQuickText>(rootItem, QLatin1String("text2"));
272 QCOMPARE(text1->y(), text2->y());
277 // mostly testing that we don't crash
278 void tst_qquickanchors::loops()
281 QUrl source(testFileUrl("loop1.qml"));
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());
287 QQuickView *view = new QQuickView;
288 view->setSource(source);
289 qApp->processEvents();
295 QUrl source(testFileUrl("loop2.qml"));
297 QString expect = source.toString() + ":8:3: QML Image: Possible anchor loop detected on horizontal anchor.";
298 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
300 QQuickView *view = new QQuickView;
301 view->setSource(source);
302 qApp->processEvents();
308 void tst_qquickanchors::illegalSets()
310 QFETCH(QString, qml);
311 QFETCH(QString, warning);
313 QTest::ignoreMessage(QtWarningMsg, warning.toLatin1());
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();
325 void tst_qquickanchors::illegalSets_data()
327 QTest::addColumn<QString>("qml");
328 QTest::addColumn<QString>("warning");
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.";
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.";
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.";
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.";
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.";
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.";
357 foreach (const QString &side, QStringList() << "top" << "bottom" << "baseline") {
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.";
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.";
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.";
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.";
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.";
383 void tst_qquickanchors::reset()
385 QFETCH(QString, side);
386 QFETCH(QQuickAnchorLine::AnchorLine, anchorLine);
387 QFETCH(QQuickAnchors::Anchor, usedAnchor);
389 QQuickItem *baseItem = new QQuickItem;
391 QQuickAnchorLine anchor;
392 anchor.item = baseItem;
393 anchor.anchorLine = anchorLine;
395 QQuickItem *item = new QQuickItem;
396 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
398 const QMetaObject *meta = itemPrivate->anchors()->metaObject();
399 QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
401 QVERIFY(p.write(itemPrivate->anchors(), qVariantFromValue(anchor)));
402 QCOMPARE(itemPrivate->anchors()->usedAnchors().testFlag(usedAnchor), true);
404 QVERIFY(p.reset(itemPrivate->anchors()));
405 QCOMPARE(itemPrivate->anchors()->usedAnchors().testFlag(usedAnchor), false);
411 void tst_qquickanchors::reset_data()
413 QTest::addColumn<QString>("side");
414 QTest::addColumn<QQuickAnchorLine::AnchorLine>("anchorLine");
415 QTest::addColumn<QQuickAnchors::Anchor>("usedAnchor");
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;
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;
427 void tst_qquickanchors::resetConvenience()
429 QQuickItem *baseItem = new QQuickItem;
430 QQuickItem *item = new QQuickItem;
431 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
434 itemPrivate->anchors()->setFill(baseItem);
435 QVERIFY(itemPrivate->anchors()->fill() == baseItem);
436 itemPrivate->anchors()->resetFill();
437 QVERIFY(itemPrivate->anchors()->fill() == 0);
440 itemPrivate->anchors()->setCenterIn(baseItem);
441 QVERIFY(itemPrivate->anchors()->centerIn() == baseItem);
442 itemPrivate->anchors()->resetCenterIn();
443 QVERIFY(itemPrivate->anchors()->centerIn() == 0);
449 void tst_qquickanchors::nullItem()
451 QFETCH(QString, side);
453 QQuickAnchorLine anchor;
454 QQuickItem *item = new QQuickItem;
455 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
457 const QMetaObject *meta = itemPrivate->anchors()->metaObject();
458 QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
460 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML Item: Cannot anchor to a null item.");
461 QVERIFY(p.write(itemPrivate->anchors(), qVariantFromValue(anchor)));
466 void tst_qquickanchors::nullItem_data()
468 QTest::addColumn<QString>("side");
470 QTest::newRow("left") << "left";
471 QTest::newRow("top") << "top";
472 QTest::newRow("right") << "right";
473 QTest::newRow("bottom") << "bottom";
475 QTest::newRow("hcenter") << "horizontalCenter";
476 QTest::newRow("vcenter") << "verticalCenter";
477 QTest::newRow("baseline") << "baseline";
481 void tst_qquickanchors::crash1()
483 QUrl source(testFileUrl("crash1.qml"));
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.";
487 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
489 QQuickView *view = new QQuickView(source);
490 qApp->processEvents();
495 void tst_qquickanchors::fill()
497 QQuickView *view = new QQuickView(testFileUrl("fill.qml"));
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);
519 void tst_qquickanchors::fillRTL()
521 QQuickView *view = new QQuickView(testFileUrl("fill.qml"));
523 qApp->processEvents();
524 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("filler"));
525 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
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);
545 void tst_qquickanchors::centerIn()
547 QQuickView *view = new QQuickView(testFileUrl("centerin.qml"));
549 qApp->processEvents();
550 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
551 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
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);
564 void tst_qquickanchors::centerInRTL()
566 QQuickView *view = new QQuickView(testFileUrl("centerin.qml"));
568 qApp->processEvents();
569 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
570 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
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);
585 void tst_qquickanchors::centerInRotation()
587 QQuickView *view = new QQuickView(testFileUrl("centerinRotation.qml"));
589 qApp->processEvents();
590 QQuickRectangle* outer = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("outer"));
591 QQuickRectangle* inner = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("inner"));
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));
602 void tst_qquickanchors::hvCenter()
604 QQuickView *view = new QQuickView(testFileUrl("hvCenter.qml"));
606 qApp->processEvents();
607 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
608 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
611 QCOMPARE(rect->x(), 10.0);
612 QCOMPARE(rect->y(), 19.0);
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);
622 void tst_qquickanchors::hvCenterRTL()
624 QQuickView *view = new QQuickView(testFileUrl("hvCenter.qml"));
626 qApp->processEvents();
627 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered"));
628 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
632 QCOMPARE(rect->x(), 10.0);
633 QCOMPARE(rect->y(), 19.0);
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);
642 void tst_qquickanchors::margins()
644 QQuickView *view = new QQuickView(testFileUrl("margins.qml"));
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);
654 rectPrivate->anchors()->setTopMargin(0.0);
655 rectPrivate->anchors()->setMargins(20.0);
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);
665 void tst_qquickanchors::marginsRTL()
667 QQuickView *view = new QQuickView(testFileUrl("margins.qml"));
669 QQuickRectangle* rect = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("filler"));
670 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
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);
678 rectPrivate->anchors()->setTopMargin(0.0);
679 rectPrivate->anchors()->setMargins(20.0);
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);
690 QTEST_MAIN(tst_qquickanchors)
692 #include "tst_qquickanchors.moc"