Changeset View
Changeset View
Standalone View
Standalone View
autotests/identitytest.cpp
Show All 20 Lines | |||||
21 | #include "identitymanager.h" | 21 | #include "identitymanager.h" | ||
22 | #include "identity.h" | 22 | #include "identity.h" | ||
23 | 23 | | |||
24 | #include <KConfig> | 24 | #include <KConfig> | ||
25 | #include <KConfigGroup> | 25 | #include <KConfigGroup> | ||
26 | 26 | | |||
27 | #include <QMimeData> | 27 | #include <QMimeData> | ||
28 | #include <QStandardPaths> | 28 | #include <QStandardPaths> | ||
29 | #include <QDataStream> | ||||
29 | 30 | | |||
30 | using namespace KIdentityManagement; | 31 | using namespace KIdentityManagement; | ||
31 | 32 | | |||
32 | QTEST_GUILESS_MAIN(IdentityTester) | 33 | QTEST_GUILESS_MAIN(IdentityTester) | ||
33 | 34 | | |||
34 | void IdentityTester::initTestCase() | 35 | void IdentityTester::initTestCase() | ||
35 | { | 36 | { | ||
36 | QStandardPaths::setTestModeEnabled(true); | 37 | QStandardPaths::setTestModeEnabled(true); | ||
37 | } | 38 | } | ||
38 | 39 | | |||
40 | bool IdentityTester::compareIdentities(const Identity &actual, const Identity &expected) | ||||
41 | { | ||||
42 | bool ok = false; | ||||
43 | [&]() { | ||||
44 | QCOMPARE(actual.uoid(), expected.uoid()); | ||||
45 | // Don't compare isDefault - only one copy can be default, so this property | ||||
46 | // is not copied! It does not affect result of operator==() either. | ||||
47 | //QCOMPARE(actual.isDefault(), expected.isDefault()); | ||||
48 | QCOMPARE(actual.identityName(), expected.identityName()); | ||||
49 | QCOMPARE(actual.fullName(), expected.fullName()); | ||||
50 | QCOMPARE(actual.organization(), expected.organization()); | ||||
51 | QCOMPARE(actual.pgpEncryptionKey(), expected.pgpEncryptionKey()); | ||||
52 | QCOMPARE(actual.pgpSigningKey(), expected.pgpSigningKey()); | ||||
53 | QCOMPARE(actual.smimeEncryptionKey(), expected.smimeEncryptionKey()); | ||||
54 | QCOMPARE(actual.smimeSigningKey(), expected.smimeSigningKey()); | ||||
55 | QCOMPARE(actual.preferredCryptoMessageFormat(), expected.preferredCryptoMessageFormat()); | ||||
56 | QCOMPARE(actual.emailAliases(), expected.emailAliases()); | ||||
57 | QCOMPARE(actual.primaryEmailAddress(), expected.primaryEmailAddress()); | ||||
58 | QCOMPARE(actual.vCardFile(), expected.vCardFile()); | ||||
59 | QCOMPARE(actual.replyToAddr(), expected.replyToAddr()); | ||||
60 | QCOMPARE(actual.bcc(), expected.bcc()); | ||||
61 | QCOMPARE(actual.cc(), expected.cc()); | ||||
62 | QCOMPARE(actual.attachVcard(), expected.attachVcard()); | ||||
63 | QCOMPARE(actual.autocorrectionLanguage(), expected.autocorrectionLanguage()); | ||||
64 | QCOMPARE(actual.disabledFcc(), expected.disabledFcc()); | ||||
65 | QCOMPARE(actual.pgpAutoSign(), expected.pgpAutoSign()); | ||||
66 | QCOMPARE(actual.pgpAutoEncrypt(), expected.pgpAutoEncrypt()); | ||||
67 | QCOMPARE(actual.defaultDomainName(), expected.defaultDomainName()); | ||||
68 | QCOMPARE(actual.signatureText(), expected.signatureText()); | ||||
69 | QCOMPARE(const_cast<Identity&>(actual).signature(), const_cast<Identity&>(expected).signature()); | ||||
70 | QCOMPARE(actual.transport(), expected.transport()); | ||||
71 | QCOMPARE(actual.fcc(), expected.fcc()); | ||||
72 | QCOMPARE(actual.drafts(), expected.drafts()); | ||||
73 | QCOMPARE(actual.templates(), expected.templates()); | ||||
74 | QCOMPARE(actual.dictionary(), expected.dictionary()); | ||||
75 | QCOMPARE(actual.isXFaceEnabled(), expected.isXFaceEnabled()); | ||||
76 | QCOMPARE(actual.xface(), expected.xface()); | ||||
77 | ok = true; | ||||
78 | }(); | ||||
79 | | ||||
80 | return ok; | ||||
81 | } | ||||
82 | | ||||
83 | | ||||
84 | void IdentityTester::test_Identity() | ||||
85 | { | ||||
86 | Identity identity; | ||||
87 | identity.setUoid(42); | ||||
88 | QCOMPARE(identity.uoid(), 42u); | ||||
89 | identity.setIsDefault(true); | ||||
90 | QCOMPARE(identity.isDefault(), true); | ||||
91 | identity.setIdentityName(QStringLiteral("01234")); | ||||
92 | QCOMPARE(identity.identityName(), QStringLiteral("01234")); | ||||
93 | identity.setFullName(QStringLiteral("Daniel Vrátil")); | ||||
94 | QCOMPARE(identity.fullName(), QStringLiteral("Daniel Vrátil")); | ||||
95 | identity.setOrganization(QStringLiteral("KDE")); | ||||
96 | QCOMPARE(identity.organization(), QStringLiteral("KDE")); | ||||
97 | identity.setPGPEncryptionKey("0x0123456789ABCDEF"); | ||||
98 | QCOMPARE(identity.pgpEncryptionKey(), QByteArray("0x0123456789ABCDEF")); | ||||
99 | identity.setPGPSigningKey("0xFEDCBA9876543210"); | ||||
100 | QCOMPARE(identity.pgpSigningKey(), QByteArray("0xFEDCBA9876543210")); | ||||
101 | identity.setSMIMEEncryptionKey("0xABCDEF0123456789"); | ||||
102 | QCOMPARE(identity.smimeEncryptionKey(), QByteArray("0xABCDEF0123456789")); | ||||
103 | identity.setSMIMESigningKey("0xFEDCBA9876543210"); | ||||
104 | QCOMPARE(identity.smimeSigningKey(), QByteArray("0xFEDCBA9876543210")); | ||||
105 | identity.setPreferredCryptoMessageFormat(QStringLiteral("PGP")); | ||||
106 | QCOMPARE(identity.preferredCryptoMessageFormat(), QStringLiteral("PGP")); | ||||
107 | identity.setPrimaryEmailAddress(QStringLiteral("dvratil@kde.org")); | ||||
108 | const QStringList aliases = { QStringLiteral("dvratil1@kde.org"), QStringLiteral("example@example.org") }; | ||||
109 | identity.setEmailAliases(aliases); | ||||
110 | QCOMPARE(identity.emailAliases(), aliases); | ||||
111 | QVERIFY(identity.matchesEmailAddress(QStringLiteral("dvratil@kde.org"))); | ||||
112 | QVERIFY(identity.matchesEmailAddress(aliases[0])); | ||||
113 | QVERIFY(identity.matchesEmailAddress(aliases[1])); | ||||
114 | | ||||
115 | QCOMPARE(identity.primaryEmailAddress(), QStringLiteral("dvratil@kde.org")); | ||||
116 | const auto vcardFile = QStringLiteral("BEGIN:VCARD\n" | ||||
117 | "VERSION:2.1\n" | ||||
118 | "N:Vrátil;Daniel;;\n" | ||||
119 | "END:VCARD"); | ||||
120 | identity.setVCardFile(vcardFile); | ||||
121 | QCOMPARE(identity.vCardFile(), vcardFile); | ||||
122 | identity.setReplyToAddr(QStringLiteral("dvratil+reply@kde.org")); | ||||
123 | QCOMPARE(identity.replyToAddr(), QStringLiteral("dvratil+reply@kde.org")); | ||||
124 | identity.setBcc(QStringLiteral("dvratil+bcc@kde.org")); | ||||
125 | QCOMPARE(identity.bcc(), QStringLiteral("dvratil+bcc@kde.org")); | ||||
126 | identity.setCc(QStringLiteral("dvratil+cc@kde.org")); | ||||
127 | QCOMPARE(identity.cc(), QStringLiteral("dvratil+cc@kde.org")); | ||||
128 | identity.setAttachVcard(true); | ||||
129 | QCOMPARE(identity.attachVcard(), true); | ||||
130 | identity.setAutocorrectionLanguage(QStringLiteral("cs_CZ")); | ||||
131 | QCOMPARE(identity.autocorrectionLanguage(), QStringLiteral("cs_CZ")); | ||||
132 | identity.setDisabledFcc(true); | ||||
133 | QCOMPARE(identity.disabledFcc(), true); | ||||
134 | identity.setPgpAutoSign(true); | ||||
135 | QCOMPARE(identity.pgpAutoSign(), true); | ||||
136 | identity.setPgpAutoEncrypt(true); | ||||
137 | QCOMPARE(identity.pgpAutoEncrypt(), true); | ||||
138 | identity.setDefaultDomainName(QStringLiteral("kde.org")); | ||||
139 | QCOMPARE(identity.defaultDomainName(), QStringLiteral("kde.org")); | ||||
140 | Signature sig; | ||||
141 | sig.setEnabledSignature(true); | ||||
142 | sig.setText(QStringLiteral("Regards,\nDaniel")); | ||||
143 | identity.setSignature(sig); | ||||
144 | QCOMPARE(identity.signature(), sig); | ||||
145 | identity.setTransport(QStringLiteral("smtp")); | ||||
146 | QCOMPARE(identity.transport(), QStringLiteral("smtp")); | ||||
147 | identity.setFcc(QStringLiteral("123")); // must be an Akonadi::Collection::Id | ||||
148 | QCOMPARE(identity.fcc(), QStringLiteral("123")); | ||||
149 | identity.setDrafts(QStringLiteral("124")); | ||||
150 | QCOMPARE(identity.drafts(), QStringLiteral("124")); | ||||
151 | identity.setTemplates(QStringLiteral("125")); | ||||
152 | QCOMPARE(identity.templates(), QStringLiteral("125")); | ||||
153 | identity.setDictionary(QStringLiteral("Čeština")); | ||||
154 | QCOMPARE(identity.dictionary(), QStringLiteral("Čeština")); | ||||
155 | identity.setXFaceEnabled(true); | ||||
156 | QCOMPARE(identity.isXFaceEnabled(), true); | ||||
157 | identity.setXFace(QStringLiteral(":-P")); | ||||
158 | QCOMPARE(identity.xface(), QStringLiteral(":-P")); | ||||
159 | | ||||
160 | // Test copy | ||||
161 | { | ||||
162 | const Identity copy = identity; | ||||
163 | QCOMPARE(copy, identity); | ||||
164 | // Test that the operator==() actually works | ||||
165 | QVERIFY(compareIdentities(copy, identity)); | ||||
166 | | ||||
knauss: you should also test the negativ outcome, so that you really make sure, that the == operator… | |||||
167 | identity.setXFace(QStringLiteral(":-(")); | ||||
168 | QVERIFY(!(copy == identity)); | ||||
169 | } | ||||
170 | | ||||
171 | // Test serialization | ||||
172 | { | ||||
173 | QByteArray ba; | ||||
174 | QDataStream inStream(&ba, QIODevice::WriteOnly); | ||||
175 | inStream << identity; | ||||
176 | | ||||
177 | Identity copy; | ||||
178 | QDataStream outStream(&ba, QIODevice::ReadOnly); | ||||
179 | outStream >> copy; | ||||
180 | | ||||
181 | // We already verified that operator==() works, so this should be enough | ||||
182 | QVERIFY(compareIdentities(copy, identity)); | ||||
183 | } | ||||
184 | } | ||||
185 | | ||||
186 | | ||||
39 | void IdentityTester::test_NullIdentity() | 187 | void IdentityTester::test_NullIdentity() | ||
40 | { | 188 | { | ||
41 | IdentityManager manager; | 189 | IdentityManager manager; | ||
42 | QVERIFY(manager.identityForAddress(QStringLiteral("thisaddressforsuredoesnotexist@kde.org")).isNull()); | 190 | QVERIFY(manager.identityForAddress(QStringLiteral("thisaddressforsuredoesnotexist@kde.org")).isNull()); | ||
43 | } | 191 | } | ||
44 | 192 | | |||
45 | void IdentityTester::test_Aliases() | 193 | void IdentityTester::test_Aliases() | ||
46 | { | 194 | { | ||
Show All 30 Lines | |||||
77 | manager.commit(); | 225 | manager.commit(); | ||
78 | 226 | | |||
79 | QCOMPARE(manager.allEmails().size(), 4); | 227 | QCOMPARE(manager.allEmails().size(), 4); | ||
80 | QCOMPARE(manager.identityForAddress(QStringLiteral("firstname@example.com")).identityName().toLatin1().data(), "Test1"); | 228 | QCOMPARE(manager.identityForAddress(QStringLiteral("firstname@example.com")).identityName().toLatin1().data(), "Test1"); | ||
81 | } | 229 | } | ||
82 | 230 | | |||
83 | void IdentityTester::test_toMimeData() | 231 | void IdentityTester::test_toMimeData() | ||
84 | { | 232 | { | ||
85 | IdentityManager manager; | 233 | Identity identity(QStringLiteral("Test1")); | ||
86 | Identity &identity = manager.newFromScratch(QStringLiteral("Test1")); | | |||
87 | identity.setFullName(QStringLiteral("name")); | 234 | identity.setFullName(QStringLiteral("name")); | ||
88 | QMimeData mimeData; | 235 | QMimeData mimeData; | ||
89 | identity.populateMimeData(&mimeData); | 236 | identity.populateMimeData(&mimeData); | ||
90 | 237 | | |||
91 | Identity identity2 = Identity::fromMimeData(&mimeData); | 238 | Identity identity2 = Identity::fromMimeData(&mimeData); | ||
92 | 239 | | |||
93 | // The deserializer fills in the QHash will lots of invalid variants, which is OK, but the CTOR doesn't fill | 240 | QVERIFY(compareIdentities(identity, identity2)); | ||
94 | // The hash with the missing fields, so this comparison will fail. Maybe do a smarter Identity== where missing | | |||
95 | // from the hash or being invalid is equivalent | | |||
96 | QEXPECT_FAIL("", "The deserialized instance is different", Continue); | | |||
97 | QCOMPARE(identity, identity2); | 241 | QCOMPARE(identity, identity2); | ||
98 | 242 | | |||
99 | QEXPECT_FAIL("", "Missing qRegisterMetaTypeStreamOperators() I guess ?", Continue); | | |||
100 | QCOMPARE(identity.fullName(), identity2.fullName()); | 243 | QCOMPARE(identity.fullName(), identity2.fullName()); | ||
101 | } | 244 | } |
you should also test the negativ outcome, so that you really make sure, that the == operator not just always return true :)
Something like