1 /*
2   SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3 
4   SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "noteedittest.h"
8 #include "../createnoteplugin/noteedit.h"
9 #include "globalsettings_messageviewer.h"
10 #include <Akonadi/Collection>
11 #include <Akonadi/CollectionComboBox>
12 #include <Akonadi/EntityTreeModel>
13 #include <KMime/KMimeMessage>
14 
15 #include <QLineEdit>
16 #include <QPushButton>
17 #include <QShortcut>
18 #include <QSignalSpy>
19 #include <QStandardItemModel>
20 #include <QTest>
21 
22 namespace MessageViewer
23 {
24 extern MESSAGEVIEWER_EXPORT QAbstractItemModel *_k_noteEditStubModel;
25 }
26 
NoteEditTest()27 NoteEditTest::NoteEditTest()
28 {
29     qRegisterMetaType<Akonadi::Collection>();
30     qRegisterMetaType<KMime::Message::Ptr>();
31     QStandardPaths::setTestModeEnabled(true);
32 
33     auto model = new QStandardItemModel;
34     for (int id = 42; id < 51; ++id) {
35         Akonadi::Collection collection(id);
36         collection.setRights(Akonadi::Collection::AllRights);
37         collection.setName(QString::number(id));
38         collection.setContentMimeTypes(QStringList() << Akonadi::NoteUtils::noteMimeType());
39 
40         auto item = new QStandardItem(collection.name());
41         item->setData(QVariant::fromValue(collection), Akonadi::EntityTreeModel::CollectionRole);
42         item->setData(QVariant::fromValue(collection.id()), Akonadi::EntityTreeModel::CollectionIdRole);
43 
44         model->appendRow(item);
45     }
46     MessageViewer::_k_noteEditStubModel = model;
47 
48     // Fake a default-selected collection for shouldHaveDefaultValuesOnCreation test
49     MessageViewer::MessageViewerSettingsBase::self()->setLastNoteSelectedFolder(43);
50 }
51 
shouldHaveDefaultValuesOnCreation()52 void NoteEditTest::shouldHaveDefaultValuesOnCreation()
53 {
54     MessageViewer::NoteEdit edit;
55 
56     QVERIFY(edit.collection().isValid());
57     QVERIFY(!edit.message());
58     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
59     auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
60     QVERIFY(save);
61     QCOMPARE(save->isEnabled(), false);
62     QVERIFY(noteedit);
63     QCOMPARE(noteedit->text(), QString());
64 }
65 
shouldEmitCollectionChanged()66 void NoteEditTest::shouldEmitCollectionChanged()
67 {
68     MessageViewer::NoteEdit edit;
69     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::collectionChanged);
70     edit.setCollection(Akonadi::Collection(42));
71     QCOMPARE(spy.count(), 1);
72     QCOMPARE(spy.at(0).at(0).value<Akonadi::Collection>(), Akonadi::Collection(42));
73 }
74 
shouldEmitMessageChanged()75 void NoteEditTest::shouldEmitMessageChanged()
76 {
77     MessageViewer::NoteEdit edit;
78     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::messageChanged);
79     KMime::Message::Ptr msg(new KMime::Message);
80     edit.setMessage(msg);
81     QCOMPARE(spy.count(), 1);
82     QCOMPARE(spy.at(0).at(0).value<KMime::Message::Ptr>(), msg);
83 }
84 
shouldNotEmitWhenCollectionIsNotChanged()85 void NoteEditTest::shouldNotEmitWhenCollectionIsNotChanged()
86 {
87     MessageViewer::NoteEdit edit;
88     edit.setCollection(Akonadi::Collection(42));
89     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::collectionChanged);
90     edit.setCollection(Akonadi::Collection(42));
91     QCOMPARE(spy.count(), 0);
92 }
93 
shouldNotEmitWhenMessageIsNotChanged()94 void NoteEditTest::shouldNotEmitWhenMessageIsNotChanged()
95 {
96     MessageViewer::NoteEdit edit;
97     KMime::Message::Ptr msg(new KMime::Message);
98     edit.setMessage(msg);
99     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::messageChanged);
100     edit.setMessage(msg);
101     QCOMPARE(spy.count(), 0);
102 }
103 
shouldHaveSameValueAfterSet()104 void NoteEditTest::shouldHaveSameValueAfterSet()
105 {
106     MessageViewer::NoteEdit edit;
107     KMime::Message::Ptr msg(new KMime::Message);
108     edit.setCollection(Akonadi::Collection(42));
109     edit.setMessage(msg);
110     QCOMPARE(edit.collection(), Akonadi::Collection(42));
111     QCOMPARE(edit.message(), msg);
112 }
113 
shouldHaveASubject()114 void NoteEditTest::shouldHaveASubject()
115 {
116     MessageViewer::NoteEdit edit;
117     KMime::Message::Ptr msg(new KMime::Message);
118 
119     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
120     QVERIFY(noteedit);
121     QCOMPARE(noteedit->text(), QString());
122 
123     QString subject = QStringLiteral("Test Note");
124     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
125     edit.setMessage(msg);
126 
127     QCOMPARE(noteedit->text(), subject);
128 }
129 
shouldEmptySubjectWhenMessageIsNull()130 void NoteEditTest::shouldEmptySubjectWhenMessageIsNull()
131 {
132     MessageViewer::NoteEdit edit;
133     KMime::Message::Ptr msg(new KMime::Message);
134     QString subject = QStringLiteral("Test Note");
135     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
136     edit.setMessage(msg);
137     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
138     edit.setMessage(KMime::Message::Ptr());
139     QCOMPARE(noteedit->text(), QString());
140 }
141 
shouldEmptySubjectWhenMessageHasNotSubject()142 void NoteEditTest::shouldEmptySubjectWhenMessageHasNotSubject()
143 {
144     MessageViewer::NoteEdit edit;
145     KMime::Message::Ptr msg(new KMime::Message);
146     QString subject = QStringLiteral("Test Note");
147     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
148     edit.setMessage(msg);
149     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
150     KMime::Message::Ptr msgSubjectEmpty(new KMime::Message);
151     edit.setMessage(msgSubjectEmpty);
152     QCOMPARE(noteedit->text(), QString());
153 }
154 
shouldSelectLineWhenPutMessage()155 void NoteEditTest::shouldSelectLineWhenPutMessage()
156 {
157     MessageViewer::NoteEdit edit;
158     KMime::Message::Ptr msg(new KMime::Message);
159     QString subject = QStringLiteral("Test Note");
160     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
161     edit.setMessage(msg);
162     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
163     QVERIFY(noteedit->hasSelectedText());
164     const QString selectedText = noteedit->selectedText();
165     QCOMPARE(selectedText, subject);
166 }
167 
shouldEmitCollectionChangedWhenChangeComboboxItem()168 void NoteEditTest::shouldEmitCollectionChangedWhenChangeComboboxItem()
169 {
170     MessageViewer::NoteEdit edit;
171     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
172     QVERIFY(akonadicombobox);
173     QVERIFY(akonadicombobox->currentCollection().isValid());
174 }
175 
shouldEmitNotEmitNoteWhenTextIsEmpty()176 void NoteEditTest::shouldEmitNotEmitNoteWhenTextIsEmpty()
177 {
178     MessageViewer::NoteEdit edit;
179     KMime::Message::Ptr msg(new KMime::Message);
180     edit.setMessage(msg);
181     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
182     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote);
183     QTest::keyClick(noteedit, Qt::Key_Enter);
184     QCOMPARE(spy.count(), 0);
185 }
186 
shouldEmitNotEmitNoteWhenTextTrimmedIsEmpty()187 void NoteEditTest::shouldEmitNotEmitNoteWhenTextTrimmedIsEmpty()
188 {
189     MessageViewer::NoteEdit edit;
190     KMime::Message::Ptr msg(new KMime::Message);
191     edit.setMessage(msg);
192     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
193     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote);
194     noteedit->setText(QStringLiteral("      "));
195     QTest::keyClick(noteedit, Qt::Key_Enter);
196     QCOMPARE(spy.count(), 0);
197 
198     noteedit->setText(QStringLiteral("      F"));
199     QTest::keyClick(noteedit, Qt::Key_Enter);
200     QCOMPARE(spy.count(), 1);
201 }
202 
shouldEmitNoteWhenPressEnter()203 void NoteEditTest::shouldEmitNoteWhenPressEnter()
204 {
205     MessageViewer::NoteEdit edit;
206     KMime::Message::Ptr msg(new KMime::Message);
207     QString subject = QStringLiteral("Test Note");
208     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
209     edit.setMessage(msg);
210     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
211     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote);
212     QTest::keyClick(noteedit, Qt::Key_Enter);
213     QCOMPARE(spy.count(), 1);
214 }
215 
shouldNoteHasCorrectSubject()216 void NoteEditTest::shouldNoteHasCorrectSubject()
217 {
218     MessageViewer::NoteEdit edit;
219     KMime::Message::Ptr msg(new KMime::Message);
220     QString subject = QStringLiteral("Test Note");
221     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
222     edit.setMessage(msg);
223     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
224     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote);
225     QTest::keyClick(noteedit, Qt::Key_Enter);
226     QCOMPARE(spy.count(), 1);
227     auto notePtr = spy.at(0).at(0).value<KMime::Message::Ptr>();
228     QVERIFY((bool)notePtr);
229     Akonadi::NoteUtils::NoteMessageWrapper note(notePtr);
230     QCOMPARE(note.title(), subject);
231 }
232 
shouldClearAllWhenCloseWidget()233 void NoteEditTest::shouldClearAllWhenCloseWidget()
234 {
235     MessageViewer::NoteEdit edit;
236     KMime::Message::Ptr msg(new KMime::Message);
237     QString subject = QStringLiteral("Test Note");
238     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
239     edit.setMessage(msg);
240     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
241     edit.slotCloseWidget();
242     QCOMPARE(noteedit->text(), QString());
243     QVERIFY(!edit.message());
244 }
245 
shouldEmitCollectionChangedWhenCurrentCollectionWasChanged()246 void NoteEditTest::shouldEmitCollectionChangedWhenCurrentCollectionWasChanged()
247 {
248     MessageViewer::NoteEdit edit;
249     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
250     akonadicombobox->setCurrentIndex(0);
251     QCOMPARE(akonadicombobox->currentIndex(), 0);
252     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::collectionChanged);
253     akonadicombobox->setCurrentIndex(3);
254     QCOMPARE(akonadicombobox->currentIndex(), 3);
255     QCOMPARE(spy.count(), 1);
256 }
257 
shouldEmitCorrectCollection()258 void NoteEditTest::shouldEmitCorrectCollection()
259 {
260     MessageViewer::NoteEdit edit;
261     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
262     KMime::Message::Ptr msg(new KMime::Message);
263     QString subject = QStringLiteral("Test Note");
264     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
265     edit.setMessage(msg);
266     akonadicombobox->setCurrentIndex(3);
267     Akonadi::Collection col = akonadicombobox->currentCollection();
268     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
269     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote);
270     QTest::keyClick(noteedit, Qt::Key_Enter);
271     QCOMPARE(spy.count(), 1);
272     QCOMPARE(spy.at(0).at(1).value<Akonadi::Collection>(), col);
273 }
274 
shouldHideWidgetWhenClickOnCloseButton()275 void NoteEditTest::shouldHideWidgetWhenClickOnCloseButton()
276 {
277     MessageViewer::NoteEdit edit;
278     edit.show();
279     QVERIFY(QTest::qWaitForWindowExposed(&edit));
280     QVERIFY(edit.isVisible());
281     auto close = edit.findChild<QPushButton *>(QStringLiteral("close-button"));
282     QTest::mouseClick(close, Qt::LeftButton);
283     QCOMPARE(edit.isVisible(), false);
284 }
285 
shouldHideWidgetWhenPressEscape()286 void NoteEditTest::shouldHideWidgetWhenPressEscape()
287 {
288     MessageViewer::NoteEdit edit;
289     edit.show();
290     // make sure the window is active so we can test for focus
291     qApp->setActiveWindow(&edit);
292     QVERIFY(QTest::qWaitForWindowExposed(&edit));
293     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
294     noteedit->setFocus();
295     QVERIFY(noteedit->hasFocus());
296     QTest::keyPress(&edit, Qt::Key_Escape);
297     QCOMPARE(edit.isVisible(), false);
298 }
299 
shouldHideWidgetWhenSaveClicked()300 void NoteEditTest::shouldHideWidgetWhenSaveClicked()
301 {
302     MessageViewer::NoteEdit edit;
303     edit.show();
304     QVERIFY(QTest::qWaitForWindowExposed(&edit));
305 
306     KMime::Message::Ptr msg(new KMime::Message);
307     msg->subject(true)->fromUnicodeString(QStringLiteral("Test Note"), "us-ascii");
308     edit.setMessage(msg);
309     auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
310     QTest::mouseClick(save, Qt::LeftButton);
311     QCOMPARE(edit.isVisible(), false);
312 }
313 
shouldSaveCollectionSettings()314 void NoteEditTest::shouldSaveCollectionSettings()
315 {
316     MessageViewer::NoteEdit edit;
317     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
318     akonadicombobox->setCurrentIndex(3);
319     const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id();
320     auto close = edit.findChild<QPushButton *>(QStringLiteral("close-button"));
321     QTest::mouseClick(close, Qt::LeftButton);
322     QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastNoteSelectedFolder(), id);
323 }
324 
shouldSaveCollectionSettingsWhenCloseWidget()325 void NoteEditTest::shouldSaveCollectionSettingsWhenCloseWidget()
326 {
327     MessageViewer::NoteEdit edit;
328     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
329     akonadicombobox->setCurrentIndex(4);
330     const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id();
331     edit.writeConfig();
332     QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastNoteSelectedFolder(), id);
333 }
334 
shouldSaveCollectionSettingsWhenDeleteWidget()335 void NoteEditTest::shouldSaveCollectionSettingsWhenDeleteWidget()
336 {
337     auto edit = new MessageViewer::NoteEdit;
338     auto akonadicombobox = edit->findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
339     akonadicombobox->setCurrentIndex(4);
340     const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id();
341     delete edit;
342     QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastNoteSelectedFolder(), id);
343 }
344 
shouldSetFocusWhenWeCallNoteEdit()345 void NoteEditTest::shouldSetFocusWhenWeCallNoteEdit()
346 {
347     MessageViewer::NoteEdit edit;
348     edit.show();
349     // make sure the window is active so we can test for focus
350     qApp->setActiveWindow(&edit);
351     QVERIFY(QTest::qWaitForWindowExposed(&edit));
352     KMime::Message::Ptr msg(new KMime::Message);
353     QString subject = QStringLiteral("Test Note");
354     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
355     edit.setMessage(msg);
356     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
357     QCOMPARE(noteedit->hasFocus(), true);
358     edit.setFocus();
359     QCOMPARE(noteedit->hasFocus(), false);
360     edit.showNoteEdit();
361     QCOMPARE(noteedit->hasFocus(), true);
362 }
363 
shouldNotEmitNoteWhenMessageIsNull()364 void NoteEditTest::shouldNotEmitNoteWhenMessageIsNull()
365 {
366     MessageViewer::NoteEdit edit;
367     QString subject = QStringLiteral("Test Note");
368     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
369     noteedit->setText(subject);
370     QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote);
371     QTest::keyClick(noteedit, Qt::Key_Enter);
372     QCOMPARE(spy.count(), 0);
373 }
374 
shouldClearLineAfterEmitNewNote()375 void NoteEditTest::shouldClearLineAfterEmitNewNote()
376 {
377     MessageViewer::NoteEdit edit;
378     KMime::Message::Ptr msg(new KMime::Message);
379     QString subject = QStringLiteral("Test Note");
380     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
381     edit.setMessage(msg);
382     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
383     QTest::keyClick(noteedit, Qt::Key_Enter);
384     QCOMPARE(noteedit->text(), QString());
385 }
386 
shouldHaveLineEditFocus()387 void NoteEditTest::shouldHaveLineEditFocus()
388 {
389     MessageViewer::NoteEdit edit;
390     edit.show();
391     // make sure the window is active so we can test for focus
392     qApp->setActiveWindow(&edit);
393     QVERIFY(QTest::qWaitForWindowExposed(&edit));
394     KMime::Message::Ptr msg(new KMime::Message);
395     QString subject = QStringLiteral("Test Note");
396     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
397     edit.setMessage(msg);
398     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
399     QCOMPARE(noteedit->hasFocus(), true);
400 }
401 
shouldShouldEnabledSaveEditorButton()402 void NoteEditTest::shouldShouldEnabledSaveEditorButton()
403 {
404     MessageViewer::NoteEdit edit;
405     KMime::Message::Ptr msg(new KMime::Message);
406     msg->subject(true)->fromUnicodeString(QStringLiteral("Test note"), "us-ascii");
407     edit.setMessage(msg);
408 
409     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
410     auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
411     QCOMPARE(save->isEnabled(), true);
412     noteedit->clear();
413 
414     QCOMPARE(save->isEnabled(), false);
415     QVERIFY(noteedit->text().isEmpty());
416 
417     noteedit->setText(QStringLiteral("  "));
418     QCOMPARE(save->isEnabled(), false);
419 }
420 
421 QTEST_MAIN(NoteEditTest)
422