gwenview: fix document test crashes

the testModifyAndSaveAs test case fails tho

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-03-23 14:17:38 +02:00
parent e02661135c
commit c316adfdbb
3 changed files with 23 additions and 15 deletions

View file

@ -522,7 +522,7 @@ void Document::slotJobFinished(KJob* job)
d->mJobQueue.removeAll(documentJob); d->mJobQueue.removeAll(documentJob);
} }
if (d->mJobQueue.isEmpty()) { if (d->mJobQueue.isEmpty() && d->mCurrentJob.isNull()) {
kDebug() << "All done"; kDebug() << "All done";
busyChanged(d->mUrl, false); busyChanged(d->mUrl, false);
allTasksDone(); allTasksDone();
@ -537,7 +537,7 @@ void Document::slotJobFinished(KJob* job)
bool Document::isBusy() const bool Document::isBusy() const
{ {
return !d->mJobQueue.isEmpty(); return (!d->mCurrentJob.isNull() || !d->mJobQueue.isEmpty());
} }
} // namespace } // namespace

View file

@ -64,9 +64,9 @@ static void waitUntilMetaInfoLoaded(Document::Ptr doc)
} }
} }
static bool waitUntilJobIsDone(DocumentJob* job) static bool waitUntilJobIsDone(const Document* doc, DocumentJob* job)
{ {
JobWatcher watcher(job); JobWatcher watcher(doc, job);
watcher.wait(); watcher.wait();
return watcher.error() == KJob::NoError; return watcher.error() == KJob::NoError;
} }
@ -329,7 +329,7 @@ void DocumentTest::testSaveRemote()
doc->waitUntilLoaded(); doc->waitUntilLoaded();
dstUrl.addPath("testSaveRemote.png"); dstUrl.addPath("testSaveRemote.png");
QVERIFY(waitUntilJobIsDone(doc->save(dstUrl, "png"))); QVERIFY(waitUntilJobIsDone(doc.constData(), doc->save(dstUrl, "png")));
} }
/** /**
@ -405,7 +405,7 @@ void DocumentTest::testSaveAs()
doc->startLoadingFullImage(); doc->startLoadingFullImage();
KUrl destUrl = urlForTestOutputFile("result.png"); KUrl destUrl = urlForTestOutputFile("result.png");
QVERIFY(waitUntilJobIsDone(doc->save(destUrl, "png"))); QVERIFY(waitUntilJobIsDone(doc.constData(), doc->save(destUrl, "png")));
QCOMPARE(doc->format().data(), "png"); QCOMPARE(doc->format().data(), "png");
QCOMPARE(doc->url(), destUrl); QCOMPARE(doc->url(), destUrl);
QCOMPARE(doc->metaInfo()->getValueForKey("General.Name"), destUrl.fileName()); QCOMPARE(doc->metaInfo()->getValueForKey("General.Name"), destUrl.fileName());
@ -441,7 +441,7 @@ void DocumentTest::testLosslessSave()
doc->waitUntilLoaded(); doc->waitUntilLoaded();
KUrl url2 = urlForTestOutputFile("orient1.png"); KUrl url2 = urlForTestOutputFile("orient1.png");
QVERIFY(waitUntilJobIsDone(doc->save(url2, "png"))); QVERIFY(waitUntilJobIsDone(doc.constData(), doc->save(url2, "png")));
QImage image1; QImage image1;
QVERIFY(image1.load(url1.toLocalFile())); QVERIFY(image1.load(url1.toLocalFile()));
@ -471,7 +471,7 @@ void DocumentTest::testLosslessRotate()
// Save it // Save it
KUrl url2 = urlForTestOutputFile("lossless2.png"); KUrl url2 = urlForTestOutputFile("lossless2.png");
waitUntilJobIsDone(doc->save(url2, "png")); waitUntilJobIsDone(doc.constData(), doc->save(url2, "png"));
// Load the saved image // Load the saved image
doc = DocumentFactory::instance()->load(url2); doc = DocumentFactory::instance()->load(url2);
@ -481,7 +481,7 @@ void DocumentTest::testLosslessRotate()
// Rotate the other way // Rotate the other way
QVERIFY(doc->editor()); QVERIFY(doc->editor());
doc->editor()->applyTransformation(ROT_270); doc->editor()->applyTransformation(ROT_270);
waitUntilJobIsDone(doc->save(url2, "png")); waitUntilJobIsDone(doc.constData(), doc->save(url2, "png"));
// Compare the saved images // Compare the saved images
QVERIFY(image1.load(url1.toLocalFile())); QVERIFY(image1.load(url1.toLocalFile()));
@ -533,12 +533,16 @@ void DocumentTest::testModifyAndSaveAs()
QCOMPARE(args.at(0).value<KUrl>(), url); QCOMPARE(args.at(0).value<KUrl>(), url);
// Save it under a new name // Save it under a new name
KUrl destUrl = urlForTestOutputFile("modify.png"); KUrl destUrl = urlForTestOutputFile("modify.jpg");
QVERIFY(waitUntilJobIsDone(doc->save(destUrl, "png"))); QVERIFY(waitUntilJobIsDone(doc.constData(), doc->save(destUrl, "png")));
// Wait a bit because save() will clear the undo stack when back to the // Wait a bit because save() will clear the undo stack when back to the
// event loop // event loop
QTest::qWait(100); QTest::qWait(100); // saved() is emitted asynchronously
QCOMPARE(savedSpy.count(), 1);
args = savedSpy.takeFirst();
QCOMPARE(args.at(0).value<KUrl>(), url);
QCOMPARE(args.at(1).value<KUrl>(), destUrl);
QVERIFY(!doc->isModified()); QVERIFY(!doc->isModified());
QVERIFY(!factory->hasUrl(url)); QVERIFY(!factory->hasUrl(url));

View file

@ -55,8 +55,9 @@ class JobWatcher : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
JobWatcher(KJob* job) JobWatcher(const Gwenview::Document* doc, KJob* job)
: mJob(job) : mDoc(doc)
, mJob(job)
, mError(0) { , mError(0) {
job->setAutoDelete(false); job->setAutoDelete(false);
connect(job, SIGNAL(result(KJob*)), connect(job, SIGNAL(result(KJob*)),
@ -65,7 +66,9 @@ public:
void wait() void wait()
{ {
mJob->exec(); while (mDoc->isBusy()) {
QTest::qWait(100);
}
} }
int error() const int error() const
@ -81,6 +84,7 @@ private Q_SLOTS:
} }
private: private:
const Gwenview::Document* mDoc;
KJob* mJob; KJob* mJob;
int mError; int mError;
}; };