~robru/autopilot-qt/packaging

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (C) 2013 Canonical, Ltd.
 *
 * Authors:
 *  Michael Zanetti <michael.zanetti@canonical.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <QStringList>
#include <QtTest>
#include <QMainWindow>
#include <QDebug>
#include <QGridLayout>
#include <QPushButton>

#include "introspection.h"

QVariant IntrospectNode(QObject* obj);

class tst_Introspection : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void cleanupTestCase();

    void test_introspect_data();
    void test_introspect();

    void test_application_names_data();
    void test_application_names();

    void test_properties_data();
    void test_properties();

private:
    QMainWindow *m_object;
};


void tst_Introspection::initTestCase()
{
    QApplication::setApplicationName("tst_introspection");

    m_object = new QMainWindow();
    QWidget *centralWidget = new QWidget();
    centralWidget->setObjectName("centralTestWidget");
    m_object->setCentralWidget(centralWidget);

    QGridLayout *layout = new QGridLayout();
    layout->setObjectName("myTestLayout");
    centralWidget->setLayout(layout);

    QPushButton *button = new QPushButton("MyButton1");
    button->setObjectName("myButton1");
    layout->addWidget(button);

    button = new QPushButton("MyButton2");
    button->setObjectName("myButton2");
    layout->addWidget(button);

    m_object->setObjectName("testWindow");
    m_object->setProperty("dynamicTestProperty", "testValue");
    m_object->setProperty("myUInt", QVariant(quint8(5)));
    m_object->setProperty("myStringList", QVariant(QStringList() << "string1" << "string2" << "string3"));
    m_object->setProperty("myColor", QColor("red"));
    m_object->setProperty("myByteArray", QByteArray("0xDEADBEEF"));
    m_object->setProperty("myUrl", QUrl("http://www.ubuntu.com"));
    m_object->setProperty("myDateTime", QDateTime::currentDateTime());
    m_object->setProperty("myDate", QDateTime::currentDateTime().date());
    m_object->setProperty("myTime", QTime::currentTime());
    m_object->setMaximumSize(1234, 4321);
    m_object->resize(123, 321);
    m_object->move(333, 444);
    m_object->setVisible(false);
    m_object->setWindowOpacity(0.12345);

    m_object->show();
}

void tst_Introspection::cleanupTestCase()
{
    m_object->close();
    delete m_object;
}

void tst_Introspection::test_introspect_data()
{
    // some query
    QTest::addColumn<QString>("xpath");

    // number of expected results
    QTest::addColumn<int>("resultCount");

    // first result object type. Empty string if 0 results expected
    QTest::addColumn<QString>("firstResultType");

    // Choose a property from the first result object to be compared, empty QString/QVariant if 0 results expected
    QTest::addColumn<QString>("firstResultPropertyName");
    QTest::addColumn<QVariant>("firstResultPropertyValue");

#ifdef QT5_SUPPORT
    QTest::newRow("/") << "/" << 1 << "/tst_introspection" << "Children" << QVariant(QStringList() << "QMainWindow" << "QWidgetWindow");
    QTest::newRow("//QWidget[id=6]") << "//QWidget[id=6]" << 1 << "/tst_introspection/QMainWindow/QWidget" << "objectName" << QVariant("centralTestWidget");
    QTest::newRow("//QPushButton[id=9]") << "//QPushButton[id=9]" << 1 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton2");
#else
    QTest::newRow("/") << "/" << 1 << "/tst_introspection" << "Children" << QVariant(QStringList() << "QMainWindow");
    QTest::newRow("//QWidget[id=5]") << "//QWidget[id=5]" << 1 << "/tst_introspection/QMainWindow/QWidget" << "objectName" << QVariant("centralTestWidget");

    // Depending on the environment, Qt4 could add a second QWidget at position 6. That moves other items down by one.
    if (Introspect("//QWidget[id=6]").count() > 0) {
        QTest::newRow("//QPushButton[id=9]") << "//QPushButton[id=9]" << 1 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton2");
    } else {
        QTest::newRow("//QPushButton[id=8]") << "//QPushButton[id=8]" << 1 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton2");
    }
#endif

    QTest::newRow("//GridLayout") << "//QGridLayout" << 1 << "/tst_introspection/QMainWindow/QWidget/QGridLayout" << "objectName" << QVariant("myTestLayout");
    QTest::newRow("//QPushButton") << "//QPushButton" << 2 << "/tst_introspection/QMainWindow/QWidget/QPushButton" << "objectName" << QVariant("myButton1");
    QTest::newRow("//QWidget/*") << "//QWidget/*" << 5 << "/tst_introspection/QMainWindow/QWidget/QGridLayout" << "objectName" << QVariant("myTestLayout");
    QTest::newRow("broken query") << "broken query" << 0 << QString() << QString() << QVariant();
}

