Debugger: Add autotest skeleton for inspector service
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qdeclarativeinspector / tst_qdeclarativeinspector.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 #include <qtest.h>
42 #include <QSignalSpy>
43 #include <QTimer>
44 #include <QHostAddress>
45 #include <QDebug>
46 #include <QThread>
47
48 #include "../../../../src/plugins/qmltooling/qmldbg_inspector/qdeclarativeinspectorprotocol.h"
49 #include "../../../../shared/util.h"
50 #include "../shared/debugutil_p.h"
51
52 using namespace QmlJSDebugger;
53
54 #define PORT 13772
55 #define STR_PORT "13772"
56
57 class QDeclarativeInspectorClient : public QDeclarativeDebugClient
58 {
59     Q_OBJECT
60
61 public:
62     QDeclarativeInspectorClient(QDeclarativeDebugConnection *connection)
63         : QDeclarativeDebugClient(QLatin1String("QDeclarativeObserverMode"), connection)
64         , m_showAppOnTop(false)
65     {
66     }
67
68     bool showAppOnTop() const { return m_showAppOnTop; }
69     void setShowAppOnTop(bool showOnTop);
70
71 signals:
72     void showAppOnTopChanged();
73
74 protected:
75     void messageReceived(const QByteArray &message);
76
77 private:
78     bool m_showAppOnTop;
79 };
80
81 class tst_QDeclarativeInspector : public QObject
82 {
83     Q_OBJECT
84
85 public:
86     tst_QDeclarativeInspector()
87         : m_process(0)
88         , m_connection(0)
89         , m_client(0)
90     {
91     }
92
93
94 private:
95     QDeclarativeDebugProcess *m_process;
96     QDeclarativeDebugConnection *m_connection;
97     QDeclarativeInspectorClient *m_client;
98
99 private slots:
100     void initTestCase();
101     void cleanupTestCase();
102     void init();
103     void cleanup();
104
105     void connect();
106     void showAppOnTop();
107 };
108
109
110 void QDeclarativeInspectorClient::setShowAppOnTop(bool showOnTop)
111 {
112     QByteArray message;
113     QDataStream ds(&message, QIODevice::WriteOnly);
114     ds << InspectorProtocol::ShowAppOnTop << showOnTop;
115
116     sendMessage(message);
117 }
118
119 void QDeclarativeInspectorClient::messageReceived(const QByteArray &message)
120 {
121     QDataStream ds(message);
122     InspectorProtocol::Message type;
123     ds >> type;
124
125     switch (type) {
126     case InspectorProtocol::ShowAppOnTop:
127         ds >> m_showAppOnTop;
128         emit showAppOnTopChanged();
129         break;
130     default:
131         qDebug() << "Unhandled message " << (int)type;
132     }
133 }
134
135 void tst_QDeclarativeInspector::initTestCase()
136 {
137 }
138
139 void tst_QDeclarativeInspector::cleanupTestCase()
140 {
141 }
142
143 void tst_QDeclarativeInspector::init()
144 {
145     const QString executable = SRCDIR"/app/app";
146     const QString argument = "-qmljsdebugger=port:"STR_PORT",block";
147
148     m_process = new QDeclarativeDebugProcess(executable);
149     m_process->start(QStringList() << argument);
150     if (!m_process->waitForSessionStart()) {
151         QFAIL(QString("Could not launch app '%1'.\nApplication output:\n%2").arg(executable, m_process->output()).toAscii());
152     }
153
154     QDeclarativeDebugConnection *m_connection = new QDeclarativeDebugConnection();
155     m_client = new QDeclarativeInspectorClient(m_connection);
156
157     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
158 }
159
160 void tst_QDeclarativeInspector::cleanup()
161 {
162     delete m_process;
163     delete m_connection;
164     delete m_client;
165 }
166
167 void tst_QDeclarativeInspector::connect()
168 {
169     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
170 }
171
172 void tst_QDeclarativeInspector::showAppOnTop()
173 {
174     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
175
176     m_client->setShowAppOnTop(true);
177     QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(showAppOnTopChanged())));
178     QCOMPARE(m_client->showAppOnTop(), true);
179
180     m_client->setShowAppOnTop(false);
181     QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(showAppOnTopChanged())));
182     QCOMPARE(m_client->showAppOnTop(), false);
183 }
184
185 QTEST_MAIN(tst_QDeclarativeInspector)
186
187 #include "tst_qdeclarativeinspector.moc"