3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_initialize(t) {
8 var strategy = new OpenLayers.Strategy.Save();
9 t.eq(strategy.auto, false, "auto is false by default");
12 function test_activate(t) {
16 var strategy = new OpenLayers.Strategy.Save();
17 var layer = new OpenLayers.Layer.Vector(null, {
19 protocol: new OpenLayers.Protocol(),
20 strategies: [strategy]
22 var map = new OpenLayers.Map("map");
24 map.zoomToMaxExtent();
26 // check that auto true registers listeners
27 strategy.deactivate();
30 t.ok(layer.events.listeners["featureadded"][0].func === strategy.triggerSave,
31 "[auto true] triggerSave registered as listener for featureadded");
32 t.ok(layer.events.listeners["afterfeaturemodified"][0].func === strategy.triggerSave,
33 "[auto true] triggerSave registered as listener for afterfeaturemodified");
35 // check that auto can be set to interval
36 strategy.deactivate();
39 t.ok(strategy.timer != null, "[auto number] timer set")
45 function test_save(t) {
48 var strategy = new OpenLayers.Strategy.Save();
50 // mock up a protocol for synchronous and successful commits
51 var protocol = new OpenLayers.Protocol({
52 commit: function(features, options) {
53 var response = new OpenLayers.Protocol.Response();
54 response.reqFeatures = features;
55 response.insertIds = [];
56 for(var i=0; i<features.length; ++i) {
57 if(features[i].state == OpenLayers.State.INSERT) {
58 response.insertIds.push("new_" + i);
61 response.code = OpenLayers.Protocol.Response.SUCCESS;
62 options.callback.call(options.scope, response);
66 var layer = new OpenLayers.Layer.Vector(null, {
69 strategies: [strategy]
71 var map = new OpenLayers.Map("map");
73 map.zoomToMaxExtent();
75 // give the layer some features
77 new OpenLayers.Feature.Vector(), // insert
78 new OpenLayers.Feature.Vector(), // delete
79 new OpenLayers.Feature.Vector(), // update
80 new OpenLayers.Feature.Vector() // nothing
82 features[0].state = OpenLayers.State.INSERT;
83 features[1].state = OpenLayers.State.DELETE;
84 features[2].state = OpenLayers.State.UPDATE;
85 layer.addFeatures(features);
87 // save feature modifications
88 strategy.save(features);
90 // confirm that newly created feature has an id and no longer has insert state
91 t.eq(features[0].fid, "new_0", "newly created feature gets fid");
92 t.ok(features[0].state == null, "newly created feature no longer insert state");
94 // confirm that deleted features are not on layer
95 t.eq(layer.features.length, 3, "layer only has three features");
96 t.ok(features[1].layer == null, "deleted feature has no layer");
98 // confirm that updated feature no longer has update state
99 t.ok(features[2].state == null, "updated feature no longer update state");
106 <div id="map" style="width: 400px; height: 200px" />