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

10 11 12 13 14 15 16 17 18

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

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

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));
41
    for(const QString &logFile: logFiles) {
42 43 44 45 46 47 48 49 50 51 52 53
        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
54 55
    
    UnitTest::cleanup();
56 57
}

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

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

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

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

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

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

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

141 142
    if (arm) {
        checkExpectedFileDialog();
143
    
144 145 146 147 148 149 150 151 152 153 154 155
        // 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
156
    _connectLogWorker(true);
157 158
}

159 160 161 162 163 164 165 166 167 168
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);
}