File indexing completed on 2024-07-21 10:09:35

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 "screencolorpicker.h"
0007 
0008 #include <qglobal.h>
0009 #include <qobject.h>
0010 #include <qpointer.h>
0011 #include <qtest.h>
0012 #include <qtestcase.h>
0013 
0014 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0015 #include <qcontainerfwd.h>
0016 #include <qmap.h>
0017 #include <qtmetamacros.h>
0018 #else
0019 #include <qmetatype.h>
0020 #include <qobjectdefs.h>
0021 #include <qstring.h>
0022 #endif
0023 
0024 #ifdef PERCEPTUALCOLORLIB_STATIC
0025 #include <optional>
0026 #endif
0027 
0028 namespace PerceptualColor
0029 {
0030 class TestScreenColorPicker : public QObject
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     explicit TestScreenColorPicker(QObject *parent = nullptr)
0036         : QObject(parent)
0037     {
0038     }
0039 
0040 private Q_SLOTS:
0041     void initTestCase()
0042     {
0043         // Called before the first test function is executed
0044     }
0045 
0046     void cleanupTestCase()
0047     {
0048         // Called after the last test function was executed
0049     }
0050 
0051     void init()
0052     {
0053         // Called before each test function is executed
0054     }
0055 
0056     void cleanup()
0057     {
0058         // Called after every test function
0059     }
0060 
0061     void testIsAvailable()
0062     {
0063         ScreenColorPicker picker(nullptr);
0064         // Result depends on the platform. Make sure that at least is
0065         // does not crash.
0066         const bool result = picker.isAvailable();
0067         Q_UNUSED(result)
0068     }
0069 
0070     void testGetPortalResponse()
0071     {
0072         ScreenColorPicker picker(nullptr);
0073         // Difficult to test. Make sure that at least it does not crash.
0074         picker.getPortalResponse(1, QVariantMap());
0075     }
0076 
0077     void testInitializeQColorDialogSupport()
0078     {
0079         ScreenColorPicker picker(nullptr);
0080         // Difficult to test. Make sure that at least it does not crash.
0081         picker.initializeQColorDialogSupport();
0082 
0083 #ifdef PERCEPTUALCOLORLIB_STATIC
0084         // NOTE The variable m_hasQColorDialogSupport is defined as “static
0085         // inline … = …;”. This decision has caused issues when
0086         // perceptualcolorinternal is used as a shared/dynamic
0087         // library, and the value of m_hasQColorDialogSupport is
0088         // accessed from  outside. Specifically, on MSVC, incorrect
0089         // values are returned during variable access from outside, likely
0090         // due to MSVC creating another  concurrent variable within the unit
0091         // test executable, while initializeQColorDialogSupport modifies
0092         // the variable within the dll. However, in general, having “static
0093         // inline … = …;” variables is acceptable if they are
0094         // private class members, as nobody can access them. Consequently,
0095         // this part of the unit test is intended for static builds only.
0096 
0097         // There has to be at least a result (even if we do not know which)
0098         QVERIFY(picker.m_hasQColorDialogSupport.has_value());
0099 
0100         // Tough future code changes in Qt could break our
0101         // QColorDialog support, it is a good idea to check
0102         // here if the QColorDialog support does actually work,
0103         // so we might get at least alerts by failing unit tests.
0104         if (picker.m_hasQColorDialogSupport.has_value()) {
0105             QCOMPARE(picker.m_hasQColorDialogSupport.value(), true);
0106         }
0107 #endif
0108         QVERIFY(!picker.m_qColorDialogScreenButton.isNull());
0109         QVERIFY(!picker.m_qColorDialog.isNull());
0110     }
0111 
0112     void testHasPortalSupport()
0113     {
0114         // Difficult to test. Make sure that at least it does not crash.
0115         const bool result = ScreenColorPicker::hasPortalSupport();
0116         Q_UNUSED(result)
0117     }
0118 
0119     void testQueryPortalSupport()
0120     {
0121         // Difficult to test. Make sure that at least it does not crash.
0122         const bool result = ScreenColorPicker::queryPortalSupport();
0123         Q_UNUSED(result)
0124     }
0125 
0126     void testTranslateViaQColorDialog()
0127     {
0128         // Difficult to test. Make sure that at least it does not crash.
0129         const auto result = ScreenColorPicker::translateViaQColorDialog("abcdefghijkl");
0130         Q_UNUSED(result)
0131     }
0132 };
0133 
0134 } // namespace PerceptualColor
0135 
0136 QTEST_MAIN(PerceptualColor::TestScreenColorPicker)
0137 
0138 // The following “include” is necessary because we do not use a header file:
0139 #include "testscreencolorpicker.moc"