UnitTest.h 8.41 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

/// @file
///     @brief Base class for all unit tests
///
///     @author Don Gagne <don@thegagnes.com>

dogmaphobic's avatar
dogmaphobic committed
16
#ifndef __mobile__
17
#pragma once
Don Gagne's avatar
Don Gagne committed
18 19 20 21

#include <QObject>
#include <QtTest>
#include <QMessageBox>
22
#include <QFileDialog>
Don Gagne's avatar
Don Gagne committed
23

24 25
#include "QGCMAVLink.h"
#include "LinkInterface.h"
26
#include "Fact.h"
27
#include "MissionItem.h"
28

29
#define UT_REGISTER_TEST(className) static UnitTestWrapper<className> className(#className);
Don Gagne's avatar
Don Gagne committed
30 31

class QGCMessageBox;
32
class QGCQFileDialog;
33 34 35
class LinkManager;
class MockLink;
class MainWindow;
36
class Vehicle;
Don Gagne's avatar
Don Gagne committed
37 38 39 40 41 42 43 44

class UnitTest : public QObject
{
    Q_OBJECT

public:
    UnitTest(void);
    virtual ~UnitTest(void);
45

Don Gagne's avatar
Don Gagne committed
46 47
    /// @brief Called to run all the registered unit tests
    ///     @param singleTest Name of test to just run a single test
48
    static int run(QString& singleTest);
49

Don Gagne's avatar
Don Gagne committed
50 51 52
    /// @brief Sets up for an expected QGCMessageBox
    ///     @param response Response to take on message box
    void setExpectedMessageBox(QMessageBox::StandardButton response);
53

54 55 56 57 58 59 60
    /// @brief Types for UnitTest::setExpectedFileDialog
    enum FileDialogType {
        getExistingDirectory,
        getOpenFileName,
        getOpenFileNames,
        getSaveFileName
    };
61

62
    /// @brief Sets up for an expected QGCQFileDialog
63 64 65
    ///     @param type Type of expected file dialog
    ///     @param response Files to return from call. Multiple files only supported by getOpenFileNames
    void setExpectedFileDialog(enum FileDialogType type, QStringList response);
66

Don Gagne's avatar
Don Gagne committed
67 68
    enum {
        expectFailNoFailure =           1 << 0, ///< not expecting any failures
69 70
        expectFailNoDialog =            1 << 1, ///< expecting a failure due to no dialog displayed
        expectFailBadResponseButton =   1 << 2, ///< expecting a failure due to bad button response (QGCMessageBox only)
71
        expectFailWrongFileDialog =     1 << 3  ///< expecting one dialog type, got the wrong type (QGCQFileDialog ony)
Don Gagne's avatar
Don Gagne committed
72
    };
73

Don Gagne's avatar
Don Gagne committed
74 75 76
    /// @brief Check whether a message box was displayed and correctly responded to
    //          @param Expected failure response flags
    void checkExpectedMessageBox(int expectFailFlags = expectFailNoFailure);
77

Don Gagne's avatar
Don Gagne committed
78 79 80
    /// Checks that the specified number of message boxes where shown. Do not call setExpectedMessageBox when using this method.
    void checkMultipleExpectedMessageBox(int messageCount);

81 82 83
    /// @brief Check whether a message box was displayed and correctly responded to
    //          @param Expected failure response flags
    void checkExpectedFileDialog(int expectFailFlags = expectFailNoFailure);
84

Don Gagne's avatar
Don Gagne committed
85
    /// @brief Adds a unit test to the list. Should only be called by UnitTestWrapper.
Don Gagne's avatar
Don Gagne committed
86
    static void _addTest(QObject* test);
Don Gagne's avatar
Don Gagne committed
87

88 89 90 91 92 93 94 95
    /// Creates a file with random contents of the specified size.
    /// @return Fully qualified path to created file
    static QString createRandomFile(uint32_t byteCount);

    /// Will throw qWarning at location where files differ
    /// @return true: files are alike, false: files differ
    static bool fileCompare(const QString& file1, const QString& file2);

96 97 98 99
    /// Fuzzy compare on two doubles, where NaN is a possible value
    /// @return true: equal
    static bool doubleNaNCompare(double value1, double value2);

100
    /// Changes the Facts rawValue such that it emits a valueChanged signal.
101 102
    ///     @param increment 0 use standard increment, other increment by specified amount if double value
    void changeFactValue(Fact* fact, double increment = 0);
103

104 105 106 107
    /// Returns true is the position of the two coordinates is less then a meter from each other.
    /// Does not check altitude.
    static bool fuzzyCompareLatLon(const QGeoCoordinate& coord1, const QGeoCoordinate& coord2);

Don Gagne's avatar
Don Gagne committed
108
protected slots:
109

Don Gagne's avatar
Don Gagne committed
110 111
    // These are all pure virtuals to force the derived class to implement each one and in turn
    // call the UnitTest private implementation.
112

Don Gagne's avatar
Don Gagne committed
113
    /// @brief Called before each test.
114
    ///         Make sure to call UnitTest::init first in your derived class.
Don Gagne's avatar
Don Gagne committed
115
    virtual void init(void);
116

Don Gagne's avatar
Don Gagne committed
117
    /// @brief Called after each test.
118
    ///         Make sure to call UnitTest::cleanup last in your derived class.
Don Gagne's avatar
Don Gagne committed
119
    virtual void cleanup(void);
120

Don Gagne's avatar
Don Gagne committed
121
protected:
122
    void _connectMockLink(MAV_AUTOPILOT autopilot = MAV_AUTOPILOT_PX4);
123
    void _connectMockLinkNoInitialConnectSequence(void) { _connectMockLink(MAV_AUTOPILOT_INVALID); }
124 125 126
    void _disconnectMockLink(void);
    void _createMainWindow(void);
    void _closeMainWindow(bool cancelExpected = false);
127
    void _missionItemsEqual(MissionItem& actual, MissionItem& expected);
128 129 130 131

