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