1 /***************************************************************************
2     testqgsmaptooledit.cpp
3      --------------------------------------
4     Date                 : 6.2.2017
5     Copyright            : (C) 2017 Alexander Lisovenko
6     Email                : alexander.lisovenko@gmail.com
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 #include <QCoreApplication>
16 
17 #include "qgstest.h"
18 #include "qgsguiutils.h"
19 #include "qgsmaptooledit.h"
20 #include "qgsapplication.h"
21 #include "qgsmapcanvas.h"
22 #include "qgslogger.h"
23 #include "qgssettingsregistrycore.h"
24 #include "qgsvectorlayer.h"
25 
26 class TestQgsMapToolEdit : public QObject
27 {
28     Q_OBJECT
29   public:
30     TestQgsMapToolEdit() = default;
31 
32   private slots:
33     void initTestCase(); // will be called before the first testfunction is executed.
34     void cleanupTestCase(); // will be called after the last testfunction was executed.
35     void init(); // will be called before each testfunction is executed.
36     void cleanup(); // will be called after every testfunction.
37 
38     void checkDefaultZValue();
39     void checkDefaultMValue();
40     void checkLayers();
41 
42   private:
43     QgsMapCanvas *mCanvas = nullptr;
44 
45 };
46 
initTestCase()47 void TestQgsMapToolEdit::initTestCase()
48 {
49   QgsApplication::init();
50   QgsApplication::initQgis();
51   QgsApplication::showSettings();
52 }
53 
cleanupTestCase()54 void TestQgsMapToolEdit::cleanupTestCase()
55 {
56   QgsApplication::exitQgis();
57 }
58 
init()59 void TestQgsMapToolEdit::init()
60 {
61   mCanvas = new QgsMapCanvas();
62 }
63 
cleanup()64 void TestQgsMapToolEdit::cleanup()
65 {
66   delete mCanvas;
67 }
68 
checkDefaultZValue()69 void TestQgsMapToolEdit::checkDefaultZValue()
70 {
71   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.remove();
72 
73   QgsMapToolEdit *tool = new QgsMapToolEdit( mCanvas );
74   QCOMPARE( tool->defaultZValue(), Qgis::DEFAULT_Z_COORDINATE );
75 
76   const double z_value_for_test = Qgis::DEFAULT_Z_COORDINATE + 1;
77   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( z_value_for_test );
78 
79   QCOMPARE( tool->defaultZValue(), z_value_for_test );
80 }
81 
checkDefaultMValue()82 void TestQgsMapToolEdit::checkDefaultMValue()
83 {
84   QgsSettings settings;
85   settings.remove( QStringLiteral( "/qgis/digitizing/default_m_value" ) );
86 
87   QgsMapToolEdit *tool = new QgsMapToolEdit( mCanvas );
88   QCOMPARE( tool->defaultMValue(), Qgis::DEFAULT_M_COORDINATE );
89 
90   const double m_value_for_test = Qgis::DEFAULT_M_COORDINATE + 1;
91   settings.setValue( QStringLiteral( "/qgis/digitizing/default_m_value" ), m_value_for_test );
92 
93   QCOMPARE( tool->defaultMValue(), m_value_for_test );
94 }
95 
checkLayers()96 void TestQgsMapToolEdit::checkLayers()
97 {
98   QgsProject::instance()->clear();
99   //set up canvas with a mix of project and non-project layers
100   QgsVectorLayer *vl1 = new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:3946&field=halig:string&field=valig:string" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
101   QVERIFY( vl1->isValid() );
102   QgsProject::instance()->addMapLayer( vl1 );
103 
104   std::unique_ptr< QgsVectorLayer > vl2 = std::make_unique< QgsVectorLayer >( QStringLiteral( "Point?crs=epsg:3946&field=halig:string&field=valig:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
105   QVERIFY( vl2->isValid() );
106 
107   std::unique_ptr< QgsMapCanvas > canvas = std::make_unique< QgsMapCanvas >();
108   canvas->setLayers( { vl1, vl2.get() } );
109 
110   std::unique_ptr< QgsMapToolEdit > tool = std::make_unique< QgsMapToolEdit >( canvas.get() );
111 
112   // retrieving layer by id should work for both layers from the project AND for freestanding layers
113   QCOMPARE( tool->layer( vl1->id() ), vl1 );
114   QCOMPARE( tool->layer( vl2->id() ), vl2.get() );
115   QCOMPARE( tool->layer( QStringLiteral( "xxx" ) ), nullptr );
116 }
117 
118 QGSTEST_MAIN( TestQgsMapToolEdit )
119 #include "testqgsmaptooledit.moc"
120