tests/auto/qstackedlayout/tst_qstackedlayout.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 #include <QLineEdit>
       
    45 #include <QStackedLayout>
       
    46 #include <qapplication.h>
       
    47 #include <qwidget.h>
       
    48 #include <QPushButton>
       
    49 
       
    50 #include "../../shared/util.h"
       
    51 
       
    52 //TESTED_CLASS=
       
    53 //TESTED_FILES=gui/kernel/qlayout.cpp gui/kernel/qlayout.h
       
    54 
       
    55 class tst_QStackedLayout : public QObject
       
    56 {
       
    57     Q_OBJECT
       
    58 
       
    59 public:
       
    60     tst_QStackedLayout();
       
    61     virtual ~tst_QStackedLayout();
       
    62 
       
    63 
       
    64 public slots:
       
    65     void initTestCase();
       
    66     void cleanupTestCase();
       
    67     void init();
       
    68     void cleanup();
       
    69 
       
    70 private slots:
       
    71     void getSetCheck();
       
    72     void testCase();
       
    73     void deleteCurrent();
       
    74     void removeWidget();
       
    75     void keepFocusAfterSetCurrent();
       
    76 
       
    77 private:
       
    78     QWidget *testWidget;
       
    79 };
       
    80 
       
    81 // Testing get/set functions
       
    82 void tst_QStackedLayout::getSetCheck()
       
    83 {
       
    84     QStackedLayout obj1;
       
    85     // int QStackedLayout::currentIndex()
       
    86     // void QStackedLayout::setCurrentIndex(int)
       
    87     obj1.setCurrentIndex(0);
       
    88     QCOMPARE(-1, obj1.currentIndex());
       
    89     obj1.setCurrentIndex(INT_MIN);
       
    90     QCOMPARE(-1, obj1.currentIndex());
       
    91     obj1.setCurrentIndex(INT_MAX);
       
    92     QCOMPARE(-1, obj1.currentIndex());
       
    93 
       
    94     // QWidget * QStackedLayout::currentWidget()
       
    95     // void QStackedLayout::setCurrentWidget(QWidget *)
       
    96     QWidget *var2 = new QWidget();
       
    97     obj1.addWidget(var2);
       
    98     obj1.setCurrentWidget(var2);
       
    99     QCOMPARE(var2, obj1.currentWidget());
       
   100 
       
   101     obj1.setCurrentWidget((QWidget *)0);
       
   102     QCOMPARE(obj1.currentWidget(), var2);
       
   103 
       
   104     delete var2;
       
   105 }
       
   106 
       
   107 
       
   108 tst_QStackedLayout::tst_QStackedLayout()
       
   109     : testWidget(0)
       
   110 {
       
   111 }
       
   112 
       
   113 tst_QStackedLayout::~tst_QStackedLayout()
       
   114 {
       
   115 }
       
   116 
       
   117 void tst_QStackedLayout::initTestCase()
       
   118 {
       
   119 }
       
   120 
       
   121 void tst_QStackedLayout::cleanupTestCase()
       
   122 {
       
   123 }
       
   124 
       
   125 void tst_QStackedLayout::init()
       
   126 {
       
   127     if (testWidget) {
       
   128         delete testWidget;
       
   129         testWidget = 0;
       
   130     }
       
   131     testWidget = new QWidget(0);
       
   132     testWidget->resize( 200, 200 );
       
   133     testWidget->show();
       
   134 
       
   135     // make sure the tests work with focus follows mouse
       
   136     QCursor::setPos(testWidget->geometry().center());
       
   137     testWidget->activateWindow();
       
   138     QApplication::syncX();
       
   139     QTest::qWait(250);
       
   140 }
       
   141 
       
   142 void tst_QStackedLayout::cleanup()
       
   143 {
       
   144     delete testWidget;
       
   145     testWidget = 0;
       
   146 }
       
   147 
       
   148 
       
   149 void tst_QStackedLayout::testCase()
       
   150 {
       
   151     QStackedLayout onStack(testWidget);
       
   152     QStackedLayout *testLayout = &onStack;
       
   153     testWidget->setLayout(testLayout);
       
   154 
       
   155     QSignalSpy spy(testLayout,SIGNAL(currentChanged(int)));
       
   156 
       
   157     // Nothing in layout
       
   158     QCOMPARE(testLayout->currentIndex(), -1);
       
   159     QCOMPARE(testLayout->currentWidget(), static_cast<QWidget*>(0));
       
   160     QCOMPARE(testLayout->count(), 0);
       
   161 
       
   162     // One widget added to layout
       
   163     QWidget *w1 = new QWidget(testWidget);
       
   164     testLayout->addWidget(w1);
       
   165     QCOMPARE(spy.count(), 1);
       
   166     QCOMPARE(spy.at(0).at(0).toInt(), 0);
       
   167     spy.clear();
       
   168     QCOMPARE(testLayout->currentIndex(), 0);
       
   169     QCOMPARE(testLayout->currentWidget(), w1);
       
   170     QCOMPARE(testLayout->count(), 1);
       
   171 
       
   172     // Another widget added to layout
       
   173     QWidget *w2 = new QWidget(testWidget);
       
   174     testLayout->addWidget(w2);
       
   175     QCOMPARE(testLayout->currentIndex(), 0);
       
   176     QCOMPARE(testLayout->currentWidget(), w1);
       
   177     QCOMPARE(testLayout->indexOf(w2), 1);
       
   178     QCOMPARE(testLayout->count(), 2);
       
   179 
       
   180     // Change the current index
       
   181     testLayout->setCurrentIndex(1);
       
   182     QCOMPARE(spy.count(), 1);
       
   183     QCOMPARE(spy.at(0).at(0).toInt(), 1);
       
   184     spy.clear();
       
   185     QCOMPARE(testLayout->currentIndex(), 1);
       
   186     QCOMPARE(testLayout->currentWidget(), w2);
       
   187 
       
   188     // First widget removed from layout
       
   189     testLayout->removeWidget(w1);
       
   190     QCOMPARE(testLayout->currentIndex(), 0);
       
   191     QCOMPARE(testLayout->currentWidget(), w2);
       
   192     QCOMPARE(testLayout->count(), 1);
       
   193 
       
   194     // Second widget removed from layout; back to nothing
       
   195     testLayout->removeWidget(w2);
       
   196     QCOMPARE(spy.count(), 1);
       
   197     QCOMPARE(spy.at(0).at(0).toInt(), -1);
       
   198     spy.clear();
       
   199     QCOMPARE(testLayout->currentIndex(), -1);
       
   200     QCOMPARE(testLayout->currentWidget(), static_cast<QWidget*>(0));
       
   201     QCOMPARE(testLayout->count(), 0);
       
   202 
       
   203     // Another widget inserted at current index.
       
   204     // Current index should become current index + 1, but the
       
   205     // current widget should stay the same
       
   206     testLayout->addWidget(w1);
       
   207     QCOMPARE(testLayout->currentIndex(), 0);
       
   208     QCOMPARE(testLayout->currentWidget(), w1);
       
   209     testLayout->insertWidget(0, w2);
       
   210     QCOMPARE(testLayout->currentIndex(), 1);
       
   211     QCOMPARE(testLayout->currentWidget(), w1);
       
   212     QVERIFY(w1->isVisible());
       
   213     QVERIFY(!w2->isVisible());
       
   214 
       
   215     testLayout->setCurrentWidget(w2);
       
   216     // Another widget added, so we have: w2, w1, w3 with w2 current
       
   217     QWidget *w3 = new QWidget(testWidget);
       
   218     testLayout->addWidget(w3);
       
   219     QCOMPARE(testLayout->indexOf(w2), 0);
       
   220     QCOMPARE(testLayout->indexOf(w1), 1);
       
   221     QCOMPARE(testLayout->indexOf(w3), 2);
       
   222 
       
   223     // Set index to 1 and remove that widget (w1).
       
   224     // Then, current index should still be 1, but w3
       
   225     // should be the new current widget.
       
   226     testLayout->setCurrentIndex(1);
       
   227     testLayout->removeWidget(w1);
       
   228     QCOMPARE(testLayout->currentIndex(), 1);
       
   229     QCOMPARE(testLayout->currentWidget(), w3);
       
   230     QVERIFY(w3->isVisible());
       
   231 
       
   232     // Remove the current widget (w3).
       
   233     // Since it's the last one in the list, current index should now
       
   234     // become 0 and w2 becomes the current widget.
       
   235     testLayout->removeWidget(w3);
       
   236     QCOMPARE(testLayout->currentIndex(), 0);
       
   237     QCOMPARE(testLayout->currentWidget(), w2);
       
   238     QVERIFY(w2->isVisible());
       
   239 
       
   240     // Make sure index is decremented when we remove a widget at index < current index
       
   241     testLayout->addWidget(w1);
       
   242     testLayout->addWidget(w3);
       
   243     testLayout->setCurrentIndex(2);
       
   244     testLayout->removeWidget(w2); // At index 0
       
   245     QCOMPARE(testLayout->currentIndex(), 1);
       
   246     QCOMPARE(testLayout->currentWidget(), w3);
       
   247     QVERIFY(w3->isVisible());
       
   248     testLayout->removeWidget(w1); // At index 0
       
   249     QCOMPARE(testLayout->currentIndex(), 0);
       
   250     QCOMPARE(testLayout->currentWidget(), w3);
       
   251     QVERIFY(w3->isVisible());
       
   252     testLayout->removeWidget(w3);
       
   253     QCOMPARE(testLayout->currentIndex(), -1);
       
   254     QCOMPARE(testLayout->currentWidget(), static_cast<QWidget*>(0));
       
   255 }
       
   256 
       
   257 void tst_QStackedLayout::deleteCurrent()
       
   258 {
       
   259     QStackedLayout *testLayout = new QStackedLayout(testWidget);
       
   260 
       
   261     QWidget *w1 = new QWidget;
       
   262     testLayout->addWidget(w1);
       
   263     QWidget *w2 = new QWidget;
       
   264     testLayout->addWidget(w2);
       
   265     QCOMPARE(testLayout->currentWidget(), w1);
       
   266     delete testLayout->currentWidget();
       
   267     QCOMPARE(testLayout->currentWidget(), w2);
       
   268 }
       
   269 
       
   270 void tst_QStackedLayout::removeWidget()
       
   271 {
       
   272     if (testWidget->layout()) delete testWidget->layout();
       
   273     QVBoxLayout *vbox = new QVBoxLayout(testWidget);
       
   274 
       
   275     QPushButton *top = new QPushButton("top", testWidget);  //add another widget that can receive focus
       
   276     top->setObjectName("top");
       
   277     vbox->addWidget(top);
       
   278 
       
   279     QStackedLayout *testLayout = new QStackedLayout();
       
   280     QPushButton *w1 = new QPushButton("1st", testWidget);
       
   281     w1->setObjectName("1st");
       
   282     testLayout->addWidget(w1);
       
   283     QPushButton *w2 = new QPushButton("2nd", testWidget);
       
   284     w2->setObjectName("2nd");
       
   285     testLayout->addWidget(w2);
       
   286     vbox->addLayout(testLayout);
       
   287     top->setFocus();
       
   288     QTest::qWait(100);
       
   289     top->activateWindow();
       
   290     QTest::qWait(100);
       
   291     int i =0;
       
   292     for (;;) {
       
   293         if (QApplication::focusWidget() == top)
       
   294             break;
       
   295         else if (i >= 5)
       
   296             QSKIP("Can't get focus", SkipSingle);
       
   297         QTest::qWait(100);
       
   298         ++i;
       
   299     }
       
   300     QCOMPARE(QApplication::focusWidget(), static_cast<QWidget *>(top));
       
   301 
       
   302     // focus should stay at the 'top' widget
       
   303     testLayout->removeWidget(w1);
       
   304 
       
   305     QCOMPARE(QApplication::focusWidget(), static_cast<QWidget *>(top));
       
   306 }
       
   307 
       
   308 class LineEdit : public QLineEdit
       
   309 {
       
   310 public:
       
   311     LineEdit() : hasFakeEditFocus(false)
       
   312     { }
       
   313 
       
   314     bool hasFakeEditFocus;
       
   315 
       
   316 protected:
       
   317     bool isSingleFocusWidget() const
       
   318     {
       
   319         const QWidget *w = this;
       
   320         while ((w = w->nextInFocusChain()) != this) {
       
   321             if (w->isVisible() && static_cast<const QWidget*>(w->focusProxy()) != this
       
   322                 && w->focusPolicy() & Qt::TabFocus) {
       
   323                 return false;
       
   324             }
       
   325         }
       
   326         return true;
       
   327     }
       
   328 
       
   329     void focusInEvent(QFocusEvent *event)
       
   330     {
       
   331         QLineEdit::focusInEvent(event);
       
   332         hasFakeEditFocus = isSingleFocusWidget();
       
   333     }
       
   334 
       
   335     void focusOutEvent(QFocusEvent *event)
       
   336     {
       
   337         hasFakeEditFocus = false;
       
   338         QLineEdit::focusOutEvent(event);
       
   339     }
       
   340 };
       
   341 
       
   342 void tst_QStackedLayout::keepFocusAfterSetCurrent()
       
   343 {
       
   344     if (testWidget->layout()) delete testWidget->layout();
       
   345     QStackedLayout *stackLayout = new QStackedLayout(testWidget);
       
   346     testWidget->setFocusPolicy(Qt::NoFocus);
       
   347 
       
   348     LineEdit *edit1 = new LineEdit;
       
   349     LineEdit *edit2 = new LineEdit;
       
   350     stackLayout->addWidget(edit1);
       
   351     stackLayout->addWidget(edit2);
       
   352 
       
   353     stackLayout->setCurrentIndex(0);
       
   354 
       
   355     testWidget->show();
       
   356     QApplication::setActiveWindow(testWidget);
       
   357     QTest::qWaitForWindowShown(testWidget);
       
   358     QApplication::processEvents();
       
   359 
       
   360     edit1->setFocus();
       
   361     edit1->activateWindow();
       
   362     QTest::qWait(25);
       
   363 
       
   364     QTRY_VERIFY(edit1->hasFocus());
       
   365 
       
   366     stackLayout->setCurrentIndex(1);
       
   367     QVERIFY(!edit1->hasFocus());
       
   368     QVERIFY(edit2->hasFocus());
       
   369     QVERIFY(edit2->hasFakeEditFocus);
       
   370 }
       
   371 
       
   372 QTEST_MAIN(tst_QStackedLayout)
       
   373 #include "tst_qstackedlayout.moc"
       
   374