Fixed unit tests, more accurate limits for latitude coordinates.
[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 <QtTest/QtTest>
23
24 #include "map/mapengine.h"
25
26 class TestMapEngine: public QObject
27 {
28     Q_OBJECT
29 private slots:
30     void convertTileNumberToSceneCoordinate();
31     void convertLatLonToSceneCoordinate_data();
32     void convertLatLonToSceneCoordinate();
33     void calculateRect();
34     void setLocation();
35     void removeTilesOutOfBounds();
36     void calculateTileGrid();
37     void convert();
38 };
39
40 /**
41 * @brief Test converting tile numbers to scene coordinates
42 *
43 * Different zoom levels are also tested
44 */
45 void TestMapEngine::convertTileNumberToSceneCoordinate()
46 {
47     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(0,0)), QPoint(0,0));
48     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(18, QPoint(1,2)), QPoint(256,512));
49     QCOMPARE(MapEngine::convertTileNumberToSceneCoordinate(16, QPoint(3,4)), QPoint(3072,4096));
50 }
51
52 /**
53   * @brief Test data for converting latitude and longitude coordinates to scene coordinates
54   */
55 void TestMapEngine::convertLatLonToSceneCoordinate_data()
56 {
57     QTest::addColumn<QPointF>("coordinate");
58     QTest::addColumn<QPoint>("result");
59
60     QTest::newRow("top left") << QPointF(MIN_LONGITUDE, MAX_LATITUDE) << QPoint(0, 0);
61
62     int x = (1 << MAX_MAP_ZOOM_LEVEL) * TILE_SIZE_X;
63     int y = (1 << MAX_MAP_ZOOM_LEVEL) * TILE_SIZE_Y;
64     QTest::newRow("bottom right") << QPointF(MAX_LONGITUDE, MIN_LATITUDE) << QPoint(x, y);
65 }
66
67 /**
68 * @brief Test converting real world cordinates to scene coordinates
69 * @todo Implement
70 */
71 void TestMapEngine::convertLatLonToSceneCoordinate()
72 {
73     QFETCH(QPointF, coordinate);
74     QFETCH(QPoint, result);
75
76     QCOMPARE(MapEngine::convertLatLonToSceneCoordinate(coordinate), result);
77 }
78
79 void TestMapEngine::calculateRect()
80 {
81     QVERIFY(false);
82 //    MapEngine engine;
83 //    engine.viewResized(QSize(800, 480));
84 //    engine.setZoomLevel(14);
85 //
86 //    int zoomFactor = 1 << (MAX_MAP_ZOOM_LEVEL - engine.getZoomLevel());
87 //
88 //    QRect grid1 = QRect(-1, 0, 6, 4);
89 //    QCOMPARE(engine.calculateGrid(QPointF(550.23*zoomFactor, 550.23*zoomFactor)), grid1);
90 //
91 //    QRect grid2 = QRect(-3, -2, 6, 4);
92 //    QCOMPARE(engine.calculateGrid(QPointF(0.23*zoomFactor, 0.23*zoomFactor)), grid2);
93 //
94 //    QRect grid3 = QRect(1017, 498, 6, 4);
95 //    QCOMPARE(engine.calculateGrid(QPointF(1020*TILE_SIZE_X*zoomFactor, 500*TILE_SIZE_Y*zoomFactor)), grid3);
96 //
97 //    engine.viewResized(QSize(1280, 1024));
98 //
99 //    QRect grid4 = QRect(1016, 497, 8, 7);
100 //    QCOMPARE(engine.calculateGrid(QPointF(1020*TILE_SIZE_X*zoomFactor, 500*TILE_SIZE_Y*zoomFactor)), grid4);
101 }
102
103 void TestMapEngine::setLocation()
104 {
105     QVERIFY(false);
106 //    MapEngine engine;
107 //    engine.setZoomLevel(14);
108 //    engine.viewResized(QSize(800, 480));
109 //
110 //    int zoomFactor = 1 << (MAX_MAP_ZOOM_LEVEL - engine.getZoomLevel());
111 //
112 //    QSignalSpy fetchImageSpy(&engine, SIGNAL(fetchImage(QUrl)));
113 //    QTest::qWait(1000);
114 //    fetchImageSpy.clear();
115 //
116 //    engine.setLocation(QPointF(1220.23*zoomFactor, 1220.23*zoomFactor));
117 //    QTest::qWait(1000);
118 //    QCOMPARE(fetchImageSpy.count(), 6*4);
119 //    fetchImageSpy.clear();
120 //
121 //    //Move one tile right and one down = 9 new tiles
122 //    engine.setLocation(QPointF((1220.23+TILE_SIZE_X)*zoomFactor, (1220.23+TILE_SIZE_Y)*zoomFactor));
123 //    QTest::qWait(1000);
124 //    QCOMPARE(fetchImageSpy.count(), 9);
125 //    fetchImageSpy.clear();
126 }
127
128 void TestMapEngine::convert()
129 {
130     int zoomFactor = 1 << (MAX_MAP_ZOOM_LEVEL - 14);
131
132     QPoint tileNumber = QPoint(1020, 500);
133     QPoint sceneCoordinate = QPoint(tileNumber.x()*TILE_SIZE_X*zoomFactor, tileNumber.y()*TILE_SIZE_Y*zoomFactor);
134     QPoint tile = MapEngine::convertSceneCoordinateToTileNumber(14, sceneCoordinate);
135
136     QCOMPARE(tileNumber, tile);
137 }
138
139 void TestMapEngine::calculateTileGrid()
140 {
141     QVERIFY(false);
142 //    MapEngine engine;
143 //
144 //    engine.viewResized(QSize(800, 480));
145 //    engine.setZoomLevel(14);
146 //
147 //    int zoomFactor = 1 << (MAX_MAP_ZOOM_LEVEL - engine.getZoomLevel());
148 //
149 //    engine.calculateTileGrid(QPointF(1020*TILE_SIZE_X*zoomFactor, 500*TILE_SIZE_X*zoomFactor));
150 }
151
152 void TestMapEngine::removeTilesOutOfBounds()
153 {
154     QVERIFY(false);
155 //    MapEngine engine;
156 //    engine.viewResized(QSize(800, 480));
157 //    engine.setZoomLevel(14);
158 //
159 //    int zoomFactor = 1 << (MAX_ZOOM_LEVEL - engine.getZoomLevel());
160 //
161 //    engine.setLocation(QPointF(1220.23*zoomFactor, 1220.23*zoomFactor));
162 //    qDebug() << "Scene items: " << engine.scene()->items().count();
163 //    QTest::qWait(1000);
164 //    //Move one tile right and one tile down
165 //    engine.setLocation(QPointF((1220.23+TILE_SIZE_X)*zoomFactor, (1220.23+TILE_SIZE_Y)*zoomFactor));
166 //    QCOMPARE(engine.scene()->items().count(), 15);
167 }
168
169 QTEST_MAIN(TestMapEngine)
170 #include "testmapengine.moc"