Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / geometry / tst_geometry.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the Qt scene graph research project.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtCore/QString>
43 #include <QtTest/QtTest>
44
45 #include <QtQuick/qsggeometry.h>
46
47 class GeometryTest : public QObject
48 {
49     Q_OBJECT
50
51 public:
52
53 private Q_SLOTS:
54     void testPoint2D();
55     void testTexturedPoint2D();
56     void testCustomGeometry();
57
58 private:
59 };
60
61 void GeometryTest::testPoint2D()
62 {
63     QSGGeometry geometry(QSGGeometry::defaultAttributes_Point2D(), 4, 0);
64
65     QCOMPARE(geometry.attributeCount(), 1);
66     QCOMPARE(geometry.sizeOfVertex(), (int) sizeof(float) * 2);
67     QCOMPARE(geometry.vertexCount(), 4);
68     QCOMPARE(geometry.indexCount(), 0);
69     QVERIFY(geometry.indexData() == 0);
70
71     QSGGeometry::updateRectGeometry(&geometry, QRectF(1, 2, 3, 4));
72
73     QSGGeometry::Point2D *pts = geometry.vertexDataAsPoint2D();
74     QVERIFY(pts != 0);
75
76     QCOMPARE(pts[0].x, (float) 1);
77     QCOMPARE(pts[0].y, (float) 2);
78     QCOMPARE(pts[3].x, (float) 4);
79     QCOMPARE(pts[3].y, (float) 6);
80
81     // Verify that resize gives me enough allocated data without crashing...
82     geometry.allocate(100, 100);
83     pts = geometry.vertexDataAsPoint2D();
84     quint16 *is = geometry.indexDataAsUShort();
85     for (int i=0; i<100; ++i) {
86         pts[i].x = i;
87         pts[i].y = i + 100;
88         is[i] = i;
89     }
90     QVERIFY(true);
91 }
92
93
94 void GeometryTest::testTexturedPoint2D()
95 {
96     QSGGeometry geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4, 0);
97
98     QCOMPARE(geometry.attributeCount(), 2);
99     QCOMPARE(geometry.sizeOfVertex(), (int) sizeof(float) * 4);
100     QCOMPARE(geometry.vertexCount(), 4);
101     QCOMPARE(geometry.indexCount(), 0);
102     QVERIFY(geometry.indexData() == 0);
103
104     QSGGeometry::updateTexturedRectGeometry(&geometry, QRectF(1, 2, 3, 4), QRectF(5, 6, 7, 8));
105
106     QSGGeometry::TexturedPoint2D *pts = geometry.vertexDataAsTexturedPoint2D();
107     QVERIFY(pts != 0);
108
109     QCOMPARE(pts[0].x, (float) 1);
110     QCOMPARE(pts[0].y, (float) 2);
111     QCOMPARE(pts[0].tx, (float) 5);
112     QCOMPARE(pts[0].ty, (float) 6);
113
114     QCOMPARE(pts[3].x, (float) 4);
115     QCOMPARE(pts[3].y, (float) 6);
116     QCOMPARE(pts[3].tx, (float) 12);
117     QCOMPARE(pts[3].ty, (float) 14);
118
119     // Verify that resize gives me enough allocated data without crashing...
120     geometry.allocate(100, 100);
121     pts = geometry.vertexDataAsTexturedPoint2D();
122     quint16 *is = geometry.indexDataAsUShort();
123     for (int i=0; i<100; ++i) {
124         pts[i].x = i;
125         pts[i].y = i + 100;
126         pts[i].tx = i + 200;
127         pts[i].ty = i + 300;
128         is[i] = i;
129     }
130     QVERIFY(true);
131 }
132
133 void GeometryTest::testCustomGeometry()
134 {
135     struct V {
136         float x, y;
137         unsigned char r, g, b, a;
138         float v1, v2, v3, v4;
139     };
140
141     static QSGGeometry::Attribute attributes[] = {
142         { 0, 2, GL_FLOAT },
143         { 1, 4, GL_UNSIGNED_BYTE },
144         { 2, 4, GL_FLOAT },
145     };
146     static QSGGeometry::AttributeSet set = { 4, 6 * sizeof(float) + 4 * sizeof(unsigned char), attributes };
147
148     QSGGeometry geometry(set, 1000, 4000);
149
150     // Verify that space has been allocated.
151     quint16 *ii = geometry.indexDataAsUShort();
152     for (int i=0; i<geometry.indexCount(); ++i) {
153         ii[i] = i;
154     }
155
156     V *v = (V *) geometry.vertexData();
157     for (int i=0; i<geometry.vertexCount(); ++i) {
158         v[i].x = 0;
159         v[i].y = 1;
160         v[i].r = 2;
161         v[i].g = 3;
162         v[i].b = 4;
163         v[i].a = 5;
164         v[i].v1 = 6;
165         v[i].v2 = 7;
166         v[i].v3 = 8;
167         v[i].v4 = 9;
168     }
169
170     // Verify the data's integrity
171     for (int i=0; i<4000; ++i)
172         QCOMPARE(ii[i], (quint16) i);
173     for (int i=0; i<1000; ++i)
174         QVERIFY(v[i].v1 == 6);
175
176 }
177
178
179 QTEST_MAIN(GeometryTest);
180
181 #include "tst_geometry.moc"