1 /****************************************************************************
3 ** Copyright (C) 2011 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include <QtGui/QGraphicsWidget>
44 #include <private/qgraphicsitem_p.h>
45 #include <QtDeclarative/qdeclarativeengine.h>
46 #include <QtDeclarative/qdeclarativecomponent.h>
47 #include <QtDeclarative/qdeclarativeview.h>
48 #include <private/qdeclarativerectangle_p.h>
49 #include <private/qdeclarativetext_p.h>
50 #include <QtDeclarative/private/qdeclarativeanchors_p_p.h>
51 #include <QtDeclarative/private/qdeclarativeitem_p.h>
54 // In Symbian OS test data is located in applications private dir
58 Q_DECLARE_METATYPE(QDeclarativeAnchors::Anchor)
59 Q_DECLARE_METATYPE(QDeclarativeAnchorLine::AnchorLine)
62 class tst_qdeclarativeanchors : public QObject
66 tst_qdeclarativeanchors() {}
70 void basicAnchorsQGraphicsWidget();
71 void basicAnchorsRTL();
74 void illegalSets_data();
77 void resetConvenience();
92 Find an item with the specified id.
95 T *findItem(QGraphicsObject *parent, const QString &objectName)
97 const QMetaObject &mo = T::staticMetaObject;
98 QList<QGraphicsItem *> children = parent->childItems();
99 for (int i = 0; i < children.count(); ++i) {
100 QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(children.at(i)->toGraphicsObject());
102 if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
103 return static_cast<T*>(item);
105 item = findItem<T>(item, objectName);
107 return static_cast<T*>(item);
114 QGraphicsObject *findObject(QGraphicsObject *parent, const QString &objectName)
116 QList<QGraphicsItem *> children = parent->childItems();
117 for (int i = 0; i < children.count(); ++i) {
118 QGraphicsObject *item = children.at(i)->toGraphicsObject();
120 if (objectName.isEmpty() || item->objectName() == objectName) {
123 item = findObject(item, objectName);
133 void tst_qdeclarativeanchors::basicAnchors()
135 QDeclarativeView *view = new QDeclarativeView;
136 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/anchors.qml"));
138 qApp->processEvents();
141 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect1"))->x(), 26.0);
142 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect2"))->x(), 122.0);
143 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect3"))->x(), 74.0);
144 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect4"))->x(), 16.0);
145 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect5"))->x(), 112.0);
146 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect6"))->x(), 64.0);
149 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect7"))->x(), 0.0);
150 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect8"))->x(), 240.0);
151 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect9"))->x(), 120.0);
152 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect10"))->x(), -10.0);
153 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect11"))->x(), 230.0);
154 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect12"))->x(), 110.0);
157 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect13"))->y(), 20.0);
158 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect14"))->y(), 155.0);
161 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect15"))->x(), 26.0);
162 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect15"))->width(), 96.0);
163 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect16"))->x(), 26.0);
164 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect16"))->width(), 192.0);
165 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect17"))->x(), -70.0);
166 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect17"))->width(), 192.0);
169 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect18"))->y(), 20.0);
170 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect18"))->height(), 40.0);
172 //more parent horizontal
173 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect19"))->x(), 115.0);
174 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect20"))->x(), 235.0);
175 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect21"))->x(), -5.0);
178 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect22"))->x(), 69.0);
179 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect22"))->y(), 5.0);
182 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect23"))->x(), 31.0);
183 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect23"))->y(), 5.0);
184 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect23"))->width(), 86.0);
185 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect23"))->height(), 10.0);
188 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect24"))->x(), 26.0);
189 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect25"))->y(), 60.0);
190 QCOMPARE(findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("rect26"))->y(), 5.0);
193 QDeclarativeText *text1 = findItem<QDeclarativeText>(view->rootObject(), QLatin1String("text1"));
194 QDeclarativeText *text2 = findItem<QDeclarativeText>(view->rootObject(), QLatin1String("text2"));
195 QCOMPARE(text1->y(), text2->y());
200 void tst_qdeclarativeanchors::basicAnchorsQGraphicsWidget()
202 QDeclarativeView *view = new QDeclarativeView;
203 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/anchorsqgraphicswidget.qml"));
205 qApp->processEvents();
208 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect1"))->x(), 26.0);
209 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect2"))->x(), 122.0);
210 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect3"))->x(), 74.0);
211 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect4"))->x(), 16.0);
212 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect5"))->x(), 112.0);
213 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect6"))->x(), 64.0);
216 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect7"))->x(), 0.0);
217 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect8"))->x(), 240.0);
218 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect9"))->x(), 120.0);
219 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect10"))->x(), -10.0);
220 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect11"))->x(), 230.0);
221 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect12"))->x(), 110.0);
224 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect13"))->y(), 20.0);
225 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect14"))->y(), 155.0);
228 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect15"))->x(), 26.0);
229 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect15"))->property("width").toReal(), 96.0);
230 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect16"))->x(), 26.0);
231 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect16"))->property("width").toReal(), 192.0);
232 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect17"))->x(), -70.0);
233 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect17"))->property("width").toReal(), 192.0);
236 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect18"))->y(), 20.0);
237 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect18"))->property("height").toReal(), 40.0);
239 //more parent horizontal
240 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect19"))->x(), 115.0);
241 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect20"))->x(), 235.0);
242 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect21"))->x(), -5.0);
245 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect22"))->x(), 69.0);
246 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect22"))->y(), 5.0);
249 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect23"))->x(), 31.0);
250 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect23"))->y(), 5.0);
251 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect23"))->property("width").toReal(), 86.0);
252 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect23"))->property("height").toReal(), 10.0);
255 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect24"))->x(), 26.0);
256 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect25"))->y(), 60.0);
257 QCOMPARE(findObject(view->rootObject(), QLatin1String("rect26"))->y(), 5.0);
260 QDeclarativeText *text1 = findItem<QDeclarativeText>(view->rootObject(), QLatin1String("text1"));
261 QDeclarativeText *text2 = findItem<QDeclarativeText>(view->rootObject(), QLatin1String("text2"));
262 QCOMPARE(text1->y(), text2->y());
267 QDeclarativeItem* childItem(QDeclarativeItem *parentItem, const char * itemString) {
268 return findItem<QDeclarativeItem>(parentItem, QLatin1String(itemString));
271 qreal offsetMasterRTL(QDeclarativeItem *rootItem, const char * itemString) {
272 QDeclarativeItem* masterItem = findItem<QDeclarativeItem>(rootItem, QLatin1String("masterRect"));
273 return masterItem->width()+2*masterItem->x()-findItem<QDeclarativeItem>(rootItem, QLatin1String(itemString))->width();
276 qreal offsetParentRTL(QDeclarativeItem *rootItem, const char * itemString) {
277 return rootItem->width()+2*rootItem->x()-findItem<QDeclarativeItem>(rootItem, QLatin1String(itemString))->width();
280 void mirrorAnchors(QDeclarativeItem *item) {
281 QDeclarativeItemPrivate *itemPrivate = QDeclarativeItemPrivate::get(item);
282 itemPrivate->setLayoutMirror(true);
285 void tst_qdeclarativeanchors::basicAnchorsRTL()
287 QDeclarativeView *view = new QDeclarativeView;
288 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/anchors.qml"));
290 qApp->processEvents();
292 QDeclarativeItem* rootItem = qobject_cast<QDeclarativeItem*>(view->rootObject());
293 foreach(QObject *child, rootItem->children()) {
294 bool mirrored = QDeclarativeItemPrivate::get(qobject_cast<QDeclarativeItem*>(child))->anchors()->property("mirrored").toBool();
295 QCOMPARE(mirrored, false);
298 foreach(QObject *child, rootItem->children())
299 mirrorAnchors(qobject_cast<QDeclarativeItem*>(child));
301 foreach(QObject *child, rootItem->children()) {
302 bool mirrored = QDeclarativeItemPrivate::get(qobject_cast<QDeclarativeItem*>(child))->anchors()->property("mirrored").toBool();
303 QCOMPARE(mirrored, true);
307 QCOMPARE(childItem(rootItem, "rect1")->x(), offsetMasterRTL(rootItem, "rect1")-26.0);
308 QCOMPARE(childItem(rootItem, "rect2")->x(), offsetMasterRTL(rootItem, "rect2")-122.0);
309 QCOMPARE(childItem(rootItem, "rect3")->x(), offsetMasterRTL(rootItem, "rect3")-74.0);
310 QCOMPARE(childItem(rootItem, "rect4")->x(), offsetMasterRTL(rootItem, "rect4")-16.0);
311 QCOMPARE(childItem(rootItem, "rect5")->x(), offsetMasterRTL(rootItem, "rect5")-112.0);
312 QCOMPARE(childItem(rootItem, "rect6")->x(), offsetMasterRTL(rootItem, "rect6")-64.0);
315 QCOMPARE(childItem(rootItem, "rect7")->x(), offsetParentRTL(rootItem, "rect7")-0.0);
316 QCOMPARE(childItem(rootItem, "rect8")->x(), offsetParentRTL(rootItem, "rect8")-240.0);
317 QCOMPARE(childItem(rootItem, "rect9")->x(), offsetParentRTL(rootItem, "rect9")-120.0);
318 QCOMPARE(childItem(rootItem, "rect10")->x(), offsetParentRTL(rootItem, "rect10")+10.0);
319 QCOMPARE(childItem(rootItem, "rect11")->x(), offsetParentRTL(rootItem, "rect11")-230.0);
320 QCOMPARE(childItem(rootItem, "rect12")->x(), offsetParentRTL(rootItem, "rect12")-110.0);
323 QCOMPARE(childItem(rootItem, "rect13")->y(), 20.0);
324 QCOMPARE(childItem(rootItem, "rect14")->y(), 155.0);
327 QCOMPARE(childItem(rootItem, "rect15")->x(), offsetMasterRTL(rootItem, "rect15")-26.0);
328 QCOMPARE(childItem(rootItem, "rect15")->width(), 96.0);
329 QCOMPARE(childItem(rootItem, "rect16")->x(), offsetMasterRTL(rootItem, "rect16")-26.0);
330 QCOMPARE(childItem(rootItem, "rect16")->width(), 192.0);
331 QCOMPARE(childItem(rootItem, "rect17")->x(), offsetMasterRTL(rootItem, "rect17")+70.0);
332 QCOMPARE(childItem(rootItem, "rect17")->width(), 192.0);
335 QCOMPARE(childItem(rootItem, "rect18")->y(), 20.0);
336 QCOMPARE(childItem(rootItem, "rect18")->height(), 40.0);
338 //more parent horizontal
339 QCOMPARE(childItem(rootItem, "rect19")->x(), offsetParentRTL(rootItem, "rect19")-115.0);
340 QCOMPARE(childItem(rootItem, "rect20")->x(), offsetParentRTL(rootItem, "rect20")-235.0);
341 QCOMPARE(childItem(rootItem, "rect21")->x(), offsetParentRTL(rootItem, "rect21")+5.0);
344 QCOMPARE(childItem(rootItem, "rect22")->x(), offsetMasterRTL(rootItem, "rect22")-69.0);
345 QCOMPARE(childItem(rootItem, "rect22")->y(), 5.0);
348 QCOMPARE(childItem(rootItem, "rect23")->x(), offsetMasterRTL(rootItem, "rect23")-31.0);
349 QCOMPARE(childItem(rootItem, "rect23")->y(), 5.0);
350 QCOMPARE(childItem(rootItem, "rect23")->width(), 86.0);
351 QCOMPARE(childItem(rootItem, "rect23")->height(), 10.0);
354 QCOMPARE(childItem(rootItem, "rect24")->x(), offsetMasterRTL(rootItem, "rect24")-26.0);
355 QCOMPARE(childItem(rootItem, "rect25")->y(), 60.0);
356 QCOMPARE(childItem(rootItem, "rect26")->y(), 5.0);
359 QDeclarativeText *text1 = findItem<QDeclarativeText>(rootItem, QLatin1String("text1"));
360 QDeclarativeText *text2 = findItem<QDeclarativeText>(rootItem, QLatin1String("text2"));
361 QCOMPARE(text1->y(), text2->y());
366 // mostly testing that we don't crash
367 void tst_qdeclarativeanchors::loops()
370 QUrl source(QUrl::fromLocalFile(SRCDIR "/data/loop1.qml"));
372 QString expect = source.toString() + ":6:5: QML Text: Possible anchor loop detected on horizontal anchor.";
373 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
374 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
375 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
377 QDeclarativeView *view = new QDeclarativeView;
378 view->setSource(source);
379 qApp->processEvents();
385 QUrl source(QUrl::fromLocalFile(SRCDIR "/data/loop2.qml"));
387 QString expect = source.toString() + ":8:3: QML Image: Possible anchor loop detected on horizontal anchor.";
388 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
390 QDeclarativeView *view = new QDeclarativeView;
391 view->setSource(source);
392 qApp->processEvents();
398 void tst_qdeclarativeanchors::illegalSets()
400 QFETCH(QString, qml);
401 QFETCH(QString, warning);
403 QTest::ignoreMessage(QtWarningMsg, warning.toLatin1());
405 QDeclarativeEngine engine;
406 QDeclarativeComponent component(&engine);
407 component.setData(QByteArray("import QtQuick 1.0\n" + qml.toUtf8()), QUrl::fromLocalFile(""));
408 if (!component.isReady())
409 qWarning() << "Test errors:" << component.errors();
410 QVERIFY(component.isReady());
411 QObject *o = component.create();
415 void tst_qdeclarativeanchors::illegalSets_data()
417 QTest::addColumn<QString>("qml");
418 QTest::addColumn<QString>("warning");
420 QTest::newRow("H - too many anchors")
421 << "Rectangle { id: rect; Rectangle { anchors.left: rect.left; anchors.right: rect.right; anchors.horizontalCenter: rect.horizontalCenter } }"
422 << "file::2:23: QML Rectangle: Cannot specify left, right, and hcenter anchors.";
424 foreach (const QString &side, QStringList() << "left" << "right") {
425 QTest::newRow("H - anchor to V")
426 << QString("Rectangle { Rectangle { anchors.%1: parent.top } }").arg(side)
427 << "file::2:13: QML Rectangle: Cannot anchor a horizontal edge to a vertical edge.";
429 QTest::newRow("H - anchor to non parent/sibling")
430 << QString("Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.%1: rect.%1 } }").arg(side)
431 << "file::2:45: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.";
433 QTest::newRow("H - anchor to self")
434 << QString("Rectangle { id: rect; anchors.%1: rect.%1 }").arg(side)
435 << "file::2:1: QML Rectangle: Cannot anchor item to self.";
439 QTest::newRow("V - too many anchors")
440 << "Rectangle { id: rect; Rectangle { anchors.top: rect.top; anchors.bottom: rect.bottom; anchors.verticalCenter: rect.verticalCenter } }"
441 << "file::2:23: QML Rectangle: Cannot specify top, bottom, and vcenter anchors.";
443 QTest::newRow("V - too many anchors with baseline")
444 << "Rectangle { Text { id: text1; text: \"Hello\" } Text { anchors.baseline: text1.baseline; anchors.top: text1.top; } }"
445 << "file::2:47: QML Text: Baseline anchor cannot be used in conjunction with top, bottom, or vcenter anchors.";
447 foreach (const QString &side, QStringList() << "top" << "bottom" << "baseline") {
449 QTest::newRow("V - anchor to H")
450 << QString("Rectangle { Rectangle { anchors.%1: parent.left } }").arg(side)
451 << "file::2:13: QML Rectangle: Cannot anchor a vertical edge to a horizontal edge.";
453 QTest::newRow("V - anchor to non parent/sibling")
454 << QString("Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.%1: rect.%1 } }").arg(side)
455 << "file::2:45: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.";
457 QTest::newRow("V - anchor to self")
458 << QString("Rectangle { id: rect; anchors.%1: rect.%1 }").arg(side)
459 << "file::2:1: QML Rectangle: Cannot anchor item to self.";
463 QTest::newRow("centerIn - anchor to non parent/sibling")
464 << "Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.centerIn: rect} }"
465 << "file::2:45: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.";
468 QTest::newRow("fill - anchor to non parent/sibling")
469 << "Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.fill: rect} }"
470 << "file::2:45: QML Rectangle: Cannot anchor to an item that isn't a parent or sibling.";
473 void tst_qdeclarativeanchors::reset()
475 QFETCH(QString, side);
476 QFETCH(QDeclarativeAnchorLine::AnchorLine, anchorLine);
477 QFETCH(QDeclarativeAnchors::Anchor, usedAnchor);
479 QDeclarativeItem *baseItem = new QDeclarativeItem;
481 QDeclarativeAnchorLine anchor;
482 anchor.item = baseItem;
483 anchor.anchorLine = anchorLine;
485 QDeclarativeItem *item = new QDeclarativeItem;
486 QDeclarativeItemPrivate *itemPrivate = QDeclarativeItemPrivate::get(item);
488 const QMetaObject *meta = itemPrivate->anchors()->metaObject();
489 QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
491 QVERIFY(p.write(itemPrivate->anchors(), qVariantFromValue(anchor)));
492 QCOMPARE(itemPrivate->anchors()->usedAnchors().testFlag(usedAnchor), true);
494 QVERIFY(p.reset(itemPrivate->anchors()));
495 QCOMPARE(itemPrivate->anchors()->usedAnchors().testFlag(usedAnchor), false);
501 void tst_qdeclarativeanchors::reset_data()
503 QTest::addColumn<QString>("side");
504 QTest::addColumn<QDeclarativeAnchorLine::AnchorLine>("anchorLine");
505 QTest::addColumn<QDeclarativeAnchors::Anchor>("usedAnchor");
507 QTest::newRow("left") << "left" << QDeclarativeAnchorLine::Left << QDeclarativeAnchors::LeftAnchor;
508 QTest::newRow("top") << "top" << QDeclarativeAnchorLine::Top << QDeclarativeAnchors::TopAnchor;
509 QTest::newRow("right") << "right" << QDeclarativeAnchorLine::Right << QDeclarativeAnchors::RightAnchor;
510 QTest::newRow("bottom") << "bottom" << QDeclarativeAnchorLine::Bottom << QDeclarativeAnchors::BottomAnchor;
512 QTest::newRow("hcenter") << "horizontalCenter" << QDeclarativeAnchorLine::HCenter << QDeclarativeAnchors::HCenterAnchor;
513 QTest::newRow("vcenter") << "verticalCenter" << QDeclarativeAnchorLine::VCenter << QDeclarativeAnchors::VCenterAnchor;
514 QTest::newRow("baseline") << "baseline" << QDeclarativeAnchorLine::Baseline << QDeclarativeAnchors::BaselineAnchor;
517 void tst_qdeclarativeanchors::resetConvenience()
519 QDeclarativeItem *baseItem = new QDeclarativeItem;
520 QDeclarativeItem *item = new QDeclarativeItem;
521 QDeclarativeItemPrivate *itemPrivate = QDeclarativeItemPrivate::get(item);
524 itemPrivate->anchors()->setFill(baseItem);
525 QVERIFY(itemPrivate->anchors()->fill() == baseItem);
526 itemPrivate->anchors()->resetFill();
527 QVERIFY(itemPrivate->anchors()->fill() == 0);
530 itemPrivate->anchors()->setCenterIn(baseItem);
531 QVERIFY(itemPrivate->anchors()->centerIn() == baseItem);
532 itemPrivate->anchors()->resetCenterIn();
533 QVERIFY(itemPrivate->anchors()->centerIn() == 0);
539 void tst_qdeclarativeanchors::nullItem()
541 QFETCH(QString, side);
543 QDeclarativeAnchorLine anchor;
544 QDeclarativeItem *item = new QDeclarativeItem;
545 QDeclarativeItemPrivate *itemPrivate = QDeclarativeItemPrivate::get(item);
547 const QMetaObject *meta = itemPrivate->anchors()->metaObject();
548 QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
550 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML Item: Cannot anchor to a null item.");
551 QVERIFY(p.write(itemPrivate->anchors(), qVariantFromValue(anchor)));
556 void tst_qdeclarativeanchors::nullItem_data()
558 QTest::addColumn<QString>("side");
560 QTest::newRow("left") << "left";
561 QTest::newRow("top") << "top";
562 QTest::newRow("right") << "right";
563 QTest::newRow("bottom") << "bottom";
565 QTest::newRow("hcenter") << "horizontalCenter";
566 QTest::newRow("vcenter") << "verticalCenter";
567 QTest::newRow("baseline") << "baseline";
570 void tst_qdeclarativeanchors::crash1()
572 QUrl source(QUrl::fromLocalFile(SRCDIR "/data/crash1.qml"));
574 QString expect = source.toString() + ":4:5: QML Text: Possible anchor loop detected on fill.";
576 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
578 // QT-3245 ... anchor loop detection needs improving.
579 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
580 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
581 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
582 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
583 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
584 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
585 QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
587 QDeclarativeView *view = new QDeclarativeView(source);
588 qApp->processEvents();
593 void tst_qdeclarativeanchors::fill()
595 QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/fill.qml"));
597 qApp->processEvents();
598 QDeclarativeRectangle* rect = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("filler"));
599 QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
600 QCOMPARE(rect->x(), 0.0 + 10.0);
601 QCOMPARE(rect->y(), 0.0 + 30.0);
602 QCOMPARE(rect->width(), 200.0 - 10.0 - 20.0);
603 QCOMPARE(rect->height(), 200.0 - 30.0 - 40.0);
604 //Alter Offsets (tests QTBUG-6631)
605 rectPrivate->anchors()->setLeftMargin(20.0);
606 rectPrivate->anchors()->setRightMargin(0.0);
607 rectPrivate->anchors()->setBottomMargin(0.0);
608 rectPrivate->anchors()->setTopMargin(10.0);
609 QCOMPARE(rect->x(), 0.0 + 20.0);
610 QCOMPARE(rect->y(), 0.0 + 10.0);
611 QCOMPARE(rect->width(), 200.0 - 20.0);
612 QCOMPARE(rect->height(), 200.0 - 10.0);
617 void tst_qdeclarativeanchors::fillRTL()
619 QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/fill.qml"));
621 qApp->processEvents();
622 QDeclarativeRectangle* rect = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("filler"));
623 QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
626 QCOMPARE(rect->x(), 0.0 + 20.0);
627 QCOMPARE(rect->y(), 0.0 + 30.0);
628 QCOMPARE(rect->width(), 200.0 - 10.0 - 20.0);
629 QCOMPARE(rect->height(), 200.0 - 30.0 - 40.0);
630 //Alter Offsets (tests QTBUG-6631)
631 rectPrivate->anchors()->setLeftMargin(20.0);
632 rectPrivate->anchors()->setRightMargin(0.0);
633 rectPrivate->anchors()->setBottomMargin(0.0);
634 rectPrivate->anchors()->setTopMargin(10.0);
635 QCOMPARE(rect->x(), 0.0 + 0.0);
636 QCOMPARE(rect->y(), 0.0 + 10.0);
637 QCOMPARE(rect->width(), 200.0 - 20.0);
638 QCOMPARE(rect->height(), 200.0 - 10.0);
642 void tst_qdeclarativeanchors::centerIn()
644 QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/centerin.qml"));
646 qApp->processEvents();
647 QDeclarativeRectangle* rect = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("centered"));
648 QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
650 QCOMPARE(rect->x(), 75.0 + 10);
651 QCOMPARE(rect->y(), 75.0 + 30);
652 //Alter Offsets (tests QTBUG-6631)
653 rectPrivate->anchors()->setHorizontalCenterOffset(-20.0);
654 rectPrivate->anchors()->setVerticalCenterOffset(-10.0);
655 QCOMPARE(rect->x(), 75.0 - 20.0);
656 QCOMPARE(rect->y(), 75.0 - 10.0);
662 void tst_qdeclarativeanchors::centerInRTL()
664 QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/centerin.qml"));
666 qApp->processEvents();
667 QDeclarativeRectangle* rect = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("centered"));
668 QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
671 QCOMPARE(rect->x(), 75.0 - 10);
672 QCOMPARE(rect->y(), 75.0 + 30);
673 //Alter Offsets (tests QTBUG-6631)
674 rectPrivate->anchors()->setHorizontalCenterOffset(-20.0);
675 rectPrivate->anchors()->setVerticalCenterOffset(-10.0);
676 QCOMPARE(rect->x(), 75.0 + 20.0);
677 QCOMPARE(rect->y(), 75.0 - 10.0);
682 void tst_qdeclarativeanchors::hvCenter()
684 QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/hvCenter.qml"));
686 qApp->processEvents();
687 QDeclarativeRectangle* rect = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("centered"));
688 QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
691 QCOMPARE(rect->x(), 10.0);
692 QCOMPARE(rect->y(), 19.0);
694 rectPrivate->anchors()->setHorizontalCenterOffset(-5.0);
695 rectPrivate->anchors()->setVerticalCenterOffset(5.0);
696 QCOMPARE(rect->x(), 10.0 - 5.0);
697 QCOMPARE(rect->y(), 19.0 + 5.0);
702 void tst_qdeclarativeanchors::hvCenterRTL()
704 QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/hvCenter.qml"));
706 qApp->processEvents();
707 QDeclarativeRectangle* rect = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("centered"));
708 QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
712 QCOMPARE(rect->x(), 10.0);
713 QCOMPARE(rect->y(), 19.0);
715 rectPrivate->anchors()->setHorizontalCenterOffset(-5.0);
716 rectPrivate->anchors()->setVerticalCenterOffset(5.0);
717 QCOMPARE(rect->x(), 10.0 + 5.0);
718 QCOMPARE(rect->y(), 19.0 + 5.0);
722 void tst_qdeclarativeanchors::margins()
724 QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/margins.qml"));
726 qApp->processEvents();
727 QDeclarativeRectangle* rect = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("filler"));
728 QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
729 QCOMPARE(rect->x(), 5.0);
730 QCOMPARE(rect->y(), 6.0);
731 QCOMPARE(rect->width(), 200.0 - 5.0 - 10.0);
732 QCOMPARE(rect->height(), 200.0 - 6.0 - 10.0);
734 rectPrivate->anchors()->setTopMargin(0.0);
735 rectPrivate->anchors()->setMargins(20.0);
737 QCOMPARE(rect->x(), 5.0);
738 QCOMPARE(rect->y(), 20.0);
739 QCOMPARE(rect->width(), 200.0 - 5.0 - 20.0);
740 QCOMPARE(rect->height(), 200.0 - 20.0 - 20.0);
745 void tst_qdeclarativeanchors::marginsRTL()
747 QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/margins.qml"));
749 QDeclarativeRectangle* rect = findItem<QDeclarativeRectangle>(view->rootObject(), QLatin1String("filler"));
750 QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
753 QCOMPARE(rect->x(), 10.0);
754 QCOMPARE(rect->y(), 6.0);
755 QCOMPARE(rect->width(), 200.0 - 5.0 - 10.0);
756 QCOMPARE(rect->height(), 200.0 - 6.0 - 10.0);
758 rectPrivate->anchors()->setTopMargin(0.0);
759 rectPrivate->anchors()->setMargins(20.0);
761 QCOMPARE(rect->x(), 20.0);
762 QCOMPARE(rect->y(), 20.0);
763 QCOMPARE(rect->width(), 200.0 - 5.0 - 20.0);
764 QCOMPARE(rect->height(), 200.0 - 20.0 - 20.0);
770 QTEST_MAIN(tst_qdeclarativeanchors)
772 #include "tst_qdeclarativeanchors.moc"