1 /*
2     This file is part of the KContacts framework.
3     SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "soundtest.h"
9 #include "addressee.h"
10 #include "kcontacts/sound.h"
11 #include "vcardtool_p.h"
12 #include <QTest>
13 
QTEST_MAIN(SoundTest)14 QTEST_MAIN(SoundTest)
15 
16 static QByteArray testData()
17 {
18     static QByteArray data;
19 
20     if (data.isNull()) {
21         for (int i = 0; i < 20; ++i) {
22             data.append(char(i));
23         }
24     }
25 
26     return data;
27 }
28 
emptyTest()29 void SoundTest::emptyTest()
30 {
31     KContacts::Sound sound;
32 
33     QVERIFY(sound.isEmpty());
34 }
35 
storeTestIntern()36 void SoundTest::storeTestIntern()
37 {
38     KContacts::Sound sound;
39 
40     sound.setData(testData());
41 
42     QVERIFY(sound.isIntern() == true);
43     QVERIFY(sound.data() == testData());
44 }
45 
storeTestExtern()46 void SoundTest::storeTestExtern()
47 {
48     KContacts::Sound sound;
49 
50     sound.setUrl(QLatin1String("http://myhomepage.com/sound.wav"));
51 
52     QVERIFY(sound.isIntern() == false);
53     QVERIFY(sound.url() == QLatin1String("http://myhomepage.com/sound.wav"));
54 }
55 
equalsTestIntern()56 void SoundTest::equalsTestIntern()
57 {
58     KContacts::Sound sound1;
59     KContacts::Sound sound2;
60 
61     sound1.setData(testData());
62     sound2.setData(testData());
63 
64     QVERIFY(sound1 == sound2);
65 }
66 
equalsTestExtern()67 void SoundTest::equalsTestExtern()
68 {
69     KContacts::Sound sound1;
70     KContacts::Sound sound2;
71 
72     sound1.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
73     sound2.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
74 
75     QVERIFY(sound1 == sound2);
76 }
77 
differsTest()78 void SoundTest::differsTest()
79 {
80     KContacts::Sound sound1;
81     KContacts::Sound sound2;
82 
83     sound1.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
84     sound2.setData(testData());
85 
86     QVERIFY(sound1 != sound2);
87 }
88 
assignmentTestIntern()89 void SoundTest::assignmentTestIntern()
90 {
91     KContacts::Sound sound1;
92     KContacts::Sound sound2;
93 
94     sound1.setData(testData());
95 
96     sound2 = sound1;
97 
98     QVERIFY(sound1 == sound2);
99 }
100 
assignmentTestExtern()101 void SoundTest::assignmentTestExtern()
102 {
103     KContacts::Sound sound1;
104     KContacts::Sound sound2;
105 
106     sound1.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
107 
108     sound2 = sound1;
109 
110     QVERIFY(sound1 == sound2);
111 }
112 
serializeTest()113 void SoundTest::serializeTest()
114 {
115     KContacts::Sound sound1;
116     KContacts::Sound sound2;
117 
118     sound1.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
119     sound1.setData(testData());
120 
121     QByteArray data;
122     QDataStream s(&data, QIODevice::WriteOnly);
123     s << sound1;
124 
125     QDataStream t(&data, QIODevice::ReadOnly);
126     t >> sound2;
127 
128     QVERIFY(sound1 == sound2);
129 }
130 
shouldParseSource()131 void SoundTest::shouldParseSource()
132 {
133     QByteArray vcarddata(
134         "BEGIN:VCARD\n"
135         "VERSION:3.0\n"
136         "N:LastName;FirstName;;;\n"
137         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\n"
138         "REV:2015-03-14T09:24:45+00:00\n"
139         "SOUND;VALUE=URI:http://myhomepage.com/sound.wav\n"
140         "FN:FirstName LastName\n"
141         "END:VCARD\n");
142 
143     KContacts::VCardTool vcard;
144     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
145     QCOMPARE(lst.count(), 1);
146     QVERIFY(!lst.at(0).sound().isEmpty());
147     KContacts::Sound sound = lst.at(0).sound();
148     QVERIFY(!sound.isIntern());
149     QCOMPARE(sound.url(), QStringLiteral("http://myhomepage.com/sound.wav"));
150 }
151 
shouldGenerateVCard4WithData()152 void SoundTest::shouldGenerateVCard4WithData()
153 {
154     KContacts::Addressee::List lst;
155     KContacts::Addressee addr;
156     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
157     addr.setUid(QStringLiteral("testuid"));
158     KContacts::Sound sound1;
159 
160     sound1.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
161     sound1.setData(testData());
162     addr.setSound(sound1);
163 
164     lst << addr;
165     KContacts::VCardTool vcard;
166     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
167     QByteArray expected;
168     expected = QByteArray(
169         "BEGIN:VCARD\r\n"
170         "VERSION:4.0\r\n"
171         "EMAIL:foo@kde.org\r\n"
172         "N:;;;;\r\n"
173         "SOUND;ENCODING=b:AAECAwQFBgcICQoLDA0ODxAREhM=\r\n"
174         "UID:testuid\r\n"
175         "END:VCARD\r\n\r\n");
176 
177     QCOMPARE(ba, expected);
178 }
179 
shouldGenerateVCard4WithUrl()180 void SoundTest::shouldGenerateVCard4WithUrl()
181 {
182     KContacts::Addressee::List lst;
183     KContacts::Addressee addr;
184     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
185     addr.setUid(QStringLiteral("testuid"));
186     KContacts::Sound sound1;
187 
188     sound1.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
189     addr.setSound(sound1);
190 
191     lst << addr;
192     KContacts::VCardTool vcard;
193     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
194     QByteArray expected;
195     expected = QByteArray(
196         "BEGIN:VCARD\r\n"
197         "VERSION:4.0\r\n"
198         "EMAIL:foo@kde.org\r\n"
199         "N:;;;;\r\n"
200         "SOUND;VALUE=URI:http://myhomepage.com/sound.wav\r\n"
201         "UID:testuid\r\n"
202         "END:VCARD\r\n\r\n");
203 
204     QCOMPARE(ba, expected);
205 }
206 
shouldGenerateVCard3WithData()207 void SoundTest::shouldGenerateVCard3WithData()
208 {
209     KContacts::Addressee::List lst;
210     KContacts::Addressee addr;
211     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
212     addr.setUid(QStringLiteral("testuid"));
213     KContacts::Sound sound1;
214 
215     sound1.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
216     sound1.setData(testData());
217     addr.setSound(sound1);
218 
219     lst << addr;
220     KContacts::VCardTool vcard;
221     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
222     QByteArray expected;
223     expected = QByteArray(
224         "BEGIN:VCARD\r\n"
225         "VERSION:3.0\r\n"
226         "EMAIL:foo@kde.org\r\n"
227         "N:;;;;\r\n"
228         "SOUND;ENCODING=b:AAECAwQFBgcICQoLDA0ODxAREhM=\r\n"
229         "UID:testuid\r\n"
230         "END:VCARD\r\n\r\n");
231 
232     QCOMPARE(ba, expected);
233 }
234 
shouldGenerateVCard3WithUrl()235 void SoundTest::shouldGenerateVCard3WithUrl()
236 {
237     KContacts::Addressee::List lst;
238     KContacts::Addressee addr;
239     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
240     addr.setUid(QStringLiteral("testuid"));
241     KContacts::Sound sound1;
242 
243     sound1.setUrl(QStringLiteral("http://myhomepage.com/sound.wav"));
244     addr.setSound(sound1);
245 
246     lst << addr;
247     KContacts::VCardTool vcard;
248     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
249     QByteArray expected;
250     expected = QByteArray(
251         "BEGIN:VCARD\r\n"
252         "VERSION:3.0\r\n"
253         "EMAIL:foo@kde.org\r\n"
254         "N:;;;;\r\n"
255         "SOUND;VALUE=URI:http://myhomepage.com/sound.wav\r\n"
256         "UID:testuid\r\n"
257         "END:VCARD\r\n\r\n");
258 
259     QCOMPARE(ba, expected);
260 }
261