1 /***************************************************************************
2     testqgskeyvaluewidget.cpp
3      --------------------------------------
4     Date                 : 08 09 2016
5     Copyright            : (C) 2016 Patrick Valsecchi
6     Email                : patrick dot valsecchi at camptocamp 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 #include <QSignalSpy>
19 
20 #include <editorwidgets/qgskeyvaluewidgetfactory.h>
21 #include <qgskeyvaluewidget.h>
22 #include <editorwidgets/core/qgseditorwidgetwrapper.h>
23 #include <qgsapplication.h>
24 
25 class TestQgsKeyValueWidget : public QObject
26 {
27     Q_OBJECT
28   public:
29 
30   private slots:
initTestCase()31     void initTestCase() // will be called before the first testfunction is executed.
32     {
33       QgsApplication::init();
34       QgsApplication::initQgis();
35     }
36 
cleanupTestCase()37     void cleanupTestCase() // will be called after the last testfunction was executed.
38     {
39       QgsApplication::exitQgis();
40     }
41 
testUpdate()42     void testUpdate()
43     {
44       const QgsKeyValueWidgetFactory factory( QStringLiteral( "testKeyValue" ) );
45       QgsEditorWidgetWrapper *wrapper = factory.create( nullptr, 0, nullptr, nullptr );
46       QVERIFY( wrapper );
47       const QSignalSpy spy( wrapper, SIGNAL( valueChanged( const QVariant & ) ) );
48 
49       QgsKeyValueWidget *widget = qobject_cast< QgsKeyValueWidget * >( wrapper->widget() );
50       QVERIFY( widget );
51 
52       QVariantMap initial;
53       initial[QStringLiteral( "1" )] = "one";
54       initial[QStringLiteral( "2" )] = "two";
55       wrapper->setValues( initial, QVariantList() );
56 
57       const QVariant value = wrapper->value();
58       QCOMPARE( int( value.type() ), int( QVariant::Map ) );
59       QCOMPARE( value.toMap(), initial );
60       QCOMPARE( spy.count(), 0 );
61 
62       QAbstractItemModel *model = widget->tableView->model();
63       model->setData( model->index( 0, 1 ), "hello" );
64       QCOMPARE( spy.count(), 1 );
65 
66       QVariantMap expected = initial;
67       expected[QStringLiteral( "1" )] = "hello";
68       const QVariant eventValue = spy.at( 0 ).at( 0 ).value<QVariant>();
69       QCOMPARE( int( eventValue.type() ), int( QVariant::Map ) );
70       QCOMPARE( eventValue.toMap(), expected );
71       QCOMPARE( wrapper->value().toMap(), expected );
72       QCOMPARE( spy.count(), 1 );
73     }
74 };
75 
76 QGSTEST_MAIN( TestQgsKeyValueWidget )
77 #include "testqgskeyvaluewidget.moc"
78