MultiSignalSpy.cc 7.07 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
/*=====================================================================
 
 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/>.
 
 ======================================================================*/

#include "MultiSignalSpy.h"
#include <QEventLoop>
#include <QCoreApplication>
#include <QDebug>
28
#include <QTest>
Don Gagne's avatar
Don Gagne committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

/// @file
///     @brief This class allows you to keep track of signal counts on a set of signals associated with an object.
///     Mainly used for writing object unit tests.
///
///     @author Don Gagne <don@thegagnes.com>

MultiSignalSpy::MultiSignalSpy(QObject* parent) :
    QObject(parent),
    _signalEmitter(NULL),
    _rgSignals(NULL),
    _cSignals(0)
{
}

MultiSignalSpy::~MultiSignalSpy()
{
    Q_ASSERT(_rgSignals);

    for (size_t i=0; i<_cSignals; i++) {
        delete _rgSpys[i];
    }
}

/// Initializes the class. Must be called once before use.
///     @return true if success, false for failure

bool MultiSignalSpy::init(
                        QObject*        signalEmitter,  ///< [in] object which the signals are emitted from
                        const char**    rgSignals,      ///< [in] array of signals to spy on
                        size_t          cSignals)       ///< [in] numbers of signals in rgSignals
{
    if (!signalEmitter || !rgSignals || cSignals == 0) {
        qDebug() << "Invalid arguments";
        return false;
    }
    
    _signalEmitter = signalEmitter;
    _rgSignals = rgSignals;
    _cSignals = cSignals;

    // Allocate and connect QSignalSpy's
    _rgSpys = new QSignalSpy*[_cSignals];
    Q_ASSERT(_rgSpys != NULL);
    for (size_t i=0; i<_cSignals; i++) {
        _rgSpys[i] = new QSignalSpy(_signalEmitter, _rgSignals[i]);
        if (_rgSpys[i] == NULL) {
            qDebug() << "Unabled to allocated QSignalSpy";
            return false;
        }
        if (!_rgSpys[i]->isValid()) {
            qDebug() << "Invalid signal";
            return false;
        }
    }
    
    return true;
}

88
bool MultiSignalSpy::_checkSignalByMaskWorker(quint16 mask, bool multipleSignalsAllowed)
Don Gagne's avatar
Don Gagne committed
89 90 91 92 93 94
{
    for (size_t i=0; i<_cSignals; i++) {
        if ((1 << i) & mask) {
            QSignalSpy* spy = _rgSpys[i];
            Q_ASSERT(spy != NULL);
            
95
            if ((multipleSignalsAllowed && spy->count() ==  0) || spy->count() != 1) {
96
                _printSignalState();
Don Gagne's avatar
Don Gagne committed
97 98 99 100 101 102 103 104
                return false;
            }
        }
    }
    
    return true;
}

