Debugger: Improve output of autotests
[profile/ivi/qtdeclarative.git] / tests / auto / qml / debugger / qqmlinspector / tst_qqmlinspector.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 #include <qtest.h>
42 #include <QSignalSpy>
43 #include <QTimer>
44 #include <QHostAddress>
45 #include <QDebug>
46 #include <QThread>
47
48 #include "../../../../../src/plugins/qmltooling/shared/qqmlinspectorprotocol.h"
49 #include "../shared/debugutil_p.h"
50
51 using namespace QmlJSDebugger;
52
53 #define PORT 13772
54 #define STR_PORT "13772"
55
56 class QQmlInspectorClient : public QQmlDebugClient
57 {
58     Q_OBJECT
59
60 public:
61     QQmlInspectorClient(QQmlDebugConnection *connection)
62         : QQmlDebugClient(QLatin1String("QDeclarativeObserverMode"), connection)
63         , m_showAppOnTop(false)
64     {
65     }
66
67     bool showAppOnTop() const { return m_showAppOnTop; }
68     void setShowAppOnTop(bool showOnTop);
69
70 signals:
71     void showAppOnTopChanged();
72
73 protected:
74     void messageReceived(const QByteArray &message);
75
76 private:
77     bool m_showAppOnTop;
78 };
79
80 class tst_QQmlInspector : public QObject
81 {
82     Q_OBJECT
83
84 public:
85     tst_QQmlInspector()
86         : m_process(0)
87         , m_connection(0)
88         , m_client(0)
89     {
90     }
91
92
93 private:
94     QQmlDebugProcess *m_process;
95     QQmlDebugConnection *m_connection;
96     QQmlInspectorClient *m_client;
97
98 private slots:
99     void initTestCase();
100     void cleanupTestCase();
101     void init();
102     void cleanup();
103
104     void connect();
105     void showAppOnTop();
106 };
107
108
109 void QQmlInspectorClient::setShowAppOnTop(bool showOnTop)
110 {
111     QByteArray message;
112     QDataStream ds(&message, QIODevice::WriteOnly);
113     ds << InspectorProtocol::ShowAppOnTop << showOnTop;
114
115     sendMessage(message);
116 }
117
118 void QQmlInspectorClient::messageReceived(const QByteArray &message)
119 {
120     QDataStream ds(message);
121     InspectorProtocol::Message type;
122     ds >> type;
123
124     switch (type) {
125     case InspectorProtocol::ShowAppOnTop:
126         ds >> m_showAppOnTop;
127         emit showAppOnTopChanged();
128         break;
129     default:
130         qDebug() << "Unhandled message " << (int)type;
131     }
132 }
133
134 void tst_QQmlInspector::initTestCase()
135 {
136 }
137
138 void tst_QQmlInspector::cleanupTestCase()
139 {
140 }
141
142 void tst_QQmlInspector::init()
143 {
144 #if defined(Q_OS_WIN)
145     const QString executable = "app\\app.exe";
146 #else
147     const QString executable = "app/app";
148 #endif
149     const QString argument = "-qmljsdebugger=port:"STR_PORT",block";
150
151     m_process = new QQmlDebugProcess(executable);
152     m_process->start(QStringList() << argument);
153     QVERIFY2(m_process->waitForSessionStart(),
154              "Could not launch application, or did not get 'Waiting for connection'.");
155
156     QQmlDebugConnection *m_connection = new QQmlDebugConnection();
157     m_client = new QQmlInspectorClient(m_connection);
158
159     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
160 }
161
162 void tst_QQmlInspector::cleanup()
163 {
164     if (QTest::currentTestFailed()) {
165         qDebug() << "Process State:" << m_process->state();
166         qDebug() << "Application Output:" << m_process->output();
167     }
168     delete m_process;
169     delete m_connection;
170     delete m_client;
171 }
172
173 void tst_QQmlInspector::connect()
174 {
175     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
176 }
177
178 void tst_QQmlInspector::showAppOnTop()
179 {
180     QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
181
182     m_client->setShowAppOnTop(true);
183     QVERIFY(QQmlDebugTest::waitForSignal(m_client, SIGNAL(showAppOnTopChanged())));
184     QCOMPARE(m_client->showAppOnTop(), true);
185
186     m_client->setShowAppOnTop(false);
187     QVERIFY(QQmlDebugTest::waitForSignal(m_client, SIGNAL(showAppOnTopChanged())));
188     QCOMPARE(m_client->showAppOnTop(), false);
189 }
190
191 QTEST_MAIN(tst_QQmlInspector)
192
193 #include "tst_qqmlinspector.moc"