File indexing completed on 2024-11-03 10:01:48

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <QObject>
0008 #include <QSignalSpy>
0009 #include <QTest>
0010 
0011 #include <Solid/Job>
0012 
0013 using namespace Solid;
0014 
0015 class MockSolidJob : public Solid::Job
0016 {
0017     Q_OBJECT
0018 public:
0019     enum Error {
0020         Foo = Job::UserDefinedError,
0021         Bar,
0022     };
0023     bool execWithError();
0024     void startWithError();
0025 private Q_SLOTS:
0026     virtual void doStart();
0027     void emitQueued()
0028     {
0029         emitResult();
0030     }
0031 
0032 private:
0033     Q_DECLARE_PRIVATE(Job)
0034 };
0035 
0036 void MockSolidJob::doStart()
0037 {
0038     QMetaObject::invokeMethod(this, "emitQueued", Qt::QueuedConnection);
0039 }
0040 
0041 bool MockSolidJob::execWithError()
0042 {
0043     setError(Bar);
0044     setErrorText(QStringLiteral("Error Bar happened"));
0045     return exec();
0046 }
0047 
0048 void MockSolidJob::startWithError()
0049 {
0050     setError(Foo);
0051     setErrorText(QStringLiteral("Error Foo happened"));
0052     start();
0053 }
0054 
0055 class testSolidJob : public QObject
0056 {
0057     Q_OBJECT
0058 
0059 private Q_SLOTS:
0060     void testAsyncAndResult();
0061     void testAsyncWithError();
0062     void testAutoDelete();
0063     void testSync();
0064     void testError();
0065 };
0066 
0067 void testSolidJob::testAsyncAndResult()
0068 {
0069     MockSolidJob *job = new MockSolidJob();
0070     QSignalSpy spy(job, SIGNAL(result(Solid::Job *)));
0071 
0072     job->start();
0073 
0074     QVERIFY(spy.wait());
0075     // Result is emitted
0076     QCOMPARE(spy.count(), 1);
0077     // Result argument is the job that emitted it
0078     QCOMPARE(spy.takeFirst().first().value<MockSolidJob *>(), job);
0079 }
0080 
0081 void testSolidJob::testAsyncWithError()
0082 {
0083     MockSolidJob *job = new MockSolidJob();
0084     QSignalSpy spy(job, SIGNAL(result(Solid::Job *)));
0085 
0086     job->startWithError();
0087     QVERIFY(spy.wait()); // Even on error, we get a result
0088 
0089     MockSolidJob *rJob = spy.takeFirst().first().value<MockSolidJob *>();
0090     QCOMPARE(rJob->error(), (int)MockSolidJob::Foo);
0091     QCOMPARE(rJob->errorText(), QStringLiteral("Error Foo happened"));
0092 }
0093 
0094 void testSolidJob::testAutoDelete()
0095 {
0096     MockSolidJob *job = new MockSolidJob();
0097     QSignalSpy spy(job, SIGNAL(destroyed(QObject *)));
0098     QSignalSpy spyResult(job, SIGNAL(destroyed(QObject *)));
0099 
0100     job->start();
0101     QVERIFY(spy.wait());
0102     QVERIFY(!spyResult.isEmpty()); // Make sure result was emitted as well
0103 }
0104 
0105 void testSolidJob::testSync()
0106 {
0107     MockSolidJob *job = new MockSolidJob();
0108     QSignalSpy spy(job, SIGNAL(destroyed(QObject *)));
0109 
0110     QVERIFY(job->exec());
0111     QVERIFY(spy.wait()); // Jobs started with exec should also autodelete
0112 }
0113 
0114 void testSolidJob::testError()
0115 {
0116     MockSolidJob *job = new MockSolidJob();
0117     QVERIFY(!job->execWithError());
0118 
0119     QCOMPARE(job->error(), (int)MockSolidJob::Bar);
0120     QCOMPARE(job->errorText(), QStringLiteral("Error Bar happened"));
0121 }
0122 
0123 QTEST_MAIN(testSolidJob)
0124 
0125 #include "solidjobtest.moc"