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

0001 /*
0002   SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
0003   SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "testfreebusyitemmodel.h"
0009 #include "../freebusyitem.h"
0010 #include "../freebusyitemmodel.h"
0011 
0012 #include <KCalendarCore/Attendee>
0013 
0014 #include <QAbstractItemModelTester>
0015 #include <QTest>
0016 
0017 using namespace CalendarSupport;
0018 
0019 // Workaround QTBUG-51789 causing a crash when QtWebEngineWidgets
0020 // is linked into a QCoreApplication.
0021 QTEST_GUILESS_MAIN(FreeBusyItemModelTest)
0022 
0023 void FreeBusyItemModelTest::testModelValidity()
0024 {
0025     auto model = new FreeBusyItemModel(this);
0026     new QAbstractItemModelTester(model, this);
0027 
0028     QVERIFY(model->rowCount() == 0);
0029 
0030     const QDateTime dt1(QDate(2010, 7, 24), QTime(7, 0, 0), QTimeZone::utc());
0031     const QDateTime dt2(QDate(2010, 7, 24), QTime(10, 0, 0), QTimeZone::utc());
0032     KCalendarCore::Attendee a1(QStringLiteral("fred"), QStringLiteral("fred@example.com"));
0033     KCalendarCore::FreeBusy::Ptr fb1(new KCalendarCore::FreeBusy());
0034 
0035     fb1->addPeriod(dt1, KCalendarCore::Duration(60 * 60));
0036     fb1->addPeriod(dt2, KCalendarCore::Duration(60 * 60));
0037 
0038     FreeBusyItem::Ptr item1(new FreeBusyItem(a1, nullptr));
0039     item1->setFreeBusy(fb1);
0040 
0041     model->addItem(item1);
0042     QVERIFY(model->rowCount() == 1);
0043     QVERIFY(model->containsAttendee(a1));
0044 
0045     QModelIndex i = model->index(0, 0);
0046     QCOMPARE(a1.fullName(), model->data(i, Qt::DisplayRole).toString());
0047     QCOMPARE(a1, model->data(i, FreeBusyItemModel::AttendeeRole).value<KCalendarCore::Attendee>());
0048     QCOMPARE(item1->freeBusy(), model->data(i, FreeBusyItemModel::FreeBusyRole).value<KCalendarCore::FreeBusy::Ptr>());
0049 
0050     QCOMPARE(model->rowCount(i), 2);
0051 
0052     model->removeRow(0);
0053     QVERIFY(model->rowCount() == 0);
0054 
0055     model->addItem(item1);
0056     QVERIFY(model->rowCount() == 1);
0057 
0058     model->removeAttendee(a1);
0059     QVERIFY(model->rowCount() == 0);
0060 
0061     model->addItem(item1);
0062     QVERIFY(model->rowCount() == 1);
0063 
0064     model->removeItem(item1);
0065     QVERIFY(model->rowCount() == 0);
0066 
0067     model->addItem(item1);
0068     QVERIFY(model->rowCount() == 1);
0069 
0070     model->clear();
0071     QVERIFY(model->rowCount() == 0);
0072 }
0073 
0074 void FreeBusyItemModelTest::testModelValidity2()
0075 {
0076     auto model = new FreeBusyItemModel(this);
0077     new QAbstractItemModelTester(model, this);
0078 
0079     const QDateTime dt1(QDate(2010, 7, 24), QTime(7, 0, 0), QTimeZone::utc());
0080     const QDateTime dt2(QDate(2010, 7, 24), QTime(10, 0, 0), QTimeZone::utc());
0081     const QDateTime dt3(QDate(2010, 7, 24), QTime(12, 0, 0), QTimeZone::utc());
0082     const QDateTime dt4(QDate(2010, 7, 24), QTime(14, 0, 0), QTimeZone::utc());
0083 
0084     KCalendarCore::Attendee a1(QStringLiteral("fred"), QStringLiteral("fred@example.com"));
0085     KCalendarCore::Attendee a2(QStringLiteral("joe"), QStringLiteral("joe@example.com"));
0086     KCalendarCore::Attendee a3(QStringLiteral("max"), QStringLiteral("max@example.com"));
0087     KCalendarCore::FreeBusy::Ptr fb1(new KCalendarCore::FreeBusy());
0088     KCalendarCore::FreeBusy::Ptr fb2(new KCalendarCore::FreeBusy());
0089     KCalendarCore::FreeBusy::Ptr fb3(new KCalendarCore::FreeBusy());
0090 
0091     fb1->addPeriod(dt1, KCalendarCore::Duration(60 * 60));
0092     fb1->addPeriod(dt2, KCalendarCore::Duration(60 * 60));
0093 
0094     fb2->addPeriod(dt1, KCalendarCore::Duration(60 * 60));
0095     fb2->addPeriod(dt2, KCalendarCore::Duration(60 * 60));
0096     fb2->addPeriod(dt3, KCalendarCore::Duration(60 * 60));
0097 
0098     fb3->addPeriod(dt1, KCalendarCore::Duration(60 * 60));
0099     fb3->addPeriod(dt2, KCalendarCore::Duration(60 * 60));
0100     fb3->addPeriod(dt4, KCalendarCore::Duration(60 * 60 * 2));
0101 
0102     FreeBusyItem::Ptr item1(new FreeBusyItem(a1, nullptr));
0103     item1->setFreeBusy(fb1);
0104     FreeBusyItem::Ptr item2(new FreeBusyItem(a2, nullptr));
0105     FreeBusyItem::Ptr item3(new FreeBusyItem(a3, nullptr));
0106 
0107     model->addItem(item1);
0108     model->addItem(item2);
0109     model->addItem(item3);
0110 
0111     QCOMPARE(model->rowCount(), 3);
0112 
0113     QVERIFY(model->containsAttendee(a1));
0114     QVERIFY(model->containsAttendee(a2));
0115     QVERIFY(model->containsAttendee(a3));
0116 
0117     QModelIndex i1 = model->index(0, 0);
0118     QCOMPARE(a1.fullName(), model->data(i1, Qt::DisplayRole).toString());
0119     QCOMPARE(a1, model->data(i1, FreeBusyItemModel::AttendeeRole).value<KCalendarCore::Attendee>());
0120     QCOMPARE(item1->freeBusy(), model->data(i1, FreeBusyItemModel::FreeBusyRole).value<KCalendarCore::FreeBusy::Ptr>());
0121 
0122     QModelIndex i2 = model->index(1, 0);
0123     QCOMPARE(a2.fullName(), model->data(i2, Qt::DisplayRole).toString());
0124     QCOMPARE(a2, model->data(i2, FreeBusyItemModel::AttendeeRole).value<KCalendarCore::Attendee>());
0125     QVERIFY(model->rowCount(i2) == 0);
0126     QVERIFY(model->data(i2, FreeBusyItemModel::FreeBusyRole).isValid() == false);
0127 
0128     QModelIndex i3 = model->index(2, 0);
0129     QCOMPARE(a3.fullName(), model->data(i3, Qt::DisplayRole).toString());
0130     QCOMPARE(a3, model->data(i3, FreeBusyItemModel::AttendeeRole).value<KCalendarCore::Attendee>());
0131     QVERIFY(model->rowCount(i3) == 0);
0132     QVERIFY(model->data(i3, FreeBusyItemModel::FreeBusyRole).isValid() == false);
0133 
0134     model->slotInsertFreeBusy(fb2, QStringLiteral("joe@example.com"));
0135     QCOMPARE(item2->freeBusy(), model->data(i2, FreeBusyItemModel::FreeBusyRole).value<KCalendarCore::FreeBusy::Ptr>());
0136     QVERIFY(model->rowCount(i2) == fb2->fullBusyPeriods().size());
0137 
0138     QModelIndex i2_0 = model->index(0, 0, i2);
0139     QCOMPARE(fb2->fullBusyPeriods().first(), model->data(i2_0, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0140     QModelIndex i2_1 = model->index(1, 0, i2);
0141     QCOMPARE(fb2->fullBusyPeriods().at(1), model->data(i2_1, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0142     QModelIndex i2_2 = model->index(2, 0, i2);
0143     QCOMPARE(fb2->fullBusyPeriods().last(), model->data(i2_2, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0144 
0145     model->slotInsertFreeBusy(fb3, QStringLiteral("max@example.com"));
0146     QCOMPARE(item3->freeBusy(), model->data(i3, FreeBusyItemModel::FreeBusyRole).value<KCalendarCore::FreeBusy::Ptr>());
0147     QVERIFY(model->rowCount(i3) == fb3->fullBusyPeriods().size());
0148 
0149     QModelIndex i3_0 = model->index(0, 0, i3);
0150     QCOMPARE(fb3->fullBusyPeriods().first(), model->data(i3_0, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0151     QModelIndex i3_1 = model->index(1, 0, i3);
0152     QCOMPARE(fb3->fullBusyPeriods().at(1), model->data(i3_1, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0153     QModelIndex i3_2 = model->index(2, 0, i3);
0154     QCOMPARE(fb3->fullBusyPeriods().last(), model->data(i3_2, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0155 
0156     model->removeAttendee(a2);
0157 
0158     QCOMPARE(2, model->rowCount());
0159 
0160     QVERIFY(model->containsAttendee(a1) == true);
0161     QVERIFY(model->containsAttendee(a2) == false);
0162     QVERIFY(model->containsAttendee(a3) == true);
0163 
0164     i3_0 = model->index(0, 0, i3);
0165     QCOMPARE(fb3->fullBusyPeriods().first(), model->data(i3_0, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0166     i3_1 = model->index(1, 0, i3);
0167     QCOMPARE(fb3->fullBusyPeriods().at(1), model->data(i3_1, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0168     i3_2 = model->index(2, 0, i3);
0169     QCOMPARE(fb3->fullBusyPeriods().last(), model->data(i3_2, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>());
0170 }
0171 
0172 void FreeBusyItemModelTest::testInsertFreeBusy()
0173 {
0174     auto model = new FreeBusyItemModel(this);
0175     new QAbstractItemModelTester(model, this);
0176 
0177     const QDateTime dt1(QDate(2010, 7, 24), QTime(7, 0, 0), QTimeZone::utc());
0178     const QDateTime dt2(QDate(2010, 7, 24), QTime(10, 0, 0), QTimeZone::utc());
0179     KCalendarCore::Attendee a1(QStringLiteral("fred"), QStringLiteral("fred@example.com"));
0180     KCalendarCore::FreeBusy::Ptr fb1(new KCalendarCore::FreeBusy());
0181     fb1->addPeriod(dt1, KCalendarCore::Duration(60 * 60));
0182     fb1->addPeriod(dt2, KCalendarCore::Duration(60 * 60));
0183 
0184     const QDateTime dt3(QDate(2010, 7, 24), QTime(12, 0, 0), QTimeZone::utc());
0185     const QDateTime dt4(QDate(2010, 7, 24), QTime(14, 0, 0), QTimeZone::utc());
0186     KCalendarCore::FreeBusy::Ptr fb2(new KCalendarCore::FreeBusy());
0187     fb2->addPeriod(dt1, KCalendarCore::Duration(60 * 60));
0188     fb2->addPeriod(dt2, KCalendarCore::Duration(60 * 60));
0189     fb2->addPeriod(dt3, KCalendarCore::Duration(60 * 60));
0190     fb2->addPeriod(dt4, KCalendarCore::Duration(60 * 60 * 2));
0191 
0192     FreeBusyItem::Ptr item1(new FreeBusyItem(a1, nullptr));
0193     item1->setFreeBusy(fb1);
0194 
0195     model->addItem(item1);
0196 
0197     QModelIndex i = model->index(0, 0);
0198     QCOMPARE(model->rowCount(i), 2);
0199 
0200     model->slotInsertFreeBusy(fb2, QStringLiteral("fred@example.com"));
0201 
0202     QCOMPARE(model->rowCount(i), 4);
0203 }
0204 
0205 #include "moc_testfreebusyitemmodel.cpp"