LinkManagerTest.cc 3.14 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * 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

LinkManagerTest::LinkManagerTest(void) :
21 22
    _linkMgr(nullptr),
    _multiSpy(nullptr)
Don Gagne's avatar
Don Gagne committed
23 24 25 26 27
{
}

void LinkManagerTest::init(void)
{
Don Gagne's avatar
Don Gagne committed
28 29
    UnitTest::init();
    
30 31
    Q_ASSERT(_linkMgr == nullptr);
    Q_ASSERT(_multiSpy == nullptr);
Don Gagne's avatar
Don Gagne committed
32
    
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
    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;
    
50 51
    _linkMgr = nullptr;
    _multiSpy = nullptr;
Don Gagne's avatar
Don Gagne committed
52
    
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
    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]);
94
    QVERIFY(object != nullptr);
Don Gagne's avatar
Don Gagne committed
95
    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
}