1 /* Copyright (C) 2012 - 2013 Peter Amidon <peter@picnicpark.org>
2 
3    This file is part of the Trojita Qt IMAP e-mail client,
4    http://trojita.flaska.net/
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License as
8    published by the Free Software Foundation; either version 2 of
9    the License or (at your option) version 3 or any later version
10    accepted by the membership of KDE e.V. (or its successor approved
11    by the membership of KDE e.V.), which shall act as a proxy
12    defined in Section 14 of version 3 of the license.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 
24 #include <QtTest>
25 #include "test_SenderIdentitiesModel.h"
26 #include "Utils/ModelEvents.h"
27 #include "Common/MetaTypes.h"
28 
29 #define COMPARE_ROWNUM(identity, rowNumber) \
30   QVERIFY(model->index(rowNumber, SenderIdentitiesModel::COLUMN_NAME).isValid()); \
31   QCOMPARE(model->data(model->index(rowNumber, SenderIdentitiesModel::COLUMN_NAME)).toString(), identity.realName); \
32   QCOMPARE(model->data(model->index(rowNumber, SenderIdentitiesModel::COLUMN_SIGNATURE)).toString(), identity.signature);
33 
34 #define COMPARE_INDEX(identity, indexName, indexSignature) \
35   QVERIFY(indexName.isValid()); \
36   QVERIFY(indexSignature.isValid()); \
37   QCOMPARE(model->data(indexName).toString(), identity.realName); \
38   QCOMPARE(model->data(indexSignature).toString(), identity.signature);
39 
40 using namespace Composer;
41 
SenderIdentitiesModelTest()42 SenderIdentitiesModelTest::SenderIdentitiesModelTest() :
43     identity1(QStringLiteral("test name #1"), QStringLiteral("test1@mail.org"), QStringLiteral("testing"), QStringLiteral("tester 1")),
44     identity2(QStringLiteral("test name #2"), QStringLiteral("test2@mail.org"), QStringLiteral("testing"), QStringLiteral("tester 2")),
45     identity3(QStringLiteral("test name #3"), QStringLiteral("test3@mail.org"), QStringLiteral("testing"), QStringLiteral("tester 3"))
46 {
47 }
48 
initTestCase()49 void SenderIdentitiesModelTest::initTestCase()
50 {
51     Common::registerMetaTypes();
52 }
53 
init()54 void SenderIdentitiesModelTest::init()
55 {
56     model = new SenderIdentitiesModel();
57 }
58 
testPropertyStorage()59 void SenderIdentitiesModelTest::testPropertyStorage()
60 {
61     QLatin1String realName("Testing Tester");
62     QLatin1String emailAddress("test@testing.org");
63     QLatin1String organization("Testing Inc.");
64     QLatin1String signature("A tester working for Testing Inc.");
65     ItemSenderIdentity testIdentity(realName, emailAddress, organization, signature);
66     QCOMPARE(testIdentity.realName, realName);
67     QCOMPARE(testIdentity.emailAddress, emailAddress);
68     QCOMPARE(testIdentity.organisation, organization);
69     QCOMPARE(testIdentity.signature, signature);
70 }
71 
testAddIdentity()72 void SenderIdentitiesModelTest::testAddIdentity()
73 {
74     QSignalSpy spy(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
75     QSignalSpy spy2(model, SIGNAL(rowsInserted(QModelIndex, int, int)));
76     model->appendIdentity(identity1);
77     QCOMPARE(model->rowCount(), 1);
78     QCOMPARE(spy.count(), 1);
79     QCOMPARE(ModelInsertRemoveEvent(spy.at(0)), ModelInsertRemoveEvent(QModelIndex(), 0, 0));
80     QCOMPARE(spy2.count(), 1);
81     QCOMPARE(ModelInsertRemoveEvent(spy2.at(0)), ModelInsertRemoveEvent(QModelIndex(), 0, 0));
82     COMPARE_ROWNUM(identity1, 0);
83 }
84 
testRemoveIdentity1()85 void SenderIdentitiesModelTest::testRemoveIdentity1()
86 {
87     model->appendIdentity(identity1);
88     model->appendIdentity(identity2);
89     QCOMPARE(model->rowCount(), 2);
90     QSignalSpy spy(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
91     QSignalSpy spy2(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
92     model->removeIdentityAt(1);
93     QCOMPARE(model->rowCount(), 1);
94     QCOMPARE(spy.count(), 1);
95     QCOMPARE(ModelInsertRemoveEvent(spy.at(0)), ModelInsertRemoveEvent(QModelIndex(), 1, 1));
96     QCOMPARE(spy2.count(), 1);
97     QCOMPARE(ModelInsertRemoveEvent(spy2.at(0)), ModelInsertRemoveEvent(QModelIndex(), 1, 1)) ;
98     QCOMPARE(model->rowCount(), 1);
99     COMPARE_ROWNUM(identity1, 0);
100 }
101 
testRemoveIdentity2()102 void SenderIdentitiesModelTest::testRemoveIdentity2()
103 {
104     model->appendIdentity(identity1);
105     model->appendIdentity(identity2);
106     QCOMPARE(model->rowCount(), 2);
107     QSignalSpy spy(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
108     QSignalSpy spy2(model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
109     model->removeIdentityAt(0);
110     QCOMPARE(model->rowCount(), 1);
111     QCOMPARE(spy.count(), 1);
112     QCOMPARE(ModelInsertRemoveEvent(spy.at(0)), ModelInsertRemoveEvent(QModelIndex(), 0, 0));
113     QCOMPARE(spy2.count(), 1);
114     QCOMPARE(ModelInsertRemoveEvent(spy2.at(0)), ModelInsertRemoveEvent(QModelIndex(), 0, 0));
115     QCOMPARE(model->rowCount(), 1);
116     COMPARE_ROWNUM(identity2, 0);
117 }
118 
testMoveFirstIdentity()119 void SenderIdentitiesModelTest::testMoveFirstIdentity()
120 {
121     model->appendIdentity(identity1);
122     model->appendIdentity(identity2);
123     model->appendIdentity(identity3);
124     QCOMPARE(model->rowCount(), 3);
125     QPersistentModelIndex nameIndex =
126         model->index(0, SenderIdentitiesModel::COLUMN_NAME);
127     QPersistentModelIndex signatureIndex =
128         model->index(0, SenderIdentitiesModel::COLUMN_SIGNATURE);
129     QSignalSpy spy(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
130     QSignalSpy spy2(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
131     model->moveIdentity(0, 2);
132     QCOMPARE(model->rowCount(), 3);
133     QCOMPARE(spy.count(), 1);
134     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 0, 0, QModelIndex(), 3));
135     QCOMPARE(spy2.count(), 1);
136     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 0, 0, QModelIndex(), 3));
137     COMPARE_ROWNUM(identity2, 0);
138     COMPARE_ROWNUM(identity1, 2);
139     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
140     COMPARE_ROWNUM(identity3, 1);
141     model->appendIdentity(identity3);
142     QCOMPARE(model->rowCount(), 4);
143     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
144     model->removeIdentityAt(0);
145     QCOMPARE(model->rowCount(), 3);
146     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
147 }
148 
testMoveLastIdentity()149 void SenderIdentitiesModelTest::testMoveLastIdentity()
150 {
151     model->appendIdentity(identity1);
152     model->appendIdentity(identity2);
153     model->appendIdentity(identity3);
154     QCOMPARE(model->rowCount(), 3);
155     QPersistentModelIndex nameIndex =
156         model->index(0, SenderIdentitiesModel::COLUMN_NAME);
157     QPersistentModelIndex signatureIndex =
158         model->index(0, SenderIdentitiesModel::COLUMN_SIGNATURE);
159     QSignalSpy spy(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
160     QSignalSpy spy2(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
161     model->moveIdentity(2, 0);
162     QCOMPARE(model->rowCount(), 3);
163     QCOMPARE(spy.count(), 1);
164     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 2, 2, QModelIndex(), 0));
165     QCOMPARE(spy2.count(), 1);
166     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 2, 2, QModelIndex(), 0));
167     COMPARE_ROWNUM(identity3, 0);
168     COMPARE_ROWNUM(identity1, 1);
169     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
170     COMPARE_ROWNUM(identity2, 2);
171     model->appendIdentity(identity3);
172     QCOMPARE(model->rowCount(), 4);
173     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
174     model->removeIdentityAt(0);
175     QCOMPARE(model->rowCount(), 3);
176     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
177 }
178 
testMoveMiddleIdentity()179 void SenderIdentitiesModelTest::testMoveMiddleIdentity()
180 {
181     model->appendIdentity(identity1);
182     model->appendIdentity(identity2);
183     model->appendIdentity(identity3);
184     QCOMPARE(model->rowCount(), 3);
185     QPersistentModelIndex nameIndex =
186         model->index(0, SenderIdentitiesModel::COLUMN_NAME);
187     QPersistentModelIndex signatureIndex =
188         model->index(0, SenderIdentitiesModel::COLUMN_SIGNATURE);
189     QSignalSpy spy(model, SIGNAL(rowsAboutToBeMoved(QModelIndex, int, int, QModelIndex, int)));
190     QSignalSpy spy2(model, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)));
191     model->moveIdentity(1, 2);
192     QCOMPARE(model->rowCount(), 3);
193     QCOMPARE(spy.count(), 1);
194     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 1, 1, QModelIndex(), 3));
195     QCOMPARE(spy2.count(), 1);
196     QCOMPARE(ModelMoveEvent(spy.at(0)), ModelMoveEvent(QModelIndex(), 1, 1, QModelIndex(), 3));
197     COMPARE_ROWNUM(identity3, 1);
198     COMPARE_ROWNUM(identity1, 0);
199     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
200     COMPARE_ROWNUM(identity2, 2);
201     model->appendIdentity(identity3);
202     QCOMPARE(model->rowCount(), 4);
203     COMPARE_INDEX(identity1, nameIndex, signatureIndex);
204     model->removeIdentityAt(0);
205     QCOMPARE(model->rowCount(), 3);
206     QVERIFY(!nameIndex.isValid());
207     QVERIFY(!signatureIndex.isValid());
208 }
209 
cleanup()210 void SenderIdentitiesModelTest::cleanup()
211 {
212     delete model;
213 }
214 
215 
216 
217 QTEST_GUILESS_MAIN( SenderIdentitiesModelTest )
218