| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | |
| 30 | #include <QtTest/QtTest> |
| 31 | |
| 32 | #include <qguiapplication.h> |
| 33 | #include <qdebug.h> |
| 34 | #include <qsystemtrayicon.h> |
| 35 | #include <qmenu.h> |
| 36 | |
| 37 | class tst_QSystemTrayIcon: public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | tst_QSystemTrayIcon(); |
| 43 | virtual ~tst_QSystemTrayIcon(); |
| 44 | |
| 45 | private slots: |
| 46 | void getSetCheck(); |
| 47 | void showHide(); |
| 48 | void showMessage(); |
| 49 | void supportsMessages(); |
| 50 | void lastWindowClosed(); |
| 51 | }; |
| 52 | |
| 53 | tst_QSystemTrayIcon::tst_QSystemTrayIcon() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | tst_QSystemTrayIcon::~tst_QSystemTrayIcon() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | // Testing get/set functions |
| 62 | void tst_QSystemTrayIcon::showHide() |
| 63 | { |
| 64 | QSystemTrayIcon icon; |
| 65 | icon.setIcon(QIcon("icons/icon.png" )); |
| 66 | icon.show(); |
| 67 | icon.setIcon(QIcon("icons/icon.png" )); |
| 68 | icon.hide(); |
| 69 | } |
| 70 | |
| 71 | // Testing get/set functions |
| 72 | void tst_QSystemTrayIcon::showMessage() |
| 73 | { |
| 74 | QSystemTrayIcon icon; |
| 75 | icon.setIcon(QIcon("icons/icon.png" )); |
| 76 | |
| 77 | icon.showMessage(title: "Title" , msg: "Messagecontents" ); |
| 78 | icon.showMessage(title: "Title" , msg: "Messagecontents" , icon: QSystemTrayIcon::NoIcon); |
| 79 | icon.showMessage(title: "Title" , msg: "Messagecontents" , icon: QSystemTrayIcon::Warning); |
| 80 | icon.showMessage(title: "Title" , msg: "Messagecontents" , icon: QSystemTrayIcon::Critical); |
| 81 | |
| 82 | icon.show(); |
| 83 | icon.showMessage(title: "Title" , msg: "Messagecontents" ); |
| 84 | icon.showMessage(title: "Title" , msg: "Messagecontents" , icon: QSystemTrayIcon::NoIcon); |
| 85 | icon.showMessage(title: "Title" , msg: "Messagecontents" , icon: QSystemTrayIcon::Warning); |
| 86 | icon.showMessage(title: "Title" , msg: "Messagecontents" , icon: QSystemTrayIcon::Critical); |
| 87 | } |
| 88 | |
| 89 | // Testing get/set functions |
| 90 | void tst_QSystemTrayIcon::getSetCheck() |
| 91 | { |
| 92 | QSystemTrayIcon icon; |
| 93 | QCOMPARE(true, icon.toolTip().isEmpty()); |
| 94 | icon.setToolTip("testToolTip" ); |
| 95 | QCOMPARE(true, "testToolTip" == icon.toolTip()); |
| 96 | |
| 97 | QCOMPARE(true, icon.icon().isNull()); |
| 98 | icon.setIcon(QIcon("icons/icon.png" )); |
| 99 | QCOMPARE(false, icon.icon().isNull()); |
| 100 | |
| 101 | QMenu ; |
| 102 | QCOMPARE(true, icon.contextMenu() == 0); |
| 103 | icon.setContextMenu(&menu); |
| 104 | QCOMPARE(false, icon.contextMenu() == 0); |
| 105 | } |
| 106 | |
| 107 | void tst_QSystemTrayIcon::supportsMessages() |
| 108 | { |
| 109 | if (QGuiApplication::platformName().startsWith(s: QLatin1String("wayland" ), cs: Qt::CaseInsensitive)) |
| 110 | QSKIP("Wayland: This fails. Figure out why." ); |
| 111 | |
| 112 | // ### fixme: Check platforms. |
| 113 | const QString platform = QGuiApplication::platformName(); |
| 114 | if (platform.compare(QStringLiteral("xcb" ), cs: Qt::CaseInsensitive) |
| 115 | && platform.compare(QStringLiteral("windows" ), cs: Qt::CaseInsensitive) |
| 116 | && platform.compare(QStringLiteral("cocoa" ), cs: Qt::CaseInsensitive)) { |
| 117 | QEXPECT_FAIL("" , "QTBUG-20978 QSystemTrayIcon is unimplemented for this platform" , Abort); |
| 118 | } |
| 119 | QCOMPARE(QSystemTrayIcon::supportsMessages(), true); |
| 120 | } |
| 121 | |
| 122 | void tst_QSystemTrayIcon::lastWindowClosed() |
| 123 | { |
| 124 | QSignalSpy spy(qApp, &QApplication::lastWindowClosed); |
| 125 | QWidget window; |
| 126 | QSystemTrayIcon icon; |
| 127 | icon.setIcon(QIcon("whatever.png" )); |
| 128 | icon.show(); |
| 129 | window.show(); |
| 130 | QTimer::singleShot(msec: 2500, receiver: &window, SLOT(close())); |
| 131 | QTimer::singleShot(msec: 20000, qApp, SLOT(quit())); // in case the test fails |
| 132 | qApp->exec(); |
| 133 | QCOMPARE(spy.count(), 1); |
| 134 | } |
| 135 | |
| 136 | QTEST_MAIN(tst_QSystemTrayIcon) |
| 137 | #include "tst_qsystemtrayicon.moc" |
| 138 | |