localqmlprofilerrunner_test.cpp 5.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "localqmlprofilerrunner_test.h"
27 28 29

#include "../qmlprofilerruncontrol.h"

30 31
#include <debugger/analyzer/analyzermanager.h>
#include <debugger/analyzer/analyzerstartparameters.h>
32 33 34

#include <projectexplorer/runnables.h>

35 36 37
#include <QtTest>
#include <QTcpServer>

38 39
using namespace ProjectExplorer;

40 41 42 43 44 45 46
namespace QmlProfiler {
namespace Internal {

LocalQmlProfilerRunnerTest::LocalQmlProfilerRunnerTest(QObject *parent) : QObject(parent)
{
}

47
void LocalQmlProfilerRunnerTest::connectRunner(QmlProfilerRunner *runner)
48
{
49
    connect(runner, &QmlProfilerRunner::localRunnerStarted, this, [this] {
50 51 52 53
        QVERIFY(!running);
        ++runCount;
        running = true;
    });
54
    connect(runner, &QmlProfilerRunner::localRunnerStopped, this, [this] {
55 56 57 58 59
        QVERIFY(running);
        running = false;
    });
}

60 61
void LocalQmlProfilerRunnerTest::testRunner()
{
62 63
    debuggee.executable = "\\-/|\\-/";
    debuggee.environment = Utils::Environment::systemEnvironment();
64 65 66 67

    // should not be used anywhere but cannot be empty
    configuration.socket = connection.analyzerSocket = QString("invalid");

68
    rc = new ProjectExplorer::RunControl(nullptr, ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
69
    rc->setRunnable(debuggee);
70
    rc->setConnection(connection);
71 72
    auto runner = new QmlProfilerRunner(rc);
    runner->setLocalConfiguration(configuration);
73
    connectRunner(runner);
74

75 76 77
    rc->initiateStart();
    QTimer::singleShot(0, this, &LocalQmlProfilerRunnerTest::testRunner1);
}
78

79 80
void LocalQmlProfilerRunnerTest::testRunner1()
{
81 82
    QTRY_COMPARE_WITH_TIMEOUT(runCount, 1, 10000);
    QTRY_VERIFY_WITH_TIMEOUT(!running, 10000);
83

84
    configuration.socket = connection.analyzerSocket = QmlProfilerRunner::findFreeSocket();
85

86
    debuggee.executable = QCoreApplication::applicationFilePath();
87
    // comma is used to specify a test function. In this case, an invalid one.
88
    debuggee.commandLineArguments = QString("-test QmlProfiler,");
89 90

    delete rc;
91
    rc = new ProjectExplorer::RunControl(nullptr, ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
92
    rc->setRunnable(debuggee);
93
    rc->setConnection(connection);
94 95
    auto runner = new QmlProfilerRunner(rc);
    runner->setLocalConfiguration(configuration);
96 97 98 99
    connectRunner(runner);
    rc->initiateStart();
    QTimer::singleShot(0, this, &LocalQmlProfilerRunnerTest::testRunner2);
}
100

101 102
void LocalQmlProfilerRunnerTest::testRunner2()
{
103 104
    QTRY_COMPARE_WITH_TIMEOUT(runCount, 2, 10000);
    QTRY_VERIFY_WITH_TIMEOUT(!running, 10000);
105 106 107

    delete rc;

108
    debuggee.commandLineArguments.clear();
109 110 111
    configuration.socket.clear();
    connection.analyzerSocket.clear();
    configuration.port = connection.analyzerPort =
112
            QmlProfilerRunner::findFreePort(connection.analyzerHost);
113
    rc = new ProjectExplorer::RunControl(nullptr, ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
114
    rc->setRunnable(debuggee);
115
    rc->setConnection(connection);
116 117
    auto runner = new QmlProfilerRunner(rc);
    runner->setLocalConfiguration(configuration);
118 119
    connectRunner(runner);
    rc->initiateStart();
120

121 122 123 124 125
    QTimer::singleShot(0, this, &LocalQmlProfilerRunnerTest::testRunner3);
}

void LocalQmlProfilerRunnerTest::testRunner3()
{
126
    QTRY_COMPARE_WITH_TIMEOUT(runCount, 3, 10000);
127 128 129
    rc->initiateStop();
    QTimer::singleShot(0, this, &LocalQmlProfilerRunnerTest::testRunner4);
}
130

131 132 133
void LocalQmlProfilerRunnerTest::testRunner4()
{
    QTRY_VERIFY_WITH_TIMEOUT(!running, 10000);
134 135 136 137 138 139
    delete rc;
}

void LocalQmlProfilerRunnerTest::testFindFreePort()
{
    QString host;
140
    Utils::Port port = QmlProfilerRunner::findFreePort(host);
141 142 143 144 145 146 147 148
    QVERIFY(port.isValid());
    QVERIFY(!host.isEmpty());
    QTcpServer server;
    QVERIFY(server.listen(QHostAddress(host), port.number()));
}

void LocalQmlProfilerRunnerTest::testFindFreeSocket()
{
149
    QString socket = QmlProfilerRunner::findFreeSocket();
150 151 152 153 154 155 156 157 158
    QVERIFY(!socket.isEmpty());
    QVERIFY(!QFile::exists(socket));
    QFile file(socket);
    QVERIFY(file.open(QIODevice::WriteOnly));
    file.close();
}

} // namespace Internal
} // namespace QmlProfiler