tests/auto/qboxlayout/tst_qboxlayout.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 #include <QtGui>
       
    45 
       
    46 //TESTED_CLASS=
       
    47 //TESTED_FILES=
       
    48 
       
    49 class tst_QBoxLayout : public QObject
       
    50 {
       
    51     Q_OBJECT
       
    52 
       
    53 public:
       
    54     tst_QBoxLayout();
       
    55     virtual ~tst_QBoxLayout();
       
    56 
       
    57 public slots:
       
    58     void initTestCase();
       
    59     void cleanupTestCase();
       
    60     void init();
       
    61     void cleanup();
       
    62 
       
    63 private slots:
       
    64     void insertSpacerItem();
       
    65     void sizeHint();
       
    66     void sizeConstraints();
       
    67     void setGeometry();
       
    68     void setStyleShouldChangeSpacing();
       
    69 };
       
    70 
       
    71 class CustomLayoutStyle : public QWindowsStyle
       
    72 {
       
    73     Q_OBJECT
       
    74 public:
       
    75     CustomLayoutStyle() : QWindowsStyle()
       
    76     {
       
    77         hspacing = 5;
       
    78         vspacing = 10;
       
    79     }
       
    80 
       
    81     virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0,
       
    82                             const QWidget * widget = 0 ) const;
       
    83 
       
    84     int hspacing;
       
    85     int vspacing;
       
    86 };
       
    87 
       
    88 int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/,
       
    89                                    const QWidget * widget /*= 0*/ ) const
       
    90 {
       
    91     switch (metric) {
       
    92         case PM_LayoutLeftMargin:
       
    93             return 0;
       
    94         break;
       
    95         case PM_LayoutTopMargin:
       
    96             return 3;
       
    97         break;
       
    98         case PM_LayoutRightMargin:
       
    99             return 6;
       
   100         break;
       
   101         case PM_LayoutBottomMargin:
       
   102             return 9;
       
   103         break;
       
   104         case PM_LayoutHorizontalSpacing:
       
   105             return hspacing;
       
   106         case PM_LayoutVerticalSpacing:
       
   107             return vspacing;
       
   108         break;
       
   109         default:
       
   110             break;
       
   111     }
       
   112     return QWindowsStyle::pixelMetric(metric, option, widget);
       
   113 }
       
   114 
       
   115 
       
   116 tst_QBoxLayout::tst_QBoxLayout()
       
   117 {
       
   118 }
       
   119 
       
   120 tst_QBoxLayout::~tst_QBoxLayout()
       
   121 {
       
   122 }
       
   123 
       
   124 void tst_QBoxLayout::initTestCase()
       
   125 {
       
   126 }
       
   127 
       
   128 void tst_QBoxLayout::cleanupTestCase()
       
   129 {
       
   130 }
       
   131 
       
   132 void tst_QBoxLayout::init()
       
   133 {
       
   134 }
       
   135 
       
   136 void tst_QBoxLayout::cleanup()
       
   137 {
       
   138 }
       
   139 
       
   140 void tst_QBoxLayout::insertSpacerItem()
       
   141 {
       
   142     QWidget *window = new QWidget;
       
   143 
       
   144     QSpacerItem *spacer1 = new QSpacerItem(20, 10, QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   145     QSpacerItem *spacer2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   146 
       
   147     QBoxLayout *layout = new QHBoxLayout;
       
   148     layout->addWidget(new QLineEdit("Foooooooooooooooooooooooooo"));
       
   149     layout->addSpacerItem(spacer1);
       
   150     layout->addWidget(new QLineEdit("Baaaaaaaaaaaaaaaaaaaaaaaaar"));
       
   151     layout->insertSpacerItem(0, spacer2);
       
   152     window->setLayout(layout);
       
   153 
       
   154     QVERIFY(layout->itemAt(0) == spacer2);
       
   155     QVERIFY(layout->itemAt(2) == spacer1);
       
   156 
       
   157     window->show();
       
   158 }
       
   159 
       
   160 void tst_QBoxLayout::sizeHint()
       
   161 {
       
   162     QWidget *window = new QWidget;
       
   163     QHBoxLayout *lay1 = new QHBoxLayout;
       
   164     QHBoxLayout *lay2 = new QHBoxLayout;
       
   165     QLabel *label = new QLabel("widget twooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
       
   166     lay2->addWidget(label);
       
   167     lay1->addLayout(lay2);
       
   168     window->setLayout(lay1);
       
   169     window->show();
       
   170     label->setText("foooooooo baaaaaaar");
       
   171     QSize sh = lay1->sizeHint();
       
   172     QApplication::processEvents();
       
   173     // Note that this is not strictly required behaviour - actually
       
   174     // the preferred behaviour would be that sizeHint returns
       
   175     // the same value regardless of what's lying in the event queue.
       
   176     // (i.e. we would check for equality here instead)
       
   177     QVERIFY(lay1->sizeHint() != sh);
       
   178 }
       
   179 
       
   180 void tst_QBoxLayout::sizeConstraints()
       
   181 {
       
   182     QWidget *window = new QWidget;
       
   183     QHBoxLayout *lay = new QHBoxLayout;
       
   184     lay->addWidget(new QLabel("foooooooooooooooooooooooooooooooooooo"));
       
   185     lay->addWidget(new QLabel("baaaaaaaaaaaaaaaaaaaaaaaaaaaaaar"));
       
   186     lay->setSizeConstraint(QLayout::SetFixedSize);
       
   187     window->setLayout(lay);
       
   188     window->show();
       
   189     QApplication::processEvents();
       
   190     QSize sh = window->sizeHint();
       
   191     lay->takeAt(1);
       
   192     QVERIFY(sh.width() >= window->sizeHint().width() &&
       
   193             sh.height() >= window->sizeHint().height());
       
   194 
       
   195 }
       
   196 
       
   197 void tst_QBoxLayout::setGeometry()
       
   198 {
       
   199     QWidget w;
       
   200     QVBoxLayout *lay = new QVBoxLayout;
       
   201     lay->setMargin(0);
       
   202     lay->setSpacing(0);
       
   203     QHBoxLayout *lay2 = new QHBoxLayout;
       
   204     QDial *dial = new QDial;
       
   205     lay2->addWidget(dial);
       
   206     lay2->setAlignment(Qt::AlignTop);
       
   207     lay2->setAlignment(Qt::AlignRight);
       
   208     lay->addLayout(lay2);
       
   209     w.setLayout(lay);
       
   210     w.show();
       
   211 
       
   212     QRect newGeom(0, 0, 70, 70);
       
   213     lay2->setGeometry(newGeom);
       
   214     QApplication::processEvents();
       
   215     QVERIFY2(newGeom.contains(dial->geometry()), "dial->geometry() should be smaller and within newGeom");
       
   216 }
       
   217 
       
   218 void tst_QBoxLayout::setStyleShouldChangeSpacing()
       
   219 {
       
   220 
       
   221     QWidget *window = new QWidget;
       
   222     QHBoxLayout *hbox = new QHBoxLayout(window);
       
   223     QPushButton *pb1 = new QPushButton(tr("The spacing between this"));
       
   224     QPushButton *pb2 = new QPushButton(tr("and this button should depend on the style of the parent widget"));;
       
   225     pb1->setAttribute(Qt::WA_LayoutUsesWidgetRect);
       
   226     pb2->setAttribute(Qt::WA_LayoutUsesWidgetRect);
       
   227     hbox->addWidget(pb1);
       
   228     hbox->addWidget(pb2);
       
   229     CustomLayoutStyle *style1 = new CustomLayoutStyle;
       
   230     style1->hspacing = 6;
       
   231     window->setStyle(style1);
       
   232     window->show();
       
   233 
       
   234     QTest::qWait(100);
       
   235     int spacing = pb2->geometry().left() - pb1->geometry().right() - 1;
       
   236     QCOMPARE(spacing, 6);
       
   237 
       
   238     CustomLayoutStyle *style2 = new CustomLayoutStyle();
       
   239     style2->hspacing = 10;
       
   240     window->setStyle(style2);
       
   241     QTest::qWait(100);
       
   242     spacing = pb2->geometry().left() - pb1->geometry().right() - 1;
       
   243     QCOMPARE(spacing, 10);
       
   244 
       
   245     delete window;
       
   246     delete style1;
       
   247     delete style2;
       
   248 }
       
   249 
       
   250 
       
   251 QTEST_MAIN(tst_QBoxLayout)
       
   252 #include "tst_qboxlayout.moc"