File indexing completed on 2024-06-09 04:23:54
0001 /* 0002 * SPDX-FileCopyrightText: 2017 Boudewijn Rempt <boud@valdyas.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "TestResourceStorage.h" 0008 0009 #include <simpletest.h> 0010 0011 #include <QImage> 0012 #include <QPainter> 0013 #include <QUuid> 0014 0015 #include <ksharedconfig.h> 0016 #include <kconfiggroup.h> 0017 0018 #include <KoMD5Generator.h> 0019 #include <KoPattern.h> 0020 0021 #include "DummyResource.h" 0022 #include "ResourceTestHelper.h" 0023 #include "KisResourceStorage.h" 0024 #include "KisResourceLocator.h" 0025 #include "KisResourceTypes.h" 0026 0027 #ifndef FILES_DATA_DIR 0028 #error "FILES_DATA_DIR not set. A directory with the data used for testing installing resources" 0029 #endif 0030 0031 0032 void TestResourceStorage::initTestCase() 0033 { 0034 m_dstLocation = ResourceTestHelper::filesDestDir(); 0035 ResourceTestHelper::cleanDstLocation(m_dstLocation); 0036 QDir().mkpath(m_dstLocation); 0037 KConfigGroup cfg(KSharedConfig::openConfig(), ""); 0038 cfg.writeEntry(KisResourceLocator::resourceLocationKey, m_dstLocation); 0039 m_locator = KisResourceLocator::instance(); 0040 ResourceTestHelper::createDummyLoaderRegistry(); 0041 } 0042 0043 void TestResourceStorage ::testStorage() 0044 { 0045 { 0046 KisResourceStorage storage(QString(FILES_DATA_DIR)); 0047 QVERIFY(storage.type() == KisResourceStorage::StorageType::Folder); 0048 QVERIFY(storage.valid()); 0049 } 0050 0051 { 0052 KisResourceStorage storage(QString(FILES_DATA_DIR) + "/bundles/test1.bundle"); 0053 QVERIFY(storage.type() == KisResourceStorage::StorageType::Bundle); 0054 QVERIFY(storage.valid()); 0055 } 0056 0057 { 0058 KisResourceStorage storage(QString(FILES_DATA_DIR) + "/bundles/test2.bundle"); 0059 QVERIFY(storage.type() == KisResourceStorage::StorageType::Bundle); 0060 QVERIFY(storage.valid()); 0061 } 0062 0063 { 0064 KisResourceStorage storage(QUuid().toString()); 0065 QVERIFY(storage.type() == KisResourceStorage::StorageType::Memory); 0066 QVERIFY(storage.valid()); 0067 } 0068 0069 { 0070 KisResourceStorage storage("Just a random storage"); 0071 QVERIFY(storage.type() == KisResourceStorage::StorageType::Memory); 0072 QVERIFY(storage.valid()); 0073 } 0074 0075 { 0076 KisResourceStorage storage(""); 0077 QVERIFY(storage.type() == KisResourceStorage::StorageType::Unknown); 0078 QVERIFY(!storage.valid()); 0079 } 0080 } 0081 0082 void TestResourceStorage::testImportExportResource() 0083 { 0084 QImage img(256, 256, QImage::Format_ARGB32); 0085 QPainter gc(&img); 0086 gc.fillRect(0, 0, 256, 256, Qt::red); 0087 img.save("testpattern.png"); 0088 0089 QByteArray ba; 0090 0091 { 0092 QFile f("testpattern.png"); 0093 f.open(QFile::ReadOnly); 0094 ba = f.readAll(); 0095 f.close(); 0096 } 0097 0098 const QString md5 = KoMD5Generator::generateHash(ba); 0099 0100 { 0101 QDir().mkpath(m_dstLocation + "/" + ResourceType::Patterns); 0102 KisResourceStorage folderStorage(m_dstLocation); 0103 0104 QFile f("testpattern.png"); 0105 f.open(QFile::ReadOnly); 0106 bool r = folderStorage.importResource("patterns/testpattern.png", &f); 0107 QVERIFY(r); 0108 QCOMPARE(md5, folderStorage.resourceMd5("patterns/testpattern.png")); 0109 0110 QBuffer buffer; 0111 buffer.open(QIODevice::WriteOnly); 0112 r = folderStorage.exportResource("patterns/testpattern.png", &buffer); 0113 QVERIFY(r); 0114 buffer.close(); 0115 QCOMPARE(KoMD5Generator::generateHash(buffer.data()), md5); 0116 } 0117 0118 { 0119 QFile f("testpattern.png"); 0120 f.open(QFile::ReadOnly); 0121 KisResourceStorage memoryStorage("memory"); 0122 bool r = memoryStorage.importResource("patterns/testpattern.png", &f); 0123 QVERIFY(r); 0124 QCOMPARE(md5, memoryStorage.resourceMd5("patterns/testpattern.png")); 0125 0126 QBuffer buffer; 0127 buffer.open(QIODevice::WriteOnly); 0128 r = memoryStorage.exportResource("patterns/testpattern.png", &buffer); 0129 QVERIFY(r); 0130 buffer.close(); 0131 QCOMPARE(KoMD5Generator::generateHash(buffer.data()), md5); 0132 } 0133 0134 { 0135 QFile f("testpattern.png"); 0136 f.open(QFile::ReadOnly); 0137 KisResourceStorage bundleStorage(QString(FILES_DATA_DIR) + "/bundles/test1.bundle"); 0138 bool r = bundleStorage.importResource("patterns/testpattern.png", &f); 0139 QVERIFY(!r); 0140 0141 QBuffer buffer; 0142 buffer.open(QIODevice::WriteOnly); 0143 r = bundleStorage.exportResource("patterns/testpattern.png", &buffer); 0144 QVERIFY(r); 0145 } 0146 } 0147 0148 void TestResourceStorage::testAddResource() 0149 { 0150 QDir().mkpath(m_dstLocation + "/" + ResourceType::Patterns); 0151 KisResourceStorage folderStorage(m_dstLocation); 0152 0153 QImage img(256, 256, QImage::Format_ARGB32); 0154 QPainter gc(&img); 0155 gc.fillRect(0, 0, 256, 256, Qt::red); 0156 0157 KoResourceSP res(new KoPattern(img, "testpattern2", "testpattern2.png")); 0158 Q_ASSERT(res->resourceType().first == ResourceType::Patterns); 0159 bool r = folderStorage.addResource(res); 0160 QVERIFY(r); 0161 } 0162 0163 void TestResourceStorage::testStorageVersioningHelperCounting() 0164 { 0165 // create the resource 0166 QDir().mkpath(m_dstLocation + "/" + ResourceType::Patterns); 0167 0168 QImage img(256, 256, QImage::Format_ARGB32); 0169 QPainter gc(&img); 0170 gc.fillRect(0, 0, 256, 256, Qt::red); 0171 KoResourceSP res(new KoPattern(img, "testpattern", "testpattern.png")); 0172 Q_ASSERT(res->resourceType().first == ResourceType::Patterns); 0173 0174 // function that returns false for everything 0175 auto noResourcesExisting = [] (QString a) {Q_UNUSED(a); return false;}; 0176 QString resNewFilename = KisStorageVersioningHelper::chooseUniqueName(res, 0, noResourcesExisting); 0177 //QCOMPARE(resNewFilename, "testpattern.png"); 0178 QCOMPARE(resNewFilename, "testpattern.0000.png"); 0179 0180 // function that returns true for the same resource but false for everything else 0181 auto onlyFirstVersionExists = [] (QString a) { 0182 return a == "testpattern.png" || a == "testpattern.0000.png"; 0183 }; 0184 resNewFilename = KisStorageVersioningHelper::chooseUniqueName(res, 0, onlyFirstVersionExists); 0185 QCOMPARE(resNewFilename, "testpattern.0001.png"); 0186 0187 // function that returns true for first 10 versions of the resource but false for everything else 0188 auto firstTenVersionExists = [] (QString a) { 0189 if (a == "testpattern.png") return true; 0190 if (a == "testpattern.0000.png") return true; 0191 if (a == "testpattern.0001.png") return true; 0192 if (a == "testpattern.0002.png") return true; 0193 if (a == "testpattern.0003.png") return true; 0194 if (a == "testpattern.0004.png") return true; 0195 if (a == "testpattern.0005.png") return true; 0196 if (a == "testpattern.0006.png") return true; 0197 if (a == "testpattern.0007.png") return true; 0198 if (a == "testpattern.0008.png") return true; 0199 if (a == "testpattern.0009.png") return true; 0200 if (a == "testpattern.0010.png") return true; 0201 return false; 0202 }; 0203 resNewFilename = KisStorageVersioningHelper::chooseUniqueName(res, 0, firstTenVersionExists); 0204 QCOMPARE(resNewFilename, "testpattern.0011.png"); 0205 0206 } 0207 0208 void TestResourceStorage::cleanupTestCase() 0209 { 0210 ResourceTestHelper::cleanDstLocation(m_dstLocation); 0211 } 0212 0213 SIMPLE_TEST_MAIN(TestResourceStorage) 0214