File indexing completed on 2025-01-05 04:49:44
0001 /* 0002 SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-only 0005 */ 0006 0007 #include "noteedittest.h" 0008 #include "../createnoteplugin/noteedit.h" 0009 #include <Akonadi/Collection> 0010 #include <Akonadi/CollectionComboBox> 0011 #include <Akonadi/EntityTreeModel> 0012 #include <KMime/KMimeMessage> 0013 #include <messageviewer/globalsettings_messageviewer.h> 0014 0015 #include <QLineEdit> 0016 #include <QPushButton> 0017 #include <QShortcut> 0018 #include <QSignalSpy> 0019 #include <QStandardItemModel> 0020 #include <QTest> 0021 0022 namespace MessageViewer 0023 { 0024 extern MESSAGEVIEWER_EXPORT QAbstractItemModel *_k_noteEditStubModel; 0025 } 0026 0027 NoteEditTest::NoteEditTest() 0028 { 0029 qRegisterMetaType<Akonadi::Collection>(); 0030 qRegisterMetaType<KMime::Message::Ptr>(); 0031 QStandardPaths::setTestModeEnabled(true); 0032 0033 auto model = new QStandardItemModel; 0034 for (int id = 42; id < 51; ++id) { 0035 Akonadi::Collection collection(id); 0036 collection.setRights(Akonadi::Collection::AllRights); 0037 collection.setName(QString::number(id)); 0038 collection.setContentMimeTypes(QStringList() << Akonadi::NoteUtils::noteMimeType()); 0039 0040 auto item = new QStandardItem(collection.name()); 0041 item->setData(QVariant::fromValue(collection), Akonadi::EntityTreeModel::CollectionRole); 0042 item->setData(QVariant::fromValue(collection.id()), Akonadi::EntityTreeModel::CollectionIdRole); 0043 0044 model->appendRow(item); 0045 } 0046 MessageViewer::_k_noteEditStubModel = model; 0047 0048 // Fake a default-selected collection for shouldHaveDefaultValuesOnCreation test 0049 MessageViewer::MessageViewerSettingsBase::self()->setLastNoteSelectedFolder(43); 0050 } 0051 0052 void NoteEditTest::shouldHaveDefaultValuesOnCreation() 0053 { 0054 MessageViewer::NoteEdit edit; 0055 0056 QVERIFY(edit.collection().isValid()); 0057 QVERIFY(!edit.message()); 0058 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0059 auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button")); 0060 QVERIFY(save); 0061 QCOMPARE(save->isEnabled(), false); 0062 QVERIFY(noteedit); 0063 QCOMPARE(noteedit->text(), QString()); 0064 } 0065 0066 void NoteEditTest::shouldEmitCollectionChanged() 0067 { 0068 MessageViewer::NoteEdit edit; 0069 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::collectionChanged); 0070 edit.setCollection(Akonadi::Collection(42)); 0071 QCOMPARE(spy.count(), 1); 0072 QCOMPARE(spy.at(0).at(0).value<Akonadi::Collection>(), Akonadi::Collection(42)); 0073 } 0074 0075 void NoteEditTest::shouldEmitMessageChanged() 0076 { 0077 MessageViewer::NoteEdit edit; 0078 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::messageChanged); 0079 KMime::Message::Ptr msg(new KMime::Message); 0080 edit.setMessage(msg); 0081 QCOMPARE(spy.count(), 1); 0082 QCOMPARE(spy.at(0).at(0).value<KMime::Message::Ptr>(), msg); 0083 } 0084 0085 void NoteEditTest::shouldNotEmitWhenCollectionIsNotChanged() 0086 { 0087 MessageViewer::NoteEdit edit; 0088 edit.setCollection(Akonadi::Collection(42)); 0089 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::collectionChanged); 0090 edit.setCollection(Akonadi::Collection(42)); 0091 QCOMPARE(spy.count(), 0); 0092 } 0093 0094 void NoteEditTest::shouldNotEmitWhenMessageIsNotChanged() 0095 { 0096 MessageViewer::NoteEdit edit; 0097 KMime::Message::Ptr msg(new KMime::Message); 0098 edit.setMessage(msg); 0099 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::messageChanged); 0100 edit.setMessage(msg); 0101 QCOMPARE(spy.count(), 0); 0102 } 0103 0104 void NoteEditTest::shouldHaveSameValueAfterSet() 0105 { 0106 MessageViewer::NoteEdit edit; 0107 KMime::Message::Ptr msg(new KMime::Message); 0108 edit.setCollection(Akonadi::Collection(42)); 0109 edit.setMessage(msg); 0110 QCOMPARE(edit.collection(), Akonadi::Collection(42)); 0111 QCOMPARE(edit.message(), msg); 0112 } 0113 0114 void NoteEditTest::shouldHaveASubject() 0115 { 0116 MessageViewer::NoteEdit edit; 0117 KMime::Message::Ptr msg(new KMime::Message); 0118 0119 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0120 QVERIFY(noteedit); 0121 QCOMPARE(noteedit->text(), QString()); 0122 0123 QString subject = QStringLiteral("Test Note"); 0124 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0125 edit.setMessage(msg); 0126 0127 QCOMPARE(noteedit->text(), subject); 0128 } 0129 0130 void NoteEditTest::shouldEmptySubjectWhenMessageIsNull() 0131 { 0132 MessageViewer::NoteEdit edit; 0133 KMime::Message::Ptr msg(new KMime::Message); 0134 QString subject = QStringLiteral("Test Note"); 0135 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0136 edit.setMessage(msg); 0137 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0138 edit.setMessage(KMime::Message::Ptr()); 0139 QCOMPARE(noteedit->text(), QString()); 0140 } 0141 0142 void NoteEditTest::shouldEmptySubjectWhenMessageHasNotSubject() 0143 { 0144 MessageViewer::NoteEdit edit; 0145 KMime::Message::Ptr msg(new KMime::Message); 0146 QString subject = QStringLiteral("Test Note"); 0147 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0148 edit.setMessage(msg); 0149 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0150 KMime::Message::Ptr msgSubjectEmpty(new KMime::Message); 0151 edit.setMessage(msgSubjectEmpty); 0152 QCOMPARE(noteedit->text(), QString()); 0153 } 0154 0155 void NoteEditTest::shouldSelectLineWhenPutMessage() 0156 { 0157 MessageViewer::NoteEdit edit; 0158 KMime::Message::Ptr msg(new KMime::Message); 0159 QString subject = QStringLiteral("Test Note"); 0160 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0161 edit.setMessage(msg); 0162 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0163 QVERIFY(noteedit->hasSelectedText()); 0164 const QString selectedText = noteedit->selectedText(); 0165 QCOMPARE(selectedText, subject); 0166 } 0167 0168 void NoteEditTest::shouldEmitCollectionChangedWhenChangeComboboxItem() 0169 { 0170 MessageViewer::NoteEdit edit; 0171 auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox")); 0172 QVERIFY(akonadicombobox); 0173 QVERIFY(akonadicombobox->currentCollection().isValid()); 0174 } 0175 0176 void NoteEditTest::shouldEmitNotEmitNoteWhenTextIsEmpty() 0177 { 0178 MessageViewer::NoteEdit edit; 0179 KMime::Message::Ptr msg(new KMime::Message); 0180 edit.setMessage(msg); 0181 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0182 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote); 0183 QTest::keyClick(noteedit, Qt::Key_Enter); 0184 QCOMPARE(spy.count(), 0); 0185 } 0186 0187 void NoteEditTest::shouldEmitNotEmitNoteWhenTextTrimmedIsEmpty() 0188 { 0189 MessageViewer::NoteEdit edit; 0190 KMime::Message::Ptr msg(new KMime::Message); 0191 edit.setMessage(msg); 0192 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0193 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote); 0194 noteedit->setText(QStringLiteral(" ")); 0195 QTest::keyClick(noteedit, Qt::Key_Enter); 0196 QCOMPARE(spy.count(), 0); 0197 0198 noteedit->setText(QStringLiteral(" F")); 0199 QTest::keyClick(noteedit, Qt::Key_Enter); 0200 QCOMPARE(spy.count(), 1); 0201 } 0202 0203 void NoteEditTest::shouldEmitNoteWhenPressEnter() 0204 { 0205 MessageViewer::NoteEdit edit; 0206 KMime::Message::Ptr msg(new KMime::Message); 0207 QString subject = QStringLiteral("Test Note"); 0208 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0209 edit.setMessage(msg); 0210 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0211 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote); 0212 QTest::keyClick(noteedit, Qt::Key_Enter); 0213 QCOMPARE(spy.count(), 1); 0214 } 0215 0216 void NoteEditTest::shouldNoteHasCorrectSubject() 0217 { 0218 MessageViewer::NoteEdit edit; 0219 KMime::Message::Ptr msg(new KMime::Message); 0220 QString subject = QStringLiteral("Test Note"); 0221 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0222 edit.setMessage(msg); 0223 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0224 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote); 0225 QTest::keyClick(noteedit, Qt::Key_Enter); 0226 QCOMPARE(spy.count(), 1); 0227 auto notePtr = spy.at(0).at(0).value<KMime::Message::Ptr>(); 0228 QVERIFY((bool)notePtr); 0229 Akonadi::NoteUtils::NoteMessageWrapper note(notePtr); 0230 QCOMPARE(note.title(), subject); 0231 } 0232 0233 void NoteEditTest::shouldClearAllWhenCloseWidget() 0234 { 0235 MessageViewer::NoteEdit edit; 0236 KMime::Message::Ptr msg(new KMime::Message); 0237 QString subject = QStringLiteral("Test Note"); 0238 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0239 edit.setMessage(msg); 0240 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0241 edit.slotCloseWidget(); 0242 QCOMPARE(noteedit->text(), QString()); 0243 QVERIFY(!edit.message()); 0244 } 0245 0246 void NoteEditTest::shouldEmitCollectionChangedWhenCurrentCollectionWasChanged() 0247 { 0248 MessageViewer::NoteEdit edit; 0249 auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox")); 0250 akonadicombobox->setCurrentIndex(0); 0251 QCOMPARE(akonadicombobox->currentIndex(), 0); 0252 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::collectionChanged); 0253 akonadicombobox->setCurrentIndex(3); 0254 QCOMPARE(akonadicombobox->currentIndex(), 3); 0255 QCOMPARE(spy.count(), 1); 0256 } 0257 0258 void NoteEditTest::shouldEmitCorrectCollection() 0259 { 0260 MessageViewer::NoteEdit edit; 0261 auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox")); 0262 KMime::Message::Ptr msg(new KMime::Message); 0263 QString subject = QStringLiteral("Test Note"); 0264 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0265 edit.setMessage(msg); 0266 akonadicombobox->setCurrentIndex(3); 0267 Akonadi::Collection col = akonadicombobox->currentCollection(); 0268 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0269 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote); 0270 QTest::keyClick(noteedit, Qt::Key_Enter); 0271 QCOMPARE(spy.count(), 1); 0272 QCOMPARE(spy.at(0).at(1).value<Akonadi::Collection>(), col); 0273 } 0274 0275 void NoteEditTest::shouldHideWidgetWhenClickOnCloseButton() 0276 { 0277 MessageViewer::NoteEdit edit; 0278 edit.show(); 0279 QVERIFY(QTest::qWaitForWindowExposed(&edit)); 0280 QVERIFY(edit.isVisible()); 0281 auto close = edit.findChild<QPushButton *>(QStringLiteral("close-button")); 0282 QTest::mouseClick(close, Qt::LeftButton); 0283 QCOMPARE(edit.isVisible(), false); 0284 } 0285 0286 void NoteEditTest::shouldHideWidgetWhenPressEscape() 0287 { 0288 MessageViewer::NoteEdit edit; 0289 edit.show(); 0290 // make sure the window is active so we can test for focus 0291 edit.activateWindow(); 0292 QVERIFY(QTest::qWaitForWindowExposed(&edit)); 0293 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0294 noteedit->setFocus(); 0295 QVERIFY(noteedit->hasFocus()); 0296 QTest::keyPress(&edit, Qt::Key_Escape); 0297 QCOMPARE(edit.isVisible(), false); 0298 } 0299 0300 void NoteEditTest::shouldHideWidgetWhenSaveClicked() 0301 { 0302 MessageViewer::NoteEdit edit; 0303 edit.show(); 0304 QVERIFY(QTest::qWaitForWindowExposed(&edit)); 0305 0306 KMime::Message::Ptr msg(new KMime::Message); 0307 msg->subject(true)->fromUnicodeString(QStringLiteral("Test Note"), "us-ascii"); 0308 edit.setMessage(msg); 0309 auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button")); 0310 QTest::mouseClick(save, Qt::LeftButton); 0311 QCOMPARE(edit.isVisible(), false); 0312 } 0313 0314 void NoteEditTest::shouldSaveCollectionSettings() 0315 { 0316 MessageViewer::NoteEdit edit; 0317 auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox")); 0318 akonadicombobox->setCurrentIndex(3); 0319 const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id(); 0320 auto close = edit.findChild<QPushButton *>(QStringLiteral("close-button")); 0321 QTest::mouseClick(close, Qt::LeftButton); 0322 QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastNoteSelectedFolder(), id); 0323 } 0324 0325 void NoteEditTest::shouldSaveCollectionSettingsWhenCloseWidget() 0326 { 0327 MessageViewer::NoteEdit edit; 0328 auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox")); 0329 akonadicombobox->setCurrentIndex(4); 0330 const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id(); 0331 edit.writeConfig(); 0332 QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastNoteSelectedFolder(), id); 0333 } 0334 0335 void NoteEditTest::shouldSaveCollectionSettingsWhenDeleteWidget() 0336 { 0337 auto edit = new MessageViewer::NoteEdit; 0338 auto akonadicombobox = edit->findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox")); 0339 akonadicombobox->setCurrentIndex(4); 0340 const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id(); 0341 delete edit; 0342 QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastNoteSelectedFolder(), id); 0343 } 0344 0345 void NoteEditTest::shouldSetFocusWhenWeCallNoteEdit() 0346 { 0347 MessageViewer::NoteEdit edit; 0348 edit.show(); 0349 // make sure the window is active so we can test for focus 0350 edit.activateWindow(); 0351 QVERIFY(QTest::qWaitForWindowExposed(&edit)); 0352 KMime::Message::Ptr msg(new KMime::Message); 0353 QString subject = QStringLiteral("Test Note"); 0354 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0355 edit.setMessage(msg); 0356 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0357 QCOMPARE(noteedit->hasFocus(), true); 0358 edit.setFocus(); 0359 QCOMPARE(noteedit->hasFocus(), false); 0360 edit.showNoteEdit(); 0361 QCOMPARE(noteedit->hasFocus(), true); 0362 } 0363 0364 void NoteEditTest::shouldNotEmitNoteWhenMessageIsNull() 0365 { 0366 MessageViewer::NoteEdit edit; 0367 QString subject = QStringLiteral("Test Note"); 0368 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0369 noteedit->setText(subject); 0370 QSignalSpy spy(&edit, &MessageViewer::NoteEdit::createNote); 0371 QTest::keyClick(noteedit, Qt::Key_Enter); 0372 QCOMPARE(spy.count(), 0); 0373 } 0374 0375 void NoteEditTest::shouldClearLineAfterEmitNewNote() 0376 { 0377 MessageViewer::NoteEdit edit; 0378 KMime::Message::Ptr msg(new KMime::Message); 0379 QString subject = QStringLiteral("Test Note"); 0380 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0381 edit.setMessage(msg); 0382 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0383 QTest::keyClick(noteedit, Qt::Key_Enter); 0384 QCOMPARE(noteedit->text(), QString()); 0385 } 0386 0387 void NoteEditTest::shouldHaveLineEditFocus() 0388 { 0389 MessageViewer::NoteEdit edit; 0390 edit.show(); 0391 // make sure the window is active so we can test for focus 0392 edit.activateWindow(); 0393 QVERIFY(QTest::qWaitForWindowExposed(&edit)); 0394 KMime::Message::Ptr msg(new KMime::Message); 0395 QString subject = QStringLiteral("Test Note"); 0396 msg->subject(true)->fromUnicodeString(subject, "us-ascii"); 0397 edit.setMessage(msg); 0398 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0399 QCOMPARE(noteedit->hasFocus(), true); 0400 } 0401 0402 void NoteEditTest::shouldShouldEnabledSaveEditorButton() 0403 { 0404 MessageViewer::NoteEdit edit; 0405 KMime::Message::Ptr msg(new KMime::Message); 0406 msg->subject(true)->fromUnicodeString(QStringLiteral("Test note"), "us-ascii"); 0407 edit.setMessage(msg); 0408 0409 auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit")); 0410 auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button")); 0411 QCOMPARE(save->isEnabled(), true); 0412 noteedit->clear(); 0413 0414 QCOMPARE(save->isEnabled(), false); 0415 QVERIFY(noteedit->text().isEmpty()); 0416 0417 noteedit->setText(QStringLiteral(" ")); 0418 QCOMPARE(save->isEnabled(), false); 0419 } 0420 0421 QTEST_MAIN(NoteEditTest) 0422 0423 #include "moc_noteedittest.cpp"