a3d3dce644a0dee9795668c8403f9390066b32bc
[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 #include <private/qqmldebugclient_p.h>
48 #include <private/qqmldebugservice_p.h>
49
50 bool QQmlDebugTest::waitForSignal(QObject *receiver, const char *member, int timeout) {
51     QEventLoop loop;
52     QTimer timer;
53     timer.setSingleShot(true);
54     QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
55     QObject::connect(receiver, member, &loop, SLOT(quit()));
56     timer.start(timeout);
57     loop.exec();
58     return timer.isActive();
59 }
60
61 QQmlDebugTestService::QQmlDebugTestService(const QString &s, float version, QObject *parent)
62     : QQmlDebugService(s, version, parent)
63 {
64     registerService();
65 }
66
67 void QQmlDebugTestService::messageReceived(const QByteArray &ba)
68 {
69     sendMessage(ba);
70 }
71
72 void QQmlDebugTestService::stateChanged(State)
73 {
74     emit stateHasChanged();
75 }
76
77
78 QQmlDebugTestClient::QQmlDebugTestClient(const QString &s, QQmlDebugConnection *c)
79     : QQmlDebugClient(s, c)
80 {
81 }
82
83 QByteArray QQmlDebugTestClient::waitForResponse()
84 {
85     lastMsg.clear();
86     QQmlDebugTest::waitForSignal(this, SIGNAL(serverMessage(QByteArray)));
87     if (lastMsg.isEmpty()) {
88         qWarning() << "tst_QQmlDebugTestClient: no response from server!";
89         return QByteArray();
90     }
91     return lastMsg;
92 }
93
94 void QQmlDebugTestClient::stateChanged(State stat)
95 {
96     QCOMPARE(stat, state());
97     emit stateHasChanged();
98 }
99
100 void QQmlDebugTestClient::messageReceived(const QByteArray &ba)
101 {
102     lastMsg = ba;
103     emit serverMessage(ba);
104 }
105
106 QQmlDebugProcess::QQmlDebugProcess(const QString &executable)
107     : m_executable(executable)
108     , m_started(false)
109 {
110     m_process.setProcessChannelMode(QProcess::MergedChannels);
111     m_timer.setSingleShot(true);
112     m_timer.setInterval(5000);
113     connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(processAppOutput()));
114     connect(&m_timer, SIGNAL(timeout()), &m_eventLoop, SLOT(quit()));
115 }
116
117 QQmlDebugProcess::~QQmlDebugProcess()
118 {
119     stop();
120 }
121
122 void QQmlDebugProcess::start(const QStringList &arguments)
123 {
124     m_mutex.lock();
125     m_process.setEnvironment(m_environment);
126     m_process.start(m_executable, arguments);
127     m_process.waitForStarted();
128     m_timer.start();
129     m_mutex.unlock();
130 }
131
132 void QQmlDebugProcess::stop()
133 {
134     if (m_process.state() != QProcess::NotRunning) {
135         m_process.kill();
136         m_process.waitForFinished(5000);
137     }
138 }
139
140 bool QQmlDebugProcess::waitForSessionStart()
141 {
142     if (m_process.state() != QProcess::Running) {
143         qWarning() << "Could not start up " << m_executable;
144         return false;
145     }
146     m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
147
148     return m_started;
149 }
150
151 void QQmlDebugProcess::setEnvironment(const QStringList &environment)
152 {
153     m_environment = environment;
154 }
155
156 QString QQmlDebugProcess::output() const
157 {
158     return m_output;
159 }
160
161 void QQmlDebugProcess::processAppOutput()
162 {
163     m_mutex.lock();
164
165     QString newOutput = m_process.readAll();
166     m_output.append(newOutput);
167     m_outputBuffer.append(newOutput);
168
169     while (true) {
170         const int nlIndex = m_outputBuffer.indexOf(QLatin1Char('\n'));
171         if (nlIndex < 0) // no further complete lines
172             break;
173         const QString line = m_outputBuffer.left(nlIndex);
174         m_outputBuffer = m_outputBuffer.right(m_outputBuffer.size() - nlIndex - 1);
175
176         if (line.startsWith("QML debugging is enabled")) // ignore
177             continue;
178         if (line.startsWith("QML Debugger:")) {
179             if (line.contains("Waiting for connection ")) {
180                 m_started = true;
181                 m_eventLoop.quit();
182                 continue;
183             }
184             if (line.contains("Connection established.")) {
185                 continue;
186             }
187         }
188     }
189     m_mutex.unlock();
190 }