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

    function test_initialize(t) {
        t.plan(1);
        var control = new OpenLayers.Control.DrawFeature("foo", function() {});
        t.ok(control instanceof OpenLayers.Control.DrawFeature,
             "constructor returns an instance");
    }
    
    function test_multi(t) {
        t.plan(4);

        var layer = new OpenLayers.Layer.Vector();
        var control;
        
        // multi false by default
        control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Polygon
        );
        t.ok(!control.multi, "control.multi false by default");
        t.ok(!control.handler.multi, "handler.multi false by default");
        
        // set on handler
        control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Polygon, {multi: true}
        );
        t.ok(control.handler.multi, "handler.multi set from control options");
        
        // respect handlerOptions
        control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Polygon,
            {multi: true, handlerOptions: {multi: false}}
        );
        t.ok(!control.handler.multi, "handlerOptions.multi respected");

    }

    function test_rendererOptions(t) {
        t.plan(2);
        
        var map = new OpenLayers.Map("map");
        var renderers = ["Canvas", "VML"];

        var layer = new OpenLayers.Layer.Vector(null, {
            renderers: renderers,
            rendererOptions: {zIndexing: true},
            isBaseLayer: true
        });
        map.addLayer(layer);
        
        var control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Polygon, {autoActivate: true}
        );
        map.addControl(control);

        var sketchLayer = control.handler.layer;

        t.eq(sketchLayer.renderers, renderers, "Preferred renderers");
        t.eq(sketchLayer.rendererOptions.zIndexing, true, "renderer options");
        
        map.destroy();

    }
    
    function test_drawFeature(t) {
        t.plan(3);
        var layer = new OpenLayers.Layer.Vector();
        var control = new OpenLayers.Control.DrawFeature(layer, function() {});
        var geom = {};
        
        layer.addFeatures = function(features) {
            t.ok(features[0].geometry == geom, "layer.addFeatures called");
            t.eq(features[0].state, OpenLayers.State.INSERT, "layer state set");
        };
        function handlefeatureadded(event) {
            t.ok(event.feature.geometry == geom, "featureadded triggered");
        }
        control.events.on({"featureadded": handlefeatureadded});
        control.drawFeature(geom);
        control.events.un({"featureadded": handlefeatureadded});
        
    }
    
    function test_sketch_events(t) {
        t.plan(11);
        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
        });
        var control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Path, {
                handlerOptions: {persist: true}
            }
        );
        map.addLayer(layer);
        map.addControl(control);
        map.zoomToMaxExtent();
        
        var log;
        layer.events.on({
            sketchstarted: function(event) {
                log['sketchstarted'] = event;
            },
            sketchmodified: function(event) {
                log['sketchmodified'] = event;
            },
            sketchcomplete: function(event) {
                log['sketchcomplete'] = event;
            }
        });
        
        // mock up draw/modify of a point
        log = {};
        control.activate();
        t.eq(log, {}, "[activate] no event triggered");

        log = {};
        map.events.triggerEvent("mousemove", {xy: new OpenLayers.Pixel(0, 0)});
        t.eq(log.sketchstarted.type, "sketchstarted", "[mousemove] sketchstarted triggered");
        t.geom_eq(log.sketchstarted.vertex, new OpenLayers.Geometry.Point(-200, 125), "[mousemove] correct vertex");
        t.eq(log.sketchmodified.type, "sketchmodified", "[mousemove] sketchmodified triggered");
        t.geom_eq(log.sketchmodified.vertex, new OpenLayers.Geometry.Point(-200, 125), "[mousemove] correct vertex");

        map.events.triggerEvent("mousedown", {xy: new OpenLayers.Pixel(0, 0)});

        log = {};
        map.events.triggerEvent("mouseup", {xy: new OpenLayers.Pixel(0, 0)});
        t.eq(log.sketchmodified.type, "sketchmodified", "[mouseup] sketchmodified triggered");
        t.geom_eq(log.sketchmodified.vertex, new OpenLayers.Geometry.Point(-200, 125), "[mouseup] correct vertex");

        log = {};
        map.events.triggerEvent("mousemove", {xy: new OpenLayers.Pixel(10, 10)});
        t.eq(log.sketchmodified.type, "sketchmodified", "[mousemove] sketchmodified triggered");
        t.geom_eq(log.sketchmodified.vertex, new OpenLayers.Geometry.Point(-190, 115), "[mousemove] correct vertex");

        log = {};
        map.events.triggerEvent("dblclick", {xy: new OpenLayers.Pixel(10, 10)});
        t.eq(log.sketchcomplete.type, "sketchcomplete", "[dblclick] sketchcomplete triggered");
        t.geom_eq(log.sketchcomplete.feature.geometry,
                  new OpenLayers.Geometry.LineString([
                      new OpenLayers.Geometry.Point(-200, 125),
                      new OpenLayers.Geometry.Point(-190, 115)
                  ]),
                  "[dblclick] correct geometry");

        map.destroy();
    }

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