2eedd2e96e0b0bc1d014dc29fff19ed6be7379c9
[situare] / tests / map / ownlocationitem / testownlocationitem.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Ville Tiensuu - ville.tiensuu@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 #include <QList>
25
26 #include "map/ownlocationitem.h"
27 #include "map/mapscene.h"
28 #include "map/maptile.h"
29 #include "map/mapengine.h"
30 #include "map/mapcommon.h"
31 #include "map/mapview.h"
32
33 namespace TestOwnLocation  //  Test data for function is defined in namespace
34 {   
35     const qreal xCoordinate = 65.525;
36     const qreal yCoordinate = 25.345;
37     const QPointF testLocationPoint(65.525, 25.345);
38     const QPointF defaultLocationPoint(DEFAULT_LONGITUDE,DEFAULT_LATITUDE);
39     const QPointF locationIconOffset(-MAP_OWN_LOCATION_ICON_SIZE/2, -MAP_OWN_LOCATION_ICON_SIZE/2);
40     const int itemIgnoresTransformationsFlagValue = 0x20;
41 }
42
43 using namespace TestOwnLocation;
44
45
46 /**
47 * @brief Class that test OwnLocationItem
48 *
49 * @author Ville Tiensuu
50 */
51 class TestOwnLocationItem: public QObject
52  {
53      Q_OBJECT
54  private slots:
55
56     /**
57     * @brief Test method for constructors.
58     *        Creates instance of OwnLocationItem using three different constructors and tests
59     *        that positions are set correctly.
60     *        Tests that Z-values are set correctly.
61     *        Tests that offses are set correctly.
62     *        Tests that ItemIgnoresTransformations flag is set.
63     */
64      void testConstructors();
65
66      /**
67      * @brief Creates instance of OwnLocationItem.
68      *        Sets test position for location item in geographic coordinates.
69      *        Reads position from location item in scene coordinates and
70      *        verifies that it correctly formed.
71      */
72      void testSetAndGetPosition();
73
74      /**
75      * @brief Creates instance of OwnLocationItem.
76      *        Verifies that instance is visible.
77      *        Hides instance and verifies that it is not visible.
78      */
79      void testHideAndShowPosition();
80  };
81
82  void TestOwnLocationItem::testConstructors()
83  {
84      OwnLocationItem ownLocationItem1;
85      OwnLocationItem ownLocationItem2(xCoordinate, yCoordinate);
86      OwnLocationItem ownLocationItem3(testLocationPoint);
87
88      // Test Positions
89      QCOMPARE(ownLocationItem1.position(),
90               MapEngine::convertLatLonToSceneCoordinate(defaultLocationPoint));
91
92      QCOMPARE(ownLocationItem2.position(),
93               MapEngine::convertLatLonToSceneCoordinate(testLocationPoint));
94
95      QCOMPARE(ownLocationItem3.position(),
96               MapEngine::convertLatLonToSceneCoordinate(testLocationPoint));
97
98      // Test Z-values
99      QCOMPARE(static_cast<int>(ownLocationItem1.zValue()), OWN_LOCATION_ICON_Z_LEVEL);
100      QCOMPARE(static_cast<int>(ownLocationItem2.zValue()), OWN_LOCATION_ICON_Z_LEVEL);
101      QCOMPARE(static_cast<int>(ownLocationItem3.zValue()), OWN_LOCATION_ICON_Z_LEVEL);
102
103      // Test Offsets
104      QCOMPARE(ownLocationItem1.offset(), locationIconOffset);
105      QCOMPARE(ownLocationItem2.offset(), locationIconOffset);
106      QCOMPARE(ownLocationItem3.offset(), locationIconOffset);
107
108      // Test ItemIgnoresTransformations Flags
109      QGraphicsItem::GraphicsItemFlags ownLocationItem1Flags = ownLocationItem1.flags();
110      QGraphicsItem::GraphicsItemFlags ownLocationItem2Flags = ownLocationItem1.flags();
111      QGraphicsItem::GraphicsItemFlags ownLocationItem3Flags = ownLocationItem1.flags();
112
113      QCOMPARE(ownLocationItem1Flags, itemIgnoresTransformationsFlagValue);
114      QCOMPARE(ownLocationItem2Flags, itemIgnoresTransformationsFlagValue);
115      QCOMPARE(ownLocationItem3Flags, itemIgnoresTransformationsFlagValue);
116  }
117
118  void TestOwnLocationItem::testSetAndGetPosition()
119  {
120      OwnLocationItem ownLocation;
121      ownLocation.setPosition(testLocationPoint);
122
123      QCOMPARE(ownLocation.position(),
124               MapEngine::convertLatLonToSceneCoordinate(testLocationPoint));
125  }
126
127  void TestOwnLocationItem::testHideAndShowPosition()
128  {
129      OwnLocationItem ownLocation;
130
131      QCOMPARE(ownLocation.isVisible(), true);     
132
133      ownLocation.hideOwnLocation();
134
135      QCOMPARE(ownLocation.isVisible(), false);
136
137  }
138
139  QTEST_MAIN(TestOwnLocationItem)
140  #include "testownlocationitem.moc"