tests/auto/qdatawidgetmapper/tst_qdatawidgetmapper.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 #include <QtGui/QtGui>
       
    42 #include <QtTest/QtTest>
       
    43 
       
    44 class tst_QDataWidgetMapper: public QObject
       
    45 {
       
    46     Q_OBJECT
       
    47 private slots:
       
    48     void setModel();
       
    49     void navigate();
       
    50     void addMapping();
       
    51     void currentIndexChanged();
       
    52     void changingValues();
       
    53     void setData();
       
    54     void mappedWidgetAt();
       
    55 
       
    56     void comboBox();
       
    57 };
       
    58 
       
    59 static QStandardItemModel *testModel(QObject *parent = 0)
       
    60 {
       
    61     QStandardItemModel *model = new QStandardItemModel(10, 10, parent);
       
    62 
       
    63     for (int row = 0; row < 10; ++row) {
       
    64         for (int col = 0; col < 10; ++col)
       
    65             model->setData(model->index(row, col), QString("item %1 %2").arg(row).arg(col));
       
    66     }
       
    67 
       
    68     return model;
       
    69 }
       
    70 
       
    71 void tst_QDataWidgetMapper::setModel()
       
    72 {
       
    73     QDataWidgetMapper mapper;
       
    74 
       
    75     QCOMPARE(mapper.model(), (QAbstractItemModel *)0);
       
    76 
       
    77     { // let the model go out of scope firstma
       
    78         QStandardItemModel model;
       
    79         mapper.setModel(&model);
       
    80         QCOMPARE(mapper.model(), static_cast<QAbstractItemModel *>(&model));
       
    81     }
       
    82 
       
    83     QCOMPARE(mapper.model(), (QAbstractItemModel *)0);
       
    84 
       
    85     { // let the mapper go out of scope first
       
    86         QStandardItemModel model2;
       
    87         QDataWidgetMapper mapper2;
       
    88         mapper2.setModel(&model2);
       
    89     }
       
    90 }
       
    91 
       
    92 void tst_QDataWidgetMapper::navigate()
       
    93 {
       
    94     QDataWidgetMapper mapper;
       
    95     QAbstractItemModel *model = testModel(&mapper);
       
    96     mapper.setModel(model);
       
    97 
       
    98     QLineEdit edit1;
       
    99     QLineEdit edit2;
       
   100     QLineEdit edit3;
       
   101 
       
   102     mapper.addMapping(&edit1, 0);
       
   103     mapper.toFirst();
       
   104     mapper.addMapping(&edit2, 1);
       
   105     mapper.addMapping(&edit3, 2);
       
   106 
       
   107     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   108     QVERIFY(edit2.text().isEmpty());
       
   109     QVERIFY(edit3.text().isEmpty());
       
   110     QVERIFY(mapper.submit());
       
   111     edit2.setText(QString("item 0 1"));
       
   112     edit3.setText(QString("item 0 2"));
       
   113     QVERIFY(mapper.submit());
       
   114 
       
   115     mapper.toFirst(); //this will repopulate
       
   116     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   117     QCOMPARE(edit2.text(), QString("item 0 1"));
       
   118     QCOMPARE(edit3.text(), QString("item 0 2"));
       
   119 
       
   120 
       
   121     mapper.toFirst();
       
   122     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   123     QCOMPARE(edit2.text(), QString("item 0 1"));
       
   124     QCOMPARE(edit3.text(), QString("item 0 2"));
       
   125 
       
   126     mapper.toPrevious(); // should do nothing
       
   127     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   128     QCOMPARE(edit2.text(), QString("item 0 1"));
       
   129     QCOMPARE(edit3.text(), QString("item 0 2"));
       
   130 
       
   131     mapper.toNext();
       
   132     QCOMPARE(edit1.text(), QString("item 1 0"));
       
   133     QCOMPARE(edit2.text(), QString("item 1 1"));
       
   134     QCOMPARE(edit3.text(), QString("item 1 2"));
       
   135 
       
   136     mapper.toLast();
       
   137     QCOMPARE(edit1.text(), QString("item 9 0"));
       
   138     QCOMPARE(edit2.text(), QString("item 9 1"));
       
   139     QCOMPARE(edit3.text(), QString("item 9 2"));
       
   140 
       
   141     mapper.toNext(); // should do nothing
       
   142     QCOMPARE(edit1.text(), QString("item 9 0"));
       
   143     QCOMPARE(edit2.text(), QString("item 9 1"));
       
   144     QCOMPARE(edit3.text(), QString("item 9 2"));
       
   145 
       
   146     mapper.setCurrentIndex(4);
       
   147     QCOMPARE(edit1.text(), QString("item 4 0"));
       
   148     QCOMPARE(edit2.text(), QString("item 4 1"));
       
   149     QCOMPARE(edit3.text(), QString("item 4 2"));
       
   150 
       
   151     mapper.setCurrentIndex(-1); // should do nothing
       
   152     QCOMPARE(edit1.text(), QString("item 4 0"));
       
   153     QCOMPARE(edit2.text(), QString("item 4 1"));
       
   154     QCOMPARE(edit3.text(), QString("item 4 2"));
       
   155 
       
   156     mapper.setCurrentIndex(10); // should do nothing
       
   157     QCOMPARE(edit1.text(), QString("item 4 0"));
       
   158     QCOMPARE(edit2.text(), QString("item 4 1"));
       
   159     QCOMPARE(edit3.text(), QString("item 4 2"));
       
   160 
       
   161     mapper.setCurrentModelIndex(QModelIndex()); // should do nothing
       
   162     QCOMPARE(edit1.text(), QString("item 4 0"));
       
   163     QCOMPARE(edit2.text(), QString("item 4 1"));
       
   164     QCOMPARE(edit3.text(), QString("item 4 2"));
       
   165 
       
   166     mapper.setCurrentModelIndex(model->index(6, 0));
       
   167     QCOMPARE(edit1.text(), QString("item 6 0"));
       
   168     QCOMPARE(edit2.text(), QString("item 6 1"));
       
   169     QCOMPARE(edit3.text(), QString("item 6 2"));
       
   170 
       
   171     /* now try vertical navigation */
       
   172 
       
   173     mapper.setOrientation(Qt::Vertical);
       
   174 
       
   175     mapper.addMapping(&edit1, 0);
       
   176     mapper.addMapping(&edit2, 1);
       
   177     mapper.addMapping(&edit3, 2);
       
   178 
       
   179     mapper.toFirst();
       
   180     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   181     QCOMPARE(edit2.text(), QString("item 1 0"));
       
   182     QCOMPARE(edit3.text(), QString("item 2 0"));
       
   183 
       
   184     mapper.toPrevious(); // should do nothing
       
   185     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   186     QCOMPARE(edit2.text(), QString("item 1 0"));
       
   187     QCOMPARE(edit3.text(), QString("item 2 0"));
       
   188 
       
   189     mapper.toNext();
       
   190     QCOMPARE(edit1.text(), QString("item 0 1"));
       
   191     QCOMPARE(edit2.text(), QString("item 1 1"));
       
   192     QCOMPARE(edit3.text(), QString("item 2 1"));
       
   193 
       
   194     mapper.toLast();
       
   195     QCOMPARE(edit1.text(), QString("item 0 9"));
       
   196     QCOMPARE(edit2.text(), QString("item 1 9"));
       
   197     QCOMPARE(edit3.text(), QString("item 2 9"));
       
   198 
       
   199     mapper.toNext(); // should do nothing
       
   200     QCOMPARE(edit1.text(), QString("item 0 9"));
       
   201     QCOMPARE(edit2.text(), QString("item 1 9"));
       
   202     QCOMPARE(edit3.text(), QString("item 2 9"));
       
   203 
       
   204     mapper.setCurrentIndex(4);
       
   205     QCOMPARE(edit1.text(), QString("item 0 4"));
       
   206     QCOMPARE(edit2.text(), QString("item 1 4"));
       
   207     QCOMPARE(edit3.text(), QString("item 2 4"));
       
   208 
       
   209     mapper.setCurrentIndex(-1); // should do nothing
       
   210     QCOMPARE(edit1.text(), QString("item 0 4"));
       
   211     QCOMPARE(edit2.text(), QString("item 1 4"));
       
   212     QCOMPARE(edit3.text(), QString("item 2 4"));
       
   213 
       
   214     mapper.setCurrentIndex(10); // should do nothing
       
   215     QCOMPARE(edit1.text(), QString("item 0 4"));
       
   216     QCOMPARE(edit2.text(), QString("item 1 4"));
       
   217     QCOMPARE(edit3.text(), QString("item 2 4"));
       
   218 
       
   219     mapper.setCurrentModelIndex(QModelIndex()); // should do nothing
       
   220     QCOMPARE(edit1.text(), QString("item 0 4"));
       
   221     QCOMPARE(edit2.text(), QString("item 1 4"));
       
   222     QCOMPARE(edit3.text(), QString("item 2 4"));
       
   223 
       
   224     mapper.setCurrentModelIndex(model->index(0, 6));
       
   225     QCOMPARE(edit1.text(), QString("item 0 6"));
       
   226     QCOMPARE(edit2.text(), QString("item 1 6"));
       
   227     QCOMPARE(edit3.text(), QString("item 2 6"));
       
   228 }
       
   229 
       
   230 void tst_QDataWidgetMapper::addMapping()
       
   231 {
       
   232     QDataWidgetMapper mapper;
       
   233     QAbstractItemModel *model = testModel(&mapper);
       
   234     mapper.setModel(model);
       
   235 
       
   236     QLineEdit edit1;
       
   237     mapper.addMapping(&edit1, 0);
       
   238     mapper.toFirst();
       
   239     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   240 
       
   241     mapper.addMapping(&edit1, 1);
       
   242     mapper.toFirst();
       
   243     QCOMPARE(edit1.text(), QString("item 0 1"));
       
   244 
       
   245     QCOMPARE(mapper.mappedSection(&edit1), 1);
       
   246 
       
   247     edit1.clear();
       
   248     mapper.removeMapping(&edit1);
       
   249     mapper.toFirst();
       
   250     QCOMPARE(edit1.text(), QString());
       
   251 
       
   252     {
       
   253         QLineEdit edit2;
       
   254         mapper.addMapping(&edit2, 2);
       
   255         mapper.toFirst();
       
   256         QCOMPARE(edit2.text(), QString("item 0 2"));
       
   257     } // let the edit go out of scope
       
   258 
       
   259     QCOMPARE(mapper.mappedWidgetAt(2), (QWidget *)0);
       
   260     mapper.toLast();
       
   261 }
       
   262 
       
   263 void tst_QDataWidgetMapper::currentIndexChanged()
       
   264 {
       
   265     QDataWidgetMapper mapper;
       
   266     QAbstractItemModel *model = testModel(&mapper);
       
   267     mapper.setModel(model);
       
   268 
       
   269     QSignalSpy spy(&mapper, SIGNAL(currentIndexChanged(int)));
       
   270 
       
   271     mapper.toFirst();
       
   272     QCOMPARE(spy.count(), 1);
       
   273     QCOMPARE(spy.takeFirst().at(0).toInt(), 0);
       
   274 
       
   275     mapper.toNext();
       
   276     QCOMPARE(spy.count(), 1);
       
   277     QCOMPARE(spy.takeFirst().at(0).toInt(), 1);
       
   278 
       
   279     mapper.setCurrentIndex(7);
       
   280     QCOMPARE(spy.count(), 1);
       
   281     QCOMPARE(spy.takeFirst().at(0).toInt(), 7);
       
   282 
       
   283     mapper.setCurrentIndex(-1);
       
   284     QCOMPARE(spy.count(), 0);
       
   285 
       
   286     mapper.setCurrentIndex(42);
       
   287     QCOMPARE(spy.count(), 0);
       
   288 }
       
   289 
       
   290 void tst_QDataWidgetMapper::changingValues()
       
   291 {
       
   292     QDataWidgetMapper mapper;
       
   293     QAbstractItemModel *model = testModel(&mapper);
       
   294     mapper.setModel(model);
       
   295 
       
   296     QLineEdit edit1;
       
   297     mapper.addMapping(&edit1, 0);
       
   298     mapper.toFirst();
       
   299     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   300 
       
   301     QLineEdit edit2;
       
   302     mapper.addMapping(&edit2, 0, "text");
       
   303     mapper.toFirst();
       
   304     QCOMPARE(edit2.text(), QString("item 0 0"));
       
   305 
       
   306     model->setData(model->index(0, 0), QString("changed"));
       
   307     QCOMPARE(edit1.text(), QString("changed"));
       
   308     QCOMPARE(edit2.text(), QString("changed"));
       
   309 }
       
   310 
       
   311 void tst_QDataWidgetMapper::setData()
       
   312 {
       
   313     QDataWidgetMapper mapper;
       
   314     QAbstractItemModel *model = testModel(&mapper);
       
   315     mapper.setModel(model);
       
   316 
       
   317     QLineEdit edit1;
       
   318     QLineEdit edit2;
       
   319     QLineEdit edit3;
       
   320 
       
   321     mapper.addMapping(&edit1, 0);
       
   322     mapper.addMapping(&edit2, 1);
       
   323     mapper.addMapping(&edit3, 0, "text");
       
   324     mapper.toFirst();
       
   325     QCOMPARE(edit1.text(), QString("item 0 0"));
       
   326     QCOMPARE(edit2.text(), QString("item 0 1"));
       
   327     QCOMPARE(edit3.text(), QString("item 0 0"));
       
   328 
       
   329     edit1.setText("new text");
       
   330 
       
   331     mapper.submit();
       
   332     QCOMPARE(model->data(model->index(0, 0)).toString(), QString("new text"));
       
   333 
       
   334     edit3.setText("more text");
       
   335 
       
   336     mapper.submit();
       
   337     QCOMPARE(model->data(model->index(0, 0)).toString(), QString("more text"));
       
   338 }
       
   339 
       
   340 void tst_QDataWidgetMapper::comboBox()
       
   341 {
       
   342     QDataWidgetMapper mapper;
       
   343     QAbstractItemModel *model = testModel(&mapper);
       
   344     mapper.setModel(model);
       
   345     mapper.setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
       
   346 
       
   347     QComboBox readOnlyBox;
       
   348     readOnlyBox.setEditable(false);
       
   349     readOnlyBox.addItem("read only item 0");
       
   350     readOnlyBox.addItem("read only item 1");
       
   351     readOnlyBox.addItem("read only item 2");
       
   352 
       
   353     QComboBox readWriteBox;
       
   354     readWriteBox.setEditable(true);
       
   355     readWriteBox.addItem("read write item 0");
       
   356     readWriteBox.addItem("read write item 1");
       
   357     readWriteBox.addItem("read write item 2");
       
   358 
       
   359     // populat the combo boxes with data
       
   360     mapper.addMapping(&readOnlyBox, 0, "currentIndex");
       
   361     mapper.addMapping(&readWriteBox, 1, "currentText");
       
   362     mapper.toFirst();
       
   363 
       
   364     QCOMPARE(readOnlyBox.currentText(), QString("read only item 0"));
       
   365     QCOMPARE(readWriteBox.currentText(), QString("read write item 0"));
       
   366 
       
   367     // set some new values on the boxes
       
   368     readOnlyBox.setCurrentIndex(1);
       
   369     readWriteBox.setEditText("read write item y");
       
   370 
       
   371     mapper.submit();
       
   372 
       
   373     // make sure the new values are in the model
       
   374     QCOMPARE(model->data(model->index(0, 0)).toInt(), 1);
       
   375     QCOMPARE(model->data(model->index(0, 1)).toString(), QString("read write item y"));
       
   376 
       
   377     // now test updating of the widgets
       
   378     model->setData(model->index(0, 0), 2, Qt::EditRole);
       
   379     model->setData(model->index(0, 1), QString("read write item z"), Qt::EditRole);
       
   380 
       
   381     QCOMPARE(readOnlyBox.currentIndex(), 2);
       
   382     QEXPECT_FAIL("", "See tasks 125493 and 147153", Abort);
       
   383     QCOMPARE(readWriteBox.currentText(), QString("read write item z"));
       
   384 }
       
   385 
       
   386 void tst_QDataWidgetMapper::mappedWidgetAt()
       
   387 {
       
   388     QDataWidgetMapper mapper;
       
   389     QAbstractItemModel *model = testModel(&mapper);
       
   390     mapper.setModel(model);
       
   391 
       
   392     QLineEdit lineEdit1;
       
   393     QLineEdit lineEdit2;
       
   394 
       
   395     QCOMPARE(mapper.mappedWidgetAt(432312), (QWidget*)0);
       
   396 
       
   397     mapper.addMapping(&lineEdit1, 1);
       
   398     mapper.addMapping(&lineEdit2, 2);
       
   399 
       
   400     QCOMPARE(mapper.mappedWidgetAt(1), static_cast<QWidget *>(&lineEdit1));
       
   401     QCOMPARE(mapper.mappedWidgetAt(2), static_cast<QWidget *>(&lineEdit2));
       
   402 
       
   403     mapper.addMapping(&lineEdit2, 4242);
       
   404 
       
   405     QCOMPARE(mapper.mappedWidgetAt(2), (QWidget*)0);
       
   406     QCOMPARE(mapper.mappedWidgetAt(4242), static_cast<QWidget *>(&lineEdit2));
       
   407 }
       
   408 
       
   409 QTEST_MAIN(tst_QDataWidgetMapper)
       
   410 #include "tst_qdatawidgetmapper.moc"