File indexing completed on 2024-06-09 04:23:53

0001 /*
0002  * SPDX-FileCopyrightText: 2017 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "TestFolderStorage.h"
0008 #include <simpletest.h>
0009 
0010 #include <kconfiggroup.h>
0011 #include <ksharedconfig.h>
0012 
0013 #include <KritaVersionWrapper.h>
0014 
0015 #include <KisFolderStorage.h>
0016 #include <KoResource.h>
0017 #include <KisResourceCacheDb.h>
0018 
0019 #include <KisResourceLocator.h>
0020 
0021 #include "DummyResource.h"
0022 #include "ResourceTestHelper.h"
0023 
0024 #ifndef FILES_DATA_DIR
0025 #error "FILES_DATA_DIR not set. A directory with the data used for testing installing resources"
0026 #endif
0027 
0028 
0029 void TestFolderStorage::initTestCase()
0030 {
0031     ResourceTestHelper::initTestDb();
0032 
0033     m_srcLocation = QString(FILES_DATA_DIR);
0034     QVERIFY2(QDir(m_srcLocation).exists(), m_srcLocation.toUtf8());
0035 
0036     m_dstLocation = ResourceTestHelper::filesDestDir();
0037     ResourceTestHelper::cleanDstLocation(m_dstLocation);
0038 
0039     KConfigGroup cfg(KSharedConfig::openConfig(), "");
0040     cfg.writeEntry(KisResourceLocator::resourceLocationKey, m_dstLocation);
0041 
0042     m_locator = KisResourceLocator::instance();
0043 
0044     ResourceTestHelper::createDummyLoaderRegistry();
0045 
0046     KisResourceCacheDb::initialize(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
0047     m_locator->initialize(m_srcLocation);
0048 
0049     if (!m_locator->errorMessages().isEmpty()) qDebug() << m_locator->errorMessages();
0050 }
0051 
0052 void TestFolderStorage ::testStorage()
0053 {
0054     KisFolderStorage folderStorage(m_dstLocation);
0055     QSharedPointer<KisResourceStorage::ResourceIterator> iter = folderStorage.resources(ResourceType::Brushes);
0056     QVERIFY(iter->hasNext());
0057     int count = 0;
0058     while (iter->hasNext()) {
0059         iter->next();
0060         qDebug() << iter->url() << iter->type() << iter->lastModified();
0061         count++;
0062     }
0063     QVERIFY(count == 1);
0064 }
0065 
0066 void TestFolderStorage::testTagIterator()
0067 {
0068     KisFolderStorage folderStorage(m_dstLocation);
0069     QSharedPointer<KisResourceStorage::TagIterator> iter = folderStorage.tags(ResourceType::PaintOpPresets);
0070     QVERIFY(iter->hasNext());
0071     int count = 0;
0072     while (iter->hasNext()) {
0073         iter->next();
0074         qDebug() << iter->tag();
0075         count++;
0076     }
0077     QVERIFY(count == 1);
0078 }
0079 
0080 void TestFolderStorage::testAddResource()
0081 {
0082     KoResourceSP resource(new DummyResource("anewresource.kpp", ResourceType::PaintOpPresets));
0083     resource->setValid(true);
0084     resource->setVersion(0);
0085 
0086     KisFolderStorage folderStorage(m_dstLocation);
0087     bool r = folderStorage.saveAsNewVersion(ResourceType::PaintOpPresets, resource);
0088     QVERIFY(r);
0089 
0090     ResourceTestHelper::testVersionedStorage(folderStorage,
0091                                              ResourceType::PaintOpPresets,
0092                                              "paintoppresets/anewresource.0000.kpp",
0093                                              m_dstLocation);
0094     ResourceTestHelper::testVersionedStorageIterator(folderStorage,
0095                                                      ResourceType::PaintOpPresets,
0096                                                      "paintoppresets/anewresource.0000.kpp");
0097 
0098 }
0099 
0100 void TestFolderStorage::testResourceFilePath()
0101 {
0102     KoResourceSP resource(new DummyResource("anewresource.kpp", ResourceType::PaintOpPresets));
0103     resource->setValid(true);
0104     resource->setVersion(0);
0105 
0106     KisFolderStorage folderStorage(m_dstLocation);
0107     bool r = folderStorage.saveAsNewVersion(ResourceType::PaintOpPresets, resource);
0108     QVERIFY(r);
0109 
0110     QCOMPARE(folderStorage.resourceFilePath("paintoppresets/anewresource.0000.kpp"),
0111              QFileInfo(m_dstLocation + "/" + "paintoppresets/anewresource.0000.kpp").absoluteFilePath());
0112 }
0113 
0114 void TestFolderStorage::testResourceCaseSensitivity()
0115 {
0116     KoResourceSP resource(new DummyResource("resourcecasetest.kpp", ResourceType::PaintOpPresets));
0117     resource->setValid(true);
0118     resource->setVersion(0);
0119 
0120     KisFolderStorage folderStorage(m_dstLocation);
0121     bool r = folderStorage.addResource(ResourceType::PaintOpPresets, resource);
0122     QVERIFY(r);
0123 
0124     KoResourceSP res1 = folderStorage.resource("paintoppresets/resourcecasetest.kpp");
0125     QVERIFY(res1);
0126     QCOMPARE(res1->filename(), "resourcecasetest.kpp");
0127 
0128     /**
0129      * In the folder storage we expect all resources to have
0130      * filesystem-dependent name resolution. That is, if we request a resource
0131      * with wrong casing, it should still fetch the resource. But the filename
0132      * field of the resource must contain the correct casing
0133      */
0134 
0135 #if defined Q_OS_WIN || defined Q_OS_MACOS
0136     KoResourceSP res2 = folderStorage.resource("paintoppresets/ReSoUrCeCaSeTeSt.kpp");
0137     QVERIFY(res2);
0138     QCOMPARE(res2->filename(), "resourcecasetest.kpp");
0139 #endif
0140 }
0141 
0142 void TestFolderStorage::cleanupTestCase()
0143 {
0144     ResourceTestHelper::rmTestDb();
0145     ResourceTestHelper::cleanDstLocation(m_dstLocation);
0146 }
0147 
0148 SIMPLE_TEST_MAIN(TestFolderStorage)
0149