Skip to content
Permalink
Browse files
[ExternalResourceWidget] Fix crash when loading blank after a page
  • Loading branch information
troopa81 authored and m-kuhn committed Dec 27, 2021
1 parent 54946c9 commit dbc4f592f0dd137173f21a8f44f50b6bd71b80b3
Showing with 55 additions and 1 deletion.
  1. +1 −1 src/gui/qgsexternalresourcewidget.cpp
  2. +1 −0 src/gui/qgsexternalresourcewidget.h
  3. +53 −0 tests/src/gui/testqgsexternalresourcewidgetwrapper.cpp
@@ -217,7 +217,7 @@ void QgsExternalResourceWidget::loadDocument( const QString &path )
#ifdef WITH_QTWEBKIT
if ( mDocumentViewerContent == Web )
{
mWebView->setUrl( QUrl( QStringLiteral( "about:blank" ) ) );
mWebView->load( QUrl( QStringLiteral( "about:blank" ) ) );
}
#endif
if ( mDocumentViewerContent == Image )
@@ -171,6 +171,7 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
QWebView *mWebView = nullptr;
#endif

friend class TestQgsExternalResourceWidgetWrapper;
};

#endif // QGSEXTERNALRESOURCEWIDGET_H
@@ -22,6 +22,13 @@

#include <QLineEdit>

#ifdef WITH_QTWEBKIT
#include <QWebFrame>
#include <QWebView>
#endif

#define SAMPLE_IMAGE QStringLiteral( "%1/sample_image.png" ).arg( TEST_DATA_DIR )

/**
* @ingroup UnitTests
* This is a unit test for the external resource widget wrapper
@@ -37,6 +44,7 @@ class TestQgsExternalResourceWidgetWrapper : public QObject
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void test_setNullValues();
void testBlankAfterValue();

private:
std::unique_ptr<QgsVectorLayer> vl;
@@ -63,6 +71,14 @@ void TestQgsExternalResourceWidgetWrapper::init()
vl = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "NoGeometry?field=link:string" ),
QStringLiteral( "myvl" ),
QLatin1String( "memory" ) );

QgsFeature feat1( vl->fields(), 1 );
feat1.setAttribute( QStringLiteral( "type" ), QStringLiteral( "type1" ) );
vl->dataProvider()->addFeature( feat1 );

QgsFeature feat2( vl->fields(), 2 );
feat2.setAttribute( QStringLiteral( "type" ), QStringLiteral( "type2" ) );
vl->dataProvider()->addFeature( feat2 );
}

void TestQgsExternalResourceWidgetWrapper::cleanup()
@@ -99,5 +115,42 @@ void TestQgsExternalResourceWidgetWrapper::test_setNullValues()
delete widget;
}

void TestQgsExternalResourceWidgetWrapper::testBlankAfterValue()
{
// test that application doesn't crash when we set a blank page in web preview
// after an item have been set

QgsExternalResourceWidgetWrapper ww( vl.get(), 0, nullptr, nullptr );
QWidget *widget = ww.createWidget( nullptr );
QVERIFY( widget );

QVariantMap config;
config.insert( QStringLiteral( "DocumentViewer" ), QgsExternalResourceWidget::Web );
ww.setConfig( config );

QgsFeature feat = vl->getFeature( 1 );
QVERIFY( feat.isValid() );
ww.setFeature( feat );

ww.initWidget( widget );
QVERIFY( ww.mQgsWidget );

widget->show();

QEventLoop loop;
connect( ww.mQgsWidget->mWebView, &QWebView::loadFinished, &loop, &QEventLoop::quit );

ww.setValues( QString( "file://%1" ).arg( SAMPLE_IMAGE ), QVariantList() );

QVERIFY( ww.mQgsWidget->mWebView->isVisible() );

loop.exec();

ww.setValues( QString(), QVariantList() );

QVERIFY( ww.mQgsWidget->mWebView->isVisible() );
QCOMPARE( ww.mQgsWidget->mWebView->url().toString(), QStringLiteral( "about:blank" ) );
}

QGSTEST_MAIN( TestQgsExternalResourceWidgetWrapper )
#include "testqgsexternalresourcewidgetwrapper.moc"

0 comments on commit dbc4f59

Please sign in to comment.