269e84550888305fea00b7f1c5ea4e95a93b1590
[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 "common.h"
26 #include "map/mapcommon.h"
27 #include "map/mapengine.h"
28
29 const int DEFAULT_TEST_ZOOMLEVEL = 5;
30 const QPoint DEFAULT_TEST_SCENECOORDINATE = QPoint(12345,54321);
31 const QPoint NEW_TEST_SCENECOORDINATE = QPoint(50000,40000);
32
33 class TestMapEngine: public QObject
34 {
35     Q_OBJECT
36
37 private slots:
38     void convertTileNumberToSceneCoordinate();
39     void convertLatLonToSceneCoordinate_data();
40     void convertLatLonToSceneCoordinate();
41     void setLocationNewTilesCount();
42     void setLocationRemovedTilesCount();
43     void zoomOutRemovedTilesCount();
44     void zoomInRemovedTilesCount();
45     void usingLastLocation();
46 };
47
48 /**
49 * @brief Test converting tile numbers to scene coordinates
50 *
51 * Different zoom levels are also tested
52 */
53 void TestMapEngine::convertTileNumberToSceneCoordinate()
54 {
55     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(0,0)), QPoint(0,0));
56     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(1,2)), QPoint(256,512));
57     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(16, QPoint(3,4)), QPoint(3072,4096));
58 }
59
60 /**
61   * @brief Test data for converting latitude and longitude coordinates to scene coordinates
62   */
63 void TestMapEngine::convertLatLonToSceneCoordinate_data()
64 {
65     QTest::addColumn<QPointF>("coordinate");
66     QTest::addColumn<QPoint>("result");
67
68     QTest::newRow("top left") << QPointF(MIN_LONGITUDE, MAX_LATITUDE) << QPoint(0, 0);
69
70     int x = (1 << MAX_MAP_ZOOM_LEVEL) * TILE_SIZE_X;
71     int y = (1 << MAX_MAP_ZOOM_LEVEL) * TILE_SIZE_Y;
72     QTest::newRow("bottom right") << QPointF(MAX_LONGITUDE, MIN_LATITUDE) << QPoint(x, y);
73 }
74
75 /**
76 * @brief Test converting real world cordinates to scene coordinates
77 * @todo Implement
78 */
79 void TestMapEngine::convertLatLonToSceneCoordinate()
80 {
81     QFETCH(QPointF, coordinate);
82     QFETCH(QPoint, result);
83
84     QCOMPARE(MapEngine::convertLatLonToSceneCoordinate(coordinate), result);
85 }
86
87 void TestMapEngine::setLocationNewTilesCount()
88 {
89     MapEngine engine;
90     engine.viewResized(QSize(800, 480));
91
92     QSignalSpy fetchImageSpy(&engine, SIGNAL(fetchImage(int,int,int)));
93     QTest::qWait(1000);
94     fetchImageSpy.clear();
95
96     // first test, scene is empty so all tiles should be downloaded
97     engine.setLocation(QPoint(1220*16, 1220*16));
98     QTest::qWait(1000);
99     QCOMPARE(fetchImageSpy.count(), 5*4);
100     fetchImageSpy.clear();
101
102     // move one tile to east and south, only most right one column and most bottom one row tiles
103     // should be downloaded
104     engine.setLocation(QPoint((1220+TILE_SIZE_X)*16, (1220+TILE_SIZE_Y)*16));
105     QTest::qWait(1000);
106     QCOMPARE(fetchImageSpy.count(), 4 + 4);
107     fetchImageSpy.clear();
108 }
109
110 void TestMapEngine::setLocationRemovedTilesCount()
111 {
112     MapEngine engine;
113     engine.viewResized(QSize(800, 480));
114
115     const int maxItemsCount = 40;
116
117     engine.setLocation(QPoint(1220*16, 1220*16));
118     QTest::qWait(1000);
119     engine.setLocation(QPoint(2220*16, 2220*16));
120     QTest::qWait(1000);
121     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
122
123     engine.setLocation(QPoint(520*16, 2220*16));
124     QTest::qWait(1000);
125     engine.setLocation(QPoint(2220*16, 520*16));
126     QTest::qWait(1000);
127
128     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
129 }
130
131 void TestMapEngine::zoomInRemovedTilesCount()
132 {
133     MapEngine engine;
134     engine.viewResized(QSize(800, 480));
135
136     const int maxItemsCount = 40;
137
138     engine.setLocation(QPoint(1220*16, 1220*16));
139     QTest::qWait(1000);
140     QTest::qWait(1000);
141     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
142
143     engine.setLocation(QPoint(520*16, 2220*16));
144     QTest::qWait(1000);
145     engine.setLocation(QPoint(2220*16, 520*16));
146     QTest::qWait(1000);
147
148     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
149 }
150
151 void TestMapEngine::zoomOutRemovedTilesCount()
152 {
153     MapEngine engine;
154     engine.viewResized(QSize(800, 480));
155
156     const int maxItemsCount = 40;
157
158     engine.setLocation(QPoint(1220*16, 1220.23*16));
159     QTest::qWait(1000);
160     engine.setLocation(QPoint(2220*16, 2220.23*16));
161     QTest::qWait(1000);
162     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
163
164     engine.setLocation(QPoint(520*16, 2220*16));
165     QTest::qWait(1000);
166     engine.setLocation(QPoint(2220*16, 520*16));
167     QTest::qWait(1000);
168
169     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
170 }
171
172 void TestMapEngine::usingLastLocation()
173 {
174     // Create mapengine and start monitoring zoomLevelChanged-signal
175     MapEngine *mapengine = new MapEngine;
176     QSignalSpy mapEngineSpy(mapengine, SIGNAL(zoomLevelChanged(int)));
177     QVERIFY (mapEngineSpy.isValid());
178     QCOMPARE (mapEngineSpy.count(), 0);
179
180     // Write new zoomlevel and location to settings
181     QSettings settings(DIRECTORY_NAME, FILE_NAME);
182     settings.setValue(MAP_LAST_ZOOMLEVEL, DEFAULT_TEST_ZOOMLEVEL);
183     settings.setValue(MAP_LAST_POSITION,
184                       mapengine->convertSceneCoordinateToLatLon(DEFAULT_TEST_ZOOMLEVEL,
185                                                                 DEFAULT_TEST_SCENECOORDINATE));
186
187     // Call mapengines init() and verify that signal will be send
188     mapengine->init();
189     QCOMPARE (mapEngineSpy.count(), 1);
190
191     // Remove zoomlevel and location from settings, call init() again and check that
192     // signals is send again
193     settings.remove(MAP_LAST_ZOOMLEVEL);
194     settings.remove(MAP_LAST_POSITION);
195     mapengine->init();
196     QCOMPARE(mapEngineSpy.count(), 2);
197
198     // Check parameters of sended signals
199     QList<QVariant> parameters = mapEngineSpy.takeFirst();
200     QVERIFY(parameters.at(0).toInt() == DEFAULT_TEST_ZOOMLEVEL);
201
202     parameters = mapEngineSpy.takeFirst();
203     QVERIFY(parameters.at(0).toInt() == DEFAULT_START_ZOOM_LEVEL);
204
205     // Call set location with know parameter to get location changed
206     // Store new location and zoomlevel to settings
207     mapengine->setLocation(NEW_TEST_SCENECOORDINATE);
208     delete mapengine;
209
210     // Read settings and verify that zoomlevel is correct
211     MapEngine testengine;// = new mapEngine;
212     QPointF LatLonLocation =
213             settings.value(MAP_LAST_POSITION, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toPointF();
214     QCOMPARE(LatLonLocation, testengine.convertSceneCoordinateToLatLon(DEFAULT_TEST_ZOOMLEVEL,
215                                                                       NEW_TEST_SCENECOORDINATE));
216 }
217
218 QTEST_MAIN(TestMapEngine)
219 #include "testmapengine.moc"