3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_initialize(t) {
10 var layer = new OpenLayers.Layer.Vector();
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")
19 // construct with a single target layer
20 control = new OpenLayers.Control.Split({
23 t.ok(control.layer === layer, "target layer properly set");
26 // construct with same target and source
27 control = new OpenLayers.Control.Split({
31 t.ok(control.source === layer, "source layer properly set");
35 function test_setSource(t) {
38 var layer1 = new OpenLayers.Layer.Vector();
39 var layer2 = new OpenLayers.Layer.Vector();
41 var control = new OpenLayers.Control.Split({layer: layer1});
43 var map = new OpenLayers.Map("map");
44 map.addLayers([layer1, layer2]);
45 map.addControl(control);
48 // confirm sketch hander created
49 t.ok(control.handler, "sketch handler created");
50 t.eq(control.handler.active, true, "sketch handler active");
52 control.setSource(layer1);
53 t.ok(control.source === layer1, "layer1 properly set");
54 t.ok(!control.handler, "no more sketch handler");
56 // activate and switch to new source layer
57 control.setSource(layer2);
58 t.ok(control.source === layer2, "layer2 properly set");
64 function test_activate(t) {
67 var layer = new OpenLayers.Layer.Vector();
68 var control = new OpenLayers.Control.Split({layer: layer});
69 var map = new OpenLayers.Map("map");
71 map.addControl(control);
73 // test activation with no source layer
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");
80 // change the source layer - this should call activate again
81 control.setSource(layer);
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");
92 function test_deactivate(t) {
96 var layer = new OpenLayers.Layer.Vector();
97 var control = new OpenLayers.Control.Split({layer: layer});
98 var map = new OpenLayers.Map("map");
100 map.addControl(control);
102 // activate and check sketch handler
104 t.ok(control.handler, "sketch handler present");
105 t.eq(control.handler.active, true, "sketch handler active");
107 // deactivate and check sketch handler
108 control.deactivate();
109 t.eq(control.handler.active, false, "sketch handler deactivated");
111 // set a source layer
112 control.setSource(layer);
114 // activate and check that listeners are registered
116 t.ok(layer.events.listeners.sketchcomplete, "sketchcomplete listener registered");
117 t.ok(layer.events.listeners.afterfeaturemodified, "afterfeaturemodified listener registered");
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");
127 function test_isEligible(t) {
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(
138 t.eq(control.isEligible(feature), true, "plain old feature is eligible");
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");
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");
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");
155 control.targetFilter = new OpenLayers.Filter.Comparison({
156 type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
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");
168 function test_considerSplit(t) {
170 var layer = new OpenLayers.Layer.Vector();
172 var wkt = OpenLayers.Geometry.fromWKT;
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)")
182 var Feature = OpenLayers.Feature.Vector;
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)
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";
200 var DELETE = OpenLayers.State.DELETE;
201 var INSERT = OpenLayers.State.INSERT;
202 var UPDATE = OpenLayers.State.UPDATE;
208 source: feature("dbe"),
210 original: feature("abc", {state: "destroyed"}),
211 features: [feature("ab", {state: INSERT}), feature("bc", {state: INSERT})]
213 original: feature("dbe", {state: "destroyed"}),
214 features: [feature("db", {state: INSERT}), feature("be", {state: INSERT})]
217 options: {deferDelete: true},
219 feature("abc", {state: INSERT})
221 source: feature("dbe"),
223 original: feature("abc", {state: "destroyed"}),
224 features: [feature("ab", {state: INSERT}), feature("bc", {state: INSERT})]
226 original: feature("dbe", {state: DELETE}),
227 features: [feature("db", {state: INSERT}), feature("be", {state: INSERT})]
230 options: {deferDelete: true},
232 feature("abc", {state: UPDATE})
234 source: feature("dbe", {state: INSERT}),
236 original: feature("abc", {state: DELETE}),
237 features: [feature("ab", {state: INSERT}), feature("bc", {state: INSERT})]
239 original: feature("dbe", {state: "destroyed"}),
240 features: [feature("db", {state: INSERT}), feature("be", {state: INSERT})]
245 var c, control, options, log, event, split;
246 for(var i=0; i<cases.length; ++i) {
248 ++count; // test number of splits
249 for(var j=0; j<c.splits.length; ++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
262 for(var i=0; i<cases.length; ++i) {
265 options = OpenLayers.Util.extend({layer: layer, source: layer}, c.options);
266 control = new OpenLayers.Control.Split(options);
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];
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");
294 <div id="map" style="width: 100px; height: 100px;"></div>