UnitTest.h 8.62 KB
Newer Older
1 2
/****************************************************************************
 *
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.
 *
 ****************************************************************************/

dogmaphobic's avatar
dogmaphobic committed
10
#ifndef __mobile__
11
#pragma once
Don Gagne's avatar
Don Gagne committed
12 13 14 15

#include <QObject>
#include <QtTest>
#include <QMessageBox>
16
#include <QFileDialog>
Don Gagne's avatar
Don Gagne committed
17

18 19
#include "QGCMAVLink.h"
#include "LinkInterface.h"
20
#include "Fact.h"
21
#include "MissionItem.h"
22

23 24
#define UT_REGISTER_TEST(className)             static UnitTestWrapper<className> className(#className, false);
#define UT_REGISTER_TEST_STANDALONE(className)  static UnitTestWrapper<className> className(#className, true);  // Test will only be run with specifically called to from command line
Don Gagne's avatar
Don Gagne committed
25 26

class QGCMessageBox;
27
class QGCQFileDialog;
28 29
class LinkManager;
class MockLink;
30
class Vehicle;
Don Gagne's avatar
Don Gagne committed
31 32 33 34 35 36 37 38

class UnitTest : public QObject
{
    Q_OBJECT

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

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

Don Gagne's avatar
Don Gagne committed
44 45 46
    /// @brief Sets up for an expected QGCMessageBox
    ///     @param response Response to take on message box
    void setExpectedMessageBox(QMessageBox::StandardButton response);
47

48 49 50 51 52 53 54
    /// @brief Types for UnitTest::setExpectedFileDialog
    enum FileDialogType {
        getExistingDirectory,
        getOpenFileName,
        getOpenFileNames,
        getSaveFileName
    };
55

56
    /// @brief Sets up for an expected QGCQFileDialog
57 58 59
    ///     @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);
60

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

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

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

75 76 77
    /// @brief Check whether a message box was displayed and correctly responded to
    //          @param Expected failure response flags
    void checkExpectedFileDialog(int expectFailFlags = expectFailNoFailure);
78

79 80 81
    bool standalone(void) { return _standalone; }
    void setStandalone(bool standalone) { _standalone = standalone; }

Don Gagne's avatar
Don Gagne committed
82
    /// @brief Adds a unit test to the list. Should only be called by UnitTestWrapper.
83
    static void _addTest(UnitTest* test);
Don Gagne's avatar
Don Gagne committed
84

85 86 87 88 89 90 91 92
    /// 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);

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

97 98
    QGeoCoordinate changeCoordinateValue(const QGeoCoordinate& coordinate);

99 100 101 102
    /// 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
103
protected slots:
104

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

Don Gagne's avatar
Don Gagne committed
108
    /// @brief Called before each test.
109
    ///         Make sure to call UnitTest::init first in your derived class.
Don Gagne's avatar
Don Gagne committed
110
    virtual void init(void);
111

Don Gagne's avatar
Don Gagne committed
112
    /// @brief Called after each test.
113
    ///         Make sure to call UnitTest::cleanup last in your derived class.
Don Gagne's avatar
Don Gagne committed
114
    virtual void cleanup(void);
115

Don Gagne's avatar
Don Gagne committed
116
protected:
117
    void _connectMockLink(MAV_AUTOPILOT autopilot = MAV_AUTOPILOT_PX4);
118
    void _connectMockLinkNoInitialConnectSequence(void) { _connectMockLink(MAV_AUTOPILOT_INVALID); }
119
    void _disconnectMockLink(void);
120
    void _missionItemsEqual(MissionItem& actual, MissionItem& expected);
121

122 123 124
    LinkManager*    _linkManager    = nullptr;
    MockLink*       _mockLink       = nullptr;
    Vehicle*        _vehicle        = nullptr;
125

126 127
    bool _expectMissedFileDialog = false;   // true: expect a missed file dialog, used for internal testing
    bool _expectMissedMessageBox = false;   // true: expect a missed message box, used for internal testing
128 129 130 131

private slots:
    void _linkDeleted(LinkInterface* link);

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

135 136 137 138 139
    static QMessageBox::StandardButton _messageBox(QMessageBox::Icon icon,
                                                   const QString& title,
                                                   const QString& text,
                                                   QMessageBox::StandardButtons buttons,
                                                   QMessageBox::StandardButton defaultButton);
140

Don Gagne's avatar
Don Gagne committed
141 142
    // This allows the private call to _messageBox
    friend class QGCMessageBox;
143

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

146 147 148 149 150
    static QString _getExistingDirectory(
        QWidget* parent,
        const QString& caption,
        const QString& dir,
        QFileDialog::Options options);
151

152 153 154 155 156 157
    static QString _getOpenFileName(
        QWidget* parent,
        const QString& caption,
        const QString& dir,
        const QString& filter,
        QFileDialog::Options options);
158

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

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

174 175 176
    static QString _fileDialogResponseSingle(enum FileDialogType type);

    // This allows the private calls to the file dialog methods
177
    friend class QGCQFileDialog;
178

Don Gagne's avatar
Don Gagne committed
179
    void _unitTestCalled(void);
180
    static QList<UnitTest*>& _testList(void);
Don Gagne's avatar
Don Gagne committed
181

182
    // Catch QGCMessageBox calls
Don Gagne's avatar
Don Gagne committed
183 184 185 186
    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
187

188
    // Catch QGCQFileDialog calls
189 190 191 192 193
    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
194

195 196 197 198
    bool _unitTestRun   = false;    ///< true: Unit Test was run
    bool _initCalled    = false;    ///< true: UnitTest::_init was called
    bool _cleanupCalled = false;    ///< true: UnitTest::_cleanup was called
    bool _standalone    = false;    ///< true: Only run when requested specifically from command line
Don Gagne's avatar
Don Gagne committed
199 200 201 202 203
};

template <class T>
class UnitTestWrapper {
public:
204 205
    UnitTestWrapper(const QString& name, bool standalone)
        : _unitTest(new T)
Don Gagne's avatar
Don Gagne committed
206 207
    {
        _unitTest->setObjectName(name);
208
        _unitTest->setStandalone(standalone);
Don Gagne's avatar
Don Gagne committed
209 210 211 212 213 214 215 216
        UnitTest::_addTest(_unitTest.data());
    }

private:
    QSharedPointer<T> _unitTest;
};

#endif