final fixing after merge
[situare] / tests / map / uselastlocation / testuselastlocation.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 "map/mapengine.h"
25 #include "map/mapcommon.h"
26 #include "common.h"
27 #include "ui/mainwindow.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 TestUseLastPosition: public QObject
34  {
35      Q_OBJECT
36  private slots:
37
38      void testMapEngineMethods();
39  };
40
41  void TestUseLastPosition::testMapEngineMethods()
42  {
43      // Create mapengine and start monitoring zoomLevelChanged-signal
44      MapEngine *mapengine = new MapEngine;
45      QSignalSpy mapEngineSpy(mapengine, SIGNAL(zoomLevelChanged(int)));
46      QVERIFY (mapEngineSpy.isValid());
47      QCOMPARE (mapEngineSpy.count(), 0);
48
49      // Write new zoomlevel and location to settings
50      QSettings settings(DIRECTORY_NAME, FILE_NAME);
51      settings.setValue(MAP_LAST_ZOOMLEVEL, DEFAULT_TEST_ZOOMLEVEL);
52      settings.setValue(MAP_LAST_POSITION,
53                        mapengine->convertSceneCoordinateToLatLon(DEFAULT_TEST_ZOOMLEVEL,
54                                                                  DEFAULT_TEST_SCENECOORDINATE));
55
56      // Call mapengines init() and verify that signal will be send
57      mapengine->init();
58      QCOMPARE (mapEngineSpy.count(), 1);
59
60      // Remove zoomlevel and location from settings, call init() again and check that
61      // signals is send again
62      settings.remove(MAP_LAST_ZOOMLEVEL);
63      settings.remove(MAP_LAST_POSITION);
64      mapengine->init();
65      QCOMPARE(mapEngineSpy.count(), 2);
66
67      // Check parameters of sended signals
68      QList<QVariant> parameters = mapEngineSpy.takeFirst();
69      QVERIFY(parameters.at(0).toInt() == DEFAULT_TEST_ZOOMLEVEL);
70
71      parameters = mapEngineSpy.takeFirst();
72      QVERIFY(parameters.at(0).toInt() == DEFAULT_START_ZOOM_LEVEL);
73
74      // Call set location with know parameter to get location changed
75      // Store new location and zoomlevel to settings
76      mapengine->setLocation(NEW_TEST_SCENECOORDINATE);
77      delete mapengine;
78
79      // Read settings and verify that zoomlevel is correct
80      MapEngine testengine;// = new mapEngine;
81      QPointF LatLonLocation =
82              settings.value(MAP_LAST_POSITION, ERROR_VALUE_NOT_FOUND_ON_SETTINGS).toPointF();
83      QCOMPARE(LatLonLocation, testengine.convertSceneCoordinateToLatLon(DEFAULT_TEST_ZOOMLEVEL,
84                                                                        NEW_TEST_SCENECOORDINATE));
85  }
86
87  QTEST_MAIN(TestUseLastPosition)
88  #include "testuselastlocation.moc"