tests/auto/qlayout/tst_qlayout.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     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
       
    14 ** this package.
       
    15 **
       
    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.
       
    23 **
       
    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.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <qcoreapplication.h>
       
    46 #include <qdebug.h>
       
    47 #include <qboxlayout.h>
       
    48 #include <qmenubar.h>
       
    49 #include <qdialog.h>
       
    50 #include <qsizegrip.h>
       
    51 #include <qlabel.h>
       
    52 #include <QtGui/QFrame>
       
    53 #include <QtGui/QWindowsStyle>
       
    54 #include <QtGui/QSizePolicy>
       
    55 #include <QPushButton>
       
    56 #include <QRadioButton>
       
    57 #include <private/qlayoutengine_p.h>
       
    58 
       
    59 #ifdef Q_OS_MAC
       
    60 # include <QtGui/QMacStyle>
       
    61 #endif
       
    62 
       
    63 //TESTED_CLASS=
       
    64 //TESTED_FILES=
       
    65 
       
    66 class tst_QLayout : public QObject
       
    67 {
       
    68 Q_OBJECT
       
    69 
       
    70 public:
       
    71     tst_QLayout();
       
    72     virtual ~tst_QLayout();
       
    73 
       
    74 private slots:
       
    75     void getSetCheck();
       
    76     void geometry();
       
    77     void smartMaxSize();
       
    78     void setLayoutBugs();
       
    79 #ifdef QT3_SUPPORT
       
    80     void task193350_sizeGrip();
       
    81 #endif
       
    82     void setContentsMargins();
       
    83     void layoutItemRect();
       
    84     void warnIfWrongParent();
       
    85     void controlTypes();
       
    86     void adjustSizeShouldMakeSureLayoutIsActivated();
       
    87 };
       
    88 
       
    89 tst_QLayout::tst_QLayout()
       
    90 {
       
    91 }
       
    92 
       
    93 tst_QLayout::~tst_QLayout()
       
    94 {
       
    95 }
       
    96 
       
    97 // Testing get/set functions
       
    98 void tst_QLayout::getSetCheck()
       
    99 {
       
   100     QBoxLayout obj1(QBoxLayout::LeftToRight);
       
   101     // QWidget * QLayout::menuBar()
       
   102     // void QLayout::setMenuBar(QWidget *)
       
   103     QMenuBar *var1 = new QMenuBar();
       
   104     obj1.setMenuBar(var1);
       
   105     QCOMPARE(static_cast<QWidget *>(var1), obj1.menuBar());
       
   106     obj1.setMenuBar((QWidget *)0);
       
   107     QCOMPARE((QWidget *)0, obj1.menuBar());
       
   108     delete var1;
       
   109 }
       
   110 
       
   111 class SizeHinterFrame : public QFrame
       
   112 {
       
   113 public:
       
   114     SizeHinterFrame(const QSize &sh, const QSize &msh = QSize())
       
   115     : QFrame(0), sh(sh), msh(msh) {
       
   116         setFrameStyle(QFrame::Box | QFrame::Plain);
       
   117     }
       
   118 
       
   119 
       
   120 
       
   121     void setSizeHint(const QSize &s) { sh = s; }
       
   122     QSize sizeHint() const { return sh; }
       
   123     QSize minimumSizeHint() const { return msh; }
       
   124 
       
   125 private:
       
   126     QSize sh;
       
   127     QSize msh;
       
   128 };
       
   129 
       
   130 
       
   131 void tst_QLayout::geometry()
       
   132 {
       
   133     // For QWindowsStyle we know that QWidgetItem::geometry() and QWidget::geometry()
       
   134     // should be the same.
       
   135     QApplication::setStyle(new QWindowsStyle);
       
   136     QWidget w;
       
   137     QVBoxLayout layout(&w);
       
   138     SizeHinterFrame widget(QSize(100,100));
       
   139     layout.addWidget(&widget);
       
   140     QLayoutItem *item = layout.itemAt(0);
       
   141     w.show();
       
   142     QApplication::processEvents();
       
   143     QCOMPARE(item->geometry().size(), QSize(100,100));
       
   144 
       
   145     widget.setMinimumSize(QSize(110,110));
       
   146     QCOMPARE(item->geometry().size(), QSize(110,110));
       
   147 
       
   148     widget.setMinimumSize(QSize(0,0));
       
   149     widget.setMaximumSize(QSize(90,90));
       
   150     widget.setSizeHint(QSize(100,100));
       
   151     QCOMPARE(item->geometry().size(), QSize(90,90));
       
   152 }
       
   153 
       
   154 void tst_QLayout::smartMaxSize()
       
   155 {
       
   156     QVector<int> expectedWidths; 
       
   157 
       
   158     QFile f(QLatin1String("baseline/smartmaxsize"));
       
   159     QCOMPARE(f.open(QIODevice::ReadOnly | QIODevice::Text), true);
       
   160 
       
   161     QTextStream stream(&f);
       
   162 
       
   163     while(!stream.atEnd()) {
       
   164         QString line = stream.readLine(200);
       
   165         expectedWidths.append(line.section(QLatin1Char(' '), 6, -1, QString::SectionSkipEmpty).toInt());
       
   166     }
       
   167     f.close();
       
   168 
       
   169     int sizeCombinations[] = { 0, 10, 20, QWIDGETSIZE_MAX};
       
   170     QSizePolicy::Policy policies[] = {  QSizePolicy::Fixed, 
       
   171                                         QSizePolicy::Minimum, 
       
   172                                         QSizePolicy::Maximum, 
       
   173                                         QSizePolicy::Preferred,
       
   174                                         QSizePolicy::Expanding,
       
   175                                         QSizePolicy::MinimumExpanding,
       
   176                                         QSizePolicy::Ignored
       
   177                                         };
       
   178     Qt::Alignment alignments[] = {  0,
       
   179                                     Qt::AlignLeft,
       
   180                                     Qt::AlignRight,
       
   181                                     Qt::AlignHCenter
       
   182                                     };
       
   183 
       
   184     int expectedIndex = 0;
       
   185     int regressionCount = 0;
       
   186     for (int p = 0; p < sizeof(policies)/sizeof(QSizePolicy::Policy); ++p) {
       
   187         QSizePolicy sizePolicy;
       
   188         sizePolicy.setHorizontalPolicy(policies[p]);
       
   189         for (int min = 0; min < sizeof(sizeCombinations)/sizeof(int); ++min) {
       
   190             int minSize = sizeCombinations[min];
       
   191             for (int max = 0; max < sizeof(sizeCombinations)/sizeof(int); ++max) {
       
   192                 int maxSize = sizeCombinations[max];
       
   193                 for (int sh = 0; sh < sizeof(sizeCombinations)/sizeof(int); ++sh) {
       
   194                     int sizeHint = sizeCombinations[sh];
       
   195                     for (int a = 0; a < sizeof(alignments)/sizeof(int); ++a) {
       
   196                         Qt::Alignment align = alignments[a];
       
   197                         QSize sz = qSmartMaxSize(QSize(sizeHint, 1), QSize(minSize, 1), QSize(maxSize, 1), sizePolicy, align);
       
   198                         int width = sz.width();
       
   199 #if 0
       
   200                         qDebug() << expectedIndex << sizePolicy.horizontalPolicy() << align << minSize << sizeHint << maxSize << width;
       
   201 #else
       
   202                         int expectedWidth = expectedWidths[expectedIndex];
       
   203                         if (width != expectedWidth) {
       
   204                             qDebug() << "error at index" << expectedIndex << ":" << sizePolicy.horizontalPolicy() << align << minSize << sizeHint << maxSize << width;
       
   205                             ++regressionCount;
       
   206                         }
       
   207 #endif
       
   208                         ++expectedIndex;
       
   209                     }
       
   210                 }
       
   211             }
       
   212         }
       
   213     }
       
   214     QCOMPARE(regressionCount, 0);
       
   215 }
       
   216 
       
   217 void tst_QLayout::setLayoutBugs()
       
   218 {
       
   219     QWidget widget(0);
       
   220     QHBoxLayout *hBoxLayout = new QHBoxLayout(&widget);
       
   221 
       
   222     for(int i = 0; i < 6; ++i) {
       
   223         QPushButton *pushButton = new QPushButton("Press me!", &widget);
       
   224         hBoxLayout->addWidget(pushButton);
       
   225     }
       
   226 
       
   227     widget.setLayout(hBoxLayout);
       
   228     QVERIFY(widget.layout() == hBoxLayout);
       
   229 
       
   230     QWidget containerWidget(0);
       
   231     containerWidget.setLayout(widget.layout());
       
   232     QVERIFY(widget.layout() == 0);
       
   233     QVERIFY(containerWidget.layout() == hBoxLayout);
       
   234 }
       
   235 
       
   236 #ifdef QT3_SUPPORT
       
   237 void tst_QLayout::task193350_sizeGrip()
       
   238 {
       
   239     QDialog dialog;
       
   240     dialog.setSizeGripEnabled(true);
       
   241 
       
   242     QVBoxLayout* layout = new QVBoxLayout(&dialog);
       
   243     layout->setAutoAdd(true);
       
   244     new QLabel("Label", &dialog);
       
   245 
       
   246     dialog.show();
       
   247     QCOMPARE(layout->indexOf(qFindChild<QSizeGrip *>(&dialog)),-1);
       
   248 }
       
   249 #endif
       
   250 
       
   251 class MyLayout : public QLayout
       
   252 {
       
   253     public:
       
   254         MyLayout() : invalidated(false) {}
       
   255         virtual void invalidate() {invalidated = true;}
       
   256         bool invalidated;
       
   257         QSize sizeHint() const {return QSize();}
       
   258         void addItem(QLayoutItem*) {}
       
   259         QLayoutItem* itemAt(int) const {return 0;}
       
   260         QLayoutItem* takeAt(int) {return 0;}
       
   261         int count() const {return 0;}
       
   262 };
       
   263 
       
   264 void tst_QLayout::setContentsMargins()
       
   265 {
       
   266     MyLayout layout;
       
   267     layout.invalidated = false;
       
   268     int left, top, right, bottom;
       
   269     
       
   270     layout.setContentsMargins(52, 53, 54, 55);
       
   271     QVERIFY(layout.invalidated);
       
   272     layout.invalidated = false;
       
   273 
       
   274     layout.getContentsMargins(&left, &top, &right, &bottom);
       
   275     QCOMPARE(left, 52);
       
   276     QCOMPARE(top, 53);
       
   277     QCOMPARE(right, 54);
       
   278     QCOMPARE(bottom, 55);
       
   279  
       
   280     layout.setContentsMargins(52, 53, 54, 55);
       
   281     QVERIFY(!layout.invalidated);
       
   282 }
       
   283 
       
   284 class EventReceiver : public QObject
       
   285 {
       
   286 public:
       
   287     bool eventFilter(QObject *watched, QEvent *event)
       
   288     {
       
   289         if (event->type() == QEvent::Show) {
       
   290             geom = static_cast<QWidget*>(watched)->geometry();
       
   291         }
       
   292         return false;
       
   293     }
       
   294     QRect geom;
       
   295 };
       
   296 
       
   297 void tst_QLayout::layoutItemRect()
       
   298 {
       
   299 #ifdef Q_OS_MAC
       
   300     if (qobject_cast<QMacStyle*>(QApplication::style())) {
       
   301         QWidget *window = new QWidget;
       
   302         QRadioButton *radio = new QRadioButton(window);
       
   303         QWidgetItem item(radio);
       
   304         EventReceiver eventReceiver;
       
   305         radio->installEventFilter(&eventReceiver);
       
   306 
       
   307         radio->show();
       
   308         QApplication::processEvents();
       
   309         QApplication::processEvents();
       
   310         QSize s = item.sizeHint();
       
   311 
       
   312         item.setAlignment(Qt::AlignVCenter);
       
   313         item.setGeometry(QRect(QPoint(0, 0), s));
       
   314 
       
   315         QCOMPARE(radio->geometry().size(), radio->sizeHint());
       
   316         delete radio;
       
   317     }
       
   318 #endif
       
   319 }
       
   320 
       
   321 void tst_QLayout::warnIfWrongParent()
       
   322 {
       
   323     QWidget root;
       
   324     QHBoxLayout lay;
       
   325     lay.setParent(&root);
       
   326     QTest::ignoreMessage(QtWarningMsg, "QLayout::parentWidget: A layout can only have another layout as a parent.");
       
   327     QCOMPARE(lay.parentWidget(), static_cast<QWidget*>(0));
       
   328 }
       
   329 
       
   330 void tst_QLayout::controlTypes()
       
   331 {
       
   332     QVBoxLayout layout;
       
   333     QCOMPARE(layout.controlTypes(), QSizePolicy::DefaultType);
       
   334     QSizePolicy p;
       
   335     QCOMPARE(p.controlType(),QSizePolicy::DefaultType);
       
   336 
       
   337 }
       
   338 
       
   339 void tst_QLayout::adjustSizeShouldMakeSureLayoutIsActivated()
       
   340 {
       
   341     QWidget main;
       
   342 
       
   343     QVBoxLayout *const layout = new QVBoxLayout(&main);
       
   344     layout->setMargin(0);
       
   345     SizeHinterFrame *frame = new SizeHinterFrame(QSize(200, 10), QSize(200, 8));
       
   346     frame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
       
   347     layout->addWidget(frame);
       
   348 
       
   349     SizeHinterFrame *frame2 = new SizeHinterFrame(QSize(200, 10), QSize(200, 8));
       
   350     frame2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
       
   351     layout->addWidget(frame2);
       
   352 
       
   353     main.show();
       
   354 
       
   355     frame2->hide();
       
   356     main.adjustSize();
       
   357     QCOMPARE(main.size(), QSize(200, 10));
       
   358 }
       
   359 
       
   360 QTEST_MAIN(tst_QLayout)
       
   361 #include "tst_qlayout.moc"