PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/OpenLayers/tests/Handler/RegularPolygon.html

https://gitlab.com/rsilveira1987/Expresso
HTML | 228 lines | 207 code | 21 blank | 0 comment | 0 complexity | 2e79ec6d382a17d6a34f5ff4cef16551 MD5 | raw file
  1. <html>
  2. <head>
  3. <script src="../../lib/OpenLayers.js"></script>
  4. <script type="text/javascript">
  5. function test_Handler_RegularPolygon_constructor(t) {
  6. t.plan(3);
  7. var control = new OpenLayers.Control();
  8. control.id = Math.random();
  9. var callbacks = {foo: "bar"};
  10. var options = {bar: "foo"};
  11. var oldInit = OpenLayers.Handler.prototype.initialize;
  12. OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
  13. t.eq(con.id, control.id,
  14. "constructor calls parent with the correct control");
  15. t.eq(call, callbacks,
  16. "constructor calls parent with the correct callbacks");
  17. t.eq(opt, options,
  18. "regular polygon constructor calls parent with the correct options");
  19. }
  20. var handler = new OpenLayers.Handler.RegularPolygon(control, callbacks, options);
  21. OpenLayers.Handler.prototype.initialize = oldInit;
  22. }
  23. function test_Handler_RegularPolygon_activation(t) {
  24. t.plan(3);
  25. var map = new OpenLayers.Map('map');
  26. var control = new OpenLayers.Control();
  27. map.addControl(control);
  28. var handler = new OpenLayers.Handler.RegularPolygon(control);
  29. handler.active = true;
  30. var activated = handler.activate();
  31. t.ok(!activated,
  32. "activate returns false if the handler was already active");
  33. handler.active = false;
  34. activated = handler.activate();
  35. t.ok(activated,
  36. "activate returns true if the handler was not already active");
  37. activated = handler.deactivate();
  38. t.ok(activated,
  39. "deactivate returns true if the handler was active already");
  40. map.destroy();
  41. }
  42. function test_Handler_RegularPolygon_deactivation(t) {
  43. t.plan(1);
  44. var map = new OpenLayers.Map('map');
  45. var control = new OpenLayers.Control();
  46. map.addControl(control);
  47. var handler = new OpenLayers.Handler.RegularPolygon(control, {foo: 'bar'});
  48. handler.activate();
  49. handler.layer.destroy();
  50. handler.deactivate();
  51. t.eq(handler.layer, null,
  52. "deactivate doesn't throw an error if layer was" +
  53. " previously destroyed");
  54. map.destroy();
  55. }
  56. function test_Handler_RegularPolygon_four_corners(t) {
  57. t.plan(7);
  58. var map = new OpenLayers.Map('map');
  59. map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
  60. map.zoomToMaxExtent();
  61. var control = new OpenLayers.Control();
  62. map.addControl(control);
  63. var handler = new OpenLayers.Handler.RegularPolygon(control, {});
  64. var activated = handler.activate();
  65. var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
  66. handler.down(evt);
  67. var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1};
  68. handler.move(evt);
  69. t.eq(handler.feature.geometry.getBounds().toBBOX(),
  70. "-35.15625,-35.15625,35.15625,35.15625",
  71. "correct bounds after move");
  72. t.eq(handler.feature.geometry.components[0].components.length, 5,
  73. "geometry has 5 components");
  74. t.eq(handler.feature.geometry.CLASS_NAME,
  75. "OpenLayers.Geometry.Polygon",
  76. "geometry is a polygon");
  77. t.eq(handler.radius, 25*1.40625, "feature radius as set on handler");
  78. var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
  79. handler.move(evt);
  80. t.eq(handler.feature.geometry.getBounds().toBBOX(),
  81. "-35.15625,-35.15625,35.15625,35.15625",
  82. "correct bounds after move with a fixed radius");
  83. handler.cancel();
  84. handler.setOptions({radius:2 / Math.sqrt(2)});
  85. var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
  86. handler.down(evt);
  87. t.eq(handler.feature.geometry.getBounds().toBBOX(),
  88. "-1,-1,1,1",
  89. "bounds with manual radius setting");
  90. var evt = {xy: new OpenLayers.Pixel(175, 90), which: 1};
  91. handler.move(evt);
  92. t.eq(handler.feature.geometry.getBounds().toBBOX(),
  93. "34.15625,-22.09375,36.15625,-20.09375",
  94. "bounds with manual radius setting and mousemove");
  95. map.destroy();
  96. }
  97. function test_Handler_RegularPolygon_circle(t) {
  98. t.plan(7);
  99. var map = new OpenLayers.Map('map');
  100. map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
  101. map.zoomToMaxExtent();
  102. var control = new OpenLayers.Control();
  103. map.addControl(control);
  104. var handler = new OpenLayers.Handler.RegularPolygon(control, {}, {'sides':40});
  105. var activated = handler.activate();
  106. var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
  107. handler.down(evt);
  108. var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1};
  109. handler.move(evt);
  110. t.eq(handler.feature.geometry.getBounds().toBBOX(),
  111. "-35.15625,-35.15625,35.15625,35.15625",
  112. "correct bounds after move");
  113. t.eq(handler.feature.geometry.components[0].components.length, 41,
  114. "geometry has correct numbre of components");
  115. t.eq(handler.feature.geometry.CLASS_NAME,
  116. "OpenLayers.Geometry.Polygon",
  117. "geometry is a polygon");
  118. t.eq(handler.radius, 25*1.40625, "feature radius as set on handler");
  119. var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
  120. handler.move(evt);
  121. t.eq(handler.feature.geometry.getBounds().toBBOX(),
  122. "-35.823348,-35.823348,35.823348,35.823348",
  123. "correct bounds after move with fixed radius");
  124. handler.cancel();
  125. handler.setOptions({radius:1});
  126. var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
  127. handler.down(evt);
  128. t.eq(handler.feature.geometry.getBounds().toBBOX(),
  129. "-0.996917,-0.996917,0.996917,0.996917",
  130. "bounds with manual radius setting");
  131. var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
  132. handler.move(evt);
  133. t.eq(handler.feature.geometry.getBounds().toBBOX(),
  134. "34.159333,-8.028167,36.153167,-6.034333",
  135. "bounds with manual radius setting and mousemove");
  136. map.destroy();
  137. }
  138. function test_Handler_RegularPolygon_irregular(t) {
  139. t.plan(4);
  140. var map = {
  141. getLonLatFromPixel: function(px) {
  142. return {lon: px.x, lat: px.y};
  143. },
  144. getResolution: function() {
  145. return 1;
  146. }
  147. };
  148. var layer = {
  149. addFeatures: function() {},
  150. drawFeature: function(feature, style) {
  151. var ring = feature.geometry.components[0];
  152. t.eq(ring.components[0].x, 20, "correct right");
  153. t.eq(ring.components[0].y, 10, "correct bottom");
  154. t.eq(ring.components[2].x, 10, "correct left");
  155. t.eq(ring.components[2].y, 15, "correct top");
  156. }
  157. };
  158. var control = {};
  159. var options = {
  160. sides: 4,
  161. irregular: true,
  162. layer: layer,
  163. map: map
  164. };
  165. var handler = new OpenLayers.Handler.RegularPolygon(
  166. control, null, options
  167. );
  168. handler.origin = new OpenLayers.Geometry.Point(10, 10);
  169. handler.feature = new OpenLayers.Feature.Vector(
  170. new OpenLayers.Geometry.Polygon(
  171. [new OpenLayers.Geometry.LinearRing()]
  172. )
  173. );
  174. // should result in a 10 x 5 rectangle
  175. handler.move({xy: {x: 20, y: 15}});
  176. }
  177. function test_callbacks(t) {
  178. t.plan(1);
  179. // setup
  180. var map = new OpenLayers.Map("map", {
  181. getLonLatFromPixel: function(px) {
  182. return {lon: px.x, lat: px.y};
  183. }
  184. });
  185. var control = {"map": map};
  186. var done = function(geom) {
  187. t.ok(true,
  188. "done callback called even if no move between down and up");
  189. };
  190. var handler = new OpenLayers.Handler.RegularPolygon(
  191. control, {"done": done});
  192. handler.activate();
  193. var xy = new OpenLayers.Pixel(Math.random(), Math.random());
  194. var isLeftClick = OpenLayers.Event.isLeftClick;
  195. OpenLayers.Event.isLeftClick = function() { return true; };
  196. // test
  197. map.events.triggerEvent("mousedown", {"xy": xy});
  198. map.events.triggerEvent("mouseup", {"xy": xy});
  199. // tear down
  200. OpenLayers.Event.isLeftClick = isLeftClick;
  201. }
  202. </script>
  203. </head>
  204. <body>
  205. <div id="map" style="width: 300px; height: 150px;"/>
  206. </body>
  207. </html>