summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Control/Measure.html
blob: ee6d192b5ae9144c00faf6bff3c5326fa4dcdee3 (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
384
385
386
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">

    function test_initialize(t) {
        
        t.plan(1);
        
        var map = new OpenLayers.Map("map");
        var control = new OpenLayers.Control.Measure(
            OpenLayers.Handler.Path, {persist: true}
        );
        map.addControl(control);
        
        t.eq(control.persist, true, "passing persist to constructor sets persist on handler");
        
        map.destroy();
        
    }
    
    function test_cancel(t) {
        
        t.plan(4);
        
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer(null, {
            isBaseLayer: true
        });
        map.addLayer(layer);
        map.zoomToMaxExtent();
        
        var control = new OpenLayers.Control.Measure(
            OpenLayers.Handler.Path, {persist: true}
        );
        map.addControl(control);
        
        control.activate();

        try {
            control.cancel();
            t.ok(true, "calling cancel before drawing works");
        } catch(err) {
            t.fail("calling cancel before drawing causes trouble: " + err);
        }
        t.eq(control.active, true, "control remains active after cancel");
        
        // create a simple measurement
        function trigger(type, x, y) {
            map.events.triggerEvent(type, {
                xy: new OpenLayers.Pixel(x, y)
            })
        };

        trigger("mousemove", 0, 0);
        // keep a reference to the line being drawn
        var line = control.handler.line;
        trigger("mousedown", 0, 0);
        trigger("mouseup", 0, 0);
        trigger("mousemove", 10, 10);
        trigger("mousedown", 10, 10);
        trigger("mouseup", 10, 10);
        trigger("dblclick", 10, 10);

        // the geometry is finalized, we first confirm that it is persisted
        t.ok(line.layer === control.handler.layer, "feature persists");
        
        // cancel and see that sketch is gone
        control.cancel();
        t.eq(line.layer, null, "feature is gone after cancel");

        map.destroy();
    }

    // test for <http://trac.openlayers.org/ticket/2691>
    function test_partial(t) {        

        t.plan(28);        
        
        var map = new OpenLayers.Map({
            div: "map",
            units: "m",
            resolutions: [1],
            layers: [
                new OpenLayers.Layer(null, {
                    isBaseLayer: true
                })
            ],
            center: new OpenLayers.LonLat(0, 0)
        });

        var log = [];
        var control = new OpenLayers.Control.Measure(
            OpenLayers.Handler.Path, {persist: true, 
                eventListeners: {
                    measurepartial: function(evt) {
                        log.push(evt);
                    },
                    measure: function(evt){
                        log.push(evt);
                    }
                },
                handlerOptions: {
                    pixelTolerance: 0,
                    dblclickTolerance: 0
                }
            }
        );
        map.addControl(control);
        control.activate();
        
        
        // convenience function to trigger mouse events
        function trigger(type, x, y) {
            map.events.triggerEvent(type, {
                xy: new OpenLayers.Pixel(x, y)
            })
        };
        
        // delay in seconds
        var delay = control.partialDelay / 1000;
        
        // establish first point
        trigger("mousemove", 0, 0);
        trigger("mousedown", 0, 0);
        trigger("mouseup", 0, 0);

        
        // a) move 10 pixels and click
        trigger("mousemove", 0, 10);
        trigger("mousedown", 0, 10);
        trigger("mouseup", 0, 10);
        
        // confirm measurepartial is not fired before delay
        t.eq(log.length, 0, "a) no event fired yet")

        t.delay_call( 
            // wait for delay then confirm event was logged
            delay, function() {
                t.eq(log.length, 1, "a) event logged")
                t.eq(log[0] && log[0].type, "measurepartial", "a) event logged");
                t.eq(log[0] && log[0].measure, 10, "a) correct measure");
                
                // b) move 10 pixels and click
                trigger("mousemove", 0, 20);
                trigger("mousedown", 0, 20);
                trigger("mouseup", 0, 20);
                
                // confirm measurepartial is not fired before delay
                t.eq(log.length, 1, "b) no event fired yet")
                
            },
            delay, function() {
                t.eq(log.length, 2, "b) event logged");
                t.eq(log[1] && log[1].type, "measurepartial", "b) correct type");
                t.eq(log[1] && log[1].measure, 20, "b) correct measure");

                // c) move 10 pixels and click
                trigger("mousemove", 0, 30);
                trigger("mousedown", 0, 30);
                trigger("mouseup", 0, 30);
            },
            // wait for half delay and confirm event not logged
            delay / 2, function() {
                // confirm measurepartial is not fired before delay
                t.eq(log.length, 2, "c) no event fired yet")
            },
            // wait for rest of delay and confirm event logged
            delay / 2, function() {
                t.eq(log.length, 3, "c) event logged");
                t.eq(log[2] && log[2].type, "measurepartial", "c) correct type");
                t.eq(log[2] && log[2].measure, 30, "c) correct measure");
                
                // d) move 10 pixels and click
                trigger("mousemove", 0, 40);
                trigger("mousedown", 0, 40);
                trigger("mouseup", 0, 40);

                // confirm measurepartial is not fired before delay
                t.eq(log.length, 3, "d) no event fired yet")
                
                // e) double click to finish
                trigger("dblclick", 0, 40);

                t.eq(log.length, 4, "e) event logged");
                t.eq(log[3] && log[3].type, "measure", "e) correct type");
                t.eq(log[3] && log[3].measure, 40, "e) correct measure");                
            },
            // wait for rest of delay and confirm no measurepartial logged
            delay, function() {
                // confirm measurepartial is not fired after dblclick
                t.eq(log.length, 4, "e) no additional event fired");
                
                // change to freehand mode and confirm synchronous event dispatch
                control.handler.freehand = true;
                // clear log
                log = [];
                
                // f) establish first freehand point
                trigger("mousemove", 0, 0);
                trigger("mousedown", 0, 0);
                t.eq(log.length, 0, "f) no event fired yet")
                
                // g) move 10 pixels
                trigger("mousemove", 10, 0);

                t.eq(log.length, 1, "g) event logged");
                t.eq(log[0] && log[0].type, "measurepartial", "g) correct type");
                t.eq(log[0] && log[0].measure, 10, "g) correct measure");
                
                // h) move 10 pixels
                trigger("mousemove", 20, 0);

                t.eq(log.length, 2, "h) event logged");
                t.eq(log[1] && log[1].type, "measurepartial", "h) correct type");
                t.eq(log[1] && log[1].measure, 20, "h) correct measure");

                // i) mouse up to finish
                trigger("mouseup", 20, 0);

                t.eq(log.length, 3, "i) event logged");
                t.eq(log[2] && log[2].type, "measure", "i) correct type");
                t.eq(log[2] && log[2].measure, 20, "i) correct measure");

                // j) clean up
                log = [];
                map.destroy();
            },
            // wait for delay and confirm event not logged
            delay, function() {
                t.eq(log.length, 0, "j) no event fired after destroy");
            }
        );
        
    }

    function test_immediate(t) {
        t.plan(32);
        
        var map = new OpenLayers.Map({
            div: "map",
            units: "m",
            resolutions: [1],
            layers: [
                new OpenLayers.Layer(null, {
                    isBaseLayer: true
                })
            ],
            center: new OpenLayers.LonLat(0, 0)
        });

        var log = [];
        var control = new OpenLayers.Control.Measure(
            OpenLayers.Handler.Path, {
                persist: true,
                immediate: true,
                eventListeners: {
                    measurepartial: function(evt) {
                        log.push(evt);
                    },
                    measure: function(evt){
                        log.push(evt);
                    }
                },
                handlerOptions: {
                    pixelTolerance: 0,
                    dblclickTolerance: 0
                }
            }
        );
        map.addControl(control);
        control.activate();

        // convenience function to trigger mouse events
        function trigger(type, x, y) {
            map.events.triggerEvent(type, {
                xy: new OpenLayers.Pixel(x, y)
            })
        };

        // delay in seconds
        var delay = control.partialDelay / 1000;

        // a) establish first point
        trigger("mousemove", 0, 0);
        trigger("mousedown", 0, 0);
        trigger("mouseup", 0, 0);

        // move 10 pixels
        trigger("mousemove", 0, 10);

        t.eq(log.length, 1, "a) has fired an event");

        t.delay_call(
            delay, function() {
                // confirm measurepartial is fired
                t.eq(log.length, 1, "a) one event logged");
                t.ok(log[0] && log[0].type == "measurepartial", "a) correct type");
                // mousemove within the partialDelay fires no event, so the
                // measure below is the one of the initial point
                t.eq(log[0]?log[0].measure:-1 , 10, "a) correct measure");

                // b) move 10 pixels
                trigger("mousemove", 0, 20);
                // c) move 10 pixels again
                trigger("mousemove", 0, 30);

                // confirm measurepartial is fired 2 times
                t.eq(log.length, 3, "b) event logged");
                t.eq(log[1] && log[1].type, "measurepartial", "b) correct type");
                t.eq(log[1] && log[1].measure, 20, "b) correct measure");
                t.eq(log[2] && log[2].type, "measurepartial", "c) correct type");
                t.eq(log[2] && log[2].measure, 30, "c) correct measure");

                // d) switch immediate measurement off
                control.setImmediate(false);
                t.eq(control.immediate, false, "d) immediate is false");

                // e) move 10 pixels and click
                trigger("mousemove", 0, 40);
                trigger("mousedown", 0, 40);
                trigger("mouseup", 0, 40);
                // confirm measurepartial is not fired before delay
                t.eq(log.length, 3, "e) no event fired yet")
            },
            // wait for delay then confirm event was logged
            delay, function() {
                t.eq(log.length, 4, "e) event logged")
                t.ok(log[3] && log[3].type == "measurepartial", "e) correct type");
                t.ok(log[3] && log[3].measure == 40, "e) correct measure");

                // f) switch immediate measurement on
                control.setImmediate(true);
                t.eq(control.immediate, true, "f) immediate is true");

                // g) move 10 pixels
                trigger("mousemove", 0, 50);
            },
            delay, function() {
                t.eq(log.length, 5, "g) event logged");
                t.ok(log[4] && log[4].type == "measurepartial", "g) correct type");
                t.ok(log[4] && log[4].measure == 50, "g) correct measure");

                // h) move 10 pixels
                trigger("mousemove", 0, 60);

                t.eq(log.length, 6, "h) event logged");
                t.ok(log[5] && log[5].type == "measurepartial", "h) correct type");
                t.ok(log[5] && log[5].measure == 60, "h) correct measure");

                // i) double click to finish
                trigger("mousedown", 0, 60);
                t.eq(log.length, 7, "i) event logged");
                t.eq(log[6] && log[6].type, "measurepartial", "i) correct type");
                t.eq(log[6] && log[6].measure, 60, "i) correct measure");
                trigger("mouseup", 0, 60);
                t.eq(log.length, 7, "i) no event fired yet");
            },
            delay, function() {
                t.eq(log.length, 8, "j) event logged");
                t.eq(log[7] && log[7].type, "measurepartial", "j) correct type");
                t.eq(log[7] && log[7].measure, 60, "j) correct measure");

                trigger("dblclick", 0, 60);
                t.eq(log.length, 9, "k) event logged");
                t.eq(log[8] && log[8].type, "measure", "k) correct type");
                t.eq(log[8] && log[8].measure, 60, "k) correct measure");
                // clear log
                log = [];

                // l) clean up
                map.destroy();
                // wait for delay and confirm event not logged
            },
            delay, function() {
                t.eq(log.length, 0, "l) no event fired after destroy");
            }
        );
    }
    
  </script>
</head>
<body>
    <div id="map" style="width: 512px; height: 256px;"></div>
</body>
</html>