void tst_Introspection::test_introspect()
{
    QFETCH(QString, xpath);
    QFETCH(int, resultCount);
    QFETCH(QString, firstResultType);
    QFETCH(QString, firstResultPropertyName);
    QFETCH(QVariant, firstResultPropertyValue);

    QList<QVariant> resultList = Introspect(xpath);

    QCOMPARE(resultList.count(), resultCount);

    if (resultCount > 0) {
        QVariant firstResult = resultList.first();
        QVariantMap firstResultProperties = firstResult.toList().last().toMap();

        QCOMPARE(firstResult.toList().first().toString(), firstResultType);
        QCOMPARE(firstResultProperties.value(firstResultPropertyName), firstResultPropertyValue);
    }
}

void tst_Introspection::test_application_names_data()
{
    QTest::addColumn<QString>("app_name");

    QTest::newRow("Unset") << "untitled1";
    QTest::newRow("Tech") << "autopilot-qt";
    QTest::newRow("Userfriendly") << "Autopilot Qt Driver";
    QTest::newRow("Fqdn name") << "com.canonical.Autopilot.Qt";
}

void tst_Introspection::test_application_names()
{
    QFETCH(QString, app_name);
    qApp->setApplicationName(app_name);

#ifdef QT5_SUPPORT
    QList<QVariant> result = Introspect("//QWidgetWindow");
#else
    QList<QVariant> result = Introspect("//QMainWindow");
#endif

    QVERIFY(!result.isEmpty());
}

void tst_Introspection::test_properties_data()
{
    QTest::addColumn<QString>("propertyName");
    QTest::addColumn<QVariant>("propertyValue");
    QTest::addColumn<bool>("fuzzyCompare");

    QTest::newRow("static property") << "objectName" << QVariant(m_object->objectName()) << false;
    QTest::newRow("dynamic property") << "dynamicTestProperty" << m_object->property("dynamicTestProperty") << false;

    QTest::newRow("int") << "width" << QVariant(m_object->width()) << false;
    QTest::newRow("uint") << "myUInt" << m_object->property("myUInt") << false;
    QTest::newRow("bool") << "visible" << QVariant(m_object->isVisible()) << false;
    QTest::newRow("double") << "windowOpacity" << QVariant(m_object->windowOpacity()) << true;

    QTest::newRow("QString") << "objectName" << QVariant(m_object->objectName()) << false;
    QTest::newRow("QStringList") << "myStringList" << m_object->property("myStringList") << false;
    QTest::newRow("QSize") << "maximumSize" << QVariant(QList<QVariant>() << m_object->maximumWidth() << m_object->maximumHeight()) << false;
    QTest::newRow("QPoint") << "pos" << QVariant(QList<QVariant>() << m_object->x() << m_object->y()) << false;
    QTest::newRow("QRect") << "geometry" << QVariant(QList<QVariant>() << m_object->geometry().x() << m_object->geometry().y() << m_object->geometry().width() << m_object->geometry().height()) << false;
    QTest::newRow("QColor") << "myColor" << QVariant(QList<QVariant>() << 255 << 0 << 0 << 255) << false;
    QTest::newRow("QByteArray") << "myByteArray" << m_object->property("myByteArray") << false;
    QTest::newRow("QUrl") << "myUrl" << m_object->property("myUrl") << false;
    QTest::newRow("QDateTime") << "myDateTime" << QVariant(m_object->property("myDateTime").toDateTime().toTime_t()) << false;
    QTest::newRow("QDate") << "myDate" << QVariant(m_object->property("myDate").toDateTime().toTime_t()) << false;
    QTest::newRow("QTime") << "myTime" << QVariant(m_object->property("myTime").toTime().toString("hh:mm:ss")) << false;
}

void tst_Introspection::test_properties()
{
    QFETCH(QString, propertyName);
    QFETCH(QVariant, propertyValue);
    QFETCH(bool, fuzzyCompare);

    QVariant result = IntrospectNode(m_object);

    QCOMPARE(result.toList().count(), 2);
    QVariantMap properties = result.toList().at(1).toMap();

    if (fuzzyCompare) {
        qFuzzyCompare(properties.value(propertyName).toDouble(), propertyValue.toDouble());
    } else {
        QCOMPARE(properties.value(propertyName), propertyValue);
    }
}

QTEST_MAIN(tst_Introspection)

#include "tst_introspection.moc"