File: solidjobtest.cpp

package info (click to toggle)
solid 5.78.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 5,100 kB
  • sloc: cpp: 21,549; xml: 464; lex: 111; yacc: 83; sh: 14; makefile: 5
file content (123 lines) | stat: -rw-r--r-- 2,814 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
    SPDX-FileCopyrightText: 2014 Alejandro Fiestas Olivares <afiestas@kde.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include <QObject>
#include <QTest>
#include <QSignalSpy>

#include <Solid/Job>

using namespace Solid;

class MockSolidJob : public Solid::Job
{
    Q_OBJECT
public:
    enum Error {
        Foo = Job::UserDefinedError,
        Bar
    };
    bool execWithError();
    void startWithError();
private Q_SLOTS:
    virtual void doStart();
    void emitQueued() {
        emitResult();
    }
private:
    Q_DECLARE_PRIVATE(Job)
};

void MockSolidJob::doStart()
{
    QMetaObject::invokeMethod(this, "emitQueued", Qt::QueuedConnection);
}

bool MockSolidJob::execWithError()
{
    setError(Bar);
    setErrorText(QStringLiteral("Error Bar happened"));
    return exec();
}

void MockSolidJob::startWithError()
{
    setError(Foo);
    setErrorText(QStringLiteral("Error Foo happened"));
    start();
}

class testSolidJob : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void testAsyncAndResult();
    void testAsyncWithError();
    void testAutoDelete();
    void testSync();
    void testError();
};

void testSolidJob::testAsyncAndResult()
{
    MockSolidJob *job = new MockSolidJob();
    QSignalSpy spy(job, SIGNAL(result(Solid::Job*)));

    job->start();

    QVERIFY(spy.wait());
    //Result is emitted
    QCOMPARE(spy.count(), 1);
    //Result argument is the job that emitted it
    QCOMPARE(spy.takeFirst().first().value<MockSolidJob*>(), job);
}

void testSolidJob::testAsyncWithError()
{
    MockSolidJob *job = new MockSolidJob();
    QSignalSpy spy(job, SIGNAL(result(Solid::Job*)));

    job->startWithError();
    QVERIFY(spy.wait()); //Even on error, we get a result

    MockSolidJob *rJob = spy.takeFirst().first().value<MockSolidJob*>();
    QCOMPARE(rJob->error(), (int) MockSolidJob::Foo);
    QCOMPARE(rJob->errorText(), QStringLiteral("Error Foo happened"));
}

void testSolidJob::testAutoDelete()
{
    MockSolidJob *job = new MockSolidJob();
    QSignalSpy spy(job, SIGNAL(destroyed(QObject*)));
    QSignalSpy spyResult(job, SIGNAL(destroyed(QObject*)));

    job->start();
    QVERIFY(spy.wait());
    QVERIFY(!spyResult.isEmpty()); // Make sure result was emitted as well
}

void testSolidJob::testSync()
{
    MockSolidJob *job = new MockSolidJob();
    QSignalSpy spy(job, SIGNAL(destroyed(QObject*)));

    QVERIFY(job->exec());
    QVERIFY(spy.wait()); //Jobs started with exec should also autodelete
}

void testSolidJob::testError()
{
    MockSolidJob *job = new MockSolidJob();
    QVERIFY(!job->execWithError());

    QCOMPARE(job->error(), (int)MockSolidJob::Bar);
    QCOMPARE(job->errorText(), QStringLiteral("Error Bar happened"));
}

QTEST_MAIN(testSolidJob)

#include "solidjobtest.moc"