tests/auto/modeltest/tst_modeltest.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    42 
    42 
    43 #include <QtTest/QtTest>
    43 #include <QtTest/QtTest>
    44 #include <QtGui/QtGui>
    44 #include <QtGui/QtGui>
    45 
    45 
    46 #include "modeltest.h"
    46 #include "modeltest.h"
       
    47 #include "dynamictreemodel.h"
    47 
    48 
    48 
    49 
    49 class tst_ModelTest : public QObject
    50 class tst_ModelTest : public QObject
    50 {
    51 {
    51     Q_OBJECT
    52     Q_OBJECT
    62 
    63 
    63 private slots:
    64 private slots:
    64     void stringListModel();
    65     void stringListModel();
    65     void treeWidgetModel();
    66     void treeWidgetModel();
    66     void standardItemModel();
    67     void standardItemModel();
       
    68     void testInsertThroughProxy();
       
    69     void moveSourceItems();
    67 };
    70 };
    68 
    71 
    69 
    72 
    70 
    73 
    71 void tst_ModelTest::initTestCase()
    74 void tst_ModelTest::initTestCase()
    90 
    93 
    91 void tst_ModelTest::stringListModel()
    94 void tst_ModelTest::stringListModel()
    92 {
    95 {
    93     QStringListModel model;
    96     QStringListModel model;
    94     QSortFilterProxyModel proxy;
    97     QSortFilterProxyModel proxy;
    95     
    98 
    96     ModelTest t1(&model);
    99     ModelTest t1(&model);
    97     ModelTest t2(&proxy);
   100     ModelTest t2(&proxy);
    98     
   101 
    99     proxy.setSourceModel(&model);
   102     proxy.setSourceModel(&model);
   100   
   103 
   101     model.setStringList(QStringList() << "2" << "3" << "1");
   104     model.setStringList(QStringList() << "2" << "3" << "1");
   102     model.setStringList(QStringList() << "a" << "e" << "plop" << "b" << "c" );
   105     model.setStringList(QStringList() << "a" << "e" << "plop" << "b" << "c" );
   103     
   106 
   104     proxy.setDynamicSortFilter(true);
   107     proxy.setDynamicSortFilter(true);
   105     proxy.setFilterRegExp(QRegExp("[^b]"));
   108     proxy.setFilterRegExp(QRegExp("[^b]"));
   106 }
   109 }
   107 
   110 
   108 void tst_ModelTest::treeWidgetModel()
   111 void tst_ModelTest::treeWidgetModel()
   109 {
   112 {
   110     QTreeWidget widget;
   113     QTreeWidget widget;
   111 
   114 
   112     ModelTest t1(widget.model());
   115     ModelTest t1(widget.model());
   113     
   116 
   114     QTreeWidgetItem *root = new QTreeWidgetItem(&widget, QStringList("root"));
   117     QTreeWidgetItem *root = new QTreeWidgetItem(&widget, QStringList("root"));
   115     for (int i = 0; i < 20; ++i) {
   118     for (int i = 0; i < 20; ++i) {
   116         new QTreeWidgetItem(root, QStringList(QString::number(i)));
   119         new QTreeWidgetItem(root, QStringList(QString::number(i)));
   117     }
   120     }
   118     QTreeWidgetItem *remove = root->child(2);
   121     QTreeWidgetItem *remove = root->child(2);
   119     root->removeChild(remove);
   122     root->removeChild(remove);
   120     QTreeWidgetItem *parent = new QTreeWidgetItem(&widget, QStringList("parent"));
   123     QTreeWidgetItem *parent = new QTreeWidgetItem(&widget, QStringList("parent"));
   121     new QTreeWidgetItem(parent, QStringList("child"));
   124     new QTreeWidgetItem(parent, QStringList("child"));
   122     widget.setItemHidden(parent, true);
   125     widget.setItemHidden(parent, true);
   123     
   126 
   124     widget.sortByColumn(0);
   127     widget.sortByColumn(0);
   125 }
   128 }
   126 
   129 
   127 void tst_ModelTest::standardItemModel()
   130 void tst_ModelTest::standardItemModel()
   128 {
   131 {
   129     QStandardItemModel model(10,10);
   132     QStandardItemModel model(10,10);
   130     QSortFilterProxyModel proxy;
   133     QSortFilterProxyModel proxy;
   131     
   134 
   132     
   135 
   133     ModelTest t1(&model);
   136     ModelTest t1(&model);
   134     ModelTest t2(&proxy);
   137     ModelTest t2(&proxy);
   135     
   138 
   136     proxy.setSourceModel(&model);
   139     proxy.setSourceModel(&model);
   137     
   140 
   138     model.insertRows(2, 5);
   141     model.insertRows(2, 5);
   139     model.removeRows(4, 5);
   142     model.removeRows(4, 5);
   140 
   143 
   141     model.insertColumns(2, 5);
   144     model.insertColumns(2, 5);
   142     model.removeColumns(4, 5);
   145     model.removeColumns(4, 5);
   143     
   146 
   144     model.insertRows(0,5, model.index(1,1));
   147     model.insertRows(0,5, model.index(1,1));
   145     model.insertColumns(0,5, model.index(1,3));
   148     model.insertColumns(0,5, model.index(1,3));
   146     
   149 }
   147 }
   150 
       
   151 void tst_ModelTest::testInsertThroughProxy()
       
   152 {
       
   153     DynamicTreeModel *model = new DynamicTreeModel(this);
       
   154 
       
   155     QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this);
       
   156     proxy->setSourceModel(model);
       
   157 
       
   158     new ModelTest(proxy, this);
       
   159 
       
   160     ModelInsertCommand *insertCommand = new ModelInsertCommand(model, this);
       
   161     insertCommand->setNumCols(4);
       
   162     insertCommand->setStartRow(0);
       
   163     insertCommand->setEndRow(9);
       
   164     // Parent is QModelIndex()
       
   165     insertCommand->doCommand();
       
   166 
       
   167     insertCommand = new ModelInsertCommand(model, this);
       
   168     insertCommand->setNumCols(4);
       
   169     insertCommand->setAncestorRowNumbers(QList<int>() << 5);
       
   170     insertCommand->setStartRow(0);
       
   171     insertCommand->setEndRow(9);
       
   172     insertCommand->doCommand();
       
   173 
       
   174     ModelMoveCommand *moveCommand = new ModelMoveCommand(model, this);
       
   175     moveCommand->setNumCols(4);
       
   176     moveCommand->setStartRow(0);
       
   177     moveCommand->setEndRow(0);
       
   178     moveCommand->setDestRow(9);
       
   179     moveCommand->setDestAncestors(QList<int>() << 5);
       
   180     moveCommand->doCommand();
       
   181 }
       
   182 
       
   183 /**
       
   184   Makes the persistent index list publicly accessible
       
   185 */
       
   186 class AccessibleProxyModel : public QSortFilterProxyModel
       
   187 {
       
   188     Q_OBJECT
       
   189 public:
       
   190     AccessibleProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
       
   191 
       
   192     QModelIndexList persistent()
       
   193     {
       
   194         return persistentIndexList();
       
   195     }
       
   196 };
       
   197 
       
   198 class ObservingObject : public QObject
       
   199 {
       
   200     Q_OBJECT
       
   201 public:
       
   202     ObservingObject(AccessibleProxyModel  *proxy, QObject *parent = 0)
       
   203     : QObject(parent),
       
   204     m_proxy(proxy)
       
   205     {
       
   206         connect(m_proxy, SIGNAL(layoutAboutToBeChanged()), SLOT(storePersistent()));
       
   207         connect(m_proxy, SIGNAL(layoutChanged()), SLOT(checkPersistent()));
       
   208     }
       
   209 
       
   210 public slots:
       
   211 
       
   212     void storePersistent(const QModelIndex &parent)
       
   213     {
       
   214         for (int row = 0; row < m_proxy->rowCount(parent); ++row) {
       
   215             QModelIndex proxyIndex = m_proxy->index(row, 0, parent);
       
   216             QModelIndex sourceIndex = m_proxy->mapToSource(proxyIndex);
       
   217             Q_ASSERT(proxyIndex.isValid());
       
   218             Q_ASSERT(sourceIndex.isValid());
       
   219             m_persistentSourceIndexes.append(sourceIndex);
       
   220             m_persistentProxyIndexes.append(proxyIndex);
       
   221             if (m_proxy->hasChildren(proxyIndex))
       
   222                 storePersistent(proxyIndex);
       
   223         }
       
   224     }
       
   225 
       
   226     void storePersistent()
       
   227     {
       
   228         m_persistentSourceIndexes.clear();
       
   229         m_persistentProxyIndexes.clear();
       
   230         Q_ASSERT(m_proxy->persistent().isEmpty());
       
   231         storePersistent(QModelIndex());
       
   232         Q_ASSERT(!m_proxy->persistent().isEmpty());
       
   233     }
       
   234 
       
   235     void checkPersistent()
       
   236     {
       
   237         for (int row = 0; row < m_persistentProxyIndexes.size(); ++row) {
       
   238             QModelIndex updatedProxy = m_persistentProxyIndexes.at(row);
       
   239             QModelIndex updatedSource = m_persistentSourceIndexes.at(row);
       
   240         }
       
   241         for (int row = 0; row < m_persistentProxyIndexes.size(); ++row) {
       
   242             QModelIndex updatedProxy = m_persistentProxyIndexes.at(row);
       
   243             QModelIndex updatedSource = m_persistentSourceIndexes.at(row);
       
   244             QCOMPARE(m_proxy->mapToSource(updatedProxy), updatedSource);
       
   245         }
       
   246     }
       
   247 
       
   248 private:
       
   249     AccessibleProxyModel  *m_proxy;
       
   250     QList<QPersistentModelIndex> m_persistentSourceIndexes;
       
   251     QList<QPersistentModelIndex> m_persistentProxyIndexes;
       
   252 };
       
   253 
       
   254 void tst_ModelTest::moveSourceItems()
       
   255 {
       
   256     DynamicTreeModel *model = new DynamicTreeModel(this);
       
   257     AccessibleProxyModel *proxy = new AccessibleProxyModel(this);
       
   258     proxy->setSourceModel(model);
       
   259 
       
   260     ModelInsertCommand *insertCommand = new ModelInsertCommand(model, this);
       
   261     insertCommand->setStartRow(0);
       
   262     insertCommand->setEndRow(2);
       
   263     insertCommand->doCommand();
       
   264 
       
   265     insertCommand = new ModelInsertCommand(model, this);
       
   266     insertCommand->setAncestorRowNumbers(QList<int>() << 1);
       
   267     insertCommand->setStartRow(0);
       
   268     insertCommand->setEndRow(2);
       
   269     insertCommand->doCommand();
       
   270 
       
   271     ObservingObject observer(proxy);
       
   272 
       
   273     ModelMoveCommand *moveCommand = new ModelMoveCommand(model, this);
       
   274     moveCommand->setStartRow(0);
       
   275     moveCommand->setEndRow(0);
       
   276     moveCommand->setDestAncestors(QList<int>() << 1);
       
   277     moveCommand->setDestRow(0);
       
   278     moveCommand->doCommand();
       
   279 }
       
   280 
   148 
   281 
   149 QTEST_MAIN(tst_ModelTest)
   282 QTEST_MAIN(tst_ModelTest)
   150 #include "tst_modeltest.moc"
   283 #include "tst_modeltest.moc"