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