Skip to content

Commit

Permalink
[ExternalResourceWidget] Fix crash when loading blank after a page
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 authored and nyalldawson committed Dec 2, 2021
1 parent 0d7e248 commit 701bced
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/gui/qgsexternalresourcewidget.cpp
Expand Up @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsexternalresourcewidget.h
Expand Up @@ -171,6 +171,7 @@ class GUI_EXPORT QgsExternalResourceWidget : public QWidget
QWebView *mWebView = nullptr;
#endif

friend class TestQgsExternalResourceWidgetWrapper;
};

#endif // QGSEXTERNALRESOURCEWIDGET_H
53 changes: 53 additions & 0 deletions tests/src/gui/testqgsexternalresourcewidgetwrapper.cpp
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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 701bced

Please sign in to comment.