File indexing completed on 2024-12-08 10:17:10
0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com> 0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT 0003 0004 // First included header is the public header of the class we are testing; 0005 // this forces the header to be self-contained. 0006 #include "chromalightnessimageparameters.h" 0007 0008 #include "asyncimageprovider.h" 0009 #include "helper.h" 0010 #include "rgbcolorspacefactory.h" 0011 #include <qcolor.h> 0012 #include <qglobal.h> 0013 #include <qimage.h> 0014 #include <qobject.h> 0015 #include <qsharedpointer.h> 0016 #include <qsize.h> 0017 #include <qtest.h> 0018 #include <qtestcase.h> 0019 #include <qtestdata.h> 0020 0021 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 0022 #include <qtmetamacros.h> 0023 #else 0024 #include <qobjectdefs.h> 0025 #include <qstring.h> 0026 #endif 0027 0028 namespace PerceptualColor 0029 { 0030 class RgbColorSpace; 0031 0032 class TestChromaLightnessImageParameters : public QObject 0033 { 0034 Q_OBJECT 0035 0036 public: 0037 explicit TestChromaLightnessImageParameters(QObject *parent = nullptr) 0038 : QObject(parent) 0039 { 0040 } 0041 0042 private: 0043 QSharedPointer<PerceptualColor::RgbColorSpace> m_rgbColorSpace = RgbColorSpaceFactory::createSrgb(); 0044 0045 private Q_SLOTS: 0046 void initTestCase() 0047 { 0048 // Called before the first test function is executed 0049 } 0050 0051 void cleanupTestCase() 0052 { 0053 // Called after the last test function was executed 0054 } 0055 0056 void init() 0057 { 0058 // Called before each test function is executed 0059 } 0060 0061 void cleanup() 0062 { 0063 // Called after every test function 0064 } 0065 0066 void testConstructorDestructor() 0067 { 0068 ChromaLightnessImageParameters test; 0069 } 0070 0071 #ifndef MSVC_DLL 0072 // The automatic export of otherwise private symbols on MSVC 0073 // shared libraries via CMake's WINDOWS_EXPORT_ALL_SYMBOLS property 0074 // does not work well for Qt meta objects. AsyncImageProvider inherits 0075 // from AsyncImageProviderBase, which relies on Qt meta object 0076 // functionality and whose API is private. Therefore, instantiation of 0077 // AsyncImageProvider is not possible, so the following unit tests cannot 0078 // be built for MSVC shared libraries. 0079 0080 void testGetImage0() 0081 { 0082 ChromaLightnessImageParameters m_imageParameters; 0083 m_imageParameters.rgbColorSpace = m_rgbColorSpace; 0084 m_imageParameters.hue = 0; 0085 0086 // Testing extremely small images 0087 0088 AsyncImageProvider<ChromaLightnessImageParameters> m_imageProvider; 0089 m_imageParameters.imageSizePhysical = QSize(0, 0); 0090 m_imageProvider.setImageParameters(m_imageParameters); 0091 m_imageProvider.refreshSync(); 0092 delayedEventProcessing(); 0093 const QImage m_image = m_imageProvider.getCache(); 0094 QCOMPARE(m_image.size(), QSize(0, 0)); 0095 } 0096 0097 void testGetImage1() 0098 { 0099 ChromaLightnessImageParameters m_imageParameters; 0100 m_imageParameters.rgbColorSpace = m_rgbColorSpace; 0101 m_imageParameters.hue = 0; 0102 0103 // Testing extremely small images 0104 0105 AsyncImageProvider<ChromaLightnessImageParameters> m_imageProvider; 0106 m_imageParameters.imageSizePhysical = QSize(1, 1); 0107 m_imageProvider.setImageParameters(m_imageParameters); 0108 m_imageProvider.refreshSync(); 0109 delayedEventProcessing(); 0110 const QImage m_image = m_imageProvider.getCache(); 0111 QCOMPARE(m_image.size(), QSize(1, 1)); 0112 } 0113 0114 void testGetImage2() 0115 { 0116 ChromaLightnessImageParameters m_imageParameters; 0117 m_imageParameters.rgbColorSpace = m_rgbColorSpace; 0118 m_imageParameters.hue = 0; 0119 0120 // Testing extremely small images 0121 0122 AsyncImageProvider<ChromaLightnessImageParameters> m_imageProvider; 0123 m_imageParameters.imageSizePhysical = QSize(2, 2); 0124 m_imageProvider.setImageParameters(m_imageParameters); 0125 m_imageProvider.refreshSync(); 0126 delayedEventProcessing(); 0127 const QImage m_image = m_imageProvider.getCache(); 0128 QCOMPARE(m_image.size(), QSize(2, 2)); 0129 } 0130 0131 void testGetImageInvalid() 0132 { 0133 ChromaLightnessImageParameters m_imageParameters; 0134 m_imageParameters.rgbColorSpace = m_rgbColorSpace; 0135 m_imageParameters.hue = 0; 0136 0137 // Testing extremely small images 0138 0139 AsyncImageProvider<ChromaLightnessImageParameters> m_imageProvider; 0140 m_imageParameters.imageSizePhysical = QSize(-1, -1); 0141 m_imageProvider.setImageParameters(m_imageParameters); 0142 m_imageProvider.refreshSync(); 0143 delayedEventProcessing(); 0144 const QImage m_image = m_imageProvider.getCache(); 0145 QCOMPARE(m_image.size(), QSize(0, 0)); 0146 } 0147 0148 void testGetImageNormalSize() 0149 { 0150 ChromaLightnessImageParameters m_imageParameters; 0151 m_imageParameters.rgbColorSpace = m_rgbColorSpace; 0152 m_imageParameters.hue = 0; 0153 0154 AsyncImageProvider<ChromaLightnessImageParameters> m_imageProvider; 0155 0156 // Start testing for a normal size image 0157 m_imageParameters.imageSizePhysical = QSize(201, 101); 0158 m_imageProvider.setImageParameters(m_imageParameters); 0159 m_imageProvider.refreshSync(); 0160 delayedEventProcessing(); 0161 const QImage m_image = m_imageProvider.getCache(); 0162 QCOMPARE(m_image.height(), 101); 0163 QCOMPARE(m_image.width(), 201); 0164 0165 // Test if position within the QImage is valid: 0166 QCOMPARE(m_image.pixelColor(0, 0).isValid(), true); 0167 0168 // Test if position within the QImage is valid: 0169 QCOMPARE(m_image.pixelColor(0, 100).isValid(), true); 0170 0171 // Test if position within the QImage is invalid: 0172 QTest::ignoreMessage( // 0173 QtWarningMsg, // 0174 "QImage::pixelColor: coordinate (0,101) out of range"); 0175 QCOMPARE(m_image.pixelColor(0, 101).isValid(), false); 0176 } 0177 0178 void testSetHue_data() 0179 { 0180 QTest::addColumn<qreal>("hue"); 0181 QTest::newRow("0") << 0.; 0182 QTest::newRow("20") << 20.; 0183 QTest::newRow("30") << 30.; 0184 QTest::newRow("40") << 40.; 0185 QTest::newRow("50") << 50.; 0186 QTest::newRow("60") << 60.; 0187 QTest::newRow("70") << 70.; 0188 QTest::newRow("80") << 80.; 0189 QTest::newRow("290") << 290.; 0190 } 0191 0192 void testSetHueInvalid() 0193 { 0194 // Make sure that setting an invalid values does not crash. 0195 ChromaLightnessImageParameters m_imageParameters; 0196 m_imageParameters.rgbColorSpace = m_rgbColorSpace; 0197 0198 // Set a non-zero image size: 0199 m_imageParameters.imageSizePhysical = QSize(20, 10); 0200 0201 AsyncImageProvider<ChromaLightnessImageParameters> m_imageProvider; 0202 0203 m_imageParameters.hue = 0; 0204 m_imageProvider.setImageParameters(m_imageParameters); 0205 m_imageProvider.refreshSync(); 0206 Q_UNUSED(m_imageProvider.getCache()) 0207 0208 m_imageParameters.hue = 1; 0209 m_imageProvider.setImageParameters(m_imageParameters); 0210 m_imageProvider.refreshSync(); 0211 Q_UNUSED(m_imageProvider.getCache()) 0212 0213 m_imageParameters.hue = 2; 0214 m_imageProvider.setImageParameters(m_imageParameters); 0215 m_imageProvider.refreshSync(); 0216 Q_UNUSED(m_imageProvider.getCache()) 0217 0218 m_imageParameters.hue = -10; 0219 m_imageProvider.setImageParameters(m_imageParameters); 0220 m_imageProvider.refreshSync(); 0221 Q_UNUSED(m_imageProvider.getCache()) 0222 0223 m_imageParameters.hue = -1000; 0224 m_imageProvider.setImageParameters(m_imageParameters); 0225 m_imageProvider.refreshSync(); 0226 Q_UNUSED(m_imageProvider.getCache()) 0227 0228 m_imageParameters.hue = 100; 0229 m_imageProvider.setImageParameters(m_imageParameters); 0230 m_imageProvider.refreshSync(); 0231 Q_UNUSED(m_imageProvider.getCache()) 0232 0233 m_imageParameters.hue = 110; 0234 m_imageProvider.setImageParameters(m_imageParameters); 0235 m_imageProvider.refreshSync(); 0236 Q_UNUSED(m_imageProvider.getCache()) 0237 0238 m_imageParameters.hue = 250; 0239 m_imageProvider.setImageParameters(m_imageParameters); 0240 m_imageProvider.refreshSync(); 0241 Q_UNUSED(m_imageProvider.getCache()) 0242 0243 m_imageParameters.hue = 360; 0244 m_imageProvider.setImageParameters(m_imageParameters); 0245 m_imageProvider.refreshSync(); 0246 Q_UNUSED(m_imageProvider.getCache()) 0247 0248 m_imageParameters.hue = 361; 0249 m_imageProvider.setImageParameters(m_imageParameters); 0250 m_imageProvider.refreshSync(); 0251 Q_UNUSED(m_imageProvider.getCache()) 0252 0253 m_imageParameters.hue = 721; 0254 m_imageProvider.setImageParameters(m_imageParameters); 0255 m_imageProvider.refreshSync(); 0256 Q_UNUSED(m_imageProvider.getCache()) 0257 } 0258 0259 #endif 0260 }; 0261 0262 } // namespace PerceptualColor 0263 0264 QTEST_MAIN(PerceptualColor::TestChromaLightnessImageParameters) 0265 0266 // The following “include” is necessary because we do not use a header file: 0267 #include "testchromalightnessimageparameters.moc"