105
bool MultiSignalSpy::_checkOnlySignalByMaskWorker(quint16 mask, bool multipleSignalsAllowed)
Don Gagne's avatar
Don Gagne committed
106 107 108 109 110 111
{
    for (size_t i=0; i<_cSignals; i++) {
        QSignalSpy* spy = _rgSpys[i];
        Q_ASSERT(spy != NULL);

        if ((1 << i) & mask) {
112
            if ((multipleSignalsAllowed && spy->count() ==  0) || (!multipleSignalsAllowed && spy->count() != 1)) {
113
                _printSignalState();
Don Gagne's avatar
Don Gagne committed
114 115 116 117
                return false;
            }
        } else {
            if (spy->count() != 0) {
118
                _printSignalState();
Don Gagne's avatar
Don Gagne committed
119 120 121 122 123 124 125 126
                return false;
            }
        }
    }
    
    return true;
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
bool MultiSignalSpy::checkSignalByMask(quint16 mask)
{
    return _checkSignalByMaskWorker(mask, false /* multipleSignalsAllowed */);
}

bool MultiSignalSpy::checkOnlySignalByMask(quint16 mask)
{
    return _checkOnlySignalByMaskWorker(mask, false /* multipleSignalsAllowed */);
}

bool MultiSignalSpy::checkSignalsByMask(quint16 mask)
{
    return _checkSignalByMaskWorker(mask, true /* multipleSignalsAllowed */);
}

bool MultiSignalSpy::checkOnlySignalsByMask(quint16 mask)
{
    return _checkOnlySignalByMaskWorker(mask, true /* multipleSignalsAllowed */);
}

Don Gagne's avatar
Don Gagne committed
147 148 149 150 151 152 153 154 155
/// @return true if signal count = 0 for specified signals
bool MultiSignalSpy::checkNoSignalByMask(quint16 mask)
{
    for (size_t i=0; i<_cSignals; i++) {
        if ((1 << i) & mask) {
            QSignalSpy* spy = _rgSpys[i];
            Q_ASSERT(spy != NULL);

            if (spy->count() != 0) {
156
                _printSignalState();
Don Gagne's avatar
Don Gagne committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                return false;
            }
        }
    }
    
    return true;
}

/// @return true if signal count = 0 on all signals
bool MultiSignalSpy::checkNoSignals(void)
{
    return checkNoSignalByMask(~0);
}

/// @return QSignalSpy for the specified signal
QSignalSpy* MultiSignalSpy::getSpyByIndex(quint16 index)
{
    Q_ASSERT(index < _cSignals);
    Q_ASSERT(_rgSpys[index] != NULL);

    return _rgSpys[index];
}

/// Sets the signal count to 0 for the specified signal
void MultiSignalSpy::clearSignalByIndex(quint16 index)
{
    Q_ASSERT(index < _cSignals);
    Q_ASSERT(_rgSpys[index] != NULL);
    
    _rgSpys[index]->clear();
}

/// Sets the signal count to 0 for all specified signals
void MultiSignalSpy::clearSignalsByMask(quint16 mask)
{
    for (size_t i=0; i<_cSignals; i++) {
        if ((1 << i) & mask) {
            QSignalSpy* spy = _rgSpys[i];
            Q_ASSERT(spy != NULL);

            spy->clear();
        }
    }
}

/// Sets the signal count to 0 for all signals
void MultiSignalSpy::clearAllSignals(void)
{
    for (quint16 i=0;i<_cSignals; i++) {
        clearSignalByIndex(i);
    }
}

void MultiSignalSpy::timerEvent(QTimerEvent * event)
{
    Q_UNUSED(event);
    _timeout = true;
}

/// Waits the specified signal
///     @return false for timeout
bool MultiSignalSpy::waitForSignalByIndex(
                                          quint16 index,  ///< [in] index of signal to wait on
                                          int     msec)   ///< [in] numbers of milleconds to wait before timeout, -1 wait forever
{
    // Check input parameters
    if (msec < -1 || msec == 0)
        return false;
    
    // activate the timeout
    _timeout = false;
    int timerId;
    if (msec != -1) {
        timerId = startTimer(msec);
        Q_ASSERT(timerId);
    } else {
        timerId = 0;
    }
    
    // Begin waiting
    QSignalSpy* spy = _rgSpys[index];
    Q_ASSERT(spy);
    
    while (spy->count() == 0 && !_timeout) {
Don Gagne's avatar
Don Gagne committed
241
        QTest::qWait(100);
Don Gagne's avatar
Don Gagne committed
242 243 244 245 246 247 248 249 250
    }
    
    // Clean up and return status
    if (timerId) {
        killTimer(timerId);
    }

    return spy->count() != 0;
}
251 252 253 254 255 256 257 258 259

void MultiSignalSpy::_printSignalState(void)
{
    for (size_t i=0; i<_cSignals; i++) {
        QSignalSpy* spy = _rgSpys[i];
        Q_ASSERT(spy != NULL);
        qDebug() << "Signal index:" << i << "count:" << spy->count();
    }
}
Don Gagne's avatar
Don Gagne committed
260 261 262 263 264 265

bool MultiSignalSpy::pullBoolFromSignalIndex(quint16 index)
{
    QSignalSpy* spy = getSpyByIndex(index);
    return spy->value(0).value(0).toBool();
}