1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
43 #include <QtQml/qqmlengine.h>
44 #include <QtQml/qqmlcomponent.h>
45 #include <QtQuick/private/qquickpath_p.h>
47 #include "../../shared/util.h"
49 class tst_QuickPath : public QQmlDataTest
57 void catmullromCurve();
58 void closedCatmullromCurve();
63 void tst_QuickPath::arc()
66 QQmlComponent c(&engine, testFileUrl("arc.qml"));
67 QQuickPath *obj = qobject_cast<QQuickPath*>(c.create());
70 QCOMPARE(obj->startX(), 0.);
71 QCOMPARE(obj->startY(), 0.);
73 QQmlListReference list(obj, "pathElements");
74 QCOMPARE(list.count(), 1);
76 QQuickPathArc* arc = qobject_cast<QQuickPathArc*>(list.at(0));
78 QCOMPARE(arc->x(), 100.);
79 QCOMPARE(arc->y(), 100.);
80 QCOMPARE(arc->radiusX(), 100.);
81 QCOMPARE(arc->radiusY(), 100.);
82 QCOMPARE(arc->useLargeArc(), false);
83 QCOMPARE(arc->direction(), QQuickPathArc::Clockwise);
85 QPainterPath path = obj->path();
86 QVERIFY(path != QPainterPath());
88 QPointF pos = obj->pointAt(0);
89 QCOMPARE(pos, QPointF(0,0));
90 pos = obj->pointAt(.25);
91 QCOMPARE(pos.toPoint(), QPoint(39,8)); //fuzzy compare
92 pos = obj->pointAt(.75);
93 QCOMPARE(pos.toPoint(), QPoint(92,61)); //fuzzy compare
94 pos = obj->pointAt(1);
95 QCOMPARE(pos, QPointF(100,100));
98 void tst_QuickPath::catmullromCurve()
101 QQmlComponent c(&engine, testFileUrl("curve.qml"));
102 QQuickPath *obj = qobject_cast<QQuickPath*>(c.create());
105 QCOMPARE(obj->startX(), 0.);
106 QCOMPARE(obj->startY(), 0.);
108 QQmlListReference list(obj, "pathElements");
109 QCOMPARE(list.count(), 3);
111 QQuickPathCatmullRomCurve* curve = qobject_cast<QQuickPathCatmullRomCurve*>(list.at(0));
113 QCOMPARE(curve->x(), 100.);
114 QCOMPARE(curve->y(), 50.);
116 curve = qobject_cast<QQuickPathCatmullRomCurve*>(list.at(2));
118 QCOMPARE(curve->x(), 100.);
119 QCOMPARE(curve->y(), 150.);
121 QPainterPath path = obj->path();
122 QVERIFY(path != QPainterPath());
124 QPointF pos = obj->pointAt(0);
125 QCOMPARE(pos, QPointF(0,0));
126 pos = obj->pointAt(.25);
127 QCOMPARE(pos.toPoint(), QPoint(63,26)); //fuzzy compare
128 pos = obj->pointAt(.75);
129 QCOMPARE(pos.toPoint(), QPoint(51,105)); //fuzzy compare
130 pos = obj->pointAt(1);
131 QCOMPARE(pos, QPointF(100,150));
134 void tst_QuickPath::closedCatmullromCurve()
137 QQmlComponent c(&engine, testFileUrl("closedcurve.qml"));
138 QQuickPath *obj = qobject_cast<QQuickPath*>(c.create());
141 QCOMPARE(obj->startX(), 50.);
142 QCOMPARE(obj->startY(), 50.);
144 QQmlListReference list(obj, "pathElements");
145 QCOMPARE(list.count(), 3);
147 QQuickPathCatmullRomCurve* curve = qobject_cast<QQuickPathCatmullRomCurve*>(list.at(2));
149 QCOMPARE(curve->x(), 50.);
150 QCOMPARE(curve->y(), 50.);
152 QVERIFY(obj->isClosed());
154 QPainterPath path = obj->path();
155 QVERIFY(path != QPainterPath());
157 QPointF pos = obj->pointAt(0);
158 QCOMPARE(pos, QPointF(50,50));
159 pos = obj->pointAt(.1);
160 QCOMPARE(pos.toPoint(), QPoint(67,56)); //fuzzy compare
161 pos = obj->pointAt(.75);
162 QCOMPARE(pos.toPoint(), QPoint(44,116)); //fuzzy compare
163 pos = obj->pointAt(1);
164 QCOMPARE(pos, QPointF(50,50));
167 void tst_QuickPath::svg()
170 QQmlComponent c(&engine, testFileUrl("svg.qml"));
171 QQuickPath *obj = qobject_cast<QQuickPath*>(c.create());
174 QCOMPARE(obj->startX(), 0.);
175 QCOMPARE(obj->startY(), 0.);
177 QQmlListReference list(obj, "pathElements");
178 QCOMPARE(list.count(), 1);
180 QQuickPathSvg* svg = qobject_cast<QQuickPathSvg*>(list.at(0));
182 QCOMPARE(svg->path(), QLatin1String("M200,300 Q400,50 600,300 T1000,300"));
184 QPainterPath path = obj->path();
185 QVERIFY(path != QPainterPath());
187 QPointF pos = obj->pointAt(0);
188 QCOMPARE(pos, QPointF(200,300));
189 pos = obj->pointAt(.25);
190 QCOMPARE(pos.toPoint(), QPoint(400,175)); //fuzzy compare
191 pos = obj->pointAt(.75);
192 QCOMPARE(pos.toPoint(), QPoint(800,425)); //fuzzy compare
193 pos = obj->pointAt(1);
194 QCOMPARE(pos, QPointF(1000,300));
197 void tst_QuickPath::line()
200 QQmlComponent c1(&engine);
202 "import QtQuick 2.0\n"
204 "startX: 0; startY: 0\n"
205 "PathLine { x: 100; y: 100 }\n"
207 QScopedPointer<QObject> o1(c1.create());
208 QQuickPath *path1 = qobject_cast<QQuickPath *>(o1.data());
211 QQmlComponent c2(&engine);
213 "import QtQuick 2.0\n"
215 "startX: 0; startY: 0\n"
216 "PathLine { x: 50; y: 50 }\n"
217 "PathLine { x: 100; y: 100 }\n"
219 QScopedPointer<QObject> o2(c2.create());
220 QQuickPath *path2 = qobject_cast<QQuickPath *>(o2.data());
223 for (int i = 0; i < 167; ++i) {
226 QPointF p1 = path1->pointAt(t);
227 QCOMPARE(p1.x(), p1.y());
229 QPointF p2 = path2->pointAt(t);
235 QTEST_MAIN(tst_QuickPath)
237 #include "tst_qquickpath.moc"