Changeset View
Changeset View
Standalone View
Standalone View
autotests/kdeplatformsystemtrayicon_unittest.cpp
- This file was added.
1 | /* This file is part of the KDE libraries | ||||
---|---|---|---|---|---|
2 | * Copyright 2019 Konrad Materka <materka@gmail.com> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU Lesser General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License or ( at | ||||
7 | * your option ) version 3 or, at the discretion of KDE e.V. ( which shall | ||||
8 | * act as a proxy as in section 14 of the GPLv3 ), any later version. | ||||
9 | * | ||||
10 | * This library is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | * Library General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Lesser General Public License | ||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QMenu> | ||||
22 | #include <QSignalSpy> | ||||
23 | #include <QTest> | ||||
24 | | ||||
25 | #include "../src/platformtheme/kdeplatformsystemtrayicon.h" | ||||
26 | | ||||
27 | class KDEPlatformSystemTrayIcon_UnitTest : public QObject | ||||
28 | { | ||||
29 | Q_OBJECT | ||||
30 | private Q_SLOTS: | ||||
31 | // test for BUG 365105 | ||||
32 | void testMenuRecreate(); | ||||
33 | void testAddActionAfterMenuRecreate(); | ||||
34 | }; | ||||
35 | | ||||
36 | void KDEPlatformSystemTrayIcon_UnitTest::testMenuRecreate() | ||||
37 | { | ||||
38 | QMenu *trayIconMenu = new QMenu(); | ||||
39 | trayIconMenu->addAction("testAction"); | ||||
40 | | ||||
41 | KDEPlatformSystemTrayIcon *kpsti = new KDEPlatformSystemTrayIcon(); | ||||
42 | | ||||
43 | // simulates first QSystemTrayIcon::show() | ||||
44 | kpsti->init(); | ||||
45 | SystemTrayMenu *ourMenu = qobject_cast<SystemTrayMenu*>(kpsti->createMenu()); | ||||
46 | trayIconMenu->setPlatformMenu(ourMenu); | ||||
47 | kpsti->updateMenu(trayIconMenu->platformMenu()); | ||||
48 | | ||||
49 | QMenu *firstMenu = ourMenu->menu(); | ||||
50 | QSignalSpy menuDestroyedSpy(firstMenu, SIGNAL(destroyed())); | ||||
51 | QCOMPARE(firstMenu->actions().size(), 1); | ||||
52 | QCOMPARE(firstMenu->actions().first()->text(), "testAction"); | ||||
53 | | ||||
54 | // simulates QSystemTrayIcon::hide() | ||||
55 | kpsti->cleanup(); | ||||
56 | | ||||
57 | // simulates second QSystemTrayIcon::show() | ||||
58 | kpsti->init(); | ||||
59 | kpsti->updateMenu(trayIconMenu->platformMenu()); | ||||
60 | | ||||
61 | QMenu *recreatedMenu = ourMenu->menu(); | ||||
62 | QVERIFY(firstMenu != recreatedMenu); | ||||
63 | QCOMPARE(recreatedMenu->actions().size(), 1); | ||||
64 | QCOMPARE(recreatedMenu->actions().first()->text(), "testAction"); | ||||
65 | QCOMPARE(menuDestroyedSpy.count(), 1); | ||||
66 | } | ||||
67 | | ||||
68 | void KDEPlatformSystemTrayIcon_UnitTest::testAddActionAfterMenuRecreate() | ||||
69 | { | ||||
70 | QMenu *trayIconMenu = new QMenu(); | ||||
71 | trayIconMenu->addAction("testAction1"); | ||||
72 | | ||||
73 | KDEPlatformSystemTrayIcon *kpsti = new KDEPlatformSystemTrayIcon(); | ||||
74 | | ||||
75 | // simulates first QSystemTrayIcon::show() | ||||
76 | kpsti->init(); | ||||
77 | SystemTrayMenu *ourMenu = qobject_cast<SystemTrayMenu*>(kpsti->createMenu()); | ||||
78 | trayIconMenu->setPlatformMenu(ourMenu); | ||||
79 | kpsti->updateMenu(trayIconMenu->platformMenu()); | ||||
80 | | ||||
81 | QMenu *firstMenu = ourMenu->menu(); | ||||
82 | QSignalSpy menuDestroyedSpy(firstMenu, SIGNAL(destroyed())); | ||||
83 | QCOMPARE(firstMenu->actions().size(), 1); | ||||
84 | QCOMPARE(firstMenu->actions().first()->text(), "testAction1"); | ||||
85 | | ||||
86 | // simulates QSystemTrayIcon::hide() | ||||
87 | kpsti->cleanup(); | ||||
88 | | ||||
89 | // add action, internal menu is destroyed | ||||
90 | trayIconMenu->addAction("testAction2"); | ||||
91 | | ||||
92 | // simulates second QSystemTrayIcon::show() | ||||
93 | kpsti->init(); | ||||
94 | kpsti->updateMenu(trayIconMenu->platformMenu()); | ||||
95 | | ||||
96 | QMenu *recreatedMenu = ourMenu->menu(); | ||||
97 | QVERIFY(firstMenu != recreatedMenu); | ||||
98 | QCOMPARE(recreatedMenu->actions().size(), 2); | ||||
99 | QCOMPARE(recreatedMenu->actions().first()->text(), "testAction1"); | ||||
100 | QCOMPARE(recreatedMenu->actions().last()->text(), "testAction2"); | ||||
101 | QCOMPARE(menuDestroyedSpy.count(), 1); | ||||
102 | } | ||||
103 | | ||||
104 | QTEST_MAIN(KDEPlatformSystemTrayIcon_UnitTest) | ||||
105 | | ||||
106 | #include "kdeplatformsystemtrayicon_unittest.moc" |