Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
guifixture.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <sonde/guitools.h>
8#include <sonde/qmltools.h>
10#include <sonde/server.h>
12
13#include "testsupport.h"
14
15#include <QtGui/QShortcut>
16#include <QtQml/QQmlComponent>
17#include <QtQml/QQmlEngine>
18#include <QtQuick/QQuickItem>
19#include <QtQuick/QQuickWindow>
20#include <QtTest/QTest>
21#include <QtWidgets/QApplication>
22#include <QtWidgets/QCheckBox>
23#include <QtWidgets/QLabel>
24#include <QtWidgets/QLineEdit>
25#include <QtWidgets/QListWidget>
26#include <QtWidgets/QPushButton>
27#include <QtWidgets/QSlider>
28#include <QtWidgets/QVBoxLayout>
29#include <QtWidgets/QWidget>
30
31#include <gtest/gtest.h>
32
33namespace SondeTest {
34
36class GuiTest : public ::testing::Test
37{
38protected:
40 void SetUp() override
41 {
45 Sonde::installQmlTools(server); // qml_eval, qml_binding, ui_open, ui_close
46 name = uniqueName(QStringLiteral("gui"));
47 ASSERT_TRUE(server.listenLocal(name));
48
49 buildForm();
50 if (withQuick())
51 buildQuick();
52
53 form->activateWindow();
54 ASSERT_TRUE(QTest::qWaitForWindowExposed(form.get()));
55 (void)QTest::qWaitForWindowActive(form.get(), 1000); // best effort on offscreen
56
57 client = std::make_unique<LocalClient>(name);
58 client->initialize();
59 }
60
62 void TearDown() override
63 {
64 client.reset();
65 quick.reset();
66 form.reset();
67 }
68
70 virtual bool withQuick() const { return true; }
71
73 QJsonObject call(const QString &tool, const QJsonObject &args = { })
74 {
75 return client->callTool(tool, args);
76 }
77
78 QString text(const QString &tool, const QJsonObject &args = { })
79 {
80 return client->callToolText(tool, args);
81 }
82
83 bool fails(const QString &tool, const QJsonObject &args = { })
84 {
85 return call(tool, args).value(QStringLiteral("isError")).toBool();
86 }
87
89 QString name;
90 std::unique_ptr<LocalClient> client;
91
92 std::unique_ptr<QWidget> form;
93 QLineEdit *edit = nullptr;
94 QPushButton *button = nullptr;
95 QCheckBox *check = nullptr;
96 QSlider *slider = nullptr;
97 QListWidget *list = nullptr;
98 QLabel *hiddenLabel = nullptr;
99 int buttonClicks = 0;
100 int shortcutHits = 0;
101
102 QQmlEngine engine;
103 std::unique_ptr<QQuickWindow> quick;
104
105private:
108 {
109 form = std::make_unique<QWidget>();
110 form->setObjectName(QStringLiteral("form"));
111 form->setWindowTitle(QStringLiteral("Form Window"));
112 auto *layout = new QVBoxLayout(form.get());
113 edit = new QLineEdit;
114 edit->setObjectName(QStringLiteral("edit"));
115 button = new QPushButton(QStringLiteral("&Press"));
116 button->setObjectName(QStringLiteral("button"));
117 QObject::connect(button, &QPushButton::clicked, [this] { ++buttonClicks; });
118 check = new QCheckBox(QStringLiteral("Check"));
119 check->setObjectName(QStringLiteral("check"));
120 slider = new QSlider(Qt::Horizontal);
121 slider->setObjectName(QStringLiteral("slider"));
122 slider->setRange(0, 100);
123 list = new QListWidget;
124 list->setObjectName(QStringLiteral("list"));
125 for (int i = 0; i < 200; ++i)
126 list->addItem(QStringLiteral("item %1").arg(i));
127 hiddenLabel = new QLabel(QStringLiteral("secret"));
128 hiddenLabel->setObjectName(QStringLiteral("hiddenLabel"));
129 hiddenLabel->hide();
130 for (QWidget *w :
131 std::initializer_list<QWidget *>{ edit, button, check, slider, list, hiddenLabel })
132 layout->addWidget(w);
133 auto *shortcut = new QShortcut(QKeySequence(QStringLiteral("Ctrl+K")), form.get());
134 shortcut->setContext(Qt::ApplicationShortcut);
135 QObject::connect(shortcut, &QShortcut::activated, [this] { ++shortcutHits; });
136 form->resize(320, 480);
137 form->move(10, 10);
138 form->show();
139 edit->setFocus();
140 }
141
144 {
145 QQmlComponent component(&engine);
146 component.setData(R"(
147 import QtQuick
148 Window {
149 objectName: "quickWin"
150 title: "Quick Window"
151 width: 300; height: 200; x: 400; y: 10
152 visible: true
153 property int clicks: 0
154 Rectangle {
155 objectName: "box"
156 x: 10; y: 10; width: 100; height: 50
157 color: "red"
158 MouseArea { objectName: "boxArea"; anchors.fill: parent; onClicked: clicks++ }
159 }
160 TextInput { objectName: "input"; x: 10; y: 100; width: 200; height: 30 }
161 Rectangle { objectName: "invisible"; visible: false; width: 10; height: 10 }
162 Rectangle { objectName: "transparent"; x: 250; y: 150; width: 40; height: 40; opacity: 0 }
163 })",
164 QUrl());
165 quick.reset(qobject_cast<QQuickWindow *>(component.create()));
166 ASSERT_TRUE(quick) << component.errorString().toStdString();
167 ASSERT_TRUE(QTest::qWaitForWindowExposed(quick.get()));
168 }
169};
170
171} // namespace SondeTest
Fixture with a fully equipped server, a widget form and a QML window.
Definition guifixture.h:37
QString name
Local socket name of the server.
Definition guifixture.h:89
void SetUp() override
Installs integrations and tools, builds and shows both windows.
Definition guifixture.h:40
virtual bool withQuick() const
Whether this test needs the QML window (override to skip it).
Definition guifixture.h:70
bool fails(const QString &tool, const QJsonObject &args={ })
Calls a tool and reports whether it returned an error result.
Definition guifixture.h:83
int shortcutHits
Times the Ctrl+K shortcut fired.
Definition guifixture.h:100
std::unique_ptr< LocalClient > client
Initialized client.
Definition guifixture.h:90
void buildForm()
Builds the Qt Widgets form.
Definition guifixture.h:107
QLineEdit * edit
Line edit "edit" (has focus initially).
Definition guifixture.h:93
std::unique_ptr< QQuickWindow > quick
The Qt Quick window ("Quick Window").
Definition guifixture.h:103
QPushButton * button
Push button "button" ("&Press").
Definition guifixture.h:94
QJsonObject call(const QString &tool, const QJsonObject &args={ })
Calls a tool and returns its result.
Definition guifixture.h:73
void buildQuick()
Builds the Qt Quick window from inline QML.
Definition guifixture.h:143
QLabel * hiddenLabel
Hidden label "hiddenLabel".
Definition guifixture.h:98
Sonde::Server server
Server under test.
Definition guifixture.h:88
void TearDown() override
Destroys the windows (before the server).
Definition guifixture.h:62
int buttonClicks
Times button was clicked.
Definition guifixture.h:99
QSlider * slider
Horizontal slider "slider", 0-100.
Definition guifixture.h:96
QString text(const QString &tool, const QJsonObject &args={ })
Calls a tool and returns its text.
Definition guifixture.h:78
QCheckBox * check
Check box "check".
Definition guifixture.h:95
QListWidget * list
List "list" with 200 items (scrollable).
Definition guifixture.h:97
QQmlEngine engine
Engine of the QML window.
Definition guifixture.h:102
std::unique_ptr< QWidget > form
The widgets window ("Form Window").
Definition guifixture.h:92
An embeddable Model Context Protocol server.
Definition server.h:92
bool listenLocal(const QString &name)
Starts (or restarts) the local socket transport.
Definition server.cpp:563
Installers for the GUI tool sets (windows, screenshots, input).
void installQuickIntegration()
Teaches the GUI tools about Qt Quick.
Definition quickintegration.cpp:515
QmlTemplateTrust * installQmlTools(Server &server)
Registers qml_eval, qml_binding and the QML window tools (installQmlWindowTools()).
Definition qmltools.cpp:177
void installWidgetsIntegration()
Teaches the GUI tools about Qt Widgets.
Definition widgetsintegration.cpp:301
void installStandardTools(Server &server)
Registers everything above, the QObject introspection tools, a LogEventSource that feeds Qt log outpu...
Definition windowtools.cpp:715
The qml_eval and qml_binding tools, and installQmlTools(): everything in Sonde::Quick that needs a se...
Qt Quick support for the GUI tools.
Sonde::Server, the embeddable MCP server.
QString uniqueName(const QString &prefix)
Returns a process-unique name with the given prefix (for sockets).
Definition testsupport.cpp:49
Helpers shared by the Sonde unit tests: event-loop waiting and in-process MCP clients for the local-s...
Qt Widgets support for the GUI tools.