diff options
author | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-05-08 20:12:04 +0200 |
---|---|---|
committer | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-05-08 20:12:04 +0200 |
commit | 45dd7bd930124009400f263abc6232900c667742 (patch) | |
tree | 4eb2a1f69c710a166412ded39ebe2934d1787273 /tests | |
parent | 799b4a3e102340996f283a6b9fd6c3efb0261a96 (diff) |
Expand the QtListModelAdaptor auto-tests with data-sets.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp | 216 |
1 files changed, 129 insertions, 87 deletions
diff --git a/tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp b/tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp index daf7bdc..5d491d0 100644 --- a/tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp +++ b/tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp @@ -21,58 +21,10 @@ ** ****************************************************************************/ #include <QtTest/QtTest> +#include <QtGui/qstandarditemmodel.h> #include <qlistmodeladaptor.h> -class MockOldListModel : public QAbstractListModel -{ -public: - int rowCount(const QModelIndex &parent=QModelIndex()) const - { - if (parent.isValid()) - return 0; - if (m_data.isEmpty()) - return 0; - QList<QVariant> data = m_data.value(m_data.keys().first()); - return data.count(); - } - - QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const - { - const_cast<MockOldListModel*>(this)->m_lastRequestedIndex = index; - if (!index.isValid() || index.parent().isValid() || index.model() != this) - return QVariant(); - if (index.column() != 0) - return QVariant(); - if (!m_data.contains(role)) - return QVariant(); - QList<QVariant> data = m_data.value(role); - if (index.row() < 0 || data.count() <= index.row()) - return QVariant(); - return QVariant(data.at(index.row())); - } - - void insertStuff(int role, QList<QVariant> data) { - if (! m_data.isEmpty()) { - QList<QVariant> existingData = m_data.value(m_data.keys().first()); - QCOMPARE(data.count(), existingData.count()); - } - QVERIFY(role >= 0 && role <= 13); // only allow valid roles - m_data.insert(role, data); - } - - void insertStrings(QStringList data) { - QList<QVariant> var; - foreach(const QString &string, data) - var << QVariant(string); - insertStuff(Qt::DisplayRole, var); - } - - QHash<int, QList<QVariant> > m_data; - - QModelIndex m_lastRequestedIndex; -}; - class tst_QtListModelAdaptor : public QObject { Q_OBJECT @@ -88,15 +40,23 @@ public slots: void cleanup(); private slots: - void checkGetData(); + void count_data(); + void count(); + void getData_data(); + void getData(); + void setData_data(); + void setData(); protected: - MockOldListModel *oldModel; - QtListModelAdaptor *model; + QStandardItemModel *source; + QtListModelAdaptor *adaptor; }; +Q_DECLARE_METATYPE(QList<int>) + tst_QtListModelAdaptor::tst_QtListModelAdaptor() { + qRegisterMetaType<QList<int> >(); } tst_QtListModelAdaptor::~tst_QtListModelAdaptor() @@ -105,16 +65,14 @@ tst_QtListModelAdaptor::~tst_QtListModelAdaptor() void tst_QtListModelAdaptor::initTestCase() { - oldModel = new MockOldListModel(); - model = new QtListModelAdaptor(oldModel); + source = new QStandardItemModel(); + adaptor = new QtListModelAdaptor(source); } void tst_QtListModelAdaptor::cleanupTestCase() { - delete oldModel; - oldModel = 0; - delete model; - model = 0; + delete source; + delete adaptor; } void tst_QtListModelAdaptor::init() @@ -123,38 +81,122 @@ void tst_QtListModelAdaptor::init() void tst_QtListModelAdaptor::cleanup() { + source->clear(); +} + +void tst_QtListModelAdaptor::count_data() +{ + QTest::addColumn<int>("itemCount"); + QTest::addColumn<int>("signalCount"); + + QTest::newRow("no items") << 0 << 0; + QTest::newRow("one items") << 1 << 1; + QTest::newRow("two items") << 2 << 1; + QTest::newRow("many items") << 10000 << 1; +} + +void tst_QtListModelAdaptor::count() +{ + QFETCH(int, itemCount); + QFETCH(int, signalCount); + + QSignalSpy itemsInserted(adaptor, SIGNAL(itemsInserted(int,int))); + source->setRowCount(itemCount); + QCOMPARE(adaptor->count(), itemCount); + QCOMPARE(itemsInserted.count(), signalCount); +} + +void tst_QtListModelAdaptor::getData_data() +{ + QTest::addColumn<QStringList>("items"); + QTest::addColumn<int>("role"); + QTest::addColumn<QStringList>("data"); + QTest::addColumn<int>("signalCount"); + + QTest::newRow("no items") + << QStringList() + << int(Qt::DisplayRole) + << QStringList() + << 0; + + QTest::newRow("three items") + << (QStringList() << "one" << "two" << "three") + << int(Qt::DisplayRole) + << (QStringList() << "four" << "five" << "six") + << 3; +} + +void tst_QtListModelAdaptor::getData() +{ + QFETCH(QStringList, items); + QFETCH(int, role); + QFETCH(QStringList, data); + QFETCH(int, signalCount); + + QSignalSpy itemsChanged(adaptor, SIGNAL(itemsChanged(int,int,const QList<int>&))); + + for (int i = 0; i < items.count(); ++i) + source->appendRow(new QStandardItem(items.at(i))); + + QCOMPARE(adaptor->count(), items.count()); + for (int j = 0; j < items.count(); ++j) + QCOMPARE(adaptor->data(j, QList<int>() << role).value(role).toString(), items.at(j)); + + for (int k = 0; k < data.count(); ++k) + source->setData(source->index(k, 0), data.at(k), role); + + for (int l = 0; l < data.count(); ++l) + QCOMPARE(adaptor->data(l, QList<int>() << role).value(role).toString(), data.at(l)); + + QCOMPARE(itemsChanged.count(), signalCount); +} + +void tst_QtListModelAdaptor::setData_data() +{ + QTest::addColumn<QStringList>("items"); + QTest::addColumn<int>("role"); + QTest::addColumn<QStringList>("data"); + QTest::addColumn<int>("signalCount"); + + QTest::newRow("no items") + << QStringList() + << int(Qt::DisplayRole) + << QStringList() + << 0; + + QTest::newRow("three items") + << (QStringList() << "one" << "two" << "three") + << int(Qt::DisplayRole) + << (QStringList() << "four" << "five" << "six") + << 3; } -void tst_QtListModelAdaptor::checkGetData() +void tst_QtListModelAdaptor::setData() { - QStringList data; - data << "One" << "two" << "Mad" << "Cow" << "Flies"; - oldModel->insertStrings(data); - QCOMPARE(model->model(), oldModel); - QVERIFY(!model->rootIndex().parent().isValid()); - QCOMPARE(model->column(), 0); - QCOMPARE(model->count(), 5); - - QHash<int, QVariant> answer; - QList<int> roles; - roles << Qt::DisplayRole; - answer = model->data(4, roles); - QCOMPARE(answer.count(), 1); - QVERIFY(answer.contains(Qt::DisplayRole)); - QCOMPARE(answer.value(Qt::DisplayRole).toString(), data[4]); - answer = model->data(5, roles); - QCOMPARE(answer.count(), 1); - QVERIFY(answer.contains(Qt::DisplayRole)); - QVERIFY(!answer.value(Qt::DisplayRole).isValid()); - - roles << Qt::DecorationRole; - - answer = model->data(2, roles); - QCOMPARE(answer.count(), 2); - QVERIFY(answer.contains(Qt::DisplayRole)); - QVERIFY(answer.contains(Qt::DecorationRole)); - QCOMPARE(answer.value(Qt::DisplayRole).toString(), data[2]); - QVERIFY(!answer.value(Qt::DecorationRole).isValid()); + QFETCH(QStringList, items); + QFETCH(int, role); + QFETCH(QStringList, data); + QFETCH(int, signalCount); + + QSignalSpy itemsChanged(adaptor, SIGNAL(itemsChanged(int,int,const QList<int>&))); + + for (int i = 0; i < items.count(); ++i) + source->appendRow(new QStandardItem(items.at(i))); + + QCOMPARE(adaptor->count(), items.count()); + for (int j = 0; j < items.count(); ++j) + QCOMPARE(adaptor->data(j, QList<int>() << role).value(role).toString(), items.at(j)); + + for (int k = 0; k < data.count(); ++k) { + QHash<int, QVariant> hash; + hash.insert(role, data.at(k)); + adaptor->setData(k, hash); + } + + for (int l = 0; l < data.count(); ++l) + QCOMPARE(source->data(source->index(l, 0), role).toString(), data.at(l)); + + QCOMPARE(itemsChanged.count(), signalCount); } QTEST_MAIN(tst_QtListModelAdaptor) |