Skip to content
Permalink
Browse files

Add model tester

  • Loading branch information
elpaso committed Oct 26, 2020
1 parent 8924596 commit 2ff56fc0628ca5363cb3676b59b2994460b41e93
Showing with 42 additions and 0 deletions.
  1. +42 −0 tests/src/python/test_qgslayertreeview.py
@@ -14,6 +14,8 @@

import os

from distutils.version import StrictVersion

from qgis.core import (
QgsLayerTreeModel,
QgsProject,
@@ -30,6 +32,16 @@
from qgis.PyQt.QtCore import QStringListModel
from qgis.PyQt.QtTest import QSignalSpy

from qgis.PyQt.Qt import PYQT_VERSION_STR

USE_MODEL_TESTER = False


if StrictVersion(PYQT_VERSION_STR) >= StrictVersion('5.11'):
from qgis.PyQt.QtTest import QAbstractItemModelTester
USE_MODEL_TESTER = True


app = start_app()
TEST_DATA_DIR = unitTestDataPath()

@@ -55,6 +67,8 @@ def __init__(self, methodName):
"layer5", "memory")
self.project.addMapLayers([self.layer, self.layer2, self.layer3])
self.model = QgsLayerTreeModel(self.project.layerTreeRoot())
if USE_MODEL_TESTER:
self.tester = QAbstractItemModelTester(self.model)

def nodeOrder(self, group):

@@ -80,12 +94,16 @@ def testSetModel(self):

# should work
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
self.assertEqual(view.layerTreeModel(), self.model)

def testSetCurrentLayer(self):

view = QgsLayerTreeView()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
current_layer_changed_spy = QSignalSpy(view.currentLayerChanged)
self.assertFalse(view.currentLayer())
view.setCurrentLayer(self.layer3)
@@ -101,6 +119,8 @@ def testSetCurrentLayer(self):
def testDefaultActions(self):
view = QgsLayerTreeView()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)

# show in overview action
@@ -121,6 +141,8 @@ def testMoveOutOfGroupActionLayer(self):
group.addLayer(self.layer5)
groupname = group.name()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.nodeOrder(self.project.layerTreeRoot().children()), [
self.layer.name(),
@@ -148,6 +170,8 @@ def testMoveToTopActionLayer(self):

view = QgsLayerTreeView()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.project.layerTreeRoot().layerOrder(), [self.layer, self.layer2, self.layer3])
view.setCurrentLayer(self.layer3)
@@ -164,6 +188,8 @@ def testMoveToTopActionGroup(self):
group.addLayer(self.layer5)
groupname = group.name()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.nodeOrder(self.project.layerTreeRoot().children()), [
self.layer.name(),
@@ -196,6 +222,8 @@ def testMoveToTopActionEmbeddedGroup(self):
group.addLayer(self.layer5)
groupname = group.name()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.nodeOrder(self.project.layerTreeRoot().children()), [
self.layer.name(),
@@ -227,6 +255,8 @@ def testMoveToTopActionLayerAndGroup(self):
group.addLayer(self.layer5)
groupname = group.name()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.nodeOrder(self.project.layerTreeRoot().children()), [
self.layer.name(),
@@ -259,6 +289,8 @@ def testMoveToBottomActionLayer(self):

view = QgsLayerTreeView()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.project.layerTreeRoot().layerOrder(), [self.layer, self.layer2, self.layer3])
view.setCurrentLayer(self.layer)
@@ -275,6 +307,8 @@ def testMoveToBottomActionGroup(self):
group.addLayer(self.layer5)
groupname = group.name()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.nodeOrder(self.project.layerTreeRoot().children()), [
groupname,
@@ -307,6 +341,8 @@ def testMoveToBottomActionEmbeddedGroup(self):
group.addLayer(self.layer5)
groupname = group.name()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.nodeOrder(self.project.layerTreeRoot().children()), [
self.layer.name(),
@@ -338,6 +374,8 @@ def testMoveToBottomActionLayerAndGroup(self):
group.addLayer(self.layer5)
groupname = group.name()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
actions = QgsLayerTreeViewDefaultActions(view)
self.assertEqual(self.nodeOrder(self.project.layerTreeRoot().children()), [
groupname,
@@ -369,6 +407,8 @@ def testSetLayerVisible(self):

view = QgsLayerTreeView()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
self.project.layerTreeRoot().findLayer(self.layer).setItemVisibilityChecked(True)
self.project.layerTreeRoot().findLayer(self.layer2).setItemVisibilityChecked(True)
self.assertTrue(self.project.layerTreeRoot().findLayer(self.layer).itemVisibilityChecked())
@@ -387,6 +427,8 @@ def testProxyModel(self):

view = QgsLayerTreeView()
view.setModel(self.model)
if USE_MODEL_TESTER:
proxy_tester = QAbstractItemModelTester(view.model())
tree_model = view.layerTreeModel()
proxy_model = view.proxyModel()

0 comments on commit 2ff56fc

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