File indexing completed on 2024-05-12 05:13:47

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "modelverificationpoint.h"
0008 #include "testhelper.h"
0009 
0010 #include "applicationcontroller.h"
0011 #include "reservationmanager.h"
0012 #include "timelinemodel.h"
0013 #include "tripgroupmanager.h"
0014 #include "tripgroupproxymodel.h"
0015 
0016 #include <QAbstractItemModelTester>
0017 #include <QtTest/qtest.h>
0018 #include <QSignalSpy>
0019 #include <QStandardPaths>
0020 
0021 void initLocale()
0022 {
0023     qputenv("LC_ALL", "en_US.utf-8");
0024     qputenv("LANG", "en_US");
0025     qputenv("TZ", "UTC");
0026 }
0027 
0028 Q_CONSTRUCTOR_FUNCTION(initLocale)
0029 
0030 class TripGroupProxyTest : public QObject
0031 {
0032     Q_OBJECT
0033 private Q_SLOTS:
0034     void initTestCase()
0035     {
0036         QStandardPaths::setTestModeEnabled(true);
0037     }
0038 
0039     void init()
0040     {
0041         TripGroupManager::clear();
0042     }
0043 
0044     void testExpandCollapse()
0045     {
0046         ReservationManager resMgr;
0047         Test::clearAll(&resMgr);
0048         auto ctrl = Test::makeAppController();
0049         ctrl->setReservationManager(&resMgr);
0050         ctrl->importFromUrl(QUrl::fromLocalFile(QLatin1StringView(SOURCE_DIR "/data/timeline/multi-traveler-merge-with-countryinfo.json")));
0051         ctrl->importFromUrl(QUrl::fromLocalFile(QLatin1StringView(SOURCE_DIR "/data/google-multi-passenger-flight.json")));
0052         ctrl->importFromUrl(QUrl::fromLocalFile(QLatin1StringView(SOURCE_DIR "/../tests/randa2017.json")));
0053 
0054         TripGroupManager groupMgr;
0055         QSignalSpy addSpy(&groupMgr, &TripGroupManager::tripGroupAdded);
0056         groupMgr.setReservationManager(&resMgr);
0057         QCOMPARE(groupMgr.tripGroups().size(), 3);
0058         QCOMPARE(addSpy.size(), 3);
0059 
0060         TimelineModel model;
0061         model.setHomeCountryIsoCode(QStringLiteral("DE"));
0062         model.setCurrentDateTime(QDateTime({1996, 10, 14}, {12, 34}));
0063         model.setReservationManager(&resMgr);
0064         model.setTripGroupManager(&groupMgr);
0065 
0066         TripGroupProxyModel proxy;
0067         QAbstractItemModelTester tester(&proxy);
0068         proxy.setSourceModel(&model);
0069         proxy.expand(addSpy.at(0).at(0).toString());
0070         proxy.expand(addSpy.at(1).at(0).toString());
0071         proxy.expand(addSpy.at(2).at(0).toString());
0072 
0073         ModelVerificationPoint vp0(QLatin1StringView(SOURCE_DIR "/data/tripgroupproxy/expand-collapse-r0.model"));
0074         vp0.setRoleFilter({TimelineModel::BatchIdRole, TimelineModel::TripGroupIdRole});
0075         QVERIFY(vp0.verify(&proxy));
0076 
0077         proxy.collapse(addSpy.at(0).at(0).toString());
0078         ModelVerificationPoint vp1(QLatin1StringView(SOURCE_DIR "/data/tripgroupproxy/expand-collapse-r1.model"));
0079         vp1.setRoleFilter({TimelineModel::BatchIdRole, TimelineModel::TripGroupIdRole});
0080         QVERIFY(vp1.verify(&proxy));
0081 
0082         proxy.collapse(addSpy.at(1).at(0).toString());
0083         ModelVerificationPoint vp2(QLatin1StringView(SOURCE_DIR "/data/tripgroupproxy/expand-collapse-r2.model"));
0084         vp2.setRoleFilter({TimelineModel::BatchIdRole, TimelineModel::TripGroupIdRole});
0085         QVERIFY(vp2.verify(&proxy));
0086 
0087         proxy.collapse(addSpy.at(2).at(0).toString());
0088         ModelVerificationPoint vp3(QLatin1StringView(SOURCE_DIR "/data/tripgroupproxy/expand-collapse-r3.model"));
0089         vp3.setRoleFilter({TimelineModel::BatchIdRole, TimelineModel::TripGroupIdRole});
0090         QVERIFY(vp3.verify(&proxy));
0091 
0092         proxy.expand(addSpy.at(2).at(0).toString());
0093         QVERIFY(vp2.verify(&proxy));
0094         proxy.expand(addSpy.at(1).at(0).toString());
0095         QVERIFY(vp1.verify(&proxy));
0096         proxy.expand(addSpy.at(0).at(0).toString());
0097         QVERIFY(vp0.verify(&proxy));
0098     }
0099 
0100     void testCurrentGroup()
0101     {
0102         ReservationManager resMgr;
0103         Test::clearAll(&resMgr);
0104         auto ctrl = Test::makeAppController();
0105         ctrl->setReservationManager(&resMgr);
0106         ctrl->importFromUrl(QUrl::fromLocalFile(QLatin1StringView(SOURCE_DIR "/../tests/randa2017.json")));
0107 
0108         TripGroupManager groupMgr;
0109         QSignalSpy addSpy(&groupMgr, &TripGroupManager::tripGroupAdded);
0110         groupMgr.setReservationManager(&resMgr);
0111         QCOMPARE(groupMgr.tripGroups().size(), 1);
0112         QCOMPARE(addSpy.size(), 1);
0113 
0114         TimelineModel model;
0115         model.setHomeCountryIsoCode(QStringLiteral("DE"));
0116         model.setCurrentDateTime(QDateTime({2017, 9, 9}, {12, 34}));
0117         model.setReservationManager(&resMgr);
0118         model.setTripGroupManager(&groupMgr);
0119 
0120         TripGroupProxyModel proxy;
0121         QAbstractItemModelTester tester(&proxy);
0122         proxy.setSourceModel(&model);
0123 
0124         // future event, should be expanded
0125         ModelVerificationPoint vp0(QLatin1StringView(SOURCE_DIR "/data/tripgroupproxy/current-r0.model"));
0126         vp0.setRoleFilter({TimelineModel::BatchIdRole, TimelineModel::TripGroupIdRole});
0127         QVERIFY(vp0.verify(&proxy));
0128 
0129         // current event, must be expanded and not collapsible
0130         model.setCurrentDateTime(QDateTime({2017, 9, 14}, {12, 34}));
0131         ModelVerificationPoint vp1(QLatin1StringView(SOURCE_DIR "/data/tripgroupproxy/current-r1.model"));
0132         vp1.setRoleFilter({TimelineModel::BatchIdRole, TimelineModel::TripGroupIdRole});
0133         QVERIFY(vp1.verify(&proxy));
0134         proxy.collapse(addSpy.at(0).at(0).toString());
0135         QVERIFY(vp1.verify(&proxy));
0136 
0137         // past event, should be collapsed
0138         model.setCurrentDateTime(QDateTime({2018, 9, 9}, {12, 34}));
0139         ModelVerificationPoint vp2(QLatin1StringView(SOURCE_DIR "/data/tripgroupproxy/current-r2.model"));
0140         vp2.setRoleFilter({TimelineModel::BatchIdRole, TimelineModel::TripGroupIdRole});
0141         QCOMPARE(proxy.rowCount(), 2);
0142         QVERIFY(vp2.verify(&proxy));
0143     }
0144 
0145 };
0146 QTEST_GUILESS_MAIN(TripGroupProxyTest)
0147 
0148 #include "tripgroupproxytest.moc"