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