File indexing completed on 2024-12-22 04:12:55

0001 #include "KisSafeDocumentLoaderTest.h"
0002 
0003 #include <testui.h>
0004 
0005 #include "config-limit-long-tests.h"
0006 #include "kis_safe_document_loader.h"
0007 #include "kis_debug.h"
0008 
0009 namespace {
0010 void writeToFile(QFile &file, QColor /*color*/)
0011 {
0012     file.reset();
0013     file.resize(0);
0014     QImage img(10,10,QImage::Format_ARGB32);
0015     img.fill(Qt::black);
0016     img.save(&file, "PNG");
0017     file.flush();
0018 }
0019 
0020 }
0021 
0022 void KisSafeDocumentLoaderTest::test()
0023 {
0024     QTemporaryFile file("safe_loader_test_XXXXXX.png");
0025 
0026     file.open();
0027     writeToFile(file, Qt::black);
0028 
0029     KisSafeDocumentLoader loader(file.fileName());
0030 
0031     QSignalSpy spy(&loader, &KisSafeDocumentLoader::loadingFinished);
0032     QSignalSpy spyFailed(&loader, &KisSafeDocumentLoader::loadingFailed);
0033 
0034     // reloadImage() is synchronous
0035     loader.reloadImage();
0036     QCOMPARE(spy.size(), 1);
0037     QCOMPARE(spyFailed.size(), 0);
0038 
0039     writeToFile(file, Qt::white);
0040 
0041     spy.wait(1500);
0042     QCOMPARE(spy.size(), 2);
0043     QCOMPARE(spyFailed.size(), 0);
0044 
0045     spy.clear();
0046 
0047     file.reset();
0048     file.resize(0);
0049     file.write("blah-blah-try-read-me");
0050     file.flush();
0051 
0052     spyFailed.wait(6000);
0053 
0054     QCOMPARE(spy.size(), 0);
0055     QCOMPARE(spyFailed.size(), 1);
0056 
0057 }
0058 
0059 void KisSafeDocumentLoaderTest::testFileLost()
0060 {
0061     QTemporaryFile file("safe_loader_test_XXXXXX.png");
0062 
0063     file.open();
0064     writeToFile(file, Qt::black);
0065 
0066     KisSafeDocumentLoader loader(file.fileName());
0067 
0068     QSignalSpy spy(&loader, &KisSafeDocumentLoader::loadingFinished);
0069     QSignalSpy spyFailed(&loader, &KisSafeDocumentLoader::loadingFailed);
0070     QSignalSpy spyExistsState(&loader, &KisSafeDocumentLoader::fileExistsStateChanged);
0071 
0072     // reloadImage() is synchronous
0073     loader.reloadImage();
0074     QCOMPARE(spy.size(), 1);
0075     QCOMPARE(spyFailed.size(), 0);
0076     QCOMPARE(spyExistsState.size(), 0);
0077 
0078     spy.clear();
0079 
0080     file.close();
0081     file.remove();
0082 
0083     spyExistsState.wait(15000);
0084     QCOMPARE(spyExistsState.size(), 1);
0085     QCOMPARE(spyExistsState[0][0].toBool(), false);
0086     spyExistsState.clear();
0087 
0088     file.open();
0089     writeToFile(file, Qt::white);
0090 
0091     spyExistsState.wait(3500);
0092     QCOMPARE(spy.size(), 0);
0093     QCOMPARE(spyFailed.size(), 0);
0094     QCOMPARE(spyExistsState.size(), 1);
0095     QCOMPARE(spyExistsState[0][0].toBool(), true);
0096     spyExistsState.clear();
0097 
0098     spy.wait(1500);
0099     QCOMPARE(spy.size(), 1);
0100     QCOMPARE(spyFailed.size(), 0);
0101     QCOMPARE(spyExistsState.size(), 0);
0102     spy.clear();
0103 
0104 
0105     writeToFile(file, Qt::yellow);
0106     spy.wait(1500);
0107     QCOMPARE(spy.size(), 1);
0108     QCOMPARE(spyFailed.size(), 0);
0109     QCOMPARE(spyExistsState.size(), 0);
0110 }
0111 
0112 KISTEST_MAIN(KisSafeDocumentLoaderTest)