File indexing completed on 2025-01-05 04:49:45

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "todoedittest.h"
0008 #include "../createtodoplugin/todoedit.h"
0009 #include <Akonadi/Collection>
0010 #include <Akonadi/CollectionComboBox>
0011 #include <Akonadi/EntityTreeModel>
0012 #include <KMessageWidget>
0013 #include <QPushButton>
0014 #include <QStandardItemModel>
0015 #include <QTest>
0016 #include <messageviewer/globalsettings_messageviewer.h>
0017 #include <qtestkeyboard.h>
0018 #include <qtestmouse.h>
0019 
0020 #include <QLineEdit>
0021 #include <QShortcut>
0022 #include <QSignalSpy>
0023 
0024 namespace MessageViewer
0025 {
0026 extern MESSAGEVIEWER_EXPORT QAbstractItemModel *_k_todoEditStubModel;
0027 }
0028 
0029 TodoEditTest::TodoEditTest()
0030 {
0031     qRegisterMetaType<Akonadi::Collection>();
0032     qRegisterMetaType<KMime::Message::Ptr>();
0033     qRegisterMetaType<KCalendarCore::Todo::Ptr>();
0034     QStandardPaths::setTestModeEnabled(true);
0035 
0036     auto model = new QStandardItemModel;
0037     for (int id = 42; id < 51; ++id) {
0038         Akonadi::Collection collection(id);
0039         collection.setRights(Akonadi::Collection::AllRights);
0040         collection.setName(QString::number(id));
0041         collection.setContentMimeTypes(QStringList() << KCalendarCore::Todo::todoMimeType());
0042 
0043         auto item = new QStandardItem(collection.name());
0044         item->setData(QVariant::fromValue(collection), Akonadi::EntityTreeModel::CollectionRole);
0045         item->setData(QVariant::fromValue(collection.id()), Akonadi::EntityTreeModel::CollectionIdRole);
0046 
0047         model->appendRow(item);
0048     }
0049     MessageViewer::_k_todoEditStubModel = model;
0050 
0051     // Fake a default-selected collection for shouldHaveDefaultValuesOnCreation test
0052     MessageViewer::MessageViewerSettingsBase::self()->setLastSelectedFolder(43);
0053 }
0054 
0055 void TodoEditTest::shouldHaveDefaultValuesOnCreation()
0056 {
0057     MessageViewer::TodoEdit edit;
0058     QVERIFY(edit.collection().isValid());
0059     QVERIFY(!edit.message());
0060     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0061     auto openEditor = edit.findChild<QPushButton *>(QStringLiteral("open-editor-button"));
0062     auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0063     QVERIFY(openEditor);
0064     QVERIFY(save);
0065     QCOMPARE(openEditor->isEnabled(), false);
0066     QCOMPARE(save->isEnabled(), false);
0067     QVERIFY(noteedit);
0068     QCOMPARE(noteedit->text(), QString());
0069 }
0070 
0071 void TodoEditTest::shouldEmitCollectionChanged()
0072 {
0073     MessageViewer::TodoEdit edit;
0074     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::collectionChanged);
0075     edit.setCollection(Akonadi::Collection(42));
0076     QCOMPARE(spy.count(), 1);
0077     QCOMPARE(spy.at(0).at(0).value<Akonadi::Collection>(), Akonadi::Collection(42));
0078 }
0079 
0080 void TodoEditTest::shouldEmitMessageChanged()
0081 {
0082     MessageViewer::TodoEdit edit;
0083     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::messageChanged);
0084     KMime::Message::Ptr msg(new KMime::Message);
0085     edit.setMessage(msg);
0086     QCOMPARE(spy.count(), 1);
0087     QCOMPARE(spy.at(0).at(0).value<KMime::Message::Ptr>(), msg);
0088 }
0089 
0090 void TodoEditTest::shouldNotEmitWhenCollectionIsNotChanged()
0091 {
0092     MessageViewer::TodoEdit edit;
0093     edit.setCollection(Akonadi::Collection(42));
0094     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::collectionChanged);
0095     edit.setCollection(Akonadi::Collection(42));
0096     QCOMPARE(spy.count(), 0);
0097 }
0098 
0099 void TodoEditTest::shouldNotEmitWhenMessageIsNotChanged()
0100 {
0101     MessageViewer::TodoEdit edit;
0102     KMime::Message::Ptr msg(new KMime::Message);
0103     edit.setMessage(msg);
0104     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::messageChanged);
0105     edit.setMessage(msg);
0106     QCOMPARE(spy.count(), 0);
0107 }
0108 
0109 void TodoEditTest::shouldHaveSameValueAfterSet()
0110 {
0111     MessageViewer::TodoEdit edit;
0112     KMime::Message::Ptr msg(new KMime::Message);
0113     edit.setCollection(Akonadi::Collection(42));
0114     edit.setMessage(msg);
0115     QCOMPARE(edit.collection(), Akonadi::Collection(42));
0116     QCOMPARE(edit.message(), msg);
0117 }
0118 
0119 void TodoEditTest::shouldHaveASubject()
0120 {
0121     MessageViewer::TodoEdit edit;
0122     KMime::Message::Ptr msg(new KMime::Message);
0123 
0124     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0125     QVERIFY(noteedit);
0126     QCOMPARE(noteedit->text(), QString());
0127 
0128     QString subject = QStringLiteral("Test Note");
0129     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0130     edit.setMessage(msg);
0131     edit.showToDoWidget();
0132 
0133     QCOMPARE(noteedit->text(), QStringLiteral("Reply to \"%1\"").arg(subject));
0134 }
0135 
0136 void TodoEditTest::shouldEmptySubjectWhenMessageIsNull()
0137 {
0138     MessageViewer::TodoEdit edit;
0139     KMime::Message::Ptr msg(new KMime::Message);
0140     QString subject = QStringLiteral("Test Note");
0141     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0142     edit.setMessage(msg);
0143     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0144     edit.setMessage(KMime::Message::Ptr());
0145     QCOMPARE(noteedit->text(), QString());
0146 }
0147 
0148 void TodoEditTest::shouldEmptySubjectWhenMessageHasNotSubject()
0149 {
0150     MessageViewer::TodoEdit edit;
0151     KMime::Message::Ptr msg(new KMime::Message);
0152     QString subject = QStringLiteral("Test Note");
0153     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0154     edit.setMessage(msg);
0155     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0156     KMime::Message::Ptr msgSubjectEmpty(new KMime::Message);
0157     edit.setMessage(msgSubjectEmpty);
0158     QCOMPARE(noteedit->text(), QString());
0159 }
0160 
0161 void TodoEditTest::shouldSelectLineWhenPutMessage()
0162 {
0163     MessageViewer::TodoEdit edit;
0164     KMime::Message::Ptr msg(new KMime::Message);
0165     QString subject = QStringLiteral("Test Note");
0166     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0167     edit.setMessage(msg);
0168     edit.showToDoWidget();
0169     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0170     QVERIFY(noteedit->hasSelectedText());
0171     const QString selectedText = noteedit->selectedText();
0172     QCOMPARE(selectedText, QStringLiteral("Reply to \"%1\"").arg(subject));
0173 }
0174 
0175 void TodoEditTest::shouldEmitCollectionChangedWhenChangeComboboxItem()
0176 {
0177     MessageViewer::TodoEdit edit;
0178     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0179     QVERIFY(akonadicombobox);
0180     QVERIFY(akonadicombobox->currentCollection().isValid());
0181 }
0182 
0183 void TodoEditTest::shouldEmitNotEmitTodoWhenTextIsEmpty()
0184 {
0185     MessageViewer::TodoEdit edit;
0186     KMime::Message::Ptr msg(new KMime::Message);
0187     edit.setMessage(msg);
0188     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0189     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::createTodo);
0190     QTest::keyClick(noteedit, Qt::Key_Enter);
0191     QCOMPARE(spy.count(), 0);
0192 }
0193 
0194 void TodoEditTest::shouldEmitNotEmitTodoWhenTextTrimmedIsEmpty()
0195 {
0196     MessageViewer::TodoEdit edit;
0197     KMime::Message::Ptr msg(new KMime::Message);
0198     edit.setMessage(msg);
0199     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0200     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::createTodo);
0201     noteedit->setText(QStringLiteral("      "));
0202     QTest::keyClick(noteedit, Qt::Key_Enter);
0203     QCOMPARE(spy.count(), 0);
0204 
0205     noteedit->setText(QStringLiteral("      F"));
0206     QTest::keyClick(noteedit, Qt::Key_Enter);
0207     QCOMPARE(spy.count(), 1);
0208 }
0209 
0210 void TodoEditTest::shouldEmitTodoWhenPressEnter()
0211 {
0212     MessageViewer::TodoEdit edit;
0213     KMime::Message::Ptr msg(new KMime::Message);
0214     QString subject = QStringLiteral("Test Note");
0215     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0216     edit.setMessage(msg);
0217     edit.showToDoWidget();
0218     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0219     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::createTodo);
0220     QTest::keyClick(noteedit, Qt::Key_Enter);
0221     QCOMPARE(spy.count(), 1);
0222 }
0223 
0224 void TodoEditTest::shouldTodoHasCorrectSubject()
0225 {
0226     MessageViewer::TodoEdit edit;
0227     KMime::Message::Ptr msg(new KMime::Message);
0228     QString subject = QStringLiteral("Test Note");
0229     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0230     edit.setMessage(msg);
0231     edit.showToDoWidget();
0232     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0233     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::createTodo);
0234     QTest::keyClick(noteedit, Qt::Key_Enter);
0235     QCOMPARE(spy.count(), 1);
0236     auto todoPtr = spy.at(0).at(0).value<KCalendarCore::Todo::Ptr>();
0237     QVERIFY(todoPtr);
0238     QCOMPARE(todoPtr->summary(), QStringLiteral("Reply to \"%1\"").arg(subject));
0239 }
0240 
0241 void TodoEditTest::shouldClearAllWhenCloseWidget()
0242 {
0243     MessageViewer::TodoEdit edit;
0244     edit.show();
0245     edit.activateWindow();
0246     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0247 
0248     KMime::Message::Ptr msg(new KMime::Message);
0249     QString subject = QStringLiteral("Test Note");
0250     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0251     edit.setMessage(msg);
0252 
0253     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0254     edit.slotCloseWidget();
0255     QCOMPARE(noteedit->text(), QString());
0256     QVERIFY(!edit.message());
0257 }
0258 
0259 void TodoEditTest::shouldEmitCollectionChangedWhenCurrentCollectionWasChanged()
0260 {
0261     MessageViewer::TodoEdit edit;
0262     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0263     akonadicombobox->setCurrentIndex(0);
0264     QCOMPARE(akonadicombobox->currentIndex(), 0);
0265     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::collectionChanged);
0266     akonadicombobox->setCurrentIndex(3);
0267     QCOMPARE(akonadicombobox->currentIndex(), 3);
0268     QCOMPARE(spy.count(), 1);
0269 }
0270 
0271 void TodoEditTest::shouldEmitCorrectCollection()
0272 {
0273     MessageViewer::TodoEdit edit;
0274     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0275     KMime::Message::Ptr msg(new KMime::Message);
0276     QString subject = QStringLiteral("Test Note");
0277     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0278     edit.setMessage(msg);
0279     edit.showToDoWidget();
0280     akonadicombobox->setCurrentIndex(3);
0281     Akonadi::Collection col = akonadicombobox->currentCollection();
0282     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0283     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::createTodo);
0284     QTest::keyClick(noteedit, Qt::Key_Enter);
0285     QCOMPARE(spy.count(), 1);
0286     QCOMPARE(spy.at(0).at(1).value<Akonadi::Collection>(), col);
0287 }
0288 
0289 void TodoEditTest::shouldHideWidgetWhenClickOnCloseButton()
0290 {
0291     MessageViewer::TodoEdit edit;
0292     edit.show();
0293     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0294     QVERIFY(edit.isVisible());
0295     auto close = edit.findChild<QPushButton *>(QStringLiteral("close-button"));
0296     QTest::mouseClick(close, Qt::LeftButton);
0297     QCOMPARE(edit.isVisible(), false);
0298 }
0299 
0300 void TodoEditTest::shouldHideWidgetWhenPressEscape()
0301 {
0302     MessageViewer::TodoEdit edit;
0303     edit.show();
0304     // make sure the window is active so we can test for focus
0305     edit.activateWindow();
0306     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0307     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0308     noteedit->setFocus();
0309     QVERIFY(noteedit->hasFocus());
0310     QTest::keyPress(&edit, Qt::Key_Escape);
0311     QCOMPARE(edit.isVisible(), false);
0312 }
0313 
0314 void TodoEditTest::shouldHideWidgetWhenSaveClicked()
0315 {
0316     MessageViewer::TodoEdit edit;
0317     edit.show();
0318     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0319 
0320     KMime::Message::Ptr msg(new KMime::Message);
0321     msg->subject(true)->fromUnicodeString(QStringLiteral("Test Note"), "us-ascii");
0322     edit.setMessage(msg);
0323     auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0324     QTest::mouseClick(save, Qt::LeftButton);
0325     QCOMPARE(edit.isVisible(), true);
0326 }
0327 
0328 void TodoEditTest::shouldSaveCollectionSettings()
0329 {
0330     MessageViewer::TodoEdit edit;
0331     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0332     akonadicombobox->setCurrentIndex(3);
0333     const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id();
0334     auto close = edit.findChild<QPushButton *>(QStringLiteral("close-button"));
0335     QTest::mouseClick(close, Qt::LeftButton);
0336     QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastSelectedFolder(), id);
0337 }
0338 
0339 void TodoEditTest::shouldSaveCollectionSettingsWhenCloseWidget()
0340 {
0341     MessageViewer::TodoEdit edit;
0342     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0343     akonadicombobox->setCurrentIndex(4);
0344     const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id();
0345     edit.writeConfig();
0346     QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastSelectedFolder(), id);
0347 }
0348 
0349 void TodoEditTest::shouldSaveCollectionSettingsWhenDeleteWidget()
0350 {
0351     auto edit = new MessageViewer::TodoEdit;
0352     auto akonadicombobox = edit->findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0353     akonadicombobox->setCurrentIndex(4);
0354     const Akonadi::Collection::Id id = akonadicombobox->currentCollection().id();
0355     delete edit;
0356     QCOMPARE(MessageViewer::MessageViewerSettingsBase::self()->lastSelectedFolder(), id);
0357 }
0358 
0359 void TodoEditTest::shouldSetFocusWhenWeCallTodoEdit()
0360 {
0361     MessageViewer::TodoEdit edit;
0362     edit.show();
0363     // make sure the window is active so we can test for focus
0364     edit.activateWindow();
0365     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0366     KMime::Message::Ptr msg(new KMime::Message);
0367     QString subject = QStringLiteral("Test Note");
0368     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0369     edit.setMessage(msg);
0370     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0371     QCOMPARE(noteedit->hasFocus(), true);
0372     edit.setFocus();
0373     QCOMPARE(noteedit->hasFocus(), false);
0374     edit.showToDoWidget();
0375     QCOMPARE(noteedit->hasFocus(), true);
0376 }
0377 
0378 void TodoEditTest::shouldNotEmitTodoWhenMessageIsNull()
0379 {
0380     MessageViewer::TodoEdit edit;
0381     QString subject = QStringLiteral("Test Note");
0382     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0383     noteedit->setText(subject);
0384     QSignalSpy spy(&edit, &MessageViewer::TodoEdit::createTodo);
0385     QTest::keyClick(noteedit, Qt::Key_Enter);
0386     QCOMPARE(spy.count(), 0);
0387 }
0388 
0389 void TodoEditTest::shouldClearLineAfterEmitNewNote()
0390 {
0391     MessageViewer::TodoEdit edit;
0392     KMime::Message::Ptr msg(new KMime::Message);
0393     QString subject = QStringLiteral("Test Note");
0394     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0395     edit.setMessage(msg);
0396     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0397     QTest::keyClick(noteedit, Qt::Key_Enter);
0398     QCOMPARE(noteedit->text(), QString());
0399 }
0400 
0401 void TodoEditTest::shouldHaveLineEditFocus()
0402 {
0403     MessageViewer::TodoEdit edit;
0404     edit.show();
0405     // make sure the window is active so we can test for focus
0406     edit.activateWindow();
0407     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0408     KMime::Message::Ptr msg(new KMime::Message);
0409     QString subject = QStringLiteral("Test Note");
0410     msg->subject(true)->fromUnicodeString(subject, "us-ascii");
0411     edit.setMessage(msg);
0412     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0413     QCOMPARE(noteedit->hasFocus(), true);
0414 }
0415 
0416 void TodoEditTest::shouldShowMessageWidget()
0417 {
0418     MessageViewer::TodoEdit edit;
0419     edit.show();
0420     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0421 
0422     KMime::Message::Ptr msg(new KMime::Message);
0423     msg->subject(true)->fromUnicodeString(QStringLiteral("Test note"), "us-ascii");
0424     edit.setMessage(msg);
0425     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0426     noteedit->setText(QStringLiteral("Test Note"));
0427     auto msgwidget = edit.findChild<KMessageWidget *>(QStringLiteral("msgwidget"));
0428     QCOMPARE(msgwidget->isVisible(), false);
0429     QTest::keyClick(noteedit, Qt::Key_Enter);
0430     QCOMPARE(msgwidget->isVisible(), true);
0431 }
0432 
0433 void TodoEditTest::shouldHideMessageWidget()
0434 {
0435     MessageViewer::TodoEdit edit;
0436     edit.show();
0437     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0438     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0439     noteedit->setText(QStringLiteral("Test note"));
0440 
0441     KMime::Message::Ptr msg(new KMime::Message);
0442     msg->subject(true)->fromUnicodeString(QStringLiteral("Test note"), "us-ascii");
0443     edit.setMessage(msg);
0444     auto msgwidget = edit.findChild<KMessageWidget *>(QStringLiteral("msgwidget"));
0445     msgwidget->show();
0446     QCOMPARE(msgwidget->isVisible(), true);
0447     noteedit->setText(QStringLiteral("Another note"));
0448     QCOMPARE(msgwidget->isVisible(), false);
0449 }
0450 
0451 void TodoEditTest::shouldHideMessageWidgetWhenAddNewMessage()
0452 {
0453     MessageViewer::TodoEdit edit;
0454     edit.show();
0455     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0456 
0457     KMime::Message::Ptr msg(new KMime::Message);
0458     msg->subject(true)->fromUnicodeString(QStringLiteral("Test note"), "us-ascii");
0459     edit.setMessage(msg);
0460     edit.showToDoWidget();
0461     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0462     noteedit->setText(QStringLiteral("Test Note"));
0463     auto msgwidget = edit.findChild<KMessageWidget *>(QStringLiteral("msgwidget"));
0464     QTest::keyClick(noteedit, Qt::Key_Enter);
0465     QCOMPARE(msgwidget->isVisible(), true);
0466 
0467     KMime::Message::Ptr msg2(new KMime::Message);
0468     msg2->subject(true)->fromUnicodeString(QStringLiteral("Test note 2"), "us-ascii");
0469     edit.setMessage(msg2);
0470     edit.showToDoWidget();
0471     QCOMPARE(msgwidget->isVisible(), false);
0472 }
0473 
0474 void TodoEditTest::shouldHideMessageWidgetWhenCloseWidget()
0475 {
0476     MessageViewer::TodoEdit edit;
0477     edit.show();
0478     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0479 
0480     KMime::Message::Ptr msg(new KMime::Message);
0481     msg->subject(true)->fromUnicodeString(QStringLiteral("Test note"), "us-ascii");
0482     edit.setMessage(msg);
0483     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0484     noteedit->setText(QStringLiteral("Test Note"));
0485     auto msgwidget = edit.findChild<KMessageWidget *>(QStringLiteral("msgwidget"));
0486     QTest::keyClick(noteedit, Qt::Key_Enter);
0487     QCOMPARE(msgwidget->isVisible(), true);
0488     edit.slotCloseWidget();
0489 
0490     QCOMPARE(msgwidget->isHidden(), true);
0491 }
0492 
0493 void TodoEditTest::shouldEnabledSaveOpenEditorButton()
0494 {
0495     MessageViewer::TodoEdit edit;
0496     KMime::Message::Ptr msg(new KMime::Message);
0497     msg->subject(true)->fromUnicodeString(QStringLiteral("Test note"), "us-ascii");
0498     edit.setMessage(msg);
0499     edit.showToDoWidget();
0500 
0501     auto noteedit = edit.findChild<QLineEdit *>(QStringLiteral("noteedit"));
0502     auto openEditor = edit.findChild<QPushButton *>(QStringLiteral("open-editor-button"));
0503     auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0504     QCOMPARE(openEditor->isEnabled(), true);
0505     QCOMPARE(save->isEnabled(), true);
0506     noteedit->clear();
0507 
0508     QCOMPARE(openEditor->isEnabled(), false);
0509     QCOMPARE(save->isEnabled(), false);
0510     noteedit->setText(QStringLiteral("test 2"));
0511     QCOMPARE(openEditor->isEnabled(), true);
0512     QCOMPARE(save->isEnabled(), true);
0513 
0514     noteedit->setText(QStringLiteral(" "));
0515     QCOMPARE(openEditor->isEnabled(), false);
0516     QCOMPARE(save->isEnabled(), false);
0517 
0518     noteedit->setText(QStringLiteral(" foo"));
0519     QCOMPARE(openEditor->isEnabled(), true);
0520     QCOMPARE(save->isEnabled(), true);
0521 }
0522 
0523 void TodoEditTest::shouldDisabledSaveOpenEditorButtonWhenCollectionComboBoxIsEmpty()
0524 {
0525     MessageViewer::TodoEdit edit;
0526     auto akonadicombobox = edit.findChild<Akonadi::CollectionComboBox *>(QStringLiteral("akonadicombobox"));
0527     // Create an empty combobox
0528     akonadicombobox->setModel(new QStandardItemModel());
0529 
0530     KMime::Message::Ptr msg(new KMime::Message);
0531     msg->subject(true)->fromUnicodeString(QStringLiteral("Test note"), "us-ascii");
0532     edit.setMessage(msg);
0533 
0534     auto openEditor = edit.findChild<QPushButton *>(QStringLiteral("open-editor-button"));
0535     auto save = edit.findChild<QPushButton *>(QStringLiteral("save-button"));
0536     QCOMPARE(openEditor->isEnabled(), false);
0537     QCOMPARE(save->isEnabled(), false);
0538 }
0539 
0540 QTEST_MAIN(TodoEditTest)
0541 
0542 #include "moc_todoedittest.cpp"