summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Handler/RegularPolygon.html
blob: ee43dc77fa1ca5e4503a2dc2d8fba860b450e36c (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
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    function test_Handler_RegularPolygon_constructor(t) {
        t.plan(3);
        var control = new OpenLayers.Control();
        control.id = Math.random();
        var callbacks = {foo: "bar"};
        var options = {bar: "foo"};
        
        var oldInit = OpenLayers.Handler.prototype.initialize;
        
        OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
            t.eq(con.id, control.id,
                 "constructor calls parent with the correct control");
            t.eq(call, callbacks,
                 "constructor calls parent with the correct callbacks");
            t.eq(opt, options,
                 "regular polygon constructor calls parent with the correct options");
        }
        var handler = new OpenLayers.Handler.RegularPolygon(control, callbacks, options);

        OpenLayers.Handler.prototype.initialize = oldInit;
    }

    function test_Handler_RegularPolygon_activation(t) {
        t.plan(3);
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.RegularPolygon(control);
        handler.active = true;
        var activated = handler.activate();
        t.ok(!activated,
             "activate returns false if the handler was already active");
        handler.active = false;
        activated = handler.activate();
        t.ok(activated,
             "activate returns true if the handler was not already active");
        activated = handler.deactivate();
        t.ok(activated,
             "deactivate returns true if the handler was active already");
        map.destroy();     
    }

    function test_Handler_RegularPolygon_deactivation(t) {
        t.plan(1);
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
             
        var handler = new OpenLayers.Handler.RegularPolygon(control, {foo: 'bar'});
        handler.activate();
        handler.layer.destroy();
        handler.deactivate();
        t.eq(handler.layer, null,
             "deactivate doesn't throw an error if layer was" +
             " previously destroyed");
        map.destroy();     
    }

    function test_Handler_RegularPolygon_four_corners(t) {
        t.plan(7);
        var map = new OpenLayers.Map('map');
        map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
        map.zoomToMaxExtent();
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.RegularPolygon(control, {});
        var activated = handler.activate();
        var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
        handler.down(evt);
        var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1};
        handler.move(evt);
        t.eq(handler.feature.geometry.getBounds().toBBOX(),
             "-35.15625,-35.15625,35.15625,35.15625",
             "correct bounds after move");
        t.eq(handler.feature.geometry.components[0].components.length, 5,
             "geometry has 5 components");
        t.eq(handler.feature.geometry.CLASS_NAME,
             "OpenLayers.Geometry.Polygon",
             "geometry is a polygon");
        t.eq(handler.radius, 25*1.40625, "feature radius as set on handler");
        var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
        handler.move(evt);
        t.eq(handler.feature.geometry.getBounds().toBBOX(),
             "-35.15625,-35.15625,35.15625,35.15625",
             "correct bounds after move with a fixed radius");
        handler.cancel();
        handler.setOptions({radius:2 / Math.sqrt(2)});
        var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
        handler.down(evt);
        
        t.eq(handler.feature.geometry.getBounds().toBBOX(),
             "-1,-1,1,1",
             "bounds with manual radius setting");
        var evt = {xy: new OpenLayers.Pixel(175, 90), which: 1};
        handler.move(evt);
        t.eq(handler.feature.geometry.getBounds().toBBOX(),
             "34.15625,-22.09375,36.15625,-20.09375",
             "bounds with manual radius setting and mousemove");
        map.destroy();     
    }

    function test_Handler_RegularPolygon_circle(t) {
        t.plan(7);
        var map = new OpenLayers.Map('map');
        map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
        map.zoomToMaxExtent();
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.RegularPolygon(control, {}, {'sides':40});
        var activated = handler.activate();
        var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
        handler.down(evt);
        var evt = {xy: new OpenLayers.Pixel(175, 75), which: 1};
        handler.move(evt);
        t.eq(handler.feature.geometry.getBounds().toBBOX(),
             "-35.15625,-35.15625,35.15625,35.15625",
             "correct bounds after move");
        t.eq(handler.feature.geometry.components[0].components.length, 41,
             "geometry has correct numbre of components");
        t.eq(handler.feature.geometry.CLASS_NAME,
             "OpenLayers.Geometry.Polygon",
             "geometry is a polygon");
        t.eq(handler.radius, 25*1.40625, "feature radius as set on handler");
        var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
        handler.move(evt);
        t.eq(handler.feature.geometry.getBounds().toBBOX(),
             "-35.823348,-35.823348,35.823348,35.823348",
             "correct bounds after move with fixed radius");
        handler.cancel();
        handler.setOptions({radius:1});
        var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
        handler.down(evt);
        
        t.eq(handler.feature.geometry.getBounds().toBBOX(),
             "-0.996917,-0.996917,0.996917,0.996917",
             "bounds with manual radius setting");
        var evt = {xy: new OpenLayers.Pixel(175, 80), which: 1};
        handler.move(evt);
        t.eq(handler.feature.geometry.getBounds().toBBOX(),
             "34.159333,-8.028167,36.153167,-6.034333",
             "bounds with manual radius setting and mousemove");
        map.destroy();     
    }

    function test_Handler_RegularPolygon_irregular(t) {
        t.plan(4);
        var map = {
            getResolution: function() {
                return 1;
            }
        };
        var layer = {
            addFeatures: function() {},
            drawFeature: function(feature, style) {
                var ring = feature.geometry.components[0];
                t.eq(ring.components[0].x, 20, "correct right");
                t.eq(ring.components[0].y, 10, "correct bottom");
                t.eq(ring.components[2].x, 10, "correct left");
                t.eq(ring.components[2].y, 15, "correct top");
            },
            getLonLatFromViewPortPx: function(px) {
                return {lon: px.x, lat: px.y};
            }
        };
        var control = {};
        var options = {
            sides: 4,
            irregular: true,
            layer: layer,
            map: map
        };
        var handler = new OpenLayers.Handler.RegularPolygon(
            control, null, options
        );
        handler.origin = new OpenLayers.Geometry.Point(10, 10);
        handler.feature = new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Polygon(
                [new OpenLayers.Geometry.LinearRing()]
            )
        );
        // should result in a 10 x 5 rectangle
        handler.move({xy: {x: 20, y: 15}});
    }

    function test_callbacks(t) {
        t.plan(1);

        // setup
        var map = new OpenLayers.Map("map");

        var control = {"map": map};

        var done = function(geom) {
            t.ok(true,
                 "done callback called even if no move between down and up");
        };

        var handler = new OpenLayers.Handler.RegularPolygon(
            control, {"done": done});
        handler.activate();
        
        var xy = new OpenLayers.Pixel(Math.random(), Math.random());

        var isLeftClick = OpenLayers.Event.isLeftClick;
        OpenLayers.Event.isLeftClick = function() { return true; };
        handler.layer = {
            renderer: {
                clear: OpenLayers.Function.Void
            },
            addFeatures: OpenLayers.Function.Void,
            drawFeature: OpenLayers.Function.Void,
            destroyFeatures: OpenLayers.Function.Void,
            getLonLatFromViewPortPx: function() {
                return xy;
            }
        };

        // test
        map.events.triggerEvent("mousedown", {"xy": xy});
        map.events.triggerEvent("mouseup", {"xy": xy});

        // tear down
        OpenLayers.Event.isLeftClick = isLeftClick;
    }

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