| 1 |
/* This file is part of the KDE project |
| 2 |
SPDX-FileCopyrightText: 2007-2011 Dag Andersen <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
// clazy:excludeall=qstring-arg |
| 7 |
#include "SchedulerTester.h" |
| 8 |
|
| 9 |
#include "PlanTJScheduler.h" |
| 10 |
|
| 11 |
#include "kptcommand.h" |
| 12 |
#include "kptcalendar.h" |
| 13 |
#include "kptdatetime.h" |
| 14 |
#include "kptresource.h" |
| 15 |
#include "kptnode.h" |
| 16 |
#include "kpttask.h" |
| 17 |
#include "kptschedule.h" |
| 18 |
#include "kptxmlloaderobject.h" |
| 19 |
|
| 20 |
#include <QTest> |
| 21 |
|
| 22 |
#include "tests/DateTimeTester.h" |
| 23 |
|
| 24 |
#include "tests/debug.cpp" |
| 25 |
|
| 26 |
namespace KPlato |
| 27 |
{ |
| 28 |
|
| 29 |
QStringList SchedulerTester::data() |
| 30 |
{ |
| 31 |
return QStringList() |
| 32 |
<< "test1.plan" |
| 33 |
<< "startnotearlier.plan" |
| 34 |
; |
| 35 |
} |
| 36 |
|
| 37 |
void SchedulerTester::loadDocument(const QString &dir, const QString &fname, KoXmlDocument &doc) const |
| 38 |
{ |
| 39 |
QFile file(dir + fname); |
| 40 |
QVERIFY2(file.open(QIODevice::ReadOnly), fname.toLatin1()); |
| 41 |
|
| 42 |
QString error; |
| 43 |
bool setContent; |
| 44 |
int line, column; |
| 45 |
if (! (setContent = doc.setContent(&file, &error, &line, &column))) { |
| 46 |
file.close(); |
| 47 |
QString s = QString("%1: %2 Line %3, column %4").arg(fname).arg(error).arg(line).arg(column); |
| 48 |
QVERIFY2(setContent, s.toLatin1()); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
void SchedulerTester::testSingle() |
| 53 |
{ |
| 54 |
QString dir = QFINDTESTDATA("data/"); |
| 55 |
const auto lst = data(); |
| 56 |
for (const QString &fname : lst) { |
| 57 |
qDebug()<<"Testing file:"<<fname; |
| 58 |
KoXmlDocument doc; |
| 59 |
loadDocument(dir, fname, doc); |
| 60 |
|
| 61 |
testProject(fname, doc); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void SchedulerTester::testProject(const QString &fname, const KoXmlDocument &doc) |
| 66 |
{ |
| 67 |
KoXmlElement pel = doc.documentElement().namedItem("project").toElement(); |
| 68 |
if (pel.isNull()) { |
| 69 |
const QString s = QString("%1: Cannot find 'project' element").arg(fname); |
| 70 |
QVERIFY2(!pel.isNull(), s.toLatin1()); |
| 71 |
} |
| 72 |
Project project; |
| 73 |
project.setTimeZone(QTimeZone("UTC")); |
| 74 |
|
| 75 |
XMLLoaderObject status; |
| 76 |
status.setProject(&project); |
| 77 |
status.setVersion(doc.documentElement().attribute("version", PLAN_FILE_SYNTAX_VERSION)); |
| 78 |
bool projectLoad = status.loadProject(&project, doc); |
| 79 |
if (! projectLoad) { |
| 80 |
const QString s = QString("%1: Failed to load project").arg(fname); |
| 81 |
QVERIFY2(projectLoad, s.toLatin1()); |
| 82 |
} |
| 83 |
QString s = project.description(); |
| 84 |
if (! s.isEmpty()) { |
| 85 |
qDebug(); |
| 86 |
qDebug()<<project.description(); |
| 87 |
qDebug(); |
| 88 |
} |
| 89 |
//Debug::print(&project, "Before calculation ------------", true); |
| 90 |
ScheduleManager *manager = project.scheduleManagers().value(0); |
| 91 |
s = QString("%1: No schedule to compare with").arg(fname); |
| 92 |
QVERIFY2(manager, s.toLatin1()); |
| 93 |
|
| 94 |
ScheduleManager *sm = project.createScheduleManager("Test Plan"); |
| 95 |
project.addScheduleManager(sm); |
| 96 |
|
| 97 |
PlanTJScheduler tj(&project, sm, 5*60*1000); |
| 98 |
qDebug() << "+++++++++++++++++++++++++++calculate-start"; |
| 99 |
tj.doRun(); |
| 100 |
tj.updateProject(tj.project(), tj.manager(), &project, sm); |
| 101 |
qDebug() << "+++++++++++++++++++++++++++calculate-end"; |
| 102 |
//Debug::print(&project, "After calculation ------------", true); |
| 103 |
if (sm->calculationResult() != ScheduleManager::CalculationDone) { |
| 104 |
Debug::printSchedulingLog(*sm, "----"); |
| 105 |
} |
| 106 |
s = QString("%1: Scheduling failed").arg(fname); |
| 107 |
QVERIFY2(sm->calculationResult() == ScheduleManager::CalculationDone, s.toLatin1()); |
| 108 |
|
| 109 |
long id1 = manager->scheduleId(); |
| 110 |
long id2 = sm->scheduleId(); |
| 111 |
qDebug()<<"Project start, finish:"<<project.startTime(id1)<<project.startTime(id2)<<project.timeZone(); |
| 112 |
s = QString("%1: Compare project schedules:\n Expected: %2\n Result: %3") |
| 113 |
.arg(fname) |
| 114 |
.arg(project.startTime(id1).toString(Qt::ISODate)) |
| 115 |
.arg(project.startTime(id2).toString(Qt::ISODate)); |
| 116 |
QVERIFY2(project.startTime(id1) == project.startTime(id2), s.toLatin1()); |
| 117 |
const auto lst = project.allNodes(); |
| 118 |
for (Node *n : lst) { |
| 119 |
compare(fname, n, id1, id2); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void SchedulerTester::compare(const QString &fname, Node *n, long id1, long id2) |
| 124 |
{ |
| 125 |
QString s = QString("%1: '%2' Compare task schedules:\n Expected: %3\n Result: %4").arg(fname).arg(n->name()); |
| 126 |
QVERIFY2(n->startTime(id1) == n->startTime(id2), (s.arg(n->startTime(id1).toString(Qt::ISODate)).arg(n->startTime(id2).toString(Qt::ISODate))).toLatin1()); |
| 127 |
QVERIFY2(n->endTime(id1) == n->endTime(id2), (s.arg(n->endTime(id1).toString(Qt::ISODate)).arg(n->endTime(id2).toString(Qt::ISODate))).toLatin1()); |
| 128 |
} |
| 129 |
|
| 130 |
void SchedulerTester::loadDocuments(QString &dir, QList<QString> files, QList<KoXmlDocument> &docs) const |
| 131 |
{ |
| 132 |
for (const QString &fname : files) { |
| 133 |
QFile file(dir + fname); |
| 134 |
QVERIFY2(file.open(QIODevice::ReadOnly), fname.toLatin1()); |
| 135 |
KoXmlDocument doc; |
| 136 |
loadDocument(dir, fname, doc); |
| 137 |
docs << doc; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
} //namespace KPlato |
| 142 |
|
| 143 |
QTEST_GUILESS_MAIN(KPlato::SchedulerTester) |