Fixed unit tests
[situare] / tests / map / mapengine / testmapengine.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 <QGraphicsScene>
23 #include <QtTest/QtTest>
24
25 #include "map/mapcommon.h"
26 #include "map/mapengine.h"
27
28 class TestMapEngine: public QObject
29 {
30     Q_OBJECT
31
32 private slots:
33     void convertTileNumberToSceneCoordinate();
34     void convertLatLonToSceneCoordinate_data();
35     void convertLatLonToSceneCoordinate();
36     void setLocationNewTilesCount();
37     void setLocationRemovedTilesCount();
38     void zoomOutRemovedTilesCount();
39     void zoomInRemovedTilesCount();
40 };
41
42 /**
43 * @brief Test converting tile numbers to scene coordinates
44 *
45 * Different zoom levels are also tested
46 */
47 void TestMapEngine::convertTileNumberToSceneCoordinate()
48 {
49     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(0,0)), QPoint(0,0));
50     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(1,2)), QPoint(256,512));
51     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(16, QPoint(3,4)), QPoint(3072,4096));
52 }
53
54 /**
55   * @brief Test data for converting latitude and longitude coordinates to scene coordinates
56   */
57 void TestMapEngine::convertLatLonToSceneCoordinate_data()
58 {
59     QTest::addColumn<QPointF>("coordinate");
60     QTest::addColumn<QPoint>("result");
61
62     QTest::newRow("top left") << QPointF(MIN_LONGITUDE, MAX_LATITUDE) << QPoint(0, 0);
63
64     int x = (1 << MAX_MAP_ZOOM_LEVEL) * TILE_SIZE_X;
65     int y = (1 << MAX_MAP_ZOOM_LEVEL) * TILE_SIZE_Y;
66     QTest::newRow("bottom right") << QPointF(MAX_LONGITUDE, MIN_LATITUDE) << QPoint(x, y);
67 }
68
69 /**
70 * @brief Test converting real world cordinates to scene coordinates
71 * @todo Implement
72 */
73 void TestMapEngine::convertLatLonToSceneCoordinate()
74 {
75     QFETCH(QPointF, coordinate);
76     QFETCH(QPoint, result);
77
78     QCOMPARE(MapEngine::convertLatLonToSceneCoordinate(coordinate), result);
79 }
80
81 void TestMapEngine::setLocationNewTilesCount()
82 {
83     MapEngine engine;
84     engine.viewResized(QSize(800, 480));
85
86     QSignalSpy fetchImageSpy(&engine, SIGNAL(fetchImage(int,int,int)));
87     QTest::qWait(1000);
88     fetchImageSpy.clear();
89
90     engine.setLocation(QPoint(1220*16, 1220*16));
91     QTest::qWait(1000);
92     QCOMPARE(fetchImageSpy.count(), 6*4);
93     fetchImageSpy.clear();
94
95     engine.setLocation(QPoint((1220+TILE_SIZE_X)*16, (1220+TILE_SIZE_Y)*16));
96     QTest::qWait(1000);
97     QCOMPARE(fetchImageSpy.count(), 9);
98     fetchImageSpy.clear();
99 }
100
101 void TestMapEngine::setLocationRemovedTilesCount()
102 {
103     MapEngine engine;
104     engine.viewResized(QSize(800, 480));
105
106     const int maxItemsCount = 40;
107
108     engine.setLocation(QPoint(1220*16, 1220*16));
109     QTest::qWait(1000);
110     engine.setLocation(QPoint(2220*16, 2220*16));
111     QTest::qWait(1000);
112     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
113
114     engine.setLocation(QPoint(520*16, 2220*16));
115     QTest::qWait(1000);
116     engine.setLocation(QPoint(2220*16, 520*16));
117     QTest::qWait(1000);
118
119     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
120 }
121
122 void TestMapEngine::zoomInRemovedTilesCount()
123 {
124     MapEngine engine;
125     engine.viewResized(QSize(800, 480));
126
127     const int maxItemsCount = 40;
128
129     engine.setLocation(QPoint(1220*16, 1220*16));
130     QTest::qWait(1000);
131     QTest::qWait(1000);
132     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
133
134     engine.setLocation(QPoint(520*16, 2220*16));
135     QTest::qWait(1000);
136     engine.setLocation(QPoint(2220*16, 520*16));
137     QTest::qWait(1000);
138
139     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
140 }
141
142 void TestMapEngine::zoomOutRemovedTilesCount()
143 {
144     MapEngine engine;
145     engine.viewResized(QSize(800, 480));
146
147     const int maxItemsCount = 40;
148
149     engine.setLocation(QPoint(1220*16, 1220.23*16));
150     QTest::qWait(1000);
151     engine.setLocation(QPoint(2220*16, 2220.23*16));
152     QTest::qWait(1000);
153     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
154
155     engine.setLocation(QPoint(520*16, 2220*16));
156     QTest::qWait(1000);
157     engine.setLocation(QPoint(2220*16, 520*16));
158     QTest::qWait(1000);
159
160     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
161 }
162
163 QTEST_MAIN(TestMapEngine)
164 #include "testmapengine.moc"