From 2e98952068850d9b71385fb64180a836829cdb05 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 21 Aug 2012 13:57:27 +0200 Subject: [PATCH] ProjectExplorer: Properly test device support. Use Creator's test infrastructure instead of dumping the code into tests/ where it will be forgotten. Change-Id: I0db767ff68754e04d727671f4ecdd61fa9f75c1b Reviewed-by: Tobias Hunger --- .../devicesupport/devicemanager.cpp | 103 ++++++++++++- .../devicesupport/devicemanager.h | 2 +- src/plugins/projectexplorer/projectexplorer.h | 2 + .../devices/devicemanager/devicemanager.pro | 9 -- .../devicemanager/devicemanagertest.cpp | 142 ------------------ .../devices/devicemanager/devicemanagertest.h | 64 -------- tests/manual/devices/devicemanager/main.cpp | 41 ----- tests/manual/devices/devices.pri | 21 --- tests/manual/devices/devices.pro | 2 - 9 files changed, 104 insertions(+), 282 deletions(-) delete mode 100644 tests/manual/devices/devicemanager/devicemanager.pro delete mode 100644 tests/manual/devices/devicemanager/devicemanagertest.cpp delete mode 100644 tests/manual/devices/devicemanager/devicemanagertest.h delete mode 100644 tests/manual/devices/devicemanager/main.cpp delete mode 100644 tests/manual/devices/devices.pri delete mode 100644 tests/manual/devices/devices.pro diff --git a/src/plugins/projectexplorer/devicesupport/devicemanager.cpp b/src/plugins/projectexplorer/devicesupport/devicemanager.cpp index 0dfa96e1867..79082e121fd 100644 --- a/src/plugins/projectexplorer/devicesupport/devicemanager.cpp +++ b/src/plugins/projectexplorer/devicesupport/devicemanager.cpp @@ -88,9 +88,9 @@ DeviceManager *DeviceManagerPrivate::clonedInstance = 0; using namespace Internal; -DeviceManager *DeviceManager::instance(const QString &magicTestToken) +DeviceManager *DeviceManager::instance() { - static DeviceManager deviceManager(magicTestToken != QLatin1String("magicTestToken")); + static DeviceManager deviceManager(true); return &deviceManager; } @@ -416,5 +416,104 @@ IDevice::ConstPtr DeviceManager::fromRawPointer(const IDevice *device) const return fromRawPointer(const_cast(device)); } +} // namespace ProjectExplorer + + +#ifdef WITH_TESTS +#include "projectexplorer.h" +#include +#include +#include + +namespace ProjectExplorer { + +class TestDevice : public IDevice +{ +public: + TestDevice() + : IDevice(testTypeId(), AutoDetected, Hardware, Core::Id(QUuid::createUuid().toString())) {} + + static Core::Id testTypeId() { return Core::Id("TestType"); } +private: + TestDevice(const TestDevice &other) : IDevice(other) {} + QString displayType() const { return QLatin1String("blubb"); } + IDeviceWidget *createWidget() { return 0; } + QList actionIds() const { return QList(); } + QString displayNameForActionId(Core::Id) const { return QString(); } + void executeAction(Core::Id, QWidget *) const { } + Ptr clone() const { return Ptr(new TestDevice(*this)); } +}; + +void ProjectExplorerPlugin::testDeviceManager() +{ + TestDevice::Ptr dev = IDevice::Ptr(new TestDevice); + dev->setDisplayName(QLatin1String("blubbdiblubbfurz!")); + QVERIFY(dev->isAutoDetected()); + QCOMPARE(dev->type(), TestDevice::testTypeId()); + + TestDevice::Ptr dev2 = dev->clone(); + QCOMPARE(dev->id(), dev2->id()); + + DeviceManager * const mgr = DeviceManager::instance(); + QVERIFY(!mgr->find(dev->id())); + const int oldDeviceCount = mgr->deviceCount(); + + QSignalSpy deviceAddedSpy(mgr, SIGNAL(deviceAdded(Core::Id))); + QSignalSpy deviceRemovedSpy(mgr, SIGNAL(deviceRemoved(Core::Id))); + QSignalSpy deviceUpdatedSpy(mgr, SIGNAL(deviceUpdated(Core::Id))); + QSignalSpy deviceListChangedSpy(mgr, SIGNAL(deviceListChanged())); + QSignalSpy updatedSpy(mgr, SIGNAL(updated())); + + mgr->addDevice(dev); + QCOMPARE(mgr->deviceCount(), oldDeviceCount + 1); + QVERIFY(mgr->find(dev->id())); + QVERIFY(mgr->hasDevice(dev->displayName())); + QCOMPARE(deviceAddedSpy.count(), 1); + QCOMPARE(deviceRemovedSpy.count(), 0); + QCOMPARE(deviceUpdatedSpy.count(), 0); + QCOMPARE(deviceListChangedSpy.count(), 0); + QCOMPARE(updatedSpy.count(), 1); + deviceAddedSpy.clear(); + updatedSpy.clear(); + + mgr->addDevice(dev2); + QCOMPARE(mgr->deviceCount(), oldDeviceCount + 1); + QVERIFY(mgr->find(dev->id())); + QCOMPARE(deviceAddedSpy.count(), 0); + QCOMPARE(deviceRemovedSpy.count(), 0); + QCOMPARE(deviceUpdatedSpy.count(), 1); + QCOMPARE(deviceListChangedSpy.count(), 0); + QCOMPARE(updatedSpy.count(), 1); + deviceUpdatedSpy.clear(); + updatedSpy.clear(); + + TestDevice::Ptr dev3 = IDevice::Ptr(new TestDevice); + QVERIFY(dev->id() != dev3->id()); + + dev3->setDisplayName(dev->displayName()); + mgr->addDevice(dev3); + QCOMPARE(mgr->deviceAt(mgr->deviceCount() - 1)->displayName(), + QString(dev3->displayName() + QLatin1String(" (2)"))); + QCOMPARE(deviceAddedSpy.count(), 1); + QCOMPARE(deviceRemovedSpy.count(), 0); + QCOMPARE(deviceUpdatedSpy.count(), 0); + QCOMPARE(deviceListChangedSpy.count(), 0); + QCOMPARE(updatedSpy.count(), 1); + deviceAddedSpy.clear(); + updatedSpy.clear(); + + mgr->removeDevice(dev->id()); + mgr->removeDevice(dev3->id()); + QCOMPARE(mgr->deviceCount(), oldDeviceCount); + QVERIFY(!mgr->find(dev->id())); + QVERIFY(!mgr->find(dev3->id())); + QCOMPARE(deviceAddedSpy.count(), 0); + QCOMPARE(deviceRemovedSpy.count(), 2); +// QCOMPARE(deviceUpdatedSpy.count(), 0); Uncomment once the "default" stuff is gone. + QCOMPARE(deviceListChangedSpy.count(), 0); + QCOMPARE(updatedSpy.count(), 2); +} } // namespace ProjectExplorer + +#endif // WITH_TESTS diff --git a/src/plugins/projectexplorer/devicesupport/devicemanager.h b/src/plugins/projectexplorer/devicesupport/devicemanager.h index 852b0932529..ae8cf3fd2d8 100644 --- a/src/plugins/projectexplorer/devicesupport/devicemanager.h +++ b/src/plugins/projectexplorer/devicesupport/devicemanager.h @@ -54,7 +54,7 @@ class PROJECTEXPLORER_EXPORT DeviceManager : public QObject public: ~DeviceManager(); - static DeviceManager *instance(const QString &magicTestToken = QString()); + static DeviceManager *instance(); int deviceCount() const; IDevice::ConstPtr deviceAt(int index) const; diff --git a/src/plugins/projectexplorer/projectexplorer.h b/src/plugins/projectexplorer/projectexplorer.h index 70fb4eb8694..1ec882c1dab 100644 --- a/src/plugins/projectexplorer/projectexplorer.h +++ b/src/plugins/projectexplorer/projectexplorer.h @@ -260,6 +260,8 @@ private slots: void testAbiOfBinary_data(); void testAbiOfBinary(); void testFlavorForOs(); + + void testDeviceManager(); #endif private: diff --git a/tests/manual/devices/devicemanager/devicemanager.pro b/tests/manual/devices/devicemanager/devicemanager.pro deleted file mode 100644 index 71c9fd94c31..00000000000 --- a/tests/manual/devices/devicemanager/devicemanager.pro +++ /dev/null @@ -1,9 +0,0 @@ -include(../devices.pri) - -TARGET=devicemanagertest -SOURCES= \ - main.cpp \ - devicemanagertest.cpp - -HEADERS += \ - devicemanagertest.h diff --git a/tests/manual/devices/devicemanager/devicemanagertest.cpp b/tests/manual/devices/devicemanager/devicemanagertest.cpp deleted file mode 100644 index 9ea35e56391..00000000000 --- a/tests/manual/devices/devicemanager/devicemanagertest.cpp +++ /dev/null @@ -1,142 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (info@qt.nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** -**************************************************************************/ - -#include "devicemanagertest.h" - -#include -#include -#include - -#include - -#include -#include - -using namespace ProjectExplorer; -using namespace RemoteLinux; - -#define DEV_MGR_CHECK(cond) QTC_ASSERT(cond, exit(1)) - -DeviceManagerTest::DeviceManagerTest(QObject *parent) : QObject(parent) -{ -} - -void DeviceManagerTest::run() -{ - DeviceManager * const devMgr = DeviceManager::instance("magicTestToken"); - DEV_MGR_CHECK(m_slotsCalled.isEmpty()); - connect(devMgr, SIGNAL(deviceAdded(Core::Id)), SLOT(handleDeviceAdded(Core::Id))); - connect(devMgr, SIGNAL(deviceRemoved(Core::Id)), SLOT(handleDeviceRemoved(Core::Id))); - connect(devMgr, SIGNAL(deviceUpdated(Core::Id)), SLOT(handleDeviceUpdated(Core::Id))); - connect(devMgr, SIGNAL(updated()), SLOT(handleUpdated())); - connect(devMgr, SIGNAL(deviceListChanged()), SLOT(handleDeviceListChanged())); - - std::cout << "Initial add." << std::endl; - m_currentId = m_currentUpdateId = Core::Id("id1"); - LinuxDevice::Ptr device = LinuxDevice::create("blubb", "mytype", - LinuxDevice::Hardware, IDevice::AutoDetected, m_currentId); - devMgr->addDevice(device); - DEV_MGR_CHECK(devMgr->deviceCount() == 1); - DEV_MGR_CHECK(devMgr->defaultDevice("mytype") == device); - DEV_MGR_CHECK(m_slotsCalled.count() == 2); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotDeviceAdded)); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotUpdated)); - - std::cout << "Add with different id, but same name." << std::endl; - m_slotsCalled.clear(); - m_currentId = Core::Id("id2"); - m_currentUpdateId = Core::Id("doesnotexist"); - device = LinuxDevice::create("blubb", "mytype", - LinuxDevice::Hardware, IDevice::AutoDetected, m_currentId); - devMgr->addDevice(device); - DEV_MGR_CHECK(devMgr->deviceCount() == 2); - DEV_MGR_CHECK(devMgr->defaultDevice("mytype")->id() == Core::Id("id1")); - DEV_MGR_CHECK(m_slotsCalled.count() == 2); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotDeviceAdded)); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotUpdated)); - DEV_MGR_CHECK(devMgr->deviceAt(1)->displayName() == "blubb (2)"); - - std::cout << "Add with same id." << std::endl; - m_slotsCalled.clear(); - m_currentId = m_currentUpdateId = Core::Id("id1"); - device = LinuxDevice::create("blubbblubb", "mytype", - LinuxDevice::Hardware, IDevice::AutoDetected, m_currentId); - devMgr->addDevice(device); - DEV_MGR_CHECK(devMgr->deviceCount() == 2); - DEV_MGR_CHECK(devMgr->defaultDevice("mytype")->id() == Core::Id("id1")); - DEV_MGR_CHECK(m_slotsCalled.count() == 2); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotDeviceUpdated)); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotUpdated)); - DEV_MGR_CHECK(devMgr->deviceAt(0)->displayName() == "blubbblubb"); - - std::cout << "Remove." << std::endl; - m_slotsCalled.clear(); - m_currentId = Core::Id("id1"); - m_currentUpdateId = Core::Id("id2"); - devMgr->removeDevice(m_currentId); - DEV_MGR_CHECK(devMgr->deviceCount() == 1); - DEV_MGR_CHECK(devMgr->defaultDevice("mytype")->id() == Core::Id("id2")); - DEV_MGR_CHECK(m_slotsCalled.count() == 3); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotDeviceRemoved)); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotDeviceUpdated)); - DEV_MGR_CHECK(m_slotsCalled.contains(SlotUpdated)); - - std::cout << "All tests finished successfully." << std::endl; - qApp->quit(); -} - -void DeviceManagerTest::handleDeviceAdded(Core::Id id) -{ - DEV_MGR_CHECK(id == m_currentId); - m_slotsCalled << SlotDeviceAdded; -} - -void DeviceManagerTest::handleDeviceRemoved(Core::Id id) -{ - DEV_MGR_CHECK(id == m_currentId); - m_slotsCalled << SlotDeviceRemoved; -} - -void DeviceManagerTest::handleDeviceUpdated(Core::Id id) -{ - DEV_MGR_CHECK(id == m_currentUpdateId); - m_slotsCalled << SlotDeviceUpdated; -} - -void DeviceManagerTest::handleDeviceListChanged() -{ - qWarning("deviceListChanged() unexpectedly called."); - qApp->exit(1); -} - -void DeviceManagerTest::handleUpdated() -{ - m_slotsCalled << SlotUpdated; -} diff --git a/tests/manual/devices/devicemanager/devicemanagertest.h b/tests/manual/devices/devicemanager/devicemanagertest.h deleted file mode 100644 index 62ac2f0a544..00000000000 --- a/tests/manual/devices/devicemanager/devicemanagertest.h +++ /dev/null @@ -1,64 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (info@qt.nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** -**************************************************************************/ -#ifndef DEVICEMANAGERTEST_H -#define DEVICEMANAGERTEST_H - -#include - -#include -#include - -class DeviceManagerTest : public QObject -{ - Q_OBJECT -public: - explicit DeviceManagerTest(QObject *parent = 0); - -public slots: - void run(); - -private slots: - void handleDeviceAdded(Core::Id id); - void handleDeviceRemoved(Core::Id id); - void handleDeviceUpdated(Core::Id id); - void handleDeviceListChanged(); - void handleUpdated(); - -private: - enum Slot { - SlotDeviceAdded, SlotDeviceRemoved, SlotDeviceUpdated, SlotDeviceListChanged, - SlotUpdated - }; - QList m_slotsCalled; - Core::Id m_currentId; - Core::Id m_currentUpdateId; -}; - -#endif // DEVICEMANAGERTEST_H diff --git a/tests/manual/devices/devicemanager/main.cpp b/tests/manual/devices/devicemanager/main.cpp deleted file mode 100644 index b2254b13b46..00000000000 --- a/tests/manual/devices/devicemanager/main.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (info@qt.nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** -**************************************************************************/ -#include "devicemanagertest.h" - -#include -#include - -int main(int argc, char *argv[]) -{ - QCoreApplication app(argc, argv); - DeviceManagerTest devMgrTest; - QTimer::singleShot(0, &devMgrTest, SLOT(run())); - return app.exec(); -} diff --git a/tests/manual/devices/devices.pri b/tests/manual/devices/devices.pri deleted file mode 100644 index 8720f213a27..00000000000 --- a/tests/manual/devices/devices.pri +++ /dev/null @@ -1,21 +0,0 @@ -QT = core - -include (../../../qtcreator.pri) -include (../../../src/plugins/remotelinux/remotelinux.pri) - -LIBS += -L$$IDE_PLUGIN_PATH/Nokia - -macx:QMAKE_LFLAGS += -Wl,-rpath,\"$$IDE_BIN_PATH/..\" -INCLUDEPATH *= $$IDE_SOURCE_TREE/src/plugins -LIBS *= -L$$IDE_LIBRARY_PATH/Nokia -unix { - QMAKE_LFLAGS += -Wl,-rpath,\"$$IDE_PLUGIN_PATH/..\" - QMAKE_LFLAGS += -Wl,-rpath,\"$$IDE_PLUGIN_PATH/Nokia\" -} - -CONFIG += console -CONFIG -= app_bundle -TEMPLATE = app - -DEPENDPATH+=. -INCLUDEPATH+=. diff --git a/tests/manual/devices/devices.pro b/tests/manual/devices/devices.pro deleted file mode 100644 index d7eb59a7ac2..00000000000 --- a/tests/manual/devices/devices.pro +++ /dev/null @@ -1,2 +0,0 @@ -TEMPLATE = subdirs -SUBDIRS = devicemanager