LinkManagerTest.cc 3.12 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14 15 16 17

/// @file
///     @brief LinkManager Unit Test
///
///     @author Don Gagne <don@thegagnes.com>

#include "LinkManagerTest.h"
#include "MockLink.h"
18
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
19 20 21 22 23 24 25 26 27

LinkManagerTest::LinkManagerTest(void) :
    _linkMgr(NULL),
    _multiSpy(NULL)
{
}

void LinkManagerTest::init(void)
{
Don Gagne's avatar
Don Gagne committed
28 29
    UnitTest::init();
    
Don Gagne's avatar
Don Gagne committed
30 31 32
    Q_ASSERT(_linkMgr == NULL);
    Q_ASSERT(_multiSpy == NULL);
    
33
    _linkMgr = qgcApp()->toolbox()->linkManager();
Don Gagne's avatar
Don Gagne committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    Q_CHECK_PTR(_linkMgr);
    
    _rgSignals[newLinkSignalIndex] = SIGNAL(newLink(LinkInterface*));
    _rgSignals[linkDeletedSignalIndex] = SIGNAL(linkDeleted(LinkInterface*));
    
    _multiSpy = new MultiSignalSpy();
    QCOMPARE(_multiSpy->init(_linkMgr, _rgSignals, _cSignals), true);
}

void LinkManagerTest::cleanup(void)
{
    Q_ASSERT(_linkMgr);
    Q_ASSERT(_multiSpy);
    
    delete _multiSpy;
    
    _linkMgr = NULL;
    _multiSpy = NULL;
    
Don Gagne's avatar
Don Gagne committed
53
    UnitTest::cleanup();
Don Gagne's avatar
Don Gagne committed
54 55
}

Don Gagne's avatar
Don Gagne committed
56

Don Gagne's avatar
Don Gagne committed
57 58 59
void LinkManagerTest::_add_test(void)
{
    Q_ASSERT(_linkMgr);
60
    Q_ASSERT(_linkMgr->links().count() == 0);
Don Gagne's avatar
Don Gagne committed
61
    
62
    _connectMockLink();
Don Gagne's avatar
Don Gagne committed
63
    
64 65
    QCOMPARE(_linkMgr->links().count(), 1);
    QCOMPARE(_linkMgr->links().at(0), _mockLink);
Don Gagne's avatar
Don Gagne committed
66 67 68 69 70
}

void LinkManagerTest::_delete_test(void)
{
    Q_ASSERT(_linkMgr);
71
    Q_ASSERT(_linkMgr->links().count() == 0);
Don Gagne's avatar
Don Gagne committed
72
    
73 74
    _connectMockLink();
    _disconnectMockLink();
Don Gagne's avatar
Don Gagne committed
75
    
76
    QCOMPARE(_linkMgr->links().count(), 0);
Don Gagne's avatar
Don Gagne committed
77 78 79 80 81
}

void LinkManagerTest::_addSignals_test(void)
{
    Q_ASSERT(_linkMgr);
82
    Q_ASSERT(_linkMgr->links().count() == 0);
Don Gagne's avatar
Don Gagne committed
83 84
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
85 86
    _connectMockLink();

Don Gagne's avatar
Don Gagne committed
87 88 89 90 91 92 93 94 95
    QCOMPARE(_multiSpy->checkOnlySignalByMask(newLinkSignalMask), true);
    QSignalSpy* spy = _multiSpy->getSpyByIndex(newLinkSignalIndex);
    
    // Check signal argument
    QList<QVariant> signalArgs = spy->takeFirst();
    QCOMPARE(signalArgs.count(), 1);
    QObject* object = qvariant_cast<QObject *>(signalArgs[0]);
    QVERIFY(object != NULL);
    MockLink* signalLink = qobject_cast<MockLink*>(object);
96
    QCOMPARE(signalLink, _mockLink);
Don Gagne's avatar
Don Gagne committed
97 98 99 100 101
}

void LinkManagerTest::_deleteSignals_test(void)
{
    Q_ASSERT(_linkMgr);
102
    Q_ASSERT(_linkMgr->links().count() == 0);
Don Gagne's avatar
Don Gagne committed
103 104
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
105
    _connectMockLink();
Don Gagne's avatar
Don Gagne committed
106
    _multiSpy->clearAllSignals();
107
    _disconnectMockLink();
Don Gagne's avatar
Don Gagne committed
108 109 110 111 112 113 114 115 116
    
    QCOMPARE(_multiSpy->checkOnlySignalByMask(linkDeletedSignalMask), true);
    QSignalSpy* spy = _multiSpy->getSpyByIndex(linkDeletedSignalIndex);
    
    // Best we can do is check the argument count. We can't check the contents of signal argument
    // because the object has been deleted and any access to the variant to try to cast it back
    // to MockLink* will cause the link to be de-referenced and hence crash.
    QList<QVariant> signalArgs = spy->takeFirst();
    QCOMPARE(signalArgs.count(), 1);
Don Gagne's avatar
Don Gagne committed
117
}