diff options
Diffstat (limited to 'misc/openlayers/tests/Handler')
-rw-r--r-- | misc/openlayers/tests/Handler/Box.html | 106 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Click.html | 735 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Drag.html | 603 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Feature.html | 698 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Hover.html | 136 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Keyboard.html | 150 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/MouseWheel.html | 182 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Path.html | 1464 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Pinch.html | 285 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Point.html | 600 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/Polygon.html | 1161 | ||||
-rw-r--r-- | misc/openlayers/tests/Handler/RegularPolygon.html | 235 |
12 files changed, 6355 insertions, 0 deletions
diff --git a/misc/openlayers/tests/Handler/Box.html b/misc/openlayers/tests/Handler/Box.html new file mode 100644 index 0000000..edb20d0 --- /dev/null +++ b/misc/openlayers/tests/Handler/Box.html @@ -0,0 +1,106 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_Box_constructor(t) { + t.plan(5); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {start: "foo", done: "bar"}; + var options = {bar: "foo"}; + + var handler = new OpenLayers.Handler.Box(control, callbacks, options); + + t.eq(handler.control.id, control.id, "handler created with the correct control"); + t.eq(handler.callbacks.start, "foo", "handler created with the correct start callback"); + t.eq(handler.callbacks.done, "bar", "handler created with the correct done callback"); + t.eq(handler.bar, "foo", "handler created with the correct options"); + t.ok(handler.dragHandler instanceof OpenLayers.Handler.Drag, "drag handler created"); + } + + function test_Handler_Box_draw(t) { + + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Box(control, { + start: function(e) { + t.ok(true, "start callback called"); + }, + done: function(e) { + t.ok(e.equals(new OpenLayers.Bounds(5, 11, 11, 5)), "box result correct"); + } + }); + handler.activate(); + + // determine whether we can test the box position, the hidden frame + // our tests run in causes us problem here in FF and IE: + // IE8: left is NaN + // FF3: left is NaN + // FF4; left is NaN + // Chromium 10: left is 0 + var testdiv = OpenLayers.Util.createDiv('testdiv', new OpenLayers.Pixel(5, 5)); + map.div.appendChild(testdiv); + var left = parseInt(OpenLayers.Element.getStyle(testdiv, 'border-left-width')); + map.div.removeChild(testdiv); + var testAll = !isNaN(left); + + t.plan(testAll ? 11 : 3); + + // we change NaN values to 0 values in the handler's + // boxOffsets object, this is to prevent "invalid + // "argument" errors in IE + if(!testAll) { + var offset = handler.getBoxOffsets(); + offset.left = 0; + offset.right = 0; + offset.top = 0; + offset.bottom = 0; + offset.width = 0; + offset.height = 0; + } + + + handler.dragHandler.start = {x: 5, y: 5}; + handler.startBox(); + offset = handler.getBoxOffsets(); + handler.moveBox({x: 10, y: 10}); + if (testAll) { + t.eq(parseInt(handler.zoomBox.style.left), 5 - offset.left, "x position of box correct"); + t.eq(parseInt(handler.zoomBox.style.top), 5 - offset.top, "y position of box correct"); + t.eq(parseInt(handler.zoomBox.style.width), 5 + offset.width + 1, "x dimension of box correct"); + t.eq(parseInt(handler.zoomBox.style.height), 5 + offset.height + 1, "y dimension of box correct"); + } + handler.moveBox({x: 0, y: 0}); + if (testAll) { + t.eq(parseInt(handler.zoomBox.style.left), 0 - offset.left, "new x position of box correct"); + t.eq(parseInt(handler.zoomBox.style.top), 0 - offset.top, "new y position of box correct"); + t.eq(parseInt(handler.zoomBox.style.width), 5 + offset.width + 1, "x dimension of box still correct"); + t.eq(parseInt(handler.zoomBox.style.height), 5 + offset.height + 1, "y dimension of box still correct"); + } + handler.endBox({x: 11, y: 11}); + t.eq(handler.zoomBox, null, "box removed after endBox"); + } + + function test_Handler_Box_destroy(t) { + t.plan(1); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Box(control); + handler.activate(); + try { + handler.destroy(); + t.ok(true, "destroying the box handler should not raise any error"); + } catch(err) { + t.fail("destroying the box handler causes trouble: " + err); + } + map.destroy(); + } + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"></div> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Click.html b/misc/openlayers/tests/Handler/Click.html new file mode 100644 index 0000000..be508ca --- /dev/null +++ b/misc/openlayers/tests/Handler/Click.html @@ -0,0 +1,735 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + + function px(x, y) { + return new OpenLayers.Pixel(x, y); + } + + function test_Handler_Click_constructor(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.Click(control, callbacks, options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_Handler_Click_activate(t) { + t.plan(2); + var control = { + map: new OpenLayers.Map('map') + }; + var handler = new OpenLayers.Handler.Click(control); + handler.active = true; + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + handler.dragging = true; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + + } + + function test_Handler_Click_events(t) { + t.plan(80); + + var map = new OpenLayers.Map('map'); + var control = { + map: map + }; + map.events.registerPriority = function(type, obj, func) { + var f = OpenLayers.Function.bind(func, obj) + var r = f({xy:null}); + if(typeof r == "string") { + // this is one of the mock handler methods + t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, + "registered method is not one of the events " + + "that should not be handled"); + t.ok(OpenLayers.Util.indexOf(events, type) > -1, + "activate calls registerPriority with browser event: " + type); + t.eq(typeof func, "function", + "activate calls registerPriority with a function"); + t.eq(func(), type, + "activate calls registerPriority with the correct method"); + t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Click", + "activate calls registerPriority with the handler"); + } + } + function setMethod(key) { + handler[key] = function() {return key}; + } + + // list below events that should be handled (events) and those + // that should not be handled (nonevents) by the handler + var events = ["click", "dblclick", "mousedown", "mouseup", "rightclick", "touchstart", "touchmove", "touchend"]; + var nonevents = ["mousemove", "resize", "focus", "blur"]; + var handler = new OpenLayers.Handler.Click(control); + // set browser event like properties on the handler + for(var i=0; i<events.length; ++i) { + setMethod(events[i]); + } + handler.activate(); + + // different listeners registered for pixelTolerance option + var events = ["click", "dblclick", "mousedown", "mouseup", "rightclick", "touchstart", "touchmove", "touchend"]; + var nonevents = ["mousemove", "resize", "focus", "blur"]; + var handler = new OpenLayers.Handler.Click(control, {}, { + pixelTolerance: 2 + }); + for(var i=0; i<events.length; ++i) { + setMethod(events[i]); + } + handler.activate(); + + } + + var callbackMap; + function callbackSetup(log, options) { + callbackMap = new OpenLayers.Map({ + div: "map", + controls: [], // no controls here because these tests use a custom setTimeout and we only want setTimeout calls from a single handler + layers: [new OpenLayers.Layer(null, {isBaseLayer: true})], + center: new OpenLayers.LonLat(0, 0), + zoom: 1 + }); + var control = new OpenLayers.Control(); + callbackMap.addControl(control); + + var callbacks = { + "click": function(evt) { + log.push({callback: "click", evt: evt}); + }, + "dblclick": function(evt) { + log.push({callback: "dblclick", evt: evt}); + } + }; + var handler = new OpenLayers.Handler.Click(control, callbacks, options); + handler.activate(); + + + var timers = {}; + window._setTimeout = window.setTimeout; + window.setTimeout = function(func, delay) { + log.push({method: "setTimeout", func: func, delay: delay}); + var key = (new Date).getTime() + "-" + Math.random(); + timers[key] = true; + // execute function that is supposed to be delayed + func(); + return key; + } + window._clearTimeout = window.clearTimeout; + window.clearTimeout = function(key) { + log.push({ + method: "clearTimeout", + keyExists: (key in timers) + }); + delete timers[key]; + } + return handler; + } + + function callbackTeardown() { + window.setTimeout = window._setTimeout; + window.clearTimeout = window._clearTimeout; + callbackMap.destroy(); + callbackMap = null; + } + + function test_callbacks_click_default(t) { + t.plan(6); + + var log = []; + var handler = callbackSetup(log); + + // set up for single click - three tests here + var testEvt = {id: Math.random()}; + handler.map.events.triggerEvent("click", testEvt); + t.eq(log.length, 2, "(click w/ single true) two items logged"); + + // first item logged is setTimeout call + t.eq(log[0].method, "setTimeout", "setTimeout called"); + t.eq(typeof log[0].func, "function", "setTimeout called with a function"); + t.eq(log[0].delay, handler.delay, "setTimeout called with proper delay"); + + // second item logged is from click callback + t.eq(log[1].callback, "click", "click callback called"); + t.eq(log[1].evt.id, testEvt.id, "got correct event"); + + callbackTeardown(); + } + + function test_callbacks_dblclick_default(t) { + t.plan(1); + + var log = []; + var handler = callbackSetup(log); + var testEvt = {id: Math.random()}; + handler.map.events.triggerEvent("dblclick", testEvt); + + t.eq(log.length, 0, "nothing happens by default with dblclick (double is false)"); + + callbackTeardown(); + + } + + function test_callbacks_dblclick_double(t) { + t.plan(3); + + var log = []; + var handler = callbackSetup(log, {"double": true}); + var testEvt = {id: Math.random()}; + handler.map.events.triggerEvent("dblclick", testEvt); + + t.eq(log.length, 1, "one item logged"); + t.eq(log[0].callback, "dblclick", "dblclick callback called") + t.eq(log[0].evt.id, testEvt.id, "dblclick callback called with event"); + + callbackTeardown(); + + } + + function test_callbacks_dblclick_sequence(t) { + t.plan(8); + + var log = []; + var handler = callbackSetup(log, {"double": true}); + var testEvt = {id: Math.random()}; + + // first click - set timer for next + handler.map.events.triggerEvent("click", testEvt); + t.ok(handler.timerId != null, "timer is set"); + log.pop(); // because the test setTimeout is synchronous we get the click callback immediately + t.eq(log.length, 1, "one item logged (after pop due to synchronous setTimeout call in our tests"); + t.eq(log[0].method, "setTimeout", "setTimeout called first"); + + // second click - timer cleared + handler.map.events.triggerEvent("click", testEvt); + t.ok(handler.timerId == null, "timer is cleared"); + t.eq(log.length, 2, "two items logged after second click"); + t.eq(log[1].method, "clearTimeout", "clearTimeout called second"); + + // dblclick event - callback called + handler.map.events.triggerEvent("dblclick", testEvt); + t.eq(log.length, 3, "three items logged"); + t.eq(log[2].callback, "dblclick", "dblclick callback called third"); + + callbackTeardown(); + + } + + function test_callbacks_within_pixelTolerance(t) { + t.plan(1); + + var log = []; + var handler = callbackSetup(log, {"double": true, pixelTolerance: 2}); + + var down = { + xy: px(0, 0) + }; + var up = { + xy: px(0, 1) + }; + + handler.map.events.triggerEvent("mousedown", down); + handler.map.events.triggerEvent("mouseup", up); + handler.map.events.triggerEvent("click", up); + + t.eq(log[log.length-1].callback, "click", "click callback called"); + + callbackTeardown(); + + } + + function test_callbacks_outside_pixelTolerance(t) { + t.plan(2); + + var log = []; + var handler = callbackSetup(log, {pixelTolerance: 2}); + + var down = { + xy: px(0, 0) + }; + var up = { + xy: px(2, 3) + }; + + handler.map.events.triggerEvent("mousedown", down); + t.ok(handler.down && handler.down.xy.equals(down.xy), "down position set"); + + handler.map.events.triggerEvent("mouseup", up); + handler.map.events.triggerEvent("click", up); + t.eq(log.length, 0, "nothing logged - event outside tolerance"); + + callbackTeardown(); + + } + + function test_callbacks_within_dblclickTolerance(t) { + t.plan(6); + + var log = []; + var handler = callbackSetup(log, {single: false, "double": true, dblclickTolerance: 8}); + + var first = { + xy: px(0, 0) + }; + var second = { + xy: px(0, 5) + }; + + handler.map.events.triggerEvent("mousedown", first); + handler.map.events.triggerEvent("mouseup", first); + handler.map.events.triggerEvent("click", first); + t.eq(log.length, 1, "one item logged"); + t.eq(log[0] && log[0].method, "setTimeout", "setTimeout called"); + + handler.map.events.triggerEvent("mousedown", second); + handler.map.events.triggerEvent("mouseup", second); + handler.map.events.triggerEvent("click", second); + t.eq(log.length, 2, "two events logged"); + t.eq(log[1] && log[1].method, "clearTimeout", "clearTimeout called"); + + handler.map.events.triggerEvent("dblclick", second); + t.eq(log.length, 3, "three items logged"); + t.eq(log[2] && log[2].callback, "dblclick", "dblclick callback called"); + + callbackTeardown(); + } + + function test_callbacks_outside_dblclickTolerance(t) { + t.plan(5); + + var log = []; + // default dblclickTolerance is 13 + var handler = callbackSetup(log, {single: false, "double": true}); + + var first = { + xy: px(0, 0) + }; + var second = { + xy: px(13.5, 0) + }; + + handler.map.events.triggerEvent("mousedown", first); + handler.map.events.triggerEvent("mouseup", first); + handler.map.events.triggerEvent("click", first); + t.eq(log.length, 1, "one item logged"); + t.eq(log[0] && log[0].method, "setTimeout", "setTimeout called"); + + handler.map.events.triggerEvent("mousedown", second); + handler.map.events.triggerEvent("mouseup", second); + handler.map.events.triggerEvent("click", second); + t.eq(log.length, 2, "two items logged"); + t.eq(log[1] && log[1].method, "clearTimeout", "clearTimeout called"); + + handler.map.events.triggerEvent("dblclick", second); + t.eq(log.length, 2, "still two items logged - dblclick callback is not called"); + + callbackTeardown(); + } + + function test_callbacks_multitouch_single(t) { + + t.plan(2); + + var log = []; + + var callbacks = { + click: function(evt) { + log.push({callback: "click", type: evt.type}); + }, + dblclick: function(evt) { + log.push({callback: "dblclick", type: evt.type}); + } + }; + + var map = new OpenLayers.Map("map"); + var layer = new OpenLayers.Layer(null, {isBaseLayer: true}); + map.addLayer(layer); + map.setCenter(new OpenLayers.LonLat(0, 0), 1); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Click( + control, callbacks, + {"double": true, single: true, pixelTolerance: 2} + ); + + // we override here so we don't have to wait for the timeout + handler.queuePotentialClick = function(evt) { + log.push({potential: true, evt: evt}); + OpenLayers.Handler.Click.prototype.queuePotentialClick.call(this, evt); + } + + handler.activate(); + + function handle(o) { + var touches = []; + if (("x0" in o) && ("y0" in o)) { + touches.push({ + clientX: o.x0, clientY: o.y0 + }); + } + if (("x1" in o) && ("y1" in o)) { + touches.push({ + clientX: o.x1, clientY: o.y1 + }); + } + handler.map.events.handleBrowserEvent({ + type: o.type, touches: touches + }); + } + + // a typical multitouch sequence goes like this: + // touchstart, touchstart, touchend, touchend + handle({type: "touchstart", x0: 10, y0: 10}); + handle({type: "touchstart", x0: 10, y0: 10, x1: 30, y1: 15}); + handle({type: "touchend"}); + handle({type: "touchend"}); + + t.eq(log.length, 1, "one item logged"); + t.eq(log[0] && log[0].potential, true, "click in queue - no dblclick called"); + + map.destroy(); + } + + function test_Handler_Click_deactivate(t) { + t.plan(6); + var control = { + map: new OpenLayers.Map('map') + }; + var handler = new OpenLayers.Handler.Click(control); + handler.active = false; + var deactivated = handler.deactivate(); + t.ok(!deactivated, + "deactivate returns false if the handler was not already active"); + handler.active = true; + handler.down = true; + handler.timerId = true; + handler.touch = true; + handler.last = true; + deactivated = handler.deactivate(); + t.ok(deactivated, + "deactivate returns true if the handler was active already"); + t.eq(handler.down, null, + "deactivate sets down to null"); + t.eq(handler.timerId, null, + "deactivate sets timerId to null"); + t.eq(handler.touch, false, + "deactivate sets touch to false"); + t.eq(handler.last, null, + "deactivate sets last to null"); + + } + + function test_Handler_Click_mouseup(t) { + t.plan(11); + + var map = new OpenLayers.Map("map"); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Click(control); + + var testEvent = {id: Math.random()}; + var propagate = true; + var log, got, modMatch, rightClick; + + // override methods to log what is called + var temp = OpenLayers.Event.isRightClick; + OpenLayers.Event.isRightClick = function(e) { + log.push({method: "isRightClick", evt: e}); + return rightClick; + }; + handler.checkModifiers = function(e) { + log.push({method: "checkModifiers", evt: e}); + return modMatch; + }; + handler.rightclick = function(e) { + log.push({method: "rightclick", evt: e}); + return propagate; + }; + + + // simulate an event with non-matching modifiers + log = []; + modMatch = false; + rightClick = false; + got = handler.mouseup(testEvent); + t.eq(log.length, 1, "one item logged"); + t.eq(log[0] && log[0].method, "checkModifiers", "a) checkModifiers called first"); + t.eq(log[0] && log[0].evt, testEvent, "a) first method called with correct event"); + + + // modifiers, handlerightclicks, and isrightclick + log = []; + rightClick = true; + modMatch = true; + handler.control.handleRightClicks = true; + got = handler.mouseup(testEvent); + t.eq(log.length, 3, "three items logged"); + t.eq(log[0] && log[0].method, "checkModifiers", "b) checkModifiers called first"); + t.eq(log[0] && log[0].evt, testEvent, "b) first method called with correct event"); + t.eq(log[1] && log[1].method, "isRightClick", "b) isRightClick called second"); + t.eq(log[1] && log[1].evt, testEvent, "b) second method called with correct event"); + t.eq(log[2] && log[2].method, "rightclick", "b) rightclick called third"); + t.eq(log[2] && log[2].evt, testEvent, "b) third method called with correct event"); + t.eq(got, propagate, "b) return from handler's rightclick returned from mouseup"); + + OpenLayers.Event.isRightClick = temp; + map.destroy(); + } + + function test_touch_click(t) { + t.plan(5); + + // set up + + var log; + + var map = new OpenLayers.Map('map'); + var control = {map: map}; + + var callbacks = { + 'click': function(e) { + log = {x: e.xy.x, y: e.xy.y, + lastTouches: e.lastTouches}; + } + }; + + var handler = new OpenLayers.Handler.Click( + control, callbacks, + {'single': true, pixelTolerance: null}); + + // test + + // the common case: a touchstart followed by a touchend + log = null; + handler.touchstart({xy: px(1, 1), touches: ["foo"]}); + handler.touchend({touches: ["foo"]}); + + t.delay_call(1, function() { + t.ok(log != null, "click callback called"); + if(log != null) { + t.eq(log.x, 1, "evt.xy.x as expected"); + t.eq(log.y, 1, "evt.xy.y as expected"); + t.ok(log.lastTouches, "evt.lastTouches as expected"); + } + + // now emulate a touch where touchstart doesn't propagate + // to the click handler, i.e. the click handler gets a + // touchend only + log = null; + handler.touchend({touches: ["foo"]}); + + t.delay_call(1, function() { + t.ok(log == null, "click callback not called"); + + // tear down + map.destroy(); + }); + }); + } + + function test_touch_within_dblclickTolerance(t) { + t.plan(4); + + var log; + + var callbacks = { + click: function(evt) { + log.push({callback: "click", type: evt.type}); + }, + dblclick: function(evt) { + log.push({callback: "dblclick", type: evt.type}); + } + }; + + var map = new OpenLayers.Map("map"); + var layer = new OpenLayers.Layer(null, {isBaseLayer: true}); + map.addLayer(layer); + map.setCenter(new OpenLayers.LonLat(0, 0), 1); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Click( + control, callbacks, + {"double": true, single: true, pixelTolerance: 2} + ); + handler.activate(); + + function handle(type, x, y) { + map.events.handleBrowserEvent({ + type: type, + touches: [ + {clientX: x, clientY: y} + ] + }); + } + + // test + log = []; + // sequence of two clicks on a touch device + // click 1 + handle("touchstart", 10, 10); + handle("touchend", 11, 10); + handle("mousemove", 11, 10); + handle("mousedown", 10, 10); + handle("mouseup", 11, 10); + handle("click", 11, 10); + // click 2 + handle("touchstart", 12, 10); + handle("touchend", 12, 10); + handle("mousedown", 12, 10); + handle("mouseup", 12, 10); + handle("click", 12, 10); + + t.eq(log.length, 1, "one callback called"); + t.eq(log[0] && log[0].callback, "dblclick", "click callback called"); + t.eq(log[0] && log[0].type, "touchend", "click callback called with touchend event"); + t.ok(!handler.timerId, "handler doesn't have a timerId waiting for click") + + // tear down + map.destroy(); + } + + function test_touch_outside_dblclickTolerance(t) { + t.plan(2); + + var log; + + var callbacks = { + click: function(evt) { + log.push({callback: "click", type: evt.type}); + }, + dblclick: function(evt) { + log.push({callback: "dblclick", type: evt.type}); + } + }; + + var map = new OpenLayers.Map("map"); + var layer = new OpenLayers.Layer(null, {isBaseLayer: true}); + map.addLayer(layer); + map.setCenter(new OpenLayers.LonLat(0, 0), 1); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Click( + control, callbacks, + {"double": true, single: true, pixelTolerance: 2, dblclickTolerance: 8} + ); + handler.activate(); + + function handle(type, x, y) { + var touches = []; + if (x !== undefined && y !== undefined) { + touches.push({ + clientX: x, clientY: y + }); + } + map.events.handleBrowserEvent({ + type: type, touches: touches + }); + } + + // test + log = []; + // sequence of two clicks on a touch device + // click 1 + handle("touchstart", 10, 10); + handle("touchend"); + handle("mousemove", 11, 10); + handle("mousedown", 10, 10); + handle("mouseup", 11, 10); + handle("click", 11, 10); + // click 2 + handle("touchstart", 20, 10); + handle("touchend"); + handle("mousedown", 20, 10); + handle("mouseup", 20, 10); + handle("click", 20, 10); + + t.eq(log.length, 0, "no callbacks called"); + t.ok(!handler.timerId, "handler doesn't have a timerId waiting for click") + + // tear down + map.destroy(); + } + + function test_touchstart(t) { + // a test to verify that the touchstart function does + // unregister the mouse listeners when it's called the + // first time + + t.plan(7); + + // set up + + var map = new OpenLayers.Map("map", { + controls: [] + }); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Click(control, {}); + control.handler = handler; + map.addControl(control); + handler.activate(); + + function allRegistered() { + var eventTypes = ['mousedown', 'mouseup', 'click', 'dblclick'], + eventType, + listeners, + listener, + flag; + for(var i=0, ilen=eventTypes.length; i<ilen; i++) { + flag = false; + eventType = eventTypes[i]; + listeners = map.events.listeners[eventType]; + for(var j=0, jlen=listeners.length; j<jlen; j++) { + listener = listeners[j]; + if(listener.func === handler[eventType] && listener.obj === handler) { + flag = true; + break; + } + } + if(!flag) { + return false; + } + } + return true; + } + + // test + + t.ok(allRegistered(), 'mouse listeners are registered'); + handler.touchstart({xy: new OpenLayers.Pixel(0, 0)}); + t.eq(map.events.listeners.mousedown.length, 0,"mousedown is not registered"); + t.eq(map.events.listeners.mouseup.length, 0,"mouseup is not registered"); + t.eq(map.events.listeners.click.length, 0,"click is not registered"); + t.eq(map.events.listeners.dblclick.length, 0,"dblclick is not registered"); + + t.ok(handler.touch, 'handler.touch is set'); + + handler.deactivate(); + t.ok(!handler.touch, 'handler.touch is not set'); + + map.destroy(); + } + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Drag.html b/misc/openlayers/tests/Handler/Drag.html new file mode 100644 index 0000000..fa9a3b2 --- /dev/null +++ b/misc/openlayers/tests/Handler/Drag.html @@ -0,0 +1,603 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_Drag_constructor(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.Drag(control, callbacks, options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_Handler_Drag_activate(t) { + t.plan(3); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Drag(control); + handler.active = true; + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + handler.dragging = true; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + t.ok(!handler.dragging, + "activate sets dragging to false"); + + } + + function test_Handler_Drag_events(t) { + t.plan(40); + + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Drag(control); + + // list below events that should be handled (events) and those + // that should not be handled (nonevents) by the handler + var events = ["mousedown", "mouseup", "mousemove", "mouseout", "click", + "touchstart", "touchmove", "touchend"]; + var nonevents = ["dblclick", "resize", "focus", "blur"]; + map.events.registerPriority = function(type, obj, func) { + var r = func(); + if(typeof r == "string") { + // this is one of the mock handler methods + t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, + "registered method is not one of the events " + + "that should not be handled"); + t.ok(OpenLayers.Util.indexOf(events, type) > -1, + "activate calls registerPriority with browser event: " + type); + t.eq(typeof func, "function", + "activate calls registerPriority with a function"); + t.eq(func(), type, + "activate calls registerPriority with the correct method"); + t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Drag", + "activate calls registerPriority with the handler"); + } + } + + // set browser event like properties on the handler + for(var i=0; i<events.length; ++i) { + setMethod(events[i]); + } + function setMethod(key) { + handler[key] = function() {return key}; + } + + var activated = handler.activate(); + + } + + function test_Handler_Drag_callbacks(t) { + t.plan(33); + + var map = new OpenLayers.Map('map', {controls: []}); + + var control = new OpenLayers.Control(); + map.addControl(control); + + // set callback methods (out doesn't get an xy) + var events = ["down", "move", "up", "done"]; + var testEvents = {}; + var xys = {}; + var callbacks = {}; + for(var i=0; i<events.length; ++i) { + var px = new OpenLayers.Pixel(Math.random(), Math.random()); + testEvents[events[i]] = {xy: px}; + setCallback(events[i]); + } + function setCallback(key) { + callbacks[key] = function(evtxy) { + t.ok(evtxy.x == testEvents[key].xy.x && + evtxy.y == testEvents[key].xy.y, + key + " callback called with the proper evt.xy"); + } + } + + var handler = new OpenLayers.Handler.Drag(control, callbacks); + handler.activate(); + + var oldIsLeftClick = OpenLayers.Event.isLeftClick; + var oldPreventDefault = OpenLayers.Event.preventDefault; + var oldCheckModifiers = handler.checkModifiers; + + // test mousedown with right click + OpenLayers.Event.isLeftClick = function() { + return false; + } + handler.checkModifiers = function() { + return true; + } + handler.started = true; + handler.start = {x: "foo", y: "bar"}; + handler.last = {x: "foo", y: "bar"}; + map.events.triggerEvent("mousedown", testEvents.down); + t.ok(!handler.started, "right-click sets started to false"); + t.eq(handler.start, null, "right-click sets start to null"); + t.eq(handler.last, null, "right-click sets last to null"); + + // test mousedown with improper modifier + OpenLayers.Event.isLeftClick = function() { + return true; + } + handler.checkModifiers = function() { + return false; + } + handler.started = true; + handler.start = {x: "foo", y: "bar"}; + handler.last = {x: "foo", y: "bar"}; + map.events.triggerEvent("mousedown", testEvents.down); + t.ok(!handler.started, "bad modifier sets started to false"); + t.eq(handler.start, null, "bad modifier sets start to null"); + t.eq(handler.last, null, "bad modifier sets last to null"); + + // test mousedown + handler.checkModifiers = function(evt) { + t.ok(evt.xy.x == testEvents.down.xy.x && + evt.xy.y == testEvents.down.xy.y, + "mousedown calls checkModifiers with the proper event"); + return true; + } + OpenLayers.Event.isLeftClick = function(evt) { + t.ok(evt.xy.x == testEvents.down.xy.x && + evt.xy.y == testEvents.down.xy.y, + "mousedown calls isLeftClick with the proper event"); + return true; + } + OpenLayers.Event.preventDefault = function(evt) { + t.ok(evt.xy.x == testEvents.down.xy.x && evt.xy.y == testEvents.down.xy.y, + "mousedown default action is disabled"); + } + map.events.triggerEvent("mousedown", testEvents.down); + t.ok(handler.started, "mousedown sets the started flag to true"); + t.ok(!handler.dragging, "mouse down sets the dragging flag to false"); + t.ok(handler.start.x == testEvents.down.xy.x && + handler.start.y == testEvents.down.xy.y, + "mouse down sets handler.start correctly"); + t.ok(handler.last.x == testEvents.down.xy.x && + handler.last.y == testEvents.down.xy.y, + "mouse down sets handler.last correctly"); + + OpenLayers.Event.preventDefault = oldPreventDefault; + OpenLayers.Event.isLeftClick = oldIsLeftClick; + handler.checkModifiers = oldCheckModifiers; + + // test mouseup before mousemove + var realUp = testEvents.up; + testEvents.up = testEvents.down; + // this will fail with notice about the done callback being called + // if done is called when it shouldn't be + map.events.triggerEvent("mouseup", testEvents.up); + testEvents.up = realUp; + + // test mousemove + handler.started = false; + map.events.triggerEvent("mousemove", {xy: {x: null, y: null}}); + // if the handler triggers the move callback, it will be with the + // incorrect evt.xy + t.ok(true, + "mousemove before the handler has started doesn't call move"); + + handler.started = true; + map.events.triggerEvent("mousemove", testEvents.move); + t.ok(handler.dragging, "mousemove sets the dragging flag to true"); + t.ok(handler.start.x == testEvents.down.xy.x && + handler.start.y == testEvents.down.xy.y, + "mouse move leaves handler.start alone"); + t.ok(handler.last.x == testEvents.move.xy.x && + handler.last.y == testEvents.move.xy.y, + "mouse move sets handler.last correctly"); + + // a second move with the same evt.xy should not trigger move callback + // if it does, the test page will complain about a bad plan number + var oldMove = handler.callbacks.move; + handler.callbacks.move = function() { + t.ok(false, + "a second move with the same evt.xy should not trigger a move callback"); + } + map.events.triggerEvent("mousemove", testEvents.move); + handler.callbacks.move = oldMove; + + // test mouseup + handler.started = false; + map.events.triggerEvent("mouseup", {xy: {x: null, y: null}}); + // if the handler triggers the up callback, it will be with the + // incorrect evt.xy + t.ok(true, + "mouseup before the handler has started doesn't call up"); + + handler.started = true; + // mouseup triggers the up and done callbacks + testEvents.done = testEvents.up; + map.events.triggerEvent("mouseup", testEvents.up); + t.ok(!this.started, "mouseup sets the started flag to false"); + t.ok(!this.dragging, "mouseup sets the dragging flag to false"); + + // test mouseout + handler.started = false; + map.events.triggerEvent("mouseout", {xy: {x: null, y: null}}); + // if the handler triggers the out or done callback, it will be with the + // incorrect evt.xy + t.ok(true, + "mouseout before the handler has started doesn't call out or done"); + + handler.started = true; + var oldMouseLeft = OpenLayers.Util.mouseLeft; + OpenLayers.Util.mouseLeft = function(evt, element) { + t.ok(evt.xy.x == testEvents.done.xy.x && + evt.xy.y == testEvents.done.xy.y, + "mouseout calls Util.mouseLeft with the correct event"); + t.eq(element.id, map.viewPortDiv.id, + "mouseout calls Util.mouseLeft with the correct element"); + return true; + } + // mouseup triggers the out and done callbacks + // out callback gets no arguments + handler.callbacks.out = function() { + t.eq(arguments.length, 0, + "mouseout calls out callback with no arguments"); + } + map.events.triggerEvent("mouseout", testEvents.done); + t.ok(!handler.started, "mouseout sets started flag to false"); + t.ok(!handler.dragging, "mouseout sets dragging flag to false"); + OpenLayers.Util.mouseLeft = oldMouseLeft; + + // test click with the click.html example - the click method on the + // drag handler returns (handler.start == handler.last), stopping + // propagation of the click event if the mouse moved during a drag. + + // regression tests will assure that the drag handler doesn't mess + // with anything else on a click + function getProperties(obj) { + var props = {}; + for(key in obj) { + if(typeof obj[key] != "function" && typeof obj[key] != "object") { + props[key] = obj[key]; + } + } + return props; + } + var before = getProperties(handler); + map.events.triggerEvent("click", null); + var after = getProperties(handler); + t.eq(before, after, "click doesn't mess with handler"); + + } + + function test_Handler_Drag_touch(t) { + // In this test we verify that "touchstart", "touchmove", and + // "touchend" events set expected states in the drag handler. + // We also verify that we prevent the default as appropriate. + + t.plan(19); + + // set up + + var m = new OpenLayers.Map('map', {controls: []}); + var c = new OpenLayers.Control(); + m.addControl(c); + var h = new OpenLayers.Handler.Drag(c, { + done: function(px) { + log.push(px); + } + }); + h.activate(); + + var _preventDefault = OpenLayers.Event.preventDefault; + OpenLayers.Event.preventDefault = function(e) { + log.push(e); + }; + + var Px = OpenLayers.Pixel, e; + var log = []; + + // test + e = {touches: [{}], xy: new Px(0, 0)}; + m.events.triggerEvent('touchstart', e); + t.eq(h.started, true, '[touchstart] started is set'); + t.eq(h.start.x, 0, '[touchstart] start.x is correct'); + t.eq(h.start.y, 0, '[touchstart] start.y is correct'); + t.eq(log.length, 1, '[touchstart] one item in log'); + t.ok(log[0] === e, "touchstart", '[touchstart] event is stopped'); + t.eq(m.events.listeners.mousedown.length, 0,"mousedown is not registered"); + t.eq(m.events.listeners.mouseup.length, 0,"mouseup is not registered"); + t.eq(m.events.listeners.mousemove.length, 0,"mousemove is not registered"); + t.eq(m.events.listeners.click.length, 0,"click is not registered"); + t.eq(m.events.listeners.mouseout.length, 0,"mouseout is not registered"); + + e = {xy: new Px(1, 1)}; + m.events.triggerEvent('touchmove', e); + t.eq(h.dragging, true, '[touchmove] dragging is set'); + t.eq(h.last.x, 1, '[touchmove] last.x is correct'); + t.eq(h.last.y, 1, '[touchmove] last.y is correct'); + t.eq(log.length, 1, '[touchmove] one item in log (event is not stopped)'); + + e = {xy: new Px(2, 2)}; + m.events.triggerEvent('touchend', e); + t.eq(h.started, false, '[touchend] started is reset'); + t.eq(h.started, false, '[touchend] started is reset'); + // the "done" callback gets the position of the last touchmove + t.eq(log.length, 2, '[touchend] two items in log'); + t.ok(log[1] instanceof Px, '[touchend] got'); + t.ok(log[1].equals(e.xy), '[touchend] done callback got correct position'); + + // tear down + + OpenLayers.Event.preventDefault = _preventDefault; + m.destroy(); + } + + function test_Handler_Drag_submethods(t) { + t.plan(8); + + var map = new OpenLayers.Map('map', {controls: []}); + + var control = new OpenLayers.Control(); + map.addControl(control); + + + var handler = new OpenLayers.Handler.Drag(control, {}); + // set test events + var events = ["down", "move", "up", "out"]; + var onselect = { + "move": OpenLayers.Function.False, + "up": OpenLayers.Function.False, + "out": OpenLayers.Function.True + } + var testEvents = {}; + var type, px; + for(var i=0; i<events.length; ++i) { + type = events[i]; + px = new OpenLayers.Pixel(Math.random(), Math.random()); + testEvents[type] = {xy: px}; + setMethod(type); + } + function setMethod(type) { + handler[type] = function(evt) { + t.ok(evt.xy.x == testEvents[type].xy.x && + evt.xy.y == testEvents[type].xy.y, + "handler." + type + " called with the right event"); + onselect[type] && t.ok(document.onselectstart === onselect[type], "document.onselectstart listener is correct after " + type); + } + } + handler.activate(); + + // pretend that we have gone through a down-move-up-out cycle before + handler.oldOnselectstart = OpenLayers.Function.True; + + // test mousedown + handler.checkModifiers = function(evt) { + return true; + } + var oldIsLeftClick = OpenLayers.Event.isLeftClick; + OpenLayers.Event.isLeftClick = function(evt) { + return true; + } + map.events.triggerEvent("mousedown", testEvents.down); + OpenLayers.Event.isLeftClick = oldIsLeftClick; + + // test mousemove + map.events.triggerEvent("mousemove", testEvents.move); + + // test mouseup + map.events.triggerEvent("mouseup", testEvents.up); + + // test mouseout + var oldMouseLeft = OpenLayers.Util.mouseLeft; + OpenLayers.Util.mouseLeft = function() { + return true; + }; + handler.started = true; + map.events.triggerEvent("mouseout", testEvents.out); + OpenLayers.Util.mouseLeft = oldMouseLeft; + + t.ok(document.onselectstart === OpenLayers.Function.True, "document.onselectstart listener correct after down-move-up-out cycle"); + + } + + function test_Handler_Drag_deactivate(t) { + t.plan(7); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Drag(control); + handler.active = false; + var deactivated = handler.deactivate(); + t.ok(!deactivated, + "deactivate returns false if the handler was not already active"); + handler.active = true; + handler.dragging = true; + handler.touch = true; + deactivated = handler.deactivate(); + t.ok(deactivated, + "deactivate returns true if the handler was active already"); + t.ok(!handler.started, + "deactivate sets started to false"); + t.ok(!handler.dragging, + "deactivate sets dragging to false"); + t.ok(handler.start == null, + "deactivate sets start to null"); + t.ok(handler.last == null, + "deactivate sets last to null"); + t.ok(!handler.touch, + "deactivate sets touch to false"); + } + + function test_interval_timer_after_mouseup(t) { + t.plan(5); + + // set up + + var map = new OpenLayers.Map('map'); + + var control = new OpenLayers.Control(); + map.addControl(control); + + var moveCnt; + + var handler = new OpenLayers.Handler.Drag(control, {}, { + interval: 1, + move: function() { + moveCnt++; + } + }); + handler.activate(); + + handler.checkModifiers = function() { return true; }; + + var ilc = OpenLayers.Event.isLeftClick; + OpenLayers.Event.isLeftClick = function() { return true; }; + + // test + + moveCnt = 0; + + var xy1 = new OpenLayers.Pixel(1, 2); + handler.mousedown({xy: xy1}); + t.ok(handler.last == xy1, "[mousedown] last is as expected"); + var xy2 = new OpenLayers.Pixel(2, 3); + handler.mousemove({xy: xy2}); + t.ok(handler.last == xy2, "[mousemove 1] last is as expected"); + t.ok(handler.timeoutId != null, "[mousemove 1] timeoutId is set"); + var xy3 = new OpenLayers.Pixel(3, 4); + handler.mousemove({xy: xy3}); + t.ok(handler.last == xy2, "[mousemove 2] last is as expected"); + var xy4 = new OpenLayers.Pixel(4, 5); + handler.mouseup({xy: xy4}); + + t.delay_call(3, function() { + // the timer should not cause a move + t.eq(moveCnt, 1, "move called once"); + // tear down + OpenLayers.Event.isLeftClick = ilc; + }); + } + + function test_interval_timer_after_mousedown(t) { + t.plan(5); + + // set up + + var map = new OpenLayers.Map('map'); + + var control = new OpenLayers.Control(); + map.addControl(control); + + var moveCnt; + + var handler = new OpenLayers.Handler.Drag(control, {}, { + interval: 1, + move: function() { + moveCnt++; + } + }); + handler.activate(); + + handler.checkModifiers = function() { return true; }; + + var ilc = OpenLayers.Event.isLeftClick; + OpenLayers.Event.isLeftClick = function() { return true; }; + + // test + + moveCnt = 0; + + var xy1 = new OpenLayers.Pixel(1, 2); + handler.mousedown({xy: xy1}); + t.ok(handler.last == xy1, "[mousedown] last is as expected"); + var xy2 = new OpenLayers.Pixel(2, 3); + handler.mousemove({xy: xy2}); + t.ok(handler.last == xy2, "[mousemove 1] last is as expected"); + t.ok(handler.timeoutId != null, "[mousemove 1] timeoutId is set"); + var xy3 = new OpenLayers.Pixel(3, 4); + handler.mousemove({xy: xy3}); + t.ok(handler.last == xy2, "[mousemove 2] last is as expected"); + var xy4 = new OpenLayers.Pixel(4, 5); + handler.mouseup({xy: xy4}); + var xy5 = new OpenLayers.Pixel(5, 6); + handler.mousedown({xy: xy4}); + + t.delay_call(3, function() { + // the timer should not cause a move + t.eq(moveCnt, 1, "move called once"); + // tear down + OpenLayers.Event.isLeftClick = ilc; + }); + } + + function test_interval_timer_before_mouseup(t) { + t.plan(5); + + // set up + + var map = new OpenLayers.Map('map'); + + var control = new OpenLayers.Control(); + map.addControl(control); + + var moveCnt; + + var handler = new OpenLayers.Handler.Drag(control, {}, { + interval: 1, + move: function() { + moveCnt++; + } + }); + handler.activate(); + + handler.checkModifiers = function() { return true; }; + + var ilc = OpenLayers.Event.isLeftClick; + OpenLayers.Event.isLeftClick = function() { return true; }; + + // test + + moveCnt = 0; + + var xy1 = new OpenLayers.Pixel(1, 2); + handler.mousedown({xy: xy1}); + t.ok(handler.last == xy1, "[mousedown] last is as expected"); + var xy2 = new OpenLayers.Pixel(2, 3); + handler.mousemove({xy: xy2}); + t.ok(handler.last == xy2, "[mousemove 1] last is as expected"); + t.ok(handler.timeoutId != null, "[mousemove 1] timeoutId is set"); + var xy3 = new OpenLayers.Pixel(3, 4); + handler.mousemove({xy: xy3}); + t.ok(handler.last == xy2, "[mousemove 2] last is as expected"); + + t.delay_call(3, function() { + // the timer should cause a move + t.eq(moveCnt, 2, "move called twice"); + var xy4 = new OpenLayers.Pixel(4, 5); + handler.mouseup({xy: xy4}); + // tear down + OpenLayers.Event.isLeftClick = ilc; + }); + } + + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Feature.html b/misc/openlayers/tests/Handler/Feature.html new file mode 100644 index 0000000..4a78e14 --- /dev/null +++ b/misc/openlayers/tests/Handler/Feature.html @@ -0,0 +1,698 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_initialize(t) { + t.plan(4); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var layer = "boo"; + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.Feature(control, layer, + callbacks, options); + + t.eq(handler.layer, "boo", + "layer property properly set"); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_activate(t) { + t.plan(3); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var layer = new OpenLayers.Layer(); + map.addLayer(layer); + var handler = new OpenLayers.Handler.Feature(control, layer); + handler.active = true; + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + + var zIndex = layer.div.style.zIndex; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + t.eq(parseInt(layer.div.style.zIndex), + map.Z_INDEX_BASE['Feature'], + "layer z-index properly adjusted"); + + } + function test_events(t) { + t.plan(35); + + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var layer = new OpenLayers.Layer(); + map.addLayer(layer); + var handler = new OpenLayers.Handler.Feature(control, layer); + + // list below events that should be handled (events) and those + // that should not be handled (nonevents) by the handler + var events = ["mousedown", "mouseup", "mousemove", "click", "dblclick", "touchstart", "touchmove"]; + var nonevents = ["mouseout", "resize", "focus", "blur", "touchend"]; + map.events.registerPriority = function(type, obj, func) { + var output = func(); + // Don't listen for setEvent handlers (#902) + if (typeof output == "string") { + t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, + "registered method is not one of the events " + + "that should not be handled"); + t.ok(OpenLayers.Util.indexOf(events, type) > -1, + "activate calls registerPriority with browser event: " + type); + t.eq(typeof func, "function", + "activate calls registerPriority with a function"); + t.eq(func(), type, + "activate calls registerPriority with the correct method:"+type); + t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Feature", + "activate calls registerPriority with the handler"); + } + } + + // set browser event like properties on the handler + for(var i=0; i<events.length; ++i) { + setMethod(events[i]); + } + function setMethod(key) { + handler[key] = function() {return key}; + } + + var activated = handler.activate(); + + } + + function test_geometrytype_limit(t) { + t.plan(1); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var layer = new OpenLayers.Layer(); + var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0)); + feature.layer = layer; + layer.getFeatureFromEvent = function(evt) { return feature }; + map.addLayer(layer); + var handler = new OpenLayers.Handler.Feature(control, layer, {}, {'geometryTypes':['OpenLayers.Geometry.Point']}); + handler.activate(); + handler.callback = function(type,featurelist) { + t.eq(featurelist[0].id, feature.id, "Correct feature called back on"); + } + handler.handle({type: "click"}); + handler.feature = null; + handler.lastFeature = null; + handler.callback = function(type,featurelist) { + t.fail("Shouldn't have called back on " + featurelist[0].geometry); + } + feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(0,0)); + feature.layer = layer; + handler.handle("click", {}); + } + + function test_callbacks(t) { + t.plan(14); + + var map = new OpenLayers.Map('map', {controls: []}); + var control = new OpenLayers.Control(); + map.addControl(control); + var layer = new OpenLayers.Layer(); + map.addLayer(layer); + + var callbacks = {}; + var newFeature, lastFeature; + var evtPx = {xy: new OpenLayers.Pixel(Math.random(), Math.random())}; + + // define a callback factory function + function getCallback(evt, feature) { + return function(f) { + t.ok(f == feature, evt + " callback called with proper feature"); + }; + } + + // override the layer's getFeatureFromEvent func so that it always + // returns newFeature + layer.getFeatureFromEvent = function(evt) { return newFeature; }; + + var handler = new OpenLayers.Handler.Feature(control, layer, callbacks); + handler.activate(); + + // test click in new feature + // only 'click' callback should be called + handler.feature = null; + lastFeature = null; + newFeature = new OpenLayers.Feature.Vector(); + newFeature.layer = layer; + callbacks['click'] = getCallback('click', newFeature); + callbacks['clickout'] = getCallback('clickout', lastFeature); + evtPx.type = "click"; + map.events.triggerEvent('click', evtPx); + + // test click in new feature and out of last feature + // both 'click' and 'clickout' callbacks should be called + lastFeature = newFeature; + newFeature = new OpenLayers.Feature.Vector(); + newFeature.layer = layer; + callbacks['click'] = getCallback('click', newFeature); + callbacks['clickout'] = getCallback('clickout', lastFeature); + evtPx.type = "click"; + map.events.triggerEvent('click', evtPx); + + // test click out of last feature + // only 'clickout' callback should be called + lastFeature = newFeature; + newFeature = null; + callbacks['click'] = getCallback('click', newFeature); + callbacks['clickout'] = getCallback('clickout', lastFeature); + evtPx.type = "click"; + map.events.triggerEvent('click', evtPx); + + layer.getFeatureFromEvent = function(evt) { t.fail("mousemove called getFeatureFromEvent without any mousemove callbacks"); }; + evtPx.type = "mousemove"; + map.events.triggerEvent('mousemove', evtPx); + layer.getFeatureFromEvent = function(evt) { return newFeature; }; + + // test over a new feature + // only 'over' callback should be called + handler.feature = null; + lastFeature = null; + newFeature = new OpenLayers.Feature.Vector(); + newFeature.layer = layer; + callbacks['over'] = getCallback('over', newFeature); + callbacks['out'] = getCallback('out', lastFeature); + evtPx.type = "mousemove"; + map.events.triggerEvent('mousemove', evtPx); + + // test over a new feature and out of last feature + // both 'over' and 'out' callbacks should be called + lastFeature = newFeature; + newFeature = new OpenLayers.Feature.Vector(); + newFeature.layer = layer; + callbacks['over'] = getCallback('over', newFeature); + callbacks['out'] = getCallback('out', lastFeature); + evtPx.type = "mousemove"; + map.events.triggerEvent('mousemove', evtPx); + + // test out of last feature + // only 'out' callback should be called + lastFeature = newFeature; + newFeature = null; + callbacks['over'] = getCallback('over', newFeature); + callbacks['out'] = getCallback('out', lastFeature); + evtPx.type = "mousemove"; + map.events.triggerEvent('mousemove', evtPx); + + // test dblclick on a feature + // 'dblclick' callback should be called + handler.feature = null; + lastFeature = null; + newFeature = new OpenLayers.Feature.Vector(); + newFeature.layer = layer; + callbacks['dblclick'] = getCallback('dblclick', newFeature); + evtPx.type = "dblclick"; + map.events.triggerEvent('dblclick', evtPx); + + // test touchstart on a feature + // 'click' callback should be called + handler.feature = null; + lastFeature = null; + newFeature = new OpenLayers.Feature.Vector(); + newFeature.layer = layer; + callbacks['click'] = getCallback('click (touch)', newFeature); + callbacks['clickout'] = getCallback('clickout (touch)', lastFeature); + evtPx.type = "touchstart"; + map.events.triggerEvent('touchstart', evtPx); + + // test touchstart on the same feature + // 'click' callback should be called + callbacks['click'] = getCallback('click (touch)', newFeature); + evtPx.type = "touchstart"; + map.events.triggerEvent('touchstart', evtPx); + + // test touchstart in new feature and out of last feature + // both 'click' and 'clickout' callbacks should be called + lastFeature = newFeature; + newFeature = new OpenLayers.Feature.Vector(); + newFeature.layer = layer; + callbacks['click'] = getCallback('click (touch)', newFeature); + callbacks['clickout'] = getCallback('clickout (touch)', lastFeature); + evtPx.type = "touchstart"; + map.events.triggerEvent('touchstart', evtPx); + + // test touchstart out of last feature + // only 'clickout' callback should be called + lastFeature = newFeature; + newFeature = null; + callbacks['click'] = getCallback('click (touch)', newFeature); + callbacks['clickout'] = getCallback('clickout (touch)', lastFeature); + evtPx.type = "touchstart"; + map.events.triggerEvent('touchstart', evtPx); + } + + function test_touchstart(t) { + // a test to verify that the touchstart function does + // unregister the mouse listeners when it's called the + // first time + + t.plan(4); + + // set up + + var map = new OpenLayers.Map('map', {controls: []}); + var control = new OpenLayers.Control(); + map.addControl(control); + var layer = new OpenLayers.Layer(); + map.addLayer(layer); + + var handler = new OpenLayers.Handler.Feature(control, layer, {}); + handler.mousedown = function() {}; // mock mousedown + handler.activate(); + + var eventTypes = ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick']; + + function allRegistered() { + var eventType, + listeners, + listener, + flag; + for(var i=0, ilen=eventTypes.length; i<ilen; i++) { + flag = false; + eventType = eventTypes[i]; + listeners = map.events.listeners[eventType]; + for(var j=0, jlen=listeners.length; j<jlen; j++) { + listener = listeners[j]; + if(listener.func === handler[eventType] && listener.obj === handler) { + flag = true; + break; + } + } + if(!flag) { + return false; + } + } + return true; + } + + function noneRegistered() { + var eventType, + times, + flag = false; + for(var i=0, ilen=eventTypes.length; i<ilen; i++) { + eventType = eventTypes[i]; + times = map.events.listeners[eventType].length; + if (times != 0) { + t.fail(eventType + " is registered " + times + " times"); + flag = true; + } + } + return !flag; + } + + // test + + t.ok(allRegistered(), 'mouse listeners are registered'); + handler.touchstart({xy: new OpenLayers.Pixel(0, 0)}); + t.ok(noneRegistered(), 'mouse listeners are unregistered'); + t.ok(handler.touch, 'handler.touch is set'); + + handler.deactivate(); + t.ok(!handler.touch, 'handler.touch is not set'); + + // tear down + + map.destroy(); + } + + function test_deactivate(t) { + t.plan(3); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var layer = new OpenLayers.Layer(); + map.addLayer(layer); + var layerIndex = parseInt(layer.div.style.zIndex); + + var handler = new OpenLayers.Handler.Feature(control, layer); + handler.active = false; + var deactivated = handler.deactivate(); + t.ok(!deactivated, + "deactivate returns false if the handler was not already active"); + + handler.active = true; + + deactivated = handler.deactivate(); + t.ok(deactivated, + "deactivate returns true if the handler was active already"); + t.eq(parseInt(layer.div.style.zIndex), + layerIndex, + "deactivate sets the layer z-index back"); + } + + function test_stopHandled(t) { + t.plan(3); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var layer = new OpenLayers.Layer(); + map.addLayer(layer); + var handler = new OpenLayers.Handler.Feature(control, layer); + handler.activate(); + handler.handle = function(evt) { return /* handled */ true; }; + var evtPx = {xy: new OpenLayers.Pixel(Math.random(), Math.random())}; + map.events.register("click", map, function(e) { + t.ok(!handler.stopClick, "clicks propagate with stopClick set to false" ); + }); + map.events.register("mousedown", map, function(e) { + t.ok(!handler.stopDown, "mousedown propagate with stopDown set to false" ); + }); + map.events.register("mouseup", map, function(e) { + t.ok(!handler.stopUp, "mouseup propagate with stopUp set to false" ); + }); + + // 0 test + map.events.triggerEvent('click', evtPx); + // 0 test + map.events.triggerEvent('mousedown', evtPx); + // 0 test + map.events.triggerEvent('mousedown', evtPx); + + // 1 test + handler.stopClick = false; + map.events.triggerEvent('click', evtPx); + // 1 test + handler.stopDown = false; + map.events.triggerEvent('mousedown', evtPx); + // 1 test + handler.stopUp = false; + map.events.triggerEvent('mouseup', evtPx); + + // 3 tests total + } + + function test_destroyed_feature(t) { + t.plan(18); + + // setup + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var layer = new OpenLayers.Layer(); + layer.removeFeatures = function() {}; + map.addLayer(layer); + var handler = new OpenLayers.Handler.Feature(control, layer); + var feature, count, lastType, lastHandled; + handler.callback = function(type, features) { + ++count; + lastType = type; + lastHandled = features; + } + + /** + * Test that a destroyed feature doesn't get sent to the "click" callback + */ + count = 0; + handler.activate(); + feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0)); + feature.layer = layer; + // mock click on a feature + layer.getFeatureFromEvent = function(evt) {return feature}; + handler.handle({type: "click"}); + // confirm that feature was handled + t.eq(count, 1, "[click] callback called once"); + t.eq(lastType, "click", "[click] correct callback type"); + t.eq(lastHandled[0].id, feature.id, "[click] correct feature sent to callback"); + + // now destroy the feature and confirm that the callback is not called again + feature.destroy(); + handler.handle({type: "click"}); + if(count === 1) { + t.ok(true, "[click] callback not called after destroy"); + } else { + t.fail("[click] callback called after destroy: " + lastType); + } + + /** + * Test that a destroyed feature doesn't get sent to the "clickout" callback + */ + count = 0; + handler.deactivate(); + handler.activate(); + feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0)); + feature.layer = layer; + + // mock a click on a feature + layer.getFeatureFromEvent = function(evt) {return feature}; + handler.handle({type: "click"}); + // confirm that callback got feature + t.eq(count, 1, "[clickout] callback called once on in"); + t.eq(lastType, "click", "[clickout] click callback called on in"); + t.eq(lastHandled.length, 1, "[clickout] callback called with one feature"); + t.eq(lastHandled[0].id, feature.id, "[clickout] callback called with correct feature"); + + // now mock a click off a destroyed feature + feature.destroy(); + layer.getFeatureFromEvent = function(evt) {return null}; + handler.handle({type: "click"}); + // confirm that callback does not get called + if(count === 1) { + t.ok(true, "[clickout] callback not called when clicking out of a destroyed feature"); + } else { + t.fail("[clickout] callback called when clicking out of a destroyed feature: " + lastType); + } + + /** + * Test that a destroyed feature doesn't get sent to the "over" callback + */ + count = 0; + handler.deactivate(); + handler.activate(); + feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0)); + feature.layer = layer; + // mock mousemove over a feature + layer.getFeatureFromEvent = function(evt) {return feature}; + handler.handle({type: "mousemove"}); + // confirm that feature was handled + t.eq(count, 1, "[over] callback called once"); + t.eq(lastType, "over", "[over] correct callback type"); + t.eq(lastHandled[0].id, feature.id, "[over] correct feature sent to callback"); + + // now destroy the feature and confirm that the callback is not called again + feature.destroy(); + handler.handle({type: "mousemove"}); + if(count === 1) { + t.ok(true, "[over] callback not called after destroy"); + } else { + t.fail("[over] callback called after destroy: " + lastType); + } + + /** + * Test that a destroyed feature doesn't get sent to the "out" callback + */ + count = 0; + handler.deactivate(); + handler.activate(); + feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0)); + feature.layer = layer; + + // mock a mousemove over a feature + layer.getFeatureFromEvent = function(evt) {return feature}; + handler.handle({type: "mousemove"}); + // confirm that callback got feature + t.eq(count, 1, "[out] callback called once on over"); + t.eq(lastType, "over", "[out] click callback called on over"); + t.eq(lastHandled.length, 1, "[out] callback called with one feature"); + t.eq(lastHandled[0].id, feature.id, "[out] callback called with correct feature"); + + // now mock a click off a destroyed feature + feature.destroy(); + layer.getFeatureFromEvent = function(evt) {return null}; + handler.handle({type: "mousemove"}); + // confirm that callback does not get called + if(count === 1) { + t.ok(true, "[out] callback not called when moving out of a destroyed feature"); + } else { + t.fail("[out] callback called when moving out of a destroyed feature: " + lastType); + } + + handler.destroy(); + } + + function test_click_tolerance(t) { + t.plan(3); + + var map, control, layer, feature, evtPx; + var clicks, callbacks, handler; + + map = new OpenLayers.Map('map', {controls: []}); + control = new OpenLayers.Control(); + map.addControl(control); + layer = new OpenLayers.Layer(); + map.addLayer(layer); + + feature = new OpenLayers.Feature.Vector(); + feature.layer = layer; + + evtPx = { + xy: new OpenLayers.Pixel(Math.random(), Math.random()), + type: "click" + }; + + // override the layer's getFeatureFromEvent func so that it always + // returns newFeature + layer.getFeatureFromEvent = function(evt) { return feature; }; + + callbacks = { + click: function() { + clicks++; + } + }; + + handler = new OpenLayers.Handler.Feature( + control, layer, callbacks, {clickTolerance: 4}); + handler.activate(); + + // distance between down and up is 1, which is + // lower than clickTolerance so "click" should trigger + handler.down = {x: 0, y: 0}; + handler.up = {x: 1, y: 0}; + clicks = 0; + map.events.triggerEvent("click", evtPx); + t.eq(clicks, 1, "click callback triggers when tolerance is not reached (lower than)"); + + // distance between down and up is 4, which is + // equal to clickTolerance so "click" should trigger + handler.down = {x: 0, y: 0}; // cached handler.down cleared (#857) + handler.up = {x: 0, y: 4}; + clicks = 0; + map.events.triggerEvent("click", evtPx); + t.eq(clicks, 1, "click callback triggers when tolerance is not reached (equal to)"); + + // distance between down and up is 5, which is + // greater than clickTolerance so "click" should not trigger + handler.down = {x: 0, y: 0}; // cached handler.down cleared (#857) + handler.up = {x: 5, y: 0}; + clicks = 0; + map.events.triggerEvent("click", evtPx); + t.eq(clicks, 0, "click callback does not trigger when tolerance is reached"); + } + + function test_multitouch_canvas(t) { + var supported = OpenLayers.Renderer.Canvas.prototype.supported(); + if (!supported) { t.plan(0); return; } + + t.plan(1); + + // set up + + var log; + + var map = new OpenLayers.Map('map'); + var layer = new OpenLayers.Layer.Vector('vectors', { + renderers: ['Canvas'], + isBaseLayer: true + }); + map.addLayer(layer); + + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Feature(control, layer, + {click: function() { log++; }}); + control.handler = handler; + map.addControl(control); + control.activate(); + + var feature = new OpenLayers.Feature.Vector( + new OpenLayers.Geometry.Point(0, 0)); + layer.addFeatures(feature); + + map.zoomToMaxExtent(); + + // test + + // mock getMousePosition on the events object to make + // sure scrolls, offsets and leftop do not interfere + map.events.getMousePosition = function(evt) { + return new OpenLayers.Pixel(evt.clientX, + evt.clientY); + }; + + log = 0; + var evt = { + type: 'touchstart', + touches: [{ + clientX: 100, + clientY: 75 + }, { + clientX: 200, + clientY: 75 + }] + }; + map.events.handleBrowserEvent(evt); + t.eq(log, 0, "no feature selection when multi-touching"); + + // tear down + + map.destroy(); + } + + function test_layerorder(t) { + t.plan(2); + var map = new OpenLayers.Map("map"); + var base = new OpenLayers.Layer(null, {isBaseLayer: true}); + var vector = new OpenLayers.Layer.Vector(); + map.addLayers([base, vector]); + map.addControl(new OpenLayers.Control.SelectFeature(vector, {autoActivate: true})); + map.zoomToMaxExtent(); + t.eq(parseInt(vector.getZIndex(), 10), 725, "vector layer's zIndex correct"); + map.events.triggerEvent("changelayer"); + t.eq(parseInt(vector.getZIndex(), 10), 725, "vector layer's zIndex still correct after changelayer event"); + + } + + function test_clear_event_position_cache(t) { + t.plan(2); + + var map, control, layer, feature, evtPx; + + map = new OpenLayers.Map('map', {controls: []}); + control = new OpenLayers.Control(); + map.addControl(control); + layer = new OpenLayers.Layer(); + layer.getFeatureFromEvent = function(evt) { return feature; }; + map.addLayer(layer); + feature = new OpenLayers.Feature.Vector(); + feature.layer = layer; + + evtPx = { + xy: new OpenLayers.Pixel(Math.random(), Math.random()), + type: "click" + }; + + handler = new OpenLayers.Handler.Feature( + control, layer, {}, {}); + handler.activate(); + + handler.down = {x: 0, y: 0}; + handler.up = {x: 1, y: 0}; + map.events.triggerEvent("click", evtPx); + t.eq(handler.down, null, "cached mousedown position is cleared after handling click"); + t.eq(handler.up, null, "cached mouseup position is cleared after handling click") + } + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Hover.html b/misc/openlayers/tests/Handler/Hover.html new file mode 100644 index 0000000..150218a --- /dev/null +++ b/misc/openlayers/tests/Handler/Hover.html @@ -0,0 +1,136 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_Hover_events(t) { + t.plan(10); + + var map = new OpenLayers.Map('map'); + var control = { + map: map + }; + map.events.registerPriority = function(type, obj, func) { + var r = func(); + if(typeof r == "string") { + // this is one of the mock handler methods + t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, + "registered method is not one of the events " + + "that should not be handled"); + t.ok(OpenLayers.Util.indexOf(events, type) > -1, + "activate calls registerPriority with browser event: " + type); + t.eq(typeof func, "function", + "activate calls registerPriority with a function"); + t.eq(func(), type, + "activate calls registerPriority with the correct method"); + t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Hover", + "activate calls registerPriority with the handler"); + } + } + function setMethod(key) { + handler[key] = function() {return key}; + } + + // list below events that should be handled (events) and those + // that should not be handled (nonevents) by the handler + var events = ["mousemove", "mouseout"]; + var nonevents = ["mousedown", "mouseup", "click", "dblclick", "resize", "focus", "blur"]; + var handler = new OpenLayers.Handler.Hover(control); + // set browser event like properties on the handler + for(var i=0; i<events.length; ++i) { + setMethod(events[i]); + } + handler.activate(); + } + + function test_Handler_Hover_callbacks(t) { + t.plan(8); + + var map = new OpenLayers.Map('map', {controls: []}); + + var control = { + map: map + }; + + var timers = {}; + var sto = window.setTimeout; + window.setTimeout = function(func, delay) { + var key = Math.random(); + timers[key] = true; + t.ok(typeof func == "function", + "setTimeout called with a function"); + t.eq(delay, handler.delay, + "setTimeout called with proper delay"); + // execute function that is supposed to be delayed + func(); + return key; + } + var cto = window.clearTimeout; + window.clearTimeout = function(key) { + if(timers[key] === true) { + delete timers[key]; + } else { + t.fail("clearTimeout called with non-existent timerId"); + } + } + + var handler = new OpenLayers.Handler.Hover(control, {}); + handler.activate(); + var testEvt; + + // test pause and move callbacks - four tests here (2 from setTimeout above) + testEvt = {id: Math.random()}; + handler.callbacks = { + "pause": function(evt) { + t.eq(evt.id, testEvt.id, + "pause callback called with correct evt"); + }, + "move": function(evt) { + t.eq(evt.id, testEvt.id, + "move callback called with correct evt"); + } + }; + map.events.triggerEvent("mousemove", testEvt); + handler.clearTimer(); + + // test pixelTolerance - four tests here (2 from setTimeout above) + handler.pixelTolerance = 2; + handler.px = new OpenLayers.Pixel(0, 0); + testEvt = { + xy: new OpenLayers.Pixel(0, 1) + }; + // mouse moves one pixel, callbacks shouldn't be called + handler.callbacks = { + "pause": function(evt) { + t.fail("(pixelTolerance met) pause callback shouldn't be called"); + }, + "move": function(evt) { + t.fail("(pixelTolerance met) move callback shoudln't be called"); + } + }; + map.events.triggerEvent("mousemove", testEvt); + handler.clearTimer(); + handler.px = new OpenLayers.Pixel(0, 0); + testEvt = { + xy: new OpenLayers.Pixel(3, 3) + }; + // mouse moves 3x3 pixels, callbacks should be called + handler.callbacks = { + "pause": function(evt) { + t.ok(evt.xy == testEvt.xy, "(pixelTolerance unmet) pause callback called"); + }, + "move": function(evt) { + t.ok(evt == testEvt, "(pixelTolerance unmet) move callback called"); + } + }; + map.events.triggerEvent("mousemove", testEvt); + handler.clearTimer(); + + window.setTimeout = sto; + window.clearTimeout = cto; + } + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Keyboard.html b/misc/openlayers/tests/Handler/Keyboard.html new file mode 100644 index 0000000..4a72c92 --- /dev/null +++ b/misc/openlayers/tests/Handler/Keyboard.html @@ -0,0 +1,150 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_Keyboard_initialize(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.Keyboard(control, callbacks, + options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_Handler_Keyboard_destroy(t) { + t.plan(3); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Keyboard(control); + var old = OpenLayers.Handler.prototype.destroy; + t.ok(handler.eventListener != null, + "eventListener is not null before destroy"); + OpenLayers.Handler.prototype.destroy = function() { + t.ok(true, "destroy calls destroy on correct parent"); + }; + handler.destroy(); + t.ok(handler.eventListener == null, + "eventListeners is null after destroy"); + OpenLayers.Handler.prototype.destroy = old; + } + + function test_Handler_Keyboard_activate(t) { + t.plan(15); + + var log; + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Keyboard(control); + + // mock OpenLayers.Event.observe + var old = OpenLayers.Event.stopObserving; + OpenLayers.Event.observe = function(obj, type, method) { + log[type] = obj; + var validType = OpenLayers.Util.indexOf(["keydown", "keyup"], type) != -1; + t.ok(validType, "activate calls observe for " + type); + t.ok(method == handler.eventListener, + "activate calls observing with correct method"); + }; + + handler.active = true; + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + + log = {}; + handler.active = false; + handler.observeElement = map.div; + activated = handler.activate(); + t.ok(log['keydown'] == map.div, + "activate calls observing for keydown with correct object"); + t.ok(log['keyup'] == map.div, + "activate calls observing for keyup with correct object"); + t.ok(activated, + "activate returns true if the handler was not already active"); + + log = {}; + handler.active = false; + handler.observeElement = null; + activated = handler.activate(); + t.ok(log['keydown'] == document, + "activate calls observing for keydown with correct object"); + t.ok(log['keyup'] == document, + "activate calls observing for keyup with correct object"); + t.ok(activated, + "activate returns true if the handler was not already active"); + + OpenLayers.Event.observe = old; + map.destroy(); + } + + function test_Handler_Keyboard_deactivate(t) { + t.plan(15); + + var log; + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Keyboard(control); + + // mock OpenLayers.Event.stopObserving + var old = OpenLayers.Event.stopObserving; + OpenLayers.Event.stopObserving = function(obj, type, method) { + log[type] = obj; + var validType = OpenLayers.Util.indexOf(["keydown", "keyup"], type) != -1; + t.ok(validType, "deactivate calls stopObserving for " + type); + t.ok(method == handler.eventListener, + "deactivate calls stopObserving with correct method"); + }; + + handler.active = false; + var deactivated = handler.deactivate(); + t.ok(!deactivated, + "deactivate returns false if the handler was not already active"); + + log = {}; + handler.active = true; + handler.observeElement = map.div; + deactivated = handler.deactivate(); + t.ok(log['keydown'] == map.div, + "deactivate calls stopObserving for keydown with correct object"); + t.ok(log['keyup'] == map.div, + "deactivate calls stopObserving for keyup with correct object"); + t.ok(deactivated, + "deactivate returns true if the handler was active already"); + + log = {}; + handler.active = true; + handler.observeElement = document; + deactivated = handler.deactivate(); + t.ok(log['keydown'] == document, + "deactivate calls stopObserving for keydown with correct object"); + t.ok(log['keyup'] == document, + "deactivate calls stopObserving for keyup with correct object"); + t.ok(deactivated, + "deactivate returns true if the handler was active already"); + + OpenLayers.Event.stopObserving = old; + map.destroy(); + } + + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/MouseWheel.html b/misc/openlayers/tests/Handler/MouseWheel.html new file mode 100644 index 0000000..687a31d --- /dev/null +++ b/misc/openlayers/tests/Handler/MouseWheel.html @@ -0,0 +1,182 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_MouseWheel_constructor(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.MouseWheel(control, callbacks, options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_Handler_MouseWheel_activate(t) { + t.plan(2); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.MouseWheel(control); + handler.active = true; + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + } + + function test_Handler_MouseWheel_mousePosition(t) { + t.plan(1); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("","",{})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var pass = false; + var handler = new OpenLayers.Handler.MouseWheel(control, {'up': + function (evt) { + if (evt.xy) { pass = true; } + } + }); + handler.setMap(map); + handler.activate(); + var delta = 120; + handler.onWheelEvent({'target':map.layers[0].div, wheelDelta: delta}); + t.ok(pass, "evt.xy was set even without a mouse move"); + } + + function test_Handler_MouseWheel_events(t) { + t.plan(6); + + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("","",{})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var deltaZ; + var handler = new OpenLayers.Handler.MouseWheel(control, { + 'up': function(evt, delta){ + deltaZ = delta; + } + }, {interval: 200}); + + // list below events that should be handled (events) and those + // that should not be handled (nonevents) by the handler + var events = ["mousemove"]; + var nonevents = ["dblclick", "resize", "focus", "blur"]; + map.events.registerPriority = function(type, obj, func) { + var r = func(); + if(typeof r == "string") { + t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, + "registered method is not one of the events " + + "that should not be handled"); + t.ok(OpenLayers.Util.indexOf(events, type) > -1, + "activate calls registerPriority with browser event: " + type); + t.eq(typeof func, "function", + "activate calls registerPriority with a function"); + t.eq(func(), type, + "activate calls registerPriority with the correct method"); + t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.MouseWheel", + "activate calls registerPriority with the handler"); + } + } + + // set browser event like properties on the handler + for(var i=0; i<events.length; ++i) { + setMethod(events[i]); + } + function setMethod(key) { + handler[key] = function() {return key}; + } + + var activated = handler.activate(); + + var delta = 120; + handler.onWheelEvent({'target':map.layers[0].div, wheelDelta: delta}); + handler.onWheelEvent({'target':map.layers[0].div, wheelDelta: delta}); + t.delay_call(1, function() { + t.eq(deltaZ, 2, "Multiple scroll actions triggered one event when interval is set"); + }); + } + + function test_Handler_MouseWheel_cumulative(t) { + t.plan(2); + + var deltaUp = 0, ticks = 0; + var callbacks = { + up: function(evt, delta) { + deltaUp += delta; + ticks++; + } + }; + + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("","",{})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control({}); + map.addControl(control); + var handler = new OpenLayers.Handler.MouseWheel(control, callbacks, { + interval: 150, + cumulative: false, + maxDelta: 6 + }); + + var delta = 120; + // generate 20 scroll up in non cumulative mode + for (var i=0; i < 20; i++) { + handler.onWheelEvent({'target':map.layers[0].div, wheelDelta: delta}); + } + + t.delay_call(2, function() { + t.eq(deltaUp / ticks, 1, "Cumulative mode works"); + t.eq(ticks, 4, "up called 4x with maxDelta of 6"); + }); + } + + function test_Handler_MouseWheel_deactivate(t) { + t.plan(2); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.MouseWheel(control); + handler.active = false; + var deactivated = handler.deactivate(); + t.ok(!deactivated, + "deactivate returns false if the handler was not already active"); + handler.active = true; + deactivated = handler.deactivate(); + t.ok(deactivated, + "deactivate returns true if the handler was active already"); + } + function test_handler_MouseWheel_destroy(t) { + t.plan(1); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.MouseWheel(control); + handler.deactivate = function() { + t.ok(true, "Deactivate called one time."); + } + handler.destroy(); + } + + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Path.html b/misc/openlayers/tests/Handler/Path.html new file mode 100644 index 0000000..8351eea --- /dev/null +++ b/misc/openlayers/tests/Handler/Path.html @@ -0,0 +1,1464 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_Path_constructor(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.Path(control, callbacks, options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_Handler_Path_activation(t) { + t.plan(5); + var log = []; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Path(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.active = true; + + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + t.ok(handler.layer instanceof OpenLayers.Layer.Vector, + "activate creates a vector layer"); + t.ok(handler.layer.map == map, + "activate adds the vector layer to the map"); + activated = handler.deactivate(); + t.ok(activated, + "deactivate returns true if the handler was active already"); + + map.destroy(); + } + + // See: http://trac.osgeo.org/openlayers/ticket/3179 + function test_activate_before_map_is_centered(t) { + t.plan(1); + var map = new OpenLayers.Map('map', { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Path(control, {}); + control.handler = handler; + map.addControl(control); + + var error; + try { + handler.activate(); + error = false; + } catch(err) { + error = true; + } + t.ok(!error, "no error on activate"); + } + + function test_bounds(t) { + t.plan(4); + var geometry; + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("", "", {})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Path(control, {}, + {stopDown: true, stopUp: true}); + var activated = handler.activate(); + // click on (150, 75) + var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; + handler.mousemove(evt); + handler.mousedown(evt); + handler.mouseup(evt); + t.eq(handler.layer.features.length, 2, + "There are two features in the layer after first click."); + // click on (175, 100) + evt = {xy: new OpenLayers.Pixel(175, 100), which: 1}; + handler.mousemove(evt); + handler.mousedown(evt); + handler.mouseup(evt); + t.eq(handler.layer.features.length, 2, + "There are two features in the layer after second click."); + t.ok(handler.line.geometry.getBounds().equals( + new OpenLayers.Bounds(0,-35.15625,35.15625,0)), + "Correct bounds"); + // mousedown on (175, 100) + evt = {xy: new OpenLayers.Pixel(175, 100), which: 1}; + handler.mousedown(evt); + // mousemove to (125, 100) + evt = {xy: new OpenLayers.Pixel(125, 100), which: 1}; + handler.mousemove(evt); + // test that the bounds have changed + t.ok(!handler.line.geometry.getBounds().equals( + new OpenLayers.Bounds(0,-35.15625,35.15625,0)), + "Correct bounds after dragging without letting go. " + + "(Came out as " + handler.line.geometry.getBounds().toBBOX() + + ".)"); + map.destroy(); + } + + function test_callbacks(t) { + t.plan(39); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var logs = [], log; + var handler = new OpenLayers.Handler.Path(control, { + create: function() { + logs.push({type: "create", args: arguments}); + }, + point: function() { + logs.push({type: "point", args: arguments}); + }, + modify: function() { + logs.push({type: "modify", args: arguments}); + }, + done: function() { + logs.push({type: "done", args: arguments}); + }, + cancel: function() { + logs.push({type: "cancel", args: arguments}); + } + }, + { + pixelTolerance: 0 + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + // mouse move + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + t.eq(logs.length, 2, "[mousemove] called back twice"); + log = logs.shift(); + t.eq(log.type, "create", "[mousemove] create called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mousemove] correct point"); + t.ok(log.args[1] === handler.line, + "[mousemove] correct feature"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousemove] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mousemove] correct point"); + t.ok(log.args[1] === handler.line, + "[mousemove] correct feature"); + // mouse down + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + t.eq(logs.length, 1, "[mousedown] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousedown] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mousedown] correct point"); + t.ok(log.args[1] === handler.line, + "[mousedown] correct feature"); + // mouse up + handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + t.eq(logs.length, 2, "[mouseup] called back twice"); + log = logs.shift(); + t.eq(log.type, "point", "[mouseup] point called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mouseup] correct point"); + t.geom_eq(log.args[1], + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-150, 75), + new OpenLayers.Geometry.Point(-150, 75) + ]), "[mouseup] correct line"); + log = logs.shift(); + t.eq(log.type, "modify", "[mouseup] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mouseup] correct point"); + t.ok(log.args[1] == handler.line, + "[mouseup] correct feature"); + // mouse move + handler.mousemove({type: "mousemove", + xy: new OpenLayers.Pixel(1, 1)}); + t.eq(logs.length, 1, "[mousemove] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousemove] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 74), + "[mousemove] correct point"); + t.ok(log.args[1] === handler.line, + "[mousemove] correct feature"); + // mouse move + handler.mousemove({type: "mousemove", + xy: new OpenLayers.Pixel(10, 10)}); + t.eq(logs.length, 1, "[mousemove] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousemove] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-140, 65), + "[mousemove] correct point"); + t.ok(log.args[1] === handler.line, + "[mousemove] correct feature"); + // mouse down + handler.mousedown({type: "mousedown", + xy: new OpenLayers.Pixel(10, 10)}); + t.eq(logs.length, 1, "[mousedown] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousedown] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-140, 65), + "[mousedown] correct point"); + t.ok(log.args[1] === handler.line, + "[mousedown] correct feature"); + // mouse up ("point", "modify") + handler.mouseup({type: "mouseup", + xy: new OpenLayers.Pixel(10, 10)}); + t.eq(logs.length, 2, "[mouseup] called back twice"); + log = logs.shift(); + log = logs.shift(); + // mouse down + handler.mousedown({type: "mousedown", + xy: new OpenLayers.Pixel(10, 10)}); + t.eq(logs.length, 0, "[mousedown] called back"); + // mouse up + handler.mouseup({type: "mouseup", + xy: new OpenLayers.Pixel(10, 10)}); + t.eq(logs.length, 0, "[mouseup] not called back"); + // double click + handler.dblclick({type: "dblclick", + xy: new OpenLayers.Pixel(10, 10)}); + t.eq(logs.length, 1, "[dblclick] called back"); + log = logs.shift(); + t.eq(log.type, "done", "[dblclick] done called"); + t.geom_eq(log.args[0], + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-150, 75), + new OpenLayers.Geometry.Point(-140, 65) + ]), + "[dblclick] correct linestring" + ); + // cancel + handler.cancel(); + t.eq(logs.length, 1, "[cancel] called back"); + log = logs.shift(); + t.eq(log.type, "cancel", "[cancel] canced called"); + t.eq(log.args[0], null, "[cancel] got null" + ); + + map.destroy(); + } + + function test_toggle_freehand(t) { + t.plan(2); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, { + done: function(g) { + log++; + } + }, {persist: true}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + log = 0; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.eq(log, 1, "feature drawn when shift pressed on mousedown"); + + log = 0; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: false}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.eq(log, 0, "feature not drawn when shift not pressed on mousedown"); + } + + function test_persist(t) { + t.plan(4); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + handler.persist = false; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature1 = handler.line; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(1, 1)}); + t.ok(feature1.layer == null, "a) feature1 destroyed"); + + handler.persist = true; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature2 = handler.line; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(1, 1)}); + t.ok(feature2.layer != null, "b) feature2 not destroyed"); + + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature3 = handler.line; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(1, 1)}); + t.ok(feature3.layer != null, "c) feature3 not destroyed"); + t.ok(feature2.layer == null, "c) feature2 destroyed"); + + map.destroy(); + } + + function test_persist_freehand(t) { + t.plan(6); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + handler.persist = false; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature1 = handler.line; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.ok(feature1.layer == null, "a) feature1 destroyed"); + + handler.persist = true; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + feature2 = handler.line; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.ok(feature2.layer != null, "b) feature2 not destroyed"); + + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + feature3 = handler.line; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.ok(feature3.layer != null, "c) feature3 not destroyed"); + t.ok(feature2.layer == null, "c) feature2 destroyed"); + + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + feature4 = handler.line; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: false}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.ok(feature4.layer != null, "d) feature4 not destroyed"); + t.ok(feature3.layer == null, "c) feature3 destroyed"); + + map.destroy(); + } + + function test_Handler_Path_destroy(t) { + t.plan(6); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("", "", {})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Path(control, {foo: 'bar'}); + + handler.activate(); + var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; + handler.mousedown(evt); + + t.ok(handler.layer, + "handler has a layer prior to destroy"); + t.ok(handler.point, + "handler has a point prior to destroy"); + t.ok(handler.line, + "handler has a line prior to destroy"); + handler.destroy(); + t.eq(handler.layer, null, + "handler.layer is null after destroy"); + t.eq(handler.point, null, + "handler.point is null after destroy"); + t.eq(handler.line, null, + "handler.line is null after destroy"); + map.destroy(); + } + + function test_maxVertices(t) { + t.plan(1); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var log = {}; + var doneCallback = function(evt) { + t.ok(evt, 'When maxVertices is reached, the geometry is finalized automatically'); + }; + var handler = new OpenLayers.Handler.Path(control, {'done': doneCallback}, {maxVertices: 2}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + // mock up feature drawing + handler.activate(); + var evt = {xy: new OpenLayers.Pixel(0, 0)}; + handler.mousemove(evt); + handler.mousedown(evt); + handler.mouseup(evt); + evt = {xy: new OpenLayers.Pixel(20, 20)}; + handler.mousemove(evt); + handler.mousedown(evt); + handler.mouseup(evt); + evt = {xy: new OpenLayers.Pixel(40, 40)}; + handler.mousemove(evt); + handler.mousedown(evt); + handler.mouseup(evt); + map.destroy(); + } + + function test_freehand_maxVertices(t) { + t.plan(1); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var log = {}; + var MAX_VERTICES = 2; + var doneCallback = function(geo) { + t.eq(geo.components.length, MAX_VERTICES, + 'When maxVertices is reached, the geometry is finalized automatically'); + }; + var handler = new OpenLayers.Handler.Path(control, + {'done': doneCallback}, + {freehand: true, + maxVertices: MAX_VERTICES}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + // mock up feature freehand drawing + handler.activate(); + var evt = {xy: new OpenLayers.Pixel(0, 0)}; + handler.mousemove(evt); + handler.mousedown(evt); + evt = {xy: new OpenLayers.Pixel(20, 20)}; + handler.mousemove(evt); + evt = {xy: new OpenLayers.Pixel(40, 40)}; + handler.mousemove(evt); + map.destroy(); + } + + /** + * Helper functions for editing method tests + */ + function editingMethodsSetup() { + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control.DrawFeature( + layer, OpenLayers.Handler.Path + ); + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + control.activate(); + return { + handler: control.handler, + map: map + } + } + function userClick(handler, x, y) { + var px = new OpenLayers.Pixel(x, y); + handler.mousemove({type: "mousemove", xy: px}); + handler.mousedown({type: "mousedown", xy: px}); + handler.mouseup({type: "mouseup", xy: px}); + } + function userTap(handler, x, y) { + var px = new OpenLayers.Pixel(x, y); + handler.touchstart({xy: px}); + handler.touchmove({xy: px}); + handler.touchend({}); + } + + /** + * Editing method tests: insertXY, insertDeltaXY, insertDirectionXY, + * insertDeflectionXY, undo, and redo + */ + function test_insertXY(t) { + t.plan(3); + var obj = editingMethodsSetup(); + var map = obj.map; + var handler = obj.handler; + + // add points at px(0, 0) and px(10, 10) + userClick(handler, 0, 0); + userClick(handler, 10, 10); + handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(50, 50)}); + + t.eq(handler.line.geometry.components.length, 3, "line has three points after two clicks"); + + // programmatically add a point + handler.insertXY(5, 6); + t.eq(handler.line.geometry.components.length, 4, "line has four points after insertXY"); + t.geom_eq( + handler.line.geometry.components[2], + new OpenLayers.Geometry.Point(5, 6), + "third point comes from insertXY" + ); + + map.destroy(); + + } + + function test_insertDeltaXY(t) { + t.plan(3); + var obj = editingMethodsSetup(); + var map = obj.map; + var handler = obj.handler; + + // add points at px(0, 0) and px(10, 10) + userClick(handler, 0, 0); + userClick(handler, 10, 10); + handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(50, 50)}); + + t.eq(handler.line.geometry.components.length, 3, "line has three points after two clicks"); + + // programmatically add a point + handler.insertDeltaXY(1, 2); + t.eq(handler.line.geometry.components.length, 4, "line has four points after insert"); + // expect a point that is offset from previous point + var exp = handler.line.geometry.components[1].clone(); + exp.move(1, 2); + t.geom_eq( + handler.line.geometry.components[2], exp, + "third point is offset by dx,dy from second point" + ); + + map.destroy(); + } + + function test_insertDirectionLength(t) { + t.plan(4); + var obj = editingMethodsSetup(); + var map = obj.map; + var handler = obj.handler; + + // add points at px(0, 0) and px(10, 10) + userClick(handler, 0, 0); + userClick(handler, 10, 10); + handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(50, 50)}); + + t.eq(handler.line.geometry.components.length, 3, "line has three points after two clicks"); + + // programmatically add a point + handler.insertDirectionLength(45, 2); + t.eq(handler.line.geometry.components.length, 4, "line has four points after insert"); + var p1 = handler.line.geometry.components[1]; + var p2 = handler.line.geometry.components[2]; + + var direction = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI; + t.eq(direction.toFixed(4), (45).toFixed(4), "inserted point offset with correct direction"); + var length = Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)); + t.eq(length.toFixed(4), (2).toFixed(4), "inserted point offset with correct length"); + + map.destroy(); + } + + function test_insertDeflectionLength(t) { + t.plan(4); + var obj = editingMethodsSetup(); + var map = obj.map; + var handler = obj.handler; + + // add points at px(0, 0) and px(10, 10) + userClick(handler, 0, 0); + userClick(handler, 10, 10); + handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(50, 50)}); + + t.eq(handler.line.geometry.components.length, 3, "line has three points after two clicks"); + var p0 = handler.line.geometry.components[0]; + var p1 = handler.line.geometry.components[1]; + // angle of first segment + var dir0 = Math.atan2(p1.y - p0.y, p1.x - p0.x) * 180 / Math.PI; + + // programmatically add a point + handler.insertDeflectionLength(-30, 5); + t.eq(handler.line.geometry.components.length, 4, "line has four points after insert"); + var p2 = handler.line.geometry.components[2]; + // angle of second segment + var dir1 = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI; + + var deflection = dir1 - dir0; + t.eq(deflection.toFixed(4), (-30).toFixed(4), "inserted point offset with correct deflection"); + + var length = Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)); + t.eq(length.toFixed(4), (5).toFixed(4), "inserted point offset with correct length"); + + map.destroy(); + } + + function test_undoredo1(t) { + t.plan(5); + var obj = editingMethodsSetup(); + var map = obj.map; + var handler = obj.handler; + + // add points and move mouse + userClick(handler, 0, 0); + userClick(handler, 10, 10); + userClick(handler, 50, 10); + handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(50, 50)}); + var original = handler.line.geometry.clone(); + var len = original.components.length; + t.eq(len, 4, "original has four points after three clicks"); + + // one undo + handler.undo(); + var currentLen = handler.line.geometry.components.length; + t.eq(currentLen, len-1, "one point removed on undo"); + t.geom_eq( + handler.line.geometry.components[currentLen-1], + original.components[len-1], + "current point (mouse position) remains the same after undo" + ); + // one redo + handler.redo(); + t.geom_eq(original, handler.line.geometry, "one redo undoes one undo"); + + // add point via touch + userTap(handler, 10, 50); + handler.undo(); + currentLen = handler.line.geometry.components.length; + t.geom_eq( + handler.line.geometry.components[currentLen-1], + handler.line.geometry.components[currentLen-2], + "current point (mouse position) is set to the last digitized " + + "point after undo on touch devices" + ); + + // cleanup + map.destroy(); + } + + function test_undoredo2(t) { + t.plan(8); + var obj = editingMethodsSetup(); + var map = obj.map; + var handler = obj.handler; + + // add points and move mouse + userClick(handler, 0, 0); + userClick(handler, 10, 10); + userClick(handler, 50, 10); + handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(50, 50)}); + var original = handler.line.geometry.clone(); + var len = original.components.length; + t.eq(len, 4, "original has four points after three clicks"); + + // two undos + handler.undo(); + handler.undo(); + var currentLen = handler.line.geometry.components.length; + t.eq(currentLen, len-2, "two points removed on two undos"); + t.geom_eq( + handler.line.geometry.components[currentLen-1], + original.components[len-1], + "current point (mouse position) remains the same after two undos" + ); + // first redo + handler.redo(); + currentLen = handler.line.geometry.components.length; + t.eq(currentLen, len-1, "point added in first redo"); + t.geom_eq( + handler.line.geometry.components[currentLen-2], + original.components[len-3], + "correct point restored in first redo" + ); + + // second redo + handler.redo(); + currentLen = handler.line.geometry.components.length; + t.eq(currentLen, len, "point added in second redo"); + t.geom_eq( + handler.line.geometry.components[currentLen-2], + original.components[len-2], + "correct point restored in second redo" + ); + t.geom_eq(handler.line.geometry, original, "correct geometry"); + + // cleanup + map.destroy(); + } + + function test_undoredo3(t) { + t.plan(3); + var obj = editingMethodsSetup(); + var map = obj.map; + var handler = obj.handler; + + // add points and move mouse + userClick(handler, 0, 0); + userClick(handler, 10, 10); + userClick(handler, 50, 10); + handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(50, 50)}); + var original = handler.line.geometry.clone(); + var len = original.components.length; + t.eq(len, 4, "original has four points after three clicks"); + + // gratuitous redos + var trouble = false; + try { + handler.undo(); + handler.undo(); + handler.redo(); + handler.redo(); + handler.redo(); + handler.redo(); + handler.redo(); + } catch (err) { + trouble = true; + } + t.ok(!trouble, "extra redos cause no ill effects"); + t.geom_eq(handler.line.geometry, original, "correct geometry"); + + // cleanup + map.destroy(); + } + + function test_undoredo4(t) { + t.plan(3); + var obj = editingMethodsSetup(); + var map = obj.map; + var handler = obj.handler; + + // add points and move mouse + userClick(handler, 0, 0); + userClick(handler, 10, 10); + userClick(handler, 50, 10); + handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(50, 50)}); + var original = handler.line.geometry.clone(); + var len = original.components.length; + t.eq(len, 4, "original has four points after three clicks"); + + // gratuitous undos + var trouble = false; + try { + handler.undo(); + handler.undo(); + handler.undo(); + handler.undo(); + handler.undo(); + handler.undo(); + handler.undo(); + } catch (err) { + trouble = true; + } + t.ok(!trouble, "extra undos cause no ill effects"); + t.eq(handler.line.geometry.components.length, 2, "still left with two points after many undos") + + // cleanup + map.destroy(); + } + + // + // Sequence tests + // + // Sequence tests basically involve executing a sequence of events + // and testing the resulting geometry. + // + // Below are tests for various drawing sequences. Tests can be + // added here each a non-working sequence is found. + // + + // stopDown:true, stopUp:true, pixelTolerance:1 + // a) click on (0, 0) + // b) mousedown on (1, 1) + // c) mouseup on (2, 2) + // d) dblclick on (10, 10) + function test_sequence1(t) { + t.plan(1); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, + {done: function(g) { log.geometry = g; }}, + {stopDown: true, stopUp: true, pixelTolerance: 1} + ); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + log = {}; + + // a) click on (0, 0) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + // b) mousedown on (1, 1) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1)}); + // c) mouseup on (2, 2) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(2, 2)}); + // d) dblclick on (10, 10) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(10, 10)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(10, 10)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(10, 10)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(10, 10)}); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-150, 75), // (0, 0) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]), "geometry is correct"); + } + + // stopDown:false, stopUp:false, pixelTolerance:1 + // a) click on (0, 0) + // b) mousedown on (1, 1) + // c) mouseup on (2, 2) + // d) dblclick on (10, 10) + function test_sequence2(t) { + t.plan(1); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, + {done: function(g) { log.geometry = g; }}, + {stopDown: false, stopUp: false, pixelTolerance: 1} + ); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + log = {}; + + // a) click on (0, 0) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + // b) mousedown on (1, 1) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1)}); + // c) mouseup on (2, 2) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(2, 2)}); + // d) dblclick on (10, 10) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(10, 10)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(10, 10)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(10, 10)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(10, 10)}); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-150, 75), // (0, 0) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]), "geometry is correct"); + } + + // a) click + // b) dblclick + // c) mousedown holding shift key + // d) mousemove holding shift key + function test_sequence3(t) { + t.plan(1); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, {}, + { + pixelTolerance: 0 + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + // a) click on (0, 0) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + // b) click on (1, 1) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1)}); + // c) click on (1, 1) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1)}); + // d) mousemove to (10, 10) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(10, 10), shiftKey: true}); + t.geom_eq(handler.line.geometry, + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-150, 75), // (0, 0) + new OpenLayers.Geometry.Point(-149, 74), // (1, 1) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]), "geometry is correct after mousemove"); + } + + // a) click + // b) dblclick + // c) mousedown holding shift key + // d) mousemove holding shift key + function test_sequence4(t) { + t.plan(2); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, + {done: function(g) { log.geometry = g; }}, + {stopDown: false, stopUp: false} + ); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + log = {}; + + // a) click on (0, 0) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + // b) dblclick on (1, 1) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(1, 1)}); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-150, 75), // (0, 0) + new OpenLayers.Geometry.Point(-149, 74) // (1, 1) + ]), "geometry is correct after dblclick"); + // c) mousedown holding shift key on (1, 1) + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + // d) mousemove holding shift key to (10, 10) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(10, 10), shiftKey: true}); + t.geom_eq(handler.line.geometry, + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-149, 74), // (1, 1) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]), "geometry is correct after mousemove"); + } + + + // a) tap + // c) doubletap + function test_touch_sequence1(t) { + t.plan(19); + + // set up + + var log; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, { + done: function(g, f) { + log = {type: 'done', geometry: g, feature: f}; + }, + modify: function(g, f) { + log = {type: 'modify', geometry: g, feature: f}; + } + }, { + doubleTouchTolerance: 2 + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.activate(); + + // test + + var ret; + + // tap on (1, 0) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(0, 0)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(1, 0)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-149, 75), + "[touchend] correct point"); + + // doubletap on (10, 10) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(9, 10)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(10, 10)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-140, 65), + "[touchend] correct point"); + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(11, 10)}); + t.ok(!ret, '[touchstart] event does not propagate'); + t.eq(log.type, 'done', '[touchend] feature finalized'); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-149, 75), // (1, 0) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]), "[touchstart] final geometry is correct"); + log = null; + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log, null, '[touchend] feature not finalized or modified'); + + // tear down + + map.destroy(); + } + + // a) tap + // b) tap-move + // c) doubletap + function test_touch_sequence2(t) { + t.plan(25); + + // set up + + var log; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, { + done: function(g, f) { + log = {type: 'done', geometry: g, feature: f}; + }, + modify: function(g, f) { + log = {type: 'modify', geometry: g, feature: f}; + } + }, { + doubleTouchTolerance: 2 + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.activate(); + + // test + + var ret; + + // tap on (1, 0) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(0, 0)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(1, 0)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-149, 75), + "[touchend] correct point"); + + // tap-move + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(9, 10)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(20, 20)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log, null, '[touchend] feature not finalized or modified'); + + // doubletap on (10, 10) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(9, 10)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(10, 10)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-140, 65), + "[touchend] correct point"); + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(11, 10)}); + t.ok(!ret, '[touchstart] event does not propagate'); + t.eq(log.type, 'done', '[touchend] feature finalized'); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-149, 75), // (1, 0) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]), "[touchstart] final geometry is correct"); + log = null; + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log, null, '[touchend] feature not finalized or modified'); + + // tear down + + map.destroy(); + } + + function test_persist_one_click_freehand(t) { + t.plan(3); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, {}, {persist: true}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + var feature1 = handler.line; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + t.ok(feature1.layer != null, "a) feature1 not destroyed"); + + // one click freehand + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + var feature2 = handler.line; + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + t.ok(feature2.layer != null, "b) feature2 not destroyed"); + t.ok(feature1.layer == null, "b) feature1 destroyed"); + + map.destroy(); + } + + function test_set_freehand(t) { + t.plan(5); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + + var geo, pointsCount; + var handler = new OpenLayers.Handler.Path(control, { + done: function(g) { + geo = g; + }, + point: function() { + pointsCount++; + } + }, + {freehand: true} + ); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + geo = null; + pointsCount = 0; + // Using mouse events + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(2, 2)}); + t.ok(geo != null, "feature drawn when mouseup"); + t.eq(pointsCount, 2, "two points have been added"); + + handler.deactivate(); + var geoMouse = geo; + + handler.activate(); + + geo = null; + pointsCount = 0; + // Using touch events + handler.touchstart( + {type: "touchstart", xy: new OpenLayers.Pixel(0, 0)}); + try { + handler.touchmove( + {type: "touchmove", xy: new OpenLayers.Pixel(1, 1)}); + handler.touchmove( + {type: "touchmove", xy: new OpenLayers.Pixel(2, 2)}); + handler.touchend( + {type: "touchend"}); + } catch(err) { + t.fail("occurred errors using touch events"); + } + t.ok(geo != null, "feature drawn when touchend"); + t.eq(pointsCount, 2, "two points have been added"); + + t.geom_eq(geo, geoMouse, + "geometry obtained using the mouse and touch events are the same"); + + map.destroy(); + } + + function test_citeComplaint(t) { + t.plan(2); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.OSM()); + var layer = new OpenLayers.Layer.Vector(); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Path(control, {}); + control.handler = handler; + map.addControl(control); + map.zoomToExtent(new OpenLayers.Bounds(-24225034.496992, -11368938.517442, -14206280.326992, -1350184.3474418)); + handler.activate(); + handler.createFeature(new OpenLayers.Pixel(100, 50)); + t.ok(handler.point.geometry.x < 0, "Geometry started correctly when wrapping the dateline using citeCompliant false"); + control.deactivate(); + + handler = new OpenLayers.Handler.Path(control, {}, {citeCompliant: true}); + control.handler = handler; + control.activate(); + handler.createFeature(new OpenLayers.Pixel(100, 50)); + t.ok(handler.point.geometry.x > 0, "Geometry started correctly when wrapping the dateline using citeCompliant true"); + + map.destroy(); + } + + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Pinch.html b/misc/openlayers/tests/Handler/Pinch.html new file mode 100644 index 0000000..c4883df --- /dev/null +++ b/misc/openlayers/tests/Handler/Pinch.html @@ -0,0 +1,285 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_constructor(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + }; + var handler = new OpenLayers.Handler.Pinch(control, callbacks, options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_activate(t) { + t.plan(3); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Pinch(control); + handler.active = true; + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + handler.pinching = true; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + t.ok(!handler.pinching, + "activate sets pinching to false"); + + } + + function test_events(t) { + // each handled event should be activated twice when handler is + // activated, so: + // 27 = 4tests * 2*3events + 1tests * 3events + t.plan(27); + + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Pinch(control); + + // list below events that should be handled (events) and those + // that should not be handled (nonevents) by the handler + var events = ["touchend", "touchmove", "touchstart"]; + var nonevents = ["mousedown", "mouseup", "mousemove", "mouseout", + "click", "dblclick", "resize", "focus", "blur"]; + map.events.registerPriority = function(type, obj, func) { + // this is one of the mock handler methods + t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, + "registered method is not one of the events " + + "that should not be handled: " + type); + t.ok(OpenLayers.Util.indexOf(events, type) > -1, + "activate calls registerPriority with browser event: " + type); + t.eq(typeof func, "function", + "activate calls registerPriority with a function"); + t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Pinch", + "activate calls registerPriority with the handler"); + }; + handler.activate(); + handler.deactivate(); + + // set browser event like properties on the handler + for(var i=0; i<events.length; ++i) { + setMethod(events[i]); + } + function setMethod(key) { + handler[key] = function() {return key;}; + } + + map.events.registerPriority = function(type, obj, func) { + var r = func(); + if(typeof r == "string") { + t.eq(r, type, + "activate calls registerPriority with the correct method"); + } + } + handler.activate(); + + } + + function test_callbacks(t) { + t.plan(32); + + var map = new OpenLayers.Map('map', {controls: [], fallThrough: true}); + + var control = new OpenLayers.Control(); + map.addControl(control); + + // set fake values for touches + var testEvents = { + start: { + type: 'touchstart', + touches: [{ + clientX: 100, + clientY: 0 + }, { + clientX: 0, + clientY: 0 + }] + }, + move: { + type: 'touchmove', + touches: [{ + clientX: 100, + clientY: 0 + }, { + clientX: 20, + clientY: 0 + }] + }, + done: { + type: 'touchend', + touches: [] + } + }; + + // set callback methods + var customCb = OpenLayers.Function.False; + var cb = function(evt) { + var callback = evt.type.replace("touch", "").replace("end", "done");; + var tch = testEvents[callback].touches; + t.ok(evt.touches[0].clientX == tch[0].clientX && + evt.touches[0].clientY == tch[0].clientY, + "touchstart sets first touch position correctly in evt"); + t.ok(evt.touches[1].clientX == tch[1].clientX && + evt.touches[1].clientY == tch[1].clientY, + "touchstart sets second touch position correctly in evt"); + t.eq(handler.start.distance, 100, "start distance is " + + "always the same"); + customCb.apply(this, arguments); + } + var callbacks = { + start: cb, + move: cb, + done: function () { + customCb.apply(this, arguments); + } + }; + + var handler = new OpenLayers.Handler.Pinch(control, callbacks); + handler.activate(); + + var old_isMultiTouch = OpenLayers.Event.isMultiTouch; + var old_stop = OpenLayers.Event.stop; + + // test single touch + OpenLayers.Event.isMultiTouch = function() { + return false; + } + + // no callbacks with tests expected (pinch not started) + map.events.handleBrowserEvent(testEvents.start); + // test 1, 2, 3 + t.ok(!handler.started, "1) touchstart (singletouch) sets started to false"); + t.eq(handler.start, null, "1) touchstart (singletouch) sets start to null"); + t.eq(handler.last, null, "1) touchstart (singletouch) sets last to null"); + + handler.started = true; + handler.start = { + distance: 100, + delta: 0, + scale: 1 + }; + handler.last = { + distance: 150, + delta: 10, + scale: 1.5 + }; + + // no callbacks with tests expected (multitouch pinch started, so ignores singletouch) + map.events.handleBrowserEvent(testEvents.start); + // test 4, 5, 6 + t.ok(handler.started, "1) touchstart (singletouch) after pinch started is ignored"); + t.ok(!!handler.start, "1) touchstart (singletouch) after pinch started is ignored"); + t.ok(!!handler.last, "1) touchstart (singletouch) after pinch started is ignored"); + + OpenLayers.Event.stop = function(evt, allowDefault) { + if(allowDefault) { + t.fail( + "touchstart is prevented from falling to other elements"); + } + } + OpenLayers.Event.isMultiTouch = function(evt) { + var res = old_isMultiTouch(evt); + t.ok(res, "fake event is a mutitouch touch event"); + return res; + } + customCb = function(evt, pinchdata) { + t.eq(pinchdata.distance, 100, "2) calculated distance is correct"); + t.eq(pinchdata.delta, 0, "2) calculated delta is correct"); + t.eq(pinchdata.scale, 1, "2) calculated scale is correct"); + } + // test 7, 8, 9, 10, 11, 12, 13 + map.events.handleBrowserEvent(testEvents.start); + // test 14, 15 + t.ok(handler.started, "2) touchstart sets the started flag to true"); + t.ok(!handler.pinching, "2) touchstart sets the pinching flag to false"); + + customCb = function(evt, pinchdata) { + t.eq(pinchdata.distance, 80, "3) calculated distance is correct"); + t.eq(pinchdata.delta, 20, "3) calculated delta is correct"); + t.eq(pinchdata.scale, 0.8, "3) calculated scale is correct"); + } + // test 16, 17, 18, 19, 20, 21, 22 + map.events.handleBrowserEvent(testEvents.move); + // test 23, 24 + t.ok(handler.started, "3) started flag still set to true"); + t.ok(handler.pinching, "3) touchmove sets the pinching flag to true"); + + OpenLayers.Event.isMultiTouch = old_isMultiTouch; + + customCb = function(evt, first, last) { + t.eq(first.distance, 100, "4) calculated distance is correct"); + t.eq(first.delta, 0, "4) calculated delta is correct"); + t.eq(first.scale, 1, "4) calculated scale is correct"); + t.eq(last.distance, 80, "4) calculated distance is correct"); + t.eq(last.delta, 20, "4) calculated delta is correct"); + t.eq(last.scale, 0.8, "4) calculated scale is correct"); + } + // test 25, 26, 27, 28, 29, 30 + map.events.handleBrowserEvent(testEvents.done); + // test 31, 32 + t.ok(!handler.started, "4) started flag is set to false"); + t.ok(!handler.pinching, "4) touchdone sets the pinching flag to false"); + + OpenLayers.Event.stop = old_stop; + + // test move or done before start + customCb = function(evt) { + t.fail("should not pass here") + } + // no callbacks with tests expected + map.events.handleBrowserEvent(testEvents.move); + map.events.handleBrowserEvent(testEvents.done); + + } + + function test_deactivate(t) { + t.plan(6); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Pinch(control); + handler.active = false; + var deactivated = handler.deactivate(); + t.ok(!deactivated, + "deactivate returns false if the handler was not already active"); + handler.active = true; + handler.pinching = true; + deactivated = handler.deactivate(); + t.ok(deactivated, + "deactivate returns true if the handler was active already"); + t.ok(!handler.started, + "deactivate sets started to false"); + t.ok(!handler.pinching, + "deactivate sets pinching to false"); + t.ok(handler.start == null, + "deactivate sets start to null"); + t.ok(handler.last == null, + "deactivate sets start to null"); + } + + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Point.html b/misc/openlayers/tests/Handler/Point.html new file mode 100644 index 0000000..b5a7cf3 --- /dev/null +++ b/misc/openlayers/tests/Handler/Point.html @@ -0,0 +1,600 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_Point_constructor(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.Point(control, callbacks, options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_Handler_Point_activation(t) { + t.plan(6); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Point(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.active = true; + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + t.ok(handler.layer instanceof OpenLayers.Layer.Vector, + "activate creates a vector layer"); + t.ok(handler.layer.map == map, + "activate adds the vector layer to the map"); + activated = handler.deactivate(); + t.ok(activated, + "deactivate returns true if the handler was active already"); + var failed = false; + try { + handler.finalize(); + msg = "finalizing after deactivation does not throw an error"; + } catch (err) { + failed = true; + msg = "finalizing after deactivation throws an error"; + } + t.ok(!failed, msg); + map.destroy(); + } + + // http://trac.osgeo.org/openlayers/ticket/3179 + function test_activate_before_map_is_centered(t) { + t.plan(1); + var map = new OpenLayers.Map('map', { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Point(control, {}); + control.handler = handler; + map.addControl(control); + + var error; + try { + handler.activate(); + error = false; + } catch(err) { + error = true; + } + t.ok(!error, "no error on activate"); + } + + function test_Handler_Point_events(t) { + t.plan(49); + var log = []; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Point(control, { + "create": function(g, f) { + log.push({geometry: g, feature: f}); + } + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + // list below events that should be handled (events) and those + // that should not be handled (nonevents) by the handler + var events = ["click", "dblclick", "mousedown", "mouseup", "mousemove", "mouseout", "touchstart", "touchmove", "touchend"]; + var nonevents = ["resize", "focus", "blur"]; + map.events.registerPriority = function(type, obj, func) { + var r = func(); + if(typeof r == "string") { + // this is one of the mock handler methods + t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, + "registered method is not one of the events " + + "that should not be handled"); + t.ok(OpenLayers.Util.indexOf(events, type) > -1, + "activate calls registerPriority with browser event: " + type); + t.eq(typeof func, "function", + "activate calls registerPriority with a function"); + t.eq(func(), type, + "activate calls registerPriority with the correct method"); + t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Point", + "activate calls registerPriority with the handler"); + } + } + + // set browser event like properties on the handler + for(var i=0; i<events.length; ++i) { + setMethod(events[i]); + } + function setMethod(key) { + handler[key] = function() {return key}; + } + + var activated = handler.activate(); + handler.destroy(); + + // test that click and dblclick are stopped + var handler = new OpenLayers.Handler.Point(control); + var oldStop = OpenLayers.Event.stop; + OpenLayers.Event.stop = function(evt) { + t.ok(evt.type == "click" || evt.type == "dblclick", + evt.type + " stopped"); + } + t.eq(handler.click({type: "click"}), false, "click returns false"); + t.eq(handler.dblclick({type: "dblclick"}), false, "dblclick returns false"); + OpenLayers.Event.stop = oldStop; + + } + + function test_callbacks(t) { + t.plan(24); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var logs = [], log; + var handler = new OpenLayers.Handler.Point(control, { + create: function() { + logs.push({type: "create", args: arguments}); + }, + modify: function() { + logs.push({type: "modify", args: arguments}); + }, + done: function() { + logs.push({type: "done", args: arguments}); + }, + cancel: function() { + logs.push({type: "cancel", args: arguments}); + } + }, + { + pixelTolerance: 0, + dblclickTolerance: 0 + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + // mouse down + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + t.eq(logs.length, 2, "[mousedown] called back twice"); + log = logs.shift(); + t.eq(log.type, "create", "[mousedown] create called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mousedown] correct point"); + t.geom_eq(log.args[1].geometry, + new OpenLayers.Geometry.Point(-150, 75), + "[mousedown] correct feature"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousedown] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mousedown] correct point"); + t.geom_eq(log.args[1].geometry, + new OpenLayers.Geometry.Point(-150, 75), + "[mousedown] correct feature"); + // mouse move + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 0)}); + t.eq(logs.length, 0, "[mousemove] not called back"); + // mouse up (no finalize - we moved) + handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(1, 0)}); + t.eq(logs.length, 0, "[mouseup] not called back"); + // mouse move + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 0)}); + t.eq(logs.length, 1, "[mousemove] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousemove] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-148, 75), + "[mousemove] correct point"); + t.geom_eq(log.args[1].geometry, + new OpenLayers.Geometry.Point(-148, 75), + "[mousemove] correct feature"); + // mouse down + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(2, 0)}); + t.eq(logs.length, 1, "[mousedown] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousedown] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-148, 75), + "[mousedown] correct point"); + t.geom_eq(log.args[1].geometry, + new OpenLayers.Geometry.Point(-148, 75), + "[mousedown] correct feature"); + // mouse up + handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(2, 0)}); + t.eq(logs.length, 1, "[mouseup] called back"); + log = logs.shift(); + t.eq(log.type, "done", "[mouseup] done called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-148, 75), + "[mouseup] correct point"); + // mouse up on same pixel + handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(2, 0)}); + t.eq(logs.length, 0, "[mouseup] not called back"); + // cancel + handler.cancel(); + t.eq(logs.length, 1, "[cancel] called back"); + log = logs.shift(); + t.eq(log.type, "cancel", "[cancel] cancel called"); + t.eq(log.args[0], null, "[cancel] got null"); + + map.destroy(); + } + + function test_persist(t) { + t.plan(3); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Point(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + handler.persist = false; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + t.eq(handler.layer.features.length, 0, + "feature destroyed on mouseup when persist is false"); + + handler.persist = true; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 0)}); + t.eq(handler.layer.features.length, 1, + "feature not destroyed on mouseup when persist is true"); + var feature = handler.layer.features[0]; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(2, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(2, 0)}); + t.ok(handler.layer.features[0] !== feature, + "persisted feature destroyed on next mouseup"); + + map.destroy(); + } + + + function test_Handler_Point_deactivation(t) { + t.plan(5); + var log = []; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Point(control, { + "cancel": function(g) { + log.push({geometry: g}); + } + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + handler.mousemove({xy: new OpenLayers.Pixel(0, 0)}); + var _layer = handler.layer; + var _geometry = handler.point.geometry; + handler.deactivate(); + t.eq(_layer.map, null, + "deactivates removes the layer from the map"); + t.eq(handler.layer, null, + "deactivates sets its \"layer\" property to null"); + t.eq(log.length, 1, + "deactivates calls \"cancel\" once"); + t.ok(log[0].geometry.equals(_geometry), + "\"cancel\" called with expected geometry"); + + handler.activate(); + handler.layer.destroy(); + handler.deactivate(); + t.eq(handler.layer, null, + "deactivate doesn't throw an error if layer was" + + " previously destroyed"); + + map.destroy(); + } + + function test_Handler_Point_bounds(t) { + t.plan(4); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("", "", {})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Point(control, {}); + var activated = handler.activate(); + var px = new OpenLayers.Pixel(150, 75); + var evt = {xy: px, which: 1}; + handler.mousemove(evt); + var lonlat = map.getLonLatFromPixel(px); + t.eq(handler.point.geometry.x, lonlat.lon, "X is correct"); + t.eq(handler.point.geometry.y, lonlat.lat, "Y is correct"); + t.ok(handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(lonlat.lon,lonlat.lat,lonlat.lon,lonlat.lat)), "Correct bounds"); + var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1}; + handler.mousemove(evt); + t.ok(!handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(0,0,0,0)), "Bounds changed after moving mouse"); + } + + function test_Handler_Point_destroy(t) { + t.plan(4); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("", "", {})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'}); + + handler.activate(); + handler.mousemove({xy: new OpenLayers.Pixel(150, 75)}); + + t.ok(handler.layer, + "handler has a layer prior to destroy"); + t.ok(handler.point, + "handler has a point prior to destroy"); + handler.destroy(); + t.eq(handler.layer, null, + "handler.layer is null after destroy"); + t.eq(handler.point, null, + "handler.point is null after destroy"); + } + + function test_touchstart(t) { + // a test to verify that the touchstart function does + // unregister the mouse listeners when it's called the + // first time + + t.plan(4); + + // set up + + var map = new OpenLayers.Map("map", { + resolutions: [1], + controls: [] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Point(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.activate(); + + var eventTypes = ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick', + 'mouseout']; + + function allRegistered() { + var eventType, + listeners, + listener, + flag; + for(var i=0, ilen=eventTypes.length; i<ilen; i++) { + flag = false; + eventType = eventTypes[i]; + listeners = map.events.listeners[eventType]; + for(var j=0, jlen=listeners.length; j<jlen; j++) { + listener = listeners[j]; + if(listener.func === handler[eventType] && listener.obj === handler) { + flag = true; + break; + } + } + if(!flag) { + return false; + } + } + return true; + } + + function noneRegistered() { + var eventType, + times, + flag = false; + for(var i=0, ilen=eventTypes.length; i<ilen; i++) { + eventType = eventTypes[i]; + times = map.events.listeners[eventType].length; + if (times != 0) { + t.fail(eventType + " is registered " + times + " times"); + flag = true; + } + } + return !flag; + } + + + // test + + t.ok(allRegistered(), 'mouse listeners are registered'); + handler.touchstart({xy: new OpenLayers.Pixel(0, 0)}); + t.ok(noneRegistered(), 'mouse listeners are unregistered'); + t.ok(handler.touch, 'handler.touch is set'); + + handler.deactivate(); + t.ok(!handler.touch, 'handler.touch is not set'); + + // tear down + + map.destroy(); + } + + + // + // Sequence tests + // + // Sequence tests basically involve executing a sequence of events + // and testing the resulting geometry. + // + // Below are tests for various drawing sequences. Tests can be + // added here each a non-working sequence is found. + // + + // tap + function test_touch_sequence1(t) { + t.plan(8); + + // set up + + var log; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Point(control, { + done: function(g, f) { + log = {geometry: g, feature: f}; + } + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.activate(); + + // test + + var ret; + + // tap on (1, 0) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(0, 0)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] no finalization'); + t.eq(handler.point, null, '[touchstart] feature not modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(1, 0)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] no finalization'); + t.eq(handler.point, null, '[touchmove] feature not modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-149, 75), + "[touchend] correct point"); + // tear down + + map.destroy(); + } + + // tap-move + function test_touch_sequence2(t) { + t.plan(9); + + // set up + + var log; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Point(control, { + done: function(g, f) { + log = {geometry: g, feature: f}; + } + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.activate(); + + // test + + var ret; + + // tap-move (0, 0) -> (9, 0) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(0, 0)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] no finalization'); + t.eq(handler.point, null, null, + '[touchstart] feature not modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(9, 0)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] no finalization'); + t.eq(handler.point, null, + '[touchmove] feature not modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log, null, '[touchend] no finalization'); + t.eq(handler.point, null, + '[touchend] feature not modified'); + + // tear down + + map.destroy(); + } + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/Polygon.html b/misc/openlayers/tests/Handler/Polygon.html new file mode 100644 index 0000000..8fad5dd --- /dev/null +++ b/misc/openlayers/tests/Handler/Polygon.html @@ -0,0 +1,1161 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_Polygon_constructor(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.Polygon(control, callbacks, options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_Handler_Polygon_activation(t) { + t.plan(5); + var log = []; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Polygon(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.active = true; + + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + t.ok(handler.layer instanceof OpenLayers.Layer.Vector, + "activate creates a vector layer"); + t.ok(handler.layer.map == map, + "activate adds the vector layer to the map"); + activated = handler.deactivate(); + t.ok(activated, + "deactivate returns true if the handler was active already"); + + map.destroy(); + } + + // See: http://trac.osgeo.org/openlayers/ticket/3179 + function test_activate_before_map_is_centered(t) { + t.plan(1); + var map = new OpenLayers.Map('map', { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control(); + var handler = new OpenLayers.Handler.Polygon(control, {}); + control.handler = handler; + map.addControl(control); + + var error; + try { + handler.activate(); + error = false; + } catch(err) { + error = true; + } + t.ok(!error, "no error on activate"); + } + + function test_bounds_stopDown_true(t) { + t.plan(2); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("", "", {})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Polygon(control, {}, + {stopDown: true, stopUp: true}); + var activated = handler.activate(); + // click on (150, 75) + var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; + handler.mousemove(evt); + handler.mousedown(evt); + handler.mouseup(evt); + // click on (175, 100) + evt = {xy: new OpenLayers.Pixel(175, 100), which: 1}; + handler.mousemove(evt); + handler.mousedown(evt); + handler.mouseup(evt); + t.ok(handler.line.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)), "Correct bounds"); + // mousedown on (175, 100) + evt = {xy: new OpenLayers.Pixel(175, 100), which: 1}; + handler.mousedown(evt); + // mousemove to (125, 100) + evt = {xy: new OpenLayers.Pixel(125, 100), which: 1}; + handler.mousemove(evt); + // test that the bounds have changed + t.ok(!handler.polygon.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)), + "Correct bounds after dragging without letting go. (Came out as "+handler.line.geometry.getBounds().toBBOX() + ".)"); + map.destroy(); + } + + function test_callbacks(t) { + t.plan(39); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({ + }); + var logs = [], log; + var handler = new OpenLayers.Handler.Polygon(control, { + create: function() { + logs.push({type: "create", args: arguments}); + }, + point: function() { + logs.push({type: "point", args: arguments}); + }, + modify: function() { + logs.push({type: "modify", args: arguments}); + }, + done: function() { + logs.push({type: "done", args: arguments}); + }, + cancel: function() { + logs.push({type: "cancel", args: arguments}); + } + }, + { + pixelTolerance: 0 + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + // create polygon + handler.activate(); + + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + t.eq(logs.length, 2, "[mousemove] called back"); + log = logs.shift(); + t.eq(log.type, "create", "[activate] create called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mousemove] correct point"); + t.ok(log.args[1] == handler.polygon, + "[mousemove] correct feature"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousemove] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mousemove] correct point"); + t.ok(log.args[1] === handler.polygon, + "[mousemove] correct feature"); + // mouse down + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + t.eq(logs.length, 1, "[mousedown] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousedown] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mousedown] correct point"); + t.ok(log.args[1] === handler.polygon, + "[mousedown] correct feature"); + // mouse up + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + t.eq(logs.length, 2, "[mouseup] called back twice"); + log = logs.shift(); + t.eq(log.type, "point", "[mouseup] point called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mouseup] correct point"); + var geom = new OpenLayers.Geometry.Polygon([ + new OpenLayers.Geometry.LinearRing([ + new OpenLayers.Geometry.Point(-150, 75) + ]) + ]); + geom.components[0].addComponent( + new OpenLayers.Geometry.Point(-150, 75), + geom.components[0].components.length + ); + t.geom_eq(log.args[1], geom, "[mouseup] correct polygon"); + log = logs.shift(); + t.eq(log.type, "modify", "[mouseup] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), + "[mouseup] correct point"); + t.ok(log.args[1] == handler.polygon, + "[mouseup] correct feature"); + // mouse move + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(10, 10)}); + t.eq(logs.length, 1, "[mousemove] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousemove] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-140, 65), + "[mousemove] correct point"); + t.ok(log.args[1] === handler.polygon, + "[mousemove] correct feature"); + // mouse down + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(10, 10)}); + t.eq(logs.length, 1, "[mousedown] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousedown] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-140, 65), + "[mousedown] correct point"); + t.ok(log.args[1] === handler.polygon, + "[mousedown] correct feature"); + // mouse up + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(10, 10)}); + log = logs.shift(); + log = logs.shift(); + // move to 0, 10 and double click + // mouse move + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 10)}); + t.eq(logs.length, 1, "[mousemove] called back"); + log = logs.shift(); + t.eq(log.type, "modify", "[mousemove] modify called"); + t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 65), + "[mousemove] correct point"); + t.ok(log.args[1] === handler.polygon, + "[mousemove] correct feature"); + // mouse down + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 10)}); + t.eq(logs.length, 1, "[mousedown] not called back"); + log = logs.shift(); + // mouse up + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 10)}); + t.eq(logs.length, 2, "[mouseup] called back"); + log = logs.shift(); + log = logs.shift(); + // mouse down + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 10)}); + t.eq(logs.length, 0, "[mousedown] not called back"); + // mouse up + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 10)}); + t.eq(logs.length, 0, "[mouseup] not called back"); + // dblclick + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(0, 10)}); + t.eq(logs.length, 1, "[dblclick] called back"); + log = logs.shift(); + t.eq(log.type, "done", "[dblclick] done called"); + t.geom_eq( + log.args[0], + new OpenLayers.Geometry.Polygon([ + new OpenLayers.Geometry.LinearRing([ + new OpenLayers.Geometry.Point(-150, 75), + new OpenLayers.Geometry.Point(-140, 65), + new OpenLayers.Geometry.Point(-150, 65), + new OpenLayers.Geometry.Point(-150, 75) + ]) + ]), + "[dblclick] correct polygon" + ); + // cancel + handler.cancel(); + t.eq(logs.length, 1, "[cancel] called back"); + log = logs.shift(); + t.eq(log.type, "cancel", "[cancel] canced called"); + + map.destroy(); + } + + function test_toggle_freehand(t) { + t.plan(2); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Polygon(control, { + done: function(g) { + log++; + } + }, {persist: true}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + log = 0; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.eq(log, 1, "feature drawn when shift pressed on mousedown"); + + log = 0; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: false}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.eq(log, 0, "feature not drawn when shift not pressed on mousedown"); + } + + function test_persist(t) { + t.plan(4); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Polygon(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + handler.persist = false; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature1 = handler.polygon; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(2, 2)}); + t.ok(feature1.layer == null, "a) feature1 destroyed"); + + handler.persist = true; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature2 = handler.polygon; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(2, 2)}); + t.ok(feature2.layer != null, "b) feature2 not destroyed"); + + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature3 = handler.polygon; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(1, 1)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1)}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(2, 2)}); + t.ok(feature3.layer != null, "c) feature3 not destroyed"); + t.ok(feature2.layer == null, "c) feature2 destroyed"); + + map.destroy(); + } + + function test_persist_freehand(t) { + t.plan(6); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Polygon(control, {}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + + handler.persist = false; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature1 = handler.polygon; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + t.ok(feature1.layer == null, "a) feature1 destroyed"); + + handler.persist = true; + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature2 = handler.polygon; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + t.ok(feature2.layer != null, "b) feature2 not destroyed"); + + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + var feature3 = handler.polygon; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + t.ok(feature3.layer != null, "c) feature3 not destroyed"); + t.ok(feature2.layer == null, "c) feature2 destroyed"); + + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + feature4 = handler.polygon; + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: false}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1), shiftKey: true}); + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(2, 2), shiftKey: true}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true}); + t.ok(feature4.layer != null, "d) feature4 not destroyed"); + t.ok(feature3.layer == null, "c) feature3 destroyed"); + + map.destroy(); + } + + function test_rings(t) { + t.plan(12); + + var log = []; + var map = new OpenLayers.Map({ + div: "map", + resolutions: [1], + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + layers: [ + new OpenLayers.Layer.Vector(null, { + isBaseLayer: true, + eventListeners: { + featureadded: function(event) { + log.push(event); + }, + sketchmodified: function(event) { + log.push(event); + }, + sketchcomplete: function(event) { + log.push(event); + } + } + }) + ], + center: new OpenLayers.LonLat(0, 0), + zoom: 0 + }); + + // create control for drawing polygons with holes + var draw = new OpenLayers.Control.DrawFeature( + map.layers[0], + OpenLayers.Handler.Polygon, + {handlerOptions: { + holeModifier: "altKey", + pixelTolerance: 0 + }} + ); + map.addControl(draw); + draw.activate(); + + var event; + function trigger(type, event) { + map.events.triggerEvent(type, OpenLayers.Util.extend({}, event)); + } + + // a) draw a polygon + log = []; + // start at -9, 9 + event = {xy: new OpenLayers.Pixel(-9, 9)}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to -1, 9 + event = {xy: new OpenLayers.Pixel(-1, 9)}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to -1, 1 + event = {xy: new OpenLayers.Pixel(-1, 1)}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to -9, 1 + event = {xy: new OpenLayers.Pixel(-9, 1)}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // finish + event = {xy: new OpenLayers.Pixel(-9, 1)}; + trigger("mousedown", event); + trigger("mouseup", event); + trigger("dblclick", event); + + // make assertions + t.eq(log.length, 14, "a) correct number of events"); + t.eq(log[log.length-1].type, "featureadded", "a) featureadded event last"); + t.eq(log[log.length-1].feature.geometry.getArea(), 64, "a) correct polygon area"); + + // b) draw a hole + log = []; + // start at -6, 6 + event = {xy: new OpenLayers.Pixel(-6, 6), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to -3, 6 + event = {xy: new OpenLayers.Pixel(-3, 6), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to -3, 3 + event = {xy: new OpenLayers.Pixel(-3, 3), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to -6, 3 + event = {xy: new OpenLayers.Pixel(-6, 3), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // finish + event = {xy: new OpenLayers.Pixel(-6, 3), altKey: true}; + trigger("mousedown", event); + trigger("mouseup", event); + trigger("dblclick", event); + + // make assertions + t.eq(log.length, 13, "b) correct number of events"); + t.eq(log[log.length-1].type, "sketchcomplete", "b) sketchcomplete event last"); + t.eq(log[log.length-1].feature.geometry.getArea(), 55, "b) correct polygon area"); + + + // c) draw a polygon that overlaps the first + log = []; + // start at -2, 2 + event = {xy: new OpenLayers.Pixel(-2, 2)}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to 2, 2 + event = {xy: new OpenLayers.Pixel(2, 2)}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to 2, -2 + event = {xy: new OpenLayers.Pixel(2, -2)}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to -2, -2 + event = {xy: new OpenLayers.Pixel(-2, -2)}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // finish + event = {xy: new OpenLayers.Pixel(-2, -2)}; + trigger("mousedown", event); + trigger("mouseup", event); + trigger("dblclick", event); + + // make assertions + t.eq(log.length, 14, "c) correct number of events"); + t.eq(log[log.length-1].type, "featureadded", "c) featureadded event last"); + t.eq(log[log.length-1].feature.geometry.getArea(), 16, "c) correct polygon area"); + + // d) draw a hole that tries to go outside the exterior ring + log = []; + // start at -1, 1 + event = {xy: new OpenLayers.Pixel(-1, 1), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to 1, 1 + event = {xy: new OpenLayers.Pixel(1, 1), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // try to draw to -8, 8 (ouside active polygon) + event = {xy: new OpenLayers.Pixel(-8, 8), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to 1, -1 + event = {xy: new OpenLayers.Pixel(1, -1), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // draw to -1, -1 + event = {xy: new OpenLayers.Pixel(-1, -1), altKey: true}; + trigger("mousemove", event); + trigger("mousedown", event); + trigger("mouseup", event); + // finish + event = {xy: new OpenLayers.Pixel(-1, 1), altKey: true}; + trigger("mousedown", event); + trigger("mouseup", event); + trigger("dblclick", event); + + // make assertions + t.eq(log.length, 18, "d) correct number of events"); + t.eq(log[log.length-1].type, "sketchcomplete", "d) sketchcomplete event last"); + t.eq(log[log.length-1].feature.geometry.getArea(), 12, "d) correct polygon area"); + + + map.destroy(); + } + + function test_Handler_Polygon_destroy(t) { + t.plan(8); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("", "", {})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.Polygon(control, {foo: 'bar'}); + + handler.activate(); + var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; + handler.mousedown(evt); + + t.ok(handler.layer, + "handler has a layer prior to destroy"); + t.ok(handler.point, + "handler has a point prior to destroy"); + t.ok(handler.line, + "handler has a line prior to destroy"); + t.ok(handler.polygon, + "handler has a polygon prior to destroy"); + handler.destroy(); + t.eq(handler.layer, null, + "handler.layer is null after destroy"); + t.eq(handler.point, null, + "handler.point is null after destroy"); + t.eq(handler.line, null, + "handler.line is null after destroy"); + t.eq(handler.polygon, null, + "handler.polygon is null after destroy"); + map.destroy(); + } + + function test_insertXY(t) { + t.plan(3); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control.DrawFeature( + layer, OpenLayers.Handler.Polygon + ); + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + control.activate(); + var handler = control.handler; + + function userClick(x, y) { + var px = new OpenLayers.Pixel(x, y); + handler.mousemove({type: "mousemove", xy: px}); + handler.mousedown({type: "mousedown", xy: px}); + handler.mouseup({type: "mouseup", xy: px}); + } + + // add points at px(0, 0) and px(10, 10) + userClick(0, 0); + userClick(10, 10); + t.eq(handler.line.geometry.components.length, 4, "ring has four points after two clicks"); + + // programmatically add a point + handler.insertXY(5, 6); + t.eq(handler.line.geometry.components.length, 5, "ring has five points after insertXY"); + t.geom_eq( + handler.line.geometry.components[2], + new OpenLayers.Geometry.Point(5, 6), + "third point comes from insertXY" + ); + + map.destroy(); + + } + + // + // Sequence tests + // + // Sequence tests basically involve executing a sequence of events + // and testing the resulting geometry. + // + // Below are tests for various drawing sequences. Tests can be + // added here each a non-working sequence is found. + // + + // stopDown:true, stopUp:true + // a) click on (0, 0) + // b) mousedown on (0.5, 0.5) + // c) mouseup on (1, 1) + // d) click on (0, 10) + // e) dblclick on (10, 10) + function test_sequence1(t) { + t.plan(1); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Polygon(control, + {done: function(g) { log.geometry = g; }}, + {stopDown: true, stopUp: true, + pixelTolerance: 0} + ); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + log = {}; + + // a) click on (0, 0) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + // b) mousedown on (0.5, 0.5) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0.5, 0.5)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0.5, 0.5)}); + // c) mouseup on (1, 1) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1)}); + // d) click on (0, 10) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 10)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 10)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 10)}); + // e) dblclick on (10, 10) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(10, 10)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(10, 10)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(10, 10)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(10, 10)}); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.Polygon([ + new OpenLayers.Geometry.LinearRing([ + new OpenLayers.Geometry.Point(-150, 75), // (0, 0) + new OpenLayers.Geometry.Point(-150, 65), // (0, 10) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]) + ]), "geometry is correct"); + } + + // stopDown:false, stopUp:false + // a) click on (0, 0) + // b) mousedown on (0.5, 0.5) + // c) mouseup on (1, 1) + // d) click on (0, 10) + // e) dblclick on (10, 10) + function test_sequence2(t) { + t.plan(1); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Polygon(control, + {done: function(g) { log.geometry = g; }}, + {stopDown: false, stopUp: false, + pixelTolerance: 0} + ); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + handler.activate(); + log = {}; + + // a) click on (0, 0) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 0)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 0)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 0)}); + // b) mousedown on (0.5, 0.5) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0.5, 0.5)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0.5, 0.5)}); + // c) mouseup on (1, 1) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(1, 1)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(1, 1)}); + // d) click on (0, 10) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(0, 10)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(0, 10)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(0, 10)}); + // e) dblclick on (10, 10) + handler.mousemove( + {type: "mousemove", xy: new OpenLayers.Pixel(10, 10)}); + handler.mousedown( + {type: "mousedown", xy: new OpenLayers.Pixel(10, 10)}); + handler.mouseup( + {type: "mouseup", xy: new OpenLayers.Pixel(10, 10)}); + handler.dblclick( + {type: "dblclick", xy: new OpenLayers.Pixel(10, 10)}); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.Polygon([ + new OpenLayers.Geometry.LinearRing([ + new OpenLayers.Geometry.Point(-150, 75), // (0, 0) + new OpenLayers.Geometry.Point(-150, 65), // (0, 10) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]) + ]), "geometry is correct"); + } + + // a) tap + // b) tap + // c) doubletap + function test_touch_sequence1(t) { + t.plan(26); + + // set up + + var log; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Polygon(control, { + done: function(g, f) { + log = {type: 'done', geometry: g, feature: f}; + }, + modify: function(g, f) { + log = {type: 'modify', geometry: g, feature: f}; + } + }, { + doubleTouchTolerance: 2 + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.activate(); + + // test + + var ret; + + // tap on (0, 0) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(1, 0)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(0, 0)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-150, 75), + "[touchend] correct point"); + + // tap on (0, 10) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(1, 10)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(0, 10)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-150, 65), + "[touchend] correct point"); + + // doubletap on (10, 10) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(9, 10)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(10, 10)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-140, 65), + "[touchend] correct point"); + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(11, 10)}); + t.ok(!ret, '[touchstart] event does not propagate'); + t.eq(log.type, 'done', '[touchend] feature finalized'); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.Polygon([ + new OpenLayers.Geometry.LinearRing([ + new OpenLayers.Geometry.Point(-150, 75), // (0, 0) + new OpenLayers.Geometry.Point(-150, 65), // (0, 10) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]) + ]), "[touchstart] geometry is correct"); + log = null; + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log, null, '[touchend] feature not finalized or modified'); + + // tear down + + map.destroy(); + } + + // a) tap + // b) tap-move + // c) tap + // d) doubletap + function test_touch_sequence2(t) { + t.plan(32); + + // set up + + var log; + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Polygon(control, { + done: function(g, f) { + log = {type: 'done', geometry: g, feature: f}; + }, + modify: function(g, f) { + log = {type: 'modify', geometry: g, feature: f}; + } + }, { + doubleTouchTolerance: 2 + }); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + handler.activate(); + + // test + + var ret; + + // tap on (0, 0) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(1, 0)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(0, 0)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-150, 75), + "[touchend] correct point"); + + // tap-move + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(1, 10)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(20, 20)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log, null, '[touchend] feature not finalized or modified'); + + // tap on (0, 10) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(1, 10)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(0, 10)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-150, 65), + "[touchend] correct point"); + + // doubletap on (10, 10) + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(9, 10)}); + t.ok(ret, '[touchstart] event propagates'); + t.eq(log, null, '[touchstart] feature not finalized or modified'); + ret = handler.touchmove({xy: new OpenLayers.Pixel(10, 10)}); + t.ok(ret, '[touchmove] event propagates'); + t.eq(log, null, '[touchmove] feature not finalized or modified'); + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log.type, 'modify', '[touchend] feature modified'); + t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-140, 65), + "[touchend] correct point"); + log = null; + ret = handler.touchstart({xy: new OpenLayers.Pixel(11, 10)}); + t.ok(!ret, '[touchstart] event does not propagate'); + t.eq(log.type, 'done', '[touchend] feature finalized'); + t.geom_eq(log.geometry, + new OpenLayers.Geometry.Polygon([ + new OpenLayers.Geometry.LinearRing([ + new OpenLayers.Geometry.Point(-150, 75), // (0, 0) + new OpenLayers.Geometry.Point(-150, 65), // (0, 10) + new OpenLayers.Geometry.Point(-140, 65) // (10, 10) + ]) + ]), "[touchstart] geometry is correct"); + log = null; + ret = handler.touchend({}); + t.ok(ret, '[touchend] event propagates'); + t.eq(log, null, '[touchend] feature not finalized or modified'); + + // tear down + + map.destroy(); + } + + function test_citeComplaint(t) { + t.plan(2); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.OSM()); + var layer = new OpenLayers.Layer.Vector(); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var handler = new OpenLayers.Handler.Polygon(control, {}); + control.handler = handler; + map.addControl(control); + map.zoomToExtent(new OpenLayers.Bounds(-24225034.496992, -11368938.517442, -14206280.326992, -1350184.3474418)); + control.activate(); + handler.createFeature(new OpenLayers.Pixel(100, 50)); + t.ok(handler.point.geometry.x < 0, "Geometry started correctly when wrapping the dateline using citeCompliant false"); + control.deactivate(); + + var handler = new OpenLayers.Handler.Polygon(control, {}, {citeCompliant: true}); + control.handler = handler; + control.activate(); + handler.createFeature(new OpenLayers.Pixel(100, 50)); + t.ok(handler.point.geometry.x > 0, "Geometry started correctly when wrapping the dateline using citeCompliant true"); + + map.destroy(); + } + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> diff --git a/misc/openlayers/tests/Handler/RegularPolygon.html b/misc/openlayers/tests/Handler/RegularPolygon.html new file mode 100644 index 0000000..ee43dc7 --- /dev/null +++ b/misc/openlayers/tests/Handler/RegularPolygon.html @@ -0,0 +1,235 @@ +<html> +<head> + <script src="../OLLoader.js"></script> + <script type="text/javascript"> + function test_Handler_RegularPolygon_constructor(t) { + t.plan(3); + var control = new OpenLayers.Control(); + control.id = Math.random(); + var callbacks = {foo: "bar"}; + var options = {bar: "foo"}; + + var oldInit = OpenLayers.Handler.prototype.initialize; + + OpenLayers.Handler.prototype.initialize = function(con, call, opt) { + t.eq(con.id, control.id, + "constructor calls parent with the correct control"); + t.eq(call, callbacks, + "constructor calls parent with the correct callbacks"); + t.eq(opt, options, + "regular polygon constructor calls parent with the correct options"); + } + var handler = new OpenLayers.Handler.RegularPolygon(control, callbacks, options); + + OpenLayers.Handler.prototype.initialize = oldInit; + } + + function test_Handler_RegularPolygon_activation(t) { + t.plan(3); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.RegularPolygon(control); + handler.active = true; + var activated = handler.activate(); + t.ok(!activated, + "activate returns false if the handler was already active"); + handler.active = false; + activated = handler.activate(); + t.ok(activated, + "activate returns true if the handler was not already active"); + activated = handler.deactivate(); + t.ok(activated, + "deactivate returns true if the handler was active already"); + map.destroy(); + } + + function test_Handler_RegularPolygon_deactivation(t) { + t.plan(1); + var map = new OpenLayers.Map('map'); + var control = new OpenLayers.Control(); + map.addControl(control); + + var handler = new OpenLayers.Handler.RegularPolygon(control, {foo: 'bar'}); + handler.activate(); + handler.layer.destroy(); + handler.deactivate(); + t.eq(handler.layer, null, + "deactivate doesn't throw an error if layer was" + + " previously destroyed"); + map.destroy(); + } + + function test_Handler_RegularPolygon_four_corners(t) { + t.plan(7); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("", "", {})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.RegularPolygon(control, {}); + var activated = handler.activate(); + var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; + handler.down(evt); + var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1}; + handler.move(evt); + t.eq(handler.feature.geometry.getBounds().toBBOX(), + "-35.15625,-35.15625,35.15625,35.15625", + "correct bounds after move"); + t.eq(handler.feature.geometry.components[0].components.length, 5, + "geometry has 5 components"); + t.eq(handler.feature.geometry.CLASS_NAME, + "OpenLayers.Geometry.Polygon", + "geometry is a polygon"); + t.eq(handler.radius, 25*1.40625, "feature radius as set on handler"); + var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1}; + handler.move(evt); + t.eq(handler.feature.geometry.getBounds().toBBOX(), + "-35.15625,-35.15625,35.15625,35.15625", + "correct bounds after move with a fixed radius"); + handler.cancel(); + handler.setOptions({radius:2 / Math.sqrt(2)}); + var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; + handler.down(evt); + + t.eq(handler.feature.geometry.getBounds().toBBOX(), + "-1,-1,1,1", + "bounds with manual radius setting"); + var evt = {xy: new OpenLayers.Pixel(175, 90), which: 1}; + handler.move(evt); + t.eq(handler.feature.geometry.getBounds().toBBOX(), + "34.15625,-22.09375,36.15625,-20.09375", + "bounds with manual radius setting and mousemove"); + map.destroy(); + } + + function test_Handler_RegularPolygon_circle(t) { + t.plan(7); + var map = new OpenLayers.Map('map'); + map.addLayer(new OpenLayers.Layer.WMS("", "", {})); + map.zoomToMaxExtent(); + var control = new OpenLayers.Control(); + map.addControl(control); + var handler = new OpenLayers.Handler.RegularPolygon(control, {}, {'sides':40}); + var activated = handler.activate(); + var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; + handler.down(evt); + var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1}; + handler.move(evt); + t.eq(handler.feature.geometry.getBounds().toBBOX(), + "-35.15625,-35.15625,35.15625,35.15625", + "correct bounds after move"); + t.eq(handler.feature.geometry.components[0].components.length, 41, + "geometry has correct numbre of components"); + t.eq(handler.feature.geometry.CLASS_NAME, + "OpenLayers.Geometry.Polygon", + "geometry is a polygon"); + t.eq(handler.radius, 25*1.40625, "feature radius as set on handler"); + var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1}; + handler.move(evt); + t.eq(handler.feature.geometry.getBounds().toBBOX(), + "-35.823348,-35.823348,35.823348,35.823348", + "correct bounds after move with fixed radius"); + handler.cancel(); + handler.setOptions({radius:1}); + var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; + handler.down(evt); + + t.eq(handler.feature.geometry.getBounds().toBBOX(), + "-0.996917,-0.996917,0.996917,0.996917", + "bounds with manual radius setting"); + var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1}; + handler.move(evt); + t.eq(handler.feature.geometry.getBounds().toBBOX(), + "34.159333,-8.028167,36.153167,-6.034333", + "bounds with manual radius setting and mousemove"); + map.destroy(); + } + + function test_Handler_RegularPolygon_irregular(t) { + t.plan(4); + var map = { + getResolution: function() { + return 1; + } + }; + var layer = { + addFeatures: function() {}, + drawFeature: function(feature, style) { + var ring = feature.geometry.components[0]; + t.eq(ring.components[0].x, 20, "correct right"); + t.eq(ring.components[0].y, 10, "correct bottom"); + t.eq(ring.components[2].x, 10, "correct left"); + t.eq(ring.components[2].y, 15, "correct top"); + }, + getLonLatFromViewPortPx: function(px) { + return {lon: px.x, lat: px.y}; + } + }; + var control = {}; + var options = { + sides: 4, + irregular: true, + layer: layer, + map: map + }; + var handler = new OpenLayers.Handler.RegularPolygon( + control, null, options + ); + handler.origin = new OpenLayers.Geometry.Point(10, 10); + handler.feature = new OpenLayers.Feature.Vector( + new OpenLayers.Geometry.Polygon( + [new OpenLayers.Geometry.LinearRing()] + ) + ); + // should result in a 10 x 5 rectangle + handler.move({xy: {x: 20, y: 15}}); + } + + function test_callbacks(t) { + t.plan(1); + + // setup + var map = new OpenLayers.Map("map"); + + var control = {"map": map}; + + var done = function(geom) { + t.ok(true, + "done callback called even if no move between down and up"); + }; + + var handler = new OpenLayers.Handler.RegularPolygon( + control, {"done": done}); + handler.activate(); + + var xy = new OpenLayers.Pixel(Math.random(), Math.random()); + + var isLeftClick = OpenLayers.Event.isLeftClick; + OpenLayers.Event.isLeftClick = function() { return true; }; + handler.layer = { + renderer: { + clear: OpenLayers.Function.Void + }, + addFeatures: OpenLayers.Function.Void, + drawFeature: OpenLayers.Function.Void, + destroyFeatures: OpenLayers.Function.Void, + getLonLatFromViewPortPx: function() { + return xy; + } + }; + + // test + map.events.triggerEvent("mousedown", {"xy": xy}); + map.events.triggerEvent("mouseup", {"xy": xy}); + + // tear down + OpenLayers.Event.isLeftClick = isLeftClick; + } + + </script> +</head> +<body> + <div id="map" style="width: 300px; height: 150px;"/> +</body> +</html> |