    LinkManager*    _linkManager;
    MockLink*       _mockLink;
    MainWindow*     _mainWindow;
132
    Vehicle*        _vehicle;
133

134 135
    bool _expectMissedFileDialog;   // true: expect a missed file dialog, used for internal testing
    bool _expectMissedMessageBox;   // true: expect a missed message box, used for internal testing
136 137 138 139

private slots:
    void _linkDeleted(LinkInterface* link);

Don Gagne's avatar
Don Gagne committed
140 141
private:
    // When the app is running in unit test mode the QGCMessageBox methods are re-routed here.
142

143 144 145 146 147
    static QMessageBox::StandardButton _messageBox(QMessageBox::Icon icon,
                                                   const QString& title,
                                                   const QString& text,
                                                   QMessageBox::StandardButtons buttons,
                                                   QMessageBox::StandardButton defaultButton);
148

Don Gagne's avatar
Don Gagne committed
149 150
    // This allows the private call to _messageBox
    friend class QGCMessageBox;
151

152
    // When the app is running in unit test mode the QGCQFileDialog methods are re-routed here.
153

154 155 156 157 158
    static QString _getExistingDirectory(
        QWidget* parent,
        const QString& caption,
        const QString& dir,
        QFileDialog::Options options);
159

160 161 162 163 164 165
    static QString _getOpenFileName(
        QWidget* parent,
        const QString& caption,
        const QString& dir,
        const QString& filter,
        QFileDialog::Options options);
166

167 168 169 170 171 172
    static QStringList _getOpenFileNames(
        QWidget* parent,
        const QString& caption,
        const QString& dir,
        const QString& filter,
        QFileDialog::Options options);
173

174 175 176 177 178
    static QString _getSaveFileName(
        QWidget* parent,
        const QString& caption,
        const QString& dir,
        const QString& filter,
179 180 181
        const QString& defaultSuffix,
        QFileDialog::Options options);

182 183 184
    static QString _fileDialogResponseSingle(enum FileDialogType type);

    // This allows the private calls to the file dialog methods
185
    friend class QGCQFileDialog;
186

Don Gagne's avatar
Don Gagne committed
187
    void _unitTestCalled(void);
Don Gagne's avatar
Don Gagne committed
188 189
	static QList<QObject*>& _testList(void);

190
    // Catch QGCMessageBox calls
Don Gagne's avatar
Don Gagne committed
191 192 193 194
    static bool                         _messageBoxRespondedTo;     ///< Message box was responded to
    static bool                         _badResponseButton;         ///< Attempt to repond to expected message box with button not being displayed
    static QMessageBox::StandardButton  _messageBoxResponseButton;  ///< Response to next message box
    static int                          _missedMessageBoxCount;     ///< Count of message box not checked with call to messageBoxWasDisplayed
195

196
    // Catch QGCQFileDialog calls
197 198 199 200 201
    static bool         _fileDialogRespondedTo;         ///< File dialog was responded to
    static bool         _fileDialogResponseSet;         ///< true: _fileDialogResponse was set by a call to UnitTest::setExpectedFileDialog
    static QStringList  _fileDialogResponse;            ///< Response to next file dialog
    static enum FileDialogType _fileDialogExpectedType; ///< type of file dialog expected to show
    static int          _missedFileDialogCount;         ///< Count of file dialogs not checked with call to UnitTest::fileDialogWasDisplayed
202

Don Gagne's avatar
Don Gagne committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    bool _unitTestRun;              ///< true: Unit Test was run
    bool _initCalled;               ///< true: UnitTest::_init was called
    bool _cleanupCalled;            ///< true: UnitTest::_cleanup was called
};

template <class T>
class UnitTestWrapper {
public:
    UnitTestWrapper(const QString& name) :
        _unitTest(new T)
    {
        _unitTest->setObjectName(name);
        UnitTest::_addTest(_unitTest.data());
    }

private:
    QSharedPointer<T> _unitTest;
};

#endif