MavlinkLogTest.cc 5.51 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.
 *
 ****************************************************************************/

10 11 12 13 14 15 16 17

/// @file
///     @brief Test for mavlink log collection
///
///     @author Don Gagne <don@thegagnes.com>

#include "MavlinkLogTest.h"
#include "MockLink.h"
18
#include "QGCTemporaryFile.h"
19
#include "QGCApplication.h"
20
#include "UAS.h"
21
#include "MultiVehicleManager.h"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

const char* MavlinkLogTest::_tempLogFileTemplate = "FlightDataXXXXXX"; ///< Template for temporary log file
const char* MavlinkLogTest::_logFileExtension = "mavlink";             ///< Extension for log files
const char* MavlinkLogTest::_saveLogFilename = "qgroundcontrol.mavlink.ut";        ///< Filename to save log files to


MavlinkLogTest::MavlinkLogTest(void)
{
    
}

void MavlinkLogTest::init(void)
{
    UnitTest::init();
    
    // Make sure temp directory is clear of mavlink logs
    QDir tmpDir(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
    QStringList logFiles(tmpDir.entryList(QStringList(QString("*.%1").arg(_logFileExtension)), QDir::Files));
40
    for(const QString &logFile: logFiles) {
41 42 43 44 45 46 47 48 49 50 51 52
        bool success = tmpDir.remove(logFile);
        Q_UNUSED(success);
        Q_ASSERT(success);
    }
}

void MavlinkLogTest::cleanup(void)
{
    // Make sure no left over logs in temp directory
    QDir tmpDir(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
    QStringList logFiles(tmpDir.entryList(QStringList(QString("*.%1").arg(_logFileExtension)), QDir::Files));
    QCOMPARE(logFiles.count(), 0);
Don Gagne's avatar
Don Gagne committed
53 54
    
    UnitTest::cleanup();
55 56
}

57
void MavlinkLogTest::_createTempLogFile(bool zeroLength)
58
{
59
    QGCTemporaryFile tempLogFile(QString("%1.%2").arg(_tempLogFileTemplate).arg(_logFileExtension));
60 61
    
    tempLogFile.open();
62 63 64
    if (!zeroLength) {
        tempLogFile.write("foo");
    }
65
    tempLogFile.close();
66 67 68 69 70 71
}

void MavlinkLogTest::_bootLogDetectionCancel_test(void)
{
    // Create a fake mavlink log
    _createTempLogFile(false);
72
    
73 74 75 76
    // We should get a message box, followed by a getSaveFileName dialog.
    setExpectedMessageBox(QMessageBox::Ok);
    setExpectedFileDialog(getSaveFileName, QStringList());

77
    // Kick the protocol to check for lost log files and wait for signals to move through
78
    connect(this, &MavlinkLogTest::checkForLostLogFiles, qgcApp()->toolbox()->mavlinkProtocol(), &MAVLinkProtocol::checkForLostLogFiles);
79 80
    emit checkForLostLogFiles();
    QTest::qWait(1000);
81 82 83 84 85 86 87 88
    
    checkExpectedMessageBox();
    checkExpectedFileDialog();
}

void MavlinkLogTest::_bootLogDetectionSave_test(void)
{
    // Create a fake mavlink log
89
    _createTempLogFile(false);
90 91 92
    
    // We should get a message box, followed by a getSaveFileName dialog.
    setExpectedMessageBox(QMessageBox::Ok);
93
    QDir logSaveDir(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
94 95 96
    QString logSaveFile(logSaveDir.filePath(_saveLogFilename));
    setExpectedFileDialog(getSaveFileName, QStringList(logSaveFile));
    
97
    // Kick the protocol to check for lost log files and wait for signals to move through
98
    connect(this, &MavlinkLogTest::checkForLostLogFiles, qgcApp()->toolbox()->mavlinkProtocol(), &MAVLinkProtocol::checkForLostLogFiles);
99 100
    emit checkForLostLogFiles();
    QTest::qWait(1000);
101 102 103 104 105 106 107 108 109 110
    
    checkExpectedMessageBox();
    checkExpectedFileDialog();
    
    // Make sure the file is there and delete it
    QCOMPARE(logSaveDir.remove(_saveLogFilename), true);
}

void MavlinkLogTest::_bootLogDetectionZeroLength_test(void)
{
111
    // Create a fake empty mavlink log
112
    _createTempLogFile(true);
113
    
114
    // Kick the protocol to check for lost log files and wait for signals to move through
115
    connect(this, &MavlinkLogTest::checkForLostLogFiles, qgcApp()->toolbox()->mavlinkProtocol(), &MAVLinkProtocol::checkForLostLogFiles);
116 117
    emit checkForLostLogFiles();
    QTest::qWait(1000);
118
    
119
    // Zero length log files should not generate any additional UI pop-ups. It should just be deleted silently.
120 121
}

122
void MavlinkLogTest::_connectLogWorker(bool arm)
123
{
124
    _connectMockLink();
125 126 127 128

    QDir logSaveDir;
    
    if (arm) {
129
        qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()->setArmed(true);
Don Gagne's avatar
Don Gagne committed
130
        QTest::qWait(500); // Wait long enough for heartbeat to come through
131 132
        
        // On Disconnect: We should get a getSaveFileName dialog.
133
        logSaveDir.setPath(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
134 135 136
        QString logSaveFile(logSaveDir.filePath(_saveLogFilename));
        setExpectedFileDialog(getSaveFileName, QStringList(logSaveFile));
    }
137
    
138
    _disconnectMockLink();
139

140 141
    if (arm) {
        checkExpectedFileDialog();
142
    
143 144 145 146 147 148 149 150 151 152 153 154
        // Make sure the file is there and delete it
        QCOMPARE(logSaveDir.remove(_saveLogFilename), true);
    }
}

void MavlinkLogTest::_connectLogNoArm_test(void)
{
    _connectLogWorker(false);
}

void MavlinkLogTest::_connectLogArm_test(void)
{
Don Gagne's avatar
Don Gagne committed
155
    _connectLogWorker(true);
156 157
}

158 159 160 161 162 163 164 165 166 167
void MavlinkLogTest::_deleteTempLogFiles_test(void)
{
    // Verify that the MAVLinkProtocol::deleteTempLogFiles api works correctly
    
    _createTempLogFile(false);
    MAVLinkProtocol::deleteTempLogFiles();
    QDir tmpDir(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
    QStringList logFiles(tmpDir.entryList(QStringList(QString("*.%1").arg(_logFileExtension)), QDir::Files));
    QCOMPARE(logFiles.count(), 0);
}