Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / tests / auto / qml / debugger / qqmlenginedebuginspectorintegrationtest / tst_qqmlenginedebuginspectorintegration.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QSignalSpy>
44 #include <QTimer>
45 #include <QHostAddress>
46 #include <QDebug>
47 #include <QThread>
48 #include <QtCore/QLibraryInfo>
49
50 #include "../shared/debugutil_p.h"
51 #include "../../../shared/util.h"
52 #include "qqmlinspectorclient.h"
53 #include "qqmlenginedebugclient.h"
54
55 #define PORT 3776
56 #define STR_PORT "3776"
57
58 class tst_QQmlEngineDebugInspectorIntegration : public QQmlDataTest
59 {
60     Q_OBJECT
61
62 public:
63     tst_QQmlEngineDebugInspectorIntegration()
64         : m_process(0)
65         , m_connection(0)
66         , m_inspectorClient(0)
67         , m_engineDebugClient(0)
68     {
69     }
70
71
72 private:
73     QmlDebugObjectReference findRootObject();
74
75     QQmlDebugProcess *m_process;
76     QQmlDebugConnection *m_connection;
77     QQmlInspectorClient *m_inspectorClient;
78     QQmlEngineDebugClient *m_engineDebugClient;
79
80 private slots:
81     void init();
82     void cleanup();
83
84     void connect();
85     void clearObjectReferenceHashonReloadQml();
86 };
87
88
89 QmlDebugObjectReference tst_QQmlEngineDebugInspectorIntegration::findRootObject()
90 {
91     bool success = false;
92     m_engineDebugClient->queryAvailableEngines(&success);
93
94     QQmlDebugTest::waitForSignal(m_engineDebugClient, SIGNAL(result()));
95
96     m_engineDebugClient->queryRootContexts(m_engineDebugClient->engines()[0].debugId, &success);
97     QQmlDebugTest::waitForSignal(m_engineDebugClient, SIGNAL(result()));
98     int count = m_engineDebugClient->rootContext().contexts.count();
99     m_engineDebugClient->queryObject(
100                 m_engineDebugClient->rootContext().contexts[count - 1].objects[0], &success);
101     QQmlDebugTest::waitForSignal(m_engineDebugClient, SIGNAL(result()));
102     return m_engineDebugClient->object();
103 }
104
105
106 void tst_QQmlEngineDebugInspectorIntegration::init()
107 {
108     const QString argument = "-qmljsdebugger=port:" STR_PORT ",block";
109
110     m_process = new QQmlDebugProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath)
111                                      + "/qmlscene");
112     m_process->start(QStringList() << argument << testFile("qtquick2.qml"));
113     QVERIFY2(m_process->waitForSessionStart(),
114              "Could not launch application, or did not get 'Waiting for connection'.");
115
116     QQmlDebugConnection *m_connection = new QQmlDebugConnection(this);
117     m_inspectorClient = new QQmlInspectorClient(m_connection);
118     m_engineDebugClient = new QQmlEngineDebugClient(m_connection);
119
120     m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
121     bool ok = m_connection->waitForConnected();
122     QVERIFY(ok);
123 }
124
125 void tst_QQmlEngineDebugInspectorIntegration::cleanup()
126 {
127     if (QTest::currentTestFailed()) {
128         qDebug() << "Process State:" << m_process->state();
129         qDebug() << "Application Output:" << m_process->output();
130     }
131     delete m_process;
132     delete m_engineDebugClient;
133     delete m_inspectorClient;
134 }
135
136 void tst_QQmlEngineDebugInspectorIntegration::connect()
137 {
138     QTRY_COMPARE(m_inspectorClient->state(), QQmlDebugClient::Enabled);
139     QTRY_COMPARE(m_engineDebugClient->state(), QQmlDebugClient::Enabled);
140 }
141
142 void tst_QQmlEngineDebugInspectorIntegration::clearObjectReferenceHashonReloadQml()
143 {
144     QTRY_COMPARE(m_engineDebugClient->state(), QQmlDebugClient::Enabled);
145     bool success = false;
146     QmlDebugObjectReference rootObject = findRootObject();
147     const QString fileName = QFileInfo(rootObject.source.url.toString()).fileName();
148     int lineNumber = rootObject.source.lineNumber;
149     int columnNumber = rootObject.source.columnNumber;
150     m_engineDebugClient->queryObjectsForLocation(fileName, lineNumber,
151                                         columnNumber, &success);
152     QVERIFY(success);
153     QVERIFY(QQmlDebugTest::waitForSignal(m_engineDebugClient, SIGNAL(result())));
154
155     foreach (QmlDebugObjectReference child, rootObject.children) {
156         success = false;
157         lineNumber = child.source.lineNumber;
158         columnNumber = child.source.columnNumber;
159         m_engineDebugClient->queryObjectsForLocation(fileName, lineNumber,
160                                        columnNumber, &success);
161         QVERIFY(success);
162         QVERIFY(QQmlDebugTest::waitForSignal(m_engineDebugClient, SIGNAL(result())));
163     }
164
165     QTRY_COMPARE(m_inspectorClient->state(), QQmlDebugClient::Enabled);
166
167     QByteArray contents;
168     contents.append("import QtQuick 2.0\n"
169                "Text {"
170                "y: 10\n"
171                "text: \"test\"\n"
172                "}");
173
174     QHash<QString, QByteArray> changesHash;
175     changesHash.insert("test.qml", contents);
176     m_inspectorClient->reloadQml(changesHash);
177     QVERIFY(QQmlDebugTest::waitForSignal(m_inspectorClient, SIGNAL(responseReceived())));
178
179     lineNumber = rootObject.source.lineNumber;
180     columnNumber = rootObject.source.columnNumber;
181     success = false;
182     m_engineDebugClient->queryObjectsForLocation(fileName, lineNumber,
183                                    columnNumber, &success);
184     QVERIFY(success);
185     QVERIFY(QQmlDebugTest::waitForSignal(m_engineDebugClient, SIGNAL(result())));
186
187     foreach (QmlDebugObjectReference child, rootObject.children) {
188         success = false;
189         lineNumber = child.source.lineNumber;
190         columnNumber = child.source.columnNumber;
191         m_engineDebugClient->queryObjectsForLocation(fileName, lineNumber,
192                                        columnNumber, &success);
193         QVERIFY(success);
194         QVERIFY(QQmlDebugTest::waitForSignal(m_engineDebugClient, SIGNAL(result())));
195     }
196 }
197
198 QTEST_MAIN(tst_QQmlEngineDebugInspectorIntegration)
199
200 #include "tst_qqmlenginedebuginspectorintegration.moc"