summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Control/Pan.html
blob: 0c9dfafa4428b6d2d5a1ca2d92f4c0090323d0bf (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html>
    <head>
        <script src="../OLLoader.js"></script>
        <script type="text/javascript">
        
function test_Pan_constructor (t) {
    t.plan( 2 );
    
    // setup
    var control = new OpenLayers.Control.Pan(
        "Gargoyle" // the direction, here mocked up
    );
    
    // tests
    //
    t.ok( 
        control instanceof OpenLayers.Control.Pan, 
        "new OpenLayers.Control.Pan returns object"
    );
    t.eq( 
        control.displayClass, "olControlPanGargoyle", 
        "displayClass is correct"
    );
    
    // tear down
    control.destroy();
}

function test_Pan_type (t) {
    t.plan( 1 );
    
    // setup
    var control = new OpenLayers.Control.Pan();
    
    // tests
    //
    t.eq( 
        control.type, 
        OpenLayers.Control.TYPE_BUTTON,
        "Pan control is of type OpenLayers.Control.TYPE_BUTTON"
    );
    
    // tear down
    control.destroy();
}

function test_Pan_constants (t) {
    var dirs = [
            'North',
            'East',
            'South',
            'West'
        ],
        numDirs = dirs.length,
        dir, uc_dir;
    
    t.plan(numDirs);
    
    for ( ; numDirs > 0; numDirs-- ) {
        dir = dirs[numDirs - 1 ];
        uc_dir = dir.toUpperCase();
        
        t.eq(
            OpenLayers.Control.Pan[ uc_dir ],
            dir,
            "A constant 'OpenLayers.Control.Pan." + uc_dir + "' is defined "+
                "and has the correct value of '" + dir + "'."
        );
    }
}

function test_Pan_trigger (t) {
    t.plan( 12 );
    
    // set up
    var controls = {
            n: new OpenLayers.Control.Pan(OpenLayers.Control.Pan.NORTH),
            e: new OpenLayers.Control.Pan(OpenLayers.Control.Pan.EAST),
            s: new OpenLayers.Control.Pan(OpenLayers.Control.Pan.SOUTH),
            w: new OpenLayers.Control.Pan(OpenLayers.Control.Pan.WEST)
        },
        controlKey, control,
        zoomlevel = 5,
        center = new OpenLayers.LonLat(25,25),
        log = {
            dx: null,
            dy: null
        },
        map = new OpenLayers.Map("map", {
            allOverlays: true,
            layers: [
                new OpenLayers.Layer.Vector()
            ],
            center: center,
            zoom: zoomlevel
        }),
        oldZoom;
    
    // overwrite native Map::pan
    map.pan = function(dx, dy) {
        log = {
            dx: dx,
            dy: dy
        };
        OpenLayers.Map.prototype.pan.apply(map, arguments);
    };
    
    oldCenter = map.getCenter().toString();
    
    for (controlKey in controls) {
        if (controls.hasOwnProperty(controlKey)) {
            control = controls[controlKey];
            // trigger the control; nothing should change, we aren't added yet.
            control.trigger();
            
            t.ok(
                log.dx === null && log.dy === null, 
                'Calling trigger on a non added control doesn\'t do anything.'
            );
            
            // reset log object
            log = {
                dx: null,
                dy: null
            };
        }
    }
    
    // now lets add the controls, and trigger them again
    for (controlKey in controls) {
        if (controls.hasOwnProperty(controlKey)) {
            control = controls[controlKey];
            map.addControl(control);
            // trigger again, now ...
            control.trigger();
            
            // ... the center should change ...
            t.ok(
                log.dx !== null && log.dy !== null,
                'Calling trigger on an added pan control calls map.pan()... '
            );
            
            // ... with sane arguments according to the passed direction.
            switch (control.direction) {
                case OpenLayers.Control.Pan.NORTH: 
                    t.ok(
                        log.dx === 0 && log.dy < 0,
                        '... with sane arguments: pan north only results in ' +
                            'negative delta y'
                    );
                    break;
                case OpenLayers.Control.Pan.SOUTH: 
                    t.ok(
                        log.dx === 0 && log.dy > 0,
                        '... with sane arguments: pan south only results in ' +
                            'positive delta y'
                    );
                    break;
                case OpenLayers.Control.Pan.WEST: 
                    t.ok(
                        log.dx < 0 && log.dy === 0,
                        '... with sane arguments: pan west only results in ' +
                            'negative delta x'
                    );
                    break;
                case OpenLayers.Control.Pan.EAST: 
                    t.ok(
                        log.dx > 0 && log.dy === 0,
                        '... with sane arguments: pan east only results in ' +
                            'positive delta x'
                    );
                    break;
            }
            
            // reset log-object
            log = {
                dx: null,
                dy: null
            };
            // always set to initial center and zoom:
            map.setCenter(center, zoomlevel);
        }
    }
    
    // tear down
    for (controlKey in controls) {
        if (controls.hasOwnProperty(controlKey)) {
            control = controls[controlKey];
            control.destroy();
        }
    }
    map.destroy();
}
    
        </script>
    </head>
    <body>
        <div id="map" style="width: 1000px; height: 1000px;"></div>
    </body>
</html>