|
18 | 18 | from mimetools import Message
|
19 | 19 | from StringIO import StringIO
|
20 | 20 | from qgis.server import QgsServer
|
21 |
| -from qgis.core import QgsMessageLog |
| 21 | +from qgis.core import QgsMessageLog, QgsRenderChecker |
22 | 22 | from qgis.testing import unittest
|
| 23 | +from qgis.PyQt.QtCore import QSize |
23 | 24 | from utilities import unitTestDataPath
|
24 | 25 | import osgeo.gdal
|
| 26 | +import tempfile |
| 27 | +import base64 |
25 | 28 |
|
26 | 29 | # Strip path and content length because path may vary
|
27 | 30 | RE_STRIP_PATH = r'MAP=[^&]+|Content-Length: \d+'
|
@@ -372,6 +375,82 @@ def test_getLegendGraphics(self):
|
372 | 375 | self.assertEquals(-1, h.find('Content-Type: text/xml; charset=utf-8'), "Header: %s\nResponse:\n%s" % (h, r))
|
373 | 376 | self.assertNotEquals(-1, h.find('Content-Type: image/png'), "Header: %s\nResponse:\n%s" % (h, r))
|
374 | 377 |
|
| 378 | + def test_getLegendGraphics_layertitle(self): |
| 379 | + """Test that does not return an exception but an image""" |
| 380 | + parms = { |
| 381 | + 'MAP': self.testdata_path + "test%2Bproject.qgs", |
| 382 | + 'SERVICE': 'WMS', |
| 383 | + 'VERSION': '1.3.0', |
| 384 | + 'REQUEST': 'GetLegendGraphic', |
| 385 | + 'FORMAT': 'image/png', |
| 386 | + #'WIDTH': '20', # optional |
| 387 | + #'HEIGHT': '20', # optional |
| 388 | + 'LAYER': u'testlayer+èé', |
| 389 | + 'LAYERTITLE': 'TRUE', |
| 390 | + } |
| 391 | + qs = '&'.join([u"%s=%s" % (k, v) for k, v in parms.iteritems()]) |
| 392 | + r, h = self._result(self.server.handleRequest(qs)) |
| 393 | + self._img_diff_error(r, h, "WMS_GetLegendGraphic_test", 250, QSize(10, 10)) |
| 394 | + |
| 395 | + parms = { |
| 396 | + 'MAP': self.testdata_path + "test%2Bproject.qgs", |
| 397 | + 'SERVICE': 'WMS', |
| 398 | + 'VERSION': '1.3.0', |
| 399 | + 'REQUEST': 'GetLegendGraphic', |
| 400 | + 'FORMAT': 'image/png', |
| 401 | + #'WIDTH': '20', # optional |
| 402 | + #'HEIGHT': '20', # optional |
| 403 | + 'LAYER': u'testlayer+èé', |
| 404 | + 'LAYERTITLE': 'FALSE', |
| 405 | + } |
| 406 | + qs = '&'.join([u"%s=%s" % (k, v) for k, v in parms.iteritems()]) |
| 407 | + r, h = self._result(self.server.handleRequest(qs)) |
| 408 | + self._img_diff_error(r, h, "WMS_GetLegendGraphic_test_layertitle_false", 250, QSize(10, 10)) |
| 409 | + |
| 410 | + def _result(self, data): |
| 411 | + headers = {} |
| 412 | + for line in data[0].decode('UTF-8').split("\n"): |
| 413 | + if line != "": |
| 414 | + header = line.split(":") |
| 415 | + self.assertEqual(len(header), 2, line) |
| 416 | + headers[str(header[0])] = str(header[1]).strip() |
| 417 | + |
| 418 | + return data[1], headers |
| 419 | + |
| 420 | + def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize()): |
| 421 | + temp_image = os.path.join(tempfile.gettempdir(), "%s_result.png" % control_image) |
| 422 | + |
| 423 | + with open(temp_image, "wb") as f: |
| 424 | + f.write(image) |
| 425 | + |
| 426 | + control = QgsRenderChecker() |
| 427 | + control.setControlPathPrefix("qgis_server") |
| 428 | + control.setControlName(control_image) |
| 429 | + control.setRenderedImage(temp_image) |
| 430 | + if max_size_diff.isValid(): |
| 431 | + control.setSizeTolerance(max_size_diff.width(), max_size_diff.height()) |
| 432 | + return control.compareImages(control_image), control.report() |
| 433 | + |
| 434 | + def _img_diff_error(self, response, headers, image, max_diff=10, max_size_diff=QSize()): |
| 435 | + self.assertEqual( |
| 436 | + headers.get("Content-Type"), "image/png", |
| 437 | + "Content type is wrong: %s" % headers.get("Content-Type")) |
| 438 | + test, report = self._img_diff(response, image, max_diff, max_size_diff) |
| 439 | + |
| 440 | + with open(os.path.join(tempfile.gettempdir(), image + "_result.png"), "rb") as rendered_file: |
| 441 | + encoded_rendered_file = base64.b64encode(rendered_file.read()) |
| 442 | + message = "Image is wrong\n%s\nImage:\necho '%s' | base64 -d >%s/%s_result.png" % ( |
| 443 | + report, encoded_rendered_file.strip(), tempfile.gettempdir(), image |
| 444 | + ) |
| 445 | + |
| 446 | + with open(os.path.join(tempfile.gettempdir(), image + "_result_diff.png"), "rb") as diff_file: |
| 447 | + encoded_diff_file = base64.b64encode(diff_file.read()) |
| 448 | + message += "\nDiff:\necho '%s' | base64 -d > %s/%s_result_diff.png" % ( |
| 449 | + encoded_diff_file.strip(), tempfile.gettempdir(), image |
| 450 | + ) |
| 451 | + |
| 452 | + self.assertTrue(test, message) |
| 453 | + |
375 | 454 | # The following code was used to test type conversion in python bindings
|
376 | 455 | # def test_qpair(self):
|
377 | 456 | # """Test QPair bindings"""
|
|
0 commit comments