a8c50748e23afe3e6e7712196506f0d9640c871b
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / shared / debugutil.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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/qdeclarativedebugclient_p.h>
48 #include <private/qdeclarativedebugservice_p.h>
49
50 bool QDeclarativeDebugTest::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 QDeclarativeDebugTestService::QDeclarativeDebugTestService(const QString &s, float version, QObject *parent)
62     : QDeclarativeDebugService(s, version, parent)
63 {
64     registerService();
65 }
66
67 void QDeclarativeDebugTestService::messageReceived(const QByteArray &ba)
68 {
69     sendMessage(ba);
70 }
71
72 void QDeclarativeDebugTestService::statusChanged(Status)
73 {
74     emit statusHasChanged();
75 }
76
77
78 QDeclarativeDebugTestClient::QDeclarativeDebugTestClient(const QString &s, QDeclarativeDebugConnection *c)
79     : QDeclarativeDebugClient(s, c)
80 {
81 }
82
83 QByteArray QDeclarativeDebugTestClient::waitForResponse()
84 {
85     lastMsg.clear();
86     QDeclarativeDebugTest::waitForSignal(this, SIGNAL(serverMessage(QByteArray)));
87     if (lastMsg.isEmpty()) {
88         qWarning() << "tst_QDeclarativeDebugClient: no response from server!";
89         return QByteArray();
90     }
91     return lastMsg;
92 }
93
94 void QDeclarativeDebugTestClient::statusChanged(Status stat)
95 {
96     QCOMPARE(stat, status());
97     emit statusHasChanged();
98 }
99
100 void QDeclarativeDebugTestClient::messageReceived(const QByteArray &ba)
101 {
102     lastMsg = ba;
103     emit serverMessage(ba);
104 }
105
106 QDeclarativeDebugProcess::QDeclarativeDebugProcess(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 QDeclarativeDebugProcess::~QDeclarativeDebugProcess()
118 {
119     stop();
120 }
121
122 void QDeclarativeDebugProcess::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 QDeclarativeDebugProcess::stop()
133 {
134     if (m_process.state() != QProcess::NotRunning) {
135         m_process.kill();
136         m_process.waitForFinished(5000);
137     }
138 }
139
140 bool QDeclarativeDebugProcess::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 QDeclarativeDebugProcess::setEnvironment(const QStringList &environment)
152 {
153     m_environment = environment;
154 }
155
156 QString QDeclarativeDebugProcess::output() const
157 {
158     return m_output;
159 }
160
161 void QDeclarativeDebugProcess::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("QDeclarativeDebugServer:")) {
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 }