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 engine.setLocation(QPoint(1220*16, 1220*16));
98 QCOMPARE(fetchImageSpy.count(), 6*4);
99 fetchImageSpy.clear();
101 engine.setLocation(QPoint((1220+TILE_SIZE_X)*16, (1220+TILE_SIZE_Y)*16));
103 QCOMPARE(fetchImageSpy.count(), 9);
104 fetchImageSpy.clear();
107 void TestMapEngine::setLocationRemovedTilesCount()
110 engine.viewResized(QSize(800, 480));
112 const int maxItemsCount = 40;
114 engine.setLocation(QPoint(1220*16, 1220*16));
116 engine.setLocation(QPoint(2220*16, 2220*16));
118 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
120 engine.setLocation(QPoint(520*16, 2220*16));
122 engine.setLocation(QPoint(2220*16, 520*16));
125 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
128 void TestMapEngine::zoomInRemovedTilesCount()
131 engine.viewResized(QSize(800, 480));
133 const int maxItemsCount = 40;
135 engine.setLocation(QPoint(1220*16, 1220*16));
138 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
140 engine.setLocation(QPoint(520*16, 2220*16));
142 engine.setLocation(QPoint(2220*16, 520*16));
145 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
148 void TestMapEngine::zoomOutRemovedTilesCount()
151 engine.viewResized(QSize(800, 480));
153 const int maxItemsCount = 40;
155 engine.setLocation(QPoint(1220*16, 1220.23*16));
157 engine.setLocation(QPoint(2220*16, 2220.23*16));
159 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
161 engine.setLocation(QPoint(520*16, 2220*16));
163 engine.setLocation(QPoint(2220*16, 520*16));
166 QVERIFY(engine.scene()->items().count() <= maxItemsCount);
169 void TestMapEngine::usingLastLocation()
171 // Create mapengine and start monitoring zoomLevelChanged-signal
172 MapEngine *mapengine = new MapEngine;
173 QSignalSpy mapEngineSpy(mapengine, SIGNAL(zoomLevelChanged(int)));
174 QVERIFY (mapEngineSpy.isValid());
175 QCOMPARE (mapEngineSpy.count(), 0);
177 // Write new zoomlevel and location to settings
178 QSettings settings(DIRECTORY_NAME, FILE_NAME);
179 settings.setValue(MAP_LAST_ZOOMLEVEL, DEFAULT_TEST_ZOOMLEVEL);
180 settings.setValue(MAP_LAST_POSITION,
181 mapengine->convertSceneCoordinateToLatLon(DEFAULT_TEST_ZOOMLEVEL,
182 DEFAULT_TEST_SCENECOORDINATE));
184 // Call mapengines init() and verify that signal will be send
186 QCOMPARE (mapEngineSpy.count(), 1);
188 // Remove zoomlevel and location from settings, call init() again and check that
189 // signals is send again
190 settings.remove(MAP_LAST_ZOOMLEVEL);
191 settings.remove(MAP_LAST_POSITION);
193 QCOMPARE(mapEngineSpy.count(), 2);
195 // Check parameters of sended signals
196 QList<QVariant> parameters = mapEngineSpy.takeFirst();
197 QVERIFY(parameters.at(0).toInt() == DEFAULT_TEST_ZOOMLEVEL);
199 parameters = mapEngineSpy.takeFirst();
200 QVERIFY(parameters.at(0).toInt() == DEFAULT_START_ZOOM_LEVEL);
202 // Call set location with know parameter to get location changed
203 // Store new location and zoomlevel to settings
204 mapengine->setLocation(NEW_TEST_SCENECOORDINATE);
207 // Read settings and verify that zoomlevel is correct
208 MapEngine testengine;// = new mapEngine;
209 QPointF LatLonLocation =
210 settings.value(MAP_LAST_POSITION, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toPointF();
211 QCOMPARE(LatLonLocation, testengine.convertSceneCoordinateToLatLon(DEFAULT_TEST_ZOOMLEVEL,
212 NEW_TEST_SCENECOORDINATE));
215 QTEST_MAIN(TestMapEngine)
216 #include "testmapengine.moc"