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
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(1);
var strategy = new OpenLayers.Strategy.Save();
t.eq(strategy.auto, false, "auto is false by default");
}
function test_activate(t) {
t.plan(3);
var strategy = new OpenLayers.Strategy.Save();
var layer = new OpenLayers.Layer.Vector(null, {
isBaseLayer: true,
protocol: new OpenLayers.Protocol(),
strategies: [strategy]
});
var map = new OpenLayers.Map("map");
map.addLayer(layer);
map.zoomToMaxExtent();
// check that auto true registers listeners
strategy.deactivate();
strategy.auto = true;
strategy.activate();
t.ok(layer.events.listeners["featureadded"][0].func === strategy.triggerSave,
"[auto true] triggerSave registered as listener for featureadded");
t.ok(layer.events.listeners["afterfeaturemodified"][0].func === strategy.triggerSave,
"[auto true] triggerSave registered as listener for afterfeaturemodified");
// check that auto can be set to interval
strategy.deactivate();
strategy.auto = 1;
strategy.activate();
t.ok(strategy.timer != null, "[auto number] timer set")
map.destroy();
}
function test_save(t) {
t.plan(9);
var strategy = new OpenLayers.Strategy.Save();
// mock up a protocol for synchronous and successful commits
var protocol = new OpenLayers.Protocol({
commit: function(features, options) {
var response = new OpenLayers.Protocol.Response();
response.reqFeatures = features;
response.insertIds = [];
for(var i=0; i<features.length; ++i) {
// test feature.url first
t.eq(features[i].url, "url" + i,
"feature.url correctly set (url" + i + ")");
if(features[i].state == OpenLayers.State.INSERT) {
response.insertIds.push("new_" + i);
}
}
response.code = OpenLayers.Protocol.Response.SUCCESS;
options.callback.call(options.scope, response);
}
});
var layer = new OpenLayers.Layer.Vector(null, {
isBaseLayer: true,
protocol: protocol,
strategies: [strategy],
projection: "EPSG:4326"
});
var map = new OpenLayers.Map("map", {
getProjectionObject: function() {
return new OpenLayers.Projection("EPSG:900913");
}
})
map.addLayer(layer);
map.zoomToMaxExtent();
// give the layer some features
var features = [
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(5, 45)
), // insert
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(6, 46)
), // delete
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(7, 47)
), // update
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(8, 48)
) // nothing
];
features[0].state = OpenLayers.State.INSERT;
features[0].url = "url0";
features[1].state = OpenLayers.State.DELETE;
features[1].url = "url1";
features[2].state = OpenLayers.State.UPDATE;
features[2].url = "url2";
features[3].url = "url3";
layer.addFeatures(features);
// save feature modifications
strategy.save(features);
// confirm that newly created feature has an id and no longer has insert state
t.eq(features[0].fid, "new_0", "newly created feature gets fid");
t.ok(features[0].state == null, "newly created feature no longer insert state");
// confirm that deleted features are not on layer
t.eq(layer.features.length, 3, "layer only has three features");
t.ok(features[1].layer == null, "deleted feature has no layer");
// confirm that updated feature no longer has update state
t.ok(features[2].state == null, "updated feature no longer update state");
}
</script>
</head>
<body>
<div id="map" style="width: 400px; height: 200px" />
</body>
</html>
|