3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
5 function test_Control_DragFeature_constructor(t) {
12 var control = new OpenLayers.Control.DragFeature(layer, options);
13 t.ok(control instanceof OpenLayers.Control.DragFeature,
14 "new OpenLayers.Control.DragFeature returns an instance");
15 t.eq(control.layer, "bar",
16 "constructor sets layer correctly");
17 t.eq(control.handlers.feature.geometryTypes, "foo",
18 "constructor sets options correctly on feature handler");
21 function test_Control_DragFeature_destroy(t) {
23 var map = new OpenLayers.Map("map");
24 var layer = new OpenLayers.Layer.Vector();
26 var control = new OpenLayers.Control.DragFeature(layer);
27 control.handlers.drag.destroy = function() {
29 "control.destroy calls destroy on drag handler");
31 control.handlers.feature.destroy = function() {
33 "control.destroy calls destroy on feature handler");
40 function test_Control_DragFeature_activate(t) {
42 var map = new OpenLayers.Map("map");
43 var layer = new OpenLayers.Layer.Vector();
45 var control = new OpenLayers.Control.DragFeature(layer);
46 map.addControl(control);
47 t.ok(!control.handlers.feature.active,
48 "feature handler is not active prior to activating control");
50 t.ok(control.handlers.feature.active,
51 "feature handler is active after activating control");
54 function test_Control_DragFeature_deactivate(t) {
56 var map = new OpenLayers.Map("map");
57 var layer = new OpenLayers.Layer.Vector();
59 var control = new OpenLayers.Control.DragFeature(layer);
60 map.addControl(control);
62 control.handlers.drag.deactivate = function() {
64 "control.deactivate calls deactivate on drag handler");
66 control.handlers.feature.deactivate = function() {
68 "control.deactivate calls deactivate on feature handler");
73 function test_Control_DragFeature_over(t) {
75 var map = new OpenLayers.Map("map");
76 var layer = new OpenLayers.Layer.Vector();
78 var control = new OpenLayers.Control.DragFeature(layer);
79 map.addControl(control);
82 t.ok(!control.handlers.drag.active,
83 "drag handler is not active before over a feature");
85 // simulate a mouseover on a feature
86 var feature = new OpenLayers.Feature.Vector();
87 feature.layer = layer;
88 layer.getFeatureFromEvent = function(evt) {
91 map.events.triggerEvent("mousemove", {type: "mousemove"});
93 t.eq(control.feature.id, feature.id,
94 "control gets the proper feature from the feature handler");
95 t.ok(control.handlers.drag.active,
96 "drag handler activated when over a feature");
99 function test_Control_DragFeature_down(t) {
101 var map = new OpenLayers.Map("map");
102 var layer = new OpenLayers.Layer.Vector();
104 var control = new OpenLayers.Control.DragFeature(layer);
105 map.addControl(control);
109 // simulate a mouseover on a feature
110 var feature = new OpenLayers.Feature.Vector();
111 feature.layer = layer;
112 layer.getFeatureFromEvent = function(evt) {
115 map.events.triggerEvent("mousemove", {type: "mousemove"});
117 // simulate a mousedown on a feature
118 control.onStart = function(feat, pixel) {
119 t.eq(feat.id, feature.id, "onStart called with the correct feature");
120 t.eq(pixel, "bar", "onStart called with the correct pixel");
122 map.events.triggerEvent("mousedown", {xy: "bar", which: 1, type: "mousemove"});
124 t.eq(control.lastPixel, "bar",
125 "mousedown sets the lastPixel correctly");
128 function test_Control_DragFeature_move(t) {
130 var map = new OpenLayers.Map("map");
131 var layer = new OpenLayers.Layer.Vector();
133 var control = new OpenLayers.Control.DragFeature(layer);
134 map.addControl(control);
135 map.getResolution = function() {
141 // mock up a feature - for the sole purpose of testing mousemove
142 var uid = Math.random();
143 layer.getFeatureFromEvent = function() {
144 var geom = new OpenLayers.Geometry.Point(Math.random(),
146 geom.move = function(x, y) {
147 t.eq(x, 2, "move called with dx * res");
148 t.eq(y, -4, "move called with -dy * res");
150 var feature = new OpenLayers.Feature.Vector(geom);
151 feature.layer = layer;
155 layer.drawFeature = function(feature) {
156 t.eq(feature.uid, uid,
157 "layer.drawFeature called with correct feature");
160 // simulate a mouseover on a feature
161 map.events.triggerEvent("mousemove", {type: "mousemove"});
163 // simulate a mousedown on a feature
164 var down = new OpenLayers.Pixel(0, 0);
165 map.events.triggerEvent("mousedown", {xy: down, which: 1, type: "mousemove"});
167 // simulate a mousemove on a feature
168 var move = new OpenLayers.Pixel(1, 2);
169 map.events.triggerEvent("mousemove", {xy: move, which: 1, type: "mousemove"});
173 function test_Control_DragFeature_up(t) {
175 var map = new OpenLayers.Map("map");
176 var layer = new OpenLayers.Layer.Vector();
178 var control = new OpenLayers.Control.DragFeature(layer);
179 map.addControl(control);
183 // simulate a mouseover on a feature
184 var feature = new OpenLayers.Feature.Vector();
185 feature.layer = layer;
186 layer.getFeatureFromEvent = function(evt) {
189 map.events.triggerEvent("mousemove", {type: "mousemove"});
190 t.eq(control.over, true,
191 "mouseover on a feature sets the over property to true");
192 t.ok(OpenLayers.Element.hasClass(control.map.viewPortDiv, "olControlDragFeatureOver"),
193 "mouseover on a feature adds class name to map container");
194 t.eq(control.handlers.drag.active, true,
195 "mouseover on a feature activates drag handler");
197 // simulate a mouse-up on the map, with the mouse still
198 // over the dragged feature
199 control.handlers.drag.started = true;
200 map.events.triggerEvent("mouseup", {type: "mouseup"});
201 t.eq(control.handlers.drag.active, true,
202 "mouseup while still over dragged feature does not deactivate drag handler");
204 // simulate a mouse-up on the map, with the mouse out of
205 // the dragged feature
206 control.handlers.drag.started = true;
207 control.over = false;
208 map.events.triggerEvent("mouseup", {type: "mouseup"});
209 t.eq(control.handlers.drag.active, false,
210 "mouseup deactivates drag handler");
212 control.deactivate();
213 t.ok(!OpenLayers.Element.hasClass(control.map.viewPortDiv, "olControlDragFeatureOver"),
214 "deactivate removes class name from map container");
217 function test_Control_DragFeature_done(t) {
219 var map = new OpenLayers.Map("map");
220 var layer = new OpenLayers.Layer.Vector();
222 var control = new OpenLayers.Control.DragFeature(layer);
223 map.addControl(control);
228 // simulate a mouseover on a feature
229 var feature = new OpenLayers.Feature.Vector();
230 feature.layer = layer;
231 layer.getFeatureFromEvent = function() {
234 map.events.triggerEvent("mousemove", {type: "mousemove"});
235 t.eq(control.feature.id, feature.id,
236 "feature is set on mouse over");
237 control.doneDragging();
238 t.eq(control.feature.id, feature.id,
239 "feature sticks around after doneDragging is called.");
243 function test_Control_DragFeature_out(t) {
245 var map = new OpenLayers.Map("map");
246 var layer = new OpenLayers.Layer.Vector();
248 var control = new OpenLayers.Control.DragFeature(layer);
249 map.addControl(control);
254 // simulate a mouseover on a feature
255 var feature = new OpenLayers.Feature.Vector();
256 feature.layer = layer;
257 layer.getFeatureFromEvent = function() {
260 map.events.triggerEvent("mousemove", {type: "mousemove"});
261 t.eq(control.feature.id, feature.id,
262 "feature is set on mouse over");
264 // simulate a mouseout on a feature
265 layer.getFeatureFromEvent = function() {
268 map.events.triggerEvent("mousemove", {type: "mousemove"});
269 t.ok(control.feature == null,
270 "feature is set to null on mouse out");
277 <div id="map" style="width: 400px; height: 250px;"/>