1 |
# Copyright (c) 2002, 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 Thuban for details. |
7 |
|
8 |
""" |
9 |
Test the Map class |
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.messages import CHANGED, MAP_PROJECTION_CHANGED, \ |
23 |
MAP_LAYERS_CHANGED, MAP_LAYERS_ADDED, MAP_LAYERS_REMOVED,\ |
24 |
MAP_STACKING_CHANGED, LAYER_VISIBILITY_CHANGED, LAYER_LEGEND_CHANGED, \ |
25 |
LAYER_CHANGED |
26 |
|
27 |
from Thuban.Model.session import Session |
28 |
from Thuban.Model.map import Map |
29 |
from Thuban.Model.layer import Layer |
30 |
from Thuban.Model.proj import Projection |
31 |
from Thuban.Model.color import Color |
32 |
|
33 |
|
34 |
class TestMapSimple(unittest.TestCase): |
35 |
|
36 |
"""Very simple test cases for Map""" |
37 |
|
38 |
def test_initial_state(self): |
39 |
"""Test Map's initial state""" |
40 |
map = Map("Test Map") |
41 |
self.assertEquals(map.Title(), "Test Map") |
42 |
self.assertEquals(map.Layers(), []) |
43 |
label_layer = map.LabelLayer() |
44 |
self.assertEquals(label_layer.Title(), "Labels") |
45 |
self.assertEquals(label_layer.Labels(), []) |
46 |
self.failIf(map.WasModified()) |
47 |
map.Destroy() |
48 |
|
49 |
def test_empty_map(self): |
50 |
"""Test empty Map""" |
51 |
map = Map("Test Map") |
52 |
self.assertEquals(map.BoundingBox(), None) |
53 |
self.assertEquals(map.ProjectedBoundingBox(), None) |
54 |
self.failIf(map.HasLayers()) |
55 |
map.Destroy() |
56 |
|
57 |
|
58 |
class TestMapBase(unittest.TestCase, support.SubscriberMixin): |
59 |
|
60 |
"""Base class for Map test cases that test messages""" |
61 |
|
62 |
def setUp(self): |
63 |
""" |
64 |
Clear the message list, create self.map and subscribe to its messages |
65 |
""" |
66 |
self.clear_messages() |
67 |
|
68 |
# Create a Map and subscribe to all interesting channels. |
69 |
self.map = Map("Test Map") |
70 |
for channel in (CHANGED, |
71 |
MAP_PROJECTION_CHANGED, |
72 |
MAP_LAYERS_CHANGED, |
73 |
MAP_LAYERS_ADDED, |
74 |
MAP_LAYERS_REMOVED, |
75 |
MAP_STACKING_CHANGED, |
76 |
LAYER_VISIBILITY_CHANGED, |
77 |
LAYER_LEGEND_CHANGED, |
78 |
LAYER_CHANGED): |
79 |
self.map.Subscribe(channel, self.subscribe_with_params, channel) |
80 |
|
81 |
def tearDown(self): |
82 |
"""Destroy self.map and self.session and clear the message list""" |
83 |
if hasattr(self, "session"): |
84 |
self.session.Destroy() |
85 |
self.session = None |
86 |
self.map.Destroy() |
87 |
self.map = None |
88 |
self.clear_messages() |
89 |
|
90 |
|
91 |
class TestMapAddLayer(TestMapBase): |
92 |
|
93 |
"""Simple test cases involving messages""" |
94 |
|
95 |
def test_add_layer(self): |
96 |
"""Test Map.AddLayer""" |
97 |
# make sure the created Map is unmodified |
98 |
session = self.session = Session("Test session for %s" %self.__class__) |
99 |
self.failIf(self.map.WasModified()) |
100 |
self.failIf(self.map.HasLayers()) |
101 |
|
102 |
# add a layer and check the result |
103 |
filename = os.path.join("..", "Data", "iceland", "roads-line.shp") |
104 |
roads = Layer("Roads", session.OpenShapefile(filename)) |
105 |
self.map.AddLayer(roads) |
106 |
self.assertEquals(self.map.Layers(), [roads]) |
107 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
108 |
(self.map, MAP_LAYERS_ADDED)]) |
109 |
self.assert_(self.map.WasModified()) |
110 |
self.assert_(self.map.HasLayers()) |
111 |
|
112 |
|
113 |
class TestMapWithContents(TestMapBase, support.FloatComparisonMixin): |
114 |
|
115 |
"""More complex Map test cases with messages that. |
116 |
|
117 |
All test cases here start with a non-empty map. |
118 |
""" |
119 |
|
120 |
def setUp(self): |
121 |
"""Extend the inherited method to also fill the Map. |
122 |
|
123 |
Put some layers into the map created by the inherited method and |
124 |
reset its modified flag. Make also sure that the list of |
125 |
received messages is empty. |
126 |
""" |
127 |
TestMapBase.setUp(self) |
128 |
self.session = Session("Test session for %s" % self.__class__) |
129 |
open_shp = self.session.OpenShapefile |
130 |
self.arc_layer = Layer("Roads", |
131 |
open_shp(os.path.join("..", "Data", "iceland", |
132 |
"roads-line.shp"))) |
133 |
self.poly_layer = Layer("Political", |
134 |
open_shp(os.path.join("..", "Data", "iceland", |
135 |
"political.shp"))) |
136 |
self.map.AddLayer(self.arc_layer) |
137 |
self.map.AddLayer(self.poly_layer) |
138 |
self.map.UnsetModified() |
139 |
self.clear_messages() |
140 |
|
141 |
def tearDown(self): |
142 |
TestMapBase.tearDown(self) |
143 |
self.session = None |
144 |
self.map = None |
145 |
self.poly_layer = self.arc_layer = None |
146 |
|
147 |
def test_remove_layer(self): |
148 |
"""Test Map.RemoveLayer""" |
149 |
self.map.RemoveLayer(self.arc_layer) |
150 |
self.assert_(self.map.WasModified()) |
151 |
self.assertEquals(self.map.Layers(), [self.poly_layer]) |
152 |
self.map.UnsetModified() |
153 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
154 |
(self.map, MAP_LAYERS_REMOVED), |
155 |
(CHANGED,)]) |
156 |
|
157 |
def test_clear_layers(self): |
158 |
"""Test Map.ClearLayers""" |
159 |
self.map.ClearLayers() |
160 |
self.assertEquals(self.map.Layers(), []) |
161 |
self.assertEquals(self.map.LabelLayer().Labels(), []) |
162 |
self.check_messages([(MAP_LAYERS_CHANGED,), |
163 |
(self.map, MAP_LAYERS_CHANGED), |
164 |
(self.map, MAP_LAYERS_REMOVED)]) |
165 |
self.assert_(self.map.WasModified()) |
166 |
self.failIf(self.map.HasLayers()) |
167 |
|
168 |
def test_raise_layer(self): |
169 |
"""Test Map.RaiseLayer""" |
170 |
self.map.RaiseLayer(self.arc_layer) |
171 |
self.assertEquals(self.map.Layers(), [self.poly_layer, self.arc_layer]) |
172 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
173 |
(self.map, MAP_STACKING_CHANGED)]) |
174 |
self.assert_(self.map.WasModified()) |
175 |
|
176 |
def test_raise_layer_top(self): |
177 |
"""Test Map.MoveLayerToTop""" |
178 |
open_shp = self.session.OpenShapefile |
179 |
dummy = Layer("Roads", |
180 |
open_shp(os.path.join("..", "Data", "iceland", |
181 |
"roads-line.shp"))) |
182 |
self.map.AddLayer(dummy) |
183 |
self.clear_messages() |
184 |
|
185 |
self.map.MoveLayerToTop(self.poly_layer) |
186 |
self.assertEquals(self.map.Layers(), |
187 |
[self.arc_layer, dummy, self.poly_layer]) |
188 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
189 |
(self.map, MAP_STACKING_CHANGED)]) |
190 |
self.assert_(self.map.WasModified()) |
191 |
|
192 |
self.map.RemoveLayer(dummy) |
193 |
|
194 |
def test_lower_layer_bottom(self): |
195 |
"""Test Map.MoveLayerToBottom""" |
196 |
open_shp = self.session.OpenShapefile |
197 |
dummy = Layer("Roads", |
198 |
open_shp(os.path.join("..", "Data", "iceland", |
199 |
"roads-line.shp"))) |
200 |
self.map.AddLayer(dummy) |
201 |
self.clear_messages() |
202 |
|
203 |
self.map.MoveLayerToBottom(dummy) |
204 |
self.assertEquals(self.map.Layers(), |
205 |
[dummy, self.arc_layer, self.poly_layer]) |
206 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
207 |
(self.map, MAP_STACKING_CHANGED)]) |
208 |
self.assert_(self.map.WasModified()) |
209 |
|
210 |
self.map.RemoveLayer(dummy) |
211 |
|
212 |
def test_raise_highest_layer(self): |
213 |
"""Test Map.RaiseLayer with highest layer |
214 |
|
215 |
Attempting to raise the highest layer should not modify the map. |
216 |
In particular it should not send any messages. |
217 |
""" |
218 |
self.map.RaiseLayer(self.poly_layer) |
219 |
self.assertEquals(self.map.Layers(), [self.arc_layer, self.poly_layer]) |
220 |
self.check_messages([]) |
221 |
self.failIf(self.map.WasModified()) |
222 |
|
223 |
def test_lower_layer(self): |
224 |
"""Test Map.LowerLayer""" |
225 |
self.map.LowerLayer(self.poly_layer) |
226 |
self.assertEquals(self.map.Layers(), [self.poly_layer, self.arc_layer]) |
227 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
228 |
(self.map, MAP_STACKING_CHANGED)]) |
229 |
self.assert_(self.map.WasModified()) |
230 |
|
231 |
def test_lower_lowest_layer(self): |
232 |
"""Test Map.LowerLayer with lowest layer. |
233 |
|
234 |
Attempting to lower the lowest layer should not modify the map. |
235 |
In particular it should not send any messages. |
236 |
""" |
237 |
self.map.LowerLayer(self.arc_layer) |
238 |
self.assertEquals(self.map.Layers(), [self.arc_layer, self.poly_layer]) |
239 |
self.check_messages([]) |
240 |
self.failIf(self.map.WasModified()) |
241 |
|
242 |
def test_bounding_box(self): |
243 |
"""Test Map.BoundingBox""" |
244 |
self.assertFloatSeqEqual(self.map.BoundingBox(), |
245 |
(-24.546524047851562, 63.286754608154297, |
246 |
-13.495815277099609, 66.563774108886719)) |
247 |
|
248 |
def test_projected_bounding_box(self): |
249 |
"""Test Map.ProjectedBoundingBox""" |
250 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
251 |
self.map.SetProjection(proj) |
252 |
self.assertFloatSeqEqual(self.map.ProjectedBoundingBox(), |
253 |
(608873.03380603762, 7019694.6517963577, |
254 |
1173560.0288053728, 7447353.2203218574), |
255 |
epsilon = 1e-5) |
256 |
|
257 |
def test_set_projection(self): |
258 |
"""Test Map.SetProjection""" |
259 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
260 |
self.map.SetProjection(proj) |
261 |
self.check_messages([(self.map, None, MAP_PROJECTION_CHANGED)]) |
262 |
self.assert_(self.map.WasModified()) |
263 |
|
264 |
def test_tree_info(self): |
265 |
"""Test Map.TreeInfo""" |
266 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
267 |
self.map.SetProjection(proj) |
268 |
# compute the extent string because there are platform |
269 |
# differences in the way %g is handled: |
270 |
# glibc: "%g" % 7.01969e+06 == "7.01969e+06" |
271 |
# w32/VC: "%g" % 7.01969e+06 == "7.01969e+006" |
272 |
extent = 'Extent (projected): (%g, %g, %g, %g)'\ |
273 |
% (608873, 7.01969e+06, 1.17356e+06, 7.44735e+06) |
274 |
self.assertEquals(self.map.TreeInfo(), |
275 |
('Map: Test Map', |
276 |
[('Extent (lat-lon):' |
277 |
' (-24.5465, 63.2868, -13.4958, 66.5638)'), |
278 |
extent, |
279 |
('Projection', |
280 |
['zone=26', 'proj=utm', 'ellps=clrk66']), |
281 |
self.poly_layer, |
282 |
self.arc_layer])) |
283 |
|
284 |
def test_forwarding_visibility(self): |
285 |
"""Test Map's forwarding of Layer.SetVisible messages""" |
286 |
self.poly_layer.SetVisible(0) |
287 |
self.check_messages([(self.poly_layer, LAYER_VISIBILITY_CHANGED)]) |
288 |
|
289 |
def test_unset_modified(self): |
290 |
"""Test Map.UnsetModified. |
291 |
|
292 |
Test whether a change to a layer results in the map being |
293 |
considered modified and test whether then calling the map's |
294 |
UnsetModified clears the changed flag in the layer as well. |
295 |
""" |
296 |
self.failIf(self.map.WasModified()) |
297 |
self.poly_layer.GetClassification().SetDefaultFill(Color(0.0, 0.5, 1.0)) |
298 |
self.assert_(self.map.WasModified()) |
299 |
self.map.UnsetModified() |
300 |
self.failIf(self.map.WasModified()) |
301 |
self.failIf(self.poly_layer.WasModified()) |
302 |
self.check_messages([(self.poly_layer, LAYER_CHANGED), |
303 |
(CHANGED,)]) |
304 |
|
305 |
if __name__ == "__main__": |
306 |
support.run_tests() |
307 |
|