File indexing completed on 2024-11-10 04:56:23
0001 /* 0002 SPDX-FileCopyrightText: 2016 Martin Graesslin <mgraesslin@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 0006 */ 0007 0008 #include "onscreennotificationtest.h" 0009 0010 #include "input.h" 0011 #include "onscreennotification.h" 0012 0013 #include <KConfigGroup> 0014 #include <KSharedConfig> 0015 0016 #include <QQmlEngine> 0017 #include <QSignalSpy> 0018 #include <QTest> 0019 0020 QTEST_MAIN(OnScreenNotificationTest); 0021 0022 namespace KWin 0023 { 0024 0025 void InputRedirection::installInputEventSpy(InputEventSpy *spy) 0026 { 0027 } 0028 0029 void InputRedirection::uninstallInputEventSpy(InputEventSpy *spy) 0030 { 0031 } 0032 0033 InputRedirection *InputRedirection::s_self = nullptr; 0034 0035 } 0036 0037 using KWin::OnScreenNotification; 0038 0039 void OnScreenNotificationTest::show() 0040 { 0041 OnScreenNotification notification; 0042 auto config = KSharedConfig::openConfig(QString(), KSharedConfig::SimpleConfig); 0043 KConfigGroup group = config->group(QStringLiteral("OnScreenNotification")); 0044 group.writeEntry(QStringLiteral("QmlPath"), QString("/does/not/exist.qml")); 0045 group.sync(); 0046 notification.setConfig(config); 0047 notification.setEngine(new QQmlEngine(¬ification)); 0048 notification.setMessage(QStringLiteral("Some text so that we see it in the test")); 0049 0050 QSignalSpy visibleChangedSpy(¬ification, &OnScreenNotification::visibleChanged); 0051 QCOMPARE(notification.isVisible(), false); 0052 notification.setVisible(true); 0053 QCOMPARE(notification.isVisible(), true); 0054 QCOMPARE(visibleChangedSpy.count(), 1); 0055 0056 // show again should not trigger 0057 notification.setVisible(true); 0058 QCOMPARE(visibleChangedSpy.count(), 1); 0059 0060 // timer should not have hidden 0061 QTest::qWait(500); 0062 QCOMPARE(notification.isVisible(), true); 0063 0064 // hide again 0065 notification.setVisible(false); 0066 QCOMPARE(notification.isVisible(), false); 0067 QCOMPARE(visibleChangedSpy.count(), 2); 0068 0069 // now show with timer 0070 notification.setTimeout(250); 0071 notification.setVisible(true); 0072 QCOMPARE(notification.isVisible(), true); 0073 QCOMPARE(visibleChangedSpy.count(), 3); 0074 QVERIFY(visibleChangedSpy.wait()); 0075 QCOMPARE(notification.isVisible(), false); 0076 QCOMPARE(visibleChangedSpy.count(), 4); 0077 } 0078 0079 void OnScreenNotificationTest::timeout() 0080 { 0081 OnScreenNotification notification; 0082 QSignalSpy timeoutChangedSpy(¬ification, &OnScreenNotification::timeoutChanged); 0083 QCOMPARE(notification.timeout(), 0); 0084 notification.setTimeout(1000); 0085 QCOMPARE(notification.timeout(), 1000); 0086 QCOMPARE(timeoutChangedSpy.count(), 1); 0087 notification.setTimeout(1000); 0088 QCOMPARE(timeoutChangedSpy.count(), 1); 0089 notification.setTimeout(0); 0090 QCOMPARE(notification.timeout(), 0); 0091 QCOMPARE(timeoutChangedSpy.count(), 2); 0092 } 0093 0094 void OnScreenNotificationTest::iconName() 0095 { 0096 OnScreenNotification notification; 0097 QSignalSpy iconNameChangedSpy(¬ification, &OnScreenNotification::iconNameChanged); 0098 QCOMPARE(notification.iconName(), QString()); 0099 notification.setIconName(QStringLiteral("foo")); 0100 QCOMPARE(notification.iconName(), QStringLiteral("foo")); 0101 QCOMPARE(iconNameChangedSpy.count(), 1); 0102 notification.setIconName(QStringLiteral("foo")); 0103 QCOMPARE(iconNameChangedSpy.count(), 1); 0104 notification.setIconName(QStringLiteral("bar")); 0105 QCOMPARE(notification.iconName(), QStringLiteral("bar")); 0106 QCOMPARE(iconNameChangedSpy.count(), 2); 0107 } 0108 0109 void OnScreenNotificationTest::message() 0110 { 0111 OnScreenNotification notification; 0112 QSignalSpy messageChangedSpy(¬ification, &OnScreenNotification::messageChanged); 0113 QCOMPARE(notification.message(), QString()); 0114 notification.setMessage(QStringLiteral("foo")); 0115 QCOMPARE(notification.message(), QStringLiteral("foo")); 0116 QCOMPARE(messageChangedSpy.count(), 1); 0117 notification.setMessage(QStringLiteral("foo")); 0118 QCOMPARE(messageChangedSpy.count(), 1); 0119 notification.setMessage(QStringLiteral("bar")); 0120 QCOMPARE(notification.message(), QStringLiteral("bar")); 0121 QCOMPARE(messageChangedSpy.count(), 2); 0122 } 0123 0124 #include "moc_onscreennotificationtest.cpp"