6ce6ace577c99cae48b8eace244887f8db587df3
[situare] / tests / testmap / testmapengine / 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 setViewLocation();
32     //void convertLatLonToTile();
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 DUMMY TESTCASE!
54 *
55 * @todo Actual test should be added when there is something to be tested
56 */
57 //void TestMapEngine::setViewLocation()
58 //{
59 //    MapEngine mapEngine;
60 //    mapEngine.setViewLocation(QPointF(25.0000, 65.0000));
61 //}
62
63 /**
64 * @brief Test converting real world cordinates to tile numbers
65 * @todo Implement
66 */
67 //void TestMapEngine::convertLatLonToTile()
68 //{
69 //    QCOMPARE(MapEngine::convertLatLonToTile(7, QPointF(25.5, 65.0)), QPoint(73, 33));
70 //    QCOMPARE(MapEngine::convertLatLonToTile(1, QPointF(25.5, 65.0)), QPoint(1, 0));
71 //    QCOMPARE(MapEngine::convertLatLonToTile(1, QPointF(-190.0, 65.0)), QPoint(UNDEFINED, UNDEFINED));
72 //    QCOMPARE(MapEngine::convertLatLonToTile(100, QPointF(20.0, 65.0)), QPoint(UNDEFINED, UNDEFINED));
73 //}
74
75 void TestMapEngine::calculateRect()
76 {
77     MapEngine engine;
78     engine.viewResized(QSize(800, 480));
79     engine.setZoomLevel(14);
80
81     int zoomFactor = 1 << (MAX_ZOOM_LEVEL - engine.getZoomLevel());
82
83     QRect grid1 = QRect(-1, 0, 6, 4);
84     QCOMPARE(engine.calculateGrid(QPointF(550.23*zoomFactor, 550.23*zoomFactor)), grid1);
85
86     QRect grid2 = QRect(-3, -2, 6, 4);
87     QCOMPARE(engine.calculateGrid(QPointF(0.23*zoomFactor, 0.23*zoomFactor)), grid2);
88
89     QRect grid3 = QRect(1017, 498, 6, 4);
90     QCOMPARE(engine.calculateGrid(QPointF(1020*TILE_SIZE_X*zoomFactor, 500*TILE_SIZE_Y*zoomFactor)), grid3);
91
92     engine.viewResized(QSize(1280, 1024));
93
94     QRect grid4 = QRect(1016, 497, 8, 7);
95     QCOMPARE(engine.calculateGrid(QPointF(1020*TILE_SIZE_X*zoomFactor, 500*TILE_SIZE_Y*zoomFactor)), grid4);
96 }
97
98 void TestMapEngine::setLocation()
99 {
100     MapEngine engine;
101     engine.setZoomLevel(14);
102     engine.viewResized(QSize(800, 480));
103
104     int zoomFactor = 1 << (MAX_ZOOM_LEVEL - engine.getZoomLevel());
105
106     QSignalSpy fetchImageSpy(&engine, SIGNAL(fetchImage(QUrl)));
107     QTest::qWait(1000);
108     fetchImageSpy.clear();
109
110     engine.setLocation(QPointF(1220.23*zoomFactor, 1220.23*zoomFactor));
111     QTest::qWait(1000);
112     QCOMPARE(fetchImageSpy.count(), 6*4);
113     fetchImageSpy.clear();
114
115     //Move one tile right and one down = 9 new tiles
116     engine.setLocation(QPointF((1220.23+TILE_SIZE_X)*zoomFactor, (1220.23+TILE_SIZE_Y)*zoomFactor));
117     QTest::qWait(1000);
118     QCOMPARE(fetchImageSpy.count(), 9);
119     fetchImageSpy.clear();
120 }
121
122 void TestMapEngine::convert()
123 {
124     int zoomFactor = 1 << (MAX_ZOOM_LEVEL - 14);
125
126     QPoint tileNumber = QPoint(1020, 500);
127     QPointF sceneCoordinate = QPointF(tileNumber.x()*TILE_SIZE_X*zoomFactor, tileNumber.y()*TILE_SIZE_Y*zoomFactor);
128     QPoint tile = MapEngine::convertSceneCoordinateToTileNumber(14, sceneCoordinate);
129
130     QCOMPARE(tileNumber, tile);
131 }
132
133 void TestMapEngine::calculateTileGrid()
134 {
135     MapEngine engine;
136
137     engine.viewResized(QSize(800, 480));
138     engine.setZoomLevel(14);
139
140     int zoomFactor = 1 << (MAX_ZOOM_LEVEL - engine.getZoomLevel());
141
142     engine.calculateTileGrid(QPointF(1020*TILE_SIZE_X*zoomFactor, 500*TILE_SIZE_X*zoomFactor));
143 }
144
145 //void TestMapEngine::removeTilesOutOfBounds()
146 //{
147 //    MapEngine engine;
148 //    engine.viewResized(QSize(800, 480));
149 //    engine.setZoomLevel(14);
150 //
151 //    int zoomFactor = 1 << (MAX_ZOOM_LEVEL - engine.getZoomLevel());
152 //
153 //    engine.setLocation(QPointF(1220.23*zoomFactor, 1220.23*zoomFactor));
154 //    qDebug() << "Scene items: " << engine.scene()->items().count();
155 //    QTest::qWait(1000);
156 //    //Move one tile right and one tile down
157 //    engine.setLocation(QPointF((1220.23+TILE_SIZE_X)*zoomFactor, (1220.23+TILE_SIZE_Y)*zoomFactor));
158 //    QCOMPARE(engine.scene()->items().count(), 15);
159 //}
160
161 QTEST_MAIN(TestMapEngine)
162 #include "testmapengine.moc"