File: TestLogger.cpp

package info (click to toggle)
amarok 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 112,344 kB
  • sloc: cpp: 195,053; xml: 4,329; ansic: 2,634; javascript: 673; ruby: 528; python: 507; sh: 252; makefile: 12
file content (187 lines) | stat: -rw-r--r-- 5,909 bytes parent folder | download | duplicates (3)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/****************************************************************************************
 * Copyright (c) 2010 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
 *                                                                                      *
 * This program is free software; you can redistribute it and/or modify it under        *
 * the terms of the GNU General Public License as published by the Free Software        *
 * Foundation; either version 2 of the License, or (at your option) any later           *
 * version.                                                                             *
 *                                                                                      *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
 *                                                                                      *
 * You should have received a copy of the GNU General Public License along with         *
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
 ****************************************************************************************/

#include "TestLogger.h"

#include "core/logger/Logger.h"

#include "mocks/MockLogger.h"

#include <ThreadWeaver/ThreadWeaver>
#include <ThreadWeaver/Job>

#include <QCoreApplication>

#include <KJob>

#include <gmock/gmock.h>

QTEST_GUILESS_MAIN( TestLogger )

using ::testing::Return;
using ::testing::AnyNumber;
using ::testing::_;
using ::testing::Mock;


class DummyJob : public KJob
{
public:
    void start() override {}
};

TestLogger::TestLogger()
{
    int argc = 1;
    char **argv = (char **) malloc(sizeof(char *));
    argv[0] = strdup( QCoreApplication::applicationName().toLocal8Bit().data() );
    ::testing::InitGoogleMock( &argc, argv );
    free( argv );
}

void
TestLogger::init()
{
}

void
TestLogger::cleanup()
{
}

class ProgressJob : public QObject, public ThreadWeaver::Job
{
    Q_OBJECT
public:
    ProgressJob() : deleteJob( false ), deleteObject( false ) {}
    void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread) override
    {
        Q_UNUSED(self);
        Q_UNUSED(thread);
        KJob *job = new DummyJob();
        QObject *obj = new QObject();
        Amarok::Logger::newProgressOperation( job, QStringLiteral( "foo" ), obj );

        if( deleteJob ) delete job;
        if( deleteObject ) delete obj;
    }

    bool deleteJob;
    bool deleteObject;

    protected:
    void defaultBegin(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread) override
    {
        Q_EMIT started(self);
        ThreadWeaver::Job::defaultBegin(self, thread);
    }

    void defaultEnd(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread) override
    {
        ThreadWeaver::Job::defaultEnd(self, thread);
        if (!self->success()) {
            Q_EMIT failed(self);
        }
        Q_EMIT done(self);
    }

    Q_SIGNALS:
    /** This signal is emitted when this job is being processed by a thread. */
    void started(ThreadWeaver::JobPointer);
    /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */
    void done(ThreadWeaver::JobPointer);
    /** This job has failed.
     * This signal is emitted when success() returns false after the job is executed. */
    void failed(ThreadWeaver::JobPointer);
};

void
TestLogger::testDoNotForwardDeletedJob()
{
    Amarok::MockLogger *mock = new Amarok::MockLogger();
    EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, _, _, _ ) ).Times( 0 );

    ProgressJob *job = new ProgressJob();
    job->deleteJob = true;
    ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>(job) );

    QTest::qSleep( 10 ); //ensure that the job has time to run
    QTest::qWait( 20 ); //give the Logger-internal timer time to fire

    QVERIFY( Mock::VerifyAndClearExpectations( &mock ) );
    delete mock;
}

void
TestLogger::testDoNotForwardDeletedSlot()
{
    Amarok::MockLogger *mock = new Amarok::MockLogger();
    EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, nullptr, _, _ ) ).Times( 1 ).WillOnce( Return() );

    ProgressJob *job = new ProgressJob();
    job->deleteObject = true;
    ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>(job) );

    QTest::qSleep( 10 ); //ensure that the job has time to run
    QTest::qWait( 20 ); //give the Logger-internal timer time to fire

    QVERIFY( Mock::VerifyAndClearExpectations( &mock ) );
    delete mock;
}

void
TestLogger::testForwardLongMessage()
{
    Amarok::MockLogger *mock = new Amarok::MockLogger();
    EXPECT_CALL( *mock, longMessageImpl( _, _ ) ).Times( 1 ).WillOnce( Return() );

    Amarok::Logger::longMessage( QStringLiteral( "foo" ), Amarok::Logger::Information );

    QTest::qWait( 20 );

    QVERIFY( Mock::VerifyAndClearExpectations( &mock ) );
    delete mock;
}

void
TestLogger::testForwardProgressOperation()
{
    Amarok::MockLogger *mock = new Amarok::MockLogger();
    EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, _, _, _ ) ).Times( 1 ).WillOnce( Return() );

    Amarok::Logger::newProgressOperation( new DummyJob(), QStringLiteral( "foo" ) );

    QTest::qWait( 20 );

    QVERIFY( Mock::VerifyAndClearExpectations( &mock ) );
    delete mock;
}

void
TestLogger::testForwardShortMessage()
{
    Amarok::MockLogger *mock = new Amarok::MockLogger();
    EXPECT_CALL( *mock, shortMessageImpl( _ ) ).Times( 1 ).WillOnce( Return() );

    Amarok::Logger::shortMessage( QStringLiteral( "foo" ) );

    QTest::qWait( 20 );

    QVERIFY( Mock::VerifyAndClearExpectations( &mock ) );
    delete mock;
}

#include "TestLogger.moc"