File indexing completed on 2024-11-10 06:38:06

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #ifndef KJOBTEST_H
0009 #define KJOBTEST_H
0010 
0011 #include "kjob.h"
0012 #include "kjobuidelegate.h"
0013 #include <QEventLoop>
0014 #include <QMap>
0015 #include <QObject>
0016 
0017 class TestJob : public KJob
0018 {
0019     Q_OBJECT
0020 public:
0021     TestJob();
0022     ~TestJob() override;
0023 
0024     void start() override;
0025     using KJob::isFinished;
0026     using KJob::setProgressUnit;
0027 
0028 protected:
0029     bool doKill() override;
0030 
0031 public:
0032     void setError(int errorCode);
0033     void setErrorText(const QString &errorText);
0034     void setProcessedSize(qulonglong size);
0035     void setTotalSize(qulonglong size);
0036     void setProcessedFiles(qulonglong files);
0037     void setTotalFiles(qulonglong files);
0038     void setPercent(unsigned long percentage);
0039 };
0040 
0041 class TestJobUiDelegate : public KJobUiDelegate
0042 {
0043     Q_OBJECT
0044 protected:
0045     virtual void connectJob(KJob *job);
0046 };
0047 
0048 class WaitJob;
0049 
0050 class KJobTest : public QObject
0051 {
0052     Q_OBJECT
0053 public:
0054     enum class Action {
0055         Start,
0056         KillQuietly,
0057         KillVerbosely,
0058     };
0059     Q_ENUM(Action)
0060 
0061     KJobTest();
0062 
0063 public Q_SLOTS:
0064 
0065     // These slots need to be public, otherwise qtestlib calls them as part of the test
0066     void slotStartInnerJob();
0067     void slotFinishOuterJob();
0068     void slotFinishInnerJob();
0069 
0070 private Q_SLOTS:
0071     void testEmitResult_data();
0072     void testEmitResult();
0073     void testProgressTracking();
0074     void testExec_data();
0075     void testExec();
0076     void testKill_data();
0077     void testKill();
0078     void testDestroy();
0079     void testEmitAtMostOnce_data();
0080     void testEmitAtMostOnce();
0081     void testDelegateUsage();
0082     void testNestedExec();
0083 
0084     void slotResult(KJob *job);
0085     void slotFinished(KJob *job);
0086 
0087 private:
0088     TestJob *setupErrorResultFinished();
0089 
0090     QEventLoop loop;
0091     int m_lastError;
0092     QString m_lastErrorText;
0093     int m_resultCount;
0094     int m_finishedCount;
0095 
0096     WaitJob *m_outerJob;
0097     WaitJob *m_innerJob;
0098     QMap<KJob *, int> m_jobFinishCount;
0099 };
0100 
0101 class WaitJob : public KJob
0102 {
0103     Q_OBJECT
0104 public:
0105     void start() override;
0106     void makeItFinish();
0107 };
0108 
0109 #endif