QmlProfiler: Drop static accessors from QmlProfilerTool

It wasn't really a singleton even before. For testing purposes make the
client/state/model managers accessible.

Change-Id: Ie5efbc47a6b9119495f999e4e05877d4789da407
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Ulf Hermann
2018-01-09 14:50:12 +01:00
parent 7d039de7a8
commit e1ad7a1784
7 changed files with 89 additions and 67 deletions

View File

@@ -106,10 +106,17 @@ void QmlProfilerPlugin::extensionsInitialized()
};
RunControl::registerWorkerCreator(ProjectExplorer::Constants::QML_PROFILER_RUN_MODE,
[](RunControl *runControl) { return new QmlProfilerRunner(runControl); });
[this](RunControl *runControl) {
QmlProfilerRunner *runner = new QmlProfilerRunner(runControl);
connect(runner, &QmlProfilerRunner::starting,
m_profilerTool, &QmlProfilerTool::finalizeRunControl);
return runner;
});
RunControl::registerWorker<LocalQmlProfilerSupport>
(ProjectExplorer::Constants::QML_PROFILER_RUN_MODE, constraint);
RunControl::registerWorker(ProjectExplorer::Constants::QML_PROFILER_RUN_MODE,
[this](ProjectExplorer::RunControl *runControl) {
return new LocalQmlProfilerSupport(m_profilerTool, runControl);
}, constraint);
}
ExtensionSystem::IPlugin::ShutdownFlag QmlProfilerPlugin::aboutToShutdown()

View File

@@ -28,8 +28,6 @@
#include "qmlprofilerclientmanager.h"
#include "qmlprofilertool.h"
#include <app/app_version.h>
#include <coreplugin/icore.h>
#include <coreplugin/helpmanager.h>
@@ -57,6 +55,7 @@ using namespace ProjectExplorer;
using namespace QmlProfiler::Internal;
namespace QmlProfiler {
namespace Internal {
static QString QmlServerUrl = "QmlServerUrl";
@@ -92,46 +91,8 @@ QmlProfilerRunner::~QmlProfilerRunner()
void QmlProfilerRunner::start()
{
Internal::QmlProfilerTool::instance()->finalizeRunControl(this);
emit starting(this);
QTC_ASSERT(d->m_profilerState, return);
QUrl serverUrl = this->serverUrl();
QmlProfilerClientManager *clientManager = Internal::QmlProfilerTool::clientManager();
connect(clientManager, &QmlProfilerClientManager::connectionFailed,
this, [this, clientManager] {
QMessageBox *infoBox = new QMessageBox(ICore::mainWindow());
infoBox->setIcon(QMessageBox::Critical);
infoBox->setWindowTitle(Core::Constants::IDE_DISPLAY_NAME);
infoBox->setText(QmlProfilerTool::tr("Could not connect to the in-process QML profiler.\n"
"Do you want to retry?"));
infoBox->setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel | QMessageBox::Help);
infoBox->setDefaultButton(QMessageBox::Retry);
infoBox->setModal(true);
connect(infoBox, &QDialog::finished, this, [clientManager, this](int result) {
switch (result) {
case QMessageBox::Retry:
clientManager->retryConnect();
break;
case QMessageBox::Help:
HelpManager::handleHelpRequest(
"qthelp://org.qt-project.qtcreator/doc/creator-debugging-qml.html");
Q_FALLTHROUGH();
case QMessageBox::Cancel:
// The actual error message has already been logged.
QmlProfilerTool::logState(QmlProfilerTool::tr("Failed to connect."));
cancelProcess();
break;
}
});
infoBox->show();
}, Qt::QueuedConnection); // Queue any connection failures after reportStarted()
clientManager->connectToServer(serverUrl);
d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppRunning);
reportStarted();
}
@@ -261,18 +222,22 @@ static QUrl localServerUrl(RunControl *runControl)
return serverUrl;
}
LocalQmlProfilerSupport::LocalQmlProfilerSupport(RunControl *runControl)
: LocalQmlProfilerSupport(runControl, localServerUrl(runControl))
LocalQmlProfilerSupport::LocalQmlProfilerSupport(QmlProfilerTool *profilerTool,
RunControl *runControl)
: LocalQmlProfilerSupport(profilerTool, runControl, localServerUrl(runControl))
{
}
LocalQmlProfilerSupport::LocalQmlProfilerSupport(RunControl *runControl, const QUrl &serverUrl)
LocalQmlProfilerSupport::LocalQmlProfilerSupport(QmlProfilerTool *profilerTool,
RunControl *runControl, const QUrl &serverUrl)
: SimpleTargetRunner(runControl)
{
setDisplayName("LocalQmlProfilerSupport");
m_profiler = new QmlProfilerRunner(runControl);
m_profiler->setServerUrl(serverUrl);
connect(m_profiler, &QmlProfilerRunner::starting,
profilerTool, &QmlProfilerTool::finalizeRunControl);
addStopDependency(m_profiler);
// We need to open the local server before the application tries to connect.
@@ -301,4 +266,5 @@ LocalQmlProfilerSupport::LocalQmlProfilerSupport(RunControl *runControl, const Q
setRunnable(debuggee);
}
} // namespace Internal
} // namespace QmlProfiler

