~ubuntu-branches/ubuntu/trusty/autopilot-qt/trusty

« back to all changes in this revision

Viewing changes to tests/unittests/tst_introspection.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Michael Zanetti, Christopher Lee, Olivier Tilloy, Ubuntu daily release
  • Date: 2013-06-05 07:08:47 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130605070847-cm5heug9bku0q19d
Tags: 1.3daily13.06.05-0ubuntu1
[ Michael Zanetti ]
* update tests to work with AP 1.3.
* make it work with Qt 5.0.2.

[ Christopher Lee ]
* Adding GetVersion so we can check that the version of the DBus
  protocol is as expected.

[ Olivier Tilloy ]
* Do not override an existing property with the value of a parent
  object. (LP: #1170764)

[ Ubuntu daily release ]
* Automatic snapshot from revision 61

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Michael Zanetti <michael.zanetti@canonical.com>
 
6
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 *
 
19
 */
 
20
 
 
21
#include <QStringList>
 
22
#include <QtTest>
 
23
#include <QMainWindow>
 
24
#include <QDebug>
 
25
#include <QGridLayout>
 
26
#include <QPushButton>
 
27
 
 
28
#include "introspection.h"
 
29
 
 
30
QVariant IntrospectNode(QObject* obj);
 
31
 
 
32
class tst_Introspection : public QObject
 
33
{
 
34
    Q_OBJECT
 
35
 
 
36
private slots:
 
37
    void initTestCase();
 
38
    void cleanupTestCase();
 
39
 
 
40
    void test_introspect_data();
 
41
    void test_introspect();
 
42
 
 
43
    void test_application_names_data();
 
44
    void test_application_names();
 
45
 
 
46
    void test_properties_data();
 
47
    void test_properties();
 
48
 
 
49
private:
 
50
    QMainWindow *m_object;
 
51
};
 
52
 
 
53
 
 
54
void tst_Introspection::initTestCase()
 
55
{
 
56
    QApplication::setApplicationName("tst_introspection");
 
57
 
 
58
    m_object = new QMainWindow();
 
59
    QWidget *centralWidget = new QWidget();
 
60
    centralWidget->setObjectName("centralTestWidget");
 
61
    m_object->setCentralWidget(centralWidget);
 
62
 
 
63
    QGridLayout *layout = new QGridLayout();
 
64
    layout->setObjectName("myTestLayout");
 
65
    centralWidget->setLayout(layout);
 
66
 
 
67
    QPushButton *button = new QPushButton("MyButton1");
 
68
    button->setObjectName("myButton1");
 
69
    layout->addWidget(button);
 
70
 
 
71
    button = new QPushButton("MyButton2");
 
72
    button->setObjectName("myButton2");
 
73
    layout->addWidget(button);
 
74
 
 
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);
 
87
 
 
88
    m_object->show();
 
89
}
 
90
 
 
91
void tst_Introspection::cleanupTestCase()
 
92
{
 
93
    m_object->close();
 
94
    delete m_object;
 
95
}
 
96
 
 
97
void tst_Introspection::test_introspect_data()
 
98
{
 
99
    // some query
 
100
    QTest::addColumn<QString>("xpath");
 
101
 
 
102
    // number of expected results
 
103
    QTest::addColumn<int>("resultCount");
 
104
 
 
105
    // first result object type. Empty string if 0 results expected
 
106
    QTest::addColumn<QString>("firstResultType");
 
107
 
 
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");
 
111
 
 
112
#ifdef QT5_SUPPORT
 
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");
 
116
#else
 
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");
 
119
 
 
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");
 
123
    } else {
 
124
        QTest::newRow("//QPushButton[id=8]") << "//QPushButton[id=8]" << 1 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton2");
 
125
    }
 
126
#endif
 
127
 
 
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();
 
132
}
 
133
 
 
134
void tst_Introspection::test_introspect()
 
135
{
 
136
    QFETCH(QString, xpath);
 
137
    QFETCH(int, resultCount);
 
138
    QFETCH(QString, firstResultType);
 
139
    QFETCH(QString, firstResultPropertyName);
 
140
    QFETCH(QVariant, firstResultPropertyValue);
 
141
 
 
142
    QList<QVariant> resultList = Introspect(xpath);
 
143
 
 
144
    QCOMPARE(resultList.count(), resultCount);
 
145
 
 
146
    if (resultCount > 0) {
 
147
        QVariant firstResult = resultList.first();
 
148
        QVariantMap firstResultProperties = firstResult.toList().last().toMap();
 
149
 
 
150
        QCOMPARE(firstResult.toList().first().toString(), firstResultType);
 
151
        QCOMPARE(firstResultProperties.value(firstResultPropertyName), firstResultPropertyValue);
 
152
    }
 
153
}
 
154
 
 
155
void tst_Introspection::test_application_names_data()
 
156
{
 
157
    QTest::addColumn<QString>("app_name");
 
158
 
 
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";
 
163
}
 
164
 
 
165
void tst_Introspection::test_application_names()
 
166
{
 
167
    QFETCH(QString, app_name);
 
168
    qApp->setApplicationName(app_name);
 
169
 
 
170
#ifdef QT5_SUPPORT
 
171
    QList<QVariant> result = Introspect("//QWidgetWindow");
 
172
#else
 
173
    QList<QVariant> result = Introspect("//QMainWindow");
 
174
#endif
 
175
 
 
176
    QVERIFY(!result.isEmpty());
 
177
}
 
178
 
 
179
void tst_Introspection::test_properties_data()
 
180
{
 
181
    QTest::addColumn<QString>("propertyName");
 
182
    QTest::addColumn<QVariant>("propertyValue");
 
183
    QTest::addColumn<bool>("fuzzyCompare");
 
184
 
 
185
    QTest::newRow("static property") << "objectName" << QVariant(m_object->objectName()) << false;
 
186
    QTest::newRow("dynamic property") << "dynamicTestProperty" << m_object->property("dynamicTestProperty") << false;
 
187
 
 
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;
 
192
 
 
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;
 
201
}
 
202
 
 
203
void tst_Introspection::test_properties()
 
204
{
 
205
    QFETCH(QString, propertyName);
 
206
    QFETCH(QVariant, propertyValue);
 
207
    QFETCH(bool, fuzzyCompare);
 
208
 
 
209
    QVariant result = IntrospectNode(m_object);
 
210
 
 
211
    QCOMPARE(result.toList().count(), 2);
 
212
    QVariantMap properties = result.toList().at(1).toMap();
 
213
 
 
214
    if (fuzzyCompare) {
 
215
        qFuzzyCompare(properties.value(propertyName).toDouble(), propertyValue.toDouble());
 
216
    } else {
 
217
        QCOMPARE(properties.value(propertyName), propertyValue);
 
218
    }
 
219
}
 
220
 
 
221
QTEST_MAIN(tst_Introspection)
 
222
 
 
223
#include "tst_introspection.moc"