Skip to content

Commit

Permalink
[tests] Switch composer picture remote url test to use a local
Browse files Browse the repository at this point in the history
python server, avoids dependance on remote service
  • Loading branch information
nyalldawson committed Jul 2, 2015
1 parent 9b976cf commit b7afe39
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 20 deletions.
16 changes: 0 additions & 16 deletions tests/src/core/testqgscomposerpicture.cpp
Expand Up @@ -60,8 +60,6 @@ class TestQgsComposerPicture : public QObject
void pictureExpression();
void pictureInvalidExpression();

void pictureRemoteUrl(); //test fetching picture from a url

private:
QgsComposition* mComposition;
QgsComposerPicture* mComposerPicture;
Expand Down Expand Up @@ -386,19 +384,5 @@ void TestQgsComposerPicture::pictureInvalidExpression()
mComposerPicture->setUsePictureExpression( false );
}

void TestQgsComposerPicture::pictureRemoteUrl()
{
//test picture source via bad expression
mComposition->addComposerPicture( mComposerPicture );

mComposerPicture->setPicturePath( "http://www.qgis.org/en/_static/logo.png" );

QgsCompositionChecker checker( "composerpicture_remote", mComposition );
QVERIFY( checker.testComposition( mReport, 0, 0 ) );

mComposition->removeItem( mComposerPicture );
mComposerPicture->setUsePictureExpression( false );
}

QTEST_MAIN( TestQgsComposerPicture )
#include "testqgscomposerpicture.moc"
9 changes: 5 additions & 4 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -27,10 +27,11 @@ ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
ADD_PYTHON_TEST(PyQgsAnalysis test_qgsanalysis.py)
ADD_PYTHON_TEST(PyQgsColorScheme test_qgscolorscheme.py)
ADD_PYTHON_TEST(PyQgsColorSchemeRegistry test_qgscolorschemeregistry.py)
ADD_PYTHON_TEST(PyQgsComposerMap test_qgscomposermap.py)
ADD_PYTHON_TEST(PyQgsComposerMapGrid test_qgscomposermapgrid.py)
ADD_PYTHON_TEST(PyQgsComposerEffects test_qgscomposereffects.py)
ADD_PYTHON_TEST(PyQgsComposerShapes test_qgscomposershapes.py)
ADD_PYTHON_TEST(PyQgsComposerMap test_qgscomposermap.py)
ADD_PYTHON_TEST(PyQgsComposerMapGrid test_qgscomposermapgrid.py)
ADD_PYTHON_TEST(PyQgsComposerEffects test_qgscomposereffects.py)
ADD_PYTHON_TEST(PyQgsComposerShapes test_qgscomposershapes.py)
ADD_PYTHON_TEST(PyQgsComposerPicture test_qgscomposerpicture.py)
ADD_PYTHON_TEST(PyQgsSymbolLayerV2 test_qgssymbollayerv2.py)
ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
ADD_PYTHON_TEST(PyQgsAtlasComposition test_qgsatlascomposition.py)
Expand Down
91 changes: 91 additions & 0 deletions tests/src/python/test_qgscomposerpicture.py
@@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsComposerPicture.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = '(C) 2015 by Nyall Dawson'
__date__ = '02/07/2015'
__copyright__ = 'Copyright 2015, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis
import os
import sys
import SocketServer
import threading
import SimpleHTTPServer
from PyQt4.QtGui import QPainter, QColor
from PyQt4.QtCore import QRectF, QCoreApplication

from qgis.core import (QgsComposerPicture,
QgsComposition,
QgsMapSettings
)
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest
)
from qgscompositionchecker import QgsCompositionChecker

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
TEST_DATA_DIR = unitTestDataPath()

class TestQgsComposerPicture(TestCase):

@classmethod
def setUpClass(cls):
# Bring up a simple HTTP server, for remote picture tests
os.chdir( unitTestDataPath() + '' )

This comment has been minimized.

Copy link
@luipir

luipir Jul 2, 2015

Contributor

Hi nyaal... just reading code to learn :)
what does it means +'',
bye

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Jul 2, 2015

Author Collaborator

@luipir a mistake ;)

handler = SimpleHTTPServer.SimpleHTTPRequestHandler

cls.httpd = SocketServer.TCPServer(('localhost', 0), handler)
cls.port = cls.httpd.server_address[1]

cls.httpd_thread = threading.Thread( target=cls.httpd.serve_forever )
cls.httpd_thread.setDaemon( True )
cls.httpd_thread.start()

def __init__(self, methodName):
"""Run once on class initialisation."""
unittest.TestCase.__init__(self, methodName)

TEST_DATA_DIR = unitTestDataPath()
self.pngImage = TEST_DATA_DIR + "/sample_image.png";

# create composition
self.mapSettings = QgsMapSettings()
self.composition = QgsComposition(self.mapSettings)
self.composition.setPaperSize(297, 210)

self.composerPicture = QgsComposerPicture( self.composition )
self.composerPicture.setPicturePath( self.pngImage )
self.composerPicture.setSceneRect( QRectF( 70, 70, 100, 100 ) )
self.composerPicture.setFrameEnabled( True )
self.composition.addComposerPicture(self.composerPicture)

def testResizeZoom(self):
"""Test picture resize zoom mode."""
self.composerPicture.setResizeMode( QgsComposerPicture.Zoom )

checker = QgsCompositionChecker('composerpicture_resize_zoom', self.composition)
testResult, message = checker.testComposition()

assert testResult, message

def testRemoteImage(self):
"""Test fetching remote picture."""
self.composerPicture.setPicturePath( 'http://localhost:' + str( TestQgsComposerPicture.port ) + '/qgis_local_server/logo.png' )

checker = QgsCompositionChecker('composerpicture_remote', self.composition)
testResult, message = checker.testComposition()

self.composerPicture.setPicturePath( self.pngImage )
assert testResult, message

if __name__ == '__main__':
unittest.main()
Binary file added tests/testdata/qgis_local_server/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b7afe39

Please sign in to comment.