| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2007 Boudewijn Rempt boud @valdyas.org |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
// clazy:excludeall=qstring-arg |
| 7 |
#include "KoProgressUpdater_test.h" |
| 8 |
|
| 9 |
#include "KoProgressUpdater.h" |
| 10 |
#include "KoUpdater.h" |
| 11 |
#include <QThread> |
| 12 |
|
| 13 |
#include <ThreadWeaver/ThreadWeaver> |
| 14 |
|
| 15 |
#include <QTest> |
| 16 |
|
| 17 |
// KoProgressUpdater signal timer has interval of 250 ms, see PROGRESSUPDATER_GUITIMERINTERVAL |
| 18 |
// Also is the timer of type Qt::CoarseTimer, which "tr[ies] to keep accuracy within 5% of the desired interval", |
| 19 |
// so wait a little longer |
| 20 |
#define WAIT_FOR_PROGRESSUPDATER_UI_UPDATES 275 |
| 21 |
|
| 22 |
class TestWeaverJob : public ThreadWeaver::Job |
| 23 |
{ |
| 24 |
public: |
| 25 |
|
| 26 |
TestWeaverJob(QPointer<KoUpdater> updater, int steps = 10) |
| 27 |
: ThreadWeaver::Job() |
| 28 |
, m_updater(updater) |
| 29 |
, m_steps(steps) |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
void run(ThreadWeaver::JobPointer, ThreadWeaver::Thread *) override |
| 34 |
{ |
| 35 |
for (int i = 1; i < m_steps + 1; ++i) { |
| 36 |
for (int j = 1; j < 10000; ++j){} |
| 37 |
m_updater->setProgress((100 / m_steps) * i); |
| 38 |
if (m_updater->interrupted()) { |
| 39 |
m_updater->setProgress(100); |
| 40 |
return; |
| 41 |
} |
| 42 |
} |
| 43 |
m_updater->setProgress(100); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
protected: |
| 48 |
QPointer<KoUpdater> m_updater; |
| 49 |
int m_steps; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
class TestJob : public QThread { |
| 54 |
Q_OBJECT |
| 55 |
public: |
| 56 |
|
| 57 |
TestJob(QPointer<KoUpdater> updater, int steps = 10) |
| 58 |
: QThread() |
| 59 |
, m_updater(updater) |
| 60 |
, m_steps(steps) |
| 61 |
{ |
| 62 |
} |
| 63 |
|
| 64 |
void run() override |
| 65 |
{ |
| 66 |
for (int i = 1; i < m_steps + 1; ++i) { |
| 67 |
sleep(1); |
| 68 |
m_updater->setProgress((100 / m_steps) * i); |
| 69 |
if (m_updater->interrupted()) { |
| 70 |
m_updater->setProgress(100); |
| 71 |
return; |
| 72 |
} |
| 73 |
} |
| 74 |
m_updater->setProgress(100); |
| 75 |
} |
| 76 |
|
| 77 |
private: |
| 78 |
|
| 79 |
QPointer<KoUpdater> m_updater; |
| 80 |
int m_steps; |
| 81 |
}; |
| 82 |
|
| 83 |
class TestProgressBar : public KoProgressProxy |
| 84 |
{ |
| 85 |
|
| 86 |
public: |
| 87 |
|
| 88 |
int min; |
| 89 |
int max; |
| 90 |
int value; |
| 91 |
QString formatString; |
| 92 |
|
| 93 |
TestProgressBar() |
| 94 |
: min(0) |
| 95 |
, max(0) |
| 96 |
, value(0) |
| 97 |
{ |
| 98 |
} |
| 99 |
|
| 100 |
int maximum() const override |
| 101 |
{ |
| 102 |
return max; |
| 103 |
} |
| 104 |
|
| 105 |
void setValue(int v) override |
| 106 |
{ |
| 107 |
value = v; |
| 108 |
} |
| 109 |
|
| 110 |
void setRange(int minimum, int maximum) override |
| 111 |
{ |
| 112 |
min = minimum; |
| 113 |
max = maximum; |
| 114 |
} |
| 115 |
|
| 116 |
void setFormat(const QString & format) override |
| 117 |
{ |
| 118 |
formatString = format; |
| 119 |
} |
| 120 |
|
| 121 |
}; |
| 122 |
|
| 123 |
void KoProgressUpdaterTest::testCreation() |
| 124 |
{ |
| 125 |
TestProgressBar bar; |
| 126 |
KoProgressUpdater pu(&bar); |
| 127 |
QPointer<KoUpdater> updater = pu.startSubtask(); |
| 128 |
QCOMPARE(bar.min, 0); |
| 129 |
QCOMPARE(bar.max, 0); |
| 130 |
QCOMPARE(bar.value, 0); |
| 131 |
QVERIFY(bar.formatString.isNull()); |
| 132 |
pu.start(); |
| 133 |
QCOMPARE(bar.min, 0); |
| 134 |
QCOMPARE(bar.max, 99); |
| 135 |
QCOMPARE(bar.value, 0); |
| 136 |
} |
| 137 |
|
| 138 |
void KoProgressUpdaterTest::testSimpleProgress() |
| 139 |
{ |
| 140 |
TestProgressBar bar; |
| 141 |
KoProgressUpdater pu(&bar); |
| 142 |
pu.start(); |
| 143 |
QPointer<KoUpdater> updater = pu.startSubtask(); |
| 144 |
updater->setProgress(50); |
| 145 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 146 |
QCOMPARE(bar.value, 50); |
| 147 |
updater->setProgress(100); |
| 148 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 149 |
QCOMPARE(bar.value, 100); |
| 150 |
} |
| 151 |
void KoProgressUpdaterTest::testSimpleThreadedProgress() |
| 152 |
{ |
| 153 |
TestProgressBar bar; |
| 154 |
KoProgressUpdater pu(&bar); |
| 155 |
pu.start(); |
| 156 |
QPointer<KoUpdater> u = pu.startSubtask(); |
| 157 |
TestJob t(u); |
| 158 |
t.start(); |
| 159 |
while (!t.isFinished()) { |
| 160 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 161 |
} |
| 162 |
for (int i = 0; i < 10 && bar.value < 100; ++i) { |
| 163 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 164 |
} |
| 165 |
QCoreApplication::processEvents(); // allow the actions 'gui' stuff to run. |
| 166 |
QCOMPARE(bar.value, 100); |
| 167 |
} |
| 168 |
|
| 169 |
void KoProgressUpdaterTest::testSubUpdaters() |
| 170 |
{ |
| 171 |
TestProgressBar bar; |
| 172 |
KoProgressUpdater pu(&bar); |
| 173 |
pu.start(); |
| 174 |
QPointer<KoUpdater> u1 = pu.startSubtask(4); |
| 175 |
QPointer<KoUpdater> u2 = pu.startSubtask(6); |
| 176 |
u1->setProgress(100); |
| 177 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 178 |
QCoreApplication::processEvents(); // allow the actions 'gui' stuff to run. |
| 179 |
QCOMPARE(bar.value, 40); |
| 180 |
u2->setProgress(100); |
| 181 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 182 |
QCoreApplication::processEvents(); // allow the actions 'gui' stuff to run. |
| 183 |
QCOMPARE(bar.value, 100); |
| 184 |
} |
| 185 |
|
| 186 |
void KoProgressUpdaterTest::testThreadedSubUpdaters() |
| 187 |
{ |
| 188 |
TestProgressBar bar; |
| 189 |
KoProgressUpdater pu(&bar); |
| 190 |
pu.start(); |
| 191 |
QPointer<KoUpdater> u1 = pu.startSubtask(4); |
| 192 |
QPointer<KoUpdater> u2= pu.startSubtask(6); |
| 193 |
|
| 194 |
TestJob t1(u1, 4); |
| 195 |
TestJob t2(u2, 6); |
| 196 |
t1.start(); |
| 197 |
t2.start(); |
| 198 |
while (t1.isRunning() || t2.isRunning()) { |
| 199 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 200 |
} |
| 201 |
for (int i = 0; i < 10 && bar.value < 100; ++i) { |
| 202 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 203 |
} |
| 204 |
QCOMPARE(bar.value, 100); |
| 205 |
} |
| 206 |
|
| 207 |
void KoProgressUpdaterTest::testRecursiveProgress() |
| 208 |
{ |
| 209 |
TestProgressBar bar; |
| 210 |
KoProgressUpdater pu(&bar); |
| 211 |
pu.start(); |
| 212 |
QPointer<KoUpdater> u1 = pu.startSubtask(); |
| 213 |
|
| 214 |
KoProgressUpdater pu2(u1); |
| 215 |
pu2.start(); |
| 216 |
QPointer<KoUpdater> u2 = pu2.startSubtask(); |
| 217 |
u2->setProgress(50); |
| 218 |
u2->setProgress(100); |
| 219 |
for (int i = 0; i < 10 && bar.value < 100; ++i) { |
| 220 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 221 |
} |
| 222 |
QCOMPARE(bar.value, 100); |
| 223 |
} |
| 224 |
|
| 225 |
void KoProgressUpdaterTest::testThreadedRecursiveProgress() |
| 226 |
{ |
| 227 |
TestProgressBar bar; |
| 228 |
KoProgressUpdater pu(&bar); |
| 229 |
pu.start(); |
| 230 |
QPointer<KoUpdater> u1 = pu.startSubtask(); |
| 231 |
|
| 232 |
KoProgressUpdater pu2(u1); |
| 233 |
pu2.start(); |
| 234 |
QPointer<KoUpdater> u2 = pu2.startSubtask(); |
| 235 |
|
| 236 |
TestJob t1(u2); |
| 237 |
t1.start(); |
| 238 |
|
| 239 |
while (t1.isRunning()) { |
| 240 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 241 |
} |
| 242 |
for (int i = 0; i < 10 && bar.value < 100; ++i) { |
| 243 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 244 |
} |
| 245 |
QCOMPARE(bar.value, 100); |
| 246 |
} |
| 247 |
|
| 248 |
void KoProgressUpdaterTest::testFromWeaver() |
| 249 |
{ |
| 250 |
jobsdone = 0; |
| 251 |
|
| 252 |
TestProgressBar bar; |
| 253 |
KoProgressUpdater pu(&bar); |
| 254 |
pu.start(10); |
| 255 |
ThreadWeaver::Queue::instance()->setMaximumNumberOfThreads(4); |
| 256 |
for (int i = 0; i < 10; ++i) { |
| 257 |
QPointer<KoUpdater> up = pu.startSubtask(); |
| 258 |
ThreadWeaver::QObjectDecorator * job = new ThreadWeaver::QObjectDecorator(new TestWeaverJob(up, 10)); |
| 259 |
connect(job, SIGNAL(done(ThreadWeaver::JobPointer)), SLOT(jobDone(ThreadWeaver::JobPointer))); |
| 260 |
ThreadWeaver::Queue::instance()->enqueue(ThreadWeaver::make_job_raw(job)); |
| 261 |
} |
| 262 |
while (!ThreadWeaver::Queue::instance()->isIdle()) { |
| 263 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); |
| 264 |
} |
| 265 |
ThreadWeaver::Queue::instance()->finish(); |
| 266 |
for (int i = 0; i < 10 && jobsdone < 10; ++i) { |
| 267 |
QTest::qWait(WAIT_FOR_PROGRESSUPDATER_UI_UPDATES); // allow the action to do its job. |
| 268 |
} |
| 269 |
QCOMPARE(jobsdone, 10); |
| 270 |
} |
| 271 |
|
| 272 |
void KoProgressUpdaterTest::jobDone(ThreadWeaver::JobPointer job) |
| 273 |
{ |
| 274 |
Q_UNUSED(job); |
| 275 |
++jobsdone; |
| 276 |
} |
| 277 |
|
| 278 |
QTEST_GUILESS_MAIN(KoProgressUpdaterTest) |
| 279 |
#include "KoProgressUpdater_test.moc" |