localqmlprofilerrunner_test.cpp 4.26 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
#include <debugger/analyzer/analyzermanager.h>
31 32 33

#include <projectexplorer/runnables.h>

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

37 38
using namespace ProjectExplorer;

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

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

hjk's avatar
hjk committed
46
void LocalQmlProfilerRunnerTest::start()
47
{
hjk's avatar
hjk committed
48 49 50 51 52 53
    delete runControl;
    runControl = new ProjectExplorer::RunControl(nullptr, ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
    runControl->setRunnable(debuggee);
    auto runner = new LocalQmlProfilerSupport(runControl, serverUrl);

    connect(runner, &LocalQmlProfilerSupport::localRunnerStarted, this, [this] {
54 55 56 57
        QVERIFY(!running);
        ++runCount;
        running = true;
    });
hjk's avatar
hjk committed
58
    connect(runner, &LocalQmlProfilerSupport::localRunnerStopped, this, [this] {
59 60 61
        QVERIFY(running);
        running = false;
    });
hjk's avatar
hjk committed
62
    runControl->initiateStart();
63 64
}

65 66
void LocalQmlProfilerRunnerTest::testRunner()
{
67 68
    debuggee.executable = "\\-/|\\-/";
    debuggee.environment = Utils::Environment::systemEnvironment();
69 70

    // should not be used anywhere but cannot be empty
71
    serverUrl.setPath("invalid");
72

hjk's avatar
hjk committed
73
    start();
74

75 76
    QTimer::singleShot(0, this, &LocalQmlProfilerRunnerTest::testRunner1);
}
77

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

83
    serverUrl = UrlConnection::localSocket();
84

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

hjk's avatar
hjk committed
89 90
    start();

91 92
    QTimer::singleShot(0, this, &LocalQmlProfilerRunnerTest::testRunner2);
}
93

94 95
void LocalQmlProfilerRunnerTest::testRunner2()
{
96 97
    QTRY_COMPARE_WITH_TIMEOUT(runCount, 2, 10000);
    QTRY_VERIFY_WITH_TIMEOUT(!running, 10000);
98

99
    debuggee.commandLineArguments.clear();
100
    serverUrl = UrlConnection::localHostAndFreePort();
hjk's avatar
hjk committed
101 102

    start();
103

104 105 106 107 108
    QTimer::singleShot(0, this, &LocalQmlProfilerRunnerTest::testRunner3);
}

void LocalQmlProfilerRunnerTest::testRunner3()
{
109
    QTRY_COMPARE_WITH_TIMEOUT(runCount, 3, 10000);
hjk's avatar
hjk committed
110
    runControl->initiateStop();
111 112
    QTimer::singleShot(0, this, &LocalQmlProfilerRunnerTest::testRunner4);
}
113

114 115 116
void LocalQmlProfilerRunnerTest::testRunner4()
{
    QTRY_VERIFY_WITH_TIMEOUT(!running, 10000);
hjk's avatar
hjk committed
117
    delete runControl;
118 119 120 121
}

void LocalQmlProfilerRunnerTest::testFindFreePort()
{
122 123 124
    QUrl serverUrl = UrlConnection::localHostAndFreePort();
    QVERIFY(serverUrl.port() != -1);
    QVERIFY(!serverUrl.host().isEmpty());
125
    QTcpServer server;
126
    QVERIFY(server.listen(QHostAddress(serverUrl.host()), serverUrl.port()));
127 128 129 130
}

void LocalQmlProfilerRunnerTest::testFindFreeSocket()
{
131 132
    QUrl serverUrl = UrlConnection::localSocket();
    QString socket = serverUrl.path();
133 134 135 136 137 138 139 140 141
    QVERIFY(!socket.isEmpty());
    QVERIFY(!QFile::exists(socket));
    QFile file(socket);
    QVERIFY(file.open(QIODevice::WriteOnly));
    file.close();
}

} // namespace Internal
} // namespace QmlProfiler