3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_activate(t) {
9 var strategy = new OpenLayers.Strategy.Paging();
10 t.eq(strategy.active, false, "not active after construction");
12 var layer = new OpenLayers.Layer.Vector("Vector Layer", {
13 strategies: [strategy]
15 var map = new OpenLayers.Map('map');
18 t.eq(strategy.active, true, "active after adding to map");
21 function test_paging(t) {
24 var strategy = new OpenLayers.Strategy.Paging();
25 var layer = new OpenLayers.Layer.Vector("Vector Layer", {
26 strategies: [strategy],
27 drawFeature: function() {}
29 var map = new OpenLayers.Map('map');
32 var features = new Array(25);
33 for(var i=0; i<features.length; ++i) {
34 features[i] = {destroy: function() {}};
37 function featuresEq(got, exp) {
39 if(got instanceof Array && exp instanceof Array) {
40 if(got.length === exp.length) {
41 for(var i=0; i<got.length; ++i) {
42 if(got[i] !== exp[i]) {
43 console.log(got[i], exp[i]);
47 eq = (i == got.length);
53 var len = strategy.pageLength();
54 t.eq(len, 10, "page length defaults to 10");
56 // add 25 features to the layer
57 layer.addFeatures(features);
58 t.eq(strategy.features.length, features.length, "strategy caches all features");
59 t.eq(layer.features.length, len, "layer gets one page of features");
60 t.ok(featuresEq(layer.features, features.slice(0, len)), "layer gets first page initially");
61 t.eq(strategy.pageNum(), 0, "strategy reports 0 based page number");
62 t.eq(strategy.pageCount(), Math.ceil(features.length / len), "strategy reports correct number of pages");
64 // load next page of features
65 var changed = strategy.pageNext();
66 t.eq(changed, true, "(1) strategy reports change");
67 t.eq(strategy.pageNum(), 1, "second page");
68 t.ok(featuresEq(layer.features, features.slice(len, 2*len)), "layer has second page of features");
70 // load next page of features (half page)
71 changed = strategy.pageNext();
72 t.eq(changed, true, "(2) strategy reports change");
73 t.eq(strategy.pageNum(), 2, "third page");
75 // try to change forward again
76 changed = strategy.pageNext();
77 t.eq(changed, false, "strategy reports no change");
78 t.eq(layer.features.length, features.length % len, "layer has partial page");
79 t.ok(featuresEq(layer.features, features.slice(2*len, 3*len)), "layer has third page of features");
80 t.eq(strategy.pageNum(), 2, "still on third page");
83 changed = strategy.pagePrevious();
84 t.eq(changed, true, "(3) strategy reports change");
85 t.eq(strategy.pageNum(), 1, "back on second page");
86 t.ok(featuresEq(layer.features, features.slice(len, 2*len)), "layer has second page of features again");
92 function test_deactivate(t) {
95 var strategy = new OpenLayers.Strategy.Paging();
96 var layer = new OpenLayers.Layer.Vector("Vector Layer", {
97 strategies: [strategy]
99 var map = new OpenLayers.Map('map');
102 t.eq(strategy.active, true, "active after adding to map");
104 map.removeLayer(layer);
105 t.eq(strategy.active, false, "not active after removing from map");
111 <div id="map" style="width: 400px; height: 200px" />