2
* Copyright (C) 2013 Canonical, Ltd.
5
* Michael Zanetti <michael.zanetti@canonical.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 3.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include <QStringList>
23
#include <QMainWindow>
25
#include <QGridLayout>
26
#include <QPushButton>
28
#include "introspection.h"
30
QVariant IntrospectNode(QObject* obj);
32
class tst_Introspection : public QObject
38
void cleanupTestCase();
40
void test_introspect_data();
41
void test_introspect();
43
void test_application_names_data();
44
void test_application_names();
46
void test_properties_data();
47
void test_properties();
50
QMainWindow *m_object;
54
void tst_Introspection::initTestCase()
56
QApplication::setApplicationName("tst_introspection");
58
m_object = new QMainWindow();
59
QWidget *centralWidget = new QWidget();
60
centralWidget->setObjectName("centralTestWidget");
61
m_object->setCentralWidget(centralWidget);
63
QGridLayout *layout = new QGridLayout();
64
layout->setObjectName("myTestLayout");
65
centralWidget->setLayout(layout);
67
QPushButton *button = new QPushButton("MyButton1");
68
button->setObjectName("myButton1");
69
layout->addWidget(button);
71
button = new QPushButton("MyButton2");
72
button->setObjectName("myButton2");
73
layout->addWidget(button);
75
m_object->setObjectName("testWindow");
76
m_object->setProperty("dynamicTestProperty", "testValue");
77
m_object->setProperty("myUInt", QVariant(quint8(5)));
78
m_object->setProperty("myStringList", QVariant(QStringList() << "string1" << "string2" << "string3"));
79
m_object->setProperty("myColor", QColor("red"));
80
m_object->setProperty("myByteArray", QByteArray("0xDEADBEEF"));
81
m_object->setProperty("myUrl", QUrl("http://www.ubuntu.com"));
82
m_object->setMaximumSize(1234, 4321);
83
m_object->resize(123, 321);
84
m_object->move(333, 444);
85
m_object->setVisible(false);
86
m_object->setWindowOpacity(0.12345);
91
void tst_Introspection::cleanupTestCase()
97
void tst_Introspection::test_introspect_data()
100
QTest::addColumn<QString>("xpath");
102
// number of expected results
103
QTest::addColumn<int>("resultCount");
105
// first result object type. Empty string if 0 results expected
106
QTest::addColumn<QString>("firstResultType");
108
// Choose a property from the first result object to be compared, empty QString/QVariant if 0 results expected
109
QTest::addColumn<QString>("firstResultPropertyName");
110
QTest::addColumn<QVariant>("firstResultPropertyValue");
113
QTest::newRow("/") << "/" << 1 << "/tst_introspection" << "Children" << QVariant(QStringList() << "QMainWindow" << "QWidgetWindow");
114
QTest::newRow("//QWidget[id=6]") << "//QWidget[id=6]" << 1 << "/tst_introspection/QMainWindow/QWidget" << "objectName" << QVariant("centralTestWidget");
115
QTest::newRow("//QPushButton[id=9]") << "//QPushButton[id=9]" << 1 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton2");
117
QTest::newRow("/") << "/" << 1 << "/tst_introspection" << "Children" << QVariant(QStringList() << "QMainWindow");
118
QTest::newRow("//QWidget[id=5]") << "//QWidget[id=5]" << 1 << "/tst_introspection/QMainWindow/QWidget" << "objectName" << QVariant("centralTestWidget");
120
// Depending on the environment, Qt4 could add a second QWidget at position 6. That moves other items down by one.
121
if (Introspect("//QWidget[id=6]").count() > 0) {
122
QTest::newRow("//QPushButton[id=9]") << "//QPushButton[id=9]" << 1 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton2");
124
QTest::newRow("//QPushButton[id=8]") << "//QPushButton[id=8]" << 1 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton2");
128
QTest::newRow("//GridLayout") << "//QGridLayout" << 1 << "/tst_introspection/QMainWindow/QWidget/QGridLayout" << "objectName" << QVariant("myTestLayout");
129
QTest::newRow("//QPushButton") << "//QPushButton" << 2 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton1");
130
QTest::newRow("//QWidget/*") << "//QWidget/*" << 5 << "/tst_introspection/QMainWindow/QWidget/QGridLayout" << "objectName" << QVariant("myTestLayout");
131
QTest::newRow("broken query") << "broken query" << 0 << QString() << QString() << QVariant();
134
void tst_Introspection::test_introspect()
136
QFETCH(QString, xpath);
137
QFETCH(int, resultCount);
138
QFETCH(QString, firstResultType);
139
QFETCH(QString, firstResultPropertyName);
140
QFETCH(QVariant, firstResultPropertyValue);
142
QList<QVariant> resultList = Introspect(xpath);
144
QCOMPARE(resultList.count(), resultCount);
146
if (resultCount > 0) {
147
QVariant firstResult = resultList.first();
148
QVariantMap firstResultProperties = firstResult.toList().last().toMap();
150
QCOMPARE(firstResult.toList().first().toString(), firstResultType);
151
QCOMPARE(firstResultProperties.value(firstResultPropertyName), firstResultPropertyValue);
155
void tst_Introspection::test_application_names_data()
157
QTest::addColumn<QString>("app_name");
159
QTest::newRow("Unset") << "untitled1";
160
QTest::newRow("Tech") << "autopilot-qt";
161
QTest::newRow("Userfriendly") << "Autopilot Qt Driver";
162
QTest::newRow("Fqdn name") << "com.canonical.Autopilot.Qt";
165
void tst_Introspection::test_application_names()
167
QFETCH(QString, app_name);
168
qApp->setApplicationName(app_name);
171
QList<QVariant> result = Introspect("//QWidgetWindow");
173
QList<QVariant> result = Introspect("//QMainWindow");
176
QVERIFY(!result.isEmpty());
179
void tst_Introspection::test_properties_data()
181
QTest::addColumn<QString>("propertyName");
182
QTest::addColumn<QVariant>("propertyValue");
183
QTest::addColumn<bool>("fuzzyCompare");
185
QTest::newRow("static property") << "objectName" << QVariant(m_object->objectName()) << false;
186
QTest::newRow("dynamic property") << "dynamicTestProperty" << m_object->property("dynamicTestProperty") << false;
188
QTest::newRow("int") << "width" << QVariant(m_object->width()) << false;
189
QTest::newRow("uint") << "myUInt" << m_object->property("myUInt") << false;
190
QTest::newRow("bool") << "visible" << QVariant(m_object->isVisible()) << false;
191
QTest::newRow("double") << "windowOpacity" << QVariant(m_object->windowOpacity()) << true;
193
QTest::newRow("QString") << "objectName" << QVariant(m_object->objectName()) << false;
194
QTest::newRow("QStringList") << "myStringList" << m_object->property("myStringList") << false;
195
QTest::newRow("QSize") << "maximumSize" << QVariant(QList<QVariant>() << m_object->maximumWidth() << m_object->maximumHeight()) << false;
196
QTest::newRow("QPoint") << "pos" << QVariant(QList<QVariant>() << m_object->x() << m_object->y()) << false;
197
QTest::newRow("QRect") << "geometry" << QVariant(QList<QVariant>() << m_object->geometry().x() << m_object->geometry().y() << m_object->geometry().width() << m_object->geometry().height()) << false;
198
QTest::newRow("QColor") << "myColor" << QVariant(QList<QVariant>() << 255 << 0 << 0 << 255) << false;
199
QTest::newRow("QByteArray") << "myByteArray" << m_object->property("myByteArray") << false;
200
QTest::newRow("QUrl") << "myUrl" << m_object->property("myUrl") << false;
203
void tst_Introspection::test_properties()
205
QFETCH(QString, propertyName);
206
QFETCH(QVariant, propertyValue);
207
QFETCH(bool, fuzzyCompare);
209
QVariant result = IntrospectNode(m_object);
211
QCOMPARE(result.toList().count(), 2);
212
QVariantMap properties = result.toList().at(1).toMap();
215
qFuzzyCompare(properties.value(propertyName).toDouble(), propertyValue.toDouble());
217
QCOMPARE(properties.value(propertyName), propertyValue);
221
QTEST_MAIN(tst_Introspection)
223
#include "tst_introspection.moc"