Skip to content
Permalink
Browse files

Adjust tests

  • Loading branch information
m-kuhn committed Oct 26, 2017
1 parent 9339595 commit 114a4ec243f5a8f714ff2740fac196ca7a748db8
Showing with 39 additions and 45 deletions.
  1. +1 −0 src/gui/qgsattributeformeditorwidget.cpp
  2. +38 −45 tests/src/gui/testqgsattributeform.cpp
@@ -38,6 +38,7 @@ QgsAttributeFormEditorWidget::QgsAttributeFormEditorWidget( QgsEditorWidgetWrapp
, mIsChanged( false )
{
mConstraintResultLabel = new QLabel();
mConstraintResultLabel->setObjectName( QStringLiteral( "ConstraintStatus" ) );
mConstraintResultLabel->setSizePolicy( QSizePolicy::Fixed, mConstraintResultLabel->sizePolicy().verticalPolicy() );

mEditPage = new QWidget();
@@ -26,6 +26,7 @@
#include <qgsfeature.h>
#include <qgsvectorlayerjoininfo.h>
#include "qgsgui.h"
#include "qgsattributeformeditorwidget.h"

class TestQgsAttributeForm : public QObject
{
@@ -46,6 +47,13 @@ class TestQgsAttributeForm : public QObject
void testConstraintsOnJoinedFields();
void testEditableJoin();
void testUpsertOnEdit();

private:
QLabel *constraintsLabel( QgsAttributeForm *form, QgsEditorWidgetWrapper *ww )
{
QgsAttributeFormEditorWidget *formEditorWidget = form->mFormEditorWidgets.value( ww->fieldIdx() );
return formEditorWidget->findChild<QLabel *>( QStringLiteral( "ConstraintStatus" ) );
}
};

void TestQgsAttributeForm::initTestCase()
@@ -84,9 +92,9 @@ void TestQgsAttributeForm::testFieldConstraint()
form.setFeature( ft );

// testing stuff
QString validLabel = QStringLiteral( "col0<font color=\"green\"></font>" );
QString invalidLabel = QStringLiteral( "col0<font color=\"red\"></font>" );
QString warningLabel = QStringLiteral( "col0<font color=\"orange\"></font>" );
QString validLabel = QStringLiteral( "<font color=\"#259b24\">\u2714</font>" );
QString invalidLabel = QStringLiteral( "<font color=\"#FF9800\">\u2718</font>" );
QString warningLabel = QStringLiteral( "<font color=\"#FFC107\">\u2718</font>" );

// set constraint
layer->setConstraintExpression( 0, QString() );
@@ -95,9 +103,8 @@ void TestQgsAttributeForm::testFieldConstraint()
QgsEditorWidgetWrapper *ww = nullptr;
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[0] );

// no constraint so we expect a label with just the field name
QLabel *label = form.mBuddyMap.value( ww->widget() );
QCOMPARE( label->text(), QString( "col0" ) );
// no constraint so we expect an empty label
QCOMPARE( constraintsLabel( &form, ww )->text(), QString() );

// set a not null constraint
layer->setConstraintExpression( 0, QStringLiteral( "col0 is not null" ) );
@@ -106,24 +113,23 @@ void TestQgsAttributeForm::testFieldConstraint()
form2.setFeature( ft );
QSignalSpy spy( &form2, SIGNAL( attributeChanged( QString, QVariant ) ) );
ww = qobject_cast<QgsEditorWidgetWrapper *>( form2.mWidgets[0] );
label = form2.mBuddyMap.value( ww->widget() );

// set value to 1
ww->setValue( 1 );
QCOMPARE( spy.count(), 2 );
QCOMPARE( label->text(), validLabel );
QCOMPARE( constraintsLabel( &form2, ww )->text(), validLabel );

// set value to null
spy.clear();
ww->setValue( QVariant() );
QCOMPARE( spy.count(), 2 );
QCOMPARE( label->text(), invalidLabel );
QCOMPARE( constraintsLabel( &form2, ww )->text(), invalidLabel );

// set value to 1
spy.clear();
ww->setValue( 1 );
QCOMPARE( spy.count(), 2 );
QCOMPARE( label->text(), validLabel );
QCOMPARE( constraintsLabel( &form2, ww )->text(), validLabel );

// set a soft constraint
layer->setConstraintExpression( 0, QStringLiteral( "col0 is not null" ) );
@@ -132,19 +138,18 @@ void TestQgsAttributeForm::testFieldConstraint()
QgsAttributeForm form3( layer );
form3.setFeature( ft );
ww = qobject_cast<QgsEditorWidgetWrapper *>( form3.mWidgets[0] );
label = form3.mBuddyMap.value( ww->widget() );

// set value to 1
ww->setValue( 1 );
QCOMPARE( label->text(), validLabel );
QCOMPARE( constraintsLabel( &form3, ww )->text(), validLabel );

// set value to null
ww->setValue( QVariant() );
QCOMPARE( label->text(), warningLabel );
QCOMPARE( constraintsLabel( &form3, ww )->text(), warningLabel );

// set value to 1
ww->setValue( 1 );
QCOMPARE( label->text(), validLabel );
QCOMPARE( constraintsLabel( &form3, ww )->text(), validLabel );
}

void TestQgsAttributeForm::testFieldMultiConstraints()
@@ -172,8 +177,8 @@ void TestQgsAttributeForm::testFieldMultiConstraints()

// testing stuff
QSignalSpy spy( &form, SIGNAL( attributeChanged( QString, QVariant ) ) );
QString val = QStringLiteral( "<font color=\"green\"></font>" );
QString inv = QStringLiteral( "<font color=\"red\"></font>" );
QString val = QStringLiteral( "<font color=\"#259b24\">\u2714</font>" );
QString inv = QStringLiteral( "<font color=\"#FF9800\">\u2718</font>" );

