]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Control/Split.html
reset openlayers directory to its original state at the end of build.sh
[syp.git] / openlayers / tests / Control / Split.html
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5
6     function test_initialize(t) {
7         
8         t.plan(4);
9
10         var layer = new OpenLayers.Layer.Vector();
11         var control;
12         
13         // construct with nothing
14         control = new OpenLayers.Control.Split();
15         t.ok(control instanceof OpenLayers.Control, "instanceof OpenLayers.Control");
16         t.ok(control instanceof OpenLayers.Control, "instanceof OpenLayers.Control.Split")
17         control.destroy();
18         
19         // construct with a single target layer
20         control = new OpenLayers.Control.Split({
21             layer: layer
22         });        
23         t.ok(control.layer === layer, "target layer properly set");
24         control.destroy();
25         
26         // construct with same target and source
27         control = new OpenLayers.Control.Split({
28             layer: layer,
29             source: layer
30         });        
31         t.ok(control.source === layer, "source layer properly set");
32         control.destroy();
33     }
34     
35     function test_setSource(t) {
36         t.plan(5);
37         
38         var layer1 = new OpenLayers.Layer.Vector();
39         var layer2 = new OpenLayers.Layer.Vector();
40         
41         var control = new OpenLayers.Control.Split({layer: layer1});
42
43         var map = new OpenLayers.Map("map");
44         map.addLayers([layer1, layer2]);
45         map.addControl(control);
46         control.activate();
47         
48         // confirm sketch hander created
49         t.ok(control.handler, "sketch handler created");
50         t.eq(control.handler.active, true, "sketch handler active");
51         
52         control.setSource(layer1);
53         t.ok(control.source === layer1, "layer1 properly set");
54         t.ok(!control.handler, "no more sketch handler");
55         
56         // activate and switch to new source layer
57         control.setSource(layer2);
58         t.ok(control.source === layer2, "layer2 properly set");
59         
60         map.destroy();
61         
62     }
63     
64     function test_activate(t) {
65         t.plan(8);
66         
67         var layer = new OpenLayers.Layer.Vector();
68         var control = new OpenLayers.Control.Split({layer: layer});
69         var map = new OpenLayers.Map("map");
70         map.addLayer(layer);
71         map.addControl(control);
72         
73         // test activation with no source layer
74         control.activate();
75         t.eq(control.active, true, "control is active");
76         t.ok(control.handler instanceof OpenLayers.Handler.Path, "line sketch handler created");
77         t.ok(control.handler.callbacks.done, "done callback set on sketch handler");
78         t.eq(control.handler.active, true, "sketch handler is active");
79         
80         // change the source layer - this should call activate again
81         control.setSource(layer);
82         
83         t.eq(control.active, true, "control is still active");
84         t.ok(control.source === layer, "source layer set");
85         t.ok(layer.events.listeners.sketchcomplete, "sketchcomplete listener registered");
86         t.ok(layer.events.listeners.afterfeaturemodified, "afterfeaturemodified listener registered");
87         
88         map.destroy();
89         
90     }
91     
92     function test_deactivate(t) {
93         
94         t.plan(7);
95         
96         var layer = new OpenLayers.Layer.Vector();
97         var control = new OpenLayers.Control.Split({layer: layer});
98         var map = new OpenLayers.Map("map");
99         map.addLayer(layer);
100         map.addControl(control);
101         
102         // activate and check sketch handler
103         control.activate();
104         t.ok(control.handler, "sketch handler present");
105         t.eq(control.handler.active, true, "sketch handler active");
106         
107         // deactivate and check sketch handler
108         control.deactivate();
109         t.eq(control.handler.active, false, "sketch handler deactivated");
110         
111         // set a source layer
112         control.setSource(layer);
113
114         // activate and check that listeners are registered
115         control.activate();
116         t.ok(layer.events.listeners.sketchcomplete, "sketchcomplete listener registered");
117         t.ok(layer.events.listeners.afterfeaturemodified, "afterfeaturemodified listener registered");
118
119         // deactivate and confirm no draw related events
120         control.deactivate();
121         t.eq(layer.events.listeners.sketchcomplete.length, 0, "no sketchcomplete listeners");
122         t.eq(layer.events.listeners.afterfeaturemodified.length, 0, "no afterfeaturemodified listeners");
123         
124         map.destroy();
125     }
126     
127     function test_isEligible(t) {
128         
129         t.plan(9);
130         
131         var control = new OpenLayers.Control.Split();
132         var geometry = OpenLayers.Geometry.fromWKT("LINESTRING(0 1, 1 2)");
133         var feature = new OpenLayers.Feature.Vector(
134             geometry,
135             {foo: "bar"}
136         );
137         
138         t.eq(control.isEligible(feature), true, "plain old feature is eligible");
139         
140         feature.state = OpenLayers.State.DELETE;
141         t.eq(control.isEligible(feature), false, "feature slated for deletion is not eligible");
142         delete feature.state;
143         t.eq(control.isEligible(feature), true, "feature with no state is eligible");
144         
145         feature.geometry = new OpenLayers.Geometry.Point(1, 1);
146         t.eq(control.isEligible(feature), false, "feature with point geometry is not eligible");
147         feature.geometry = new OpenLayers.Geometry.MultiLineString([geometry]);
148         t.eq(control.isEligible(feature), true, "feature with multilinestring geometry is eligible");
149         
150         control.feature = feature;
151         t.eq(control.isEligible(feature), false, "source feature is not eligible as target");
152         control.feature = new OpenLayers.Feature.Vector();
153         t.eq(control.isEligible(feature), true, "feature is eligible if different than source feature");
154         
155         control.targetFilter = new OpenLayers.Filter.Comparison({
156             type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
157             property: "foo",
158             value: "bar"
159         });
160         t.eq(control.isEligible(feature), false, "feature is not eligible unless it matches filter");
161         control.targetFilter.value = "baz";
162         t.eq(control.isEligible(feature), true, "feature is eligible if it matches filter");
163         
164         control.destroy();
165         
166     }
167     
168     function test_considerSplit(t) {
169
170         var layer = new OpenLayers.Layer.Vector();
171         
172         var wkt = OpenLayers.Geometry.fromWKT;
173         var geoms = {
174             abc: wkt("LINESTRING(0 0, 2 2)"),
175             ab: wkt("LINESTRING(0 0, 1 1)"),
176             bc: wkt("LINESTRING(1 1, 2 2)"),
177             dbe: wkt("LINESTRING(2 0, 0 2)"),
178             db: wkt("LINESTRING(2 0, 1 1)"),
179             be: wkt("LINESTRING(1 1, 0 2)")
180         };
181         
182         var Feature = OpenLayers.Feature.Vector;
183         var feats = {
184             abc: new Feature(geoms.abc),
185             ab: new Feature(geoms.ab),
186             bc: new Feature(geoms.bc),
187             dbe: new Feature(geoms.dbe),
188             db: new Feature(geoms.db),
189             be: new Feature(geoms.be)
190         };
191         
192         function feature(id, options) {
193             var f = OpenLayers.Util.extend(feats[id].clone(), options);
194             // for testing, we want to check when features are destroyed
195             f.destroy = function() {
196                 f.state = "destroyed";
197             }
198             return f;
199         }
200         var DELETE = OpenLayers.State.DELETE;
201         var INSERT = OpenLayers.State.INSERT;
202         var UPDATE = OpenLayers.State.UPDATE;
203         
204         var cases = [{
205             targets: [
206                 feature("abc")
207             ],
208             source: feature("dbe"),
209             splits: [{
210                 original: feature("abc", {state: "destroyed"}),
211                 features: [feature("ab", {state: INSERT}), feature("bc", {state: INSERT})]
212             }, {
213                 original: feature("dbe", {state: "destroyed"}),
214                 features: [feature("db", {state: INSERT}), feature("be", {state: INSERT})]
215             }]
216         }, {
217             options: {deferDelete: true},
218             targets: [
219                 feature("abc", {state: INSERT})
220             ],
221             source: feature("dbe"),
222             splits: [{
223                 original: feature("abc", {state: "destroyed"}),
224                 features: [feature("ab", {state: INSERT}), feature("bc", {state: INSERT})]
225             }, {
226                 original: feature("dbe", {state: DELETE}),
227                 features: [feature("db", {state: INSERT}), feature("be", {state: INSERT})]
228             }]
229         }, {
230             options: {deferDelete: true},
231             targets: [
232                 feature("abc", {state: UPDATE})
233             ],
234             source: feature("dbe", {state: INSERT}),
235             splits: [{
236                 original: feature("abc", {state: DELETE}),
237                 features: [feature("ab", {state: INSERT}), feature("bc", {state: INSERT})]
238             }, {
239                 original: feature("dbe", {state: "destroyed"}),
240                 features: [feature("db", {state: INSERT}), feature("be", {state: INSERT})]
241             }]
242         }];
243         
244         var count = 0;
245         var c, control, options, log, event, split;
246         for(var i=0; i<cases.length; ++i) {
247             c = cases[i];
248             ++count; // test number of splits
249             for(var j=0; j<c.splits.length; ++j) {
250                 split = c.splits[j];
251                 ++count; // test original state
252                 ++count; // test original geometry
253                 ++count; // test number of parts
254                 for(var k=0; k<split.features.length; ++k) {
255                     ++count; // test part state
256                     ++count; // test part geometry
257                 }
258             }
259         }
260         t.plan(count);
261
262         for(var i=0; i<cases.length; ++i) {
263             c = cases[i];
264             log = {events: []};
265             options = OpenLayers.Util.extend({layer: layer, source: layer}, c.options);
266             control = new OpenLayers.Control.Split(options);
267             control.events.on({
268                 split: function(e) {
269                     log.events.push(e);
270                 }
271             });
272             layer.features = c.targets;
273             control.considerSplit(c.source);
274             t.eq(log.events.length, c.splits.length, "case " + i + ": correct number of split events");
275             for(var j=0; j<log.events.length; ++j) {
276                 event = log.events[j];
277                 split = c.splits[j];
278                 t.eq(event.original.state, split.original.state, "case " + i + " split " + j + ": correct original state");
279                 t.geom_eq(event.original.geometry, split.original.geometry, "case " + i + " split " + j + ": correct original geometry");
280                 t.eq(event.features.length, split.features.length, "case " + i + " split " + j + ": correct number of parts");
281                 for(var k=0; k<split.features.length; ++k) {
282                     t.eq(event.features[k].state, split.features[k].state, "case " + i + " split " + j + " feature " + k + ": correct state");
283                     t.geom_eq(event.features[k].geometry, split.features[k].geometry, "case " + i + " split " + j + " feature " + k + ": correct geometry");
284                 }
285             }
286             control.destroy();
287         }
288         
289     }
290
291   </script>
292 </head>
293 <body>
294     <div id="map" style="width: 100px; height: 100px;"></div>
295 </body>
296 </html>