3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Handler_Point_constructor(t) {
7 var control = new OpenLayers.Control();
8 control.id = Math.random();
9 var callbacks = {foo: "bar"};
10 var options = {bar: "foo"};
12 var oldInit = OpenLayers.Handler.prototype.initialize;
14 OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
15 t.eq(con.id, control.id,
16 "constructor calls parent with the correct control");
18 "constructor calls parent with the correct callbacks");
20 "constructor calls parent with the correct options");
22 var handler = new OpenLayers.Handler.Point(control, callbacks, options);
24 OpenLayers.Handler.prototype.initialize = oldInit;
27 function test_Handler_Point_activation(t) {
29 var map = new OpenLayers.Map('map');
30 var control = new OpenLayers.Control();
31 map.addControl(control);
32 var handler = new OpenLayers.Handler.Point(control);
33 handler.active = true;
34 var activated = handler.activate();
36 "activate returns false if the handler was already active");
37 handler.active = false;
38 activated = handler.activate();
40 "activate returns true if the handler was not already active");
41 activated = handler.deactivate();
43 "deactivate returns true if the handler was active already");
46 function test_Handler_Point_events(t) {
49 var map = new OpenLayers.Map('map');
53 var handler = new OpenLayers.Handler.Point(control);
55 // list below events that should be handled (events) and those
56 // that should not be handled (nonevents) by the handler
57 var events = ["click", "dblclick", "mousedown", "mouseup", "mousemove"];
58 var nonevents = ["mouseout", "resize", "focus", "blur"];
59 map.events.registerPriority = function(type, obj, func) {
61 if(typeof r == "string") {
62 // this is one of the mock handler methods
63 t.eq(OpenLayers.Util.indexOf(nonevents, type), -1,
64 "registered method is not one of the events " +
65 "that should not be handled");
66 t.ok(OpenLayers.Util.indexOf(events, type) > -1,
67 "activate calls registerPriority with browser event: " + type);
68 t.eq(typeof func, "function",
69 "activate calls registerPriority with a function");
71 "activate calls registerPriority with the correct method");
72 t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Point",
73 "activate calls registerPriority with the handler");
77 // set browser event like properties on the handler
78 for(var i=0; i<events.length; ++i) {
81 function setMethod(key) {
82 handler[key] = function() {return key};
85 var activated = handler.activate();
88 // test that click and dblclick are stopped
89 var handler = new OpenLayers.Handler.Point(control);
90 var oldStop = OpenLayers.Event.stop;
91 OpenLayers.Event.stop = function(evt) {
92 t.ok(evt.type == "click" || evt.type == "dblclick",
93 evt.type + " stopped");
95 t.eq(handler.click({type: "click"}), false, "click returns false");
96 t.eq(handler.dblclick({type: "dblclick"}), false, "dblclick returns false");
97 OpenLayers.Event.stop = oldStop;
101 function test_callbacks(t) {
103 var map = new OpenLayers.Map("map", {
106 var layer = new OpenLayers.Layer.Vector("foo", {
107 maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
111 var control = new OpenLayers.Control({
114 var handler = new OpenLayers.Handler.Point(control, {
132 control.handler = handler;
133 map.addControl(control);
134 map.setCenter(new OpenLayers.LonLat(0, 0), 0);
136 // mock up feature drawing
138 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
139 t.eq(log.type, "create", "[mousedown] create called");
140 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct point");
141 t.geom_eq(log.args[1].geometry, new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct sketch feature");
142 handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 0)});
143 t.eq(log.type, "modify", "[mousemove] modify called");
144 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 75), "[mousemove] correct point");
145 t.geom_eq(log.args[1].geometry, new OpenLayers.Geometry.Point(-149, 75), "[mousemove] correct sketch feature");
146 handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(1, 0)});
147 t.eq(log.type, "done", "[mouseup] done called");
148 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 75), "[mouseup] correct point");
150 // mock up feature drawing with a cancel
151 handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
152 handler.deactivate();
153 t.eq(log.type, "cancel", "[deactivate while drawing] cancel called");
154 t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[deactivate while drawing] correct point");
160 function test_Handler_Point_deactivation(t) {
162 var map = new OpenLayers.Map('map');
163 var control = new OpenLayers.Control();
164 map.addControl(control);
166 var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'});
168 handler.layer.destroy();
169 handler.deactivate();
170 t.eq(handler.layer, null,
171 "deactivate doesn't throw an error if layer was" +
172 " previously destroyed");
175 function test_Handler_Point_bounds(t) {
177 var map = new OpenLayers.Map('map');
178 map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
179 map.zoomToMaxExtent();
180 var control = new OpenLayers.Control();
181 map.addControl(control);
182 var handler = new OpenLayers.Handler.Point(control, {});
183 var activated = handler.activate();
184 var px = new OpenLayers.Pixel(150, 75);
185 var evt = {xy: px, which: 1};
186 handler.mousedown(evt);
187 var lonlat = map.getLonLatFromPixel(px);
188 t.eq(handler.point.geometry.x, lonlat.lon, "X is correct");
189 t.eq(handler.point.geometry.y, lonlat.lat, "Y is correct");
190 t.ok(handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(lonlat.lon,lonlat.lat,lonlat.lon,lonlat.lat)), "Correct bounds");
191 var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1};
192 handler.mousemove(evt);
193 t.ok(!handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(0,0,0,0)), "Bounds changed after moving mouse");
196 function test_Handler_Point_destroy(t) {
198 var map = new OpenLayers.Map('map');
199 map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
200 map.zoomToMaxExtent();
201 var control = new OpenLayers.Control();
202 map.addControl(control);
203 var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'});
206 var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
207 handler.mousedown(evt);
210 "handler has a layer prior to destroy");
212 "handler has a point prior to destroy");
214 t.eq(handler.layer, null,
215 "handler.layer is null after destroy");
216 t.eq(handler.point, null,
217 "handler.point is null after destroy");
225 <div id="map" style="width: 300px; height: 150px;"/>