Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / debugger / qdeclarativeinspector / tst_qdeclarativeinspector.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/qdeclarativeinspectorprotocol.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 QDeclarativeInspectorClient : public QDeclarativeDebugClient
57 {
58     Q_OBJECT
59
60 public:
61     QDeclarativeInspectorClient(QDeclarativeDebugConnection *connection)
62         : QDeclarativeDebugClient(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_QDeclarativeInspector : public QObject
81 {
82     Q_OBJECT
83
84 public:
85     tst_QDeclarativeInspector()
86         : m_process(0)
87         , m_connection(0)
88         , m_client(0)
89     {
90     }
91
92
93 private:
94     QDeclarativeDebugProcess *m_process;
95     QDeclarativeDebugConnection *m_connection;
96     QDeclarativeInspectorClient *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 QDeclarativeInspectorClient::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 QDeclarativeInspectorClient::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_QDeclarativeInspector::initTestCase()
135 {
136 }
137
138 void tst_QDeclarativeInspector::cleanupTestCase()
139 {
140 }
141
142 void tst_QDeclarativeInspector::init()
143 {
144     const QString executable = SRCDIR"/app/app";
145     const QString argument = "-qmljsdebugger=port:"STR_PORT",block";
146
147     m_process = new QDeclarativeDebugProcess(executable);
148     m_process->start(QStringList() << argument);
149     if (!m_process->waitForSessionStart()) {
150         QFAIL(QString("Could not launch app '%1'.\nApplication output:\n%2").arg(executable, m_process->output()).toAscii());
151     }
152
153     QDeclarativeDebugConnection *m_connection = new QDeclarativeDebugConnection();
154     m_client = new QDeclarativeInspectorClient(m_connection);
155
156     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
157 }
158
159 void tst_QDeclarativeInspector::cleanup()
160 {
161     delete m_process;
162     delete m_connection;
163     delete m_client;
164 }
165
166 void tst_QDeclarativeInspector::connect()
167 {
168     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
169 }
170
171 void tst_QDeclarativeInspector::showAppOnTop()
172 {
173     QTRY_COMPARE(m_client->status(), QDeclarativeDebugClient::Enabled);
174
175     m_client->setShowAppOnTop(true);
176     QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(showAppOnTopChanged())));
177     QCOMPARE(m_client->showAppOnTop(), true);
178
179     m_client->setShowAppOnTop(false);
180     QVERIFY(QDeclarativeDebugTest::waitForSignal(m_client, SIGNAL(showAppOnTopChanged())));
181     QCOMPARE(m_client->showAppOnTop(), false);
182 }
183
184 QTEST_MAIN(tst_QDeclarativeInspector)
185
186 #include "tst_qdeclarativeinspector.moc"