Changeset View
Changeset View
Standalone View
Standalone View
tests/core/logger/TestLogger.cpp
- This file was moved from tests/core-impl/logger/TestProxyLogger.cpp.
1 | /**************************************************************************************** | 1 | /**************************************************************************************** | ||
---|---|---|---|---|---|
2 | * Copyright (c) 2010 Maximilian Kossick <maximilian.kossick@googlemail.com> * | 2 | * Copyright (c) 2010 Maximilian Kossick <maximilian.kossick@googlemail.com> * | ||
3 | * * | 3 | * * | ||
4 | * This program is free software; you can redistribute it and/or modify it under * | 4 | * This program is free software; you can redistribute it and/or modify it under * | ||
5 | * the terms of the GNU General Public License as published by the Free Software * | 5 | * the terms of the GNU General Public License as published by the Free Software * | ||
6 | * Foundation; either version 2 of the License, or (at your option) any later * | 6 | * Foundation; either version 2 of the License, or (at your option) any later * | ||
7 | * version. * | 7 | * version. * | ||
8 | * * | 8 | * * | ||
9 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY * | 9 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY * | ||
10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * | 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * | ||
11 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. * | 11 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. * | ||
12 | * * | 12 | * * | ||
13 | * You should have received a copy of the GNU General Public License along with * | 13 | * You should have received a copy of the GNU General Public License along with * | ||
14 | * this program. If not, see <http://www.gnu.org/licenses/>. * | 14 | * this program. If not, see <http://www.gnu.org/licenses/>. * | ||
15 | ****************************************************************************************/ | 15 | ****************************************************************************************/ | ||
16 | 16 | | |||
17 | #include "TestProxyLogger.h" | 17 | #include "TestLogger.h" | ||
18 | 18 | | |||
19 | #include "core/interfaces/Logger.h" | 19 | #include "core/logger/Logger.h" | ||
20 | #include "core-impl/logger/ProxyLogger.h" | | |||
21 | 20 | | |||
22 | #include "mocks/MockLogger.h" | 21 | #include "mocks/MockLogger.h" | ||
23 | 22 | | |||
24 | #include <ThreadWeaver/ThreadWeaver> | 23 | #include <ThreadWeaver/ThreadWeaver> | ||
25 | #include <ThreadWeaver/Job> | 24 | #include <ThreadWeaver/Job> | ||
26 | 25 | | |||
27 | #include <QCoreApplication> | 26 | #include <QCoreApplication> | ||
28 | 27 | | |||
28 | #include <KJob> | ||||
29 | | ||||
29 | #include <gmock/gmock.h> | 30 | #include <gmock/gmock.h> | ||
30 | 31 | | |||
31 | QTEST_GUILESS_MAIN( TestProxyLogger ) | 32 | QTEST_GUILESS_MAIN( TestLogger ) | ||
32 | 33 | | |||
33 | using ::testing::Return; | 34 | using ::testing::Return; | ||
34 | using ::testing::AnyNumber; | 35 | using ::testing::AnyNumber; | ||
35 | using ::testing::_; | 36 | using ::testing::_; | ||
36 | using ::testing::Mock; | 37 | using ::testing::Mock; | ||
37 | 38 | | |||
38 | static ProxyLogger *s_logger; | | |||
39 | 39 | | |||
40 | class DummyJob : public KJob | 40 | class DummyJob : public KJob | ||
41 | { | 41 | { | ||
42 | public: | 42 | public: | ||
43 | virtual void start() {} | 43 | virtual void start() {} | ||
44 | }; | 44 | }; | ||
45 | 45 | | |||
46 | TestProxyLogger::TestProxyLogger() | 46 | TestLogger::TestLogger() | ||
47 | { | 47 | { | ||
48 | int argc = 1; | 48 | int argc = 1; | ||
49 | char **argv = (char **) malloc(sizeof(char *)); | 49 | char **argv = (char **) malloc(sizeof(char *)); | ||
50 | argv[0] = strdup( QCoreApplication::applicationName().toLocal8Bit().data() ); | 50 | argv[0] = strdup( QCoreApplication::applicationName().toLocal8Bit().data() ); | ||
51 | ::testing::InitGoogleMock( &argc, argv ); | 51 | ::testing::InitGoogleMock( &argc, argv ); | ||
52 | delete[] argv; | 52 | delete[] argv; | ||
53 | } | 53 | } | ||
54 | 54 | | |||
55 | void | 55 | void | ||
56 | TestProxyLogger::init() | 56 | TestLogger::init() | ||
57 | { | 57 | { | ||
58 | s_logger = 0; | | |||
59 | } | 58 | } | ||
60 | 59 | | |||
61 | void | 60 | void | ||
62 | TestProxyLogger::cleanup() | 61 | TestLogger::cleanup() | ||
63 | { | 62 | { | ||
64 | delete s_logger; | | |||
65 | } | 63 | } | ||
66 | 64 | | |||
67 | class ProgressJob : public QObject, public ThreadWeaver::Job | 65 | class ProgressJob : public QObject, public ThreadWeaver::Job | ||
68 | { | 66 | { | ||
69 | Q_OBJECT | 67 | Q_OBJECT | ||
70 | public: | 68 | public: | ||
71 | ProgressJob() : deleteJob( false ), deleteObject( false ) {} | 69 | ProgressJob() : deleteJob( false ), deleteObject( false ) {} | ||
72 | void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread) | 70 | void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread) | ||
73 | { | 71 | { | ||
74 | Q_UNUSED(self); | 72 | Q_UNUSED(self); | ||
75 | Q_UNUSED(thread); | 73 | Q_UNUSED(thread); | ||
76 | KJob *job = new DummyJob(); | 74 | KJob *job = new DummyJob(); | ||
77 | QObject *obj = new QObject(); | 75 | QObject *obj = new QObject(); | ||
78 | s_logger->newProgressOperation( job, QString( "foo" ), obj ); | 76 | Amarok::Logger::newProgressOperation( job, QString( "foo" ), obj ); | ||
79 | 77 | | |||
80 | if( deleteJob ) delete job; | 78 | if( deleteJob ) delete job; | ||
81 | if( deleteObject ) delete obj; | 79 | if( deleteObject ) delete obj; | ||
82 | } | 80 | } | ||
83 | 81 | | |||
84 | bool deleteJob; | 82 | bool deleteJob; | ||
85 | bool deleteObject; | 83 | bool deleteObject; | ||
86 | 84 | | |||
Show All 19 Lines | |||||
106 | /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */ | 104 | /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */ | ||
107 | void done(ThreadWeaver::JobPointer); | 105 | void done(ThreadWeaver::JobPointer); | ||
108 | /** This job has failed. | 106 | /** This job has failed. | ||
109 | * This signal is emitted when success() returns false after the job is executed. */ | 107 | * This signal is emitted when success() returns false after the job is executed. */ | ||
110 | void failed(ThreadWeaver::JobPointer); | 108 | void failed(ThreadWeaver::JobPointer); | ||
111 | }; | 109 | }; | ||
112 | 110 | | |||
113 | void | 111 | void | ||
114 | TestProxyLogger::testDoNotForwardDeletedJob() | 112 | TestLogger::testDoNotForwardDeletedJob() | ||
115 | { | 113 | { | ||
116 | s_logger = new ProxyLogger(); | | |||
117 | | ||||
118 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | 114 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | ||
119 | EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, _, _, _ ) ).Times( 0 ); | 115 | EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, _, _, _ ) ).Times( 0 ); | ||
120 | 116 | | |||
121 | s_logger->setLogger( mock ); | | |||
122 | | ||||
123 | ProgressJob *job = new ProgressJob(); | 117 | ProgressJob *job = new ProgressJob(); | ||
124 | job->deleteJob = true; | 118 | job->deleteJob = true; | ||
125 | ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>(job) ); | 119 | ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>(job) ); | ||
126 | 120 | | |||
127 | QTest::qSleep( 10 ); //ensure that the job has time to run | 121 | QTest::qSleep( 10 ); //ensure that the job has time to run | ||
128 | QTest::qWait( 20 ); //give the ProxyLogger-internal timer time to fire | 122 | QTest::qWait( 20 ); //give the Logger-internal timer time to fire | ||
129 | 123 | | |||
130 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | 124 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | ||
131 | delete mock; | 125 | delete mock; | ||
132 | } | 126 | } | ||
133 | 127 | | |||
134 | void | 128 | void | ||
135 | TestProxyLogger::testDoNotForwardDeletedSlot() | 129 | TestLogger::testDoNotForwardDeletedSlot() | ||
136 | { | 130 | { | ||
137 | s_logger = new ProxyLogger(); | | |||
138 | | ||||
139 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | 131 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | ||
140 | EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, nullptr, _, _ ) ).Times( 1 ).WillOnce( Return() ); | 132 | EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, nullptr, _, _ ) ).Times( 1 ).WillOnce( Return() ); | ||
141 | 133 | | |||
142 | s_logger->setLogger( mock ); | | |||
143 | | ||||
144 | ProgressJob *job = new ProgressJob(); | 134 | ProgressJob *job = new ProgressJob(); | ||
145 | job->deleteObject = true; | 135 | job->deleteObject = true; | ||
146 | ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>(job) ); | 136 | ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>(job) ); | ||
147 | 137 | | |||
148 | QTest::qSleep( 10 ); //ensure that the job has time to run | 138 | QTest::qSleep( 10 ); //ensure that the job has time to run | ||
149 | QTest::qWait( 20 ); //give the ProxyLogger-internal timer time to fire | 139 | QTest::qWait( 20 ); //give the Logger-internal timer time to fire | ||
150 | 140 | | |||
151 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | 141 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | ||
152 | delete mock; | 142 | delete mock; | ||
153 | } | 143 | } | ||
154 | 144 | | |||
155 | void | 145 | void | ||
156 | TestProxyLogger::testForwardLongMessage() | 146 | TestLogger::testForwardLongMessage() | ||
157 | { | 147 | { | ||
158 | s_logger = new ProxyLogger(); | | |||
159 | | ||||
160 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | 148 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | ||
161 | EXPECT_CALL( *mock, longMessage( _, _ ) ).Times( 1 ).WillOnce( Return() ); | 149 | EXPECT_CALL( *mock, longMessageImpl( _, _ ) ).Times( 1 ).WillOnce( Return() ); | ||
162 | 150 | | |||
163 | s_logger->setLogger( mock ); | 151 | Amarok::Logger::longMessage( "foo", Amarok::Logger::Information ); | ||
164 | | ||||
165 | s_logger->longMessage( "foo", Amarok::Logger::Information ); | | |||
166 | 152 | | |||
167 | QTest::qWait( 20 ); | 153 | QTest::qWait( 20 ); | ||
168 | 154 | | |||
169 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | 155 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | ||
170 | delete mock; | 156 | delete mock; | ||
171 | } | 157 | } | ||
172 | 158 | | |||
173 | void | 159 | void | ||
174 | TestProxyLogger::testForwardProgressOperation() | 160 | TestLogger::testForwardProgressOperation() | ||
175 | { | 161 | { | ||
176 | s_logger = new ProxyLogger(); | | |||
177 | | ||||
178 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | 162 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | ||
179 | EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, _, _, _ ) ).Times( 1 ).WillOnce( Return() ); | 163 | EXPECT_CALL( *mock, newProgressOperationImpl( An<KJob*>(), _, _, _, _ ) ).Times( 1 ).WillOnce( Return() ); | ||
180 | 164 | | |||
181 | s_logger->setLogger( mock ); | 165 | Amarok::Logger::newProgressOperation( new DummyJob(), QString( "foo" ) ); | ||
182 | | ||||
183 | s_logger->newProgressOperation( new DummyJob(), QString( "foo" ) ); | | |||
184 | 166 | | |||
185 | QTest::qWait( 20 ); | 167 | QTest::qWait( 20 ); | ||
186 | 168 | | |||
187 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | 169 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | ||
188 | delete mock; | 170 | delete mock; | ||
189 | } | 171 | } | ||
190 | 172 | | |||
191 | void | 173 | void | ||
192 | TestProxyLogger::testForwardShortMessage() | 174 | TestLogger::testForwardShortMessage() | ||
193 | { | 175 | { | ||
194 | s_logger = new ProxyLogger(); | | |||
195 | | ||||
196 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | 176 | Amarok::MockLogger *mock = new Amarok::MockLogger(); | ||
197 | EXPECT_CALL( *mock, shortMessage( _ ) ).Times( 1 ).WillOnce( Return() ); | 177 | EXPECT_CALL( *mock, shortMessageImpl( _ ) ).Times( 1 ).WillOnce( Return() ); | ||
198 | | ||||
199 | s_logger->setLogger( mock ); | | |||
200 | 178 | | |||
201 | s_logger->shortMessage( "foo" ); | 179 | Amarok::Logger::shortMessage( "foo" ); | ||
202 | 180 | | |||
203 | QTest::qWait( 20 ); | 181 | QTest::qWait( 20 ); | ||
204 | 182 | | |||
205 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | 183 | QVERIFY( Mock::VerifyAndClearExpectations( &mock ) ); | ||
206 | delete mock; | 184 | delete mock; | ||
207 | } | 185 | } | ||
208 | 186 | | |||
209 | #include "TestProxyLogger.moc" | 187 | #include "TestLogger.moc" |