1 /***************************************************************************
2      testqgslabelpropertydialog.cpp
3      ------------------------------
4     Date                 : Feb 2020
5     Copyright            : (C) 2020 by Paul Blottiere
6     Email                : blottiere dot paul at gmail dot 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 
16 #include "qgstest.h"
17 #include <QObject>
18 
19 #include "qgisapp.h"
20 #include "qgsproject.h"
21 #include "qgsapplication.h"
22 #include "qgsvectorlayer.h"
23 #include "qgslabelingengine.h"
24 #include "qgsauxiliarystorage.h"
25 #include "qgslabelpropertydialog.h"
26 #include "qgsvectorlayerlabeling.h"
27 
28 class TestQgsLabelPropertyDialog : public QObject
29 {
30     Q_OBJECT
31 
32   public:
33     TestQgsLabelPropertyDialog() = default;
34 
35   private:
36     QString mTestDataDir;
37     QgisApp *mQgisApp = nullptr;
38 
39   private slots:
40 
initTestCase()41     void initTestCase()
42     {
43       QgsApplication::init();
44       QgsApplication::initQgis();
45       mQgisApp = new QgisApp();
46 
47       QString myDataDir( TEST_DATA_DIR );
48       mTestDataDir = myDataDir + '/';
49     }
50 
cleanupTestCase()51     void cleanupTestCase()
52     {
53       QgsApplication::exitQgis();
54     }
55 
test()56     void test()
57     {
58       // init vector layer
59       QString pointFileName = mTestDataDir + "points.shp";
60       QFileInfo pointFileInfo( pointFileName );
61       QgsVectorLayer *vl = new QgsVectorLayer( pointFileInfo.filePath(),
62           pointFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
63       QgsProject::instance()->addMapLayer( vl );
64 
65       // activate labeling
66       QgsPalLayerSettings settings;
67       vl->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
68 
69       // create auxiliary layer
70       QgsField field = vl->fields().field( 1 );
71       QgsAuxiliaryLayer *al = QgsProject::instance()->auxiliaryStorage()->createAuxiliaryLayer( field, vl );
72       vl->setAuxiliaryLayer( al );
73 
74       // create auxiliary field for BufferDraw
75       QgsAuxiliaryLayer::createProperty( QgsPalLayerSettings::BufferDraw, vl );
76       QgsPropertyDefinition def = QgsPalLayerSettings::propertyDefinitions()[QgsPalLayerSettings::BufferDraw];
77       QString propName = QgsAuxiliaryLayer::nameFromProperty( def, true );
78       QCOMPARE( int( al->featureCount() ), 0 );
79 
80       QgsFeatureId fid = 0;
81       QVariant val = vl->getFeature( fid ).attribute( propName );
82 
83       // init label property dialog and togle buffer draw
84       QgsLabelPropertyDialog dialog( vl->id(), QString(), fid, QFont(), QString(), false, settings );
85       dialog.bufferDrawToggled( true );
86 
87       // apply changes
88       QgsAttributeMap changes = dialog.changedProperties();
89       QgsAttributeMap::const_iterator changeIt = changes.constBegin();
90       for ( ; changeIt != changes.constEnd(); ++changeIt )
91       {
92         vl->changeAttributeValue( fid, changeIt.key(), changeIt.value() );
93       }
94 
95       // check auxiliary values
96       QCOMPARE( int( al->featureCount() ), 1 );
97       val = vl->getFeature( fid ).attribute( propName );
98       QCOMPARE( val.toInt(), 1 );
99 
100       // toggle false
101       dialog.bufferDrawToggled( false );
102 
103       changes = dialog.changedProperties();
104       changeIt = changes.constBegin();
105       for ( ; changeIt != changes.constEnd(); ++changeIt )
106       {
107         vl->changeAttributeValue( fid, changeIt.key(), changeIt.value() );
108       }
109 
110       val = vl->getFeature( fid ).attribute( propName );
111       QCOMPARE( val.toInt(), 0 );
112     }
113 };
114 
115 QGSTEST_MAIN( TestQgsLabelPropertyDialog )
116 #include "testqgslabelpropertydialog.moc"
117