summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Control/DragFeature.html
blob: cfc3a631f40d0672bfd2406768dc5c0b3eba3cc2 (plain)
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
<html>
<head>
    <script src="../OLLoader.js"></script>
    <script type="text/javascript">
    function test_Control_DragFeature_constructor(t) {
        t.plan(3);
        
        var options = {
            geometryTypes: "foo"
        };
        var layer = "bar";
        var control = new OpenLayers.Control.DragFeature(layer, options);
        t.ok(control instanceof OpenLayers.Control.DragFeature,
             "new OpenLayers.Control.DragFeature returns an instance");
        t.eq(control.layer, "bar",
             "constructor sets layer correctly");        
        t.eq(control.handlers.feature.geometryTypes, "foo",
             "constructor sets options correctly on feature handler");
    }
    
    function test_Control_DragFeature_destroy(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer);
        control.handlers.drag.destroy = function() {
            t.ok(true,
                 "control.destroy calls destroy on drag handler");
        }
        control.handlers.feature.destroy = function() {
            t.ok(true,
                 "control.destroy calls destroy on feature handler");
        }
        
        control.destroy();
        
    }
    
    function test_Control_DragFeature_activate(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer);
        map.addControl(control);
        t.ok(!control.handlers.feature.active,
             "feature handler is not active prior to activating control");
        control.activate();
        t.ok(control.handlers.feature.active,
             "feature handler is active after activating control");
    }

    function test_Control_DragFeature_deactivate(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer);
        map.addControl(control);
        
        control.handlers.drag.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on drag handler");
        }
        control.handlers.feature.deactivate = function() {
            t.ok(true,
                 "control.deactivate calls deactivate on feature handler");
        }
        control.deactivate();
    }
    
    function test_Control_DragFeature_over(t) {
        t.plan(5);
        var log = [];
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer, {
            onEnter: function(f) { log.push({feature: f}); }
        });
        map.addControl(control);
        
        control.activate();
        t.ok(!control.handlers.drag.active,
             "drag handler is not active before over a feature");
        
        // simulate a mouseover on a feature
        var feature = new OpenLayers.Feature.Vector();
        feature.layer = layer;
        layer.getFeatureFromEvent = function(evt) {
            return feature;
        }
        map.events.triggerEvent("mousemove", {type: "mousemove"});
        
        t.eq(control.feature.id, feature.id,
             "control gets the proper feature from the feature handler");
        t.ok(control.handlers.drag.active,
             "drag handler activated when over a feature");
        t.eq(log.length, 1,
             "onEnter called exactly once");
        t.eq(log[0].feature.id, feature.id,
             "onEnter called with expected feature");
    }

    function test_Control_DragFeature_over_touch(t) {
        t.plan(7);
        var log = [];
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer, {
            onEnter: function(f) { log.push({feature: f}); }
        });
        map.addControl(control);

        control.activate();
        t.ok(!control.handlers.drag.active,
             "drag handler is not active before touch on a feature");

        // simulate a touch on a feature
        var feature = new OpenLayers.Feature.Vector();
        feature.layer = layer;
        layer.getFeatureFromEvent = function(evt) {
            return feature;
        }
        map.events.triggerEvent("touchstart", {type: "touchstart", touches: ['foo']});

        t.eq(control.feature.id, feature.id,
             "control gets the proper feature from the feature handler");
        t.ok(control.handlers.drag.active,
             "drag handler activated when touch on a feature");
        t.ok(control.handlers.drag.started, "drag handler has started");
        t.ok(!control.handlers.drag.stopDown, "drag handler is not stopping down");
        t.eq(log.length, 1,
             "onEnter called exactly once");
        t.eq(log[0].feature.id, feature.id,
             "onEnter called with expected feature");
    }

    function test_Control_DragFeature_down(t) {
        t.plan(3);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer);
        map.addControl(control);
        
        control.activate();
        
        // simulate a mouseover on a feature
        var feature = new OpenLayers.Feature.Vector();
        feature.layer = layer;
        layer.getFeatureFromEvent = function(evt) {
            return feature;
        }
        map.events.triggerEvent("mousemove", {type: "mousemove"});

        // simulate a mousedown on a feature
        control.onStart = function(feat, pixel) {
            t.eq(feat.id, feature.id, "onStart called with the correct feature");
            t.eq(pixel, "bar", "onStart called with the correct pixel");
        }
        map.events.triggerEvent("mousedown", {xy: "bar", which: 1, type: "mousemove"});

        t.eq(control.lastPixel, "bar",
             "mousedown sets the lastPixel correctly");
    }

    function test_Control_DragFeature_move(t) {
        t.plan(3);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer);
        map.addControl(control);
        map.getResolution = function() {
            return 2;
        }

        control.activate();

        // mock up a feature - for the sole purpose of testing mousemove
        var uid = Math.random();
        layer.getFeatureFromEvent = function() {
            var geom = new OpenLayers.Geometry.Point(Math.random(),
                                                     Math.random());
            geom.move = function(x, y) {
                t.eq(x, 2, "move called with dx * res");
                t.eq(y, -4, "move called with -dy * res");
            };
            var feature = new OpenLayers.Feature.Vector(geom);
            feature.layer = layer;
            feature.uid = uid;
            return feature;
        };
        layer.drawFeature = function(feature) {
            t.eq(feature.uid, uid,
                 "layer.drawFeature called with correct feature");
        };

        // simulate a mouseover on a feature
        map.events.triggerEvent("mousemove", {type: "mousemove"});

        // simulate a mousedown on a feature
        var down = new OpenLayers.Pixel(0, 0);
        map.events.triggerEvent("mousedown", {xy: down, which: 1, type: "mousemove"});

        // simulate a mousemove on a feature
        var move = new OpenLayers.Pixel(1, 2);
        map.events.triggerEvent("mousemove", {xy: move, which: 1, type: "mousemove"});
        
    }

    function test_Control_DragFeature_up(t) {
        t.plan(6);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer);
        map.addControl(control);

        control.activate();

        // simulate a mouseover on a feature
        var feature = new OpenLayers.Feature.Vector();
        feature.layer = layer;
        layer.getFeatureFromEvent = function(evt) {
            return feature;
        }
        map.events.triggerEvent("mousemove", {type: "mousemove"});
        t.eq(control.over, true,
            "mouseover on a feature sets the over property to true");
        t.ok(OpenLayers.Element.hasClass(control.map.viewPortDiv, "olControlDragFeatureOver"),
            "mouseover on a feature adds class name to map container");
        t.eq(control.handlers.drag.active, true,
            "mouseover on a feature activates drag handler");

        // simulate a mouse-up on the map, with the mouse still
        // over the dragged feature
        control.handlers.drag.started = true;
        map.events.triggerEvent("mouseup", {type: "mouseup"});
        t.eq(control.handlers.drag.active, true,
            "mouseup while still over dragged feature does not deactivate drag handler");

        // simulate a mouse-up on the map, with the mouse out of
        // the dragged feature
        control.handlers.drag.started = true;
        control.over = false;
        map.events.triggerEvent("mouseup", {type: "mouseup"});
        t.eq(control.handlers.drag.active, false,
            "mouseup deactivates drag handler");

        control.deactivate();
        t.ok(!OpenLayers.Element.hasClass(control.map.viewPortDiv, "olControlDragFeatureOver"),
            "deactivate removes class name from map container");
    }
    
    function test_Control_DragFeature_done(t) {
        t.plan(2);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer);
        map.addControl(control);

        control.activate();


        // simulate a mouseover on a feature
        var feature = new OpenLayers.Feature.Vector();
        feature.layer = layer;
        layer.getFeatureFromEvent = function() {
            return feature;
        };
        map.events.triggerEvent("mousemove", {type: "mousemove"});
        t.eq(control.feature.id, feature.id,
             "feature is set on mouse over");
        control.doneDragging();
        t.eq(control.feature.id, feature.id, 
          "feature sticks around after doneDragging is called.");
        
    }

    function test_Control_DragFeature_out(t) {
        t.plan(4);
        var log = [];
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer, {
            onLeave: function(f) { log.push({feature: f}); }
        });
        map.addControl(control);

        control.activate();


        // simulate a mouseover on a feature
        var feature = new OpenLayers.Feature.Vector();
        feature.layer = layer;
        layer.getFeatureFromEvent = function() {
            return feature;
        };
        map.events.triggerEvent("mousemove", {type: "mousemove"});
        t.eq(control.feature.id, feature.id,
             "feature is set on mouse over");
        
        // simulate a mouseout on a feature
        layer.getFeatureFromEvent = function() {
            return null;
        };
        map.events.triggerEvent("mousemove", {type: "mousemove"});
        t.ok(control.feature == null,
             "feature is set to null on mouse out");
        t.eq(log.length, 1,
             "onLeave called exactly once");
        t.eq(log[0].feature.id, feature.id,
             "onLeave called with expected feature");
    }

    function test_Control_DragFeature_out_touch(t) {
        t.plan(5);
        var log = [];
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.DragFeature(layer, {
            onLeave: function(f) { log.push({feature: f}); }
        });
        map.addControl(control);

        control.activate();

        // simulate a touch on a feature
        var feature = new OpenLayers.Feature.Vector();
        feature.layer = layer;
        layer.getFeatureFromEvent = function() {
            return feature;
        };
        map.events.triggerEvent("touchstart", {type: "touchstart", touches: ['foo']});
        t.eq(control.feature.id, feature.id,
             "feature is set on mouse over");

        // simulate a touch outside the feature
        layer.getFeatureFromEvent = function() {
            return null;
        };
        map.events.triggerEvent("touchstart", {type: "touchstart", touches: ['foo']});
        t.ok(control.feature == null,
             "feature is set to null on mouse out");
        t.ok(control.handlers.drag.stopDown,
             "drag handler is stopping down again");
        t.eq(log.length, 1,
             "onLeave called exactly once");
        t.eq(log[0].feature.id, feature.id,
             "onLeave called with expected feature");
    }

    function test_Control_DragFeature_click(t) {
        t.plan(1);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var feature = new OpenLayers.Feature.Vector();
        feature.layer = layer;
        var control = new OpenLayers.Control.DragFeature(layer);
        map.addControl(control);

        control.activate();

        control.overFeature(feature);
        control.handlers.feature.evt = {which: 1};
        control.clickFeature(feature);
        t.eq(control.handlers.drag.started, false, "click after over does not start drag handler");
    }

    </script>
</head>
<body>
    <div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>