Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik committed Oct 23, 2023
1 parent b380b64 commit 6a8b512
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
20 changes: 11 additions & 9 deletions app/attributes/attributecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ void AttributeController::recalculateDerivedItems( bool isFormValueChange, bool
}

// Evaluate HTML and Text element expressions
recalculateRichTextWidgets(changedFormItems, expressionContext);
recalculateRichTextWidgets( changedFormItems, expressionContext );

// Evaluate tab items visiblity
{
Expand Down Expand Up @@ -934,7 +934,7 @@ void AttributeController::recalculateDerivedItems( bool isFormValueChange, bool
emit formRecalculated();
}

void AttributeController::recalculateRichTextWidgets( QSet<QUuid> &changedFormItems, QgsExpressionContext &context)
void AttributeController::recalculateRichTextWidgets( QSet<QUuid> &changedFormItems, QgsExpressionContext &context )
{
QMap<QUuid, std::shared_ptr<FormItem>>::iterator formItemsIterator = mFormItems.begin();
while ( formItemsIterator != mFormItems.end() )
Expand All @@ -943,9 +943,10 @@ void AttributeController::recalculateRichTextWidgets( QSet<QUuid> &changedFormIt
if ( itemData->type() == FormItem::RichText )
{
QString newValue;
QString definition = itemData->editorWidgetConfig().value(QStringLiteral("Definition")).toString();
bool isHTML = itemData->editorWidgetConfig().value(QStringLiteral("UseHtml")).toBool();
if (isHTML) {
QString definition = itemData->editorWidgetConfig().value( QStringLiteral( "Definition" ) ).toString();
bool isHTML = itemData->editorWidgetConfig().value( QStringLiteral( "UseHtml" ) ).toBool();
if ( isHTML )
{
// evaluate texts like: <script>document.write(expression.evaluate("\TextField\""));</script>

// QML Text does not support document.write, so just remove it
Expand All @@ -954,7 +955,7 @@ void AttributeController::recalculateRichTextWidgets( QSet<QUuid> &changedFormIt
while ( match1.hasMatch() )
{
QString expression = match1.captured( 1 );
definition = QStringLiteral( "<span>%1</span>" ).arg(definition.mid( 0, match1.capturedStart( 0 ) ) + expression + definition.mid( match1.capturedEnd( 0 ) ));
definition = QStringLiteral( "<span>%1</span>" ).arg( definition.mid( 0, match1.capturedStart( 0 ) ) + expression + definition.mid( match1.capturedEnd( 0 ) ) );
match1 = sRegEx1.match( definition );
}

Expand All @@ -974,13 +975,14 @@ void AttributeController::recalculateRichTextWidgets( QSet<QUuid> &changedFormIt
}
newValue = definition;
}
else {
else
{
newValue = QgsExpression::replaceExpressionText( definition, &context );
}
if (itemData->rawValue() != newValue)
if ( itemData->rawValue() != newValue )
{
changedFormItems.insert( itemData->id() );
itemData->setRawValue(newValue);
itemData->setRawValue( newValue );
}
}
++formItemsIterator;
Expand Down
2 changes: 1 addition & 1 deletion app/attributes/attributecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class AttributeController : public QObject
*/
void recalculateDerivedItems( bool isFormValueChange = false, bool isFirstUpdateOfNewFeature = false );
bool recalculateDefaultValues( QSet<QUuid> &changedFormItems, QgsExpressionContext &context, bool isFormValueChange = false, bool isFirstUpdateOfNewFeature = false );
void recalculateRichTextWidgets( QSet<QUuid> &changedFormItems, QgsExpressionContext &context);
void recalculateRichTextWidgets( QSet<QUuid> &changedFormItems, QgsExpressionContext &context );

// generate tab
void createTab( QgsAttributeEditorContainer *container );
Expand Down
55 changes: 20 additions & 35 deletions app/test/testattributecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ void TestAttributeController::testFieldsOutsideForm()

const TabItem *tab = controller.tabItem( 0 );
const QVector<QUuid> items = tab->formItems();
QCOMPARE( items.size(), 3 );
QCOMPARE( items.size(), 7 );

struct testcase
{
Expand Down Expand Up @@ -744,7 +744,6 @@ void TestAttributeController::testHtmlAndTextWidgets()

QVERIFY( QgsProject::instance()->read( projectDir + "/" + projectName ) );

/*
QgsMapLayer *layer = QgsProject::instance()->mapLayersByName( QStringLiteral( "points" ) ).at( 0 );
QgsVectorLayer *surveyLayer = static_cast<QgsVectorLayer *>( layer );

Expand All @@ -762,43 +761,29 @@ void TestAttributeController::testHtmlAndTextWidgets()

const TabItem *tab = controller.tabItem( 0 );
const QVector<QUuid> items = tab->formItems();
QCOMPARE( items.size(), 3 );
QCOMPARE( items.size(), 7 );

struct testcase
{
QUuid id;
QVariant value;
QVariant expectedText;
QVariant expectedText2;
QVariant expectedNum;
QVariant expectedNum2;
};
const auto htmlItem = controller.formItem( items.at( 3 ) );
QCOMPARE( htmlItem->editorWidgetType(), "richtext" );
QCOMPARE( htmlItem->editorWidgetConfig().value( "UseHtml" ).toBool(), true );

QList<testcase> testCases
{
{ items.at( 0 ), QVariant( "1" ), QVariant( "1" ), QVariant( "1 on update" ), QVariant(), QVariant() },
{ items.at( 2 ), QVariant( 2 ), QVariant( "1" ), QVariant( "1 on update" ), QVariant( 2 ), QVariant( 102 ) },
};
const auto textItem = controller.formItem( items.at( 4 ) );
QCOMPARE( textItem->editorWidgetType(), "richtext" );
QCOMPARE( textItem->editorWidgetConfig().value( "UseHtml" ).toBool(), false );

for ( const testcase &t : testCases )
{
const FormItem *item = controller.formItem( t.id );
const auto spacerItem = controller.formItem( items.at( 5 ) );
QCOMPARE( spacerItem->editorWidgetType(), "spacer" );
QCOMPARE( spacerItem->editorWidgetConfig().value( "IsHLine" ).toBool(), true );

controller.setFormValue( t.id, t.value );
const auto hLineItem = controller.formItem( items.at( 6 ) );
QCOMPARE( hLineItem->editorWidgetType(), "spacer" );
QCOMPARE( hLineItem->editorWidgetConfig().value( "IsHLine" ).toBool(), false );

QCOMPARE( controller.featureLayerPair().feature().attribute( 1 ), t.expectedText );
QCOMPARE( controller.featureLayerPair().feature().attribute( 2 ), t.expectedText2 );
QCOMPARE( controller.featureLayerPair().feature().attribute( 3 ), t.expectedNum );
QVariant v = controller.featureLayerPair().feature().attribute( 4 );
if ( v.isNull() )
{
QVERIFY( t.expectedNum2.isNull() );
}
else
{
QCOMPARE( controller.featureLayerPair().feature().attribute( 4 ), t.expectedNum2 );
}
}
// update one field on which both HTML and Text widgets depends
auto field = controller.formItem( items.at( 0 ) );
QCOMPARE( field->name(), "text" );
controller.setFormValue( field->id(), "my new text" );

*/
QCOMPARE( htmlItem->rawValue(), "<span>my new text on update</span>" );
QCOMPARE( textItem->rawValue(), "my new text on update" );
}
Binary file modified test/test_data/expressions/project.qgz
Binary file not shown.
Binary file modified test/test_data/expressions/survey.gpkg
Binary file not shown.

1 comment on commit 6a8b512

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 23.10.472011 just submitted!

Please sign in to comment.