eea777f09d73b59cbb9c10f1114df596ba770d00
[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 void QQmlDebugProcess::start(const QStringList &arguments)
103 {
104     m_mutex.lock();
105     m_process.setEnvironment(m_environment);
106     m_process.start(m_executable, arguments);
107     m_process.waitForStarted();
108     m_timer.start();
109     m_mutex.unlock();
110 }
111
112 void QQmlDebugProcess::stop()
113 {
114     if (m_process.state() != QProcess::NotRunning) {
115         m_process.kill();
116         m_process.waitForFinished(5000);
117     }
118 }
119
120 void QQmlDebugProcess::timeout()
121 {
122     qWarning() << "Timeout while waiting for QML debugging messages "
123                   "in application output";
124     m_eventLoop.quit();
125 }
126
127 bool QQmlDebugProcess::waitForSessionStart()
128 {
129     if (m_process.state() != QProcess::Running) {
130         qWarning() << "Could not start up " << m_executable;
131         return false;
132     }
133     m_eventLoop.exec();
134
135     return m_started;
136 }
137
138 void QQmlDebugProcess::setEnvironment(const QStringList &environment)
139 {
140     m_environment = environment;
141 }
142
143 QString QQmlDebugProcess::output() const
144 {
145     return m_output;
146 }
147
148 void QQmlDebugProcess::processAppOutput()
149 {
150     m_mutex.lock();
151
152     QString newOutput = m_process.readAll();
153     m_output.append(newOutput);
154     m_outputBuffer.append(newOutput);
155
156     while (true) {
157         const int nlIndex = m_outputBuffer.indexOf(QLatin1Char('\n'));
158         if (nlIndex < 0) // no further complete lines
159             break;
160         const QString line = m_outputBuffer.left(nlIndex);
161         m_outputBuffer = m_outputBuffer.right(m_outputBuffer.size() - nlIndex - 1);
162
163         if (line.startsWith("QML Debugger:")) {
164             if (line.contains("Waiting for connection ")) {
165                 m_started = true;
166                 m_eventLoop.quit();
167                 continue;
168             }
169             if (line.contains("Unable to listen")) {
170                 qWarning() << "App was unable to bind to port!";
171                 m_eventLoop.quit();
172                 continue;
173             }
174         }
175     }
176     m_mutex.unlock();
177 }