tests/auto/qstringlistmodel/tst_qstringlistmodel.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 <qabstractitemmodel.h>
       
    45 #include <qapplication.h>
       
    46 #include <qmap.h>
       
    47 #include <qstringlistmodel.h>
       
    48 #include <qstringlist.h>
       
    49 #include <qlistview.h>
       
    50 #include "qmodellistener.h"
       
    51 #include <qstringlistmodel.h>
       
    52 
       
    53 //TESTED_CLASS=
       
    54 //TESTED_FILES=
       
    55 
       
    56 void QModelListener::rowsAboutToBeRemovedOrInserted(const QModelIndex & parent, int start, int end )
       
    57 {
       
    58     int i;
       
    59     for (i = 0; start + i <= end; i++)
       
    60     {
       
    61         QModelIndex mIndex = m_pModel->index(start + i, 0, parent);
       
    62         QVariant var = m_pModel->data(mIndex, Qt::DisplayRole);
       
    63         QString str = var.toString();
       
    64         
       
    65         QCOMPARE(str, m_pAboutToStringlist->at(i));
       
    66     }
       
    67 }
       
    68 
       
    69 void QModelListener::rowsRemovedOrInserted(const QModelIndex & parent, int , int)
       
    70 {
       
    71     int i;
       
    72     // Can the rows that *are* removed be iterated now ?
       
    73 
       
    74     // What about rowsAboutToBeInserted - what will the indices be?
       
    75     // will insertRow() overwrite existing, or insert (and conseq. grow the model?)
       
    76     // What will the item then contain? empty data?
       
    77 
       
    78     // RemoveColumn. Does that also fire the rowsRemoved-family signals?
       
    79 
       
    80     for (i = 0; i < m_pExpectedStringlist->size(); i++)
       
    81     {
       
    82         QModelIndex mIndex = m_pModel->index(i, 0, parent);
       
    83         QVariant var = m_pModel->data(mIndex, Qt::DisplayRole);
       
    84         QString str = var.toString();
       
    85         
       
    86         //qDebug() << "index: " << i << " start: " << start << "end: " << end;
       
    87         QCOMPARE(str, m_pExpectedStringlist->at(i));
       
    88     }
       
    89 }
       
    90 
       
    91 
       
    92 class tst_QStringListModel : public QObject
       
    93 {
       
    94     Q_OBJECT
       
    95 
       
    96 public:
       
    97 
       
    98     tst_QStringListModel();
       
    99     virtual ~tst_QStringListModel();
       
   100 
       
   101 
       
   102 public slots:
       
   103     void initTestCase();
       
   104     void cleanupTestCase();
       
   105     void init();
       
   106     void cleanup();
       
   107 private slots:
       
   108 
       
   109     void rowsAboutToBeRemoved_rowsRemoved();
       
   110     void rowsAboutToBeRemoved_rowsRemoved_data();
       
   111 
       
   112     void rowsAboutToBeInserted_rowsInserted();
       
   113     void rowsAboutToBeInserted_rowsInserted_data();
       
   114 };
       
   115 
       
   116 
       
   117 tst_QStringListModel::tst_QStringListModel()
       
   118 
       
   119 {
       
   120 }
       
   121 
       
   122 tst_QStringListModel::~tst_QStringListModel()
       
   123 {
       
   124 }
       
   125 
       
   126 void tst_QStringListModel::initTestCase()
       
   127 {
       
   128 }
       
   129 
       
   130 void tst_QStringListModel::cleanupTestCase()
       
   131 {
       
   132 }
       
   133 
       
   134 void tst_QStringListModel::init()
       
   135 {
       
   136 }
       
   137 
       
   138 void tst_QStringListModel::cleanup()
       
   139 {
       
   140 }
       
   141 
       
   142 /*
       
   143   tests
       
   144 */
       
   145 
       
   146 
       
   147 void tst_QStringListModel::rowsAboutToBeRemoved_rowsRemoved_data()
       
   148 {
       
   149     QTest::addColumn<QStringList>("input");
       
   150     QTest::addColumn<int>("row");
       
   151     QTest::addColumn<int>("count");
       
   152     QTest::addColumn<QStringList>("aboutto");
       
   153     QTest::addColumn<QStringList>("res");
       
   154 
       
   155     QStringList strings0;   strings0    << "One" << "Two" << "Three" << "Four" << "Five";
       
   156     QStringList aboutto0;   aboutto0    << "Two" << "Three";
       
   157     QStringList res0;       res0        << "One" << "Four" << "Five";
       
   158     QTest::newRow( "data0" )   << strings0 << 1 << 2 << aboutto0 << res0;
       
   159 
       
   160     QStringList strings1;   strings1    << "One" << "Two" << "Three" << "Four" << "Five";
       
   161     QStringList aboutto1;   aboutto1    << "One" << "Two";
       
   162     QStringList res1;       res1        << "Three" << "Four" << "Five";
       
   163     QTest::newRow( "data1" )   << strings1 << 0 << 2 << aboutto1 << res1;
       
   164 
       
   165     QStringList strings2;   strings2    << "One" << "Two" << "Three" << "Four" << "Five";
       
   166     QStringList aboutto2;   aboutto2    << "Four" << "Five";
       
   167     QStringList res2;       res2        << "One" << "Two" << "Three";
       
   168     QTest::newRow( "data2" )   << strings2 << 3 << 2 << aboutto2 << res2;
       
   169 
       
   170     QStringList strings3;   strings3    << "One" << "Two" << "Three" << "Four" << "Five";
       
   171     QStringList aboutto3;   aboutto3    << "One" << "Two" << "Three" << "Four" << "Five";
       
   172     QStringList res3;       res3        ;
       
   173     QTest::newRow( "data3" )   << strings3 << 0 << 5 << aboutto3 << res3;
       
   174 
       
   175     /* Not sure if this is a valid test */
       
   176     QStringList strings4;   strings4    << "One" << "Two" << "Three" << "Four" << "Five";
       
   177     QStringList aboutto4;   aboutto4    << "Five" << "";
       
   178     QStringList res4;       res4        << "One" << "Two" << "Three" << "Four";
       
   179     QTest::newRow( "data4" )   << strings4 << 4 << 2 << aboutto4 << res4;
       
   180 
       
   181     /*
       
   182      * Keep this, template to add more data
       
   183     QStringList strings2;   strings2    << "One" << "Two" << "Three" << "Four" << "Five";
       
   184     QStringList aboutto2;   aboutto2    << "One" << "Two" << "Three" << "Four" << "Five";
       
   185     QStringList res2;       res2        << "One" << "Two" << "Three" << "Four" << "Five";
       
   186     QTest::newRow( "data2" )   << strings2 << 0 << 5 << aboutto2 << res2;
       
   187 */
       
   188 
       
   189 }
       
   190 
       
   191 void tst_QStringListModel::rowsAboutToBeRemoved_rowsRemoved()
       
   192 {
       
   193     QFETCH(QStringList, input);
       
   194     QFETCH(int, row);
       
   195     QFETCH(int, count);
       
   196     QFETCH(QStringList, aboutto);
       
   197     QFETCH(QStringList, res);
       
   198 
       
   199     QStringListModel *model = new QStringListModel(input);
       
   200     QModelListener *pListener = new QModelListener(&aboutto, &res, model);
       
   201     pListener->connect(model,       SIGNAL( rowsAboutToBeRemoved(const QModelIndex & , int , int )), 
       
   202                        pListener,   SLOT(   rowsAboutToBeRemovedOrInserted(const QModelIndex & , int , int ))    );
       
   203 
       
   204     pListener->connect(model,       SIGNAL( rowsRemoved(const QModelIndex & , int , int )), 
       
   205                        pListener,   SLOT(   rowsRemovedOrInserted(const QModelIndex & , int , int ))    );
       
   206 
       
   207     model->removeRows(row,count);
       
   208     // At this point, control goes to our connected slots inn this order: 
       
   209     // 1. rowsAboutToBeRemovedOrInserted
       
   210     // 2. rowsRemovedOrInserted
       
   211     // Control returns here
       
   212 
       
   213     delete pListener;
       
   214     delete model;
       
   215 
       
   216 }
       
   217 
       
   218 void tst_QStringListModel::rowsAboutToBeInserted_rowsInserted_data()
       
   219 {
       
   220     QTest::addColumn<QStringList>("input");
       
   221     QTest::addColumn<int>("row");
       
   222     QTest::addColumn<int>("count");
       
   223     QTest::addColumn<QStringList>("aboutto");
       
   224     QTest::addColumn<QStringList>("res");
       
   225 
       
   226     QStringList strings0;   strings0    << "One" << "Two" << "Three" << "Four" << "Five";
       
   227     QStringList aboutto0;   aboutto0    << "Two" << "Three";
       
   228     QStringList res0;       res0        << "One" << "" << "" << "Two" << "Three" << "Four" << "Five";
       
   229     QTest::newRow( "data0" )   << strings0 << 1 << 2 << aboutto0 << res0;
       
   230 
       
   231     QStringList strings1;   strings1    << "One" << "Two" << "Three" << "Four" << "Five";
       
   232     QStringList aboutto1;   aboutto1    << "One" << "Two";
       
   233     QStringList res1;       res1        << "" << "" << "One" << "Two" << "Three" << "Four" << "Five";
       
   234     QTest::newRow( "data1" )   << strings1 << 0 << 2 << aboutto1 << res1;
       
   235 
       
   236     QStringList strings2;   strings2    << "One" << "Two" << "Three" << "Four" << "Five";
       
   237     QStringList aboutto2;   aboutto2    << "Four" << "Five";
       
   238     QStringList res2;       res2        << "One" << "Two" << "Three" << "" << "" << "Four" << "Five";
       
   239     QTest::newRow( "data2" )   << strings2 << 3 << 2 << aboutto2 << res2;
       
   240 
       
   241     QStringList strings3;   strings3    << "One" << "Two" << "Three" << "Four" << "Five";
       
   242     QStringList aboutto3;   aboutto3    << "One" << "Two" << "Three" << "Four" << "Five";
       
   243     QStringList res3;       res3        << "" << "" << "" << "" << "" << "One" << "Two" << "Three" << "Four" << "Five";
       
   244     QTest::newRow( "data3" )   << strings3 << 0 << 5 << aboutto3 << res3;
       
   245 
       
   246     /*
       
   247      * Keep this, template to add more data
       
   248     QStringList strings2;   strings2    << "One" << "Two" << "Three" << "Four" << "Five";
       
   249     QStringList aboutto2;   aboutto2    << "One" << "Two" << "Three" << "Four" << "Five";
       
   250     QStringList res2;       res2        << "One" << "Two" << "Three" << "Four" << "Five";
       
   251     QTest::newRow( "data2" )   << strings2 << 0 << 5 << aboutto2 << res2;
       
   252 */
       
   253 
       
   254 }
       
   255 
       
   256 void tst_QStringListModel::rowsAboutToBeInserted_rowsInserted()
       
   257 {
       
   258     QFETCH(QStringList, input);
       
   259     QFETCH(int, row);
       
   260     QFETCH(int, count);
       
   261     QFETCH(QStringList, aboutto);
       
   262     QFETCH(QStringList, res);
       
   263 
       
   264     QStringListModel *model = new QStringListModel(input);
       
   265     QModelListener *pListener = new QModelListener(&aboutto, &res, model);
       
   266     connect(model,       SIGNAL( rowsAboutToBeInserted(const QModelIndex & , int , int )), 
       
   267                        pListener,   SLOT(   rowsAboutToBeRemovedOrInserted(const QModelIndex & , int , int ))    );
       
   268 
       
   269     connect(model,       SIGNAL( rowsInserted(const QModelIndex & , int , int )), 
       
   270                        pListener,   SLOT(   rowsRemovedOrInserted(const QModelIndex & , int , int ))    );
       
   271 
       
   272     model->insertRows(row,count);
       
   273     // At this point, control goes to our connected slots inn this order: 
       
   274     // 1. rowsAboutToBeRemovedOrInserted
       
   275     // 2. rowsRemovedOrInserted
       
   276     // Control returns here
       
   277 
       
   278     delete pListener;
       
   279     delete model;
       
   280 
       
   281 }
       
   282 
       
   283 
       
   284 QTEST_MAIN(tst_QStringListModel)
       
   285 #include "tst_qstringlistmodel.moc"
       
   286