1 /*
2 This file is part of the KContacts framework.
3 SPDX-FileCopyrightText: 2016-2019 Laurent Montel <montel@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8 #include "titletest.h"
9 #include "parametermap_p.h"
10 #include "title.h"
11 #include "vcardtool_p.h"
12 #include <QTest>
13
TitleTest(QObject * parent)14 TitleTest::TitleTest(QObject *parent)
15 : QObject(parent)
16 {
17 }
18
~TitleTest()19 TitleTest::~TitleTest()
20 {
21 }
22
shouldHaveDefaultValue()23 void TitleTest::shouldHaveDefaultValue()
24 {
25 KContacts::Title title;
26 QVERIFY(!title.isValid());
27 QVERIFY(title.title().isEmpty());
28 QVERIFY(title.params().empty());
29 }
30
shouldAssignValue()31 void TitleTest::shouldAssignValue()
32 {
33 const QString lang(QStringLiteral("fr"));
34 KContacts::ParameterMap params;
35 params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
36 params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
37 KContacts::Title title(lang);
38 title.setParams(params);
39 QVERIFY(title.isValid());
40 QVERIFY(!title.title().isEmpty());
41 QCOMPARE(title.title(), lang);
42 QVERIFY(!title.params().empty());
43 QCOMPARE(title.params(), params);
44 }
45
shouldAssignExternal()46 void TitleTest::shouldAssignExternal()
47 {
48 KContacts::Title title;
49 const QString lang(QStringLiteral("fr"));
50 title.setTitle(lang);
51 QVERIFY(title.isValid());
52 QCOMPARE(title.title(), lang);
53 }
54
shouldSerialized()55 void TitleTest::shouldSerialized()
56 {
57 KContacts::Title title;
58 KContacts::Title result;
59 const QString lang(QStringLiteral("fr"));
60 title.setTitle(lang);
61 KContacts::ParameterMap params;
62 params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
63 params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
64 title.setParams(params);
65
66 QByteArray data;
67 QDataStream s(&data, QIODevice::WriteOnly);
68 s << title;
69
70 QDataStream t(&data, QIODevice::ReadOnly);
71 t >> result;
72
73 QVERIFY(title == result);
74 }
75
shouldEqualTitle()76 void TitleTest::shouldEqualTitle()
77 {
78 KContacts::Title title;
79 KContacts::Title result;
80 const QString lang(QStringLiteral("fr"));
81 title.setTitle(lang);
82 KContacts::ParameterMap params;
83 params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
84 params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
85 title.setParams(params);
86
87 result = title;
88 QVERIFY(title == result);
89 }
90
shouldParseTwoTitles()91 void TitleTest::shouldParseTwoTitles()
92 {
93 QByteArray vcarddata(
94 "BEGIN:VCARD\n"
95 "VERSION:3.0\n"
96 "N:LastName;FirstName;;;\n"
97 "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
98 "Title:boo\n"
99 "Title:bla\n"
100 "REV:2015-03-14T09:24:45+00:00\n"
101 "FN:FirstName LastName\n"
102 "END:VCARD\n");
103
104 KContacts::VCardTool vcard;
105 const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
106 QCOMPARE(lst.count(), 1);
107 QCOMPARE(lst.at(0).extraTitleList().count(), 2);
108 QCOMPARE(lst.at(0).title(), QStringLiteral("boo"));
109 QCOMPARE(lst.at(0).extraTitleList().at(0).title(), QStringLiteral("boo"));
110 QCOMPARE(lst.at(0).extraTitleList().at(1).title(), QStringLiteral("bla"));
111 }
112
shouldParseTitle()113 void TitleTest::shouldParseTitle()
114 {
115 QByteArray vcarddata(
116 "BEGIN:VCARD\n"
117 "VERSION:3.0\n"
118 "N:LastName;FirstName;;;\n"
119 "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
120 "Title:boo\n"
121 "REV:2015-03-14T09:24:45+00:00\n"
122 "FN:FirstName LastName\n"
123 "END:VCARD\n");
124
125 KContacts::VCardTool vcard;
126 const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
127 QCOMPARE(lst.count(), 1);
128 QCOMPARE(lst.at(0).extraTitleList().count(), 1);
129 QCOMPARE(lst.at(0).title(), QStringLiteral("boo"));
130 QCOMPARE(lst.at(0).extraTitleList().at(0).title(), QStringLiteral("boo"));
131 }
132
shouldParseWithoutTitle()133 void TitleTest::shouldParseWithoutTitle()
134 {
135 QByteArray vcarddata(
136 "BEGIN:VCARD\n"
137 "VERSION:3.0\n"
138 "N:LastName;FirstName;;;\n"
139 "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
140 "REV:2015-03-14T09:24:45+00:00\n"
141 "FN:FirstName LastName\n"
142 "END:VCARD\n");
143
144 KContacts::VCardTool vcard;
145 const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
146 QCOMPARE(lst.count(), 1);
147 QCOMPARE(lst.at(0).extraTitleList().count(), 0);
148 QCOMPARE(lst.at(0).title(), QString());
149 }
150
shouldCreateVCard()151 void TitleTest::shouldCreateVCard()
152 {
153 KContacts::AddresseeList lst;
154 KContacts::Addressee addr;
155 addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
156 addr.setUid(QStringLiteral("testuid"));
157 KContacts::Title::List lstTitle;
158 KContacts::Title title(QStringLiteral("fr"));
159 lstTitle << title;
160 addr.setExtraTitleList(lstTitle);
161 lst << addr;
162 KContacts::VCardTool vcard;
163 const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
164 QByteArray expected(
165 "BEGIN:VCARD\r\n"
166 "VERSION:4.0\r\n"
167 "EMAIL:foo@kde.org\r\n"
168 "N:;;;;\r\n"
169 "TITLE:fr\r\n"
170 "UID:testuid\r\n"
171 "END:VCARD\r\n\r\n");
172
173 QCOMPARE(ba, expected);
174 }
175
shouldCreateVCardWithTwoTitle()176 void TitleTest::shouldCreateVCardWithTwoTitle()
177 {
178 KContacts::AddresseeList lst;
179 KContacts::Addressee addr;
180 addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
181 addr.setUid(QStringLiteral("testuid"));
182 KContacts::Title::List lstTitle;
183 KContacts::Title title(QStringLiteral("fr"));
184 KContacts::Title title2(QStringLiteral("fr2"));
185 lstTitle << title << title2;
186 addr.setExtraTitleList(lstTitle);
187 lst << addr;
188 KContacts::VCardTool vcard;
189 const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
190 QByteArray expected(
191 "BEGIN:VCARD\r\n"
192 "VERSION:4.0\r\n"
193 "EMAIL:foo@kde.org\r\n"
194 "N:;;;;\r\n"
195 "TITLE:fr\r\n"
196 "TITLE:fr2\r\n"
197 "UID:testuid\r\n"
198 "END:VCARD\r\n\r\n");
199
200 QCOMPARE(ba, expected);
201 }
202
shouldCreateVCardWithParameters()203 void TitleTest::shouldCreateVCardWithParameters()
204 {
205 KContacts::AddresseeList lst;
206 KContacts::Addressee addr;
207 addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
208 addr.setUid(QStringLiteral("testuid"));
209 KContacts::Title::List lstTitle;
210 KContacts::Title title(QStringLiteral("fr"));
211 KContacts::ParameterMap params;
212 params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
213 params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
214 title.setParams(params);
215 lstTitle << title;
216 addr.setExtraTitleList(lstTitle);
217 lst << addr;
218 KContacts::VCardTool vcard;
219 const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
220 QByteArray expected(
221 "BEGIN:VCARD\r\n"
222 "VERSION:4.0\r\n"
223 "EMAIL:foo@kde.org\r\n"
224 "N:;;;;\r\n"
225 "TITLE;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
226 "UID:testuid\r\n"
227 "END:VCARD\r\n\r\n");
228 QCOMPARE(ba, expected);
229 }
230
shouldGenerateTitleForVCard3()231 void TitleTest::shouldGenerateTitleForVCard3()
232 {
233 KContacts::AddresseeList lst;
234 KContacts::Addressee addr;
235 addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
236 addr.setUid(QStringLiteral("testuid"));
237 KContacts::Title::List lstTitle;
238 KContacts::Title title(QStringLiteral("fr"));
239 KContacts::ParameterMap params;
240 params.push_back({QStringLiteral("Foo1"), {QStringLiteral("bla1"), QStringLiteral("blo1")}});
241 params.push_back({QStringLiteral("Foo2"), {QStringLiteral("bla2"), QStringLiteral("blo2")}});
242 title.setParams(params);
243 lstTitle << title;
244 addr.setExtraTitleList(lstTitle);
245 lst << addr;
246 KContacts::VCardTool vcard;
247 const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
248 QByteArray expected(
249 "BEGIN:VCARD\r\n"
250 "VERSION:3.0\r\n"
251 "EMAIL:foo@kde.org\r\n"
252 "N:;;;;\r\n"
253 "TITLE;FOO1=bla1,blo1;FOO2=bla2,blo2:fr\r\n"
254 "UID:testuid\r\n"
255 "END:VCARD\r\n\r\n");
256 QCOMPARE(ba, expected);
257 }
258
259 QTEST_MAIN(TitleTest)
260