View File

@@ -36,7 +36,9 @@
#include <qmldebug/qmloutputparser.h>
namespace QmlProfiler {
namespace Internal {
class QmlProfilerTool;
class QmlProfilerRunner : public ProjectExplorer::RunWorker
{
Q_OBJECT
@@ -53,6 +55,9 @@ public:
void cancelProcess();
void notifyRemoteFinished();
signals:
void starting(QmlProfilerRunner *self);
private:
void start() override;
void stop() override;
@@ -68,12 +73,13 @@ class LocalQmlProfilerSupport : public ProjectExplorer::SimpleTargetRunner
Q_OBJECT
public:
LocalQmlProfilerSupport(ProjectExplorer::RunControl *runControl);
LocalQmlProfilerSupport(ProjectExplorer::RunControl *runControl,
LocalQmlProfilerSupport(QmlProfilerTool *profilerTool, ProjectExplorer::RunControl *runControl);
LocalQmlProfilerSupport(QmlProfilerTool *profilerTool, ProjectExplorer::RunControl *runControl,
const QUrl &serverUrl);
private:
QmlProfilerRunner *m_profiler;
};
} // namespace Internal
} // namespace QmlProfiler

View File

@@ -38,6 +38,8 @@
#include "qmlprofilerplugin.h"
#include "qmlprofilertextmark.h"
#include <app/app_version.h>
#include <debugger/debuggericons.h>
#include <debugger/analyzer/analyzermanager.h>
@@ -129,12 +131,9 @@ public:
bool m_toolBusy = false;
};
static QmlProfilerTool *s_instance;
QmlProfilerTool::QmlProfilerTool(QObject *parent)
: QObject(parent), d(new QmlProfilerToolPrivate)
{
s_instance = this;
setObjectName(QLatin1String("QmlProfilerTool"));
d->m_profilerState = new QmlProfilerStateManager(this);
@@ -341,11 +340,6 @@ QmlProfilerTool::~QmlProfilerTool()
delete d;
}
QmlProfilerTool *QmlProfilerTool::instance()
{
return s_instance;
}
void QmlProfilerTool::updateRunActions()
{
if (d->m_toolBusy) {
@@ -396,6 +390,40 @@ void QmlProfilerTool::finalizeRunControl(QmlProfilerRunner *runWorker)
d->m_profilerModelManager->populateFileFinder(runConfiguration ? runConfiguration->target()
: nullptr);
connect(d->m_profilerConnections, &QmlProfilerClientManager::connectionFailed,
runWorker, [this, runWorker]() {
QMessageBox *infoBox = new QMessageBox(ICore::mainWindow());
infoBox->setIcon(QMessageBox::Critical);
infoBox->setWindowTitle(Core::Constants::IDE_DISPLAY_NAME);
infoBox->setText(QmlProfilerTool::tr("Could not connect to the in-process QML profiler.\n"
"Do you want to retry?"));
infoBox->setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel | QMessageBox::Help);
infoBox->setDefaultButton(QMessageBox::Retry);
infoBox->setModal(true);
connect(infoBox, &QDialog::finished, runWorker, [this, runWorker](int result) {
switch (result) {
case QMessageBox::Retry:
d->m_profilerConnections->retryConnect();
break;
case QMessageBox::Help:
HelpManager::handleHelpRequest(
"qthelp://org.qt-project.qtcreator/doc/creator-debugging-qml.html");
Q_FALLTHROUGH();
case QMessageBox::Cancel:
// The actual error message has already been logged.
QmlProfilerTool::logState(QmlProfilerTool::tr("Failed to connect."));
runWorker->cancelProcess();
break;
}
});
infoBox->show();
}, Qt::QueuedConnection); // Queue any connection failures after reportStarted()
d->m_profilerConnections->connectToServer(runWorker->serverUrl());
d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppRunning);
}
void QmlProfilerTool::recordingButtonChanged(bool recording)
@@ -573,6 +601,7 @@ ProjectExplorer::RunControl *QmlProfilerTool::attachToWaitingApplication()
auto runControl = new RunControl(runConfig, ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
auto profiler = new QmlProfilerRunner(runControl);
profiler->setServerUrl(serverUrl);
connect(profiler, &QmlProfilerRunner::starting, this, &QmlProfilerTool::finalizeRunControl);
connect(d->m_profilerConnections, &QmlProfilerClientManager::connectionClosed,
runControl, &RunControl::initiateStop);
@@ -819,7 +848,17 @@ void QmlProfilerTool::showNonmodalWarning(const QString &warningMsg)
QmlProfilerClientManager *QmlProfilerTool::clientManager()
{
return s_instance->d->m_profilerConnections;
return d->m_profilerConnections;
}
QmlProfilerModelManager *QmlProfilerTool::modelManager()
{
return d->m_profilerModelManager;
}
QmlProfilerStateManager *QmlProfilerTool::stateManager()
{
return d->m_profilerState;
}
void QmlProfilerTool::profilerStateChanged()

View File

@@ -40,10 +40,12 @@ class RunControl;
namespace QmlProfiler {
class QmlProfilerRunner;
class QmlProfilerModelManager;
class QmlProfilerStateManager;
namespace Internal {
class QmlProfilerRunner;
class QmlProfilerClientManager;
class QMLPROFILER_EXPORT QmlProfilerTool : public QObject
@@ -54,8 +56,6 @@ public:
explicit QmlProfilerTool(QObject *parent);
~QmlProfilerTool();
static QmlProfilerTool *instance();
void finalizeRunControl(QmlProfilerRunner *runWorker);
bool prepareTool();
@@ -68,7 +68,9 @@ public:
static void logError(const QString &msg);
static void showNonmodalWarning(const QString &warningMsg);
static QmlProfilerClientManager *clientManager();
QmlProfilerClientManager *clientManager();
QmlProfilerModelManager *modelManager();
QmlProfilerStateManager *stateManager();
void profilerStateChanged();
void serverRecordingChanged();

View File

@@ -28,6 +28,7 @@
#include <debugger/analyzer/analyzermanager.h>
#include <projectexplorer/runnables.h>
#include <qmlprofiler/qmlprofilerruncontrol.h>
#include <qmlprofiler/qmlprofilertool.h>
#include <utils/url.h>
@@ -43,6 +44,7 @@ LocalQmlProfilerRunnerTest::LocalQmlProfilerRunnerTest(QObject *parent) : QObjec
void LocalQmlProfilerRunnerTest::testRunner()
{
QmlProfilerTool tool(nullptr);
QPointer<ProjectExplorer::RunControl> runControl;
QPointer<LocalQmlProfilerSupport> profiler;
ProjectExplorer::StandardRunnable debuggee;
@@ -64,7 +66,7 @@ void LocalQmlProfilerRunnerTest::testRunner()
runControl = new ProjectExplorer::RunControl(nullptr,
ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
runControl->setRunnable(debuggee);
profiler = new LocalQmlProfilerSupport(runControl, serverUrl);
profiler = new LocalQmlProfilerSupport(&tool, runControl, serverUrl);
auto connectRunner = [&]() {
connect(runControl, &ProjectExplorer::RunControl::aboutToStart, this, [&]() {
@@ -112,7 +114,7 @@ void LocalQmlProfilerRunnerTest::testRunner()
runControl = new ProjectExplorer::RunControl(nullptr,
ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
runControl->setRunnable(debuggee);
profiler = new LocalQmlProfilerSupport(runControl, serverUrl);
profiler = new LocalQmlProfilerSupport(&tool, runControl, serverUrl);
connectRunner();
runControl->initiateStart();
@@ -132,7 +134,7 @@ void LocalQmlProfilerRunnerTest::testRunner()
runControl = new ProjectExplorer::RunControl(nullptr,
ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
runControl->setRunnable(debuggee);
profiler = new LocalQmlProfilerSupport(runControl, serverUrl);
profiler = new LocalQmlProfilerSupport(&tool, runControl, serverUrl);
connectRunner();
runControl->initiateStart();

View File

@@ -39,7 +39,7 @@ namespace Internal {
void QmlProfilerToolTest::testAttachToWaitingApplication()
{
QmlProfilerTool *profilerTool = QmlProfilerTool::instance();
QmlProfilerTool profilerTool(nullptr);
QTcpServer server;
QUrl serverUrl = Utils::urlFromLocalHostAndFreePort();
QVERIFY(serverUrl.port() >= 0);
@@ -63,7 +63,7 @@ void QmlProfilerToolTest::testAttachToWaitingApplication()
});
timer.start();
ProjectExplorer::RunControl *runControl = profilerTool->attachToWaitingApplication();
ProjectExplorer::RunControl *runControl = profilerTool.attachToWaitingApplication();
QVERIFY(runControl);
QTRY_VERIFY(connection);