File indexing completed on 2024-05-12 05:11:13
0001 /* 0002 SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "noteutils.h" 0008 0009 #include <QDebug> 0010 #include <QHash> 0011 #include <QTest> 0012 0013 #include <KMime/KMimeMessage> 0014 #include <QDateTime> 0015 #include <QTimeZone> 0016 0017 using namespace Akonadi::NoteUtils; 0018 class NotesTest : public QObject 0019 { 0020 Q_OBJECT 0021 private Q_SLOTS: 0022 0023 void testSerializeAndParse() 0024 { 0025 NoteMessageWrapper note; 0026 note.setTitle(QStringLiteral("title")); 0027 note.setText(QStringLiteral("title")); 0028 note.setUid(QStringLiteral("uid")); 0029 note.setClassification(NoteMessageWrapper::Private); 0030 note.setFrom(QStringLiteral("from@kde.org")); 0031 note.setCreationDate(QDateTime(QDate(2012, 3, 3), QTime(3, 3, 3), QTimeZone::utc())); 0032 note.setLastModifiedDate(QDateTime(QDate(2012, 3, 3), QTime(4, 4, 4), QTimeZone::utc())); 0033 Attachment a("testfile2", QStringLiteral("mimetype/mime3")); 0034 a.setLabel(QStringLiteral("label")); 0035 note.attachments() << Attachment(QUrl(QStringLiteral("file://url/to/file")), QStringLiteral("mimetype/mime")) 0036 << Attachment("testfile", QStringLiteral("mimetype/mime2")) << a; 0037 note.custom().insert(QStringLiteral("key1"), QStringLiteral("value1")); 0038 note.custom().insert(QStringLiteral("key2"), QStringLiteral("value2")); 0039 note.custom().insert(QStringLiteral("key3"), QStringLiteral("value3")); 0040 0041 KMime::MessagePtr msg = note.message(); 0042 // qWarning() << msg->encodedContent(); 0043 0044 NoteMessageWrapper result(msg); 0045 0046 QCOMPARE(result.title(), note.title()); 0047 QCOMPARE(result.text(), note.text()); 0048 QCOMPARE(result.textFormat(), note.textFormat()); 0049 QCOMPARE(result.uid(), note.uid()); 0050 QCOMPARE(result.classification(), note.classification()); 0051 QCOMPARE(result.from(), note.from()); 0052 QCOMPARE(result.creationDate(), note.creationDate()); 0053 QCOMPARE(result.lastModifiedDate(), note.lastModifiedDate()); 0054 QCOMPARE(result.custom(), note.custom()); 0055 QCOMPARE(result.attachments(), note.attachments()); 0056 0057 // qWarning() << result.message()->encodedContent(); 0058 } 0059 0060 void createIfEmpty() 0061 { 0062 NoteMessageWrapper note; 0063 NoteMessageWrapper result(note.message()); 0064 QVERIFY(!result.uid().isEmpty()); 0065 QVERIFY(result.creationDate().isValid()); 0066 QVERIFY(result.lastModifiedDate().isValid()); 0067 } 0068 void testNormalTextWithoutAttachments() 0069 { 0070 NoteMessageWrapper note; 0071 QString text( 0072 QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"> \ 0073 <html> \ 0074 <head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\"> p, li { white-space: pre-wrap; } </style></head> \ 0075 <body style=\"font-family:'Sans Serif'; font-size:9pt;\"> <p>sdfg</p></body> \ 0076 </html>")); 0077 note.setText(text); 0078 0079 KMime::Message::Ptr msg = note.message(); 0080 NoteMessageWrapper result(msg); 0081 0082 QCOMPARE(result.text(), text); 0083 QCOMPARE(result.textFormat(), Qt::PlainText); 0084 } 0085 0086 void testRichTextWithoutAttachments() 0087 { 0088 NoteMessageWrapper note; 0089 QString text( 0090 QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"> \ 0091 <html> \ 0092 <head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\"> p, li { white-space: pre-wrap; } </style></head> \ 0093 <body style=\"font-family:'Sans Serif'; font-size:9pt;\"> <p>sdfg</p></body> \ 0094 </html>")); 0095 note.setText(text, Qt::RichText); 0096 0097 KMime::Message::Ptr msg = note.message(); 0098 NoteMessageWrapper result(msg); 0099 0100 QCOMPARE(result.text(), text); 0101 QCOMPARE(result.textFormat(), Qt::RichText); 0102 } 0103 0104 void testRichTextWithAttachments() 0105 { 0106 NoteMessageWrapper note; 0107 QString text( 0108 QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"> \ 0109 <html> \ 0110 <head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\"> p, li { white-space: pre-wrap; } </style></head> \ 0111 <body style=\"font-family:'Sans Serif'; font-size:9pt;\"> <p>sdfg</p></body> \ 0112 </html>")); 0113 note.setText(text, Qt::RichText); 0114 0115 Attachment a(QByteArray("testfile2"), QStringLiteral("mimetype/mime3")); 0116 a.setLabel(QStringLiteral("label")); 0117 note.attachments() << Attachment(QUrl(QStringLiteral("file://url/to/file")), QStringLiteral("mimetype/mime")) << a; 0118 0119 KMime::Message::Ptr msg = note.message(); 0120 NoteMessageWrapper result(msg); 0121 0122 QCOMPARE(result.text(), text); 0123 QCOMPARE(result.textFormat(), Qt::RichText); 0124 QCOMPARE(result.attachments(), note.attachments()); 0125 } 0126 }; 0127 0128 QTEST_MAIN(NotesTest) 0129 0130 #include "notestest.moc"