LinkManagerTest.cc 3.73 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

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

#include "LinkManagerTest.h"
#include "MockLink.h"
31
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
32 33 34 35 36 37 38 39 40 41

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

void LinkManagerTest::init(void)
{
Don Gagne's avatar
Don Gagne committed
42 43
    UnitTest::init();
    
Don Gagne's avatar
Don Gagne committed
44 45 46
    Q_ASSERT(_linkMgr == NULL);
    Q_ASSERT(_multiSpy == NULL);
    
47
    _linkMgr = qgcApp()->toolbox()->linkManager();
Don Gagne's avatar
Don Gagne committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    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
67
    UnitTest::cleanup();
Don Gagne's avatar
Don Gagne committed
68 69
}

Don Gagne's avatar
Don Gagne committed
70

Don Gagne's avatar
Don Gagne committed
71 72 73
void LinkManagerTest::_add_test(void)
{
    Q_ASSERT(_linkMgr);
Don Gagne's avatar
Don Gagne committed
74
    Q_ASSERT(_linkMgr->links()->count() == 0);
Don Gagne's avatar
Don Gagne committed
75
    
76
    _connectMockLink();
Don Gagne's avatar
Don Gagne committed
77
    
Don Gagne's avatar
Don Gagne committed
78 79
    QCOMPARE(_linkMgr->links()->count(), 1);
    QCOMPARE(_linkMgr->links()->value<MockLink*>(0), _mockLink);
Don Gagne's avatar
Don Gagne committed
80 81 82 83 84
}

void LinkManagerTest::_delete_test(void)
{
    Q_ASSERT(_linkMgr);
Don Gagne's avatar
Don Gagne committed
85
    Q_ASSERT(_linkMgr->links()->count() == 0);
Don Gagne's avatar
Don Gagne committed
86
    
87 88
    _connectMockLink();
    _disconnectMockLink();
Don Gagne's avatar
Don Gagne committed
89
    
Don Gagne's avatar
Don Gagne committed
90
    QCOMPARE(_linkMgr->links()->count(), 0);
Don Gagne's avatar
Don Gagne committed
91 92 93 94 95
}

void LinkManagerTest::_addSignals_test(void)
{
    Q_ASSERT(_linkMgr);
Don Gagne's avatar
Don Gagne committed
96
    Q_ASSERT(_linkMgr->links()->count() == 0);
Don Gagne's avatar
Don Gagne committed
97 98
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
99 100
    _connectMockLink();

Don Gagne's avatar
Don Gagne committed
101 102 103 104 105 106 107 108 109
    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);
110
    QCOMPARE(signalLink, _mockLink);
Don Gagne's avatar
Don Gagne committed
111 112 113 114 115
}

void LinkManagerTest::_deleteSignals_test(void)
{
    Q_ASSERT(_linkMgr);
Don Gagne's avatar
Don Gagne committed
116
    Q_ASSERT(_linkMgr->links()->count() == 0);
Don Gagne's avatar
Don Gagne committed
117 118
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
119
    _connectMockLink();
Don Gagne's avatar
Don Gagne committed
120
    _multiSpy->clearAllSignals();
121
    _disconnectMockLink();
Don Gagne's avatar
Don Gagne committed
122 123 124 125 126 127 128 129 130
    
    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
131
}