1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "debugutil_p.h"
47 bool QQmlDebugTest::waitForSignal(QObject *receiver, const char *member, int timeout) {
50 timer.setSingleShot(true);
51 QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
52 QObject::connect(receiver, member, &loop, SLOT(quit()));
55 return timer.isActive();
58 QQmlDebugTestClient::QQmlDebugTestClient(const QString &s, QQmlDebugConnection *c)
59 : QQmlDebugClient(s, c)
63 QByteArray QQmlDebugTestClient::waitForResponse()
66 QQmlDebugTest::waitForSignal(this, SIGNAL(serverMessage(QByteArray)));
67 if (lastMsg.isEmpty()) {
68 qWarning() << "no response from server!";
74 void QQmlDebugTestClient::stateChanged(State stat)
76 QCOMPARE(stat, state());
77 emit stateHasChanged();
80 void QQmlDebugTestClient::messageReceived(const QByteArray &ba)
83 emit serverMessage(ba);
86 QQmlDebugProcess::QQmlDebugProcess(const QString &executable)
87 : m_executable(executable)
90 m_process.setProcessChannelMode(QProcess::MergedChannels);
91 m_timer.setSingleShot(true);
92 m_timer.setInterval(5000);
93 connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(processAppOutput()));
94 connect(&m_timer, SIGNAL(timeout()), SLOT(timeout()));
97 QQmlDebugProcess::~QQmlDebugProcess()
102 QString QQmlDebugProcess::state()
105 switch (m_process.state()) {
106 case QProcess::NotRunning: {
107 stateStr = "not running";
108 if (m_process.exitStatus() == QProcess::CrashExit)
109 stateStr += " (crashed!)";
111 stateStr += ", return value" + m_process.exitCode();
114 case QProcess::Starting: stateStr = "starting"; break;
115 case QProcess::Running: stateStr = "running"; break;
120 void QQmlDebugProcess::start(const QStringList &arguments)
123 m_process.setEnvironment(m_environment);
124 m_process.start(m_executable, arguments);
125 if (!m_process.waitForStarted()) {
126 qWarning() << "QML Debug Client: Could not launch app " << m_executable
127 << ": " << m_process.errorString();
135 void QQmlDebugProcess::stop()
137 if (m_process.state() != QProcess::NotRunning) {
139 m_process.waitForFinished(5000);
143 void QQmlDebugProcess::timeout()
145 qWarning() << "Timeout while waiting for QML debugging messages "
146 "in application output. Process is in state" << m_process.state() << ".";
150 bool QQmlDebugProcess::waitForSessionStart()
152 if (m_process.state() != QProcess::Running) {
153 qWarning() << "Could not start up " << m_executable;
161 void QQmlDebugProcess::setEnvironment(const QStringList &environment)
163 m_environment = environment;
166 QString QQmlDebugProcess::output() const
171 void QQmlDebugProcess::processAppOutput()
175 QString newOutput = m_process.readAll();
176 m_output.append(newOutput);
177 m_outputBuffer.append(newOutput);
180 const int nlIndex = m_outputBuffer.indexOf(QLatin1Char('\n'));
181 if (nlIndex < 0) // no further complete lines
183 const QString line = m_outputBuffer.left(nlIndex);
184 m_outputBuffer = m_outputBuffer.right(m_outputBuffer.size() - nlIndex - 1);
186 if (line.startsWith("QML Debugger:")) {
187 if (line.contains("Waiting for connection ")) {
193 if (line.contains("Unable to listen")) {
194 qWarning() << "App was unable to bind to port!";