1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtCore/qjsondocument.h>
30#include <QtCore/qjsonobject.h>
31#include <QtCore/qtimer.h>
32#include <QtTest/qtest.h>
33#include <QtQml/qqmlapplicationengine.h>
34#include <QtQml/qqmlcomponent.h>
35
36#include <private/qhooks_p.h>
37
38const char testData[] =
39"import QtQuick 2.0\n"
40"Item {\n"
41" id: item\n"
42" property int a: 0\n"
43" Timer {\n"
44" id: timer; interval: 1; repeat: true; running: true\n"
45" onTriggered: {\n"
46" a++\n"
47" }\n"
48" }\n"
49"}\n";
50
51struct ResolvedHooks
52{
53 quintptr version;
54 quintptr numEntries;
55 const char **qt_qmlDebugMessageBuffer;
56 int *qt_qmlDebugMessageLength;
57 bool (*qt_qmlDebugSendDataToService)(const char *serviceName, const char *hexData);
58 bool (*qt_qmlDebugEnableService)(const char *data);
59 bool (*qt_qmlDebugDisableService)(const char *data);
60 void (*qt_qmlDebugObjectAvailable)();
61} *hooks;
62
63class Application : public QGuiApplication
64{
65 Q_OBJECT
66public:
67 Application(int &argc, char **argv)
68 : QGuiApplication(argc, argv),
69 component(&engine)
70 {
71 component.setData(testData, baseUrl: QUrl("MyStuff"));
72 mainObject = component.create();
73 }
74
75private slots:
76 void testEcho()
77 {
78 QJsonObject request;
79 QJsonObject arguments;
80 arguments.insert(key: QLatin1String("test"), value: QLatin1String("BUH"));
81 request.insert(key: QLatin1String("command"), value: QLatin1String("echo"));
82 request.insert(key: QLatin1String("arguments"), value: arguments);
83 QJsonDocument doc;
84 doc.setObject(request);
85 QByteArray hexdata = doc.toJson(format: QJsonDocument::Compact).toHex();
86
87 hooks = (ResolvedHooks *)qtHookData[QHooks::Startup];
88 QCOMPARE(bool(hooks), true); // Available after connector start only.
89 QCOMPARE(hooks->version, quintptr(1));
90 QCOMPARE(hooks->numEntries, quintptr(6));
91 QCOMPARE(bool(hooks->qt_qmlDebugSendDataToService), true);
92 QCOMPARE(bool(hooks->qt_qmlDebugMessageBuffer), true);
93 QCOMPARE(bool(hooks->qt_qmlDebugMessageLength), true);
94 QCOMPARE(bool(hooks->qt_qmlDebugEnableService), true);
95
96 hooks->qt_qmlDebugEnableService("NativeQmlDebugger");
97 hooks->qt_qmlDebugSendDataToService("NativeQmlDebugger", hexdata);
98 QByteArray response(*hooks->qt_qmlDebugMessageBuffer, *hooks->qt_qmlDebugMessageLength);
99
100 QCOMPARE(response, QByteArray("NativeQmlDebugger 25 {\"result\":{\"test\":\"BUH\"}}"));
101 }
102
103private:
104 QQmlApplicationEngine engine;
105 QQmlComponent component;
106 QObject *mainObject;
107};
108
109int main(int argc, char *argv[])
110{
111 char **argv2 = new char *[argc + 2];
112 for (int i = 0; i < argc; ++i)
113 argv2[i] = argv[i];
114 argv2[argc] = strdup(s: "-qmljsdebugger=native,services:NativeQmlDebugger");
115 ++argc;
116 argv2[argc] = nullptr;
117 Application app(argc, argv2);
118 return QTest::qExec(testObject: &app, argc, argv);
119}
120
121#include "tst_qqmlnativeconnector.moc"
122

source code of qtdeclarative/tests/auto/qml/debugger/qqmlnativeconnector/tst_qqmlnativeconnector.cpp