]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Strategy/BBOX.html
initial commit
[syp.git] / openlayers / tests / Strategy / BBOX.html
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5
6     function test_initialize(t) {
7         t.plan(1);
8
9         var ratio = 4;
10
11         var s = new OpenLayers.Strategy.BBOX({ratio: ratio});
12         t.eq(s.ratio, ratio, "ctor sets ratio");
13     }
14
15     function test_activate(t) {
16         t.plan(5);
17
18         var l = new OpenLayers.Layer.Vector();
19         var s = new OpenLayers.Strategy.BBOX();
20         s.setLayer(l);
21
22         t.eq(s.active, false, "not active after construction");
23
24         var activated = s.activate();
25         t.eq(activated, true, "activate returns true");
26         t.eq(s.active, true, "activated after activate");
27         t.ok(l.events.listeners["moveend"][0].obj == s &&
28              l.events.listeners["moveend"][0].func == s.update,
29              "activates registers moveend listener");
30         t.ok(l.events.listeners["refresh"][0].obj == s &&
31              l.events.listeners["refresh"][0].func == s.update,
32              "activates registers refresh listener");
33     }
34
35     function test_update(t) {
36         t.plan(5);
37
38         // Create a dummy layer that can act as the map base layer.
39         // This will be unnecessary if #1921 is addressed (allowing
40         // map to have different projection than base layer).
41         var dummy = new OpenLayers.Layer(null, {isBaseLayer: true});
42
43         var strategy = new OpenLayers.Strategy.BBOX({
44             ratio: 1 // makes for easier comparison to map bounds
45         });
46         var layer = new OpenLayers.Layer.Vector(null, {
47             isBaseLayer: true,
48             protocol: new OpenLayers.Protocol(),
49             strategies: [strategy]
50         });
51
52         // create a map with the layers and a center
53         var map = new OpenLayers.Map("map");
54         map.addLayers([dummy, layer]);
55         map.zoomToMaxExtent();
56         
57         /**
58          * The setCenter call above should set strategy bounds.  I *think* this
59          * issue is captured in http://trac.openlayers.org/ticket/1835.
60          * For now, I'm going to force an update on the strategy.  This line
61          * should be removed when the issue(s) described in #1835 are addressed.
62          */
63         strategy.update({force: true});
64         
65         // test that the strategy bounds were set
66         t.ok(map.getExtent().equals(strategy.bounds), "[set center] bounds set to map extent");
67         
68         // zoom and test that bounds are not reset
69         var old = strategy.bounds.clone();
70         map.zoomIn();
71         t.ok(strategy.bounds.equals(old), "[zoom in] bounds not reset");
72         
73         // force update and check that bounds change
74         strategy.update({force: true});
75         t.ok(!strategy.bounds.equals(old), "[force update] bounds changed");
76         t.ok(strategy.bounds.equals(map.getExtent()), "[force update] bounds set to map extent");
77         
78         // change the layer projection to confirm strategy uses same
79         layer.projection = new OpenLayers.Projection("EPSG:900913");
80         strategy.update({force: true});
81         var from = map.getProjectionObject();
82         var to = layer.projection;
83         t.ok(strategy.bounds.equals(map.getExtent().transform(from, to)), "[force update different proj] bounds transformed");
84         
85
86     }
87     
88     function test_events(t) {
89         
90         t.plan(2);
91         var log = {
92             loadstart: 0,
93             loadend: 0
94         };
95
96         var map = new OpenLayers.Map("map");
97         var layer = new OpenLayers.Layer.Vector(null, {
98             strategies: [new OpenLayers.Strategy.BBOX()],
99             protocol: new OpenLayers.Protocol({
100                 read: function(config) {
101                     config.callback.call(config.scope, {});
102                 }
103             }),
104             isBaseLayer: true,
105             eventListeners: {
106                 loadstart: function() {
107                     ++log.loadstart;
108                 },
109                 loadend: function() {
110                     ++log.loadend;
111                 }
112             }            
113         });
114         map.addLayer(layer);
115         map.zoomToMaxExtent();
116         
117         t.eq(log.loadstart, 1, "loadstart triggered");
118         t.eq(log.loadend, 1, "loadend triggered");
119         
120         map.destroy();
121         
122     }
123
124     function test_triggerRead(t) {
125         t.plan(4);
126
127         var s = new OpenLayers.Strategy.BBOX();
128
129         var filter = {"fake": "filter"};
130
131         s.createFilter = function() {
132             return filter;
133         };
134         s.response = {"fake": "response"};
135         
136         var log = {};
137
138         var protocol = new OpenLayers.Protocol({
139             read: function(options) {
140                 log.options = options;
141             },
142             abort: function(response) {
143                 log.abort = response.fake;
144             }
145         });
146         
147         var layer = new OpenLayers.Layer.Vector(null, {
148             strategies: [s],
149             protocol: protocol,
150             isBaseLayer: true
151         });
152         var map = new OpenLayers.Map("map");
153         map.addLayer(layer);
154         map.zoomToMaxExtent();
155         
156         t.ok(log.options.filter == filter,
157                 "protocol read called with correct filter");
158         t.ok(log.options.callback == s.merge,
159                 "protocol read called with correct callback");
160         t.ok(log.options.scope == s,
161                 "protocol read called with correct scope");
162         t.eq(log.abort, "response",
163                 "protocol abort called with correct response");
164
165         map.destroy();
166
167     }
168     
169     function test_resFactor(t) {
170         t.plan(2);
171         
172         var map = new OpenLayers.Map("map");
173         var bbox = new OpenLayers.Strategy.BBOX();
174         var fakeProtocol = new OpenLayers.Protocol({
175             'read': function() { 
176                 t.ok(true, "read called once without resfactor"); 
177             }
178         });
179         var layer = new OpenLayers.Layer.Vector("test", {
180             strategies: [bbox],
181             protocol: fakeProtocol,
182             isBaseLayer: true
183         });
184         map.addLayer(layer);
185         map.setCenter(new OpenLayers.LonLat(0, 0), 0);
186         map.zoomIn();
187         
188         fakeProtocol.read = function() { 
189             t.ok("read called again on zooming with resFactor: 1");
190         }
191         bbox.resFactor = 1;
192         map.zoomIn();
193         
194     }
195
196     function test_createFilter(t) {
197         t.plan(3);
198
199         var s = new OpenLayers.Strategy.BBOX();
200
201         var f;
202
203         // 2 test
204         s.setLayer({});
205         f = s.createFilter();
206         t.ok(f.CLASS_NAME.search(/^OpenLayers.Filter.Spatial/) != -1,
207              "createFilter returns a spatial filter object");
208         t.eq(f.type, OpenLayers.Filter.Spatial.BBOX,
209              "createFilter returns a BBOX-typed filter");
210
211         // 1 test
212         s.setLayer({filter: {fake: "filter"}});
213         f = s.createFilter();
214         t.ok(f.CLASS_NAME.search(/^OpenLayers.Filter.Logical/) != -1,
215              "createFilter returns a logical filter object");
216     }
217
218     function test_merge(t) {
219         t.plan(4);
220         
221         var strategy = new OpenLayers.Strategy.BBOX();
222                 
223         // create map with default projection
224         var map = new OpenLayers.Map("map");
225         
226         // create layer with custom projection
227         var layer = new OpenLayers.Layer.Vector(null, {
228             isBaseLayer: true,
229             strategies: [strategy],
230             protocol: new OpenLayers.Protocol(),
231             projection: new OpenLayers.Projection("EPSG:900913")
232         });
233         map.addLayer(layer);
234         map.zoomToMaxExtent();
235         
236         // create some features
237         var geometries = [
238             new OpenLayers.Geometry.Point(100, 200),
239             new OpenLayers.Geometry.Point(1000, 2000)
240         ];
241         var features = [
242             new OpenLayers.Feature.Vector(geometries[0].clone()),
243             new OpenLayers.Feature.Vector(geometries[1].clone())
244         ];
245
246         // call merge with a mocked up response
247         strategy.merge({features: features});
248         
249         // test that feature geometries have been transformed to map projection
250         var from = layer.projection;
251         var to = map.getProjectionObject();
252         t.geom_eq(layer.features[0].geometry, features[0].geometry.transform(from, to), "[different proj] feature 0 geometry transformed");
253         t.geom_eq(layer.features[1].geometry, features[1].geometry.transform(from, to), "[different proj] feature 1 geometry transformed");
254         
255         // same as above but with same map/layer projection
256         layer.destroyFeatures();
257         layer.projection = map.getProjectionObject();
258         
259         features = [
260             new OpenLayers.Feature.Vector(geometries[0].clone()),
261             new OpenLayers.Feature.Vector(geometries[1].clone())
262         ];
263         
264         // call merge again with mocked up response
265         strategy.merge({features: features});
266
267         // test that feature geometries have not been transformed
268         t.geom_eq(layer.features[0].geometry, features[0].geometry, "[same proj] feature 0 geometry not transformed");
269         t.geom_eq(layer.features[1].geometry, features[1].geometry, "[same proj] feature 1 geometry not transformed");
270         
271     }
272
273   </script>
274 </head>
275 <body>
276     <div id="map" style="width: 400px; height: 200px" />
277 </body>
278 </html>