// get wrappers for each widget
QgsEditorWidgetWrapper *ww0, *ww1, *ww2, *ww3;
@@ -182,17 +187,11 @@ void TestQgsAttributeForm::testFieldMultiConstraints()
ww2 = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[2] );
ww3 = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[3] );

// get label for wrappers
QLabel *label0 = form.mBuddyMap.value( ww0->widget() );
QLabel *label1 = form.mBuddyMap.value( ww1->widget() );
QLabel *label2 = form.mBuddyMap.value( ww2->widget() );
QLabel *label3 = form.mBuddyMap.value( ww3->widget() );

// no constraint so we expect a label with just the field name
QCOMPARE( label0->text(), QString( "col0" ) );
QCOMPARE( label1->text(), QString( "col1" ) );
QCOMPARE( label2->text(), QString( "col2" ) );
QCOMPARE( label3->text(), QString( "col3" ) );
// no constraint so we expect an empty label
Q_ASSERT( constraintsLabel( &form, ww0 )->text().isEmpty() );
Q_ASSERT( constraintsLabel( &form, ww1 )->text().isEmpty() );
Q_ASSERT( constraintsLabel( &form, ww2 )->text().isEmpty() );
Q_ASSERT( constraintsLabel( &form, ww3 )->text().isEmpty() );

// update constraint
layer->setConstraintExpression( 0, QStringLiteral( "col0 < (col1 * col2)" ) );
@@ -206,30 +205,26 @@ void TestQgsAttributeForm::testFieldMultiConstraints()
ww1 = qobject_cast<QgsEditorWidgetWrapper *>( form2.mWidgets[1] );
ww2 = qobject_cast<QgsEditorWidgetWrapper *>( form2.mWidgets[2] );
ww3 = qobject_cast<QgsEditorWidgetWrapper *>( form2.mWidgets[3] );
label0 = form2.mBuddyMap.value( ww0->widget() );
label1 = form2.mBuddyMap.value( ww1->widget() );
label2 = form2.mBuddyMap.value( ww2->widget() );
label3 = form2.mBuddyMap.value( ww3->widget() );
QSignalSpy spy2( &form2, SIGNAL( attributeChanged( QString, QVariant ) ) );

// change value
ww0->setValue( 2 ); // update col0
QCOMPARE( spy2.count(), 2 );

QCOMPARE( label0->text(), QString( "col0" + inv ) ); // 2 < ( 1 + 2 )
QCOMPARE( label1->text(), QString( "col1" ) );
QCOMPARE( label2->text(), QString( "col2" ) );
QCOMPARE( label3->text(), QString( "col3" + val ) ); // 2 = 2
QCOMPARE( constraintsLabel( &form2, ww0 )->text(), inv ); // 2 < ( 1 + 2 )
QCOMPARE( constraintsLabel( &form2, ww1 )->text(), QString() );
QCOMPARE( constraintsLabel( &form2, ww2 )->text(), QString() );
QCOMPARE( constraintsLabel( &form2, ww3 )->text(), val ); // 2 = 2

// change value
spy2.clear();
ww0->setValue( 1 ); // update col0
QCOMPARE( spy2.count(), 2 );

QCOMPARE( label0->text(), QString( "col0" + val ) ); // 1 < ( 1 + 2 )
QCOMPARE( label1->text(), QString( "col1" ) );
QCOMPARE( label2->text(), QString( "col2" ) );
QCOMPARE( label3->text(), QString( "col3" + inv ) ); // 2 = 1
QCOMPARE( constraintsLabel( &form2, ww0 )->text(), val ); // 1 < ( 1 + 2 )
QCOMPARE( constraintsLabel( &form2, ww1 )->text(), QString() );
QCOMPARE( constraintsLabel( &form2, ww2 )->text(), QString() );
QCOMPARE( constraintsLabel( &form2, ww3 )->text(), inv ); // 2 = 1
}

void TestQgsAttributeForm::testOKButtonStatus()
@@ -445,8 +440,8 @@ void TestQgsAttributeForm::testDynamicForm()

void TestQgsAttributeForm::testConstraintsOnJoinedFields()
{
QString validLabel = QStringLiteral( "col0<font color=\"green\"></font>" );
QString warningLabel = QStringLiteral( "col0<font color=\"orange\"></font>" );
QString validLabel = QStringLiteral( "<font color=\"#259b24\">\u2714</font>" );
QString warningLabel = QStringLiteral( "<font color=\"#FFC107\">\u2718</font>" );

// make temporary layers
QString defA = QStringLiteral( "Point?field=id_a:integer" );
@@ -501,16 +496,14 @@ void TestQgsAttributeForm::testConstraintsOnJoinedFields()
// compare
QgsEditorWidgetWrapper *ww = nullptr;
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[1] );
QLabel *label = form.mBuddyMap.value( ww->widget() );
QCOMPARE( label->text(), "layerB_" + validLabel );
QCOMPARE( constraintsLabel( &form, ww )->text(), validLabel );

// change layerA join id field
form.changeAttribute( QStringLiteral( "id_a" ), QVariant( 31 ) );

// compare
ww = qobject_cast<QgsEditorWidgetWrapper *>( form.mWidgets[1] );
label = form.mBuddyMap.value( ww->widget() );
QCOMPARE( label->text(), "layerB_" + warningLabel );
QCOMPARE( constraintsLabel( &form, ww )->text(), warningLabel );
}

void TestQgsAttributeForm::testEditableJoin()

0 comments on commit 114a4ec

Please sign in to comment.
You can’t perform that action at this time.