/[thuban]/branches/WIP-pyshapelib-bramz/test/test_selection.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/test/test_selection.py

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26