3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_initialize(t) {
11 var s = new OpenLayers.Strategy.BBOX({ratio: ratio});
12 t.eq(s.ratio, ratio, "ctor sets ratio");
15 function test_activate(t) {
18 var l = new OpenLayers.Layer.Vector();
19 var s = new OpenLayers.Strategy.BBOX();
22 t.eq(s.active, false, "not active after construction");
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");
35 function test_update(t) {
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});
43 var strategy = new OpenLayers.Strategy.BBOX({
44 ratio: 1 // makes for easier comparison to map bounds
46 var layer = new OpenLayers.Layer.Vector(null, {
48 protocol: new OpenLayers.Protocol(),
49 strategies: [strategy]
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();
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.
63 strategy.update({force: true});
65 // test that the strategy bounds were set
66 t.ok(map.getExtent().equals(strategy.bounds), "[set center] bounds set to map extent");
68 // zoom and test that bounds are not reset
69 var old = strategy.bounds.clone();
71 t.ok(strategy.bounds.equals(old), "[zoom in] bounds not reset");
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");
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");
88 function test_events(t) {
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, {});
106 loadstart: function() {
109 loadend: function() {
115 map.zoomToMaxExtent();
117 t.eq(log.loadstart, 1, "loadstart triggered");
118 t.eq(log.loadend, 1, "loadend triggered");
124 function test_triggerRead(t) {
127 var s = new OpenLayers.Strategy.BBOX();
129 var filter = {"fake": "filter"};
131 s.createFilter = function() {
134 s.response = {"fake": "response"};
138 var protocol = new OpenLayers.Protocol({
139 read: function(options) {
140 log.options = options;
142 abort: function(response) {
143 log.abort = response.fake;
147 var layer = new OpenLayers.Layer.Vector(null, {
152 var map = new OpenLayers.Map("map");
154 map.zoomToMaxExtent();
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");
169 function test_resFactor(t) {
172 var map = new OpenLayers.Map("map");
173 var bbox = new OpenLayers.Strategy.BBOX();
174 var fakeProtocol = new OpenLayers.Protocol({
176 t.ok(true, "read called once without resfactor");
179 var layer = new OpenLayers.Layer.Vector("test", {
181 protocol: fakeProtocol,
185 map.setCenter(new OpenLayers.LonLat(0, 0), 0);
188 fakeProtocol.read = function() {
189 t.ok("read called again on zooming with resFactor: 1");
196 function test_createFilter(t) {
199 var s = new OpenLayers.Strategy.BBOX();
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");
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");
218 function test_merge(t) {
221 var strategy = new OpenLayers.Strategy.BBOX();
223 // create map with default projection
224 var map = new OpenLayers.Map("map");
226 // create layer with custom projection
227 var layer = new OpenLayers.Layer.Vector(null, {
229 strategies: [strategy],
230 protocol: new OpenLayers.Protocol(),
231 projection: new OpenLayers.Projection("EPSG:900913")
234 map.zoomToMaxExtent();
236 // create some features
238 new OpenLayers.Geometry.Point(100, 200),
239 new OpenLayers.Geometry.Point(1000, 2000)
242 new OpenLayers.Feature.Vector(geometries[0].clone()),
243 new OpenLayers.Feature.Vector(geometries[1].clone())
246 // call merge with a mocked up response
247 strategy.merge({features: features});
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");
255 // same as above but with same map/layer projection
256 layer.destroyFeatures();
257 layer.projection = map.getProjectionObject();
260 new OpenLayers.Feature.Vector(geometries[0].clone()),
261 new OpenLayers.Feature.Vector(geometries[1].clone())
264 // call merge again with mocked up response
265 strategy.merge({features: features});
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");
276 <div id="map" style="width: 400px; height: 200px" />