File indexing completed on 2024-11-24 04:41:34

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Glen Ditchfield <GJDitchfield@acm.org>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #include <QTest>
0007 
0008 #include "../monthitem.h"
0009 
0010 using namespace EventViews;
0011 
0012 class MonthItemOrderTest : public QObject
0013 {
0014     Q_OBJECT
0015 
0016 private Q_SLOTS:
0017 
0018     void longerInstancesFirst();
0019     void holidaysFirst();
0020     void stableOrder();
0021 
0022 public:
0023     IncidenceMonthItem *eventItem(QDate start, QDate end);
0024 };
0025 
0026 /**
0027  * Longer instances are placed before shorter ones, regardless of their
0028  * relative dates.
0029  */
0030 void MonthItemOrderTest::longerInstancesFirst()
0031 {
0032     QDate startDate(2000, 01, 01);
0033     IncidenceMonthItem *longEvent = eventItem(startDate, startDate.addDays(1));
0034     auto longHoliday = new HolidayMonthItem(nullptr, startDate, startDate.addDays(1), QStringLiteral(""));
0035     for (int offset = -1; offset < 3; offset++) {
0036         QDate d = startDate.addDays(offset);
0037 
0038         IncidenceMonthItem *shortEvent = eventItem(d, d);
0039         QVERIFY(MonthItem::greaterThan(longEvent, shortEvent));
0040         QVERIFY(!MonthItem::greaterThan(shortEvent, longEvent));
0041         QVERIFY(MonthItem::greaterThan(longHoliday, shortEvent));
0042         QVERIFY(!MonthItem::greaterThan(shortEvent, longHoliday));
0043 
0044         auto shortHoliday = new HolidayMonthItem(nullptr, d, QStringLiteral(""));
0045         QVERIFY(MonthItem::greaterThan(longEvent, shortHoliday));
0046         QVERIFY(!MonthItem::greaterThan(shortHoliday, longEvent));
0047         QVERIFY(MonthItem::greaterThan(longHoliday, shortHoliday));
0048         QVERIFY(!MonthItem::greaterThan(shortHoliday, longHoliday));
0049     }
0050 }
0051 
0052 /**
0053  * Holidays are placed before events with the same length and day.
0054  */
0055 void MonthItemOrderTest::holidaysFirst()
0056 {
0057     QDate startDate(2000, 01, 01);
0058     IncidenceMonthItem *event = eventItem(startDate, startDate);
0059     auto holiday = new HolidayMonthItem(nullptr, startDate, QStringLiteral(""));
0060     QVERIFY(!MonthItem::greaterThan(event, holiday));
0061     QVERIFY(MonthItem::greaterThan(holiday, event));
0062 }
0063 
0064 /**
0065  * If two holidays are on the same day, they do not both come before the other.
0066  * Similarly for two events with the same length and start day.
0067  */
0068 void MonthItemOrderTest::stableOrder()
0069 {
0070     QDate startDate(2000, 01, 01);
0071 
0072     auto holiday = new HolidayMonthItem(nullptr, startDate, QStringLiteral(""));
0073     auto otherHoliday = new HolidayMonthItem(nullptr, startDate, QStringLiteral(""));
0074     QVERIFY(!(MonthItem::greaterThan(otherHoliday, holiday) && MonthItem::greaterThan(holiday, otherHoliday)));
0075 
0076     IncidenceMonthItem *event = eventItem(startDate, startDate);
0077     IncidenceMonthItem *otherEvent = eventItem(startDate, startDate);
0078     QVERIFY(!(MonthItem::greaterThan(otherEvent, event) && MonthItem::greaterThan(event, otherEvent)));
0079 }
0080 
0081 IncidenceMonthItem *MonthItemOrderTest::eventItem(QDate start, QDate end)
0082 {
0083     auto e = new KCalendarCore::Event;
0084     e->setDtStart(QDateTime(start, QTime(00, 00, 00)));
0085     e->setDtEnd(QDateTime(end, QTime(00, 00, 00)));
0086     e->setAllDay(true);
0087     return new IncidenceMonthItem(nullptr, nullptr, Akonadi::Item(), KCalendarCore::Event::Ptr(e), start);
0088 }
0089 
0090 QTEST_MAIN(MonthItemOrderTest)
0091 
0092 #include "monthitemordertest.moc"