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