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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qtest.h>
#include <QSignalSpy>
#include <QtDeclarative/qmlengine.h>
#include <QtDeclarative/qmlcomponent.h>
#include <QtDeclarative/qmlview.h>
#include <private/qmlgraphicsrectangle_p.h>
#include <private/qmlgraphicstext_p.h>
#include <QtDeclarative/private/qmlgraphicsanchors_p_p.h>
Q_DECLARE_METATYPE(QmlGraphicsAnchors::UsedAnchor)
Q_DECLARE_METATYPE(QmlGraphicsAnchorLine::AnchorLine)
class tst_anchors : public QObject
{
Q_OBJECT
public:
tst_anchors() {}
template<typename T>
T *findItem(QmlGraphicsItem *parent, const QString &id);
private slots:
void basicAnchors();
void loops();
void illegalSets();
void illegalSets_data();
void reset();
void reset_data();
void resetConvenience();
void nullItem();
void nullItem_data();
void crash1();
void centerIn();
void fill();
void margins();
};
/*
Find an item with the specified id.
*/
template<typename T>
T *tst_anchors::findItem(QmlGraphicsItem *parent, const QString &objectName)
{
const QMetaObject &mo = T::staticMetaObject;
QList<QGraphicsItem *> children = parent->childItems();
for (int i = 0; i < children.count(); ++i) {
QmlGraphicsItem *item = qobject_cast<QmlGraphicsItem *>(children.at(i)->toGraphicsObject());
if (item) {
if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
return static_cast<T*>(item);
}
item = findItem<T>(item, objectName);
if (item)
return static_cast<T*>(item);
}
}
return 0;
}
void tst_anchors::basicAnchors()
{
QmlView *view = new QmlView;
view->setUrl(QUrl::fromLocalFile(SRCDIR "/data/anchors.qml"));
view->execute();
qApp->processEvents();
//sibling horizontal
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect1"))->x(), 26.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect2"))->x(), 122.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect3"))->x(), 74.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect4"))->x(), 16.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect5"))->x(), 112.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect6"))->x(), 64.0);
//parent horizontal
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect7"))->x(), 0.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect8"))->x(), 240.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect9"))->x(), 120.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect10"))->x(), -10.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect11"))->x(), 230.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect12"))->x(), 110.0);
//vertical
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect13"))->y(), 20.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect14"))->y(), 155.0);
//stretch
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect15"))->x(), 26.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect15"))->width(), 96.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect16"))->x(), 26.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect16"))->width(), 192.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect17"))->x(), -70.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect17"))->width(), 192.0);
//vertical stretch
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect18"))->y(), 20.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect18"))->height(), 40.0);
//more parent horizontal
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect19"))->x(), 115.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect20"))->x(), 235.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect21"))->x(), -5.0);
//centerIn
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect22"))->x(), 69.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect22"))->y(), 5.0);
//margins
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect23"))->x(), 31.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect23"))->y(), 5.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect23"))->width(), 86.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect23"))->height(), 10.0);
// offsets
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect24"))->x(), 26.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect25"))->y(), 60.0);
QCOMPARE(findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("rect26"))->y(), 5.0);
//baseline
QmlGraphicsText *text1 = findItem<QmlGraphicsText>(view->root(), QLatin1String("text1"));
QmlGraphicsText *text2 = findItem<QmlGraphicsText>(view->root(), QLatin1String("text2"));
QCOMPARE(text1->y(), text2->y());
delete view;
}
// mostly testing that we don't crash
void tst_anchors::loops()
{
{
QmlView *view = new QmlView;
view->setUrl(QUrl::fromLocalFile(SRCDIR "/data/loop1.qml"));
QString expect = "QML Text (" + view->url().toString() + ":6:5" + ") Possible anchor loop detected on horizontal anchor.";
QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
view->execute();
qApp->processEvents();
delete view;
}
{
QmlView *view = new QmlView;
view->setUrl(QUrl::fromLocalFile(SRCDIR "/data/loop2.qml"));
QString expect = "QML Image (" + view->url().toString() + ":8:3" + ") Possible anchor loop detected on horizontal anchor.";
QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
view->execute();
qApp->processEvents();
delete view;
}
}
void tst_anchors::illegalSets()
{
QFETCH(QString, qml);
QFETCH(QString, warning);
QTest::ignoreMessage(QtWarningMsg, warning.toLatin1());
QmlEngine engine;
QmlComponent component(&engine);
component.setData(QByteArray("import Qt 4.6\n" + qml.toUtf8()), QUrl::fromLocalFile(""));
if (!component.isReady())
qWarning() << "Test errors:" << component.errors();
QVERIFY(component.isReady());
QObject *o = component.create();
delete o;
}
void tst_anchors::illegalSets_data()
{
QTest::addColumn<QString>("qml");
QTest::addColumn<QString>("warning");
QTest::newRow("H - too many anchors")
<< "Rectangle { id: rect; Rectangle { anchors.left: rect.left; anchors.right: rect.right; anchors.horizontalCenter: rect.horizontalCenter } }"
<< "QML Rectangle (file::2:23) Can't specify left, right, and hcenter anchors.";
foreach (const QString &side, QStringList() << "left" << "right") {
QTest::newRow("H - anchor to V")
<< QString("Rectangle { Rectangle { anchors.%1: parent.top } }").arg(side)
<< "QML Rectangle (file::2:13) Can't anchor a horizontal edge to a vertical edge.";
QTest::newRow("H - anchor to non parent/sibling")
<< QString("Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.%1: rect.%1 } }").arg(side)
<< "QML Rectangle (file::2:45) Can't anchor to an item that isn't a parent or sibling.";
QTest::newRow("H - anchor to self")
<< QString("Rectangle { id: rect; anchors.%1: rect.%1 }").arg(side)
<< "QML Rectangle (file::2:1) Can't anchor item to self.";
}
QTest::newRow("V - too many anchors")
<< "Rectangle { id: rect; Rectangle { anchors.top: rect.top; anchors.bottom: rect.bottom; anchors.verticalCenter: rect.verticalCenter } }"
<< "QML Rectangle (file::2:23) Can't specify top, bottom, and vcenter anchors.";
QTest::newRow("V - too many anchors with baseline")
<< "Rectangle { Text { id: text1; text: \"Hello\" } Text { anchors.baseline: text1.baseline; anchors.top: text1.top; } }"
<< "QML Text (file::2:47) Baseline anchor can't be used in conjunction with top, bottom, or vcenter anchors.";
foreach (const QString &side, QStringList() << "top" << "bottom" << "baseline") {
QTest::newRow("V - anchor to H")
<< QString("Rectangle { Rectangle { anchors.%1: parent.left } }").arg(side)
<< "QML Rectangle (file::2:13) Can't anchor a vertical edge to a horizontal edge.";
QTest::newRow("V - anchor to non parent/sibling")
<< QString("Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.%1: rect.%1 } }").arg(side)
<< "QML Rectangle (file::2:45) Can't anchor to an item that isn't a parent or sibling.";
QTest::newRow("V - anchor to self")
<< QString("Rectangle { id: rect; anchors.%1: rect.%1 }").arg(side)
<< "QML Rectangle (file::2:1) Can't anchor item to self.";
}
QTest::newRow("centerIn - anchor to non parent/sibling")
<< "Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.centerIn: rect} }"
<< "QML Rectangle (file::2:45) Can't anchor to an item that isn't a parent or sibling.";
QTest::newRow("fill - anchor to non parent/sibling")
<< "Rectangle { Item { Rectangle { id: rect } } Rectangle { anchors.fill: rect} }"
<< "QML Rectangle (file::2:45) Can't anchor to an item that isn't a parent or sibling.";
}
void tst_anchors::reset()
{
QFETCH(QString, side);
QFETCH(QmlGraphicsAnchorLine::AnchorLine, anchorLine);
QFETCH(QmlGraphicsAnchors::UsedAnchor, usedAnchor);
QmlGraphicsItem *baseItem = new QmlGraphicsItem;
QmlGraphicsAnchorLine anchor;
anchor.item = baseItem;
anchor.anchorLine = anchorLine;
QmlGraphicsItem *item = new QmlGraphicsItem;
const QMetaObject *meta = item->anchors()->metaObject();
QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
QVERIFY(p.write(item->anchors(), qVariantFromValue(anchor)));
QCOMPARE(item->anchors()->usedAnchors().testFlag(usedAnchor), true);
QVERIFY(p.reset(item->anchors()));
QCOMPARE(item->anchors()->usedAnchors().testFlag(usedAnchor), false);
delete item;
delete baseItem;
}
void tst_anchors::reset_data()
{
QTest::addColumn<QString>("side");
QTest::addColumn<QmlGraphicsAnchorLine::AnchorLine>("anchorLine");
QTest::addColumn<QmlGraphicsAnchors::UsedAnchor>("usedAnchor");
QTest::newRow("left") << "left" << QmlGraphicsAnchorLine::Left << QmlGraphicsAnchors::HasLeftAnchor;
QTest::newRow("top") << "top" << QmlGraphicsAnchorLine::Top << QmlGraphicsAnchors::HasTopAnchor;
QTest::newRow("right") << "right" << QmlGraphicsAnchorLine::Right << QmlGraphicsAnchors::HasRightAnchor;
QTest::newRow("bottom") << "bottom" << QmlGraphicsAnchorLine::Bottom << QmlGraphicsAnchors::HasBottomAnchor;
QTest::newRow("hcenter") << "horizontalCenter" << QmlGraphicsAnchorLine::HCenter << QmlGraphicsAnchors::HasHCenterAnchor;
QTest::newRow("vcenter") << "verticalCenter" << QmlGraphicsAnchorLine::VCenter << QmlGraphicsAnchors::HasVCenterAnchor;
QTest::newRow("baseline") << "baseline" << QmlGraphicsAnchorLine::Baseline << QmlGraphicsAnchors::HasBaselineAnchor;
}
void tst_anchors::resetConvenience()
{
QmlGraphicsItem *baseItem = new QmlGraphicsItem;
QmlGraphicsItem *item = new QmlGraphicsItem;
//fill
item->anchors()->setFill(baseItem);
QVERIFY(item->anchors()->fill() == baseItem);
item->anchors()->resetFill();
QVERIFY(item->anchors()->fill() == 0);
//centerIn
item->anchors()->setCenterIn(baseItem);
QVERIFY(item->anchors()->centerIn() == baseItem);
item->anchors()->resetCenterIn();
QVERIFY(item->anchors()->centerIn() == 0);
delete item;
delete baseItem;
}
void tst_anchors::nullItem()
{
QFETCH(QString, side);
QmlGraphicsAnchorLine anchor;
QmlGraphicsItem *item = new QmlGraphicsItem;
const QMetaObject *meta = item->anchors()->metaObject();
QMetaProperty p = meta->property(meta->indexOfProperty(side.toUtf8().constData()));
QTest::ignoreMessage(QtWarningMsg, "QML Item (unknown location) Can't anchor to a null item.");
QVERIFY(p.write(item->anchors(), qVariantFromValue(anchor)));
delete item;
}
void tst_anchors::nullItem_data()
{
QTest::addColumn<QString>("side");
QTest::newRow("left") << "left";
QTest::newRow("top") << "top";
QTest::newRow("right") << "right";
QTest::newRow("bottom") << "bottom";
QTest::newRow("hcenter") << "horizontalCenter";
QTest::newRow("vcenter") << "verticalCenter";
QTest::newRow("baseline") << "baseline";
}
void tst_anchors::crash1()
{
QmlView *view = new QmlView;
view->setUrl(QUrl::fromLocalFile(SRCDIR "/data/crash1.qml"));
QString expect = "QML Text (" + view->url().toString() + ":4:5" + ") Possible anchor loop detected on fill.";
QTest::ignoreMessage(QtWarningMsg, expect.toLatin1());
QTest::ignoreMessage(QtWarningMsg, expect.toLatin1()); // XXX ideally, should be one message
view->execute();
qApp->processEvents();
delete view;
}
void tst_anchors::fill()
{
QmlView *view = new QmlView;
view->setUrl(QUrl::fromLocalFile(SRCDIR "/data/fill.qml"));
view->execute();
qApp->processEvents();
QmlGraphicsRectangle* rect = findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("filler"));
QCOMPARE(rect->x(), 0.0 + 10.0);
QCOMPARE(rect->y(), 0.0 + 30.0);
QCOMPARE(rect->width(), 200.0 - 10.0 - 20.0);
QCOMPARE(rect->height(), 200.0 - 30.0 - 40.0);
//Alter Offsets (QTBUG-6631)
rect->anchors()->setLeftMargin(20.0);
rect->anchors()->setRightMargin(0.0);
rect->anchors()->setBottomMargin(0.0);
rect->anchors()->setTopMargin(10.0);
QCOMPARE(rect->x(), 0.0 + 20.0);
QCOMPARE(rect->y(), 0.0 + 10.0);
QCOMPARE(rect->width(), 200.0 - 20.0);
QCOMPARE(rect->height(), 200.0 - 10.0);
delete view;
}
void tst_anchors::centerIn()
{
QmlView *view = new QmlView;
view->setUrl(QUrl::fromLocalFile(SRCDIR "/data/centerin.qml"));
view->execute();
qApp->processEvents();
QmlGraphicsRectangle* rect = findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("centered"));
QCOMPARE(rect->x(), 75.0 + 10);
QCOMPARE(rect->y(), 75.0 + 30);
//Alter Offsets (QTBUG-6631)
rect->anchors()->setHorizontalCenterOffset(-20.0);
rect->anchors()->setVerticalCenterOffset(-10.0);
QCOMPARE(rect->x(), 75.0 - 20.0);
QCOMPARE(rect->y(), 75.0 - 10.0);
delete view;
}
void tst_anchors::margins()
{
QmlView *view = new QmlView;
view->setUrl(QUrl::fromLocalFile(SRCDIR "/data/margins.qml"));
view->execute();
qApp->processEvents();
QmlGraphicsRectangle* rect = findItem<QmlGraphicsRectangle>(view->root(), QLatin1String("filler"));
QCOMPARE(rect->x(), 5.0);
QCOMPARE(rect->y(), 6.0);
QCOMPARE(rect->width(), 200.0 - 5.0 - 10.0);
QCOMPARE(rect->height(), 200.0 - 6.0 - 10.0);
rect->anchors()->setTopMargin(0.0);
rect->anchors()->setMargins(20.0);
QCOMPARE(rect->x(), 5.0);
QCOMPARE(rect->y(), 20.0);
QCOMPARE(rect->width(), 200.0 - 5.0 - 20.0);
QCOMPARE(rect->height(), 200.0 - 20.0 - 20.0);
delete view;
}
QTEST_MAIN(tst_anchors)
#include "tst_qmlgraphicsanchors.moc"
|