diff options
Diffstat (limited to 'tests/auto/qgraphicslinearlayout')
-rw-r--r-- | tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp | 125 |
1 files changed, 81 insertions, 44 deletions
diff --git a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp index 8409ec7e..4f28df4 100644 --- a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp +++ b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp @@ -46,6 +46,8 @@ #include <qgraphicswidget.h> #include <qgraphicsscene.h> #include <qgraphicsview.h> +#include <qapplication.h> +#include <qplastiquestyle.h> class tst_QGraphicsLinearLayout : public QObject { Q_OBJECT @@ -76,6 +78,7 @@ private slots: void invalidate(); void itemAt_data(); void itemAt(); + void itemAt_visualOrder(); void orientation_data(); void orientation(); void removeAt_data(); @@ -140,6 +143,13 @@ public: // It is only called once. void tst_QGraphicsLinearLayout::initTestCase() { + // since the style will influence the results, we have to ensure + // that the tests are run using the same style on all platforms +#ifdef Q_WS_S60 + QApplication::setStyle(new QWindowsStyle); +#else + QApplication::setStyle(new QPlastiqueStyle); +#endif } // This will be called after the last test function is executed. @@ -163,19 +173,19 @@ class RectWidget : public QGraphicsWidget public: RectWidget(QGraphicsItem *parent = 0, const QBrush &brush = QBrush()) : QGraphicsWidget(parent){ m_brush = brush;} - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->setBrush(m_brush); painter->drawRoundRect(rect()); } - + void setSizeHint(Qt::SizeHint which, const QSizeF &size) { m_sizeHints[which] = size; updateGeometry(); } - + virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const { if (m_sizeHints[which].isValid()) { return m_sizeHints[which]; @@ -210,17 +220,16 @@ void tst_QGraphicsLinearLayout::qgraphicslinearlayout() layout.setOrientation(Qt::Vertical); layout.orientation(); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::insertItem: cannot insert null item"); - //QCOMPARE(layout.count(), 0); + QCOMPARE(layout.count(), 0); layout.addItem(0); - //QCOMPARE(layout.count(), 0); + QCOMPARE(layout.count(), 0); layout.addStretch(0); - //QCOMPARE(layout.count(), 1); + QCOMPARE(layout.count(), 0); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::insertItem: cannot insert null item"); layout.insertItem(0, 0); layout.insertStretch(0, 0); layout.removeItem(0); - layout.removeAt(0); - //QCOMPARE(layout.count(), 1); + QCOMPARE(layout.count(), 0); layout.setSpacing(0); layout.spacing(); QTest::ignoreMessage(QtWarningMsg, "QGraphicsLinearLayout::setStretchFactor: cannot assign a stretch factor to a null item"); @@ -231,8 +240,7 @@ void tst_QGraphicsLinearLayout::qgraphicslinearlayout() QCOMPARE(layout.alignment(0), 0); layout.setGeometry(QRectF()); layout.geometry(); - //QCOMPARE(layout.count(), 1); - layout.itemAt(0); + QCOMPARE(layout.count(), 0); layout.invalidate(); layout.sizeHint(Qt::MinimumSize, QSizeF()); } @@ -285,7 +293,7 @@ void tst_QGraphicsLinearLayout::alignment() int i; bool addWidget = true; for (i = 0; i < sizeof(alignmentsToTest)/sizeof(Qt::Alignment); ++i) { - QGraphicsLayoutItem *loutItem; + QGraphicsLayoutItem *loutItem; Qt::Alignment align = alignmentsToTest[i]; if (!align && i > 0) addWidget = false; @@ -537,7 +545,10 @@ void tst_QGraphicsLinearLayout::insertItem() QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); layout.insertItem(insertItemAt, item); QCOMPARE(layout.count(), itemCount + layoutCount + 1); - QCOMPARE(layout.itemAt(insertItemAt), insertItemAt == -1 ? (QGraphicsLayoutItem*)0 : item); + + if (insertItemAt >= 0 && (itemCount + layoutCount >= 0)) { + QCOMPARE(layout.itemAt(insertItemAt), item); + } layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -599,8 +610,7 @@ void tst_QGraphicsLinearLayout::insertStretch() } widget->setLayout(layout); layout->insertStretch(insertItemAt, stretch); - QCOMPARE(layout->itemAt(insertItemAt), (QGraphicsLayoutItem*)0); - QCOMPARE(layout->count(), itemCount + layoutCount + 1); + QCOMPARE(layout->count(), itemCount + layoutCount); layout->activate(); view.show(); @@ -669,7 +679,6 @@ void tst_QGraphicsLinearLayout::invalidate() void tst_QGraphicsLinearLayout::itemAt_data() { QTest::addColumn<int>("index"); - QTest::newRow("-1") << -1; QTest::newRow("0") << 0; QTest::newRow("1") << 1; QTest::newRow("2") << 2; @@ -681,7 +690,32 @@ void tst_QGraphicsLinearLayout::itemAt() // see also the insertItem() etc tests QFETCH(int, index); SubQGraphicsLinearLayout layout; - QCOMPARE(layout.itemAt(index), (QGraphicsLayoutItem*)0); + for (int i = 0; i < 3; ++i) + layout.addItem(new QGraphicsWidget); + + QVERIFY(layout.itemAt(index) != 0); +} + +void tst_QGraphicsLinearLayout::itemAt_visualOrder() +{ + QGraphicsLinearLayout *l = new QGraphicsLinearLayout; + + QGraphicsWidget *w1 = new QGraphicsWidget; + l->addItem(w1); + + QGraphicsWidget *w3 = new QGraphicsWidget; + l->addItem(w3); + + QGraphicsWidget *w0 = new QGraphicsWidget; + l->insertItem(0, w0); + + QGraphicsWidget *w2 = new QGraphicsWidget; + l->insertItem(2, w2); + + QCOMPARE(l->itemAt(0), static_cast<QGraphicsLayoutItem*>(w0)); + QCOMPARE(l->itemAt(1), static_cast<QGraphicsLayoutItem*>(w1)); + QCOMPARE(l->itemAt(2), static_cast<QGraphicsLayoutItem*>(w2)); + QCOMPARE(l->itemAt(3), static_cast<QGraphicsLayoutItem*>(w3)); } void tst_QGraphicsLinearLayout::orientation_data() @@ -695,7 +729,7 @@ void tst_QGraphicsLinearLayout::orientation_data() void tst_QGraphicsLinearLayout::orientation() { QFETCH(Qt::Orientation, orientation); - + QGraphicsScene scene; QGraphicsView view(&scene); QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); @@ -721,7 +755,7 @@ void tst_QGraphicsLinearLayout::orientation() qreal pos; if (initialOrientation == Qt::Horizontal) pos = item->pos().x(); - else + else pos = item->pos().y(); positions.append(pos); @@ -736,7 +770,7 @@ void tst_QGraphicsLinearLayout::orientation() QGraphicsWidget *item = static_cast<QGraphicsWidget*>(layout.itemAt(i)); if (initialOrientation == Qt::Horizontal) QCOMPARE(item->pos().y(), positions.at(i)); - else + else QCOMPARE(item->pos().x(), positions.at(i)); } @@ -779,19 +813,18 @@ void tst_QGraphicsLinearLayout::removeAt() layout.addItem(new SubQGraphicsLinearLayout); QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); - QGraphicsLayoutItem *w = layout.itemAt(removeItemAt); - QGraphicsLayoutItem *wParent = 0; + QGraphicsLayoutItem *w = 0; + if (removeItemAt >= 0 && removeItemAt < layout.count()) + w = layout.itemAt(removeItemAt); if (w) { - wParent = w->parentLayoutItem(); + QGraphicsLayoutItem *wParent = w->parentLayoutItem(); QCOMPARE(wParent, static_cast<QGraphicsLayoutItem *>(&layout)); - } - layout.removeAt(removeItemAt); - if (w) { + layout.removeAt(removeItemAt); wParent = w->parentLayoutItem(); QCOMPARE(wParent, static_cast<QGraphicsLayoutItem *>(0)); + delete w; } - delete w; - QCOMPARE(layout.count(), itemCount + layoutCount - ((removeItemAt == -1) ? 0 : 1)); + QCOMPARE(layout.count(), itemCount + layoutCount - (w ? 1 : 0)); layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -829,11 +862,15 @@ void tst_QGraphicsLinearLayout::removeItem() for (int i = 0; i < layoutCount; ++i) layout.addItem(new SubQGraphicsLinearLayout); - QGraphicsLayoutItem *w = layout.itemAt(removeItemAt); + QGraphicsLayoutItem *w = 0; + if (removeItemAt >= 0 && removeItemAt < layout.count()) + w = layout.itemAt(removeItemAt); QSizeF oldSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); - layout.removeItem(w); - delete w; - QCOMPARE(layout.count(), itemCount + layoutCount - ((removeItemAt == -1) ? 0 : 1)); + if (w) { + layout.removeItem(w); + delete w; + } + QCOMPARE(layout.count(), itemCount + layoutCount - (w ? 1 : 0)); layout.activate(); QSizeF newSizeHint = layout.sizeHint(Qt::PreferredSize, QSizeF()); @@ -882,7 +919,7 @@ void tst_QGraphicsLinearLayout::setSpacing_data() void tst_QGraphicsLinearLayout::setSpacing() { QFETCH(qreal, spacing); - + QGraphicsScene scene; QGraphicsView view(&scene); QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); @@ -994,7 +1031,7 @@ void tst_QGraphicsLinearLayout::itemSpacing() /** * The stretch factors are not applied linearly, but they are used together with both the preferred size, maximum size to form the * internal effective stretch factor. - * There is only need to apply stretch factors if the size of the layout is different than the layouts preferred size. + * There is only need to apply stretch factors if the size of the layout is different than the layouts preferred size. * (If the size of the layout is the preferred size, then all items should get their preferred sizes. * However, imagine this use case: * Layout @@ -1007,7 +1044,7 @@ void tst_QGraphicsLinearLayout::itemSpacing() * layout->resize(120, h) * * In QLayout, C would become 50, B would become 50 and A would get 20. When scaling a layout this would give a jerky feeling, since - * the item with the highest stretch factor will first resize. When that has reached its maximum the next candidate for stretch will + * the item with the highest stretch factor will first resize. When that has reached its maximum the next candidate for stretch will * resize, and finally, item with the lowest stretch factor will resize. * In QGraphicsLinearLayout we try to scale all items so that they all reach their maximum at the same time. This means that * their relative sizes are not proportional to their stretch factors. @@ -1059,7 +1096,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() view.show(); widget->show(); - + qreal firstStretch = -1; qreal firstExtent = -1.; qreal sumExtent = 0; @@ -1073,7 +1110,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() QCOMPARE(extent, firstExtent); else if (stretch > firstStretch) QVERIFY(extent > firstExtent); - else + else QVERIFY(extent < firstExtent); } else { firstStretch = (qreal)stretch; @@ -1082,7 +1119,7 @@ void tst_QGraphicsLinearLayout::setStretchFactor() sumExtent+= extent; } QCOMPARE(sumExtent, totalSize); - + delete widget; } @@ -1167,7 +1204,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() QSizePolicy sp = item->sizePolicy(); if (orientation == Qt::Horizontal) sp.setHorizontalPolicy(QSizePolicy::Policy(sp.horizontalPolicy() | QSizePolicy::IgnoreFlag)); - else + else sp.setVerticalPolicy(QSizePolicy::Policy(sp.verticalPolicy() | QSizePolicy::IgnoreFlag)); item->setSizePolicy(sp); } @@ -1180,7 +1217,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() view.resize(400,300); if (newSize.isValid()) widget->resize(newSize); - + QApplication::processEvents(); for (i = 0; i < count; ++i) { QSizeF itemSize = layout->itemAt(i)->geometry().size(); @@ -1189,7 +1226,7 @@ void tst_QGraphicsLinearLayout::defaultStretchFactors() if (i < expectedSizes.count()) QCOMPARE(itemSize.width(), qreal(expectedSizes.at(i))); } - + delete widget; } @@ -1254,7 +1291,7 @@ void tst_QGraphicsLinearLayout::updateGeometry() layout->setContentsMargins(0, 0, 0, 0); QCOMPARE(w1->parentLayoutItem(), static_cast<QGraphicsLayoutItem*>(layout)); QCOMPARE(layout->parentLayoutItem(), static_cast<QGraphicsLayoutItem*>(window)); - + view.show(); QApplication::processEvents(); QCOMPARE(window->size().toSize(), QSize(100, 50)); @@ -1283,7 +1320,7 @@ void tst_QGraphicsLinearLayout::updateGeometry() QApplication::processEvents(); w1->setMinimumSize(120, 60); QApplication::processEvents(); - QCOMPARE(window->size().toSize(), QSize(124, 64)); + QCOMPARE(window->size().toSize(), QSize(124, 64)); } { @@ -1343,7 +1380,7 @@ void tst_QGraphicsLinearLayout::layoutDirection() w2->setPreferredSize(20, 20); w2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); layout->addItem(w2); - + scene.addItem(window); window->setLayout(layout); view.show(); @@ -1361,7 +1398,7 @@ void tst_QGraphicsLinearLayout::layoutDirection() QCOMPARE(w1->geometry().left(), 29.0); QCOMPARE(w2->geometry().right(), 23.0); QCOMPARE(w2->geometry().left(), 3.0); - + delete window; } |