2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
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.
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.
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,
22 #include <QGraphicsScene>
23 #include <QtTest/QtTest>
26 #include "map/mapcommon.h"
27 #include "map/mapengine.h"
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);
33 class TestMapEngine: public QObject
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();
49 * @brief Test converting tile numbers to scene coordinates
51 * Different zoom levels are also tested
53 void TestMapEngine::convertTileNumberToSceneCoordinate()
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));
61 * @brief Test data for converting latitude and longitude coordinates to scene coordinates
63 void TestMapEngine::convertLatLonToSceneCoordinate_data()
65 QTest::addColumn<QPointF>("coordinate");
66 QTest::addColumn<QPoint>("result");
68 QTest::newRow("top left") << QPointF(MIN_LONGITUDE, MAX_LATITUDE) << QPoint(0, 0);
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);
76 * @brief Test converting real world cordinates to scene coordinates
79 void TestMapEngine::convertLatLonToSceneCoordinate()
81 QFETCH(QPointF, coordinate);
82 QFETCH(QPoint, result);
84 QCOMPARE(MapEngine::convertLatLonToSceneCoordinate(coordinate), result);
87 void TestMapEngine::setLocationNewTilesCount()
90 engine.viewResized(QSize(800, 480));
92 QSignalSpy fetchImageSpy(&engine, SIGNAL(fetchImage(int,int,int)));
94 fetchImageSpy.clear();
96 // first test, scene is empty so all tiles should be downloaded
97 engine.setLocation(QPoint(1220*16, 1220*16));
99 QCOMPARE(fetchImageSpy.count(), 5*4);
100 fetchImageSpy.clear();
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));
106 QCOMPARE(fetchImageSpy.count(), 4 + 4);
107 fetchImageSpy.clear();
110 void TestMapEngine::setLocationRemovedTilesCount()
113 engine.viewResized(QSize(800, 480));
115 const int maxItemsCount = 40;
117 engine.setLocation(QPoint(1220*16, 1220*16));
119 engine.setLocation(QPoint(2220*16, 2220*16));
121 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
123 engine.setLocation(QPoint(520*16, 2220*16));
125 engine.setLocation(QPoint(2220*16, 520*16));
128 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
131 void TestMapEngine::zoomInRemovedTilesCount()
134 engine.viewResized(QSize(800, 480));
136 const int maxItemsCount = 40;
138 engine.setLocation(QPoint(1220*16, 1220*16));
141 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
143 engine.setLocation(QPoint(520*16, 2220*16));
145 engine.setLocation(QPoint(2220*16, 520*16));
148 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
151 void TestMapEngine::zoomOutRemovedTilesCount()
154 engine.viewResized(QSize(800, 480));
156 const int maxItemsCount = 40;
158 engine.setLocation(QPoint(1220*16, 1220.23*16));
160 engine.setLocation(QPoint(2220*16, 2220.23*16));
162 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
164 engine.setLocation(QPoint(520*16, 2220*16));
166 engine.setLocation(QPoint(2220*16, 520*16));
169 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
172 void TestMapEngine::usingLastLocation()
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);
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));
187 // Call mapengines init() and verify that signal will be send
189 QCOMPARE (mapEngineSpy.count(), 1);
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);
196 QCOMPARE(mapEngineSpy.count(), 2);
198 // Check parameters of sended signals
199 QList<QVariant> parameters = mapEngineSpy.takeFirst();
200 QVERIFY(parameters.at(0).toInt() == DEFAULT_TEST_ZOOMLEVEL);
202 parameters = mapEngineSpy.takeFirst();
203 QVERIFY(parameters.at(0).toInt() == DEFAULT_START_ZOOM_LEVEL);
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);
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));
218 QTEST_MAIN(TestMapEngine)
219 #include "testmapengine.moc"