| 1 | /* This file is part of the dbusmenu-qt library |
| 2 | Copyright 2010 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 "dbusmenuimportertest.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 <dbusmenuimporter.h> |
| 35 | #include <debug_p.h> |
| 36 | |
| 37 | // Local |
| 38 | #include "testutils.h" |
| 39 | |
| 40 | QTEST_MAIN(DBusMenuImporterTest) |
| 41 | |
| 42 | static const char *TEST_SERVICE = "com.canonical.dbusmenu-qt-test"; |
| 43 | static const char *TEST_OBJECT_PATH = "/TestMenuBar"; |
| 44 | |
| 45 | Q_DECLARE_METATYPE(QAction*) |
| 46 | |
| 47 | void DBusMenuImporterTest::initTestCase() |
| 48 | { |
| 49 | qRegisterMetaType<QAction*>("QAction*"); |
| 50 | QVERIFY(QDBusConnection::sessionBus().registerService(TEST_SERVICE)); |
| 51 | } |
| 52 | |
| 53 | void DBusMenuImporterTest::cleanup() |
| 54 | { |
| 55 | waitForDeferredDeletes(); |
| 56 | } |
| 57 | |
| 58 | void DBusMenuImporterTest::testStandardItem() |
| 59 | { |
| 60 | QMenu inputMenu; |
| 61 | QAction *action = inputMenu.addAction("Test"); |
| 62 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 63 | |
| 64 | DBusMenuImporter importer(TEST_SERVICE, TEST_OBJECT_PATH); |
| 65 | QTest::qWait(500); |
| 66 | |
| 67 | QMenu *outputMenu = importer.menu(); |
| 68 | QCOMPARE(outputMenu->actions().count(), 1); |
| 69 | QAction *outputAction = outputMenu->actions().first(); |
| 70 | QCOMPARE(outputAction->text(), QString("Test")); |
| 71 | } |
| 72 | |
| 73 | void DBusMenuImporterTest::testAddingNewItem() |
| 74 | { |
| 75 | QMenu inputMenu; |
| 76 | QAction *action = inputMenu.addAction("Test"); |
| 77 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 78 | |
| 79 | DBusMenuImporter importer(TEST_SERVICE, TEST_OBJECT_PATH); |
| 80 | QTest::qWait(500); |
| 81 | QMenu *outputMenu = importer.menu(); |
| 82 | QCOMPARE(outputMenu->actions().count(), inputMenu.actions().count()); |
| 83 | |
| 84 | inputMenu.addAction("Test2"); |
| 85 | QTest::qWait(500); |
| 86 | QCOMPARE(outputMenu->actions().count(), inputMenu.actions().count()); |
| 87 | } |
| 88 | |
| 89 | void DBusMenuImporterTest::testShortcut() |
| 90 | { |
| 91 | QMenu inputMenu; |
| 92 | QAction *action = inputMenu.addAction("Test"); |
| 93 | action->setShortcut(Qt::CTRL | Qt::Key_S); |
| 94 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 95 | |
| 96 | DBusMenuImporter importer(TEST_SERVICE, TEST_OBJECT_PATH); |
| 97 | QTest::qWait(500); |
| 98 | QMenu *outputMenu = importer.menu(); |
| 99 | |
| 100 | QAction *outputAction = outputMenu->actions().at(0); |
| 101 | QCOMPARE(outputAction->shortcut(), action->shortcut()); |
| 102 | } |
| 103 | |
| 104 | void DBusMenuImporterTest::testDeletingImporterWhileWaitingForAboutToShow() |
| 105 | { |
| 106 | // Start test program and wait for it to be ready |
| 107 | QProcess slowMenuProcess; |
| 108 | slowMenuProcess.start("./slowmenu"); |
| 109 | QTest::qWait(500); |
| 110 | |
| 111 | // Create importer and wait for the menu |
| 112 | DBusMenuImporter *importer = new DBusMenuImporter(TEST_SERVICE, TEST_OBJECT_PATH); |
| 113 | QTest::qWait(500); |
| 114 | |
| 115 | QMenu *outputMenu = importer->menu(); |
| 116 | QTimer::singleShot(100, importer, SLOT(deleteLater())); |
| 117 | outputMenu->popup(QPoint(0, 0)); |
| 118 | |
| 119 | // If it crashes, it will crash while waiting there |
| 120 | QTest::qWait(500); |
| 121 | |
| 122 | // Done, stop our test program |
| 123 | slowMenuProcess.close(); |
| 124 | slowMenuProcess.waitForFinished(); |
| 125 | } |
| 126 | |
| 127 | void DBusMenuImporterTest::testDynamicMenu() |
| 128 | { |
| 129 | QMenu rootMenu; |
| 130 | QAction* a1 = new QAction("a1", &rootMenu); |
| 131 | QAction* a2 = new QAction("a2", &rootMenu); |
| 132 | MenuFiller rootMenuFiller(&rootMenu); |
| 133 | rootMenuFiller.addAction(a1); |
| 134 | rootMenuFiller.addAction(a2); |
| 135 | |
| 136 | QMenu subMenu; |
| 137 | MenuFiller subMenuFiller(&subMenu); |
| 138 | subMenuFiller.addAction(new QAction("a3", &subMenu)); |
| 139 | |
| 140 | a1->setMenu(&subMenu); |
| 141 | |
| 142 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &rootMenu); |
| 143 | |
| 144 | // Import this menu |
| 145 | DBusMenuImporter importer(TEST_SERVICE, TEST_OBJECT_PATH); |
| 146 | QTest::qWait(500); |
| 147 | QMenu *outputMenu = importer.menu(); |
| 148 | |
| 149 | // There should be no children for now |
| 150 | QCOMPARE(outputMenu->actions().count(), 0); |
| 151 | |
| 152 | // Update menu, a1 and a2 should get added |
| 153 | QSignalSpy spy(&importer, SIGNAL(menuUpdated())); |
| 154 | QSignalSpy spyOld(&importer, SIGNAL(menuReadyToBeShown())); |
| 155 | importer.updateMenu(); |
| 156 | while (spy.isEmpty()) { |
| 157 | QTest::qWait(500); |
| 158 | } |
| 159 | |
| 160 | QCOMPARE(outputMenu->actions().count(), 2); |
| 161 | QTest::qWait(500); |
| 162 | QAction* a1Output = outputMenu->actions().first(); |
| 163 | |
| 164 | // a1Output should have an empty menu |
| 165 | QMenu* a1OutputMenu = a1Output->menu(); |
| 166 | QVERIFY(a1OutputMenu); |
| 167 | QCOMPARE(a1OutputMenu->actions().count(), 0); |
| 168 | |
| 169 | // Show a1OutputMenu, a3 should get added |
| 170 | QMetaObject::invokeMethod(a1OutputMenu, "aboutToShow"); |
| 171 | QTest::qWait(500); |
| 172 | |
| 173 | QCOMPARE(a1OutputMenu->actions().count(), 1); |
| 174 | |
| 175 | // menuUpdated() and menuReadyToBeShown() should only have been emitted |
| 176 | // once |
| 177 | QCOMPARE(spy.count(), 1); |
| 178 | QCOMPARE(spyOld.count(), 1); |
| 179 | } |
| 180 | |
| 181 | void DBusMenuImporterTest::testActionActivationRequested() |
| 182 | { |
| 183 | // Export a menu |
| 184 | QMenu inputMenu; |
| 185 | QAction *inputA1 = inputMenu.addAction("a1"); |
| 186 | QAction *inputA2 = inputMenu.addAction("a2"); |
| 187 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 188 | |
| 189 | // Import the menu |
| 190 | DBusMenuImporter importer(TEST_SERVICE, TEST_OBJECT_PATH); |
| 191 | QSignalSpy spy(&importer, SIGNAL(actionActivationRequested(QAction*))); |
| 192 | |
| 193 | QTest::qWait(500); |
| 194 | QMenu *outputMenu = importer.menu(); |
| 195 | |
| 196 | // Get matching output actions |
| 197 | QCOMPARE(outputMenu->actions().count(), 2); |
| 198 | QAction *outputA1 = outputMenu->actions().at(0); |
| 199 | QAction *outputA2 = outputMenu->actions().at(1); |
| 200 | |
| 201 | // Request activation |
| 202 | exporter.activateAction(inputA1); |
| 203 | exporter.activateAction(inputA2); |
| 204 | |
| 205 | // Check we received the signal in the right order |
| 206 | QTest::qWait(500); |
| 207 | QCOMPARE(spy.count(), 2); |
| 208 | QCOMPARE(spy.takeFirst().at(0).value<QAction*>(), outputA1); |
| 209 | QCOMPARE(spy.takeFirst().at(0).value<QAction*>(), outputA2); |
| 210 | } |
| 211 | |
| 212 | void DBusMenuImporterTest::testActionsAreDeletedWhenImporterIs() |
| 213 | { |
| 214 | // Export a menu |
| 215 | QMenu inputMenu; |
| 216 | inputMenu.addAction("a1"); |
| 217 | QMenu *inputSubMenu = inputMenu.addMenu("subMenu"); |
| 218 | inputSubMenu->addAction("a2"); |
| 219 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 220 | |
| 221 | // Import the menu |
| 222 | DBusMenuImporter *importer = new DBusMenuImporter(TEST_SERVICE, TEST_OBJECT_PATH); |
| 223 | QTest::qWait(500); |
| 224 | |
| 225 | // Put all items of the menu in a list of QPointers |
| 226 | QList< QPointer<QObject> > children; |
| 227 | |
| 228 | QMenu *outputMenu = importer->menu(); |
| 229 | QCOMPARE(outputMenu->actions().count(), 2); |
| 230 | QMenu *outputSubMenu = outputMenu->actions().at(1)->menu(); |
| 231 | QVERIFY(outputSubMenu); |
| 232 | // Fake aboutToShow so that outputSubMenu is populated |
| 233 | QMetaObject::invokeMethod(outputSubMenu, "aboutToShow"); |
| 234 | QCOMPARE(outputSubMenu->actions().count(), 1); |
| 235 | |
| 236 | children << outputMenu->actions().at(0); |
| 237 | children << outputMenu->actions().at(1); |
| 238 | children << outputSubMenu; |
| 239 | children << outputSubMenu->actions().at(0); |
| 240 | |
| 241 | delete importer; |
| 242 | waitForDeferredDeletes(); |
| 243 | |
| 244 | // There should be only invalid pointers in children |
| 245 | Q_FOREACH(QPointer<QObject> child, children) { |
| 246 | //qDebug() << child; |
| 247 | QVERIFY(child.isNull()); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void DBusMenuImporterTest::testIconData() |
| 252 | { |
| 253 | // Create an icon |
| 254 | QImage img(16, 16, QImage::Format_ARGB32); |
| 255 | { |
| 256 | QPainter painter(&img); |
| 257 | painter.setCompositionMode(QPainter::CompositionMode_Source); |
| 258 | QRect rect = img.rect(); |
| 259 | painter.fillRect(rect, Qt::transparent); |
| 260 | rect.adjust(2, 2, -2, -2); |
| 261 | painter.fillRect(rect, Qt::red); |
| 262 | rect.adjust(2, 2, -2, -2); |
| 263 | painter.fillRect(rect, Qt::green); |
| 264 | } |
| 265 | QIcon inputIcon(QPixmap::fromImage(img)); |
| 266 | |
| 267 | // Export a menu |
| 268 | QMenu inputMenu; |
| 269 | QAction *a1 = inputMenu.addAction("a1"); |
| 270 | a1->setIcon(inputIcon); |
| 271 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 272 | |
| 273 | // Import the menu |
| 274 | DBusMenuImporter *importer = new DBusMenuImporter(TEST_SERVICE, TEST_OBJECT_PATH); |
| 275 | QTest::qWait(500); |
| 276 | |
| 277 | // Check icon of action |
| 278 | QMenu *outputMenu = importer->menu(); |
| 279 | QCOMPARE(outputMenu->actions().count(), 1); |
| 280 | |
| 281 | QIcon outputIcon = outputMenu->actions().first()->icon(); |
| 282 | QVERIFY(!outputIcon.isNull()); |
| 283 | |
| 284 | QImage result = outputIcon.pixmap(16).toImage(); |
| 285 | QByteArray origBytes, resultBytes; |
| 286 | img.save(origBytes); |
| 287 | result.save(resultBytes); |
| 288 | QCOMPARE(origBytes,resultBytes); |
| 289 | } |
| 290 | |
| 291 | void DBusMenuImporterTest::testInvisibleItem() |
| 292 | { |
| 293 | QMenu inputMenu; |
| 294 | QAction *action = inputMenu.addAction("Test"); |
| 295 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 296 | |
| 297 | DBusMenuImporter importer(TEST_SERVICE, TEST_OBJECT_PATH); |
| 298 | QTest::qWait(500); |
| 299 | |
| 300 | QMenu *outputMenu = importer.menu(); |
| 301 | QCOMPARE(outputMenu->actions().count(), 1); |
| 302 | QAction *outputAction = outputMenu->actions().first(); |
| 303 | |
| 304 | QVERIFY(outputAction->isVisible()); |
| 305 | |
| 306 | // Hide the action |
| 307 | action->setVisible(false); |
| 308 | QTest::qWait(500); |
| 309 | QVERIFY(!outputAction->isVisible()); |
| 310 | |
| 311 | // Show the action |
| 312 | action->setVisible(true); |
| 313 | QTest::qWait(500); |
| 314 | QVERIFY(outputAction->isVisible()); |
| 315 | } |
| 316 | |
| 317 | void DBusMenuImporterTest::testDisabledItem() |
| 318 | { |
| 319 | QMenu inputMenu; |
| 320 | QAction *action = inputMenu.addAction("Test"); |
| 321 | DBusMenuExporter exporter(TEST_OBJECT_PATH, &inputMenu); |
| 322 | |
| 323 | DBusMenuImporter importer(TEST_SERVICE, TEST_OBJECT_PATH); |
| 324 | QTest::qWait(500); |
| 325 | |
| 326 | QMenu *outputMenu = importer.menu(); |
| 327 | QCOMPARE(outputMenu->actions().count(), 1); |
| 328 | QAction *outputAction = outputMenu->actions().first(); |
| 329 | QVERIFY(outputAction->isEnabled()); |
| 330 | |
| 331 | // Disable the action |
| 332 | DMDEBUG << "Disabling"; |
| 333 | action->setEnabled(false); |
| 334 | QTest::qWait(500); |
| 335 | QVERIFY(!outputAction->isEnabled()); |
| 336 | |
| 337 | // Enable the action |
| 338 | action->setEnabled(true); |
| 339 | QTest::qWait(500); |
| 340 | QVERIFY(outputAction->isEnabled()); |
| 341 | } |
| 342 | |
| 343 | #include "dbusmenuimportertest.moc" |