1 /*
2   SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3 
4   SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "koeventpopupmenutest.h"
8 #include "koeventpopupmenu.h"
9 
10 #include <Akonadi/Notes/NoteUtils>
11 
12 #include <CalendarSupport/NoteEditDialog>
13 
14 #include <IncidenceEditor/IncidenceDialog>
15 #include <IncidenceEditor/IncidenceEditor-Ng>
16 
17 #include <QStandardPaths>
18 
19 #include <QTest>
KoEventPopupMenuTest(QObject * parent)20 KoEventPopupMenuTest::KoEventPopupMenuTest(QObject *parent)
21     : QObject(parent)
22 {
23     QStandardPaths::setTestModeEnabled(true);
24 }
25 
createEventFromEvent()26 void KoEventPopupMenuTest::createEventFromEvent()
27 {
28     Akonadi::ETMCalendar::Ptr calendar(new Akonadi::ETMCalendar());
29     KOEventPopupMenu menu(nullptr);
30 
31     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
32     Akonadi::Item item;
33     item.setMimeType(KCalendarCore::Event::eventMimeType());
34     item.setPayload<KCalendarCore::Event::Ptr>(event);
35 
36     menu.setCalendar(calendar);
37     menu.showIncidencePopup(item, QDate());
38     auto createevent = menu.findChild<QAction *>(QStringLiteral("createevent"));
39     createevent->trigger();
40 
41     auto dlg = menu.findChild<IncidenceEditorNG::IncidenceDialog *>(QStringLiteral("incidencedialog"));
42     QVERIFY(!dlg);
43 }
44 
createTodoFromTodo()45 void KoEventPopupMenuTest::createTodoFromTodo()
46 {
47     Akonadi::ETMCalendar::Ptr calendar(new Akonadi::ETMCalendar());
48     KOEventPopupMenu menu(nullptr);
49 
50     KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
51     Akonadi::Item item;
52     item.setMimeType(KCalendarCore::Todo::todoMimeType());
53     item.setPayload<KCalendarCore::Todo::Ptr>(todo);
54 
55     menu.setCalendar(calendar);
56     menu.showIncidencePopup(item, QDate());
57     auto createtodo = menu.findChild<QAction *>(QStringLiteral("createtodo"));
58     createtodo->trigger();
59 
60     auto dlg = menu.findChild<IncidenceEditorNG::IncidenceDialog *>(QStringLiteral("incidencedialog"));
61     QVERIFY(!dlg);
62 }
63 
createEventFromTodo()64 void KoEventPopupMenuTest::createEventFromTodo()
65 {
66     Akonadi::ETMCalendar::Ptr calendar(new Akonadi::ETMCalendar());
67     KOEventPopupMenu menu(nullptr);
68 
69     KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
70     Akonadi::Item item;
71     item.setMimeType(KCalendarCore::Todo::todoMimeType());
72     item.setPayload<KCalendarCore::Todo::Ptr>(todo);
73 
74     QDateTime start;
75     QDateTime end;
76     QString summary(QStringLiteral("a test"));
77     start = QDateTime::fromSecsSinceEpoch(1402593346);
78     end = QDateTime::fromSecsSinceEpoch(1403593346);
79     todo->setDtStart(start);
80     todo->setDtDue(end);
81     todo->setSummary(summary);
82 
83     menu.setCalendar(calendar);
84     menu.showIncidencePopup(item, QDate());
85     auto createevent = menu.findChild<QAction *>(QStringLiteral("createevent"));
86     createevent->trigger();
87     auto dlg = menu.findChild<IncidenceEditorNG::IncidenceDialog *>();
88     QVERIFY(dlg);
89     auto editor = menu.findChild<IncidenceEditorNG::IncidenceEditor *>();
90     QVERIFY(editor);
91     KCalendarCore::Event::Ptr event(editor->incidence<KCalendarCore::Event>());
92     QVERIFY(event->uid() != todo->uid());
93     QCOMPARE(event->dtStart(), start);
94     QCOMPARE(event->dtEnd(), end);
95     QCOMPARE(event->allDay(), false);
96     QCOMPARE(event->summary(), summary);
97 }
98 
createTodoFromEvent()99 void KoEventPopupMenuTest::createTodoFromEvent()
100 {
101     Akonadi::ETMCalendar::Ptr calendar(new Akonadi::ETMCalendar());
102     KOEventPopupMenu menu(nullptr);
103 
104     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
105     Akonadi::Item item;
106     item.setMimeType(KCalendarCore::Event::eventMimeType());
107     item.setPayload<KCalendarCore::Event::Ptr>(event);
108 
109     QDateTime start;
110     QDateTime end;
111     QString summary(QStringLiteral("a test"));
112     start = QDateTime::fromSecsSinceEpoch(1402593346);
113     end = QDateTime::fromSecsSinceEpoch(1403593346);
114     event->setDtStart(start);
115     event->setDtEnd(end);
116     event->setSummary(summary);
117 
118     menu.setCalendar(calendar);
119     menu.showIncidencePopup(item, QDate());
120     auto createtodo = menu.findChild<QAction *>(QStringLiteral("createtodo"));
121     createtodo->trigger();
122     auto dlg = menu.findChild<IncidenceEditorNG::IncidenceDialog *>();
123     QVERIFY(dlg);
124     auto editor = menu.findChild<IncidenceEditorNG::IncidenceEditor *>();
125     QVERIFY(editor);
126     KCalendarCore::Todo::Ptr todo(editor->incidence<KCalendarCore::Todo>());
127     QVERIFY(todo->uid() != event->uid());
128     QCOMPARE(todo->dtStart(), start);
129     QCOMPARE(todo->dtDue(), end);
130     QCOMPARE(todo->allDay(), false);
131     QCOMPARE(todo->summary(), summary);
132 }
133 
createNoteFromEvent()134 void KoEventPopupMenuTest::createNoteFromEvent()
135 {
136     Akonadi::ETMCalendar::Ptr calendar(new Akonadi::ETMCalendar());
137     KOEventPopupMenu menu(nullptr);
138 
139     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
140     Akonadi::Item item;
141     item.setMimeType(KCalendarCore::Event::eventMimeType());
142     item.setPayload<KCalendarCore::Event::Ptr>(event);
143 
144     QDateTime start;
145     QDateTime end;
146     QString summary(QStringLiteral("A test"));
147     QString description(QStringLiteral("A long description"));
148     start = QDateTime::fromSecsSinceEpoch(1402593346);
149     end = QDateTime::fromSecsSinceEpoch(1403593346);
150     event->setDtStart(start);
151     event->setDtEnd(end);
152     event->setSummary(summary);
153     event->setDescription(description, true);
154 
155     menu.setCalendar(calendar);
156     menu.showIncidencePopup(item, QDate());
157     auto createnoteforevent = menu.findChild<QAction *>(QStringLiteral("createnoteforevent"));
158     auto noteedit = menu.findChild<CalendarSupport::NoteEditDialog *>();
159     QVERIFY(!noteedit);
160     createnoteforevent->trigger();
161     noteedit = menu.findChild<CalendarSupport::NoteEditDialog *>();
162     QVERIFY(noteedit);
163     Akonadi::NoteUtils::NoteMessageWrapper note(noteedit->note());
164     QCOMPARE(note.title(), summary);
165     QCOMPARE(note.text(), description);
166     QCOMPARE(note.textFormat(), Qt::RichText);
167     QCOMPARE(note.attachments().count(), 1);
168     QCOMPARE(note.attachments().at(0).mimetype(), KCalendarCore::Event::eventMimeType());
169     QCOMPARE(note.attachments().at(0).url(), (QUrl)item.url());
170     QCOMPARE(note.attachments().at(0).data(), QByteArray());
171 }
172 
createNoteFromTodo()173 void KoEventPopupMenuTest::createNoteFromTodo()
174 {
175     Akonadi::ETMCalendar::Ptr calendar(new Akonadi::ETMCalendar());
176     KOEventPopupMenu menu(nullptr);
177 
178     KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
179     Akonadi::Item item;
180     item.setMimeType(KCalendarCore::Todo::todoMimeType());
181     item.setPayload<KCalendarCore::Todo::Ptr>(todo);
182 
183     QDateTime start;
184     QDateTime end;
185     QString summary(QStringLiteral("a test"));
186     QString description(QStringLiteral("A long description"));
187     start = QDateTime::fromSecsSinceEpoch(1402593346);
188     end = QDateTime::fromSecsSinceEpoch(1403593346);
189     todo->setDtStart(start);
190     todo->setDtDue(end);
191     todo->setSummary(summary);
192     todo->setDescription(description);
193 
194     menu.setCalendar(calendar);
195     menu.showIncidencePopup(item, QDate());
196     auto createnotefortodo = menu.findChild<QAction *>(QStringLiteral("createnotefortodo"));
197 
198     auto noteedit = menu.findChild<CalendarSupport::NoteEditDialog *>();
199     QVERIFY(!noteedit);
200     createnotefortodo->trigger();
201     noteedit = menu.findChild<CalendarSupport::NoteEditDialog *>();
202     QVERIFY(noteedit);
203     Akonadi::NoteUtils::NoteMessageWrapper note(noteedit->note());
204     QCOMPARE(note.title(), summary);
205     QCOMPARE(note.text(), description);
206     QCOMPARE(note.attachments().count(), 1);
207     QCOMPARE(note.attachments().at(0).mimetype(), KCalendarCore::Todo::todoMimeType());
208     QCOMPARE(note.attachments().at(0).url(), (QUrl)item.url());
209     QCOMPARE(note.attachments().at(0).data(), QByteArray());
210 }
211 
defaultMenuEventVisible()212 void KoEventPopupMenuTest::defaultMenuEventVisible()
213 {
214     Akonadi::ETMCalendar::Ptr calendar(new Akonadi::ETMCalendar());
215     KOEventPopupMenu menu(nullptr);
216 
217     KCalendarCore::Event::Ptr event(new KCalendarCore::Event());
218     Akonadi::Item item;
219     item.setMimeType(KCalendarCore::Event::eventMimeType());
220     item.setPayload<KCalendarCore::Event::Ptr>(event);
221 
222     menu.setCalendar(calendar);
223     menu.showIncidencePopup(item, QDate());
224     auto createevent = menu.findChild<QAction *>(QStringLiteral("createevent"));
225     auto createnoteforevent = menu.findChild<QAction *>(QStringLiteral("createnoteforevent"));
226     auto createtodo = menu.findChild<QAction *>(QStringLiteral("createtodo"));
227     auto createnotefortodo = menu.findChild<QAction *>(QStringLiteral("createnotefortodo"));
228     QVERIFY(!createevent->isVisible());
229     QVERIFY(createnoteforevent->isVisible());
230     QVERIFY(createtodo->isVisible());
231     QVERIFY(!createnotefortodo->isVisible());
232 }
233 
defaultMenuTodoVisible()234 void KoEventPopupMenuTest::defaultMenuTodoVisible()
235 {
236     Akonadi::ETMCalendar::Ptr calendar(new Akonadi::ETMCalendar());
237     KOEventPopupMenu menu(nullptr);
238 
239     KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo());
240 
241     Akonadi::Item item;
242     item.setMimeType(KCalendarCore::Todo::todoMimeType());
243     item.setPayload<KCalendarCore::Todo::Ptr>(todo);
244 
245     menu.setCalendar(calendar);
246     menu.showIncidencePopup(item, QDate());
247     auto createevent = menu.findChild<QAction *>(QStringLiteral("createevent"));
248     auto createnoteforevent = menu.findChild<QAction *>(QStringLiteral("createnoteforevent"));
249     auto createtodo = menu.findChild<QAction *>(QStringLiteral("createtodo"));
250     auto createnotefortodo = menu.findChild<QAction *>(QStringLiteral("createnotefortodo"));
251     QVERIFY(createevent->isVisible());
252     QVERIFY(!createnoteforevent->isVisible());
253     QVERIFY(!createtodo->isVisible());
254     QVERIFY(createnotefortodo->isVisible());
255 }
256 
257 QTEST_MAIN(KoEventPopupMenuTest)
258