1 /***************************************************************************
2 testqgsvaluemapwidgetwrapper.cpp
3 --------------------------------------
4 Date : January 2018
5 Copyright : (C) 2018 Nyall Dawson
6 Email : nyall dot dawson 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
17 #include "qgstest.h"
18
19 #include "editorwidgets/core/qgseditorwidgetregistry.h"
20 #include "qgsapplication.h"
21 #include "qgseditorwidgetwrapper.h"
22 #include "editorwidgets/qgsvaluemapwidgetwrapper.h"
23 #include "qgsvaluemapfieldformatter.h"
24 #include "editorwidgets/qgsvaluemapconfigdlg.h"
25 #include "qgsgui.h"
26
27 class TestQgsValueMapWidgetWrapper : public QObject
28 {
29 Q_OBJECT
30 public:
31 TestQgsValueMapWidgetWrapper() = default;
32
33 private slots:
34 void initTestCase(); // will be called before the first testfunction is executed.
35 void cleanupTestCase(); // will be called after the last testfunction was executed.
36 void init(); // will be called before each testfunction is executed.
37 void cleanup(); // will be called after every testfunction.
38 void testPopulateComboBox();
39
40 };
41
initTestCase()42 void TestQgsValueMapWidgetWrapper::initTestCase()
43 {
44 QgsApplication::init();
45 QgsApplication::initQgis();
46 QgsGui::editorWidgetRegistry()->initEditors();
47 }
48
cleanupTestCase()49 void TestQgsValueMapWidgetWrapper::cleanupTestCase()
50 {
51 QgsApplication::exitQgis();
52 }
53
init()54 void TestQgsValueMapWidgetWrapper::init()
55 {
56 }
57
cleanup()58 void TestQgsValueMapWidgetWrapper::cleanup()
59 {
60 }
61
testPopulateComboBox()62 void TestQgsValueMapWidgetWrapper::testPopulateComboBox()
63 {
64 // new style config
65 QVariantMap config;
66 QList<QVariant> valueList;
67 QVariantMap nullValue;
68 nullValue.insert( QgsApplication::nullRepresentation(), QgsValueMapFieldFormatter::NULL_VALUE );
69 valueList.append( nullValue );
70 QVariantMap value1;
71 value1.insert( QStringLiteral( "desc 1" ), QStringLiteral( "val 1" ) );
72 valueList.append( value1 );
73 QVariantMap value2;
74 value2.insert( QStringLiteral( "desc 2" ), QStringLiteral( "val 2" ) );
75 valueList.append( value2 );
76
77 config.insert( QStringLiteral( "map" ), valueList );
78
79
80 std::unique_ptr< QComboBox > combo = std::make_unique< QComboBox >();
81
82 // with nulls
83 QgsValueMapConfigDlg::populateComboBox( combo.get(), config, false );
84
85 QCOMPARE( combo->count(), 3 );
86 QCOMPARE( combo->itemText( 0 ), QgsApplication::nullRepresentation() );
87 QCOMPARE( combo->itemData( 0 ).toString(), QgsValueMapFieldFormatter::NULL_VALUE );
88 QCOMPARE( combo->itemText( 1 ), QStringLiteral( "desc 1" ) );
89 QCOMPARE( combo->itemData( 1 ).toString(), QStringLiteral( "val 1" ) );
90 QCOMPARE( combo->itemText( 2 ), QStringLiteral( "desc 2" ) );
91 QCOMPARE( combo->itemData( 2 ).toString(), QStringLiteral( "val 2" ) );
92
93 // no nulls
94 combo->clear();
95 QgsValueMapConfigDlg::populateComboBox( combo.get(), config, true );
96
97 QCOMPARE( combo->count(), 2 );
98 QCOMPARE( combo->itemText( 0 ), QStringLiteral( "desc 1" ) );
99 QCOMPARE( combo->itemData( 0 ).toString(), QStringLiteral( "val 1" ) );
100 QCOMPARE( combo->itemText( 1 ), QStringLiteral( "desc 2" ) );
101 QCOMPARE( combo->itemData( 1 ).toString(), QStringLiteral( "val 2" ) );
102
103 // old style config map (2.x)
104 config.clear();
105 QVariantMap mapValue;
106 mapValue.insert( QgsApplication::nullRepresentation(), QgsValueMapFieldFormatter::NULL_VALUE );
107 mapValue.insert( QStringLiteral( "desc 1" ), QStringLiteral( "val 1" ) );
108 mapValue.insert( QStringLiteral( "desc 2" ), QStringLiteral( "val 2" ) );
109 config.insert( QStringLiteral( "map" ), mapValue );
110
111 // with nulls
112 combo->clear();
113 QgsValueMapConfigDlg::populateComboBox( combo.get(), config, false );
114
115 QCOMPARE( combo->count(), 3 );
116 QCOMPARE( combo->itemText( 0 ), QgsApplication::nullRepresentation() );
117 QCOMPARE( combo->itemData( 0 ).toString(), QgsValueMapFieldFormatter::NULL_VALUE );
118 QCOMPARE( combo->itemText( 1 ), QStringLiteral( "desc 1" ) );
119 QCOMPARE( combo->itemData( 1 ).toString(), QStringLiteral( "val 1" ) );
120 QCOMPARE( combo->itemText( 2 ), QStringLiteral( "desc 2" ) );
121 QCOMPARE( combo->itemData( 2 ).toString(), QStringLiteral( "val 2" ) );
122
123 // no nulls
124 combo->clear();
125 QgsValueMapConfigDlg::populateComboBox( combo.get(), config, true );
126
127 QCOMPARE( combo->count(), 2 );
128 QCOMPARE( combo->itemText( 0 ), QStringLiteral( "desc 1" ) );
129 QCOMPARE( combo->itemData( 0 ).toString(), QStringLiteral( "val 1" ) );
130 QCOMPARE( combo->itemText( 1 ), QStringLiteral( "desc 2" ) );
131 QCOMPARE( combo->itemData( 1 ).toString(), QStringLiteral( "val 2" ) );
132
133 }
134
135 QGSTEST_MAIN( TestQgsValueMapWidgetWrapper )
136 #include "testqgsvaluemapwidgetwrapper.moc"
137