aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlsynchronizer/tst_qqmlsynchronizer.cpp
blob: a2657128d360f1f3a7db1a483f5721fdd8112375 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtQuickTestUtils/private/qmlutils_p.h>
#include <QtQmlModels/private/qqmldelegatemodel_p.h>

#include <QtQml/qqmlcomponent.h>
#include <QtQml/qqmlengine.h>

#include <QtTest/qtest.h>

#include <QtCore/qproperty.h>

class tst_qqmlsynchronizer : public QQmlDataTest
{
    Q_OBJECT
public:
    tst_qqmlsynchronizer();

private slots:
    void basics_data();
    void basics();

    void ignored();
    void valueTypes();
    void modelObject();
    void warnings();
};

class BindablePoint : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    Q_PROPERTY(QPointF point BINDABLE bindablePoint READ default WRITE default)
public:
    BindablePoint(QObject *parent = nullptr) : QObject(parent), m_point(QPointF(101, 102)) {}

    QBindable<QPointF> bindablePoint() { return QBindable<QPointF>(&m_point); }

private:
    QProperty<QPointF> m_point;
};

tst_qqmlsynchronizer::tst_qqmlsynchronizer()
    : QQmlDataTest(QT_QMLTEST_DATADIR)
{
    qmlRegisterTypesAndRevisions<BindablePoint>("Test", 1);
}

void tst_qqmlsynchronizer::basics_data()
{
    QTest::addColumn<QUrl>("url");
    QTest::addColumn<bool>("initializesOther");
    QTest::addRow("onVsAlias") << testFileUrl("onVsAlias.qml") << true;
    QTest::addRow("onVsTargetProperty") << testFileUrl("onVsTargetProperty.qml") << true;
    QTest::addRow("aliasVsTargetProperty") << testFileUrl("aliasVsTargetProperty.qml") << false;
    QTest::addRow("onVsSourceProperty") << testFileUrl("onVsSourceProperty.qml") << false;
    QTest::addRow("aliasVsSourceProperty") << testFileUrl("aliasVsSourceProperty.qml") << true;
}

void tst_qqmlsynchronizer::basics()
{
    QFETCH(QUrl, url);
    QFETCH(bool, initializesOther);

    QQmlEngine engine;
    QQmlComponent c(&engine, url);
    QVERIFY2(c.isReady(), qPrintable(c.errorString()));
    QScopedPointer<QObject> o(c.create());
    QVERIFY(!o.isNull());

    QObject *other = o->property("other").value<QObject *>();
    QVERIFY(other);

    QCOMPARE(o->objectName(), initializesOther ? "bar" : "foo");
    QCOMPARE(other->objectName(), initializesOther ? "bar" : "foo");

    o->setObjectName("baz");
    QCOMPARE(o->objectName(), "baz");
    QCOMPARE(other->objectName(), "baz");

    other->setObjectName("foo");
    QCOMPARE(o->objectName(), "foo");
    QCOMPARE(other->objectName(), "foo");

    QCOMPARE(o->property("unbindable").toString(), initializesOther ? "a" : "b");
    QCOMPARE(other->property("unbindable").toString(), initializesOther ? "a" : "b");

    o->setProperty("unbindable", QStringLiteral("c"));

    QCOMPARE(o->property("unbindable").toString(), "c");
    QCOMPARE(other->property("unbindable").toString(), "c");

    other->setProperty("unbindable", QStringLiteral("d"));

    QCOMPARE(o->property("unbindable").toString(), "d");
    QCOMPARE(other->property("unbindable").toString(), "d");

    QCOMPARE(o->property("count").toInt(), initializesOther ? 12 : 13);
    QCOMPARE(other->property("count").toDouble(), 13.5);
    int countBounces = initializesOther ? 1 : 0;
    QCOMPARE(o->property("countBounces").toInt(), countBounces);
    QCOMPARE(o->property("countIgnores").toInt(), 0);

    o->setProperty("count", 17);
    ++countBounces;
    QCOMPARE(o->property("count").toInt(), 17);
    QCOMPARE(other->property("count").toDouble(), 13.5);
    QCOMPARE(o->property("countBounces").toInt(), countBounces);
    QCOMPARE(o->property("countIgnores").toInt(), 0);

    other->setProperty("count", 18);
    QCOMPARE(o->property("count").toInt(), 13);
    QCOMPARE(other->property("count").toDouble(), 13.5);
    QCOMPARE(o->property("countBounces").toInt(), countBounces);
    QCOMPARE(o->property("countIgnores").toInt(), 0);
}

