| 1 | /* This file is part of the dbusmenu-qt library |
| 2 | Copyright 2009 Canonical |
| 3 | Author: Aurelien Gateau <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Library General Public |
| 7 | License (LGPL) as published by the Free Software Foundation; |
| 8 | either version 2 of the License, or (at your option) any later |
| 9 | version. |
| 10 | |
| 11 | This library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Library General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Library General Public License |
| 17 | along with this library; see the file COPYING.LIB. If not, write to |
| 18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | // Self |
| 22 | #include "dbusmenuexportertest.h" |
| 23 | |
| 24 | // Qt |
| 25 | #include <QDBusConnection> |
| 26 | #include <QDBusInterface> |
| 27 | #include <QDBusReply> |
| 28 | #include <QIcon> |
| 29 | #include <QMenu> |
| 30 | #include <QtTest> |
| 31 | |
| 32 | // DBusMenuQt |
| 33 | #include <dbusmenuexporter.h> |
| 34 | #include <dbusmenutypes_p.h> |
| 35 | #include <dbusmenushortcut_p.h> |
| 36 | #include <debug_p.h> |
| 37 | |
| 38 | // Local |
| 39 | #include "testutils.h" |
| 40 | |
| 41 | QTEST_MAIN(DBusMenuExporterTest) |
| 42 | |
| 43 | static const char *TEST_SERVICE = "org.kde.dbusmenu-qt-test"; |
| 44 | static const char *TEST_OBJECT_PATH = "/TestMenuBar"; |
| 45 | |
| 46 | Q_DECLARE_METATYPE(QList<int>) |
| 47 | |
| 48 | static DBusMenuLayoutItemList getChildren(QDBusAbstractInterface* iface, int parentId, const QStringList &propertyNames) |
| 49 | { |
| 50 | QDBusPendingReply<uint, DBusMenuLayoutItem> reply = iface->call("GetLayout", parentId, /*recursionDepth=*/ 1, propertyNames); |
| 51 | reply.waitForFinished(); |
| 52 | if (!reply.isValid()) { |
| 53 | qFatal("%s", qPrintable(reply.error().message())); |
| 54 | return DBusMenuLayoutItemList(); |
| 55 | } |
| 56 | |
| 57 | DBusMenuLayoutItem rootItem = reply.argumentAt<1>(); |
| 58 | return rootItem.children; |
| 59 | } |
| 60 | |
| 61 | void DBusMenuExporterTest::init() |
| 62 | { |
| 63 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 64 | QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, false); |
| 65 | } |
| 66 | |
| 67 | void DBusMenuExporterTest::cleanup() |
| 68 | { |
| 69 | QVERIFY(QDBusConnection::sessionBus().unregisterService(TEST_SERVICE)); |
| 70 | } |
| 71 | |
| 72 | void DBusMenuExporterTest::testGetSomeProperties_data() |
| 73 | { |
| 74 | QTest::addColumn<QString>("label"); |
| 75 | QTest::addColumn<QString>("iconName"); |
| 76 | QTest::addColumn<bool>("enabled"); |
| 77 | |
| 78 | QTest::newRow("label only") << "label" << QString() << true; |
| 79 | QTest::newRow("disabled, label only") << "label" << QString() << false; |
| 80 | QTest::newRow("icon name") << "label" << "edit-undo" << true; |
| 81 | } |
| 82 | |
| 83 | void DBusMenuExporterTest::testGetSomeProperties() |
| 84 | { |
| 85 | QFETCH(QString, label); |
| 86 | QFETCH(QString, iconName); |
| 87 | QFETCH(bool, enabled); |
| 88 | |
| 89 | // Create an exporter for a menu with one action, defined by the test data |
| 90 | QMenu inputMenu; |
| 91 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 92 | |
| 93 | QAction *action = new QAction(label, &inputMenu); |
| 94 | if (!iconName.isEmpty()) { |
| 95 | QIcon icon = QIcon::fromTheme(iconName); |
| 96 | QVERIFY(!icon.isNull()); |
| 97 | action->setIcon(icon); |
| 98 | } |
| 99 | action->setEnabled(enabled); |
| 100 | inputMenu.addAction(action); |
| 101 | |
| 102 | // Check out exporter is on DBus |
| 103 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 104 | QVERIFY2(iface.isValid(), qPrintable(iface.lastError().message())); |
| 105 | |
| 106 | // Get exported menu info |
| 107 | QStringList propertyNames = QStringList() << "type" << "enabled" << "label" << "icon-name"; |
| 108 | DBusMenuLayoutItemList list = getChildren(&iface, /*parentId=*/0, propertyNames); |
| 109 | DBusMenuLayoutItem item = list.first(); |
| 110 | QVERIFY(item.id != 0); |
| 111 | QVERIFY(item.children.isEmpty()); |
| 112 | QVERIFY(!item.properties.contains("type")); |
| 113 | QCOMPARE(item.properties.value("label").toString(), label); |
| 114 | if (enabled) { |
| 115 | QVERIFY(!item.properties.contains("enabled")); |
| 116 | } else { |
| 117 | QCOMPARE(item.properties.value("enabled").toBool(), false); |
| 118 | } |
| 119 | if (iconName.isEmpty()) { |
| 120 | QVERIFY(!item.properties.contains("icon-name")); |
| 121 | } else { |
| 122 | QCOMPARE(item.properties.value("icon-name").toString(), iconName); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void DBusMenuExporterTest::testGetAllProperties() |
| 127 | { |
| 128 | // set of properties which must be returned because their values are not |
| 129 | // the default values |
| 130 | const QSet<QString> a1Properties = QSet<QString>() |
| 131 | << "label" |
| 132 | ; |
| 133 | |
| 134 | const QSet<QString> separatorProperties = QSet<QString>() |
| 135 | << "type"; |
| 136 | |
| 137 | const QSet<QString> a2Properties = QSet<QString>() |
| 138 | << "label" |
| 139 | << "enabled" |
| 140 | << "icon-name" |
| 141 | << "icon-data" // Icon data is always provided if the icon is valid. |
| 142 | << "visible" |
| 143 | ; |
| 144 | |
| 145 | // Create the menu items |
| 146 | QMenu inputMenu; |
| 147 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 148 | |
| 149 | inputMenu.addAction("a1"); |
| 150 | |
| 151 | inputMenu.addSeparator(); |
| 152 | |
| 153 | QAction *a2 = new QAction("a2", &inputMenu); |
| 154 | a2->setEnabled(false); |
| 155 | QIcon icon = QIcon::fromTheme("edit-undo"); |
| 156 | QVERIFY(!icon.isNull()); |
| 157 | a2->setIcon(icon); |
| 158 | a2->setVisible(false); |
| 159 | inputMenu.addAction(a2); |
| 160 | |
| 161 | // Export them |
| 162 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 163 | QVERIFY2(iface.isValid(), qPrintable(iface.lastError().message())); |
| 164 | |
| 165 | // Get children |
| 166 | DBusMenuLayoutItemList list = getChildren(&iface, 0, QStringList()); |
| 167 | QCOMPARE(list.count(), 3); |
| 168 | |
| 169 | // Check we get the right properties |
| 170 | DBusMenuLayoutItem item = list.takeFirst(); |
| 171 | QCOMPARE(QSet<QString>::fromList(item.properties.keys()), a1Properties); |
| 172 | |
| 173 | item = list.takeFirst(); |
| 174 | QCOMPARE(QSet<QString>::fromList(item.properties.keys()), separatorProperties); |
| 175 | |
| 176 | item = list.takeFirst(); |
| 177 | QCOMPARE(QSet<QString>::fromList(item.properties.keys()), a2Properties); |
| 178 | } |
| 179 | |
| 180 | void DBusMenuExporterTest::testGetNonExistentProperty() |
| 181 | { |
| 182 | const char* NON_EXISTENT_KEY = "i-do-not-exist"; |
| 183 | |
| 184 | QMenu inputMenu; |
| 185 | inputMenu.addAction("a1"); |
| 186 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 187 | |
| 188 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 189 | DBusMenuLayoutItemList list = getChildren(&iface, 0, QStringList() << NON_EXISTENT_KEY); |
| 190 | QCOMPARE(list.count(), 1); |
| 191 | |
| 192 | DBusMenuLayoutItem item = list.takeFirst(); |
| 193 | QVERIFY(!item.properties.contains(NON_EXISTENT_KEY)); |
| 194 | } |
| 195 | |
| 196 | void DBusMenuExporterTest::testClickedEvent() |
| 197 | { |
| 198 | QMenu inputMenu; |
| 199 | QAction *action = inputMenu.addAction("a1"); |
| 200 | QSignalSpy spy(action, SIGNAL(triggered())); |
| 201 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 202 | |
| 203 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 204 | DBusMenuLayoutItemList list = getChildren(&iface, 0, QStringList()); |
| 205 | QCOMPARE(list.count(), 1); |
| 206 | int id = list.first().id; |
| 207 | |
| 208 | QVariant empty = QVariant::fromValue(QDBusVariant(QString())); |
| 209 | uint timestamp = QDateTime::currentDateTime().toTime_t(); |
| 210 | iface.call("Event", id, "clicked", empty, timestamp); |
| 211 | QTest::qWait(500); |
| 212 | |
| 213 | QCOMPARE(spy.count(), 1); |
| 214 | } |
| 215 | |
| 216 | void DBusMenuExporterTest::testSubMenu() |
| 217 | { |
| 218 | QMenu inputMenu; |
| 219 | QMenu *subMenu = inputMenu.addMenu("menu"); |
| 220 | QAction *a1 = subMenu->addAction("a1"); |
| 221 | QAction *a2 = subMenu->addAction("a2"); |
| 222 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 223 | |
| 224 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 225 | DBusMenuLayoutItemList list = getChildren(&iface, 0, QStringList()); |
| 226 | QCOMPARE(list.count(), 1); |
| 227 | int id = list.first().id; |
| 228 | |
| 229 | list = getChildren(&iface, id, QStringList()); |
| 230 | QCOMPARE(list.count(), 2); |
| 231 | |
| 232 | DBusMenuLayoutItem item = list.takeFirst(); |
| 233 | QVERIFY(item.id != 0); |
| 234 | QCOMPARE(item.properties.value("label").toString(), a1->text()); |
| 235 | |
| 236 | item = list.takeFirst(); |
| 237 | QCOMPARE(item.properties.value("label").toString(), a2->text()); |
| 238 | } |
| 239 | |
| 240 | void DBusMenuExporterTest::testDynamicSubMenu() |
| 241 | { |
| 242 | // Track LayoutUpdated() signal: we don't want this signal to be emitted |
| 243 | // too often because it causes refreshes |
| 244 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 245 | ManualSignalSpy layoutUpdatedSpy; |
| 246 | QDBusConnection::sessionBus().connect(TEST_SERVICE, TEST_OBJECT_PATH, "com.canonical.dbusmenu", "LayoutUpdated", "ui", &layoutUpdatedSpy, SLOT(receiveCall(uint, int))); |
| 247 | |
| 248 | // Create our test menu |
| 249 | QMenu inputMenu; |
| 250 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 251 | QAction *action = inputMenu.addAction("menu"); |
| 252 | QMenu *subMenu = new QMenu(&inputMenu); |
| 253 | action->setMenu(subMenu); |
| 254 | MenuFiller filler(subMenu); |
| 255 | filler.addAction(new QAction("a1", subMenu)); |
| 256 | filler.addAction(new QAction("a2", subMenu)); |
| 257 | |
| 258 | // Get id of submenu |
| 259 | DBusMenuLayoutItemList list = getChildren(&iface, 0, QStringList()); |
| 260 | QCOMPARE(list.count(), 1); |
| 261 | int id = list.first().id; |
| 262 | |
| 263 | // Nothing for now |
| 264 | QCOMPARE(subMenu->actions().count(), 0); |
| 265 | |
| 266 | // LayoutUpdated should be emitted once because inputMenu is filled |
| 267 | QTest::qWait(500); |
| 268 | QCOMPARE(layoutUpdatedSpy.count(), 1); |
| 269 | QCOMPARE(layoutUpdatedSpy.takeFirst().at(1).toInt(), 0); |
| 270 | |
| 271 | // Pretend we show the menu |
| 272 | QDBusReply<bool> aboutToShowReply = iface.call("AboutToShow", id); |
| 273 | QVERIFY2(aboutToShowReply.isValid(), qPrintable(aboutToShowReply.error().message())); |
| 274 | QVERIFY(aboutToShowReply.value()); |
| 275 | QTest::qWait(500); |
| 276 | QCOMPARE(layoutUpdatedSpy.count(), 1); |
| 277 | QCOMPARE(layoutUpdatedSpy.takeFirst().at(1).toInt(), id); |
| 278 | |
| 279 | // Get submenu items |
| 280 | list = getChildren(&iface, id, QStringList()); |
| 281 | QVERIFY(subMenu->actions().count() > 0); |
| 282 | QCOMPARE(list.count(), subMenu->actions().count()); |
| 283 | |
| 284 | for (int pos=0; pos< list.count(); ++pos) { |
| 285 | DBusMenuLayoutItem item = list.at(pos); |
| 286 | QVERIFY(item.id != 0); |
| 287 | QAction *action = subMenu->actions().at(pos); |
| 288 | QVERIFY(action); |
| 289 | QCOMPARE(item.properties.value("label").toString(), action->text()); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void DBusMenuExporterTest::testRadioItems() |
| 294 | { |
| 295 | DBusMenuLayoutItem item; |
| 296 | DBusMenuLayoutItemList list; |
| 297 | QMenu inputMenu; |
| 298 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 299 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 300 | |
| 301 | // Create 2 radio items, check first one |
| 302 | QAction *a1 = inputMenu.addAction("a1"); |
| 303 | a1->setCheckable(true); |
| 304 | QAction *a2 = inputMenu.addAction("a1"); |
| 305 | a2->setCheckable(true); |
| 306 | |
| 307 | QActionGroup group(0); |
| 308 | group.addAction(a1); |
| 309 | group.addAction(a2); |
| 310 | a1->setChecked(true); |
| 311 | |
| 312 | QVERIFY(!a2->isChecked()); |
| 313 | |
| 314 | // Get item ids |
| 315 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 316 | list = getChildren(&iface, 0, QStringList()); |
| 317 | QCOMPARE(list.count(), 2); |
| 318 | |
| 319 | // Check items are radios and correctly toggled |
| 320 | item = list.takeFirst(); |
| 321 | QCOMPARE(item.properties.value("toggle-type").toString(), QString("radio")); |
| 322 | QCOMPARE(item.properties.value("toggle-state").toInt(), 1); |
| 323 | int a1Id = item.id; |
| 324 | item = list.takeFirst(); |
| 325 | QCOMPARE(item.properties.value("toggle-type").toString(), QString("radio")); |
| 326 | QCOMPARE(item.properties.value("toggle-state").toInt(), 0); |
| 327 | int a2Id = item.id; |
| 328 | |
| 329 | // Click a2 |
| 330 | ManualSignalSpy spy; |
| 331 | QDBusConnection::sessionBus().connect(TEST_SERVICE, TEST_OBJECT_PATH, "com.canonical.dbusmenu", "ItemsPropertiesUpdated", "a(ia{sv})a(ias)", |
| 332 | &spy, SLOT(receiveCall(DBusMenuItemList, DBusMenuItemKeysList))); |
| 333 | QVariant empty = QVariant::fromValue(QDBusVariant(QString())); |
| 334 | uint timestamp = QDateTime::currentDateTime().toTime_t(); |
| 335 | iface.call("Event", a2Id, "clicked", empty, timestamp); |
| 336 | QTest::qWait(500); |
| 337 | |
| 338 | // Check a1 is not checked, but a2 is |
| 339 | list = getChildren(&iface, 0, QStringList()); |
| 340 | QCOMPARE(list.count(), 2); |
| 341 | |
| 342 | item = list.takeFirst(); |
| 343 | QCOMPARE(item.properties.value("toggle-state").toInt(), 0); |
| 344 | |
| 345 | item = list.takeFirst(); |
| 346 | QCOMPARE(item.properties.value("toggle-state").toInt(), 1); |
| 347 | |
| 348 | // Did we get notified? |
| 349 | QCOMPARE(spy.count(), 1); |
| 350 | QSet<int> updatedIds; |
| 351 | { |
| 352 | QVariantList lst = spy.takeFirst().at(0).toList(); |
| 353 | Q_FOREACH(QVariant variant, lst) { |
| 354 | updatedIds << variant.toInt(); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | QSet<int> expectedIds; |
| 359 | expectedIds << a1Id << a2Id; |
| 360 | |
| 361 | QCOMPARE(updatedIds, expectedIds); |
| 362 | } |
| 363 | |
| 364 | void DBusMenuExporterTest::testNonExclusiveActionGroup() |
| 365 | { |
| 366 | DBusMenuLayoutItem item; |
| 367 | DBusMenuLayoutItemList list; |
| 368 | QMenu inputMenu; |
| 369 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 370 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 371 | |
| 372 | // Create 2 checkable items |
| 373 | QAction *a1 = inputMenu.addAction("a1"); |
| 374 | a1->setCheckable(true); |
| 375 | QAction *a2 = inputMenu.addAction("a1"); |
| 376 | a2->setCheckable(true); |
| 377 | |
| 378 | // Put them into a non exclusive group |
| 379 | QActionGroup group(0); |
| 380 | group.addAction(a1); |
| 381 | group.addAction(a2); |
| 382 | group.setExclusive(false); |
| 383 | |
| 384 | // Get item ids |
| 385 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 386 | list = getChildren(&iface, 0, QStringList()); |
| 387 | QCOMPARE(list.count(), 2); |
| 388 | |
| 389 | // Check items are checkmark, not radio |
| 390 | item = list.takeFirst(); |
| 391 | QCOMPARE(item.properties.value("toggle-type").toString(), QString("checkmark")); |
| 392 | int a1Id = item.id; |
| 393 | item = list.takeFirst(); |
| 394 | QCOMPARE(item.properties.value("toggle-type").toString(), QString("checkmark")); |
| 395 | int a2Id = item.id; |
| 396 | } |
| 397 | |
| 398 | void DBusMenuExporterTest::testClickDeletedAction() |
| 399 | { |
| 400 | QMenu inputMenu; |
| 401 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 402 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 403 | |
| 404 | QAction *a1 = inputMenu.addAction("a1"); |
| 405 | |
| 406 | // Get id |
| 407 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 408 | DBusMenuLayoutItemList list = getChildren(&iface, 0, QStringList()); |
| 409 | QCOMPARE(list.count(), 1); |
| 410 | int id = list.takeFirst().id; |
| 411 | |
| 412 | // Delete a1, it should not cause a crash when trying to trigger it |
| 413 | delete a1; |
| 414 | |
| 415 | // Send a click to deleted a1 |
| 416 | QVariant empty = QVariant::fromValue(QDBusVariant(QString())); |
| 417 | uint timestamp = QDateTime::currentDateTime().toTime_t(); |
| 418 | iface.call("Event", id, "clicked", empty, timestamp); |
| 419 | QTest::qWait(500); |
| 420 | } |
| 421 | |
| 422 | // Reproduce LP BUG 521011 |
| 423 | // https://bugs.launchpad.net/bugs/521011 |
| 424 | void DBusMenuExporterTest::testDeleteExporterBeforeMenu() |
| 425 | { |
| 426 | QMenu inputMenu; |
| 427 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 428 | DBusMenuExporter *exporter = new DBusMenuExporter(TEST_OBJECT_PATH, &inputMenu); |
| 429 | |
| 430 | QAction *a1 = inputMenu.addAction("a1"); |
| 431 | delete exporter; |
| 432 | inputMenu.removeAction(a1); |
| 433 | } |
| 434 | |
| 435 | void DBusMenuExporterTest::testUpdateAndDeleteSubMenu() |
| 436 | { |
| 437 | // Create a menu with a submenu |
| 438 | QMenu inputMenu; |
| 439 | QMenu *subMenu = inputMenu.addMenu("menu"); |
| 440 | QAction *a1 = subMenu->addAction("a1"); |
| 441 | |
| 442 | // Export it |
| 443 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 444 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 445 | |
| 446 | // Update a1 (which is in subMenu) and delete subMenu right after that. If |
| 447 | // DBusMenuExporter is not careful it will crash in the qWait() because it |
| 448 | // tries to send itemUpdated() for a1. |
| 449 | a1->setText("Not a menu anymore"); |
| 450 | delete subMenu; |
| 451 | QTest::qWait(500); |
| 452 | } |
| 453 | |
| 454 | void DBusMenuExporterTest::testMenuShortcut() |
| 455 | { |
| 456 | // Create a menu containing an action with a shortcut |
| 457 | QMenu inputMenu; |
| 458 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 459 | DBusMenuExporter *exporter = new DBusMenuExporter(TEST_OBJECT_PATH, &inputMenu); |
| 460 | |
| 461 | QAction *a1 = inputMenu.addAction("a1"); |
| 462 | a1->setShortcut(Qt::CTRL | Qt::Key_A); |
| 463 | |
| 464 | QAction *a2 = inputMenu.addAction("a2"); |
| 465 | a2->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_A, Qt::ALT | Qt::Key_B)); |
| 466 | |
| 467 | // No shortcut, to test the property is not added in this case |
| 468 | QAction *a3 = inputMenu.addAction("a3"); |
| 469 | |
| 470 | QList<QAction*> actionList; |
| 471 | actionList << a1 << a2 << a3; |
| 472 | |
| 473 | // Check out exporter is on DBus |
| 474 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 475 | QVERIFY2(iface.isValid(), qPrintable(iface.lastError().message())); |
| 476 | |
| 477 | // Get exported menu info |
| 478 | QStringList propertyNames = QStringList() << "label" << "shortcut"; |
| 479 | DBusMenuLayoutItemList list = getChildren(&iface, 0, propertyNames); |
| 480 | QCOMPARE(list.count(), actionList.count()); |
| 481 | |
| 482 | Q_FOREACH(const QAction* action, actionList) { |
| 483 | DBusMenuLayoutItem item = list.takeFirst(); |
| 484 | if (action->shortcut().isEmpty()) { |
| 485 | QVERIFY(!item.properties.contains("shortcut")); |
| 486 | } else { |
| 487 | QVERIFY(item.properties.contains("shortcut")); |
| 488 | QDBusArgument arg = item.properties.value("shortcut").value<QDBusArgument>(); |
| 489 | DBusMenuShortcut shortcut; |
| 490 | arg >> shortcut; |
| 491 | QCOMPARE(shortcut.toKeySequence(), action->shortcut()); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void DBusMenuExporterTest::testGetGroupProperties() |
| 497 | { |
| 498 | // Create a menu containing two actions |
| 499 | QMenu inputMenu; |
| 500 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 501 | DBusMenuExporter *exporter = new DBusMenuExporter(TEST_OBJECT_PATH, &inputMenu); |
| 502 | |
| 503 | QAction *a1 = inputMenu.addAction("a1"); |
| 504 | QAction *a2 = inputMenu.addAction("a2"); |
| 505 | |
| 506 | // Check exporter is on DBus |
| 507 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 508 | QVERIFY2(iface.isValid(), qPrintable(iface.lastError().message())); |
| 509 | |
| 510 | // Get item ids |
| 511 | DBusMenuLayoutItemList list = getChildren(&iface, 0, QStringList()); |
| 512 | QCOMPARE(list.count(), inputMenu.actions().count()); |
| 513 | |
| 514 | int id1 = list.at(0).id; |
| 515 | int id2 = list.at(1).id; |
| 516 | |
| 517 | // Get group properties |
| 518 | QList<int> ids = QList<int>() << id1 << id2; |
| 519 | QDBusReply<DBusMenuItemList> reply = iface.call("GetGroupProperties", QVariant::fromValue(ids), QStringList()); |
| 520 | QVERIFY2(reply.isValid(), qPrintable(reply.error().message())); |
| 521 | DBusMenuItemList groupPropertiesList = reply.value(); |
| 522 | |
| 523 | // Check the info we received |
| 524 | QCOMPARE(groupPropertiesList.count(), inputMenu.actions().count()); |
| 525 | |
| 526 | Q_FOREACH(const QAction* action, inputMenu.actions()) { |
| 527 | DBusMenuItem item = groupPropertiesList.takeFirst(); |
| 528 | QCOMPARE(item.properties.value("label").toString(), action->text()); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void DBusMenuExporterTest::testActivateAction() |
| 533 | { |
| 534 | // Create a menu containing two actions |
| 535 | QMenu inputMenu; |
| 536 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 537 | DBusMenuExporter *exporter = new DBusMenuExporter(TEST_OBJECT_PATH, &inputMenu); |
| 538 | |
| 539 | QAction *a1 = inputMenu.addAction("a1"); |
| 540 | QAction *a2 = inputMenu.addAction("a2"); |
| 541 | |
| 542 | // Check exporter is on DBus |
| 543 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 544 | QVERIFY2(iface.isValid(), qPrintable(iface.lastError().message())); |
| 545 | |
| 546 | ManualSignalSpy spy; |
| 547 | QDBusConnection::sessionBus().connect(TEST_SERVICE, TEST_OBJECT_PATH, "com.canonical.dbusmenu", "ItemActivationRequested", "iu", &spy, SLOT(receiveCall(int, uint))); |
| 548 | |
| 549 | // Get item ids |
| 550 | DBusMenuLayoutItemList list = getChildren(&iface, 0, QStringList()); |
| 551 | QCOMPARE(list.count(), inputMenu.actions().count()); |
| 552 | |
| 553 | int id1 = list.at(0).id; |
| 554 | int id2 = list.at(1).id; |
| 555 | |
| 556 | // Trigger actions |
| 557 | exporter->activateAction(a1); |
| 558 | exporter->activateAction(a2); |
| 559 | |
| 560 | // Check we received the signals in the correct order |
| 561 | QTest::qWait(500); |
| 562 | QCOMPARE(spy.count(), 2); |
| 563 | QCOMPARE(spy.takeFirst().at(0).toInt(), id1); |
| 564 | QCOMPARE(spy.takeFirst().at(0).toInt(), id2); |
| 565 | } |
| 566 | |
| 567 | static int trackCount(QMenu* menu) |
| 568 | { |
| 569 | QList<QObject*> lst = menu->findChildren<QObject*>(); |
| 570 | int count = 0; |
| 571 | Q_FOREACH(QObject* child, lst) { |
| 572 | if (qstrcmp(child->metaObject()->className(), "DBusMenu") == 0) { |
| 573 | ++count; |
| 574 | } |
| 575 | } |
| 576 | return count; |
| 577 | } |
| 578 | |
| 579 | // Check we do not create more than one DBusMenu object for each menu |
| 580 | // See KDE bug 254066 |
| 581 | void DBusMenuExporterTest::testTrackActionsOnlyOnce() |
| 582 | { |
| 583 | // Create a menu with a submenu, unplug the submenu and plug it back. The |
| 584 | // submenu should not have more than one DBusMenu child object. |
| 585 | QMenu mainMenu; |
| 586 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 587 | DBusMenuExporter *exporter = new DBusMenuExporter(TEST_OBJECT_PATH, &mainMenu); |
| 588 | |
| 589 | QMenu* subMenu = new QMenu("File"); |
| 590 | subMenu->addAction("a1"); |
| 591 | mainMenu.addAction(subMenu->menuAction()); |
| 592 | |
| 593 | QTest::qWait(500); |
| 594 | QCOMPARE(trackCount(subMenu), 1); |
| 595 | |
| 596 | mainMenu.removeAction(subMenu->menuAction()); |
| 597 | |
| 598 | mainMenu.addAction(subMenu->menuAction()); |
| 599 | |
| 600 | QTest::qWait(500); |
| 601 | QCOMPARE(trackCount(subMenu), 1); |
| 602 | } |
| 603 | |
| 604 | // If desktop does not want icon in menus, check we do not export them |
| 605 | void DBusMenuExporterTest::testHonorDontShowIconsInMenusAttribute() |
| 606 | { |
| 607 | QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, true); |
| 608 | QMenu inputMenu; |
| 609 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 610 | |
| 611 | QAction *action = new QAction("Undo", &inputMenu); |
| 612 | QIcon icon = QIcon::fromTheme("edit-undo"); |
| 613 | QVERIFY(!icon.isNull()); |
| 614 | action->setIcon(icon); |
| 615 | inputMenu.addAction(action); |
| 616 | |
| 617 | // Check out exporter is on DBus |
| 618 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 619 | QVERIFY2(iface.isValid(), qPrintable(iface.lastError().message())); |
| 620 | |
| 621 | // Get exported menu info |
| 622 | QStringList propertyNames = QStringList() << "icon-name"; |
| 623 | DBusMenuLayoutItemList list = getChildren(&iface, /*parentId=*/0, propertyNames); |
| 624 | DBusMenuLayoutItem item = list.first(); |
| 625 | QVERIFY(item.id != 0); |
| 626 | QVERIFY(!item.properties.contains("icon-name")); |
| 627 | } |
| 628 | |
| 629 | static bool hasInternalDBusMenuObject(QMenu* menu) |
| 630 | { |
| 631 | Q_FOREACH(QObject* obj, menu->children()) { |
| 632 | if (obj->inherits("DBusMenu")) { |
| 633 | return true; |
| 634 | } |
| 635 | } |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | // DBusMenuExporter adds an instance of an internal class named "DBusMenu" to |
| 640 | // any QMenu it tracks. Check they go away when the exporter is deleted. |
| 641 | void DBusMenuExporterTest::testDBusMenuObjectIsDeletedWhenExporterIsDeleted() |
| 642 | { |
| 643 | QMenu inputMenu; |
| 644 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 645 | DBusMenuExporter *exporter = new DBusMenuExporter(TEST_OBJECT_PATH, &inputMenu); |
| 646 | |
| 647 | QAction *a1 = inputMenu.addAction("a1"); |
| 648 | QVERIFY2(hasInternalDBusMenuObject(&inputMenu), "Test setup failed"); |
| 649 | delete exporter; |
| 650 | QVERIFY(!hasInternalDBusMenuObject(&inputMenu)); |
| 651 | } |
| 652 | |
| 653 | void DBusMenuExporterTest::testSeparatorCollapsing_data() |
| 654 | { |
| 655 | QTest::addColumn<QString>("input"); |
| 656 | QTest::addColumn<QString>("expected"); |
| 657 | |
| 658 | QTest::newRow("one-separator") << "a-b" << "a-b"; |
| 659 | QTest::newRow("two-separators") << "a-b-c" << "a-b-c"; |
| 660 | QTest::newRow("middle-separators") << "a--b" << "a-b"; |
| 661 | QTest::newRow("separators-at-begin") << "--a-b" << "a-b"; |
| 662 | QTest::newRow("separators-at-end") << "a-b--" << "a-b"; |
| 663 | QTest::newRow("separators-everywhere") << "--a---bc--d--" << "a-bc-d"; |
| 664 | QTest::newRow("empty-menu") << "" << ""; |
| 665 | QTest::newRow("separators-only") << "---" << ""; |
| 666 | } |
| 667 | |
| 668 | void DBusMenuExporterTest::testSeparatorCollapsing() |
| 669 | { |
| 670 | QFETCH(QString, input); |
| 671 | QFETCH(QString, expected); |
| 672 | |
| 673 | // Create menu from menu string |
| 674 | QMenu inputMenu; |
| 675 | |
| 676 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 677 | DBusMenuExporter *exporter = new DBusMenuExporter(TEST_OBJECT_PATH, &inputMenu); |
| 678 | |
| 679 | if (input.isEmpty()) { |
| 680 | // Pretend there was an action so that doEmitLayoutUpdated() is called |
| 681 | // even if the new menu is empty. If we don't do this we don't test |
| 682 | // DBusMenuExporterPrivate::collapseSeparators() for empty menus. |
| 683 | delete inputMenu.addAction("dummy"); |
| 684 | } |
| 685 | |
| 686 | Q_FOREACH(QChar ch, input) { |
| 687 | if (ch == '-') { |
| 688 | inputMenu.addSeparator(); |
| 689 | } else { |
| 690 | inputMenu.addAction(ch); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | QTest::qWait(500); |
| 695 | |
| 696 | // Check out exporter is on DBus |
| 697 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 698 | QVERIFY2(iface.isValid(), qPrintable(iface.lastError().message())); |
| 699 | |
| 700 | // Get exported menu info |
| 701 | QStringList propertyNames = QStringList(); |
| 702 | DBusMenuLayoutItemList list = getChildren(&iface, /*parentId=*/0, propertyNames); |
| 703 | |
| 704 | // Recreate a menu string from the item list |
| 705 | QString output; |
| 706 | Q_FOREACH(const DBusMenuLayoutItem& item, list) { |
| 707 | QVariantMap properties = item.properties; |
| 708 | if (properties.contains("visible") && !properties.value("visible").toBool()) { |
| 709 | continue; |
| 710 | } |
| 711 | QString type = properties.value("type").toString(); |
| 712 | if (type == "separator") { |
| 713 | output += '-'; |
| 714 | } else { |
| 715 | output += properties.value("label").toString(); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | // Check it matches |
| 720 | QCOMPARE(output, expected); |
| 721 | } |
| 722 | |
| 723 | static void checkPropertiesChangedArgs(const QVariantList& args, const QString& name, const QVariant& value) |
| 724 | { |
| 725 | QCOMPARE(args[0].toString(), QString("com.canonical.dbusmenu")); |
| 726 | QVariantMap map; |
| 727 | map.insert(name, value); |
| 728 | QCOMPARE(args[1].toMap(), map); |
| 729 | QCOMPARE(args[2].toStringList(), QStringList()); |
| 730 | } |
| 731 | |
| 732 | void DBusMenuExporterTest::testSetStatus() |
| 733 | { |
| 734 | QMenu inputMenu; |
| 735 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 736 | DBusMenuExporter *exporter = new DBusMenuExporter(TEST_OBJECT_PATH, &inputMenu); |
| 737 | ManualSignalSpy spy; |
| 738 | QDBusConnection::sessionBus().connect(TEST_SERVICE, TEST_OBJECT_PATH, "org.freedesktop.DBus.Properties", "PropertiesChanged", "sa{sv}as", &spy, SLOT(receiveCall(QString, QVariantMap, QStringList))); |
| 739 | |
| 740 | QTest::qWait(500); |
| 741 | |
| 742 | // Check our exporter is on DBus |
| 743 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 744 | QVERIFY2(iface.isValid(), qPrintable(iface.lastError().message())); |
| 745 | |
| 746 | QCOMPARE(exporter->status(), QString("normal")); |
| 747 | |
| 748 | // Change status, a DBus signal should be emitted |
| 749 | exporter->setStatus("notice"); |
| 750 | QCOMPARE(exporter->status(), QString("notice")); |
| 751 | QTest::qWait(500); |
| 752 | QCOMPARE(spy.count(), 1); |
| 753 | checkPropertiesChangedArgs(spy.takeFirst(), "Status", "notice"); |
| 754 | |
| 755 | // Same status => no signal |
| 756 | exporter->setStatus("notice"); |
| 757 | QTest::qWait(500); |
| 758 | QCOMPARE(spy.count(), 0); |
| 759 | |
| 760 | // Change status, a DBus signal should be emitted |
| 761 | exporter->setStatus("normal"); |
| 762 | QTest::qWait(500); |
| 763 | QCOMPARE(spy.count(), 1); |
| 764 | checkPropertiesChangedArgs(spy.takeFirst(), "Status", "normal"); |
| 765 | } |
| 766 | |
| 767 | void DBusMenuExporterTest::testGetIconDataProperty() |
| 768 | { |
| 769 | // Create an icon |
| 770 | QImage img(16, 16, QImage::Format_ARGB32); |
| 771 | { |
| 772 | QPainter painter(&img); |
| 773 | painter.setCompositionMode(QPainter::CompositionMode_Source); |
| 774 | QRect rect = img.rect(); |
| 775 | painter.fillRect(rect, Qt::transparent); |
| 776 | rect.adjust(2, 2, -2, -2); |
| 777 | painter.fillRect(rect, Qt::red); |
| 778 | rect.adjust(2, 2, -2, -2); |
| 779 | painter.fillRect(rect, Qt::green); |
| 780 | } |
| 781 | |
| 782 | QIcon icon(QPixmap::fromImage(img)); |
| 783 | |
| 784 | // Create a menu with the icon and export it |
| 785 | QMenu inputMenu; |
| 786 | QAction* a1 = inputMenu.addAction("a1"); |
| 787 | a1->setIcon(icon); |
| 788 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 789 | |
| 790 | // Get properties |
| 791 | QDBusInterface iface(TEST_SERVICE, TEST_OBJECT_PATH); |
| 792 | DBusMenuLayoutItemList layoutItemlist = getChildren(&iface, 0, QStringList()); |
| 793 | QCOMPARE(layoutItemlist.count(), 1); |
| 794 | |
| 795 | QList<int> ids = QList<int>() << layoutItemlist[0].id; |
| 796 | |
| 797 | QDBusReply<DBusMenuItemList> reply = iface.call("GetGroupProperties", QVariant::fromValue(ids), QStringList()); |
| 798 | |
| 799 | DBusMenuItemList itemlist = reply.value(); |
| 800 | QCOMPARE(itemlist.count(), 1); |
| 801 | |
| 802 | // Check we have the right property |
| 803 | DBusMenuItem item = itemlist.takeFirst(); |
| 804 | QVERIFY(!item.properties.contains("icon-name")); |
| 805 | QVERIFY(item.properties.contains("icon-data")); |
| 806 | |
| 807 | // Check saved image is the same |
| 808 | QByteArray data = item.properties.value("icon-data").toByteArray(); |
| 809 | QVERIFY(!data.isEmpty()); |
| 810 | QImage result; |
| 811 | QVERIFY(result.loadFromData(data, "PNG")); |
| 812 | QCOMPARE(result, img); |
| 813 | } |
| 814 | |
| 815 | #include "dbusmenuexportertest.moc" |