File indexing completed on 2024-06-16 10:43:49

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 "multispinboxsection.h"
0007 // Second, the private implementation.
0008 #include "multispinboxsection_p.h" // IWYU pragma: keep
0009 
0010 #include <qglobal.h>
0011 #include <qobject.h>
0012 #include <qspinbox.h>
0013 #include <qstringliteral.h>
0014 #include <qtest.h>
0015 #include <qtestcase.h>
0016 #include <qvariant.h>
0017 #include <utility>
0018 
0019 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0020 #include <qtmetamacros.h>
0021 #else
0022 #include <qobjectdefs.h>
0023 #include <qstring.h>
0024 #endif
0025 
0026 namespace PerceptualColor
0027 {
0028 class TestMultiSpinBoxSection : public QObject
0029 {
0030     Q_OBJECT
0031 
0032 public:
0033     explicit TestMultiSpinBoxSection(QObject *parent = nullptr)
0034         : QObject(parent)
0035     {
0036     }
0037 
0038 private:
0039     MultiSpinBoxSection helperGetUnusualConfig()
0040     {
0041         MultiSpinBoxSection myConfig;
0042         myConfig.setDecimals(1);
0043         myConfig.setWrapping(true);
0044         myConfig.setMaximum(3);
0045         myConfig.setMinimum(2);
0046         myConfig.setPrefix(QStringLiteral("a"));
0047         myConfig.setSingleStep(4);
0048         myConfig.setSuffix(QStringLiteral("b"));
0049         return myConfig;
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 testDefaultValues()
0074     {
0075         // The default values should be the same as for QDoubleSpinBox
0076         MultiSpinBoxSection myConfig;
0077         QDoubleSpinBox myDoubleSpinBox;
0078 
0079         // Test default values of the configuration object:
0080         QCOMPARE(myConfig.decimals(), myDoubleSpinBox.decimals());
0081         QCOMPARE(myConfig.isWrapping(), myDoubleSpinBox.wrapping());
0082         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0083         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0084         QCOMPARE(myConfig.prefix(), myDoubleSpinBox.prefix());
0085         QCOMPARE(myConfig.singleStep(), myDoubleSpinBox.singleStep());
0086         QCOMPARE(myConfig.suffix(), myDoubleSpinBox.suffix());
0087     }
0088 
0089     void testCopy()
0090     {
0091         MultiSpinBoxSection myConfig;
0092         myConfig.setDecimals(1);
0093         myConfig.setWrapping(true);
0094         myConfig.setMaximum(3);
0095         myConfig.setMinimum(2);
0096         myConfig.setPrefix(QStringLiteral("a"));
0097         myConfig.setSingleStep(4);
0098         myConfig.setSuffix(QStringLiteral("b"));
0099 
0100         // Test copy constructor
0101         MultiSpinBoxSection copyContructed(myConfig);
0102         QCOMPARE(myConfig.decimals(), 1);
0103         QCOMPARE(myConfig.isWrapping(), true);
0104         QCOMPARE(myConfig.maximum(), 3);
0105         QCOMPARE(myConfig.minimum(), 2);
0106         QCOMPARE(myConfig.prefix(), QStringLiteral("a"));
0107         QCOMPARE(myConfig.singleStep(), 4);
0108         QCOMPARE(myConfig.suffix(), QStringLiteral("b"));
0109 
0110         // Test copy assignment operator
0111         MultiSpinBoxSection copyAssigned;
0112         copyAssigned.setMaximum(9); // Change arbitrary some value
0113         copyAssigned = myConfig;
0114         QCOMPARE(myConfig.decimals(), 1);
0115         QCOMPARE(myConfig.isWrapping(), true);
0116         QCOMPARE(myConfig.maximum(), 3);
0117         QCOMPARE(myConfig.minimum(), 2);
0118         QCOMPARE(myConfig.prefix(), QStringLiteral("a"));
0119         QCOMPARE(myConfig.singleStep(), 4);
0120         QCOMPARE(myConfig.suffix(), QStringLiteral("b"));
0121     }
0122 
0123     void testMoveConstructor()
0124     {
0125         MultiSpinBoxSection testObjectToMove;
0126         testObjectToMove.setDecimals(1);
0127         testObjectToMove.setWrapping(true);
0128         testObjectToMove.setMaximum(3);
0129         testObjectToMove.setMinimum(2);
0130         testObjectToMove.setPrefix(QStringLiteral("a"));
0131         testObjectToMove.setSingleStep(4);
0132         testObjectToMove.setSuffix(QStringLiteral("b"));
0133         MultiSpinBoxSection myConfig(
0134             // Trigger the move constructor
0135             std::move(testObjectToMove));
0136         QCOMPARE(myConfig.decimals(), 1);
0137         QCOMPARE(myConfig.isWrapping(), true);
0138         QCOMPARE(myConfig.maximum(), 3);
0139         QCOMPARE(myConfig.minimum(), 2);
0140         QCOMPARE(myConfig.prefix(), QStringLiteral("a"));
0141         QCOMPARE(myConfig.singleStep(), 4);
0142         QCOMPARE(myConfig.suffix(), QStringLiteral("b"));
0143     }
0144 
0145     void testMoveAssignment()
0146     {
0147         MultiSpinBoxSection myConfig;
0148         myConfig.setMaximum(9); // change arbitrarily some value
0149 
0150         // Create a test object
0151         MultiSpinBoxSection testObjectToMove;
0152         testObjectToMove.setDecimals(1);
0153         testObjectToMove.setWrapping(true);
0154         testObjectToMove.setMaximum(3);
0155         testObjectToMove.setMinimum(2);
0156         testObjectToMove.setPrefix(QStringLiteral("a"));
0157         testObjectToMove.setSingleStep(4);
0158         testObjectToMove.setSuffix(QStringLiteral("b"));
0159 
0160         // Move assignment:
0161         myConfig = std::move(testObjectToMove);
0162 
0163         QCOMPARE(myConfig.decimals(), 1);
0164         QCOMPARE(myConfig.isWrapping(), true);
0165         QCOMPARE(myConfig.maximum(), 3);
0166         QCOMPARE(myConfig.minimum(), 2);
0167         QCOMPARE(myConfig.prefix(), QStringLiteral("a"));
0168         QCOMPARE(myConfig.singleStep(), 4);
0169         QCOMPARE(myConfig.suffix(), QStringLiteral("b"));
0170     }
0171 
0172     void testDecimals()
0173     {
0174         MultiSpinBoxSection myConfig;
0175         myConfig.setDecimals(5);
0176         QCOMPARE(myConfig.decimals(), 5);
0177     }
0178 
0179     void testIsWrapping()
0180     {
0181         MultiSpinBoxSection myConfig;
0182         myConfig.setWrapping(true);
0183         QCOMPARE(myConfig.isWrapping(), true);
0184         myConfig.setWrapping(false);
0185         QCOMPARE(myConfig.isWrapping(), false);
0186     }
0187 
0188     void testMaximum()
0189     {
0190         MultiSpinBoxSection myConfig;
0191         myConfig.setMaximum(5);
0192         QCOMPARE(myConfig.maximum(), 5);
0193     }
0194 
0195     void testMinimum()
0196     {
0197         MultiSpinBoxSection myConfig;
0198         myConfig.setMinimum(5);
0199         QCOMPARE(myConfig.minimum(), 5);
0200     }
0201 
0202     void testPrefix()
0203     {
0204         MultiSpinBoxSection myConfig;
0205         myConfig.setPrefix(QStringLiteral("a"));
0206         QCOMPARE(myConfig.prefix(), QStringLiteral("a"));
0207     }
0208 
0209     void testSingleStep()
0210     {
0211         MultiSpinBoxSection myConfig;
0212         myConfig.setSingleStep(5);
0213         QCOMPARE(myConfig.singleStep(), 5);
0214     }
0215 
0216     void testSuffix()
0217     {
0218         MultiSpinBoxSection myConfig;
0219         myConfig.setSuffix(QStringLiteral("a"));
0220         QCOMPARE(myConfig.suffix(), QStringLiteral("a"));
0221     }
0222 
0223     void testComplianceDecimals()
0224     {
0225         MultiSpinBoxSection myConfig;
0226         QDoubleSpinBox myDoubleSpinBox;
0227 
0228         myConfig.setDecimals(5);
0229         myDoubleSpinBox.setDecimals(5);
0230         QCOMPARE(myConfig.decimals(), myDoubleSpinBox.decimals());
0231 
0232         myConfig.setDecimals(50);
0233         myDoubleSpinBox.setDecimals(50);
0234         QCOMPARE(myConfig.decimals(), myDoubleSpinBox.decimals());
0235 
0236         myConfig.setDecimals(500);
0237         myDoubleSpinBox.setDecimals(500);
0238         QCOMPARE(myConfig.decimals(), myDoubleSpinBox.decimals());
0239 
0240         myConfig.setDecimals(0);
0241         myDoubleSpinBox.setDecimals(0);
0242         QCOMPARE(myConfig.decimals(), myDoubleSpinBox.decimals());
0243 
0244         myConfig.setDecimals(-1);
0245         myDoubleSpinBox.setDecimals(-1);
0246         QCOMPARE(myConfig.decimals(), myDoubleSpinBox.decimals());
0247     }
0248 
0249     void testComplianceSingleStep()
0250     {
0251         MultiSpinBoxSection myConfig;
0252         QDoubleSpinBox myDoubleSpinBox;
0253 
0254         myConfig.setSingleStep(5);
0255         myDoubleSpinBox.setSingleStep(5);
0256         QCOMPARE(myConfig.singleStep(), myDoubleSpinBox.singleStep());
0257 
0258         myConfig.setSingleStep(50);
0259         myDoubleSpinBox.setSingleStep(50);
0260         QCOMPARE(myConfig.singleStep(), myDoubleSpinBox.singleStep());
0261 
0262         myConfig.setSingleStep(500);
0263         myDoubleSpinBox.setSingleStep(500);
0264         QCOMPARE(myConfig.singleStep(), myDoubleSpinBox.singleStep());
0265 
0266         myConfig.setSingleStep(0);
0267         myDoubleSpinBox.setSingleStep(0);
0268         QCOMPARE(myConfig.singleStep(), myDoubleSpinBox.singleStep());
0269 
0270         myConfig.setSingleStep(-1);
0271         myDoubleSpinBox.setSingleStep(-1);
0272         QCOMPARE(myConfig.singleStep(), myDoubleSpinBox.singleStep());
0273     }
0274 
0275     void testComplianceMinimumTooBig()
0276     {
0277         MultiSpinBoxSection myConfig;
0278         QDoubleSpinBox myDoubleSpinBox;
0279 
0280         // Set a minimum that is bigger than the current maximum
0281         myConfig.setMaximum(50);
0282         myDoubleSpinBox.setMaximum(50);
0283         myConfig.setMinimum(60);
0284         myDoubleSpinBox.setMinimum(60);
0285         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0286         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0287 
0288         // Afterwards, change back the maximum to a value that would
0289         // have allowed to the original minimum
0290         myConfig.setMaximum(70);
0291         myDoubleSpinBox.setMaximum(70);
0292         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0293         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0294     }
0295 
0296     void testComplianceMaximumTooSmall()
0297     {
0298         MultiSpinBoxSection myConfig;
0299         QDoubleSpinBox myDoubleSpinBox;
0300 
0301         // Set a maximum that is smaller than the current minimum
0302         myConfig.setMinimum(50);
0303         myDoubleSpinBox.setMinimum(50);
0304         myConfig.setMaximum(40);
0305         myDoubleSpinBox.setMaximum(40);
0306         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0307         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0308 
0309         // Afterwards, change back the minimum to a value that would
0310         // have allowed to the original maximum
0311         myConfig.setMinimum(30);
0312         myDoubleSpinBox.setMinimum(30);
0313         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0314         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0315     }
0316 
0317     void testComplianceMinimumDecimals()
0318     {
0319         MultiSpinBoxSection myConfig;
0320         QDoubleSpinBox myDoubleSpinBox;
0321 
0322         // Set a minimum with more decimals than configured.
0323         // Check compliance of the rounding behaviour.
0324         myConfig.setDecimals(1);
0325         myConfig.setMinimum(12.3456);
0326         myDoubleSpinBox.setDecimals(1);
0327         myDoubleSpinBox.setMinimum(12.3456);
0328         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0329 
0330         // Check compliance when changing the decimals _after_ setting
0331         // the minimum value. This seems to allow accessing to the
0332         // original value of minimum in QDoubleSpinBox!
0333         myConfig.setDecimals(2);
0334         myDoubleSpinBox.setDecimals(2);
0335         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0336         myConfig.setDecimals(3);
0337         myDoubleSpinBox.setDecimals(3);
0338         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0339         myConfig.setDecimals(4);
0340         myDoubleSpinBox.setDecimals(4);
0341         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0342         myConfig.setDecimals(5);
0343         myDoubleSpinBox.setDecimals(5);
0344         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0345         myConfig.setDecimals(6);
0346         myDoubleSpinBox.setDecimals(6);
0347         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0348         myConfig.setDecimals(0);
0349         myDoubleSpinBox.setDecimals(0);
0350         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0351         myConfig.setDecimals(-1);
0352         myDoubleSpinBox.setDecimals(-1);
0353         QCOMPARE(myConfig.minimum(), myDoubleSpinBox.minimum());
0354     }
0355 
0356     void testComplianceMaximumDecimals()
0357     {
0358         MultiSpinBoxSection myConfig;
0359         QDoubleSpinBox myDoubleSpinBox;
0360 
0361         // Set a maximum with more decimals than configured.
0362         // Check compliance of the rounding behaviour.
0363         myConfig.setDecimals(1);
0364         myConfig.setMaximum(12.3456);
0365         myDoubleSpinBox.setDecimals(1);
0366         myDoubleSpinBox.setMaximum(12.3456);
0367         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0368 
0369         // Check compliance when changing the decimals _after_ setting
0370         // the maximum value. This seems to allow accessing to the
0371         // original value of maximum in QDoubleSpinBox!
0372         myConfig.setDecimals(2);
0373         myDoubleSpinBox.setDecimals(2);
0374         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0375         myConfig.setDecimals(3);
0376         myDoubleSpinBox.setDecimals(3);
0377         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0378         myConfig.setDecimals(4);
0379         myDoubleSpinBox.setDecimals(4);
0380         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0381         myConfig.setDecimals(5);
0382         myDoubleSpinBox.setDecimals(5);
0383         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0384         myConfig.setDecimals(6);
0385         myDoubleSpinBox.setDecimals(6);
0386         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0387         myConfig.setDecimals(0);
0388         myDoubleSpinBox.setDecimals(0);
0389         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0390         myConfig.setDecimals(-1);
0391         myDoubleSpinBox.setDecimals(-1);
0392         QCOMPARE(myConfig.maximum(), myDoubleSpinBox.maximum());
0393     }
0394 
0395     void testMetaTypeDeclaration()
0396     {
0397         QVariant test;
0398         // The next line should produce a compiler error if the
0399         // type is not declared to Qt’s Meta Object System.
0400         test.setValue(MultiSpinBoxSection());
0401     }
0402 };
0403 
0404 } // namespace PerceptualColor
0405 
0406 QTEST_MAIN(PerceptualColor::TestMultiSpinBoxSection)
0407 
0408 // The following “include” is necessary because we do not use a header file:
0409 #include "testmultispinboxsection.moc"