summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Strategy/Paging.html
blob: a85167eaa68ef96287970237091d00cba597b564 (plain)
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
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">

    function test_activate(t) {
        t.plan(2);

        var strategy = new OpenLayers.Strategy.Paging();
        t.eq(strategy.active, false, "not active after construction");

        var layer = new OpenLayers.Layer.Vector("Vector Layer", {
            strategies: [strategy]
        });
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);
        
        t.eq(strategy.active, true, "active after adding to map");
    }
    
    function test_paging(t) {
        t.plan(18);

        var strategy = new OpenLayers.Strategy.Paging();
        var layer = new OpenLayers.Layer.Vector("Vector Layer", {
            strategies: [strategy],
            drawFeature: function() {}
        });
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);
        
        var features = new Array(25);
        for(var i=0; i<features.length; ++i) {
            features[i] = {destroy: function() {}};
        }
        
        function featuresEq(got, exp) {
            var eq = false;
            if(got instanceof Array && exp instanceof Array) {
                if(got.length === exp.length) {
                    for(var i=0; i<got.length; ++i) {
                        if(got[i] !== exp[i]) {
                            console.log(got[i], exp[i]);
                            break;
                        }
                    }
                    eq = (i == got.length);
                }
            }
            return eq;
        }
        
        var len = strategy.pageLength();
        t.eq(len, 10, "page length defaults to 10");
        
        // add 25 features to the layer
        layer.addFeatures(features);
        t.eq(strategy.features.length, features.length, "strategy caches all features");
        t.eq(layer.features.length, len, "layer gets one page of features");
        t.ok(featuresEq(layer.features, features.slice(0, len)), "layer gets first page initially");
        t.eq(strategy.pageNum(), 0, "strategy reports 0 based page number");
        t.eq(strategy.pageCount(), Math.ceil(features.length / len), "strategy reports correct number of pages");
        
        // load next page of features
        var changed = strategy.pageNext();
        t.eq(changed, true, "(1) strategy reports change");
        t.eq(strategy.pageNum(), 1, "second page");
        t.ok(featuresEq(layer.features, features.slice(len, 2*len)), "layer has second page of features");
        
        // load next page of features (half page)
        changed = strategy.pageNext();
        t.eq(changed, true, "(2) strategy reports change");
        t.eq(strategy.pageNum(), 2, "third page");
        
        // try to change forward again
        changed = strategy.pageNext();
        t.eq(changed, false, "strategy reports no change");
        t.eq(layer.features.length, features.length % len, "layer has partial page");
        t.ok(featuresEq(layer.features, features.slice(2*len, 3*len)), "layer has third page of features");
        t.eq(strategy.pageNum(), 2, "still on third page");
        
        // change back a page
        changed = strategy.pagePrevious();
        t.eq(changed, true, "(3) strategy reports change");
        t.eq(strategy.pageNum(), 1, "back on second page");
        t.ok(featuresEq(layer.features, features.slice(len, 2*len)), "layer has second page of features again");
        
        layer.destroy();
        
    }

    function test_deactivate(t) {
        t.plan(2);

        var strategy = new OpenLayers.Strategy.Paging();
        var layer = new OpenLayers.Layer.Vector("Vector Layer", {
            strategies: [strategy]
        });
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);
        
        t.eq(strategy.active, true, "active after adding to map");
        
        map.removeLayer(layer);
        t.eq(strategy.active, false, "not active after removing from map");
    }

  </script>
</head>
<body>
    <div id="map" style="width: 400px; height: 200px" />
</body>
</html>