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 "testfreeperiodmodel.h"
0009 #include "../freeperiodmodel.h"
0010 
0011 #include <KCalendarCore/Duration>
0012 #include <KCalendarCore/Period>
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(FreePeriodModelTest)
0022 
0023 void FreePeriodModelTest::testModelValidity()
0024 {
0025     auto model = new FreePeriodModel(this);
0026     new QAbstractItemModelTester(model, this);
0027 
0028     const QDateTime dt1(QDate(2010, 7, 24), QTime(7, 0, 0), QTimeZone::utc());
0029     const QDateTime dt2(QDate(2010, 7, 24), QTime(10, 0, 0), QTimeZone::utc());
0030 
0031     KCalendarCore::Period::List list;
0032 
0033     list << KCalendarCore::Period(dt1, KCalendarCore::Duration(60 * 60));
0034     list << KCalendarCore::Period(dt2, KCalendarCore::Duration(60 * 60));
0035 
0036     QCOMPARE(model->rowCount(), 0);
0037     model->slotNewFreePeriods(list);
0038     QCOMPARE(model->rowCount(), 2);
0039 }
0040 
0041 void FreePeriodModelTest::testSplitByDay()
0042 {
0043     auto model = new FreePeriodModel(this);
0044     new QAbstractItemModelTester(model, this);
0045 
0046     const QDateTime startDt(QDate(2010, 7, 24), QTime(8, 0, 0), QTimeZone::utc());
0047     const QDateTime endDt(QDate(2010, 7, 25), QTime(8, 0, 0), QTimeZone::utc());
0048 
0049     KCalendarCore::Period::List list;
0050 
0051     // This period goes from 8am on the 24th to 8am on the 25th
0052     list << KCalendarCore::Period(startDt, endDt);
0053 
0054     QCOMPARE(model->rowCount(), 0);
0055 
0056     // as part of adding the new periods
0057     // the model should split the above period into two
0058     // one from 8am-12 on the 24th, and the second from 00-08 on the 25th
0059     model->slotNewFreePeriods(list);
0060 
0061     const QDateTime endPeriod1(QDate(2010, 7, 24), QTime(23, 59, 59, 999), QTimeZone::utc());
0062     const QDateTime startPeriod2(QDate(2010, 7, 25), QTime(0, 0, 0, 0), QTimeZone::utc());
0063 
0064     QModelIndex index = model->index(0, 0);
0065     auto period1 = model->data(index, FreePeriodModel::PeriodRole).value<KCalendarCore::Period>();
0066     index = model->index(1, 0);
0067     auto period2 = model->data(index, FreePeriodModel::PeriodRole).value<KCalendarCore::Period>();
0068 
0069     QCOMPARE(period1.start(), startDt);
0070     QCOMPARE(period1.end(), endPeriod1);
0071     QCOMPARE(period2.start(), startPeriod2);
0072     QCOMPARE(period2.end(), endDt);
0073 }
0074 
0075 #include "moc_testfreeperiodmodel.cpp"