tests/auto/qtoolbox/tst_qtoolbox.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 <qtoolbox.h>
       
    46 
       
    47 
       
    48 //TESTED_CLASS=
       
    49 //TESTED_FILES=
       
    50 
       
    51 QT_FORWARD_DECLARE_CLASS(QToolBox)
       
    52 
       
    53 class tst_QToolBoxPrivate;
       
    54 
       
    55 class tst_QToolBox : public QObject
       
    56 {
       
    57     Q_OBJECT
       
    58 
       
    59 public:
       
    60     tst_QToolBox();
       
    61     virtual ~tst_QToolBox();
       
    62 
       
    63 protected slots:
       
    64     void currentChanged(int);
       
    65 
       
    66 public slots:
       
    67     void initTestCase();
       
    68     void cleanupTestCase();
       
    69     void init();
       
    70     void cleanup();
       
    71 private slots:
       
    72     void getSetCheck();
       
    73     void populate();
       
    74     void change();
       
    75     void clear();
       
    76 
       
    77 private:
       
    78     QToolBox *testWidget;
       
    79     int currentIndex;
       
    80 
       
    81     tst_QToolBoxPrivate *d;
       
    82 };
       
    83 
       
    84 // Testing get/set functions
       
    85 void tst_QToolBox::getSetCheck()
       
    86 {
       
    87     QToolBox obj1;
       
    88     QWidget *w1 = new QWidget;
       
    89     QWidget *w2 = new QWidget;
       
    90     QWidget *w3 = new QWidget;
       
    91     QWidget *w4 = new QWidget;
       
    92     QWidget *w5 = new QWidget;
       
    93     obj1.addItem(w1, "Page1");
       
    94     obj1.addItem(w2, "Page2");
       
    95     obj1.addItem(w3, "Page3");
       
    96     obj1.addItem(w4, "Page4");
       
    97     obj1.addItem(w5, "Page5");
       
    98 
       
    99     // int QToolBox::currentIndex()
       
   100     // void QToolBox::setCurrentIndex(int)
       
   101     obj1.setCurrentIndex(3);
       
   102     QCOMPARE(3, obj1.currentIndex());
       
   103     obj1.setCurrentIndex(INT_MIN);
       
   104     QCOMPARE(3, obj1.currentIndex());
       
   105     obj1.setCurrentIndex(INT_MAX);
       
   106     QCOMPARE(3, obj1.currentIndex());
       
   107     obj1.setCurrentIndex(4);
       
   108     QCOMPARE(4, obj1.currentIndex());
       
   109 
       
   110     // QWidget * QToolBox::currentWidget()
       
   111     // void QToolBox::setCurrentWidget(QWidget *)
       
   112     obj1.setCurrentWidget(w1);
       
   113     QCOMPARE(w1, obj1.currentWidget());
       
   114     obj1.setCurrentWidget(w3);
       
   115     QCOMPARE(w3, obj1.currentWidget());
       
   116 
       
   117     obj1.setCurrentWidget((QWidget *)0);
       
   118     QCOMPARE(w3, obj1.currentWidget());
       
   119 }
       
   120 
       
   121 tst_QToolBox::tst_QToolBox()
       
   122 {
       
   123 }
       
   124 
       
   125 tst_QToolBox::~tst_QToolBox()
       
   126 {
       
   127 }
       
   128 
       
   129 class tst_QToolBoxPrivate
       
   130 {
       
   131 public:
       
   132     int count0, count1, count2, count3, count4;
       
   133     int idx1, idx2, idx3, idx32;
       
   134     int i0, i1, i2, i3, i4;
       
   135     int ci0, ci1, ci2, ci3, ci4;
       
   136     bool ci_correct;
       
   137 
       
   138     int manual_count;
       
   139 };
       
   140 
       
   141 void tst_QToolBox::init()
       
   142 {
       
   143     currentIndex = -1;
       
   144     testWidget = new QToolBox;
       
   145     connect(testWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));
       
   146 
       
   147     d = new tst_QToolBoxPrivate;
       
   148 
       
   149 
       
   150     d->count0 =  testWidget->count();
       
   151     d->ci0 =  currentIndex;
       
   152 
       
   153     QWidget *item1 = new QWidget( testWidget );
       
   154     testWidget->addItem( item1, "Item1" );
       
   155 
       
   156     d->count1 = testWidget->count();
       
   157     d->idx1 =  testWidget->indexOf(item1);
       
   158     d->ci1 = currentIndex;
       
   159     d->ci_correct = testWidget->widget(testWidget->currentIndex()) == item1;
       
   160 
       
   161     currentIndex = -1; // reset to make sure signal doesn't fire
       
   162 
       
   163     QWidget *item3 = new QWidget( testWidget );
       
   164     testWidget->addItem( item3, "Item3" );
       
   165 
       
   166     d->count2 = testWidget->count();
       
   167     d->idx3 =  testWidget->indexOf(item3);
       
   168     d->ci2 = currentIndex;
       
   169 
       
   170 
       
   171     QWidget *item2 = new QWidget( testWidget );
       
   172     testWidget->insertItem( 1, item2, "Item2");
       
   173 
       
   174     d->count3 = testWidget->count();
       
   175     d->idx2 =  testWidget->indexOf(item2);
       
   176     d->idx32 = testWidget->indexOf(item3);
       
   177     d->ci3 = currentIndex;
       
   178 
       
   179     QWidget *item0 = new QWidget( testWidget );
       
   180     testWidget->insertItem( 0, item0, "Item0");
       
   181 
       
   182     d->count4 =  testWidget->count();
       
   183     d->i0 =  testWidget->indexOf(item0);
       
   184     d->i1 = testWidget->indexOf(item1);
       
   185     d->i2 = testWidget->indexOf(item2);
       
   186     d->i3 =  testWidget->indexOf(item3);
       
   187     d->ci4 = currentIndex;
       
   188 
       
   189     d->manual_count = 4;
       
   190 }
       
   191 
       
   192 void tst_QToolBox::cleanup()
       
   193 {
       
   194     delete testWidget;
       
   195     delete d;
       
   196 }
       
   197 
       
   198 void tst_QToolBox::initTestCase()
       
   199 {
       
   200 }
       
   201 
       
   202 void tst_QToolBox::cleanupTestCase()
       
   203 {
       
   204 }
       
   205 
       
   206 void tst_QToolBox::currentChanged(int index)
       
   207 {
       
   208     currentIndex = index;
       
   209 }
       
   210 
       
   211 void tst_QToolBox::populate()
       
   212 {
       
   213     // verify preconditions
       
   214     QCOMPARE( d->count0, 0 );
       
   215     QCOMPARE( d->ci0, -1 );
       
   216     QVERIFY( d->ci_correct );
       
   217 
       
   218     QCOMPARE( d->count1, 1 );
       
   219     QCOMPARE( d->idx1, 0 );
       
   220     QCOMPARE( d->ci1, 0 );
       
   221 
       
   222     QCOMPARE( d->count2, 2 );
       
   223     QCOMPARE( d->idx3, 1 );
       
   224     QCOMPARE( d->ci2, -1 );
       
   225 
       
   226     QCOMPARE( d->count3, 3 );
       
   227     QCOMPARE( d->idx2, 1 );
       
   228     QCOMPARE( d->idx32, 2 );
       
   229     QCOMPARE( d->ci3, -1 );
       
   230 
       
   231 
       
   232     QCOMPARE( d->count4, 4 );
       
   233     QCOMPARE( d->i0, 0 );
       
   234     QCOMPARE( d->i1, 1 );
       
   235     QCOMPARE( d->i2, 2 );
       
   236     QCOMPARE( d->i3, 3 );
       
   237     QCOMPARE( d->ci4, 1 );
       
   238 
       
   239     QCOMPARE (testWidget->count(), d->manual_count);
       
   240     int oldcount = testWidget->count();
       
   241 
       
   242     QWidget *item = new QWidget( testWidget );
       
   243     testWidget->addItem( item, "Item");
       
   244     d->manual_count++;
       
   245 
       
   246     QCOMPARE( testWidget->count(), oldcount+1 );
       
   247     QCOMPARE( testWidget->indexOf(item), oldcount );
       
   248     QCOMPARE( testWidget->widget(oldcount), item );
       
   249 }
       
   250 
       
   251 void tst_QToolBox::change()
       
   252 {
       
   253     QWidget *lastItem = testWidget->widget(testWidget->count());
       
   254     QVERIFY( !lastItem );
       
   255     lastItem = testWidget->widget(testWidget->count() - 1);
       
   256     QVERIFY( lastItem );
       
   257 
       
   258     for ( int c = 0; c < testWidget->count(); ++c ) {
       
   259 	QString label = "Item " + QString::number(c);
       
   260 	testWidget->setItemText(c, label);
       
   261 	QCOMPARE( testWidget->itemText(c), label );
       
   262     }
       
   263 
       
   264     testWidget->setCurrentIndex( 0 );
       
   265     QCOMPARE( currentIndex, 0 );
       
   266 
       
   267     currentIndex = -1; // reset to make sure signal doesn't fire
       
   268     testWidget->setCurrentIndex( 0 );
       
   269     QCOMPARE( currentIndex, -1 );
       
   270     QCOMPARE( testWidget->currentIndex(), 0 );
       
   271 
       
   272     testWidget->setCurrentIndex( testWidget->count() );
       
   273     QCOMPARE( currentIndex, -1 );
       
   274     QCOMPARE( testWidget->currentIndex(), 0 );
       
   275 
       
   276     testWidget->setCurrentIndex( 1 );
       
   277     QCOMPARE( currentIndex, 1 );
       
   278     QCOMPARE( testWidget->currentIndex(), 1 );
       
   279 
       
   280     testWidget->setItemEnabled( testWidget->currentIndex(), FALSE );
       
   281     QCOMPARE( currentIndex, 2 );
       
   282     QCOMPARE( testWidget->currentIndex(), 2 );
       
   283 
       
   284     currentIndex = -1;
       
   285     testWidget->setItemEnabled( testWidget->indexOf(lastItem), FALSE );
       
   286     QCOMPARE( currentIndex, -1 );
       
   287     QCOMPARE( testWidget->currentIndex(), 2 );
       
   288 
       
   289     testWidget->setItemEnabled( testWidget->currentIndex(), FALSE );
       
   290     QCOMPARE( currentIndex, 0 );
       
   291 
       
   292     currentIndex = -1;
       
   293     testWidget->setItemEnabled( testWidget->currentIndex(), FALSE );
       
   294     QCOMPARE( currentIndex, -1 );
       
   295 
       
   296     testWidget->setItemEnabled( 1, TRUE );
       
   297 }
       
   298 
       
   299 void tst_QToolBox::clear()
       
   300 {
       
   301     // precondition: only item(1) is enabled
       
   302     QCOMPARE( testWidget->count(), 4 );
       
   303     testWidget->setCurrentIndex(0);
       
   304     currentIndex = -1;
       
   305 
       
   306     // delete current item(0)
       
   307     QPointer<QWidget> item = testWidget->widget(testWidget->currentIndex());
       
   308     testWidget->removeItem(testWidget->indexOf(item));
       
   309     QVERIFY(item);
       
   310     QCOMPARE( testWidget->count(), 3 );
       
   311     QCOMPARE( testWidget->indexOf(item), -1 );
       
   312     QCOMPARE( testWidget->currentIndex(), 0 );
       
   313     QCOMPARE(currentIndex, 0 );
       
   314 
       
   315     currentIndex = -1;
       
   316 
       
   317     item = testWidget->widget(1);
       
   318     testWidget->removeItem(testWidget->indexOf(item));
       
   319     QVERIFY( item );
       
   320     QCOMPARE( currentIndex, -1 );
       
   321     QCOMPARE( testWidget->currentIndex(), 0 );
       
   322     QCOMPARE( testWidget->count(), 2 );
       
   323     QCOMPARE( testWidget->indexOf(item), -1 );
       
   324 
       
   325     item = testWidget->widget(1);
       
   326     delete item;
       
   327     QCOMPARE( testWidget->count(), 1 );
       
   328     QCOMPARE( currentIndex, -1 );
       
   329     currentIndex = testWidget->currentIndex();
       
   330 
       
   331     item = testWidget->widget(0);
       
   332     testWidget->removeItem(testWidget->indexOf(item));
       
   333     QCOMPARE( testWidget->count(), 0 );
       
   334     QCOMPARE( currentIndex, -1 );
       
   335 }
       
   336 
       
   337 QTEST_MAIN(tst_QToolBox)
       
   338 #include "tst_qtoolbox.moc"