06221aee863d3463aaab2b95ced8fdfe5c1b4cb4
[situare] / tests / map / mapscene / testmapscene.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 #include <QDebug>
24
25 #include "map/mapscene.h"
26 #include "map/maptile.h"
27 #include "map/mapengine.h"
28
29 class TestMapScene : public QObject
30 {
31     Q_OBJECT
32 private slots:
33     void addMapTile();
34 };
35
36 /**
37 * @brief Test adding map tiles
38 *
39 * Does check that the map tile is found in right coordinates
40 */
41 void TestMapScene::addMapTile()
42 {
43     const int ZOOM_LEVEL_1ST = 18;
44     const int ZOOM_LEVEL_2ND = 16;
45     const QPoint TILE_INDEX_1ST = QPoint(1, 2);
46     const QPoint TILE_INDEX_2ND = QPoint(2, 3);
47
48     MapScene mapScene;
49
50     // First test:
51     // Zoom level is 18, so no stretching should occure
52     // Top-left corner x = 256, y = 512
53     // Bottom-right corner x + 255, y + 255
54     mapScene.setZoomLevel(ZOOM_LEVEL_1ST);
55     mapScene.addTile(ZOOM_LEVEL_1ST, TILE_INDEX_1ST, QPixmap("maptile.png"), ZOOM_LEVEL_1ST);
56
57     // Check that there is only one item in the scene and take a pointer to it
58     QCOMPARE(mapScene.items().count(), 1);
59     MapTile *tile = dynamic_cast<MapTile *>(mapScene.items().at(0));
60     QVERIFY(tile);
61
62     // Check around top-left and bottom-right corners
63     int x = 256;
64     int y = 512;
65     int s = 255; //side length -1
66     QCOMPARE(mapScene.itemAt(x-1, y), (QGraphicsItem *)0);
67     QCOMPARE(mapScene.itemAt(x, y-1), (QGraphicsItem *)0);
68     QCOMPARE(mapScene.itemAt(x, y), dynamic_cast<QGraphicsItem *>(tile));
69     QCOMPARE(mapScene.itemAt(x+s, y+s), dynamic_cast<QGraphicsItem *>(tile));
70     QCOMPARE(mapScene.itemAt(x+s+1, y+s), (QGraphicsItem *)0);
71     QCOMPARE(mapScene.itemAt(x+s, y+s+1), (QGraphicsItem *)0);
72
73     // Second test:
74     // Zoom level is 16, so stretching is also tested
75     // Top-left corner x = 2048, y = 3072
76     // Bottom-right corner x + 1023, y + 1023
77     tile->setZoomLevel(ZOOM_LEVEL_2ND, ZOOM_LEVEL_2ND);
78     tile->setTileNumber(TILE_INDEX_2ND);
79
80     // Check around top-left and bottom-right corners
81     x = 2048;
82     y = 3072;
83     s = 1023; //side length -1
84     QCOMPARE(mapScene.itemAt(x-1, y), (QGraphicsItem *)0);
85     QCOMPARE(mapScene.itemAt(x, y-1), (QGraphicsItem *)0);
86     QCOMPARE(mapScene.itemAt(x, y), dynamic_cast<QGraphicsItem *>(tile));
87     QCOMPARE(mapScene.itemAt(x+s, y+s), dynamic_cast<QGraphicsItem *>(tile));
88     QCOMPARE(mapScene.itemAt(x+s+1, y+s), (QGraphicsItem *)0);
89     QCOMPARE(mapScene.itemAt(x+s, y+s+1), (QGraphicsItem *)0);
90 }
91
92 QTEST_MAIN(TestMapScene)
93 #include "testmapscene.moc"