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

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "mailmergewidgettest.h"
0008 #include "../widgets/mailmergewidget.h"
0009 #include <PimCommon/SimpleStringListEditor>
0010 #include <QComboBox>
0011 #include <QSignalSpy>
0012 #include <QStackedWidget>
0013 #include <QTest>
0014 using namespace MailMerge;
0015 Q_DECLARE_METATYPE(MailMerge::MailMergeWidget::SourceType)
0016 MailMergeWidgetTest::MailMergeWidgetTest()
0017 {
0018     qRegisterMetaType<MailMergeWidget::SourceType>();
0019 }
0020 
0021 void MailMergeWidgetTest::shouldHaveDefaultValueOnCreation()
0022 {
0023     MailMergeWidget mailmerge;
0024     auto source = mailmerge.findChild<QComboBox *>(QStringLiteral("source"));
0025     QVERIFY(source);
0026     QCOMPARE(source->currentIndex(), 0);
0027 
0028     auto stackedwidget = mailmerge.findChild<QStackedWidget *>(QStringLiteral("stackedwidget"));
0029     QVERIFY(stackedwidget);
0030     QCOMPARE(stackedwidget->count(), 2);
0031     QCOMPARE(stackedwidget->currentIndex(), 0);
0032 
0033     for (int i = 0; i < stackedwidget->count(); ++i) {
0034         const QString objectName = stackedwidget->widget(i)->objectName();
0035         bool hasName = (objectName == QLatin1StringView("addressbookwidget") || objectName == QLatin1StringView("csvwidget"));
0036         QVERIFY(hasName);
0037     }
0038     auto listEditor = mailmerge.findChild<PimCommon::SimpleStringListEditor *>(QStringLiteral("attachment-list"));
0039     QVERIFY(listEditor);
0040     QCOMPARE(listEditor->stringList().count(), 0);
0041 }
0042 
0043 void MailMergeWidgetTest::shouldEmitSourceModeChanged()
0044 {
0045     MailMergeWidget mailmerge;
0046     auto source = mailmerge.findChild<QComboBox *>(QStringLiteral("source"));
0047     QCOMPARE(source->currentIndex(), 0);
0048     QSignalSpy spy(&mailmerge, &MailMergeWidget::sourceModeChanged);
0049     source->setCurrentIndex(1);
0050     QCOMPARE(spy.count(), 1);
0051 }
0052 
0053 void MailMergeWidgetTest::shouldDontEmitSourceModeChangedWhenIndexIsInvalid()
0054 {
0055     MailMergeWidget mailmerge;
0056     auto source = mailmerge.findChild<QComboBox *>(QStringLiteral("source"));
0057     QCOMPARE(source->currentIndex(), 0);
0058     QSignalSpy spy(&mailmerge, &MailMergeWidget::sourceModeChanged);
0059     source->setCurrentIndex(-1);
0060     QCOMPARE(spy.count(), 0);
0061 }
0062 
0063 void MailMergeWidgetTest::shouldChangeStackedWidgetIndexWhenChangeComboboxIndex()
0064 {
0065     MailMergeWidget mailmerge;
0066     auto source = mailmerge.findChild<QComboBox *>(QStringLiteral("source"));
0067     QCOMPARE(source->currentIndex(), 0);
0068 
0069     auto stackedwidget = mailmerge.findChild<QStackedWidget *>(QStringLiteral("stackedwidget"));
0070     QCOMPARE(stackedwidget->currentIndex(), 0);
0071     source->setCurrentIndex(0);
0072     QCOMPARE(stackedwidget->currentIndex(), 0);
0073     source->setCurrentIndex(1);
0074     QCOMPARE(stackedwidget->currentIndex(), 1);
0075 }
0076 
0077 QTEST_MAIN(MailMergeWidgetTest)
0078 
0079 #include "moc_mailmergewidgettest.cpp"