1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 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>
|