Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / tests / auto / quick / qquickpath / tst_qquickpath.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include <QtQml/qqmlengine.h>
44 #include <QtQml/qqmlcomponent.h>
45 #include <QtQuick/private/qquickpath_p.h>
46
47 #include "../../shared/util.h"
48
49 class tst_QuickPath : public QQmlDataTest
50 {
51     Q_OBJECT
52 public:
53     tst_QuickPath() {}
54
55 private slots:
56     void arc();
57     void catmullromCurve();
58     void closedCatmullromCurve();
59     void svg();
60     void line();
61 };
62
63 void tst_QuickPath::arc()
64 {
65     QQmlEngine engine;
66     QQmlComponent c(&engine, testFileUrl("arc.qml"));
67     QQuickPath *obj = qobject_cast<QQuickPath*>(c.create());
68     QVERIFY(obj != 0);
69
70     QCOMPARE(obj->startX(), 0.);
71     QCOMPARE(obj->startY(), 0.);
72
73     QQmlListReference list(obj, "pathElements");
74     QCOMPARE(list.count(), 1);
75
76     QQuickPathArc* arc = qobject_cast<QQuickPathArc*>(list.at(0));
77     QVERIFY(arc != 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);
84
85     QPainterPath path = obj->path();
86     QVERIFY(path != QPainterPath());
87
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));
96 }
97
98 void tst_QuickPath::catmullromCurve()
99 {
100     QQmlEngine engine;
101     QQmlComponent c(&engine, testFileUrl("curve.qml"));
102     QQuickPath *obj = qobject_cast<QQuickPath*>(c.create());
103     QVERIFY(obj != 0);
104
105     QCOMPARE(obj->startX(), 0.);
106     QCOMPARE(obj->startY(), 0.);
107
108     QQmlListReference list(obj, "pathElements");
109     QCOMPARE(list.count(), 3);
110
111     QQuickPathCatmullRomCurve* curve = qobject_cast<QQuickPathCatmullRomCurve*>(list.at(0));
112     QVERIFY(curve != 0);
113     QCOMPARE(curve->x(), 100.);
114     QCOMPARE(curve->y(), 50.);
115
116     curve = qobject_cast<QQuickPathCatmullRomCurve*>(list.at(2));
117     QVERIFY(curve != 0);
118     QCOMPARE(curve->x(), 100.);
119     QCOMPARE(curve->y(), 150.);
120
121     QPainterPath path = obj->path();
122     QVERIFY(path != QPainterPath());
123
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));
132 }
133
134 void tst_QuickPath::closedCatmullromCurve()
135 {
136     QQmlEngine engine;
137     QQmlComponent c(&engine, testFileUrl("closedcurve.qml"));
138     QQuickPath *obj = qobject_cast<QQuickPath*>(c.create());
139     QVERIFY(obj != 0);
140
141     QCOMPARE(obj->startX(), 50.);
142     QCOMPARE(obj->startY(), 50.);
143
144     QQmlListReference list(obj, "pathElements");
145     QCOMPARE(list.count(), 3);
146
147     QQuickPathCatmullRomCurve* curve = qobject_cast<QQuickPathCatmullRomCurve*>(list.at(2));
148     QVERIFY(curve != 0);
149     QCOMPARE(curve->x(), 50.);
150     QCOMPARE(curve->y(), 50.);
151
152     QVERIFY(obj->isClosed());
153
154     QPainterPath path = obj->path();
155     QVERIFY(path != QPainterPath());
156
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));
165 }
166
167 void tst_QuickPath::svg()
168 {
169     QQmlEngine engine;
170     QQmlComponent c(&engine, testFileUrl("svg.qml"));
171     QQuickPath *obj = qobject_cast<QQuickPath*>(c.create());
172     QVERIFY(obj != 0);
173
174     QCOMPARE(obj->startX(), 0.);
175     QCOMPARE(obj->startY(), 0.);
176
177     QQmlListReference list(obj, "pathElements");
178     QCOMPARE(list.count(), 1);
179
180     QQuickPathSvg* svg = qobject_cast<QQuickPathSvg*>(list.at(0));
181     QVERIFY(svg != 0);
182     QCOMPARE(svg->path(), QLatin1String("M200,300 Q400,50 600,300 T1000,300"));
183
184     QPainterPath path = obj->path();
185     QVERIFY(path != QPainterPath());
186
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));
195 }
196
197 void tst_QuickPath::line()
198 {
199     QQmlEngine engine;
200     QQmlComponent c1(&engine);
201     c1.setData(
202             "import QtQuick 2.0\n"
203             "Path {\n"
204                 "startX: 0; startY: 0\n"
205                 "PathLine { x: 100; y: 100 }\n"
206             "}", QUrl());
207     QScopedPointer<QObject> o1(c1.create());
208     QQuickPath *path1 = qobject_cast<QQuickPath *>(o1.data());
209     QVERIFY(path1);
210
211     QQmlComponent c2(&engine);
212     c2.setData(
213             "import QtQuick 2.0\n"
214             "Path {\n"
215                 "startX: 0; startY: 0\n"
216                 "PathLine { x: 50; y: 50 }\n"
217                 "PathLine { x: 100; y: 100 }\n"
218             "}", QUrl());
219     QScopedPointer<QObject> o2(c2.create());
220     QQuickPath *path2 = qobject_cast<QQuickPath *>(o2.data());
221     QVERIFY(path2);
222
223     for (int i = 0; i < 167; ++i) {
224         qreal t = i / 167.0;
225
226         QPointF p1 = path1->pointAt(t);
227         QCOMPARE(p1.x(), p1.y());
228
229         QPointF p2 = path2->pointAt(t);
230         QCOMPARE(p1, p2);
231     }
232 }
233
234
235 QTEST_MAIN(tst_QuickPath)
236
237 #include "tst_qquickpath.moc"