summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/TileManager.html
blob: 23398be2a5de4b42fb02a122496d23f3b9433544 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">
  
    function test_initialize(t) {
        t.plan(4);
        
        var tileManager = new OpenLayers.TileManager();
        var map = new OpenLayers.Map('map', {
            zoomMethod: null,
            tileManager: tileManager
        });
        var layer = new OpenLayers.Layer.WMS('WMS1', '../img/blank.gif');
        map.addLayer(layer);
        map.setCenter([16, 48], 9);
        t.ok(tileManager.tileQueue[map.id].length, "Tiles queued from layer");
        map.removeLayer(layer);
        t.eq(tileManager.tileQueue[map.id].length, 0, "Tiles unqueued when layer is removed");
        map.addLayer(new OpenLayers.Layer.WMS('WMS2', '../img/blank.gif'));
        map.zoomIn();
        t.ok(tileManager.tileQueue[map.id].length, "Tiles queued from added layer");
        map.destroy();
        t.eq(tileManager.tileQueue[map.id], undefined, "Tile queue removed when map was destroyed");
    }
    
    function test_destroy(t) {
        t.plan(3);
        
        var tileManager = new OpenLayers.TileManager();
        var map = new OpenLayers.Map('map', {tileManager: tileManager});
        var layer = new OpenLayers.Layer.WMS('WMS', '../img/blank.gif');
        map.addLayer(layer);
        map.setCenter([16, 48], 9);
        var numTileListeners = layer.grid[0][0].events.listeners.beforeload.length;
        var numLayerListeners = layer.events.listeners.retile.length;
        var numMapListeners = map.events.listeners.preremovelayer.length;
        tileManager.destroy();
        t.eq(layer.grid[0][0].events.listeners.beforeload.length, numTileListeners - 1, "no listener on tile after destroy");
        t.eq(layer.events.listeners.retile.length, numLayerListeners - 1, "no listeners on layer after destroy");
        t.eq(map.events.listeners.preremovelayer.length, numMapListeners - 1, "no listeners on map after destroy");
        map.destroy();
    }

    function test_manageTileCache(t) {
        t.plan(10);
        
        var tileManager = new OpenLayers.TileManager({
            cacheSize: 12
        });
        var map = new OpenLayers.Map('map', {tileManager: tileManager});
        layer = new OpenLayers.Layer.WMS('WMS', '../img/blank.gif');
        map.addLayer(layer);
        map.setCenter([16, 48], 9);
        var gridSize;
        
        var firstInCache, sharedTile;
        t.delay_call(2, function() {
            t.eq(tileManager.tileCacheIndex.length, 12, "tiles cached");
            t.ok(~OpenLayers.Util.indexOf(tileManager.tileCacheIndex, layer.grid[1][2].url), "tile found in cache");
            t.ok(tileManager.tileCache[layer.grid[1][2].url] === layer.grid[1][2].imgDiv, "correct object cached");
            firstInCache = tileManager.tileCache[tileManager.tileCacheIndex[0]];
            sharedTile = tileManager.tileCache[tileManager.tileCacheIndex[11]];
            gridSize = layer.div.childNodes.length;
            map.setCenter([17, 47]);
        });

        function inCache(img) {
            var search = img.src.split('?')[1];
            for (var s in tileManager.tileCache) {
                if (s.split('?')[1] == search) {
                    return true;
                }
            }
            return false;
        }

        t.delay_call(4, function() {
            t.eq(tileManager.tileCacheIndex.length, 12, "tiles cached");
            t.ok(tileManager.tileCache[layer.grid[1][2].url] === layer.grid[1][2].imgDiv, "correct object cached");
            t.ok(!inCache(firstInCache), "old tile discarded");
            t.ok(inCache(sharedTile), "shared tile still in cache");
            firstInCache = tileManager.tileCache[tileManager.tileCacheIndex[0]];
            map.setCenter([16, 48]);
        });
        t.delay_call(6, function() {
            t.ok(!inCache(firstInCache), "old tile discarded");
            t.ok(inCache(sharedTile), "shared tile still in cache");
            t.eq(layer.div.childNodes.length, gridSize, 'no unused images left in dom');
            map.destroy();
        });
    }
    
    function test_queueTileDraw(t) {
        t.plan(3);

        var tileManager = new OpenLayers.TileManager();
        var map = new OpenLayers.Map('map', {tileManager: tileManager});
        layer = new OpenLayers.Layer.WMS('WMS', '../img/blank.gif');
        map.addLayer(layer);
        map.setCenter([0, 0], 3);
        var queued = tileManager.tileQueue[map.id].length;
        t.ok(tileManager.tileQueue[map.id].length, "Tiles queued for drawing");
        map.zoomIn();
        t.eq(tileManager.tileQueue[map.id].length, queued, "Tile queue has same length after immediate zoom change");
        t.delay_call(1, function() {
            t.eq(tileManager.tileQueue[map.id].length, 0, "Tiles from queue processed");        
            map.destroy();
        });
    }
    
    function test_deferTileDraw(t) {

        t.plan(3);

        var tileManager = new OpenLayers.TileManager();
        var map = new OpenLayers.Map('map', {tileManager: tileManager});
        layer = new OpenLayers.Layer.WMS('WMS', '../img/blank.gif');
        layer.destroy = function() {}; //we're going to do funky things with the grid
        layer.applyBackBuffer = function() {}; // backbuffering isn't under test here
        map.addLayer(layer);
        map.setCenter([-10, 0], 5);
        
        map.moveTo([5, 0]);
        t.ok(tileManager.tileQueue[map.id].length, "tile loading deferred after moveTo");
        map.moveTo([0, 0]);
        t.ok(tileManager.tileQueue[map.id].length, "deferred again after another moveTo");
        t.delay_call(1, function() {
            t.eq(tileManager.tileQueue[map.id].length, 0, "tiles loaded after moveDelay");
        });
    }
  </script>
</head>
<body>
<div id="map" style="width:499px;height:549px;display:none"></div>
</body>
</html>