1 /*
2   SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
3   SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
4 
5   SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "testfreeperiodmodel.h"
9 #include "../freeperiodmodel.h"
10 
11 #include <KCalendarCore/Duration>
12 #include <KCalendarCore/Period>
13 
14 #include <QAbstractItemModelTester>
15 #include <QTest>
16 
17 using namespace CalendarSupport;
18 
19 // Workaround QTBUG-51789 causing a crash when QtWebEngineWidgets
20 // is linked into a QCoreApplication.
QTEST_GUILESS_MAIN(FreePeriodModelTest)21 QTEST_GUILESS_MAIN(FreePeriodModelTest)
22 
23 void FreePeriodModelTest::testModelValidity()
24 {
25     auto model = new FreePeriodModel(this);
26     new QAbstractItemModelTester(model, this);
27 
28     const QDateTime dt1(QDate(2010, 7, 24), QTime(7, 0, 0), Qt::UTC);
29     const QDateTime dt2(QDate(2010, 7, 24), QTime(10, 0, 0), Qt::UTC);
30 
31     KCalendarCore::Period::List list;
32 
33     list << KCalendarCore::Period(dt1, KCalendarCore::Duration(60 * 60));
34     list << KCalendarCore::Period(dt2, KCalendarCore::Duration(60 * 60));
35 
36     QCOMPARE(model->rowCount(), 0);
37     model->slotNewFreePeriods(list);
38     QCOMPARE(model->rowCount(), 2);
39 }
40 
testSplitByDay()41 void FreePeriodModelTest::testSplitByDay()
42 {
43     auto model = new FreePeriodModel(this);
44     new QAbstractItemModelTester(model, this);
45 
46     const QDateTime startDt(QDate(2010, 7, 24), QTime(8, 0, 0), Qt::UTC);
47     const QDateTime endDt(QDate(2010, 7, 25), QTime(8, 0, 0), Qt::UTC);
48 
49     KCalendarCore::Period::List list;
50 
51     // This period goes from 8am on the 24th to 8am on the 25th
52     list << KCalendarCore::Period(startDt, endDt);
53 
54     QCOMPARE(model->rowCount(), 0);
55 
56     // as part of adding the new periods
57     // the model should split the above period into two
58     // one from 8am-12 on the 24th, and the second from 00-08 on the 25th
59     model->slotNewFreePeriods(list);
60 
61     const QDateTime endPeriod1(QDate(2010, 7, 24), QTime(23, 59, 59, 999), Qt::UTC);
62     const QDateTime startPeriod2(QDate(2010, 7, 25), QTime(0, 0, 0, 0), Qt::UTC);
63 
64     QModelIndex index = model->index(0, 0);
65     auto period1 = model->data(index, FreePeriodModel::PeriodRole).value<KCalendarCore::Period>();
66     index = model->index(1, 0);
67     auto period2 = model->data(index, FreePeriodModel::PeriodRole).value<KCalendarCore::Period>();
68 
69     QCOMPARE(period1.start(), startDt);
70     QCOMPARE(period1.end(), endPeriod1);
71     QCOMPARE(period2.start(), startPeriod2);
72     QCOMPARE(period2.end(), endDt);
73 }
74