655b2541cb64cd676a5507e6b2cb3e204ec586d6
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qdeclarativepath / tst_qdeclarativepath.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <QtQuick/private/qdeclarativepath_p.h>
46
47 #include "../../shared/util.h"
48
49 class tst_QDeclarativePath : public QDeclarativeDataTest
50 {
51     Q_OBJECT
52 public:
53     tst_QDeclarativePath() {}
54
55 private slots:
56     void arc();
57     void catmullromCurve();
58     void closedCatmullromCurve();
59     void svg();
60 };
61
62 void tst_QDeclarativePath::arc()
63 {
64     QDeclarativeEngine engine;
65     QDeclarativeComponent c(&engine, testFileUrl("arc.qml"));
66     QDeclarativePath *obj = qobject_cast<QDeclarativePath*>(c.create());
67     QVERIFY(obj != 0);
68
69     QCOMPARE(obj->startX(), 0.);
70     QCOMPARE(obj->startY(), 0.);
71
72     QDeclarativeListReference list(obj, "pathElements");
73     QCOMPARE(list.count(), 1);
74
75     QDeclarativePathArc* arc = qobject_cast<QDeclarativePathArc*>(list.at(0));
76     QVERIFY(arc != 0);
77     QCOMPARE(arc->x(), 100.);
78     QCOMPARE(arc->y(), 100.);
79     QCOMPARE(arc->radiusX(), 100.);
80     QCOMPARE(arc->radiusY(), 100.);
81     QCOMPARE(arc->useLargeArc(), false);
82     QCOMPARE(arc->direction(), QDeclarativePathArc::Clockwise);
83
84     QPainterPath path = obj->path();
85     QVERIFY(path != QPainterPath());
86
87     QPointF pos = obj->pointAt(0);
88     QCOMPARE(pos, QPointF(0,0));
89     pos = obj->pointAt(.25);
90     QCOMPARE(pos.toPoint(), QPoint(39,8));  //fuzzy compare
91     pos = obj->pointAt(.75);
92     QCOMPARE(pos.toPoint(), QPoint(92,61)); //fuzzy compare
93     pos = obj->pointAt(1);
94     QCOMPARE(pos, QPointF(100,100));
95 }
96
97 void tst_QDeclarativePath::catmullromCurve()
98 {
99     QDeclarativeEngine engine;
100     QDeclarativeComponent c(&engine, testFileUrl("curve.qml"));
101     QDeclarativePath *obj = qobject_cast<QDeclarativePath*>(c.create());
102     QVERIFY(obj != 0);
103
104     QCOMPARE(obj->startX(), 0.);
105     QCOMPARE(obj->startY(), 0.);
106
107     QDeclarativeListReference list(obj, "pathElements");
108     QCOMPARE(list.count(), 3);
109
110     QDeclarativePathCatmullRomCurve* curve = qobject_cast<QDeclarativePathCatmullRomCurve*>(list.at(0));
111     QVERIFY(curve != 0);
112     QCOMPARE(curve->x(), 100.);
113     QCOMPARE(curve->y(), 50.);
114
115     curve = qobject_cast<QDeclarativePathCatmullRomCurve*>(list.at(2));
116     QVERIFY(curve != 0);
117     QCOMPARE(curve->x(), 100.);
118     QCOMPARE(curve->y(), 150.);
119
120     QPainterPath path = obj->path();
121     QVERIFY(path != QPainterPath());
122
123     QPointF pos = obj->pointAt(0);
124     QCOMPARE(pos, QPointF(0,0));
125     pos = obj->pointAt(.25);
126     QCOMPARE(pos.toPoint(), QPoint(63,26));  //fuzzy compare
127     pos = obj->pointAt(.75);
128     QCOMPARE(pos.toPoint(), QPoint(51,105)); //fuzzy compare
129     pos = obj->pointAt(1);
130     QCOMPARE(pos, QPointF(100,150));
131 }
132
133 void tst_QDeclarativePath::closedCatmullromCurve()
134 {
135     QDeclarativeEngine engine;
136     QDeclarativeComponent c(&engine, testFileUrl("closedcurve.qml"));
137     QDeclarativePath *obj = qobject_cast<QDeclarativePath*>(c.create());
138     QVERIFY(obj != 0);
139
140     QCOMPARE(obj->startX(), 50.);
141     QCOMPARE(obj->startY(), 50.);
142
143     QDeclarativeListReference list(obj, "pathElements");
144     QCOMPARE(list.count(), 3);
145
146     QDeclarativePathCatmullRomCurve* curve = qobject_cast<QDeclarativePathCatmullRomCurve*>(list.at(2));
147     QVERIFY(curve != 0);
148     QCOMPARE(curve->x(), 50.);
149     QCOMPARE(curve->y(), 50.);
150
151     QVERIFY(obj->isClosed());
152
153     QPainterPath path = obj->path();
154     QVERIFY(path != QPainterPath());
155
156     QPointF pos = obj->pointAt(0);
157     QCOMPARE(pos, QPointF(50,50));
158     pos = obj->pointAt(.1);
159     QCOMPARE(pos.toPoint(), QPoint(67,56));  //fuzzy compare
160     pos = obj->pointAt(.75);
161     QCOMPARE(pos.toPoint(), QPoint(44,116)); //fuzzy compare
162     pos = obj->pointAt(1);
163     QCOMPARE(pos, QPointF(50,50));
164 }
165
166 void tst_QDeclarativePath::svg()
167 {
168     QDeclarativeEngine engine;
169     QDeclarativeComponent c(&engine, testFileUrl("svg.qml"));
170     QDeclarativePath *obj = qobject_cast<QDeclarativePath*>(c.create());
171     QVERIFY(obj != 0);
172
173     QCOMPARE(obj->startX(), 0.);
174     QCOMPARE(obj->startY(), 0.);
175
176     QDeclarativeListReference list(obj, "pathElements");
177     QCOMPARE(list.count(), 1);
178
179     QDeclarativePathSvg* svg = qobject_cast<QDeclarativePathSvg*>(list.at(0));
180     QVERIFY(svg != 0);
181     QCOMPARE(svg->path(), QLatin1String("M200,300 Q400,50 600,300 T1000,300"));
182
183     QPainterPath path = obj->path();
184     QVERIFY(path != QPainterPath());
185
186     QPointF pos = obj->pointAt(0);
187     QCOMPARE(pos, QPointF(200,300));
188     pos = obj->pointAt(.25);
189     QCOMPARE(pos.toPoint(), QPoint(400,175));  //fuzzy compare
190     pos = obj->pointAt(.75);
191     QCOMPARE(pos.toPoint(), QPoint(800,425)); //fuzzy compare
192     pos = obj->pointAt(1);
193     QCOMPARE(pos, QPointF(1000,300));
194 }
195
196
197 QTEST_MAIN(tst_QDeclarativePath)
198
199 #include "tst_qdeclarativepath.moc"