File indexing completed on 2025-01-05 04:54:26

0001 /*
0002   SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "koeventpopupmenutest.h"
0008 #include "koeventpopupmenu.h"
0009 
0010 #include <Akonadi/CollectionCalendar>
0011 #include <Akonadi/NoteUtils>
0012 
0013 #include <CalendarSupport/NoteEditDialog>
0014 
0015 #include <IncidenceEditor/IncidenceDialog>
0016 #include <IncidenceEditor/IncidenceEditor-Ng>
0017 
0018 #include <QStandardPaths>
0019 
0020 #include <QTest>
0021 KoEventPopupMenuTest::KoEventPopupMenuTest(QObject *parent)
0022     : QObject(parent)
0023 {
0024     QStandardPaths::setTestModeEnabled(true);
0025 }
0026 
0027 void KoEventPopupMenuTest::createEventFromEvent()
0028 {
0029     auto calendar = Akonadi::CollectionCalendar::Ptr::create(Akonadi::Collection{});
0030     KOEventPopupMenu menu;
0031 
0032     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
0033     Akonadi::Item item;
0034     item.setMimeType(KCalendarCore::Event::eventMimeType());
0035     item.setPayload<KCalendarCore::Event::Ptr>(event);
0036 
0037     menu.showIncidencePopup(calendar, item, QDate());
0038     auto createevent = menu.findChild<QAction *>(QStringLiteral("createevent"));
0039     createevent->trigger();
0040 
0041     auto dlg = menu.findChild<IncidenceEditorNG::IncidenceDialog *>(QStringLiteral("incidencedialog"));
0042     QVERIFY(!dlg);
0043 }
0044 
0045 void KoEventPopupMenuTest::createTodoFromTodo()
0046 {
0047     auto calendar = Akonadi::CollectionCalendar::Ptr::create(Akonadi::Collection());
0048     KOEventPopupMenu menu;
0049 
0050     KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
0051     Akonadi::Item item;
0052     item.setMimeType(KCalendarCore::Todo::todoMimeType());
0053     item.setPayload<KCalendarCore::Todo::Ptr>(todo);
0054 
0055     menu.showIncidencePopup(calendar, item, QDate());
0056     auto createtodo = menu.findChild<QAction *>(QStringLiteral("createtodo"));
0057     createtodo->trigger();
0058 
0059     auto dlg = menu.findChild<IncidenceEditorNG::IncidenceDialog *>(QStringLiteral("incidencedialog"));
0060     QVERIFY(!dlg);
0061 }
0062 
0063 void KoEventPopupMenuTest::createEventFromTodo()
0064 {
0065     auto calendar = Akonadi::CollectionCalendar::Ptr::create(Akonadi::Collection());
0066     KOEventPopupMenu menu;
0067 
0068     KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
0069     Akonadi::Item item;
0070     item.setMimeType(KCalendarCore::Todo::todoMimeType());
0071     item.setPayload<KCalendarCore::Todo::Ptr>(todo);
0072 
0073     QDateTime start;
0074     QDateTime end;
0075     QString summary(QStringLiteral("a test"));
0076     start = QDateTime::fromSecsSinceEpoch(1402593346);
0077     end = QDateTime::fromSecsSinceEpoch(1403593346);
0078     todo->setDtStart(start);
0079     todo->setDtDue(end);
0080     todo->setSummary(summary);
0081 
0082     menu.showIncidencePopup(calendar, item, QDate());
0083     auto createevent = menu.findChild<QAction *>(QStringLiteral("createevent"));
0084     createevent->trigger();
0085     auto dlg = menu.findChild<IncidenceEditorNG::IncidenceDialog *>();
0086     QVERIFY(dlg);
0087     auto editor = menu.findChild<IncidenceEditorNG::IncidenceEditor *>();
0088     QVERIFY(editor);
0089     KCalendarCore::Event::Ptr event(editor->incidence<KCalendarCore::Event>());
0090     QVERIFY(event->uid() != todo->uid());
0091     QCOMPARE(event->dtStart(), start);
0092     QCOMPARE(event->dtEnd(), end);
0093     QCOMPARE(event->allDay(), false);
0094     QCOMPARE(event->summary(), summary);
0095 }
0096 
0097 void KoEventPopupMenuTest::createTodoFromEvent()
0098 {
0099     auto calendar = Akonadi::CollectionCalendar::Ptr::create(Akonadi::Collection());
0100     KOEventPopupMenu menu;
0101 
0102     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
0103     Akonadi::Item item;
0104     item.setMimeType(KCalendarCore::Event::eventMimeType());
0105     item.setPayload<KCalendarCore::Event::Ptr>(event);
0106 
0107     QDateTime start;
0108     QDateTime end;
0109     QString summary(QStringLiteral("a test"));
0110     start = QDateTime::fromSecsSinceEpoch(1402593346);
0111     end = QDateTime::fromSecsSinceEpoch(1403593346);
0112     event->setDtStart(start);
0113     event->setDtEnd(end);
0114     event->setSummary(summary);
0115 
0116     menu.showIncidencePopup(calendar, item, QDate());
0117     auto createtodo = menu.findChild<QAction *>(QStringLiteral("createtodo"));
0118     createtodo->trigger();
0119     auto dlg = menu.findChild<IncidenceEditorNG::IncidenceDialog *>();
0120     QVERIFY(dlg);
0121     auto editor = menu.findChild<IncidenceEditorNG::IncidenceEditor *>();
0122     QVERIFY(editor);
0123     KCalendarCore::Todo::Ptr todo(editor->incidence<KCalendarCore::Todo>());
0124     QVERIFY(todo->uid() != event->uid());
0125     QCOMPARE(todo->dtStart(), start);
0126     QCOMPARE(todo->dtDue(), end);
0127     QCOMPARE(todo->allDay(), false);
0128     QCOMPARE(todo->summary(), summary);
0129 }
0130 
0131 void KoEventPopupMenuTest::createNoteFromEvent()
0132 {
0133     auto calendar = Akonadi::CollectionCalendar::Ptr::create(Akonadi::Collection());
0134     KOEventPopupMenu menu;
0135 
0136     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
0137     Akonadi::Item item;
0138     item.setMimeType(KCalendarCore::Event::eventMimeType());
0139     item.setPayload<KCalendarCore::Event::Ptr>(event);
0140 
0141     QDateTime start;
0142     QDateTime end;
0143     QString summary(QStringLiteral("A test"));
0144     QString description(QStringLiteral("A long description"));
0145     start = QDateTime::fromSecsSinceEpoch(1402593346);
0146     end = QDateTime::fromSecsSinceEpoch(1403593346);
0147     event->setDtStart(start);
0148     event->setDtEnd(end);
0149     event->setSummary(summary);
0150     event->setDescription(description, true);
0151 
0152     menu.showIncidencePopup(calendar, item, QDate());
0153     auto createnoteforevent = menu.findChild<QAction *>(QStringLiteral("createnoteforevent"));
0154     auto noteedit = menu.findChild<CalendarSupport::NoteEditDialog *>();
0155     QVERIFY(!noteedit);
0156     createnoteforevent->trigger();
0157     noteedit = menu.findChild<CalendarSupport::NoteEditDialog *>();
0158     QVERIFY(noteedit);
0159     Akonadi::NoteUtils::NoteMessageWrapper note(noteedit->note());
0160     QCOMPARE(note.title(), summary);
0161     QCOMPARE(note.text(), description);
0162     QCOMPARE(note.textFormat(), Qt::RichText);
0163     QCOMPARE(note.attachments().count(), 1);
0164     QCOMPARE(note.attachments().at(0).mimetype(), KCalendarCore::Event::eventMimeType());
0165     QCOMPARE(note.attachments().at(0).url(), (QUrl)item.url());
0166     QCOMPARE(note.attachments().at(0).data(), QByteArray());
0167 }
0168 
0169 void KoEventPopupMenuTest::createNoteFromTodo()
0170 {
0171     auto calendar = Akonadi::CollectionCalendar::Ptr::create(Akonadi::Collection());
0172     KOEventPopupMenu menu;
0173 
0174     KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
0175     Akonadi::Item item;
0176     item.setMimeType(KCalendarCore::Todo::todoMimeType());
0177     item.setPayload<KCalendarCore::Todo::Ptr>(todo);
0178 
0179     QDateTime start;
0180     QDateTime end;
0181     QString summary(QStringLiteral("a test"));
0182     QString description(QStringLiteral("A long description"));
0183     start = QDateTime::fromSecsSinceEpoch(1402593346);
0184     end = QDateTime::fromSecsSinceEpoch(1403593346);
0185     todo->setDtStart(start);
0186     todo->setDtDue(end);
0187     todo->setSummary(summary);
0188     todo->setDescription(description);
0189 
0190     menu.showIncidencePopup(calendar, item, QDate());
0191     auto createnotefortodo = menu.findChild<QAction *>(QStringLiteral("createnotefortodo"));
0192 
0193     auto noteedit = menu.findChild<CalendarSupport::NoteEditDialog *>();
0194     QVERIFY(!noteedit);
0195     createnotefortodo->trigger();
0196     noteedit = menu.findChild<CalendarSupport::NoteEditDialog *>();
0197     QVERIFY(noteedit);
0198     Akonadi::NoteUtils::NoteMessageWrapper note(noteedit->note());
0199     QCOMPARE(note.title(), summary);
0200     QCOMPARE(note.text(), description);
0201     QCOMPARE(note.attachments().count(), 1);
0202     QCOMPARE(note.attachments().at(0).mimetype(), KCalendarCore::Todo::todoMimeType());
0203     QCOMPARE(note.attachments().at(0).url(), (QUrl)item.url());
0204     QCOMPARE(note.attachments().at(0).data(), QByteArray());
0205 }
0206 
0207 void KoEventPopupMenuTest::defaultMenuEventVisible()
0208 {
0209     auto calendar = Akonadi::CollectionCalendar::Ptr::create(Akonadi::Collection());
0210     KOEventPopupMenu menu;
0211 
0212     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
0213     Akonadi::Item item;
0214     item.setMimeType(KCalendarCore::Event::eventMimeType());
0215     item.setPayload<KCalendarCore::Event::Ptr>(event);
0216 
0217     menu.showIncidencePopup(calendar, item, QDate());
0218     auto createevent = menu.findChild<QAction *>(QStringLiteral("createevent"));
0219     auto createnoteforevent = menu.findChild<QAction *>(QStringLiteral("createnoteforevent"));
0220     auto createtodo = menu.findChild<QAction *>(QStringLiteral("createtodo"));
0221     auto createnotefortodo = menu.findChild<QAction *>(QStringLiteral("createnotefortodo"));
0222     QVERIFY(!createevent->isVisible());
0223     QVERIFY(createnoteforevent->isVisible());
0224     QVERIFY(createtodo->isVisible());
0225     QVERIFY(!createnotefortodo->isVisible());
0226 }
0227 
0228 void KoEventPopupMenuTest::defaultMenuTodoVisible()
0229 {
0230     auto calendar = Akonadi::CollectionCalendar::Ptr::create(Akonadi::Collection());
0231     KOEventPopupMenu menu;
0232 
0233     KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
0234 
0235     Akonadi::Item item;
0236     item.setMimeType(KCalendarCore::Todo::todoMimeType());
0237     item.setPayload<KCalendarCore::Todo::Ptr>(todo);
0238 
0239     menu.showIncidencePopup(calendar, item, QDate());
0240     auto createevent = menu.findChild<QAction *>(QStringLiteral("createevent"));
0241     auto createnoteforevent = menu.findChild<QAction *>(QStringLiteral("createnoteforevent"));
0242     auto createtodo = menu.findChild<QAction *>(QStringLiteral("createtodo"));
0243     auto createnotefortodo = menu.findChild<QAction *>(QStringLiteral("createnotefortodo"));
0244     QVERIFY(createevent->isVisible());
0245     QVERIFY(!createnoteforevent->isVisible());
0246     QVERIFY(!createtodo->isVisible());
0247     QVERIFY(createnotefortodo->isVisible());
0248 }
0249 
0250 QTEST_MAIN(KoEventPopupMenuTest)
0251 
0252 #include "moc_koeventpopupmenutest.cpp"