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