File indexing completed on 2024-11-24 04:53:36

0001 /* Copyright (C) 2012 - 2013 Peter Amidon <peter@picnicpark.org>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 
0024 #include <QtTest>
0025 #include "test_SenderIdentitiesModel.h"
0026 #include "Utils/ModelEvents.h"
0027 #include "Common/MetaTypes.h"
0028 
0029 #define COMPARE_ROWNUM(identity, rowNumber) \
0030   QVERIFY(model->index(rowNumber, SenderIdentitiesModel::COLUMN_NAME).isValid()); \
0031   QCOMPARE(model->data(model->index(rowNumber, SenderIdentitiesModel::COLUMN_NAME)).toString(), identity.realName); \
0032   QCOMPARE(model->data(model->index(rowNumber, SenderIdentitiesModel::COLUMN_SIGNATURE)).toString(), identity.signature);
0033 
0034 #define COMPARE_INDEX(identity, indexName, indexSignature) \
0035   QVERIFY(indexName.isValid()); \
0036   QVERIFY(indexSignature.isValid()); \
0037   QCOMPARE(model->data(indexName).toString(), identity.realName); \
0038   QCOMPARE(model->data(indexSignature).toString(), identity.signature);
0039 
0040 using namespace Composer;
0041 
0042 SenderIdentitiesModelTest::SenderIdentitiesModelTest() :
0043     identity1(QStringLiteral("test name #1"), QStringLiteral("test1@mail.org"), QStringLiteral("testing"), QStringLiteral("tester 1")),
0044     identity2(QStringLiteral("test name #2"), QStringLiteral("test2@mail.org"), QStringLiteral("testing"), QStringLiteral("tester 2")),
0045     identity3(QStringLiteral("test name #3"), QStringLiteral("test3@mail.org"), QStringLiteral("testing"), QStringLiteral("tester 3"))
0046 {
0047 }
0048 
0049 void SenderIdentitiesModelTest::initTestCase()
0050 {
0051     Common::registerMetaTypes();
0052 }
0053 
0054 void SenderIdentitiesModelTest::init()
0055 {
0056     model = new SenderIdentitiesModel();
0057 }
0058 
0059 void SenderIdentitiesModelTest::testPropertyStorage()
0060 {
0061     QLatin1String realName("Testing Tester");
0062     QLatin1String emailAddress("test@testing.org");
0063     QLatin1String organization("Testing Inc.");
0064     QLatin1String signature("A tester working for Testing Inc.");
0065     ItemSenderIdentity testIdentity(realName, emailAddress, organization, signature);
0066     QCOMPARE(testIdentity.realName, realName);
0067     QCOMPARE(testIdentity.emailAddress, emailAddress);
0068     QCOMPARE(testIdentity.organisation, organization);
0069     QCOMPARE(testIdentity.signature, signature);
0070 }
0071 
0072 void SenderIdentitiesModelTest::testAddIdentity()
0073 {
0074     QSignalSpy spy(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
0075     QSignalSpy spy2(model, SIGNAL(rowsInserted(QModelIndex, int, int)));
0076     model->appendIdentity(identity1);
0077     QCOMPARE(model->rowCount(), 1);
0078     QCOMPARE(spy.count(), 1);
0079     QCOMPARE(ModelInsertRemoveEvent(spy.at(0)), ModelInsertRemoveEvent(QModelIndex(), 0, 0));
0080     QCOMPARE(spy2.count(), 1);
0081     QCOMPARE(ModelInsertRemoveEvent(spy2.at(0)), ModelInsertRemoveEvent(QModelIndex(), 0, 0));
0082     COMPARE_ROWNUM(identity1, 0);
0083 }
0084 
0085 void SenderIdentitiesModelTest::testRemoveIdentity1()
0086 {
0087     model->appendIdentity(identity1);
0088     model->appendIdentity(identity2);
0089     QCOMPARE(model->rowCount(), 2);
0090     QSignalSpy spy(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
0091     QSignalSpy spy2(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
0092     model->removeIdentityAt(1);
0093     QCOMPARE(model->rowCount(), 1);
0094     QCOMPARE(spy.count(), 1);
0095     QCOMPARE(ModelInsertRemoveEvent(spy.at(0)), ModelInsertRemoveEvent(QModelIndex(), 1, 1));
0096     QCOMPARE(spy2.count(), 1);
0097     QCOMPARE(ModelInsertRemoveEvent(spy2.at(0)), ModelInsertRemoveEvent(QModelIndex(), 1, 1)) ;
0098     QCOMPARE(model->rowCount(), 1);
0099     COMPARE_ROWNUM(identity1, 0);
0100 }
0101 
0102 void SenderIdentitiesModelTest::testRemoveIdentity2()
0103 {
0104     model->appendIdentity(identity1);
0105     model->appendIdentity(identity2);
0106     QCOMPARE(model->rowCount(), 2);
0107     QSignalSpy spy(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
0108     QSignalSpy spy2(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
0109     model->removeIdentityAt(0);
0110     QCOMPARE(model->rowCount(), 1);
0111     QCOMPARE(spy.count(), 1);
0112     QCOMPARE(ModelInsertRemoveEvent(spy.at(0)), ModelInsertRemoveEvent(QModelIndex(), 0, 0));
0113     QCOMPARE(spy2.count(), 1);
0114     QCOMPARE(ModelInsertRemoveEvent(spy2.at(0)), ModelInsertRemoveEvent(QModelIndex(), 0, 0));
0115     QCOMPARE(model->rowCount(), 1);
0116     COMPARE_ROWNUM(identity2, 0);
0117 }
0118 
0119 void SenderIdentitiesModelTest::testMoveFirstIdentity()
0120 {
0121     model->appendIdentity(identity1);
0122     model->appendIdentity(identity2);
0123     model->appendIdentity(identity3);
0124     QCOMPARE(model->rowCount(), 3);
0125     QPersistentModelIndex nameIndex =
0126         model->index(0, SenderIdentitiesModel::COLUMN_NAME);
0127     QPersistentModelIndex signatureIndex =
0128         model->index(0, SenderIdentitiesModel::COLUMN_SIGNATURE);
0129     QSignalSpy spy(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
0130     QSignalSpy spy2(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
0131     model->moveIdentity(0, 2);
0132     QCOMPARE(model->rowCount(), 3);
0133     QCOMPARE(spy.count(), 1);
0134     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 0, 0, QModelIndex(), 3));
0135     QCOMPARE(spy2.count(), 1);
0136     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 0, 0, QModelIndex(), 3));
0137     COMPARE_ROWNUM(identity2, 0);
0138     COMPARE_ROWNUM(identity1, 2);
0139     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
0140     COMPARE_ROWNUM(identity3, 1);
0141     model->appendIdentity(identity3);
0142     QCOMPARE(model->rowCount(), 4);
0143     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
0144     model->removeIdentityAt(0);
0145     QCOMPARE(model->rowCount(), 3);
0146     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
0147 }
0148 
0149 void SenderIdentitiesModelTest::testMoveLastIdentity()
0150 {
0151     model->appendIdentity(identity1);
0152     model->appendIdentity(identity2);
0153     model->appendIdentity(identity3);
0154     QCOMPARE(model->rowCount(), 3);
0155     QPersistentModelIndex nameIndex =
0156         model->index(0, SenderIdentitiesModel::COLUMN_NAME);
0157     QPersistentModelIndex signatureIndex =
0158         model->index(0, SenderIdentitiesModel::COLUMN_SIGNATURE);
0159     QSignalSpy spy(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
0160     QSignalSpy spy2(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
0161     model->moveIdentity(2, 0);
0162     QCOMPARE(model->rowCount(), 3);
0163     QCOMPARE(spy.count(), 1);
0164     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 2, 2, QModelIndex(), 0));
0165     QCOMPARE(spy2.count(), 1);
0166     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 2, 2, QModelIndex(), 0));
0167     COMPARE_ROWNUM(identity3, 0);
0168     COMPARE_ROWNUM(identity1, 1);
0169     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
0170     COMPARE_ROWNUM(identity2, 2);
0171     model->appendIdentity(identity3);
0172     QCOMPARE(model->rowCount(), 4);
0173     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
0174     model->removeIdentityAt(0);
0175     QCOMPARE(model->rowCount(), 3);
0176     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
0177 }
0178 
0179 void SenderIdentitiesModelTest::testMoveMiddleIdentity()
0180 {
0181     model->appendIdentity(identity1);
0182     model->appendIdentity(identity2);
0183     model->appendIdentity(identity3);
0184     QCOMPARE(model->rowCount(), 3);
0185     QPersistentModelIndex nameIndex =
0186         model->index(0, SenderIdentitiesModel::COLUMN_NAME);
0187     QPersistentModelIndex signatureIndex =
0188         model->index(0, SenderIdentitiesModel::COLUMN_SIGNATURE);
0189     QSignalSpy spy(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
0190     QSignalSpy spy2(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
0191     model->moveIdentity(1, 2);
0192     QCOMPARE(model->rowCount(), 3);
0193     QCOMPARE(spy.count(), 1);
0194     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 1, 1, QModelIndex(), 3));
0195     QCOMPARE(spy2.count(), 1);
0196     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 1, 1, QModelIndex(), 3));
0197     COMPARE_ROWNUM(identity3, 1);
0198     COMPARE_ROWNUM(identity1, 0);
0199     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
0200     COMPARE_ROWNUM(identity2, 2);
0201     model->appendIdentity(identity3);
0202     QCOMPARE(model->rowCount(), 4);
0203     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
0204     model->removeIdentityAt(0);
0205     QCOMPARE(model->rowCount(), 3);
0206     QVERIFY(!nameIndex.isValid());
0207     QVERIFY(!signatureIndex.isValid());
0208 }
0209 
0210 void SenderIdentitiesModelTest::cleanup()
0211 {
0212     delete model;
0213 }
0214 
0215 
0216 
0217 QTEST_GUILESS_MAIN( SenderIdentitiesModelTest )