File indexing completed on 2024-12-01 07:27:00
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 "languagechangeeventfilter.h" 0007 0008 #include <qcoreapplication.h> 0009 #include <qcoreevent.h> 0010 #include <qglobal.h> 0011 #include <qobject.h> 0012 #include <qtest.h> 0013 #include <qtestcase.h> 0014 #include <qwidget.h> 0015 0016 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) 0017 #include <qtmetamacros.h> 0018 #else 0019 #include <qobjectdefs.h> 0020 #include <qstring.h> 0021 #endif 0022 0023 namespace PerceptualColor 0024 { 0025 0026 class ReceiverMockup : public QWidget 0027 { 0028 Q_OBJECT 0029 public: 0030 explicit ReceiverMockup(QWidget *parent = nullptr) 0031 : QWidget(parent){}; 0032 bool languageChangeEventDetected = false; 0033 0034 protected: 0035 virtual void changeEvent(QEvent *event) override 0036 { 0037 if (event->type() == QEvent::LanguageChange) { 0038 languageChangeEventDetected = true; 0039 } 0040 }; 0041 }; 0042 0043 class TestLanguageChangeEventFilter : public QObject 0044 { 0045 Q_OBJECT 0046 0047 public: 0048 explicit TestLanguageChangeEventFilter(QObject *parent = nullptr) 0049 : QObject(parent) 0050 { 0051 } 0052 0053 private Q_SLOTS: 0054 void initTestCase() 0055 { 0056 // Called before the first test function is executed 0057 } 0058 0059 void cleanupTestCase() 0060 { 0061 // Called after the last test function was executed 0062 } 0063 0064 void init() 0065 { 0066 // Called before each test function is executed 0067 } 0068 0069 void cleanup() 0070 { 0071 // Called after every test function 0072 } 0073 0074 void testConstructorDestructor() 0075 { 0076 LanguageChangeEventFilter temp; 0077 } 0078 0079 void testEventDiscarded() 0080 { 0081 // Assertions 0082 { 0083 ReceiverMockup receiverTest; 0084 QEvent languageChangeEventTest(QEvent::LanguageChange); 0085 QCoreApplication::sendEvent(&receiverTest, &languageChangeEventTest); 0086 QVERIFY(receiverTest.languageChangeEventDetected); 0087 } 0088 0089 // Actual test 0090 ReceiverMockup receiver; 0091 LanguageChangeEventFilter filter; 0092 receiver.installEventFilter(&filter); 0093 QEvent languageChangeEvent(QEvent::LanguageChange); 0094 QCoreApplication::sendEvent(&receiver, &languageChangeEvent); 0095 QCOMPARE(receiver.languageChangeEventDetected, false); 0096 } 0097 }; 0098 0099 } // namespace PerceptualColor 0100 0101 QTEST_MAIN(PerceptualColor::TestLanguageChangeEventFilter) 0102 0103 // The following “include” is necessary because we do not use a header file: 0104 #include "testlanguagechangeeventfilter.moc"