summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Strategy/Refresh.html
blob: 054f02818ca5daaa686c9114e4428e0fb4553cec (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">

    var interval = 5000;
    
    function test_initialize(t) {
        t.plan(2);

        var s = new OpenLayers.Strategy.Refresh({interval: interval});
        t.ok(typeof s.interval === "number", "interval must be a number");
        t.eq(s.interval, interval, "ctor sets interval");
    }

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

        var l = new OpenLayers.Layer.Vector();
        l.setVisibility(false);
        var s = new OpenLayers.Strategy.Refresh();
        s.setLayer(l);

        t.eq(s.active, false, "not active after construction");

        var activated = s.activate();
        t.eq(activated, true, "activate returns true");
        t.eq(s.active, true, "activated after activate");
        t.ok(l.events.listeners.visibilitychanged[0].obj == s &&
            l.events.listeners.visibilitychanged[0].func == s.reset,
            "activates registers visibilitychanged listener");
    }

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

        var l = new OpenLayers.Layer.Vector();
        l.setVisibility(false);
        var s = new OpenLayers.Strategy.Refresh();
        s.setLayer(l);
        s.activate();
        var deactivated = s.deactivate();
        t.eq(deactivated, true, "deactivate returns true");
        t.eq(s.active, false, "deactivated after activate");
        t.ok(l.events.listeners.visibilitychanged.length == 0,
            "deactivate unregisters visibilitychanged listener");
    }
	
    function test_activateWithVisibleLayer(t) {
        t.plan(5);

        var l = new OpenLayers.Layer.Vector();
        l.setVisibility(true);
        var s = new OpenLayers.Strategy.Refresh({interval: interval});
        s.setLayer(l);

        t.eq(s.active, false, "not active after construction");

        var activated = s.activate();
        t.eq(activated, true, "activate returns true");
        t.eq(s.active, true, "activated after activate");
        t.ok(l.events.listeners.visibilitychanged[0].obj == s &&
            l.events.listeners.visibilitychanged[0].func == s.reset,
            "activates registers visibilitychanged listener");
        t.ok(s.timer !== null, "timer should be set on activate if layer is visible");

        // reset the timer!!
        s.stop();
    }

    function test_events(t) {
        
        t.plan(1);
        var log = {
            visibilitychanged: 0
        };

        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector(null, {
            strategies: [new OpenLayers.Strategy.Refresh({interval: interval})],
            protocol: new OpenLayers.Protocol({
                read: function(config) {
                    config.callback.call(config.scope, {});
                }
            }),
            isBaseLayer: true,
            eventListeners: {
                visibilitychanged: function() {
                    ++log.visibilitychanged;
                }
            }            
        });
        map.addLayer(layer);

        layer.setVisibility(false);
        t.eq(log.visibilitychanged, 1, "visibilitychanged triggered");
        
        map.destroy();
        
    }

    function test_refreshWithNormalProgress(t) {
        
        t.plan(1);
        var log = {
            refreshcalled: 0
        };

        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector(null, {
            strategies: [new OpenLayers.Strategy.Refresh({
                interval: interval,
                refresh: function() {
                    if (this.layer && this.layer.refresh) {
                        ++log.refreshcalled;
                    }
                }
            })],
            protocol: new OpenLayers.Protocol({
                read: function(config) {
                    config.callback.call(config.scope, {});
                }
            }),
            isBaseLayer: true
        });
        map.addLayer(layer);

        t.delay_call((5 * (interval / 1000)) + 0.5, function() { 
            t.eq(log.refreshcalled, 5, "number of refreshes");
            map.destroy();
        });
        
    }

    function test_refreshWithSwitchingVisibility(t) {
        
        t.plan(1);
        var log = {
            refreshcalled: 0
        };

        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector(null, {
            strategies: [new OpenLayers.Strategy.Refresh({
                interval: interval,
                refresh: function() {
                    if (this.layer && this.layer.refresh) {
                        ++log.refreshcalled;
                    }
                }
            })],
            protocol: new OpenLayers.Protocol({
                read: function(config) {
                    config.callback.call(config.scope, {});
                }
            }),
            isBaseLayer: true
        });
        map.addLayer(layer);

        window.setTimeout(function() {
            layer.setVisibility(false);
        }, 2.5 * interval);
        
        t.delay_call((5 * (interval / 1000)) + 0.5, function() { 
            t.eq(log.refreshcalled, 2, "number of refreshes");
            map.destroy();
        });
        
    }

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