| 1 | #include "../misc/dbusnotification.h" |
| 2 | |
| 3 | #include "../misc/disablewarningsmoc.h" |
| 4 | |
| 5 | #include "resources/config.h" |
| 6 | |
| 7 | #include <QSignalSpy> |
| 8 | #include <QTest> |
| 9 | |
| 10 | using namespace QtUtilities; |
| 11 | |
| 12 | /*! |
| 13 | * \brief The DBusNotificationTests class tests the DBusNotification class. |
| 14 | */ |
| 15 | class DBusNotificationTests : public QObject { |
| 16 | Q_OBJECT |
| 17 | |
| 18 | private Q_SLOTS: |
| 19 | void smokeTest(); |
| 20 | void semiAutomaticTest(); |
| 21 | }; |
| 22 | |
| 23 | static void dummy(DBusNotification::Capabilities &&) |
| 24 | { |
| 25 | } |
| 26 | const static auto callback = std::function<void(DBusNotification::Capabilities &&)>(&dummy); |
| 27 | |
| 28 | /*! |
| 29 | * \brief Runs some basic functions of DBusNotification (c'tor, d'tor, some accessors, ...) but doesn't really check |
| 30 | * whether it works. |
| 31 | * \remarks This test should pass regardless whether DBus-notifications are actually available. Hence it avoids any checks |
| 32 | * which depend on that and just checks whether certain function don't lead to crashes. |
| 33 | */ |
| 34 | void DBusNotificationTests::smokeTest() |
| 35 | { |
| 36 | DBusNotification::isAvailable(); |
| 37 | DBusNotification::queryCapabilities(callback); |
| 38 | DBusNotification n(QStringLiteral("Smoke test"), NotificationIcon::Warning, 100); |
| 39 | QVERIFY2(!n.isVisible(), "not immediately visible"); |
| 40 | QCOMPARE(n.title(), QStringLiteral("Smoke test")); |
| 41 | n.setApplicationName(QStringLiteral(APP_NAME " tests; " APP_VERSION)); |
| 42 | QCOMPARE(n.applicationName(), QStringLiteral(APP_NAME " tests; " APP_VERSION)); |
| 43 | n.show(QStringLiteral("Some message")); // will emit an error if not available |
| 44 | n.isVisible(); |
| 45 | n.hide(); |
| 46 | QCOMPARE(n.message(), QStringLiteral("Some message")); |
| 47 | n.update(QStringLiteral("Another message")); |
| 48 | n.hide(); |
| 49 | if (n.isVisible()) { |
| 50 | QSignalSpy closedSpy(&n, &DBusNotification::closed); |
| 51 | closedSpy.wait(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /*! |
| 56 | * \brief Runs a semi-automatic test to verify whether DBusNotification works for real. |
| 57 | * \remarks This test needs a daemon for D-Bus notifications running and requires manual user interaction. It is therefore |
| 58 | * skipped unless an environment variable is set. |
| 59 | */ |
| 60 | void DBusNotificationTests::semiAutomaticTest() |
| 61 | { |
| 62 | const auto envValue = qgetenv(PROJECT_VARNAME_UPPER "_ENABLE_SEMI_AUTOMATIC_NOTIFICATION_TESTS"); |
| 63 | auto envValueIsInt = false; |
| 64 | if (envValue.isEmpty() || (envValue.toInt(&envValueIsInt) == 0 && envValueIsInt)) { |
| 65 | QSKIP("Set the environment variable " PROJECT_VARNAME_UPPER "_ENABLE_SEMI_AUTOMATIC_NOTIFICATION_TESTS to run " |
| 66 | "the semi-automatic D-Bus notification test."); |
| 67 | } |
| 68 | |
| 69 | QVERIFY2(DBusNotification::isAvailable(), "D-Bus notifications are available"); |
| 70 | |
| 71 | DBusNotification n(QStringLiteral("Semi-automatic test"), NotificationIcon::Information, 10000); |
| 72 | QString clickedAction, error; |
| 73 | auto actionConnection |
| 74 | = connect(&n, &DBusNotification::actionInvoked, [&clickedAction](const QString &actionName) { clickedAction = actionName; }); |
| 75 | auto errorConnection |
| 76 | = connect(&n, &DBusNotification::error, [&error]() { error = QStringLiteral("error occurred (TODO: pass an error message here)"); }); |
| 77 | n.setApplicationName(QStringLiteral(APP_NAME " tests; " APP_VERSION)); |
| 78 | n.show(QStringLiteral("Some message; will append more lines later")); |
| 79 | for (auto i = 1; i <= 10; ++i) { |
| 80 | n.update(QStringLiteral("Yet another line, should be displayed in the same notification as previous message (%1)").arg(i)); |
| 81 | QTest::qWait(100); |
| 82 | } |
| 83 | QCOMPARE(error, QString()); |
| 84 | n.setImage(QIcon::fromTheme(QStringLiteral("document-open")).pixmap(64).toImage()); |
| 85 | n.setTitle(n.title() + QStringLiteral(" - click action to continue")); |
| 86 | n.setActions(QStringList({ QStringLiteral("fail"), QStringLiteral("Let test fail"), QStringLiteral("pass"), QStringLiteral("Let test pass") })); |
| 87 | QSignalSpy actionInvokedSpy(&n, &DBusNotification::actionInvoked); |
| 88 | n.update(QStringLiteral("Click on \"Let test pass\" to continue within 10 seconds")); |
| 89 | actionInvokedSpy.wait(10000); |
| 90 | QCOMPARE(clickedAction, QStringLiteral("pass")); |
| 91 | QSignalSpy closedSpy(&n, &DBusNotification::closed); |
| 92 | n.setTimeout(5000); |
| 93 | n.setActions(QStringList()); |
| 94 | n.update(QStringLiteral("Waiting for message to close (will close automatically in 5 seconds)")); |
| 95 | closedSpy.wait(8000); |
| 96 | |
| 97 | QCOMPARE(error, QString()); |
| 98 | disconnect(actionConnection); |
| 99 | disconnect(errorConnection); |
| 100 | } |
| 101 | |
| 102 | QT_UTILITIES_DISABLE_WARNINGS_FOR_MOC_INCLUDE |
| 103 | QTEST_MAIN(DBusNotificationTests) |
| 104 | #include "dbusnotification.moc" |