File indexing completed on 2024-05-12 04:44:25
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 "lchadouble.h" 0007 0008 #include <qdebug.h> 0009 #include <qglobal.h> 0010 #include <qobject.h> 0011 #include <qstring.h> 0012 #include <qtest.h> 0013 #include <qtestcase.h> 0014 #include <qvariant.h> 0015 0016 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 0017 #include <qtmetamacros.h> 0018 #else 0019 #include <qobjectdefs.h> 0020 #endif 0021 0022 static void snippet01() 0023 { 0024 //! [Use LchaDouble] 0025 PerceptualColor::LchaDouble myValue; 0026 myValue.l = 50; // Lightness: 50% 0027 myValue.c = 25; // Chroma: 25 0028 myValue.h = 5; // Hue: 5° 0029 myValue.a = 0.5; // Alpha: 0.5 (semi-transparent) 0030 //! [Use LchaDouble] 0031 Q_UNUSED(myValue) 0032 } 0033 0034 namespace PerceptualColor 0035 { 0036 class TestLchaDouble : public QObject 0037 { 0038 Q_OBJECT 0039 0040 public: 0041 explicit TestLchaDouble(QObject *parent = nullptr) 0042 : QObject(parent) 0043 { 0044 } 0045 0046 private: 0047 static void voidMessageHandler(QtMsgType, const QMessageLogContext &, const QString &) 0048 { 0049 // dummy message handler that does not print messages 0050 } 0051 0052 private Q_SLOTS: 0053 void initTestCase() 0054 { 0055 // Called before the first test function is executed 0056 } 0057 0058 void cleanupTestCase() 0059 { 0060 // Called after the last test function was executed 0061 } 0062 0063 void init() 0064 { 0065 // Called before each test function is executed 0066 } 0067 0068 void cleanup() 0069 { 0070 // Called after every test function 0071 } 0072 0073 void testConstructorDestructor() 0074 { 0075 // This should not crash. 0076 LchaDouble test; 0077 test.l = 50; 0078 Q_UNUSED(test) 0079 } 0080 0081 void testCopyConstructor() 0082 { 0083 // This should not crash. 0084 LchaDouble test; 0085 test.l = 50; 0086 test.c = 25; 0087 test.h = 5; 0088 LchaDouble copy(test); 0089 QCOMPARE(copy.l, 50); 0090 QCOMPARE(copy.c, 25); 0091 QCOMPARE(copy.h, 5); 0092 } 0093 0094 void testHasSameCoordinates() 0095 { 0096 LchaDouble a; 0097 a.l = 50; 0098 a.c = 20; 0099 a.h = 5; 0100 LchaDouble b = a; 0101 QVERIFY(a.hasSameCoordinates(b)); 0102 QVERIFY(b.hasSameCoordinates(a)); 0103 QVERIFY(a.hasSameCoordinates(a)); 0104 QVERIFY(b.hasSameCoordinates(b)); 0105 b.h = 365; 0106 QVERIFY(!a.hasSameCoordinates(b)); 0107 QVERIFY(!b.hasSameCoordinates(a)); 0108 QVERIFY(a.hasSameCoordinates(a)); 0109 QVERIFY(b.hasSameCoordinates(b)); 0110 // When chroma is 0, hue becomes meaningless. Nevertheless, different 0111 // hues should be detected. 0112 a.c = 0; 0113 b.c = 0; 0114 QVERIFY(!a.hasSameCoordinates(b)); 0115 QVERIFY(!b.hasSameCoordinates(a)); 0116 QVERIFY(a.hasSameCoordinates(a)); 0117 QVERIFY(b.hasSameCoordinates(b)); 0118 // And when returning to the same hue, everything should be considered 0119 // as with same coordinates. 0120 b.h = 5; 0121 QVERIFY(a.hasSameCoordinates(b)); 0122 QVERIFY(b.hasSameCoordinates(a)); 0123 QVERIFY(a.hasSameCoordinates(a)); 0124 QVERIFY(b.hasSameCoordinates(b)); 0125 } 0126 0127 void testQDebugSupport() 0128 { 0129 PerceptualColor::LchaDouble test; 0130 // suppress warning for generating invalid QColor 0131 qInstallMessageHandler(voidMessageHandler); 0132 qDebug() << test; 0133 // do not suppress warning for generating invalid QColor anymore 0134 qInstallMessageHandler(nullptr); 0135 } 0136 0137 void testMetaTypeDeclaration() 0138 { 0139 QVariant test; 0140 // The next line should produce a compiler error if the 0141 // type is not declared to Qt’s Meta Object System. 0142 test.setValue(LchaDouble()); 0143 } 0144 0145 void testSnippet01() 0146 { 0147 snippet01(); 0148 } 0149 }; 0150 0151 } // namespace PerceptualColor 0152 0153 QTEST_MAIN(PerceptualColor::TestLchaDouble) 0154 0155 // The following “include” is necessary because we do not use a header file: 0156 #include "testlchadouble.moc"