MultiSignalSpy.h 2.74 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

#ifndef MULTISIGNALSPY_H
#define MULTISIGNALSPY_H

#include <QObject>
#include <QSignalSpy>
Don Gagne's avatar
Don Gagne committed
16
#include <QGeoCoordinate>
Don Gagne's avatar
Don Gagne committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

/// @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>

class MultiSignalSpy : public QObject
{
    Q_OBJECT
    
public:
    MultiSignalSpy(QObject* parent = NULL);
    ~MultiSignalSpy();

    bool init(QObject* signalEmitter, const char** rgSignals, size_t cSignals);

34 35 36
    /// @param mask bit mask specifying which signals to check. The lowest order bit represents
    ///     index 0 into the rgSignals array and so on up the bit mask.
    /// @return true if signal count = 1 for the specified signals
Don Gagne's avatar
Don Gagne committed
37
    bool checkSignalByMask(quint32 mask);
38 39 40

    /// @return true if signal count = 1 for specified signals and signal count of 0
    ///     for all other signals
Don Gagne's avatar
Don Gagne committed
41
    bool checkOnlySignalByMask(quint32 mask);
42 43 44 45

    /// @param mask bit mask specifying which signals to check. The lowest order bit represents
    ///     index 0 into the rgSignals array and so on up the bit mask.
    /// @return true if signal count >= 1 for the specified signals
Don Gagne's avatar
Don Gagne committed
46
    bool checkSignalsByMask(quint32 mask);
47 48 49

    /// @return true if signal count >= 1 for specified signals and signal count of 0
    ///     for all other signals
Don Gagne's avatar
Don Gagne committed
50
    bool checkOnlySignalsByMask(quint32 mask);
51

Don Gagne's avatar
Don Gagne committed
52
    bool checkNoSignalByMask(quint32 mask);
Don Gagne's avatar
Don Gagne committed
53 54
    bool checkNoSignals(void);

Don Gagne's avatar
Don Gagne committed
55 56
    void clearSignalByIndex(quint32 index);
    void clearSignalsByMask(quint32 mask);
Don Gagne's avatar
Don Gagne committed
57 58
    void clearAllSignals(void);

Don Gagne's avatar
Don Gagne committed
59
    bool waitForSignalByIndex(quint32 index, int msec);
Don Gagne's avatar
Don Gagne committed
60
    
Don Gagne's avatar
Don Gagne committed
61 62 63 64 65 66
    QSignalSpy* getSpyByIndex(quint32 index);

    // Returns the value type for the first parameter of the signal
    bool pullBoolFromSignalIndex(quint32 index);
    int pullIntFromSignalIndex(quint32 index);
    QGeoCoordinate pullQGeoCoordinateFromSignalIndex(quint32 index);
Don Gagne's avatar
Don Gagne committed
67

Don Gagne's avatar
Don Gagne committed
68 69 70
private:
    // QObject overrides
    void timerEvent(QTimerEvent * event);
71
    
Don Gagne's avatar
Don Gagne committed
72 73 74
    void _printSignalState(quint32 mask);
    bool _checkSignalByMaskWorker(quint32 mask, bool multipleSignalsAllowed);
    bool _checkOnlySignalByMaskWorker(quint32 mask, bool multipleSignalsAllowed);
Don Gagne's avatar
Don Gagne committed
75 76 77 78 79 80 81 82 83

    QObject*        _signalEmitter;
    const char**    _rgSignals;
    QSignalSpy**    _rgSpys;
    size_t          _cSignals;
    bool            _timeout;
};

#endif