moved test from own test class to TestMapEngine class.
[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     engine.setLocation(QPoint(1220*16, 1220*16));
97     QTest::qWait(1000);
98     QCOMPARE(fetchImageSpy.count(), 6*4);
99     fetchImageSpy.clear();
100
101     engine.setLocation(QPoint((1220+TILE_SIZE_X)*16, (1220+TILE_SIZE_Y)*16));
102     QTest::qWait(1000);
103     QCOMPARE(fetchImageSpy.count(), 9);
104     fetchImageSpy.clear();
105 }
106
107 void TestMapEngine::setLocationRemovedTilesCount()
108 {
109     MapEngine engine;
110     engine.viewResized(QSize(800, 480));
111
112     const int maxItemsCount = 40;
113
114     engine.setLocation(QPoint(1220*16, 1220*16));
115     QTest::qWait(1000);
116     engine.setLocation(QPoint(2220*16, 2220*16));
117     QTest::qWait(1000);
118     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
119
120     engine.setLocation(QPoint(520*16, 2220*16));
121     QTest::qWait(1000);
122     engine.setLocation(QPoint(2220*16, 520*16));
123     QTest::qWait(1000);
124
125     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
126 }
127
128 void TestMapEngine::zoomInRemovedTilesCount()
129 {
130     MapEngine engine;
131     engine.viewResized(QSize(800, 480));
132
133     const int maxItemsCount = 40;
134
135     engine.setLocation(QPoint(1220*16, 1220*16));
136     QTest::qWait(1000);
137     QTest::qWait(1000);
138     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
139
140     engine.setLocation(QPoint(520*16, 2220*16));
141     QTest::qWait(1000);
142     engine.setLocation(QPoint(2220*16, 520*16));
143     QTest::qWait(1000);
144
145     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
146 }
147
148 void TestMapEngine::zoomOutRemovedTilesCount()
149 {
150     MapEngine engine;
151     engine.viewResized(QSize(800, 480));
152
153     const int maxItemsCount = 40;
154
155     engine.setLocation(QPoint(1220*16, 1220.23*16));
156     QTest::qWait(1000);
157     engine.setLocation(QPoint(2220*16, 2220.23*16));
158     QTest::qWait(1000);
159     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
160
161     engine.setLocation(QPoint(520*16, 2220*16));
162     QTest::qWait(1000);
163     engine.setLocation(QPoint(2220*16, 520*16));
164     QTest::qWait(1000);
165
166     QVERIFY(engine.scene()->items().count() <= maxItemsCount);
167 }
168
169 void TestMapEngine::usingLastLocation()
170 {
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);
176
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));
183
184     // Call mapengines init() and verify that signal will be send
185     mapengine->init();
186     QCOMPARE (mapEngineSpy.count(), 1);
187
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);
192     mapengine->init();
193     QCOMPARE(mapEngineSpy.count(), 2);
194
195     // Check parameters of sended signals
196     QList<QVariant> parameters = mapEngineSpy.takeFirst();
197     QVERIFY(parameters.at(0).toInt() == DEFAULT_TEST_ZOOMLEVEL);
198
199     parameters = mapEngineSpy.takeFirst();
200     QVERIFY(parameters.at(0).toInt() == DEFAULT_START_ZOOM_LEVEL);
201
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);
205     delete mapengine;
206
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));
213 }
214
215 QTEST_MAIN(TestMapEngine)
216 #include "testmapengine.moc"