Skip to content
Permalink
Browse files

Correctly handle empty paths in QgsImageCache

  • Loading branch information
nyalldawson committed Dec 5, 2018
1 parent 58219d6 commit bafda2451b2f28dafc160b1afbd8e8c68fc44833
Showing with 26 additions and 1 deletion.
  1. +9 −1 src/core/qgsimagecache.cpp
  2. +17 −0 tests/src/core/testqgsimagecache.cpp
@@ -101,8 +101,13 @@ QgsImageCache::QgsImageCache( QObject *parent )
connect( this, &QgsAbstractContentCacheBase::remoteContentFetched, this, &QgsImageCache::remoteImageFetched );
}

QImage QgsImageCache::pathAsImage( const QString &file, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache )
QImage QgsImageCache::pathAsImage( const QString &f, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache )
{
const QString file = f.trimmed();

if ( file.isEmpty() )
return QImage();

QMutexLocker locker( &mMutex );

fitsInCache = true;
@@ -141,6 +146,9 @@ QImage QgsImageCache::pathAsImage( const QString &file, const QSize size, const

QSize QgsImageCache::originalSize( const QString &path ) const
{
if ( path.isEmpty() )
return QSize();

// direct read if path is a file -- maybe more efficient than going the bytearray route? (untested!)
if ( QFile::exists( path ) )
{
@@ -53,6 +53,7 @@ class TestQgsImageCache : public QObject
void size(); // check various size-specific handling
void opacity(); // check non-opaque image rendering
void base64();
void empty();

};

@@ -254,6 +255,22 @@ void TestQgsImageCache::base64()
QCOMPARE( size.height(), 60 );
}

void TestQgsImageCache::empty()
{
QgsImageCache cache;
bool inCache = false;

QImage img = cache.pathAsImage( QString(), QSize( 200, 200 ), true, 1.0, inCache );
QVERIFY( img.isNull() );

QVERIFY( !cache.originalSize( QString() ).isValid() );

img = cache.pathAsImage( QStringLiteral( " " ), QSize( 200, 200 ), true, 1.0, inCache );
QVERIFY( img.isNull() );

QVERIFY( !cache.originalSize( QStringLiteral( " " ) ).isValid() );
}

bool TestQgsImageCache::imageCheck( const QString &testName, QImage &image, int mismatchCount )
{
//draw background

0 comments on commit bafda24

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