File indexing completed on 2025-01-05 04:47:39

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0005  */
0006 
0007 #include "noteeditdialogtest.h"
0008 #include "noteeditdialog.h"
0009 
0010 #include <Akonadi/CollectionComboBox>
0011 #include <Akonadi/EntityTreeModel>
0012 #include <Akonadi/NoteUtils>
0013 
0014 #include <TextCustomEditor/RichTextEditor>
0015 #include <TextCustomEditor/RichTextEditorWidget>
0016 
0017 #include <QLineEdit>
0018 #include <QPushButton>
0019 #include <QSignalSpy>
0020 #include <QStandardItemModel>
0021 #include <QStandardPaths>
0022 
0023 using namespace CalendarSupport;
0024 
0025 #include <QTest>
0026 NoteEditDialogTest::NoteEditDialogTest()
0027 {
0028     QStandardPaths::setTestModeEnabled(true);
0029     qRegisterMetaType<Akonadi::Collection>();
0030     qRegisterMetaType<Akonadi::Item>();
0031     qRegisterMetaType<KMime::Message::Ptr>();
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     NoteEditDialog::_k_noteEditStubModel = model;
0047 }
0048 
0049 void NoteEditDialogTest::shouldHaveDefaultValuesOnCreation()
0050 {
0051     NoteEditDialog edit;
0052     QVERIFY(!edit.note());
0053     auto notetitle = edit.findChild<QLineEdit *>(QStringLiteral("notetitle"));
0054     auto notetext = edit.findChild<TextCustomEditor::RichTextEditorWidget *>(QStringLiteral("notetext"));
0055     auto ok = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0056     QVERIFY(notetitle);
0057     QCOMPARE(notetitle->text(), QString());
0058     QVERIFY(notetext);
0059     QCOMPARE(notetext->toPlainText(), QString());
0060     QVERIFY(ok);
0061     QCOMPARE(ok->isEnabled(), false);
0062 }
0063 
0064 void NoteEditDialogTest::shouldEmitCollectionChanged()
0065 {
0066     NoteEditDialog edit;
0067     QSignalSpy spy(&edit, &NoteEditDialog::collectionChanged);
0068     edit.setCollection(Akonadi::Collection(42));
0069     QCOMPARE(spy.count(), 1);
0070     QCOMPARE(spy.at(0).at(0).value<Akonadi::Collection>(), Akonadi::Collection(42));
0071 }
0072 
0073 void NoteEditDialogTest::shouldNotEmitWhenCollectionIsNotChanged()
0074 {
0075     NoteEditDialog edit;
0076     edit.setCollection(Akonadi::Collection(42));
0077     QSignalSpy spy(&edit, &NoteEditDialog::collectionChanged);
0078     edit.setCollection(Akonadi::Collection(42));
0079     QCOMPARE(spy.count(), 0);
0080 }
0081 
0082 void NoteEditDialogTest::shouldHaveSameValueAfterSet()
0083 {
0084     NoteEditDialog edit;
0085 
0086     Akonadi::NoteUtils::NoteMessageWrapper note;
0087     Akonadi::Item item;
0088     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0089     KMime::Message::Ptr message(note.message());
0090     item.setPayload(message);
0091 
0092     edit.setCollection(Akonadi::Collection(42));
0093     edit.load(item);
0094     QCOMPARE(edit.collection(), Akonadi::Collection(42));
0095     QCOMPARE(edit.note()->encodedContent(), message->encodedContent());
0096 }
0097 
0098 void NoteEditDialogTest::shouldHaveFilledText()
0099 {
0100     NoteEditDialog edit;
0101 
0102     Akonadi::NoteUtils::NoteMessageWrapper note;
0103     QString title = QStringLiteral("title");
0104     QString text = QStringLiteral("text");
0105     note.setTitle(title);
0106     note.setText(text);
0107     Akonadi::Item item;
0108     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0109     item.setPayload(note.message());
0110 
0111     edit.load(item);
0112     auto notetitle = edit.findChild<QLineEdit *>(QStringLiteral("notetitle"));
0113     auto notetext = edit.findChild<TextCustomEditor::RichTextEditorWidget *>(QStringLiteral("notetext"));
0114     QCOMPARE(notetitle->text(), title);
0115     QCOMPARE(notetext->toPlainText(), text);
0116 }
0117 
0118 void NoteEditDialogTest::shouldHaveRichText()
0119 {
0120     NoteEditDialog edit;
0121 
0122     Akonadi::NoteUtils::NoteMessageWrapper note;
0123     QString title = QStringLiteral("title");
0124     QString text = QStringLiteral("text");
0125     note.setTitle(title);
0126     note.setText(text, Qt::RichText);
0127     Akonadi::Item item;
0128     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0129     item.setPayload(note.message());
0130 
0131     edit.load(item);
0132     auto notetext = edit.findChild<TextCustomEditor::RichTextEditorWidget *>(QStringLiteral("notetext"));
0133     QCOMPARE(notetext->toPlainText(), text);
0134     QVERIFY(notetext->editor()->acceptRichText());
0135 }
0136 
0137 void NoteEditDialogTest::shouldDefaultCollectionIsValid()
0138 {
0139     NoteEditDialog edit;
0140     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0141     QVERIFY(akonadicombobox);
0142     QVERIFY(akonadicombobox->currentCollection().isValid());
0143 }
0144 
0145 void NoteEditDialogTest::shouldEmitCollectionChangedWhenCurrentCollectionWasChanged()
0146 {
0147     NoteEditDialog edit;
0148     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0149     akonadicombobox->setCurrentIndex(0);
0150     QCOMPARE(akonadicombobox->currentIndex(), 0);
0151     QSignalSpy spy(&edit, &NoteEditDialog::collectionChanged);
0152     akonadicombobox->setCurrentIndex(3);
0153     QCOMPARE(akonadicombobox->currentIndex(), 3);
0154     QCOMPARE(spy.count(), 1);
0155 }
0156 
0157 void NoteEditDialogTest::shouldEmitCorrectCollection()
0158 {
0159     NoteEditDialog edit;
0160     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0161 
0162     Akonadi::NoteUtils::NoteMessageWrapper note;
0163     QString title = QStringLiteral("title");
0164     QString text = QStringLiteral("text");
0165     note.setTitle(title);
0166     note.setText(text);
0167     Akonadi::Item item;
0168     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0169     item.setPayload(note.message());
0170 
0171     edit.load(item);
0172     akonadicombobox->setCurrentIndex(3);
0173     Akonadi::Collection col = akonadicombobox->currentCollection();
0174     QSignalSpy spy(&edit, &NoteEditDialog::createNote);
0175     auto ok = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0176     QTest::mouseClick(ok, Qt::LeftButton);
0177     QCOMPARE(spy.count(), 1);
0178     QCOMPARE(spy.at(0).at(1).value<Akonadi::Collection>(), col);
0179 }
0180 
0181 void NoteEditDialogTest::shouldNotEmitNoteWhenTitleIsEmpty()
0182 {
0183     NoteEditDialog edit;
0184     Akonadi::NoteUtils::NoteMessageWrapper note;
0185     Akonadi::Item item;
0186     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0187     item.setPayload(note.message());
0188 
0189     edit.load(item);
0190     QSignalSpy spy(&edit, &NoteEditDialog::createNote);
0191     auto ok = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0192 
0193     auto notetitle = edit.findChild<QLineEdit *>(QStringLiteral("notetitle"));
0194     notetitle->setText(QString());
0195     QTest::mouseClick(ok, Qt::LeftButton);
0196     QCOMPARE(spy.count(), 0);
0197     notetitle->setText(QStringLiteral("F"));
0198     QTest::mouseClick(ok, Qt::LeftButton);
0199     QCOMPARE(spy.count(), 1);
0200 }
0201 
0202 void NoteEditDialogTest::shouldNotEmitNoteWhenTextIsEmpty()
0203 {
0204     NoteEditDialog edit;
0205     Akonadi::NoteUtils::NoteMessageWrapper note;
0206     Akonadi::Item item;
0207     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0208     item.setPayload(note.message());
0209 
0210     edit.load(item);
0211     QSignalSpy spy(&edit, &NoteEditDialog::createNote);
0212     auto ok = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0213 
0214     // Need to set title to empty, 'cause NoteUtils uses default title: "New Note"
0215     auto notetitle = edit.findChild<QLineEdit *>(QStringLiteral("notetitle"));
0216     notetitle->setText(QString());
0217 
0218     QTest::mouseClick(ok, Qt::LeftButton);
0219     QCOMPARE(spy.count(), 0);
0220     auto notetext = edit.findChild<TextCustomEditor::RichTextEditorWidget *>(QStringLiteral("notetext"));
0221     notetext->editor()->setText(QStringLiteral("F"));
0222     QTest::mouseClick(ok, Qt::LeftButton);
0223     QCOMPARE(spy.count(), 1);
0224 }
0225 
0226 void NoteEditDialogTest::shouldNoteHasCorrectText()
0227 {
0228     NoteEditDialog edit;
0229     Akonadi::NoteUtils::NoteMessageWrapper note;
0230     QString text(QStringLiteral("text"));
0231     note.setText(text);
0232     Akonadi::Item item;
0233     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0234     item.setPayload(note.message());
0235 
0236     edit.load(item);
0237     QSignalSpy spy(&edit, &NoteEditDialog::createNote);
0238     auto ok = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0239 
0240     QTest::mouseClick(ok, Qt::LeftButton);
0241     QCOMPARE(spy.count(), 1);
0242     Akonadi::NoteUtils::NoteMessageWrapper rNote(spy.at(0).at(0).value<Akonadi::Item>().payload<KMime::Message::Ptr>());
0243     QCOMPARE(rNote.text(), text);
0244     auto notetext = edit.findChild<TextCustomEditor::RichTextEditorWidget *>(QStringLiteral("notetext"));
0245     QString text2 = QStringLiteral("F");
0246     notetext->editor()->setText(text2);
0247     QTest::mouseClick(ok, Qt::LeftButton);
0248     QCOMPARE(spy.count(), 2);
0249     Akonadi::NoteUtils::NoteMessageWrapper r2Note(spy.at(1).at(0).value<Akonadi::Item>().payload<KMime::Message::Ptr>());
0250     QCOMPARE(r2Note.text(), text2);
0251 }
0252 
0253 void NoteEditDialogTest::shouldNoteHasCorrectTitle()
0254 {
0255     NoteEditDialog edit;
0256     Akonadi::NoteUtils::NoteMessageWrapper note;
0257     QString text(QStringLiteral("text"));
0258     note.setTitle(text);
0259     Akonadi::Item item;
0260     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0261     item.setPayload(note.message());
0262 
0263     edit.load(item);
0264     QSignalSpy spy(&edit, &NoteEditDialog::createNote);
0265     auto ok = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0266 
0267     QTest::mouseClick(ok, Qt::LeftButton);
0268     QCOMPARE(spy.count(), 1);
0269     Akonadi::NoteUtils::NoteMessageWrapper rNote(spy.at(0).at(0).value<Akonadi::Item>().payload<KMime::Message::Ptr>());
0270     QCOMPARE(rNote.title(), text);
0271     auto notetitle = edit.findChild<QLineEdit *>(QStringLiteral("notetitle"));
0272     QString text2 = QStringLiteral("F");
0273     notetitle->setText(text2);
0274     QTest::mouseClick(ok, Qt::LeftButton);
0275     QCOMPARE(spy.count(), 2);
0276     Akonadi::NoteUtils::NoteMessageWrapper r2Note(spy.at(1).at(0).value<Akonadi::Item>().payload<KMime::Message::Ptr>());
0277     QCOMPARE(r2Note.title(), text2);
0278 }
0279 
0280 void NoteEditDialogTest::shouldNoteHasCorrectTextFormat()
0281 {
0282     NoteEditDialog edit;
0283     Akonadi::NoteUtils::NoteMessageWrapper note;
0284     QString text(QStringLiteral("text"));
0285     note.setText(text);
0286     Akonadi::Item item;
0287     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0288     item.setPayload(note.message());
0289 
0290     edit.load(item);
0291     QSignalSpy spy(&edit, &NoteEditDialog::createNote);
0292     auto ok = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0293 
0294     QTest::mouseClick(ok, Qt::LeftButton);
0295     QCOMPARE(spy.count(), 1);
0296     Akonadi::NoteUtils::NoteMessageWrapper rNote(spy.at(0).at(0).value<Akonadi::Item>().payload<KMime::Message::Ptr>());
0297     QCOMPARE(rNote.textFormat(), Qt::PlainText);
0298     auto notetext = edit.findChild<TextCustomEditor::RichTextEditorWidget *>(QStringLiteral("notetext"));
0299     notetext->editor()->setAcceptRichText(true);
0300     QTest::mouseClick(ok, Qt::LeftButton);
0301     QCOMPARE(spy.count(), 2);
0302     Akonadi::NoteUtils::NoteMessageWrapper r2Note(spy.at(1).at(0).value<Akonadi::Item>().payload<KMime::Message::Ptr>());
0303     QCOMPARE(r2Note.textFormat(), Qt::RichText);
0304 }
0305 
0306 void NoteEditDialogTest::shouldShouldEnabledSaveEditorButton()
0307 {
0308     NoteEditDialog edit;
0309     Akonadi::NoteUtils::NoteMessageWrapper note;
0310     QString text(QStringLiteral("text"));
0311     note.setTitle(text);
0312     Akonadi::Item item;
0313     item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0314     item.setPayload(note.message());
0315 
0316     edit.load(item);
0317 
0318     auto ok = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0319     auto notetitle = edit.findChild<QLineEdit *>(QStringLiteral("notetitle"));
0320 
0321     QCOMPARE(ok->isEnabled(), true);
0322     notetitle->clear();
0323 
0324     QCOMPARE(ok->isEnabled(), false);
0325 }
0326 
0327 QTEST_MAIN(NoteEditDialogTest)
0328 
0329 #include "moc_noteeditdialogtest.cpp"