void tst_qqmlsynchronizer::ignored()
{
    QQmlEngine engine;
    QQmlComponent c(&engine, testFileUrl("ignored.qml"));
    QVERIFY2(c.isReady(), qPrintable(c.errorString()));

    QScopedPointer<QObject> o(c.create());
    QVERIFY(!o.isNull());

    QObject *other = o->property("other").value<QObject *>();
    QVERIFY(other);

    QCOMPARE(o->property("count").toInt(), 12);
    QCOMPARE(other->property("count").toDouble(), 12);

    QCOMPARE(o->property("countBounces").toInt(), 0);
    QCOMPARE(o->property("countIgnores").toInt(), 0);

    other->setProperty("count", 12.4);
    QCOMPARE(o->property("count").toInt(), 12);
    QCOMPARE(o->property("countBounces").toInt(), 0);
    QCOMPARE(o->property("countIgnores").toInt(), 1);
}

void tst_qqmlsynchronizer::valueTypes()
{
    QQmlEngine engine;
    QQmlComponent c(&engine, testFileUrl("valueTypes.qml"));
    QVERIFY2(c.isReady(), qPrintable(c.errorString()));

    QScopedPointer<QObject> o(c.create());
    QVERIFY(!o.isNull());

    QObject *other = o->property("other").value<QObject *>();
    QVERIFY(other);

    QCOMPARE(o->property("point").value<QPointF>(), QPointF(101, 102));
    QCOMPARE(other->property("point").value<QPointF>(), QPointF(12, 11));
    QCOMPARE(o->objectName(), "11");
    QCOMPARE(other->objectName(), "102");
    QCOMPARE(other->property("count"), 101);
    QCOMPARE(o->property("count"), 12);

    // Since point can only be summarily signaled, this updates the other synchronizer, too.
    o->setProperty("count", 77);
    QCOMPARE(other->property("point").value<QPointF>(), QPointF(77, 11));
    QCOMPARE(o->objectName(), "11");

    other->setProperty("point", QPointF(97, 98));
    QCOMPARE(o->property("count"), 97);
    QCOMPARE(o->objectName(), "98");

    o->setObjectName("54");
    QCOMPARE(other->property("point").value<QPointF>(), QPointF(97, 54));

    o->setProperty("point", QPointF(33.25, 34.8));
    QCOMPARE(other->property("count").toDouble(), 33.25);
    QCOMPARE(other->objectName(), "34.8");
    other->setProperty("count", 55.5);
    QCOMPARE(o->property("point").value<QPointF>(), QPointF(55.5, 34.8));
    QCOMPARE(other->objectName(), "34.8");
    other->setObjectName("77.2");
    QCOMPARE(o->property("point").value<QPointF>(), QPointF(55.5, 77.2));
}

void tst_qqmlsynchronizer::modelObject()
{
    QQmlEngine engine;
    QQmlComponent c(&engine, testFileUrl("modelObject.qml"));
    QVERIFY2(c.isReady(), qPrintable(c.errorString()));

    QScopedPointer<QObject> o(c.create());
    QVERIFY(!o.isNull());

    QQmlDelegateModel *delegateModel = qobject_cast<QQmlDelegateModel *>(o.data());
    QVERIFY(delegateModel);

    QObject *delegate = delegateModel->object(0);
    QVERIFY(delegate);

    QAbstractListModel *model = delegateModel->property("listModel").value<QAbstractListModel *>();
    QVERIFY(model);

    const auto roleNames = model->roleNames();
    int role = -1;
    for (auto it = roleNames.begin(), end = roleNames.end(); it != end; ++it) {
        if (it.value() == "a") {
            role = it.key();
            break;
        }
    }

    const QModelIndex modelIndex = model->index(0);

    QCOMPARE(delegate->objectName(), "foo");
    QCOMPARE(model->data(modelIndex, role), "foo");

    model->setData(modelIndex, "c", role);
    QCOMPARE(model->data(modelIndex, role), "c");
    QCOMPARE(delegate->objectName(), "c");

    delegate->setObjectName("d");
    QCOMPARE(model->data(modelIndex, role), "d");
}

void tst_qqmlsynchronizer::warnings()
{
    QQmlEngine engine;
    const QUrl url = testFileUrl("warnings.qml");
    QQmlComponent c(&engine, url);
    QVERIFY2(c.isReady(), qPrintable(c.errorString()));

    QTest::ignoreMessage(
            QtWarningMsg,
            qPrintable(url.toString()
                + ":31:44: QML Synchronizer: Target object has no property called doesNotExist"));
    QTest::ignoreMessage(
            QtWarningMsg,
            qPrintable(url.toString()
                + ":37:39: QML Synchronizer: Member doThings of target object is a function"));

    QScopedPointer<QObject> o(c.create());
    QVERIFY(!o.isNull());

    QTest::ignoreMessage(
            QtWarningMsg,
            qPrintable(url.toString()
                + ":25:43: QML Synchronizer: Cannot convert from QPointF to QObject*"));

    o->setProperty("point", QPointF(13, 14));
}

QTEST_MAIN(tst_qqmlsynchronizer)

#include "tst_qqmlsynchronizer.moc"