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

0001 /*
0002  * SPDX-FileCopyrightText: 2018 boud <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "TestBundleStorage.h"
0007 
0008 #include <simpletest.h>
0009 #include <QImageReader>
0010 
0011 #include <KoConfig.h>
0012 
0013 #include <KisBundleStorage.h>
0014 #include <KoResource.h>
0015 #include "DummyResource.h"
0016 #include "ResourceTestHelper.h"
0017 
0018 #include <kconfiggroup.h>
0019 #include <ksharedconfig.h>
0020 #include <KisResourceLocator.h>
0021 
0022 #ifndef FILES_DATA_DIR
0023 #error "FILES_DATA_DIR not set. A directory with the data used for testing installing resources"
0024 #endif
0025 
0026 
0027 void TestBundleStorage::initTestCase()
0028 {
0029     m_dstLocation = ResourceTestHelper::filesDestDir();
0030     ResourceTestHelper::cleanDstLocation(m_dstLocation);
0031 
0032     QDir().mkpath(m_dstLocation + QString("/bundles"));
0033 
0034     const bool copyResult =
0035         QFile::copy(KRITA_SOURCE_DIR + QString("/krita/data/bundles/Krita_4_Default_Resources.bundle"),
0036                     m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0037 
0038     QVERIFY(copyResult);
0039 
0040     ResourceTestHelper::createDummyLoaderRegistry();
0041 }
0042 
0043 void TestBundleStorage::testMetaData()
0044 {
0045     KisBundleStorage storage(m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0046     QVERIFY(storage.location() == m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0047     QVERIFY(!storage.metaData(KisResourceStorage::s_meta_generator).isNull());
0048     QVERIFY(!storage.metaData(KisResourceStorage::s_meta_author).isNull());
0049     QVERIFY(!storage.metaData(KisResourceStorage::s_meta_description).isNull());
0050     QVERIFY(!storage.metaData(KisResourceStorage::s_meta_initial_creator).isNull());
0051     QVERIFY(!storage.metaData(KisResourceStorage::s_meta_dc_date).isNull());
0052     QVERIFY(!storage.metaData(KisResourceStorage::s_meta_version).isNull());
0053 }
0054 
0055 void TestBundleStorage::testResourceIterator()
0056 {
0057     KisBundleStorage storage(m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0058     QSharedPointer<KisResourceStorage::ResourceIterator> iter = storage.resources(ResourceType::Brushes);
0059     QVERIFY(iter->hasNext());
0060     int count = 0;
0061     while (iter->hasNext()) {
0062         iter->next();
0063         KoResourceSP res = iter->resource();
0064         QVERIFY(res);
0065         count++;
0066     }
0067     QVERIFY(count > 0);
0068 }
0069 
0070 void TestBundleStorage::testTagIterator()
0071 {
0072     KisBundleStorage storage(m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0073     QSharedPointer<KisResourceStorage::TagIterator> iter = storage.tags(ResourceType::PaintOpPresets);
0074     QVERIFY(iter->hasNext());
0075     int count = 0;
0076     while (iter->hasNext()) {
0077         iter->next();
0078         //qDebug() << iter->url() << iter->name() << iter->tag();
0079         count++;
0080     }
0081     QVERIFY(count > 0);
0082 }
0083 
0084 void TestBundleStorage::testResourceItem()
0085 {
0086     KisBundleStorage storage(m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0087     KisResourceStorage::ResourceItem item = storage.resourceItem("paintoppresets/g)_Dry_Brushing.kpp");
0088     QVERIFY(!item.url.isEmpty());
0089 }
0090 
0091 void TestBundleStorage::testResource()
0092 {
0093     KisBundleStorage storage(m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0094     KoResourceSP res = storage.resource("paintoppresets/g)_Dry_Brushing.kpp");
0095     QVERIFY(res);
0096     QVERIFY(res->filename() == "g)_Dry_Brushing.kpp");
0097 }
0098 
0099 void TestBundleStorage::testAddResource()
0100 {
0101 
0102     KisBundleStorage storage(m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0103 
0104     const QString resourceUrl = "paintoppresets/g)_Dry_Brushing.kpp";
0105     const QString resourceType = ResourceType::PaintOpPresets;
0106 
0107     ResourceTestHelper::testVersionedStorage(storage, resourceType, resourceUrl,
0108                                             m_dstLocation +
0109                                              QString("/bundles/Krita_4_Default_Resources.bundle_modified"));
0110 
0111     ResourceTestHelper::testVersionedStorageIterator(storage, resourceType, resourceUrl);
0112 }
0113 
0114 void TestBundleStorage::testResourceCaseSensitivity()
0115 {
0116     KisBundleStorage storage(m_dstLocation + QString("/bundles/Krita_4_Default_Resources.bundle"));
0117 
0118     const QString resourceUrl = "paintoppresets/g)_Dry_Brushing.kpp";
0119     const QString resourceType = ResourceType::PaintOpPresets;
0120 
0121     KoResourceSP resource = storage.resource(resourceUrl);
0122     QVERIFY(resource);
0123 
0124     resource->setVersion(1);
0125 
0126     bool result = storage.saveAsNewVersion(resourceType, resource);
0127     QVERIFY(result);
0128 
0129     /**
0130      * In the bundle storage we expect the new versioned resources to have
0131      * filesystem-dependent name resolution. That is, if we request a resource
0132      * with wrong casing, it should still fetch the resource. But the filename
0133      * field of the resource must contain the correct casing
0134      */
0135 
0136 #if defined Q_OS_WIN || defined Q_OS_MACOS
0137     KoResourceSP res2 = storage.resource("paintoppresets/" + resource->filename().toUpper());
0138     QVERIFY(res2);
0139     QCOMPARE(res2->filename(), resource->filename());
0140 #endif
0141 
0142 }
0143 
0144 void TestBundleStorage::cleanupTestCase()
0145 {
0146     ResourceTestHelper::cleanDstLocation(m_dstLocation);
0147 }
0148 
0149 
0150 
0151 SIMPLE_TEST_MAIN(TestBundleStorage)
0152