d2d5c044fa22ebc92929f927978903203c96009a
[situare] / tests / map / gpslocationitem / testgpslocationitem.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QtTest/QtTest>
23
24 #include "map/mapcommon.h"
25
26 #include "map/gpslocationitem.h"
27
28 class TestGPSLocationItem: public QObject
29 {
30     Q_OBJECT
31
32 private slots:
33     void constructor();
34     void childPixmap();
35     void enable();
36     void updateItem();
37     void updateItem_data();
38 };
39
40 void TestGPSLocationItem::constructor()
41 {
42     GPSLocationItem item;
43
44     // position should be UNDEFINED
45     QCOMPARE(item.pos(), QPointF(UNDEFINED, UNDEFINED));
46
47     // zValue should be set
48     QCOMPARE(item.zValue(), static_cast<qreal>(OWN_LOCATION_ICON_Z_LEVEL));
49
50     // by default the item should be hidden
51     QVERIFY(item.isVisible() == false);
52 }
53
54 /**
55 * The actual GPS location red spot is in a child QGraphicsPixmapItem
56 */
57 void TestGPSLocationItem::childPixmap()
58 {
59     GPSLocationItem item;
60
61     // there should be one QGraphicsPixmapItem child item
62     QList<QGraphicsItem *> childs = item.childItems();
63     QVERIFY(childs.count() == 1);
64     QGraphicsPixmapItem *childPixmap = dynamic_cast<QGraphicsPixmapItem *>(childs.at(0));
65     QVERIFY(childPixmap);
66
67     // pixmap should be set
68     QVERIFY(!childPixmap->pixmap().isNull());
69
70     // pixmap offset should be set based on pixmap
71     QCOMPARE(childPixmap->offset(),
72              QPointF(-childPixmap->pixmap().width() / 2, -childPixmap->pixmap().height() / 2));
73
74     // ItemIgnoresTransformations flag should be set
75     QVERIFY(childPixmap->flags() & QGraphicsItem::ItemIgnoresTransformations);
76 }
77
78 /**
79 * When setEnabled(true) is called, the item should become visible only after the first location
80 * update from the GPS is received
81 */
82 void TestGPSLocationItem::enable()
83 {
84     const qreal SCENE_RESOLUTION = 0.252006;
85     const qreal ACCURACY_100M = 100;
86     const QPointF POSITION_IN_SCENE = QPointF(1000, 1000);
87
88     // create item and update it's position so pixmap is set
89     GPSLocationItem item;
90
91     // item should not be visible because it's location is not yet set
92     QCOMPARE(item.isVisible(), false);
93
94     // setting the location when item is not set enabled should not make the item visible
95     item.updateItem(POSITION_IN_SCENE, ACCURACY_100M, SCENE_RESOLUTION);
96     QCOMPARE(item.isVisible(), false);
97
98     // setting the item enabled should not make the item visible before first location update
99     // is received
100     item.setEnabled(true);
101     QCOMPARE(item.isVisible(), false);
102
103     // item should become visible after the first location update
104     item.updateItem(POSITION_IN_SCENE, ACCURACY_100M, SCENE_RESOLUTION);
105     QCOMPARE(item.isVisible(), true);
106 }
107
108 /**
109 * Item's position and accuracy ring diameter should be updated. Because accuracy ring diameter
110 * is changed, the bounding rect should also be changed.
111 */
112 void TestGPSLocationItem::updateItem()
113 {
114     QFETCH(QPointF, position);
115     QFETCH(qreal, accuracy);
116     QFETCH(qreal, sceneResolution);
117     QFETCH(QPointF, expectedPosition);
118     QFETCH(QRectF, expectedRect);
119
120     GPSLocationItem item;
121     item.updateItem(position, accuracy, sceneResolution);
122
123     QCOMPARE(item.pos(), expectedPosition);
124     QCOMPARE(item.boundingRect(), expectedRect);
125 }
126
127 void TestGPSLocationItem::updateItem_data()
128 {
129     const qreal PEN_WIDTH = 1;
130
131     const QPointF POSITION1 = QPointF(1000, 1000);
132     const QPointF POSITION2 = QPointF(2000, 4000);
133
134     const qreal ACCURACY_100M = 100;
135     const qreal ACCURACY_15M = 15;
136
137     const qreal SCENE_RESOLUTION1 = 0.250000;
138     const qreal SCENE_RESOLUTION2 = 0.500000;
139
140     QTest::addColumn<QPointF>("position");
141     QTest::addColumn<qreal>("accuracy");
142     QTest::addColumn<qreal>("sceneResolution");
143     QTest::addColumn<QPointF>("expectedPosition");
144     QTest::addColumn<QRectF>("expectedRect");
145
146     qreal radius =  ACCURACY_100M / SCENE_RESOLUTION1;
147
148     QRectF rect = QRectF(-radius - PEN_WIDTH / 2,
149                          -radius - PEN_WIDTH / 2,
150                          2 * radius + PEN_WIDTH,
151                          2 * radius + PEN_WIDTH);
152
153     QTest::newRow("1st test") << POSITION1
154                               << ACCURACY_100M
155                               << SCENE_RESOLUTION1
156                               << POSITION1
157                               << rect;
158
159     QTest::newRow("change only position") << POSITION2
160                                           << ACCURACY_100M
161                                           << SCENE_RESOLUTION1
162                                           << POSITION2
163                                           << rect;
164
165     radius =  ACCURACY_15M / SCENE_RESOLUTION1;
166
167     rect = QRectF(-radius - PEN_WIDTH / 2,
168                   -radius - PEN_WIDTH / 2,
169                   2 * radius + PEN_WIDTH,
170                   2 * radius + PEN_WIDTH);
171
172     QTest::newRow("change only accuracy") << POSITION1
173                                           << ACCURACY_15M
174                                           << SCENE_RESOLUTION1
175                                           << POSITION1
176                                           << rect;
177
178     radius =  ACCURACY_100M / SCENE_RESOLUTION2;
179
180     rect = QRectF(-radius - PEN_WIDTH / 2,
181                   -radius - PEN_WIDTH / 2,
182                   2 * radius + PEN_WIDTH,
183                   2 * radius + PEN_WIDTH);
184
185     QTest::newRow("change only scene resolution") << POSITION1
186                                                   << ACCURACY_100M
187                                                   << SCENE_RESOLUTION2
188                                                   << POSITION1
189                                                   << rect;
190 }
191
192 QTEST_MAIN(TestGPSLocationItem)
193
194 #include "testgpslocationitem.moc"