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