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 "debugutil_p.h"
30#include "../../../shared/util.h"
31
32#include <private/qqmldebugclient_p.h>
33#include <private/qqmldebugconnection_p.h>
34#include <private/qpacket_p.h>
35#include <private/qqmlenginecontrolclient_p.h>
36
37#include <QtTest/qtest.h>
38#include <QtCore/qlibraryinfo.h>
39
40class QQmlEngineBlocker : public QObject
41{
42 Q_OBJECT
43public:
44 QQmlEngineBlocker(QQmlEngineControlClient *parent);
45
46public slots:
47 void blockEngine(int engineId, const QString &name);
48};
49
50QQmlEngineBlocker::QQmlEngineBlocker(QQmlEngineControlClient *parent): QObject(parent)
51{
52 connect(sender: parent, signal: &QQmlEngineControlClient::engineAboutToBeAdded,
53 receiver: this, slot: &QQmlEngineBlocker::blockEngine);
54 connect(sender: parent, signal: &QQmlEngineControlClient::engineAboutToBeRemoved,
55 receiver: this, slot: &QQmlEngineBlocker::blockEngine);
56}
57
58void QQmlEngineBlocker::blockEngine(int engineId, const QString &name)
59{
60 Q_UNUSED(name);
61 static_cast<QQmlEngineControlClient *>(parent())->blockEngine(engineId);
62}
63
64class tst_QQmlEngineControl : public QQmlDebugTest
65{
66 Q_OBJECT
67
68private:
69 ConnectResult connectTo(const QString &testFile, bool restrictServices);
70 QList<QQmlDebugClient *> createClients() override;
71
72 void engine_data();
73 QPointer<QQmlEngineControlClient> m_client;
74
75private slots:
76 void startEngine_data();
77 void startEngine();
78 void stopEngine_data();
79 void stopEngine();
80};
81
82QQmlDebugTest::ConnectResult tst_QQmlEngineControl::connectTo(const QString &file,
83 bool restrictServices)
84{
85 return QQmlDebugTest::connectTo(executable: QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene",
86 services: restrictServices ? QStringLiteral("EngineControl") : QString(),
87 extraArgs: testFile(fileName: file), block: true);
88}
89
90QList<QQmlDebugClient *> tst_QQmlEngineControl::createClients()
91{
92 m_client = new QQmlEngineControlClient(m_connection);
93 new QQmlEngineBlocker(m_client);
94 return QList<QQmlDebugClient *>({m_client});
95}
96
97void tst_QQmlEngineControl::engine_data()
98{
99 QTest::addColumn<bool>(name: "restrictMode");
100 QTest::newRow(dataTag: "unrestricted") << false;
101 QTest::newRow(dataTag: "restricted") << true;
102}
103
104void tst_QQmlEngineControl::startEngine_data()
105{
106 engine_data();
107}
108
109void tst_QQmlEngineControl::startEngine()
110{
111 QFETCH(bool, restrictMode);
112 QCOMPARE(connectTo("test.qml", restrictMode), ConnectSuccess);
113
114 QTRY_VERIFY(!m_client->blockedEngines().empty());
115 m_client->releaseEngine(engineId: m_client->blockedEngines().last());
116 QVERIFY(m_client->blockedEngines().isEmpty());
117
118 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(engineAdded(int,QString))),
119 "No engine start message received in time.");
120
121 QVERIFY(m_client->blockedEngines().isEmpty());
122}
123
124void tst_QQmlEngineControl::stopEngine_data()
125{
126 engine_data();
127}
128
129void tst_QQmlEngineControl::stopEngine()
130{
131 QFETCH(bool, restrictMode);
132
133 QCOMPARE(connectTo("exit.qml", restrictMode), ConnectSuccess);
134
135 QTRY_VERIFY(!m_client->blockedEngines().empty());
136 m_client->releaseEngine(engineId: m_client->blockedEngines().last());
137 QVERIFY(m_client->blockedEngines().isEmpty());
138
139 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(engineAdded(int,QString))),
140 "No engine start message received in time.");
141 QVERIFY(m_client->blockedEngines().isEmpty());
142
143 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(engineAboutToBeRemoved(int,QString))),
144 "No engine about to stop message received in time.");
145 m_client->releaseEngine(engineId: m_client->blockedEngines().last());
146 QVERIFY(m_client->blockedEngines().isEmpty());
147 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(engineRemoved(int,QString))),
148 "No engine stop message received in time.");
149 QVERIFY(m_client->blockedEngines().isEmpty());
150}
151
152QTEST_MAIN(tst_QQmlEngineControl)
153
154#include "tst_qqmlenginecontrol.moc"
155

source code of qtdeclarative/tests/auto/qml/debugger/qqmlenginecontrol/tst_qqmlenginecontrol.cpp