2010-08-15 06:31:48 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-11-21 17:06:30 -05:00
|
|
|
#include "TestGroupModel.h"
|
|
|
|
|
2010-08-15 09:03:47 -04:00
|
|
|
#include <QtTest/QSignalSpy>
|
2010-08-15 06:31:48 -04:00
|
|
|
#include <QtTest/QTest>
|
|
|
|
|
|
|
|
#include "modeltest.h"
|
2012-01-01 16:04:27 -05:00
|
|
|
#include "tests.h"
|
2010-08-15 09:03:47 -04:00
|
|
|
#include "core/Database.h"
|
2010-08-15 06:31:48 -04:00
|
|
|
#include "core/Group.h"
|
2011-12-27 09:47:06 -05:00
|
|
|
#include "crypto/Crypto.h"
|
2012-05-16 04:16:32 -04:00
|
|
|
#include "gui/group/GroupModel.h"
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2010-09-18 11:15:22 -04:00
|
|
|
void TestGroupModel::initTestCase()
|
|
|
|
{
|
|
|
|
qRegisterMetaType<QModelIndex>("QModelIndex");
|
2011-12-27 09:47:06 -05:00
|
|
|
Crypto::init();
|
2010-09-18 11:15:22 -04:00
|
|
|
}
|
|
|
|
|
2010-08-15 06:31:48 -04:00
|
|
|
void TestGroupModel::test()
|
|
|
|
{
|
2010-08-15 09:03:47 -04:00
|
|
|
Database* db = new Database();
|
|
|
|
|
2011-07-09 15:54:01 -04:00
|
|
|
Group* groupRoot = db->rootGroup();
|
|
|
|
groupRoot->setObjectName("groupRoot");
|
2010-08-15 06:31:48 -04:00
|
|
|
groupRoot->setName("groupRoot");
|
|
|
|
|
|
|
|
Group* group1 = new Group();
|
2011-07-09 15:54:01 -04:00
|
|
|
group1->setObjectName("group1");
|
2010-08-15 06:31:48 -04:00
|
|
|
group1->setName("group1");
|
|
|
|
group1->setParent(groupRoot);
|
|
|
|
|
|
|
|
Group* group11 = new Group();
|
2011-07-09 15:54:01 -04:00
|
|
|
group1->setObjectName("group11");
|
2010-08-15 06:31:48 -04:00
|
|
|
group11->setName("group11");
|
|
|
|
group11->setParent(group1);
|
|
|
|
|
|
|
|
Group* group12 = new Group();
|
2011-07-09 15:54:01 -04:00
|
|
|
group1->setObjectName("group12");
|
2010-08-15 06:31:48 -04:00
|
|
|
group12->setName("group12");
|
|
|
|
group12->setParent(group1);
|
|
|
|
|
|
|
|
Group* group121 = new Group();
|
2011-07-09 15:54:01 -04:00
|
|
|
group1->setObjectName("group121");
|
2010-08-15 06:31:48 -04:00
|
|
|
group121->setName("group121");
|
|
|
|
group121->setParent(group12);
|
|
|
|
|
2010-08-15 09:03:47 -04:00
|
|
|
GroupModel* model = new GroupModel(db, this);
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2011-06-29 12:40:26 -04:00
|
|
|
ModelTest* modelTest = new ModelTest(model, this);
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
QModelIndex indexRoot = model->index(0, 0);
|
|
|
|
QModelIndex index1 = model->index(0, 0, indexRoot);
|
|
|
|
QModelIndex index11 = model->index(0, 0, index1);
|
2012-04-23 10:57:08 -04:00
|
|
|
QPersistentModelIndex index12 = model->index(1, 0, index1);
|
2010-08-15 06:31:48 -04:00
|
|
|
QModelIndex index121 = model->index(0, 0, index12);
|
|
|
|
|
2010-08-18 05:05:46 -04:00
|
|
|
QCOMPARE(model->data(indexRoot).toString(), QString("groupRoot"));
|
|
|
|
QCOMPARE(model->data(index1).toString(), QString("group1"));
|
|
|
|
QCOMPARE(model->data(index11).toString(), QString("group11"));
|
|
|
|
QCOMPARE(model->data(index12).toString(), QString("group12"));
|
|
|
|
QCOMPARE(model->data(index121).toString(), QString("group121"));
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2012-04-23 10:57:08 -04:00
|
|
|
QSignalSpy spy1(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)));
|
2010-08-15 09:03:47 -04:00
|
|
|
group11->setName("test");
|
|
|
|
group121->setIcon(4);
|
2010-08-18 04:27:40 -04:00
|
|
|
QCOMPARE(spy1.count(), 2);
|
2012-04-23 10:57:08 -04:00
|
|
|
QCOMPARE(model->data(index11).toString(), QString("test"));
|
2010-08-18 04:27:40 -04:00
|
|
|
|
2012-04-23 10:57:08 -04:00
|
|
|
QSignalSpy spyAboutToAdd(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)));
|
|
|
|
QSignalSpy spyAdded(model, SIGNAL(rowsInserted(QModelIndex,int,int)));
|
|
|
|
QSignalSpy spyAboutToRemove(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)));
|
|
|
|
QSignalSpy spyRemoved(model, SIGNAL(rowsRemoved(QModelIndex,int,int)));
|
|
|
|
QSignalSpy spyAboutToMove(model, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
|
|
|
|
QSignalSpy spyMoved(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)));
|
|
|
|
|
|
|
|
Group* group2 = new Group();
|
2012-04-24 18:22:55 -04:00
|
|
|
group2->setObjectName("group2");
|
2012-04-23 10:57:08 -04:00
|
|
|
group2->setName("group2");
|
|
|
|
group2->setParent(groupRoot);
|
|
|
|
QModelIndex index2 = model->index(1, 0, indexRoot);
|
|
|
|
QCOMPARE(spyAboutToAdd.count(), 1);
|
|
|
|
QCOMPARE(spyAdded.count(), 1);
|
|
|
|
QCOMPARE(spyAboutToRemove.count(), 0);
|
|
|
|
QCOMPARE(spyRemoved.count(), 0);
|
|
|
|
QCOMPARE(spyAboutToMove.count(), 0);
|
|
|
|
QCOMPARE(spyMoved.count(), 0);
|
|
|
|
QCOMPARE(model->data(index2).toString(), QString("group2"));
|
2010-08-18 04:27:40 -04:00
|
|
|
|
|
|
|
group12->setParent(group1, 0);
|
2012-04-23 10:57:08 -04:00
|
|
|
QCOMPARE(spyAboutToAdd.count(), 1);
|
|
|
|
QCOMPARE(spyAdded.count(), 1);
|
|
|
|
QCOMPARE(spyAboutToRemove.count(), 0);
|
|
|
|
QCOMPARE(spyRemoved.count(), 0);
|
|
|
|
QCOMPARE(spyAboutToMove.count(), 1);
|
|
|
|
QCOMPARE(spyMoved.count(), 1);
|
|
|
|
QCOMPARE(model->data(index12).toString(), QString("group12"));
|
|
|
|
|
|
|
|
group12->setParent(group1, 1);
|
|
|
|
QCOMPARE(spyAboutToAdd.count(), 1);
|
|
|
|
QCOMPARE(spyAdded.count(), 1);
|
|
|
|
QCOMPARE(spyAboutToRemove.count(), 0);
|
|
|
|
QCOMPARE(spyRemoved.count(), 0);
|
|
|
|
QCOMPARE(spyAboutToMove.count(), 2);
|
|
|
|
QCOMPARE(spyMoved.count(), 2);
|
|
|
|
QCOMPARE(model->data(index12).toString(), QString("group12"));
|
|
|
|
|
|
|
|
group12->setParent(group2);
|
|
|
|
QCOMPARE(spyAboutToAdd.count(), 1);
|
|
|
|
QCOMPARE(spyAdded.count(), 1);
|
|
|
|
QCOMPARE(spyAboutToRemove.count(), 0);
|
|
|
|
QCOMPARE(spyRemoved.count(), 0);
|
|
|
|
QCOMPARE(spyAboutToMove.count(), 3);
|
|
|
|
QCOMPARE(spyMoved.count(), 3);
|
|
|
|
QVERIFY(index12.isValid());
|
|
|
|
QCOMPARE(model->data(index12).toString(), QString("group12"));
|
|
|
|
QCOMPARE(model->data(index12.child(0, 0)).toString(), QString("group121"));
|
2010-08-18 04:27:40 -04:00
|
|
|
|
2012-04-23 10:57:08 -04:00
|
|
|
delete group12;
|
2010-08-18 04:27:40 -04:00
|
|
|
QCOMPARE(spyAboutToAdd.count(), 1);
|
|
|
|
QCOMPARE(spyAdded.count(), 1);
|
2013-03-22 15:57:18 -04:00
|
|
|
QCOMPARE(spyAboutToRemove.count(), 2);
|
|
|
|
QCOMPARE(spyRemoved.count(), 2);
|
2012-04-23 10:57:08 -04:00
|
|
|
QCOMPARE(spyAboutToMove.count(), 3);
|
|
|
|
QCOMPARE(spyMoved.count(), 3);
|
|
|
|
QVERIFY(!index12.isValid());
|
2010-08-15 09:03:47 -04:00
|
|
|
|
2011-07-09 15:54:01 -04:00
|
|
|
// test removing a group that has children
|
|
|
|
delete group1;
|
|
|
|
|
2010-08-15 09:03:47 -04:00
|
|
|
delete db;
|
2011-06-29 12:40:26 -04:00
|
|
|
|
|
|
|
delete modelTest;
|
|
|
|
delete model;
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
2012-07-23 18:07:28 -04:00
|
|
|
QTEST_GUILESS_MAIN(TestGroupModel)
|