diff --git a/CHANGELOG.md b/CHANGELOG.md index a932016..65cc6c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,11 @@ * Add `clean_qgis_layer` decorator back alongside with automatic cleaning [#45](https://github.com/GispoCoding/pytest-qgis/pull/45) - ## Fixes -* [#55](https://github.com/GispoCoding/pytest-qgis/pull/55) Allows using MagicMocks to mock layers without problems +* [#53](https://github.com/GispoCoding/pytest-qgis/pull/53) Allow using MagicMocks to mock layers without problems * [#60](https://github.com/GispoCoding/pytest-qgis/pull/60) Allows using CRS properly again +* [#62](https://github.com/GispoCoding/pytest-qgis/pull/62) Map does no longer zoom to the first added layer upon processing the events when using `qgis_show_map` marker # Version 2.0.0 (29-11-2023) diff --git a/src/pytest_qgis/pytest_qgis.py b/src/pytest_qgis/pytest_qgis.py index 3b06657..6e36e91 100644 --- a/src/pytest_qgis/pytest_qgis.py +++ b/src/pytest_qgis/pytest_qgis.py @@ -158,6 +158,7 @@ def qgis_app(request: "SubRequest") -> QgsApplication: if not request.config._plugin_settings.qgis_init_disabled: assert _APP + QgsProject.instance().legendLayersAdded.disconnect(_APP.processEvents) if not sip.isdeleted(_CANVAS) and _CANVAS is not None: _CANVAS.deleteLater() _APP.exitQgis() @@ -303,6 +304,16 @@ def _start_and_configure_qgis_app(config: "Config") -> None: mock.patch("qgis.utils.iface", _IFACE).start() + if _APP is not None: + # QGIS zooms to the layer's extent if it + # is the first layer added to the map. + # If the qgis_show_map marker is used, this zooming might occur + # at some later time when events are processed (e.g. at qtbot.wait call) + # and this might change the extent unexpectedly. + # It is better to process events right after adding the + # layer to avoid these kind of problems. + QgsProject.instance().legendLayersAdded.connect(_APP.processEvents) + def _initialize_processing(qgis_app: QgsApplication) -> None: python_plugins_path = os.path.join(qgis_app.pkgDataPath(), "python", "plugins") diff --git a/tests/visual/test_show_map.py b/tests/visual/test_show_map.py index 812e4e8..1f6c3c5 100644 --- a/tests/visual/test_show_map.py +++ b/tests/visual/test_show_map.py @@ -126,3 +126,19 @@ def test_show_map_crs_change_to_4326_2(layer_polygon, layer_points, layer_polygo QgsProject.instance().addMapLayers( [layer_points, layer_polygon_3067, layer_polygon] ) + + +@pytest.mark.qgis_show_map(timeout=DEFAULT_TIMEOUT, zoom_to_common_extent=False) +def test_map_extent_should_not_change_to_layers_extent_when_processing_events( + layer_polygon_3067, qgis_canvas, qgis_app +): + extent_smaller_than_layer = QgsRectangle(475804, 7145949.5, 549226, 7219371.5) + + QgsProject.instance().addMapLayer(layer_polygon_3067) + qgis_canvas.setExtent(extent_smaller_than_layer) + + # This triggers the map to set the extent based on the layer + # if events are not processed after adding the layer + qgis_app.processEvents() + + assert qgis_canvas.extent().height() == extent_smaller_than_layer.height()