File indexing completed on 2024-05-19 04:32:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KISTEST
0008 #define KISTEST
0009 
0010 #include <KoTestConfig.h>
0011 #include <QApplication>
0012 #include <simpletest.h>
0013 #include <QStandardPaths>
0014 #include <QLoggingCategory>
0015 #include <QtTest/qtestsystem.h>
0016 #include <set>
0017 #include <QLocale>
0018 #include <KisSynchronizedConnection.h>
0019 
0020 /**
0021  * There is a hierarchy of libraries built on the kritaresources library
0022  * that provide resources:
0023  *
0024  * pigment: kocolorset, kosegmentgradient, kostopgradient, kopattern
0025  *   flake: koseexprscript, kogamutmask, kosvgsymbolcollection
0026  *     image: kispaintoppreset, kispsdlayerstyle
0027  *       brush: kisgbrbrush, kisimagepipebrush, kissvgbrush, kispngbrush
0028  *         ui: kiswindowloyout, kissession, kisworkspace
0029  *
0030  * Depending on which library the test links again, it should use
0031  *
0032  *  testresources.h
0033  *  testpigment.h
0034  *  testflake.h
0035  *  testimage.h
0036  *  testbrush.h
0037  *  testui.h
0038  *
0039  * To get the right KISTEST_MAIN for the resources it needs access to.
0040  *
0041  * This means that adding a new resource means not only adding it in
0042  * KisApplication, but also this file.
0043  */
0044 
0045 
0046 
0047 #if defined(QT_NETWORK_LIB)
0048 #  include <QtTest/qtest_network.h>
0049 #endif
0050 #include <QtTest/qtest_widgets.h>
0051 
0052 #ifdef QT_KEYPAD_NAVIGATION
0053 #  define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
0054 #else
0055 #  define QTEST_DISABLE_KEYPAD_NAVIGATION
0056 #endif
0057 
0058 #if defined(TESTRESOURCES) || defined(TESTPIGMENT) || defined (TESTFLAKE) || defined(TESTBRUSH) || defined(TESTIMAGE) || defined(TESTUI)
0059 #include <QImageReader>
0060 #include <QList>
0061 #include <QByteArray>
0062 #include <QStringList>
0063 #include <QStandardPaths>
0064 #include <QString>
0065 #include <QDir>
0066 #include <QStandardPaths>
0067 #include <QImageWriter>
0068 
0069 #include <KisResourceTypes.h>
0070 #include <KisResourceLoaderRegistry.h>
0071 #include <KisMimeDatabase.h>
0072 #include <KisResourceLoader.h>
0073 #include <KisResourceCacheDb.h>
0074 #include <KisResourceLocator.h>
0075 #include <KoResourcePaths.h>
0076 
0077 
0078 
0079 #if defined(TESTRESOURCES) || defined(TESTPIGMENT) || defined (TESTFLAKE) || defined(TESTIMAGE) || defined(TESTBRUSH) || defined(TESTUI)
0080 #include <resources/KoSegmentGradient.h>
0081 #include <resources/KoStopGradient.h>
0082 #include <resources/KoColorSet.h>
0083 #include <resources/KoPattern.h>
0084 #endif
0085 
0086 #if defined (TESTFLAKE) || defined(TESTIMAGE) || defined(TESTBRUSH) || defined(TESTUI)
0087 #if defined HAVE_SEEXPR
0088 #include <KisSeExprScript.h>
0089 #endif
0090 #include <resources/KoGamutMask.h>
0091 #include <resources/KoSvgSymbolCollectionResource.h>
0092 #endif
0093 
0094 #if defined(TESTIMAGE) || defined(TESTBRUSH) || defined(TESTUI)
0095 #include <kis_paintop_preset.h>
0096 #include <kis_psd_layer_style.h>
0097 #endif
0098 
0099 #if defined(TESTBRUSH) || defined(TESTUI)
0100 #include <kis_gbr_brush.h>
0101 #include <kis_imagepipe_brush.h>
0102 #include <kis_svg_brush.h>
0103 #include <kis_png_brush.h>
0104 #endif
0105 
0106 #if defined(TESTUI)
0107 #include <KisWindowLayoutResource.h>
0108 #include <kis_workspace_resource.h>
0109 #include <KisSessionResource.h>
0110 #endif
0111 
0112 namespace {
0113 
0114 void addResourceTypes()
0115 {
0116 #if defined(TESTRESOURCES) || defined(TESTPIGMENT) || defined (TESTFLAKE) || defined(TESTIMAGE) || defined(TESTBRUSH) || defined(TESTUI)
0117     // All Krita's resource types
0118     KoResourcePaths::addAssetType("markers", "data", "/styles/");
0119     KoResourcePaths::addAssetType("kis_pics", "data", "/pics/");
0120     KoResourcePaths::addAssetType("kis_images", "data", "/images/");
0121     KoResourcePaths::addAssetType("metadata_schema", "data", "/metadata/schemas/");
0122     KoResourcePaths::addAssetType("gmic_definitions", "data", "/gmic/");
0123     KoResourcePaths::addAssetType("kis_defaultpresets", "data", "/defaultpresets/");
0124     KoResourcePaths::addAssetType("psd_layer_style_collections", "data", "/asl");
0125     KoResourcePaths::addAssetType("kis_shortcuts", "data", "/shortcuts/");
0126     KoResourcePaths::addAssetType("kis_actions", "data", "/actions");
0127     KoResourcePaths::addAssetType("kis_actions", "data", "/pykrita");
0128     KoResourcePaths::addAssetType("icc_profiles", "data", "/color/icc");
0129     KoResourcePaths::addAssetType("icc_profiles", "data", "/profiles/");
0130     KoResourcePaths::addAssetType("tags", "data", "/tags/");
0131     KoResourcePaths::addAssetType("templates", "data", "/templates");
0132     KoResourcePaths::addAssetType("pythonscripts", "data", "/pykrita");
0133     KoResourcePaths::addAssetType("preset_icons", "data", "/preset_icons");
0134 
0135     // Make directories for all resources we can save, and tags
0136     QDir d;
0137     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tags/");
0138     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/asl/");
0139     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/bundles/");
0140     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/brushes/");
0141     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/gradients/");
0142     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/paintoppresets/");
0143     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/palettes/");
0144     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/patterns/");
0145     // between 4.2.x and 4.3.0 there was a change from 'taskset' to 'tasksets'
0146     // so to make older resource folders compatible with the new version, let's rename the folder
0147     // so no tasksets are lost.
0148     if (d.exists(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/taskset/")) {
0149         d.rename(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/taskset/",
0150                  QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tasksets/");
0151     }
0152     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tasksets/");
0153     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/workspaces/");
0154     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/input/");
0155     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/pykrita/");
0156     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/symbols/");
0157     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/color-schemes/");
0158     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/preset_icons/");
0159     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/preset_icons/tool_icons/");
0160     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/preset_icons/emblem_icons/");
0161     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/gamutmasks/");
0162 #if defined HAVE_SEEXPR
0163     d.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/seexpr_scripts/");
0164 #endif
0165 #endif
0166 
0167 }
0168 
0169 void registerResources()
0170 {
0171 
0172 #if defined(TESTRESOURCES) || defined(TESTPIGMENT) || defined (TESTFLAKE) || defined(TESTIMAGE) || defined(TESTBRUSH) || defined(TESTUI)
0173 
0174     QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
0175     if (dir.exists("resourcecache.sqlite")) {
0176         bool result = dir.removeRecursively();
0177         qDebug() << "result" << (result);
0178     }
0179 
0180     addResourceTypes();
0181 
0182     KisResourceLoaderRegistry *reg = KisResourceLoaderRegistry::instance();
0183 
0184     QList<QByteArray> src = QImageReader::supportedMimeTypes();
0185     QStringList allImageMimes;
0186     Q_FOREACH(const QByteArray ba, src) {
0187         if (QImageWriter::supportedMimeTypes().contains(ba)) {
0188             allImageMimes << QString::fromUtf8(ba);
0189         }
0190     }
0191     allImageMimes << KisMimeDatabase::mimeTypeForSuffix("pat");
0192 
0193     reg->add(new KisResourceLoader<KoPattern>(ResourceType::Patterns, ResourceType::Patterns, i18n("Patterns"), allImageMimes));
0194     reg->add(new KisResourceLoader<KoSegmentGradient>(ResourceSubType::SegmentedGradients, ResourceType::Gradients, i18n("Gradients"), QStringList() << "application/x-gimp-gradient"));
0195     reg->add(new KisResourceLoader<KoStopGradient>(ResourceSubType::StopGradients, ResourceType::Gradients, i18n("Gradients"), QStringList() << "image/svg+xml"));
0196 
0197     reg->add(new KisResourceLoader<KoColorSet>(ResourceType::Palettes, ResourceType::Palettes, i18n("Palettes"),
0198                                      QStringList() << KisMimeDatabase::mimeTypeForSuffix("kpl")
0199                                                << KisMimeDatabase::mimeTypeForSuffix("gpl")
0200                                                << KisMimeDatabase::mimeTypeForSuffix("pal")
0201                                                << KisMimeDatabase::mimeTypeForSuffix("act")
0202                                                << KisMimeDatabase::mimeTypeForSuffix("aco")
0203                                                << KisMimeDatabase::mimeTypeForSuffix("css")
0204                                                << KisMimeDatabase::mimeTypeForSuffix("colors")
0205                                                << KisMimeDatabase::mimeTypeForSuffix("xml")
0206                                                << KisMimeDatabase::mimeTypeForSuffix("sbz")));
0207 #endif
0208 
0209 #if defined (TESTFLAKE) || defined(TESTIMAGE) || defined(TESTBRUSH) || defined(TESTUI)
0210 #if defined HAVE_SEEXPR
0211     reg->add(new KisResourceLoader<KisSeExprScript>(ResourceType::SeExprScripts, ResourceType::SeExprScripts, i18n("SeExpr Scripts"), QStringList() << "application/x-krita-seexpr-script"));
0212 #endif
0213     reg->add(new KisResourceLoader<KoGamutMask>(ResourceType::GamutMasks, ResourceType::GamutMasks, i18n("Gamut masks"), QStringList() << "application/x-krita-gamutmasks"));
0214     reg->add(new KisResourceLoader<KoSvgSymbolCollectionResource>(ResourceType::Symbols, ResourceType::Symbols, i18n("SVG symbol libraries"), QStringList() << "image/svg+xml"));
0215 #endif
0216 
0217 
0218 #if defined(TESTIMAGE) || defined(TESTBRUSH) || defined(TESTUI)
0219      reg->add(new KisResourceLoader<KisPaintOpPreset>(ResourceType::PaintOpPresets, ResourceType::PaintOpPresets, i18n("Brush presets"), QStringList() << "application/x-krita-paintoppreset"));
0220      reg->add(new KisResourceLoader<KisPSDLayerStyle>(ResourceType::LayerStyles,
0221                                                      ResourceType::LayerStyles,
0222                                                      ResourceType::LayerStyles,
0223                                                      QStringList() << "application/x-photoshop-style"));
0224 #endif
0225 
0226 #if defined(TESTBRUSH) || defined(TESTUI)
0227 
0228     reg->add(new KisResourceLoader<KisGbrBrush>(ResourceSubType::GbrBrushes, ResourceType::Brushes, i18n("Brush tips"), QStringList() << "image/x-gimp-brush"));
0229     reg->add(new KisResourceLoader<KisImagePipeBrush>(ResourceSubType::GihBrushes, ResourceType::Brushes, i18n("Brush tips"), QStringList() << "image/x-gimp-brush-animated"));
0230     reg->add(new KisResourceLoader<KisSvgBrush>(ResourceSubType::SvgBrushes, ResourceType::Brushes, i18n("Brush tips"), QStringList() << "image/svg+xml"));
0231     reg->add(new KisResourceLoader<KisPngBrush>(ResourceSubType::PngBrushes, ResourceType::Brushes, i18n("Brush tips"), QStringList() << "image/png"));
0232 
0233 #endif
0234 
0235 #if defined(TESTUI)
0236     reg->add(new KisResourceLoader<KisWindowLayoutResource>(ResourceType::WindowLayouts, ResourceType::WindowLayouts, i18n("Window layouts"), QStringList() << "application/x-krita-windowlayout"));
0237     reg->add(new KisResourceLoader<KisSessionResource>(ResourceType::Sessions, ResourceType::Sessions, i18n("Sessions"), QStringList() << "application/x-krita-session"));
0238     reg->add(new KisResourceLoader<KisWorkspaceResource>(ResourceType::Workspaces, ResourceType::Workspaces, i18n("Workspaces"), QStringList() << "application/x-krita-workspace"));
0239 #endif
0240 
0241 #if defined(TESTRESOURCES) || defined(TESTPIGMENT) || defined (TESTFLAKE) || defined(TESTBRUSH) || defined(TESTIMAGE) || defined(TESTUI)
0242     if (!KisResourceCacheDb::initialize(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation))) {
0243         qFatal("Could not initialize the resource cachedb");
0244     }
0245 
0246     KisResourceLocator::instance()->initialize(KoResourcePaths::getApplicationRoot() + "/share/krita");
0247 #endif
0248 
0249 }
0250 
0251 #define KISTEST_MAIN(TestObject) \
0252 int main(int argc, char *argv[]) \
0253 { \
0254     qputenv("LANGUAGE", "en"); \
0255     QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates)); \
0256     qputenv("QT_LOGGING_RULES", ""); \
0257     QStandardPaths::setTestModeEnabled(true); \
0258     qputenv("EXTRA_RESOURCE_DIRS", QByteArray(KRITA_RESOURCE_DIRS_FOR_TESTS)); \
0259     qputenv("KRITA_PLUGIN_PATH", QByteArray(KRITA_PLUGINS_DIR_FOR_TESTS)); \
0260     QApplication app(argc, argv); \
0261     app.setAttribute(Qt::AA_Use96Dpi, true); \
0262     QTEST_DISABLE_KEYPAD_NAVIGATION \
0263     registerResources(); \
0264     TestObject tc; \
0265     QTEST_SET_MAIN_SOURCE_PATH \
0266     return QTest::qExec(&tc, argc, argv); \
0267 }
0268 
0269 }
0270 #else
0271 #define KISTEST_MAIN(TestObject) \
0272 int main(int argc, char *argv[]) \
0273 { \
0274     qputenv("LANGUAGE", "en"); \
0275     QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates)); \
0276     qputenv("QT_LOGGING_RULES", ""); \
0277     qputenv("EXTRA_RESOURCE_DIRS", QByteArray(KRITA_RESOURCE_DIRS_FOR_TESTS)); \
0278     qputenv("KRITA_PLUGIN_PATH", QByteArray(KRITA_PLUGINS_DIR_FOR_TESTS)); \
0279     KisSynchronizedConnectionBase::setAutoModeForUnittestsEnabled(true); \
0280     QStandardPaths::setTestModeEnabled(true); \
0281     QApplication app(argc, argv); \
0282     app.setAttribute(Qt::AA_Use96Dpi, true); \
0283     QTEST_DISABLE_KEYPAD_NAVIGATION \
0284     TestObject tc; \
0285     QTEST_SET_MAIN_SOURCE_PATH \
0286     return QTest::qExec(&tc, argc, argv); \
0287 }
0288 #endif
0289 
0290 
0291 #endif