Debugger: Improve output of autotests
[profile/ivi/qtdeclarative.git] / tests / auto / qml / debugger / shared / debugutil.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "debugutil_p.h"
43
44 #include <QEventLoop>
45 #include <QTimer>
46
47 bool QQmlDebugTest::waitForSignal(QObject *receiver, const char *member, int timeout) {
48     QEventLoop loop;
49     QTimer timer;
50     timer.setSingleShot(true);
51     QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
52     QObject::connect(receiver, member, &loop, SLOT(quit()));
53     timer.start(timeout);
54     loop.exec();
55     return timer.isActive();
56 }
57
58 QQmlDebugTestClient::QQmlDebugTestClient(const QString &s, QQmlDebugConnection *c)
59     : QQmlDebugClient(s, c)
60 {
61 }
62
63 QByteArray QQmlDebugTestClient::waitForResponse()
64 {
65     lastMsg.clear();
66     QQmlDebugTest::waitForSignal(this, SIGNAL(serverMessage(QByteArray)));
67     if (lastMsg.isEmpty()) {
68         qWarning() << "no response from server!";
69         return QByteArray();
70     }
71     return lastMsg;
72 }
73
74 void QQmlDebugTestClient::stateChanged(State stat)
75 {
76     QCOMPARE(stat, state());
77     emit stateHasChanged();
78 }
79
80 void QQmlDebugTestClient::messageReceived(const QByteArray &ba)
81 {
82     lastMsg = ba;
83     emit serverMessage(ba);
84 }
85
86 QQmlDebugProcess::QQmlDebugProcess(const QString &executable)
87     : m_executable(executable)
88     , m_started(false)
89 {
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()));
95 }
96
97 QQmlDebugProcess::~QQmlDebugProcess()
98 {
99     stop();
100 }
101
102 QString QQmlDebugProcess::state()
103 {
104     QString stateStr;
105     switch (m_process.state()) {
106     case QProcess::NotRunning: {
107         stateStr = "not running";
108         if (m_process.exitStatus() == QProcess::CrashExit)
109             stateStr += " (crashed!)";
110         else
111             stateStr += ", return value" + m_process.exitCode();
112         break;
113     }
114     case QProcess::Starting: stateStr = "starting"; break;
115     case QProcess::Running: stateStr = "running"; break;
116     }
117     return stateStr;
118 }
119
120 void QQmlDebugProcess::start(const QStringList &arguments)
121 {
122     m_mutex.lock();
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();
128         m_eventLoop.quit();
129     } else {
130         m_timer.start();
131     }
132     m_mutex.unlock();
133 }
134
135 void QQmlDebugProcess::stop()
136 {
137     if (m_process.state() != QProcess::NotRunning) {
138         m_process.kill();
139         m_process.waitForFinished(5000);
140     }
141 }
142
143 void QQmlDebugProcess::timeout()
144 {
145     qWarning() << "Timeout while waiting for QML debugging messages "
146                   "in application output. Process is in state" << m_process.state() << ".";
147     m_eventLoop.quit();
148 }
149
150 bool QQmlDebugProcess::waitForSessionStart()
151 {
152     if (m_process.state() != QProcess::Running) {
153         qWarning() << "Could not start up " << m_executable;
154         return false;
155     }
156     m_eventLoop.exec();
157
158     return m_started;
159 }
160
161 void QQmlDebugProcess::setEnvironment(const QStringList &environment)
162 {
163     m_environment = environment;
164 }
165
166 QString QQmlDebugProcess::output() const
167 {
168     return m_output;
169 }
170
171 void QQmlDebugProcess::processAppOutput()
172 {
173     m_mutex.lock();
174
175     QString newOutput = m_process.readAll();
176     m_output.append(newOutput);
177     m_outputBuffer.append(newOutput);
178
179     while (true) {
180         const int nlIndex = m_outputBuffer.indexOf(QLatin1Char('\n'));
181         if (nlIndex < 0) // no further complete lines
182             break;
183         const QString line = m_outputBuffer.left(nlIndex);
184         m_outputBuffer = m_outputBuffer.right(m_outputBuffer.size() - nlIndex - 1);
185
186         if (line.startsWith("QML Debugger:")) {
187             if (line.contains("Waiting for connection ")) {
188                 m_timer.stop();
189                 m_started = true;
190                 m_eventLoop.quit();
191                 continue;
192             }
193             if (line.contains("Unable to listen")) {
194                 qWarning() << "App was unable to bind to port!";
195                 m_timer.stop();
196                 m_eventLoop.quit();
197                 continue;
198             }
199         }
200     }
201     m_mutex.unlock();
202 }