File indexing completed on 2024-04-28 04:48:13

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.com>                             *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "TestSimpleImporterConfigWidget.h"
0018 
0019 #include "importers/SimpleImporterConfigWidget.h"
0020 
0021 #include <QComboBox>
0022 #include <QLabel>
0023 #include <QLineEdit>
0024 #include <QSpinBox>
0025 #include <QTest>
0026 
0027 
0028 QTEST_MAIN( TestSimpleImporterConfigWidget )
0029 
0030 using namespace StatSyncing;
0031 
0032 void
0033 TestSimpleImporterConfigWidget::constructorShouldCreateTargetNameRow()
0034 {
0035     SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0036 
0037     const QLabel *label = nullptr;
0038     const QLineEdit *field = nullptr;
0039 
0040     foreach( const QObject *obj, widget.children() )
0041     {
0042         if( qobject_cast<const QLabel*>( obj ) )
0043             label = qobject_cast<const QLabel*>( obj );
0044         else if( qobject_cast<const QLineEdit*>( obj ) )
0045             field = qobject_cast<const QLineEdit*>( obj );
0046     }
0047 
0048     QVERIFY( label );
0049     QVERIFY( field );
0050 
0051     QVERIFY( !label->text().isEmpty() );
0052     QCOMPARE( label->buddy(), field );
0053 }
0054 
0055 void
0056 TestSimpleImporterConfigWidget::targetNameShouldBeSetToDefaultValue()
0057 {
0058     const QString targetName = "testTargetName";
0059     SimpleImporterConfigWidget widget( targetName, QVariantMap() );
0060 
0061     const QLineEdit *field = nullptr;
0062     foreach( const QObject *obj, widget.children() )
0063         if( qobject_cast<const QLineEdit*>( obj ) )
0064             field = qobject_cast<const QLineEdit*>( obj );
0065 
0066     QVERIFY( field );
0067     QCOMPARE( field->text(), targetName );
0068 }
0069 
0070 void
0071 TestSimpleImporterConfigWidget::targetNameShouldBeSetToConfigValueIfExists()
0072 {
0073     const QString targetName = "nameOverride";
0074 
0075     QVariantMap cfg;
0076     cfg.insert( "name", targetName );
0077 
0078     SimpleImporterConfigWidget widget( "testTargetName", cfg );
0079 
0080     const QLineEdit *field = nullptr;
0081     foreach( const QObject *obj, widget.children() )
0082         if( qobject_cast<const QLineEdit*>( obj ) )
0083             field = qobject_cast<const QLineEdit*>( obj );
0084 
0085     QVERIFY( field );
0086     QCOMPARE( field->text(), targetName );
0087 }
0088 
0089 void
0090 TestSimpleImporterConfigWidget::addFieldShouldTakeFieldOwnership()
0091 {
0092     QPointer<QWidget> lineEdit( new QLineEdit() );
0093 
0094     {
0095         SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0096         widget.addField( "configVal", "label", lineEdit.data(), "text" );
0097 
0098         QVERIFY( !lineEdit.isNull() );
0099     }
0100 
0101     QVERIFY( lineEdit.isNull() );
0102 }
0103 
0104 void
0105 TestSimpleImporterConfigWidget::addFieldShouldAddNewRow()
0106 {
0107     SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0108 
0109     QWidget *field = new QLineEdit;
0110     widget.addField( "configVal", "testLabel", field, "text" );
0111 
0112     bool foundField = false;
0113     bool foundLabel = false;
0114 
0115     foreach( const QObject *obj, widget.children() )
0116     {
0117         if( obj == field )
0118             foundField = true;
0119         else if( const QLabel *candidate = qobject_cast<const QLabel*>( obj ) )
0120             if( candidate->text() == "testLabel" )
0121                  foundLabel = true;
0122     }
0123 
0124     QVERIFY( foundField );
0125     QVERIFY( foundLabel );
0126 }
0127 
0128 void
0129 TestSimpleImporterConfigWidget::addFieldShouldAssociateLabelWithField()
0130 {
0131     SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0132 
0133     QWidget *field = new QLineEdit;
0134     widget.addField( "configVal", "testLabel", field, "text" );
0135 
0136     const QLabel *label = nullptr;
0137     foreach( const QObject *obj, widget.children() )
0138         if( const QLabel *candidate = qobject_cast<const QLabel*>( obj ) )
0139             if( candidate->text() == "testLabel" )
0140                  label = candidate;
0141 
0142     QVERIFY( label );
0143     QCOMPARE( label->buddy(), field );
0144 }
0145 
0146 void
0147 TestSimpleImporterConfigWidget::addFieldShouldNotBreakOnNullField()
0148 {
0149     SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0150     widget.addField( "configVal", "testLabel", nullptr, "text" );
0151 }
0152 
0153 void
0154 TestSimpleImporterConfigWidget::addedFieldShouldNotModifyFieldValueIfConfigDoesNotExist()
0155 {
0156     SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0157 
0158     const QString value = "myValue";
0159     QLineEdit *field = new QLineEdit( value );
0160     widget.addField( "configVal", "testLabel", field, "text" );
0161 
0162     QCOMPARE( field->text(), value );
0163 }
0164 
0165 void
0166 TestSimpleImporterConfigWidget::addedFieldShouldBeSetToConfigValueIfExists()
0167 {
0168     const QString configName = "configVal";
0169     const QString value = "myValue";
0170 
0171     QVariantMap cfg;
0172     cfg.insert( configName, value );
0173     SimpleImporterConfigWidget widget( "testTargetName", cfg );
0174 
0175     QLineEdit *field = new QLineEdit( "overrideMe" );
0176     widget.addField( configName, "testLabel", field, "text" );
0177 
0178     QCOMPARE( field->text(), value );
0179 }
0180 
0181 void
0182 TestSimpleImporterConfigWidget::addedFieldShouldNotBreakOnValueSetIfPropertyDoesNotExist()
0183 {
0184     const QString configName = "configVal";
0185 
0186     QVariantMap cfg;
0187     cfg.insert( configName, "value" );
0188     SimpleImporterConfigWidget widget( "testTargetName", cfg );
0189 
0190     widget.addField( configName, "testLabel", new QLineEdit, "There's No Such Property" );
0191 }
0192 
0193 void
0194 TestSimpleImporterConfigWidget::configShouldContainName()
0195 {
0196     const QString name = "testTargetName";
0197     SimpleImporterConfigWidget widget( name, QVariantMap() );
0198 
0199     QCOMPARE( widget.config().value( "name" ).toString(), name );
0200 }
0201 
0202 void
0203 TestSimpleImporterConfigWidget::configShouldNotBreakOnNullField()
0204 {
0205     const QString configName = "configVal";
0206 
0207     SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0208     widget.addField( configName, "testLabel", nullptr, "text" );
0209 
0210     QVERIFY( widget.config().value( configName ).toString().isEmpty() );
0211 }
0212 
0213 void
0214 TestSimpleImporterConfigWidget::configShouldContainAddedFieldsValues()
0215 {
0216     SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0217 
0218     QLineEdit *lineEdit = new QLineEdit;
0219     lineEdit->setText( "textValue" );
0220 
0221     QSpinBox *spinBox = new QSpinBox;
0222     spinBox->setValue( 57 );
0223 
0224     QComboBox *comboBox = new QComboBox;
0225     comboBox->insertItem( 0, "item1" );
0226     comboBox->insertItem( 1, "item2" );
0227     comboBox->insertItem( 2, "item3" );
0228     comboBox->setCurrentIndex( 1 );
0229 
0230     widget.addField( "text", "text", lineEdit, "text" );
0231     widget.addField( "int", "int", spinBox, "value" );
0232     widget.addField( "combo", "combo", comboBox, "currentText" );
0233 
0234     const QVariantMap cfg = widget.config();
0235     QCOMPARE( cfg.value( "text" ).toString(), QString( "textValue" ) );
0236     QCOMPARE( cfg.value( "int" ).toInt(), 57 );
0237     QCOMPARE( cfg.value( "combo" ).toString(), QString( "item2" ) );
0238 }
0239 
0240 void
0241 TestSimpleImporterConfigWidget::configShouldNotBreakOnNonexistentProperty()
0242 {
0243     const QString configName = "configName";
0244 
0245     SimpleImporterConfigWidget widget( "testTargetName", QVariantMap() );
0246     widget.addField( configName, "label", new QLineEdit, "No property" );
0247 
0248     QVERIFY( widget.config().value( configName ).toString().isEmpty() );
0249 }