1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
# Copyright (C) 2003 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with the software for details.
"""
Test cases for Thuban.UI.selection
"""
__version__ = "$Revision: 723 $"
# $Source$
# $Id: test_selection.py 723 2003-04-24 15:31:53Z bh $
import os
import unittest
import support
support.initthuban()
from Thuban.Model.session import Session
from Thuban.Model.map import Map
from Thuban.Model.layer import Layer
from Thuban.UI.selection import Selection
from Thuban.UI.messages import LAYER_SELECTED, SHAPES_SELECTED
class TestSelection(unittest.TestCase, support.SubscriberMixin):
def setUp(self):
"""Instantiate a selection.
Test cases implemented in this class can access the selection as
self.selection.
Also, subscribe self.subscribe_with_params to some of the
selection's messages.
Finally, create a list self.to_destroy with objects to be
destroyes by calling their destroy method in tearDown() for
objects created in test cases that need to be destroyed.
"""
self.clear_messages()
self.selection = Selection()
for channel in (LAYER_SELECTED, SHAPES_SELECTED):
self.selection.Subscribe(channel, self.subscribe_with_params,
channel)
self.to_destroy = [self.selection]
def tearDown(self):
"""Destroy all objects in self.to_destroy and clear the message list"""
for obj in self.to_destroy:
obj.Destroy()
self.to_destroy = None
self.session = None
self.selection = None
self.clear_messages()
def get_layer(self):
"""Return a layer to have something to test with.
Also, instantiate self.session if not done already. The layer
(and the session when it is created) are added to
self.to_destroy so that they are properly destroyed at the end
of the test.
The layer should not be added to a map in the session to avoid a
situation where its destroy method is called twice. This
situation should not arise in the selection tests.
"""
if not hasattr(self, "session"):
self.session = Session("Test Session for %s" % self.__class__)
self.to_destroy.append(self.session)
filename = os.path.join("..", "Data", "iceland", "roads-line.shp")
layer = Layer("Selection Test Layer",
self.session.OpenShapefile(filename))
self.to_destroy.append(layer)
return layer
def test_instatiation(self):
"""Test initial state of the selection"""
self.assertEquals(self.selection.SelectedLayer(), None)
self.assertEquals(self.selection.SelectedShapes(), [])
self.failIf(self.selection.HasSelectedLayer())
#
# SelectLayer Tests
#
def test_select_layer_without_current_selection(self):
"""Test Selection.SelectLayer() without current selection"""
layer = self.get_layer()
self.selection.SelectLayer(layer)
# After selecting a layer, no shapes are selected
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [])
# Since no shape was selected, only a LAYER_SELECTED message
# should have been issued.
self.check_messages([(layer, LAYER_SELECTED)])
def test_select_currently_selected_layer(self):
"""Test Selection.SelectLayer(<currently selected layer>)"""
layer = self.get_layer()
self.selection.SelectLayer(layer)
self.clear_messages()
self.selection.SelectLayer(layer)
# After selecting a layer, no shapes are selected
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [])
# Since nothing has changed, really, no messages should have
# been issued
self.check_messages([])
def test_select_layer_with_previous_selection(self):
"""Test Selection.SelectLayer() with previous selection"""
self.selection.SelectShapes(self.get_layer(), [0])
self.clear_messages()
layer = self.get_layer()
self.selection.SelectLayer(layer)
# After selecting a layer, no shapes are selected
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [])
# Since a shape and a layer was selected, a LAYER_SELECTED and a
# SHAPES_SELECTED message should have been issued.
self.check_messages([(layer, LAYER_SELECTED),
(layer, [], SHAPES_SELECTED)])
def test_select_layer_with_None(self):
"""Test Selection.SelectLayer(None)
Calling SelectLayer with None should deselect it.
"""
self.selection.SelectShapes(self.get_layer(), [0])
self.clear_messages()
self.selection.SelectLayer(None)
# After selecting a layer, no shapes are selected
self.assertEquals(self.selection.SelectedLayer(), None)
self.failIf(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [])
# Since no shape was selected, only a LAYER_SELECTED message
# should have been issued.
self.check_messages([(None, LAYER_SELECTED),
(None, [], SHAPES_SELECTED)])
#
# SelectShapes Tests
#
def test_select_new_layer_and_new_shape(self):
"""Test Selection.SelectShapes(<new layer>, <new shapes>)"""
layer = self.get_layer()
self.selection.SelectShapes(layer, [0, 3, 1])
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [0, 1, 3])
self.check_messages([(layer, LAYER_SELECTED),
(layer, [0, 1, 3], SHAPES_SELECTED)])
def test_select_old_layer_and_old_shape(self):
"""Test Selection.SelectShape(<old layer>, <old shapes>)"""
layer = self.get_layer()
self.selection.SelectShapes(layer, [0, 10, 2])
self.clear_messages()
# Deliberate use a different order of the shape ids to check
# whether they're still considered equal
self.selection.SelectShapes(layer, [2, 0, 10])
# Selecting an already selected layer and shapes should not
# result in any messages but still have the right layer and
# shapes selected
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [0, 2, 10])
self.check_messages([])
def test_select_old_layer_and_new_shape(self):
"""Test Selection.SelectShapes(<old layer>, <new shape>)"""
layer = self.get_layer()
self.selection.SelectShapes(layer, [0])
self.clear_messages()
self.selection.SelectShapes(layer, [1])
# Selecting a different shape in the already selected layer
# should only produce a SHAPES_SELECTED message
# After selecting a shape, both a shape and a layer are selected
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [1])
self.check_messages([(layer, [1], SHAPES_SELECTED)])
#
# Adding Shapes Tests
#
def test_add_shapes_new_layer_new_shapes(self):
"""Test Selection.SelectShapes(<same layer>, <new shapes>, add = 1)"""
layer = self.get_layer()
self.selection.SelectShapes(layer, [10, 7], add = 1)
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
# The list of selected shapes will be sorted in ascending order
self.assertEquals(self.selection.SelectedShapes(), [7, 10])
self.check_messages([(layer, LAYER_SELECTED),
(layer, [7, 10], SHAPES_SELECTED)])
def test_add_shapes_same_layer_new_shapes(self):
"""Test Selection.SelectShapes(<same layer>, <new shapes>, add = 1)"""
layer = self.get_layer()
self.selection.SelectShapes(layer, [0, 6, 5])
self.clear_messages()
self.selection.SelectShapes(layer, [10, 7], add = 1)
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
# The list of selected shapes will be sorted in ascending order
self.assertEquals(self.selection.SelectedShapes(), [0, 5, 6, 7, 10])
self.check_messages([(layer, [0, 5, 6, 7, 10], SHAPES_SELECTED)])
def test_add_shapes_same_layer_already_selected_shapes(self):
"""Test Selection.SelectShapes(<same layer>, <some old shapes>, add=1)
"""
layer = self.get_layer()
self.selection.SelectShapes(layer, [0, 6, 5])
self.clear_messages()
self.selection.SelectShapes(layer, [6, 0], add = 1)
self.assertEquals(self.selection.SelectedLayer(), layer)
self.failUnless(self.selection.HasSelectedLayer())
# The list of selected shapes will be sorted in ascending order
self.assertEquals(self.selection.SelectedShapes(), [0, 5, 6])
self.check_messages([])
#
# ClearSelection Tests
#
def test_clear_selection(self):
"""Test Selection.ClearSelection() when nothing is selected"""
self.selection.ClearSelection()
# After clearing the selection nothing is selected
self.assertEquals(self.selection.SelectedLayer(), None)
self.failIf(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [])
# No messages should have been sent because the selection
# doesn't have changed due to the ClearSelection()
self.check_messages([])
def test_clear_selection_with_selected_layer(self):
"""Test Selection.ClearSelection() when a layer is selected"""
self.selection.ClearSelection()
self.selection.SelectLayer(self.get_layer())
self.clear_messages()
self.selection.ClearSelection()
# After clearing the selection nothing is selected
self.assertEquals(self.selection.SelectedLayer(), None)
self.failIf(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [])
# No messages should have been sent because the selection
# doesn't have changed due to the ClearSelection()
self.check_messages([(None, LAYER_SELECTED)])
def test_clear_selection_with_selected_shape(self):
"""Test Selection.ClearSelection() when a layer is selected"""
self.selection.ClearSelection()
self.selection.SelectShapes(self.get_layer(), [0])
self.clear_messages()
self.selection.ClearSelection()
# After clearing the selection nothing is selected
self.assertEquals(self.selection.SelectedLayer(), None)
self.failIf(self.selection.HasSelectedLayer())
self.assertEquals(self.selection.SelectedShapes(), [])
# No messages should have been sent because the selection
# doesn't have changed due to the ClearSelection()
self.check_messages([(None, LAYER_SELECTED),
(None, [], SHAPES_SELECTED)])
if __name__ == "__main__":
support.run_tests()
|