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