Commit 2e989520 authored by Christian Kandeler's avatar Christian Kandeler

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: default avatarTobias Hunger <tobias.hunger@nokia.com>
parent a9735113
......@@ -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<IDevice *>(device));
}
} // namespace ProjectExplorer
#ifdef WITH_TESTS
#include "projectexplorer.h"
#include <QSignalSpy>
#include <QTest>
#include <QUuid>
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<Core::Id> actionIds() const { return QList<Core::Id>(); }
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
......@@ -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;
......
......@@ -260,6 +260,8 @@ private slots:
void testAbiOfBinary_data();
void testAbiOfBinary();
void testFlavorForOs();
void testDeviceManager();
#endif
private:
......
include(../devices.pri)
TARGET=devicemanagertest
SOURCES= \
main.cpp \
devicemanagertest.cpp
HEADERS += \
devicemanagertest.h
/**************************************************************************
**
** 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 <projectexplorer/devicesupport/devicemanager.h>
#include <remotelinux/linuxdevice.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
#include <cstdlib>
#include <iostream>
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;
}
/**************************************************************************
**
** 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 <coreplugin/id.h>
#include <QList>
#include <QObject>
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<Slot> m_slotsCalled;
Core::Id m_currentId;
Core::Id m_currentUpdateId;
};
#endif // DEVICEMANAGERTEST_H
/**************************************************************************
**
** 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 <QCoreApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
DeviceManagerTest devMgrTest;
QTimer::singleShot(0, &devMgrTest, SLOT(run()));
return app.exec();
}
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+=.
TEMPLATE = subdirs
SUBDIRS = devicemanager
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment