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

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 "eventdatetimewidgettest.h"
0008 #include "../createeventplugin/eventdatetimewidget.h"
0009 #include <KDateComboBox>
0010 #include <KTimeComboBox>
0011 #include <QSignalSpy>
0012 #include <QTest>
0013 
0014 EventDateTimeWidgetTest::EventDateTimeWidgetTest(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 EventDateTimeWidgetTest::~EventDateTimeWidgetTest() = default;
0020 
0021 void EventDateTimeWidgetTest::shouldHaveDefaultValue()
0022 {
0023     MessageViewer::EventDateTimeWidget edit;
0024     auto datecombobox = edit.findChild<KDateComboBox *>(QStringLiteral("eventdatecombobox"));
0025     QVERIFY(datecombobox);
0026     auto timecombobox = edit.findChild<KTimeComboBox *>(QStringLiteral("eventtimecombobox"));
0027     QVERIFY(timecombobox);
0028 }
0029 
0030 void EventDateTimeWidgetTest::shouldSetDateTime()
0031 {
0032     MessageViewer::EventDateTimeWidget edit;
0033     const QDateTime currentDateTime = QDateTime::currentDateTime();
0034     edit.setDateTime(currentDateTime);
0035     QCOMPARE(edit.dateTime().time().minute(), currentDateTime.time().minute());
0036     QCOMPARE(edit.dateTime().time().hour(), currentDateTime.time().hour());
0037 }
0038 
0039 void EventDateTimeWidgetTest::shouldEmitSignalWhenDateTimeChanged()
0040 {
0041     MessageViewer::EventDateTimeWidget edit;
0042     QDateTime currentDateTime = QDateTime::currentDateTime();
0043     edit.setDateTime(currentDateTime);
0044 
0045     QSignalSpy spy(&edit, &MessageViewer::EventDateTimeWidget::dateTimeChanged);
0046     currentDateTime.setDate(currentDateTime.date().addDays(1));
0047     edit.setDateTime(currentDateTime);
0048 
0049     QCOMPARE(spy.count(), 1);
0050 }
0051 
0052 void EventDateTimeWidgetTest::shouldEmitSignalWhenJustTimeChanged()
0053 {
0054     MessageViewer::EventDateTimeWidget edit;
0055     QDateTime currentDateTime = QDateTime::currentDateTime();
0056     edit.setDateTime(currentDateTime);
0057 
0058     QSignalSpy spy(&edit, &MessageViewer::EventDateTimeWidget::dateTimeChanged);
0059     QTime time = currentDateTime.time().addSecs(3600);
0060     edit.setTime(time);
0061 
0062     QCOMPARE(spy.count(), 1);
0063 }
0064 
0065 void EventDateTimeWidgetTest::shouldEmitSignalWhenJustDateChanged()
0066 {
0067     MessageViewer::EventDateTimeWidget edit;
0068     QDateTime currentDateTime = QDateTime::currentDateTime();
0069     edit.setDateTime(currentDateTime);
0070 
0071     QSignalSpy spy(&edit, &MessageViewer::EventDateTimeWidget::dateTimeChanged);
0072     QDate date = currentDateTime.date().addDays(1);
0073     edit.setDate(date);
0074 
0075     QCOMPARE(spy.count(), 1);
0076 }
0077 
0078 void EventDateTimeWidgetTest::shouldNotEmitSignalWhenDateTimeWasNotChanged()
0079 {
0080     MessageViewer::EventDateTimeWidget edit;
0081     QDateTime currentDateTime = QDateTime::currentDateTime();
0082     edit.setDateTime(currentDateTime);
0083 
0084     QSignalSpy spy(&edit, &MessageViewer::EventDateTimeWidget::dateTimeChanged);
0085     currentDateTime.setDate(currentDateTime.date().addDays(1));
0086     edit.setDateTime(currentDateTime);
0087 
0088     QCOMPARE(spy.count(), 1);
0089     edit.setDateTime(currentDateTime);
0090 
0091     // FIX ME
0092     // QCOMPARE(spy.count(), 2);
0093 }
0094 
0095 QTEST_MAIN(EventDateTimeWidgetTest)
0096 
0097 #include "moc_eventdatetimewidgettest.cpp"