File indexing completed on 2025-04-27 07:34:58
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 "abstractdiagram.h" 0007 0008 #include "helpermath.h" 0009 #include <qbrush.h> 0010 #include <qcolor.h> 0011 #include <qglobal.h> 0012 #include <qimage.h> 0013 #include <qnamespace.h> 0014 #include <qobject.h> 0015 #include <qpainter.h> 0016 #include <qsize.h> 0017 #include <qtest.h> 0018 #include <qtestcase.h> 0019 0020 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 0021 #include <qtmetamacros.h> 0022 #else 0023 #include <qobjectdefs.h> 0024 #include <qstring.h> 0025 #endif 0026 0027 class QWidget; 0028 0029 class TestAbstractDiagramHelperClass : public PerceptualColor::AbstractDiagram 0030 { 0031 Q_OBJECT 0032 public: 0033 explicit TestAbstractDiagramHelperClass(QWidget *parent = nullptr) 0034 : AbstractDiagram(parent) 0035 { 0036 // This constructor exists only to satisfy Clazy code checker, which 0037 // expects constructors taking QWidget* as argument for all classes 0038 // that inherit from QWidget. 0039 } 0040 void testSnippet01() 0041 { 0042 //! [useTransparencyBackground] 0043 // Within a class derived from AbstractDiagram, you can use this code: 0044 0045 QImage myImage(150, 200, QImage::Format_ARGB32_Premultiplied); 0046 0047 QPainter myPainter(&myImage); 0048 0049 // Fill the hole image with tiles made of transparencyBackground() 0050 myPainter.fillRect(0, 0051 0, 0052 150, 0053 200, 0054 // During painting, QBrush will ignore the 0055 // device pixel ratio of the underlying 0056 // transparencyBackground image! 0057 QBrush(transparencyBackground())); 0058 0059 // Paint semi-transparent red color above 0060 myPainter.fillRect(0, 0, 150, 200, QBrush(QColor(255, 0, 0, 128))); 0061 //! [useTransparencyBackground] 0062 } 0063 }; 0064 0065 namespace PerceptualColor 0066 { 0067 class TestAbstractDiagram : public QObject 0068 { 0069 Q_OBJECT 0070 0071 public: 0072 explicit TestAbstractDiagram(QObject *parent = nullptr) 0073 : QObject(parent) 0074 { 0075 } 0076 0077 private Q_SLOTS: 0078 void initTestCase() 0079 { 0080 // Called before the first test function is executed 0081 } 0082 void cleanupTestCase() 0083 { 0084 // Called after the last test function was executed 0085 } 0086 0087 void init() 0088 { 0089 // Called before each test function is executed 0090 } 0091 void cleanup() 0092 { 0093 // Called after every test function 0094 } 0095 0096 void testConstructorDestructor() 0097 { 0098 PerceptualColor::AbstractDiagram myDiagram; 0099 } 0100 0101 void testShow() 0102 { 0103 // Just test if instantiating does not crash: 0104 PerceptualColor::AbstractDiagram myDiagram; 0105 myDiagram.show(); 0106 QVERIFY2(myDiagram.isVisible(), "Test is diagram was shown correctly."); 0107 } 0108 0109 void testSnippet01() 0110 { 0111 TestAbstractDiagramHelperClass helper; 0112 helper.testSnippet01(); 0113 } 0114 0115 void testTransparencyBackground() 0116 { 0117 PerceptualColor::AbstractDiagram myDiagram; 0118 QImage temp = myDiagram.transparencyBackground(); 0119 QVERIFY2(temp.size().width() > 0, "Width of image is bigger than 0."); 0120 QVERIFY2(temp.size().height() > 0, "Height of image is bigger than 0."); 0121 QVERIFY2(temp.allGray(), "Image is neutral gray."); 0122 } 0123 0124 void testFocusIndicatorColor() 0125 { 0126 QVERIFY2(AbstractDiagram().focusIndicatorColor().isValid(), "focusIndicatorColor() returns a valid color."); 0127 } 0128 0129 void testPhysicalPixelSize() 0130 { 0131 AbstractDiagram temp; 0132 temp.show(); 0133 qreal widthError = (temp.width() * temp.devicePixelRatioF()) - temp.physicalPixelSize().width(); 0134 QVERIFY2(qAbs(widthError) < 1, "Rounding width with error < 1."); 0135 qreal heightError = (temp.height() * temp.devicePixelRatioF()) - temp.physicalPixelSize().height(); 0136 QVERIFY2(qAbs(heightError) < 1, "Rounding height with error < 1."); 0137 } 0138 0139 void testDiagramOffset() 0140 { 0141 AbstractDiagram myDiagram; 0142 myDiagram.resize(50, 50); 0143 QVERIFY2(isInRange(49.0, myDiagram.maximumWidgetSquareSize(), 50.0), 0144 "Verify that maximumWidgetSquareSize is within expected " 0145 "rounding range."); 0146 // Next try: off by one. 0147 myDiagram.resize(51, 51); 0148 QVERIFY2(isInRange(50.0, myDiagram.maximumWidgetSquareSize(), 51.0), 0149 "Verify that maximumWidgetSquareSize is within expected " 0150 "rounding range."); 0151 } 0152 0153 void testHandle() 0154 { 0155 AbstractDiagram temp; 0156 QVERIFY2(temp.handleRadius() > 0, "Radius is positive."); 0157 QVERIFY2(temp.handleOutlineThickness() > 0, "Thickness is positive."); 0158 QVERIFY2(temp.handleRadius() > temp.handleOutlineThickness(), 0159 "Radius is bigger than thickness. " 0160 "(Otherwise, there would be no hole in the middle.)"); 0161 } 0162 0163 void testGradientThickness() 0164 { 0165 AbstractDiagram temp; 0166 QVERIFY(temp.gradientThickness() > 0); 0167 } 0168 0169 void testGradientMinimumLength() 0170 { 0171 AbstractDiagram temp; 0172 QVERIFY(temp.gradientMinimumLength() > temp.gradientThickness()); 0173 } 0174 0175 void testHandleColorFromBackgroundLightness() 0176 { 0177 AbstractDiagram temp; 0178 QCOMPARE(temp.handleColorFromBackgroundLightness(-1), QColor(Qt::white)); 0179 QCOMPARE(temp.handleColorFromBackgroundLightness(0), QColor(Qt::white)); 0180 QCOMPARE(temp.handleColorFromBackgroundLightness(49), QColor(Qt::white)); 0181 QCOMPARE(temp.handleColorFromBackgroundLightness(51), QColor(Qt::black)); 0182 QCOMPARE(temp.handleColorFromBackgroundLightness(100), QColor(Qt::black)); 0183 QCOMPARE(temp.handleColorFromBackgroundLightness(101), QColor(Qt::black)); 0184 } 0185 }; 0186 0187 } // namespace PerceptualColor 0188 0189 QTEST_MAIN(PerceptualColor::TestAbstractDiagram) 0190 // The following “include” is necessary because we do not use a header file: 0191 #include "testabstractdiagram.moc"