File indexing completed on 2024-05-05 04:38:47

0001 /*
0002     SPDX-FileCopyrightText: 2012 Miha Čančula <miha@noughmad.eu>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "projecttestjob.h"
0008 #include <interfaces/icore.h>
0009 #include <interfaces/itestcontroller.h>
0010 #include <interfaces/iproject.h>
0011 #include <interfaces/itestsuite.h>
0012 #include <KLocalizedString>
0013 
0014 using namespace KDevelop;
0015 
0016 class KDevelop::ProjectTestJobPrivate
0017 {
0018 public:
0019     explicit ProjectTestJobPrivate(ProjectTestJob* q)
0020         : q(q)
0021         , m_currentJob(nullptr)
0022         , m_currentSuite(nullptr)
0023     {}
0024 
0025     void runNext();
0026     void gotResult(ITestSuite* suite, const TestResult& result);
0027 
0028     ProjectTestJob* q;
0029 
0030     QList<ITestSuite*> m_suites;
0031     KJob* m_currentJob;
0032     ITestSuite* m_currentSuite;
0033     ProjectTestResult m_result;
0034 };
0035 
0036 void ProjectTestJobPrivate::runNext()
0037 {
0038     m_currentSuite = m_suites.takeFirst();
0039     m_currentJob = m_currentSuite->launchAllCases(ITestSuite::Silent);
0040     m_currentJob->start();
0041 }
0042 
0043 void ProjectTestJobPrivate::gotResult(ITestSuite* suite, const TestResult& result)
0044 {
0045     if (suite == m_currentSuite) {
0046         m_result.total++;
0047         q->emitPercent(m_result.total, m_result.total + m_suites.size());
0048 
0049         switch (result.suiteResult) {
0050         case TestResult::Passed:
0051             m_result.passed++;
0052             break;
0053 
0054         case TestResult::Failed:
0055             m_result.failed++;
0056             break;
0057 
0058         case TestResult::Error:
0059             m_result.error++;
0060             break;
0061 
0062         default:
0063             break;
0064         }
0065 
0066         if (m_suites.isEmpty()) {
0067             q->emitResult();
0068         } else {
0069             runNext();
0070         }
0071     }
0072 }
0073 
0074 ProjectTestJob::ProjectTestJob(IProject* project, QObject* parent)
0075     : KJob(parent)
0076     , d_ptr(new ProjectTestJobPrivate(this))
0077 {
0078     Q_D(ProjectTestJob);
0079 
0080     setCapabilities(Killable);
0081     setObjectName(i18n("Run all tests in %1", project->name()));
0082 
0083     d->m_suites = ICore::self()->testController()->testSuitesForProject(project);
0084     connect(ICore::self()->testController(), &ITestController::testRunFinished,
0085             this, [this](ITestSuite* suite, const TestResult& result) {
0086         Q_D(ProjectTestJob);
0087         d->gotResult(suite, result);
0088     });
0089 }
0090 
0091 ProjectTestJob::~ProjectTestJob()
0092 {
0093 
0094 }
0095 
0096 void ProjectTestJob::start()
0097 {
0098     Q_D(ProjectTestJob);
0099     d->runNext();
0100 }
0101 
0102 bool ProjectTestJob::doKill()
0103 {
0104     Q_D(ProjectTestJob);
0105     if (d->m_currentJob) {
0106         d->m_currentJob->kill();
0107     } else {
0108         d->m_suites.clear();
0109     }
0110     return true;
0111 }
0112 
0113 ProjectTestResult ProjectTestJob::testResult()
0114 {
0115     Q_D(ProjectTestJob);
0116     return d->m_result;
0117 }
0118 
0119 #include "moc_projecttestjob.cpp"