File indexing completed on 2024-11-24 04:44:09

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include "freebusytest.h"
0008 #include "testhelpers.h"
0009 #include "testutils.h"
0010 
0011 #include "freebusy/freebusy.h"
0012 #include <QTest>
0013 #include <kolabfreebusy.h>
0014 
0015 #include <iostream>
0016 
0017 void FreebusyTest::testFB_data()
0018 {
0019     QTest::addColumn<Kolab::cDateTime>("start");
0020     QTest::addColumn<Kolab::cDateTime>("end");
0021     QTest::addColumn<std::vector<Kolab::Event>>("events");
0022     QTest::addColumn<std::vector<Kolab::FreebusyPeriod>>("output");
0023 
0024     // UTC check
0025     {
0026         Kolab::Period p1(Kolab::cDateTime(2011, 10, 6, 12, 1, 1, true), Kolab::cDateTime(2011, 10, 7, 12, 1, 1, true));
0027         Kolab::Period p2(Kolab::cDateTime(2011, 10, 8, 12, 1, 1, true), Kolab::cDateTime(2011, 10, 9, 12, 1, 1, true));
0028         Kolab::Period p3(Kolab::cDateTime(2011, 10, 10, 12, 1, 1, true), Kolab::cDateTime(2011, 10, 11, 12, 1, 1, true));
0029 
0030         std::vector<Kolab::Event> events;
0031         events.push_back(createEvent(p1.start, p1.end));
0032         events.push_back(createEvent(p2.start, p2.end));
0033         events.push_back(createEvent(p3.start, p3.end));
0034 
0035         Kolab::FreebusyPeriod period1;
0036         period1.setType(Kolab::FreebusyPeriod::Busy);
0037         period1.setEvent(events.at(0).uid(), events.at(0).summary(), events.at(0).location());
0038         period1.setPeriods(std::vector<Kolab::Period>() << p1);
0039 
0040         Kolab::FreebusyPeriod period2;
0041         period2.setType(Kolab::FreebusyPeriod::Busy);
0042         period2.setEvent(events.at(1).uid(), events.at(1).summary(), events.at(1).location());
0043         period2.setPeriods(std::vector<Kolab::Period>() << p2);
0044 
0045         Kolab::FreebusyPeriod period3;
0046         period3.setType(Kolab::FreebusyPeriod::Busy);
0047         period3.setEvent(events.at(2).uid(), events.at(2).summary(), events.at(2).location());
0048         period3.setPeriods(std::vector<Kolab::Period>() << p3);
0049 
0050         {
0051             std::vector<Kolab::FreebusyPeriod> output;
0052             output.push_back(period1);
0053             output.push_back(period2);
0054             output.push_back(period3);
0055 
0056             QTest::newRow("simple utc") << Kolab::cDateTime(2010, 1, 1, 1, 1, 1, true) << Kolab::cDateTime(2012, 1, 1, 1, 1, 1, true) << events << output;
0057         }
0058 
0059         {
0060             std::vector<Kolab::FreebusyPeriod> output;
0061             output.push_back(period2);
0062             output.push_back(period3);
0063 
0064             QTest::newRow("limit utc below") << Kolab::cDateTime(2011, 10, 8, 12, 1, 1, true) << Kolab::cDateTime(2012, 1, 1, 1, 1, 1, true) << events
0065                                              << output;
0066         }
0067         {
0068             std::vector<Kolab::FreebusyPeriod> output;
0069             output.push_back(period1);
0070             output.push_back(period2);
0071 
0072             QTest::newRow("limit utc above") << Kolab::cDateTime(2010, 1, 1, 1, 1, 1, true) << Kolab::cDateTime(2011, 10, 9, 12, 1, 1, true) << events
0073                                              << output;
0074         }
0075     }
0076     {
0077         Kolab::Event event;
0078         event.setUid(QUuid::createUuid().toString().toStdString());
0079         event.setStart(Kolab::cDateTime(2011, 1, 1, 0, 0, 0, true));
0080         event.setEnd(Kolab::cDateTime(2011, 1, 1, 1, 0, 0, true));
0081         Kolab::RecurrenceRule rrule;
0082         rrule.setFrequency(Kolab::RecurrenceRule::Daily);
0083         rrule.setInterval(1);
0084         rrule.setCount(2);
0085         event.setRecurrenceRule(rrule);
0086 
0087         std::vector<Kolab::Event> events;
0088         events.push_back(event);
0089 
0090         std::vector<Kolab::FreebusyPeriod> output;
0091         Kolab::FreebusyPeriod period1;
0092         period1.setType(Kolab::FreebusyPeriod::Busy);
0093         period1.setEvent(event.uid(), event.summary(), event.location());
0094         period1.setPeriods(std::vector<Kolab::Period>()
0095                            << Kolab::Period(Kolab::cDateTime(2011, 1, 1, 0, 0, 0, true), Kolab::cDateTime(2011, 1, 1, 1, 0, 0, true))
0096                            << Kolab::Period(Kolab::cDateTime(2011, 1, 2, 0, 0, 0, true), Kolab::cDateTime(2011, 1, 2, 1, 0, 0, true)));
0097         output.push_back(period1);
0098 
0099         QTest::newRow("fullday recurrence") << Kolab::cDateTime(2010, 1, 1, 1, 1, 1, true) << Kolab::cDateTime(2012, 10, 9, 12, 1, 1, true) << events << output;
0100     }
0101 }
0102 
0103 void FreebusyTest::testFB()
0104 {
0105     QFETCH(Kolab::cDateTime, start);
0106     QFETCH(Kolab::cDateTime, end);
0107     QFETCH(std::vector<Kolab::Event>, events);
0108     QFETCH(std::vector<Kolab::FreebusyPeriod>, output);
0109 
0110     Kolab::Freebusy fb = Kolab::FreebusyUtils::generateFreeBusy(events, start, end);
0111 
0112     QCOMPARE((int)fb.periods().size(), (int)output.size());
0113     for (std::size_t i = 0; i < output.size(); i++) {
0114         std::cout << i;
0115         QCOMPARE(fb.periods().at(i), output.at(i));
0116     }
0117 
0118     std::cout << Kolab::FreebusyUtils::toIFB(fb);
0119 }
0120 
0121 // void FreebusyTest::testHonorTimeFrame()
0122 // {
0123 //
0124 //     Kolab::Period p1(Kolab::cDateTime(2011,10,6,12,1,1,true), Kolab::cDateTime(2011,10,8,12,1,1,true));
0125 //     Kolab::Period p2(Kolab::cDateTime(2011,10,7,12,1,1,true), Kolab::cDateTime(2011,10,10,12,1,1,true));
0126 //     Kolab::Period p3(Kolab::cDateTime(2011,10,9,12,1,1,true), Kolab::cDateTime(2011,10,11,12,1,1,true));
0127 //
0128 //     std::vector<Kolab::Event> events;
0129 //     events.push_back(createEvent(p1.start, p1.end));
0130 //     events.push_back(createEvent(p2.start, p2.end));
0131 //     events.push_back(createEvent(p3.start, p3.end));
0132 //
0133 //     Kolab::FreebusyPeriod period1;
0134 //     period1.setType(Kolab::FreebusyPeriod::Busy);
0135 //     period1.setPeriods(std::vector<Kolab::Period>() << p1);
0136 //
0137 //     Kolab::FreebusyPeriod period2;
0138 //     period2.setType(Kolab::FreebusyPeriod::Busy);
0139 //     period2.setPeriods(std::vector<Kolab::Period>() << p2);
0140 //
0141 //     Kolab::FreebusyPeriod period3;
0142 //     period3.setType(Kolab::FreebusyPeriod::Busy);
0143 //     period3.setPeriods(std::vector<Kolab::Period>() << p3);
0144 //
0145 //
0146 //     Kolab::Freebusy fb =  Kolab::FreebusyUtils::generateFreeBusy(events, Kolab::cDateTime(2010, 1, 1,1,1,1,true), Kolab::cDateTime(2012, 1, 1,1,1,1,true));
0147 //     QCOMPARE((int)fb.periods().size(), 3);
0148 //     QCOMPARE(fb.periods().at(0), period1);
0149 //     QCOMPARE(fb.periods().at(1), period2);
0150 //     QCOMPARE(fb.periods().at(2), period3);
0151 // }
0152 
0153 QTEST_MAIN(FreebusyTest)
0154 
0155 #include "moc_freebusytest.cpp"