OpenLayers OpenLayers

root/trunk/openlayers/tests/Control/Permalink.html

Revision 7986, 11.7 kB (checked in by euzuro, 4 months ago)

Fix for Permalink's override-able createParams() function, which was designed to allow for input parameter values for center, zoom, and layers... but was actually only using the center value... zoom and layers were being taken from the map and the input parameters ignored. Big thanks to our anonymous poster for spotting this. r=ahocevar (Pullup #1489)

  • Property svn:eol-style set to native
Line 
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5     var map;
6     function test_Control_Permalink_constructor (t) {
7         t.plan( 2 );
8    
9         control = new OpenLayers.Control.Permalink();
10         t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
11         t.eq( control.displayClass,  "olControlPermalink", "displayClass is correct" );
12     }
13     function test_Control_Permalink_uncentered (t) {
14         t.plan( 1 );
15    
16         control = new OpenLayers.Control.Permalink('permalink');
17         map = new OpenLayers.Map('map');
18         map.addControl(control);
19         map.events.triggerEvent("changelayer", {});
20         t.ok(true, "permalink didn't bomb out.");
21     }   
22     function test_Control_Permalink_initwithelem (t) {
23         t.plan( 1 );
24    
25         control = new OpenLayers.Control.Permalink(OpenLayers.Util.getElement('permalink'));
26         t.ok(true, "If the above line doesn't throw an error, we're safe.");
27     }
28     function test_Control_Permalink_updateLinks (t) {
29         t.plan( 3 );
30    
31         control = new OpenLayers.Control.Permalink('permalink');
32         t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
33         map = new OpenLayers.Map('map');
34         layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
35         map.addLayer(layer);
36         layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}, {'isBaseLayer': false});
37         map.addLayer(layer);
38         layer.setVisibility(true);
39         if (!map.getCenter())  map.zoomToMaxExtent();
40         map.addControl(control);
41         map.pan(5, 0, {animate:false});
42         t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"?zoom=2&lat=0&lon=1.75781&layers=BT"), 'pan sets permalink');
43        
44         map.layers[1].setVisibility(false);
45        
46         t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"?zoom=2&lat=0&lon=1.75781&layers=BF"), 'setVisibility sets permalink');
47     }
48     function test_Control_Permalink_updateLinksBase (t) {
49         t.plan( 2 );
50    
51         control = new OpenLayers.Control.Permalink('permalink', "./edit.html" );
52         t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
53         map = new OpenLayers.Map('map');
54         layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
55         map.addLayer(layer);
56         if (!map.getCenter())  map.zoomToMaxExtent();
57         map.addControl(control);
58         map.pan(5, 0, {animate:false});
59         OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
60         t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base");
61   }
62   function test_Control_Permalink_noElement (t) {
63         t.plan( 2 );
64         control = new OpenLayers.Control.Permalink( );
65         t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
66         map = new OpenLayers.Map('map');
67         map.addControl(control);
68         t.eq(map.controls[4].div.firstChild.nodeName, "A", "Permalink control creates div with 'a' inside." );
69   }
70   function test_Control_Permalink_base_with_query (t) {
71       t.plan( 3 );
72  
73       control = new OpenLayers.Control.Permalink('permalink', "./edit.html?foo=bar" );
74       map = new OpenLayers.Map('map');
75       layer = new OpenLayers.Layer.WMS('Test Layer', "http://example.com" );
76       map.addLayer(layer);
77       if (!map.getCenter())  map.zoomToMaxExtent();
78       map.addControl(control);
79       map.pan(5, 0, {animate:false});
80       OpenLayers.Util.getElement('edit_permalink').href = './edit.html?foo=bar&zoom=2&lat=0&lon=1.75781&layers=B';
81       t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring");
82      
83       control = new OpenLayers.Control.Permalink('permalink', "./edit.html?foo=bar&" );
84       map.addControl(control);
85       map.pan(0, 0, {animate:false});
86       t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring ending with '&'");
87      
88       control = new OpenLayers.Control.Permalink('permalink', "./edit.html?" );
89       OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
90       map.addControl(control);
91       map.pan(5, 0, {animate:false});
92       map.pan(-5, 0, {animate:false});
93       t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring ending with '?'");
94
95   }
96
97     function test_Control_Permalink_nonRepeating (t) {
98         t.plan( 2 );
99    
100         control = new OpenLayers.Control.Permalink('permalink', "./edit.html?zoom=3" );
101         t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
102         map = new OpenLayers.Map('map');
103         layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
104         map.addLayer(layer);
105         if (!map.getCenter())  map.zoomToMaxExtent();
106         map.addControl(control);
107         map.pan(5, 0, {animate:false});
108         OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
109         t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with existing zoom in base");
110   }
111  
112     function test_Control_Permalink_customized(t) {
113         t.plan(2);
114      
115         var argParserClass = OpenLayers.Class(OpenLayers.Control.ArgParser, {
116             CLASS_NAME: "CustomArgParser"
117         });
118      
119         control = new OpenLayers.Control.Permalink(null, "./edit.html", {
120             argParserClass: argParserClass,
121             createParams: function(center, zoom, layers) {
122                 var params = OpenLayers.Control.Permalink.prototype.createParams.apply(control, arguments);
123                 params.customParam = "foo";
124                 return params;
125             }
126         });
127         map = new OpenLayers.Map('map');
128         layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
129         map.addLayer(layer);
130         if (!map.getCenter())  map.zoomToMaxExtent();
131         map.addControl(control);
132         map.pan(5, 0, {animate:false});
133      
134         t.eq(this.map.controls[this.map.controls.length-1].CLASS_NAME, "CustomArgParser", "Custom ArgParser added correctly.");
135         t.eq(control.div.firstChild.getAttribute("href"), "./edit.html?zoom=2&lat=0&lon=1.75781&layers=B&customParam=foo", "Custom parameter encoded correctly.");
136     }
137    
138     function test_Control_Permalink_createParams(t) {
139         t.plan(18);
140
141         var baseLayer = { 'isBaseLayer': true };
142
143         var m = {
144             'getCenter': function() { return null; }
145         };
146        
147         var pl = {
148             'map': m,
149             'base': {}
150         };
151
152         old_getParameters = OpenLayers.Util.getParameters;
153         OpenLayers.Util.getParameters = function(base) {
154             t.ok(base == pl.base, "correct base sent in to Util.getParameters()");
155             return g_Params;
156         };
157        
158       //null center, null map.getCenter()
159         g_Params = {};
160         m.baseLayer = baseLayer;
161         var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, []);
162         t.ok(returnParams == g_Params, "correct params returned on null center");
163
164       //valid center, zoom, layers
165         g_Params = { 'test': {} };
166         var center = { 'lon': 1.2345678901, 'lat': 9.8765432109 };
167         var zoom = {};
168         var layers = [
169             { 'isBaseLayer': true },
170             baseLayer,
171             { 'isBaseLayer': false, 'getVisibility': function() { return true; } },
172             { 'isBaseLayer': false, 'getVisibility': function() { return false; } }
173         ];
174         var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, [center, zoom, layers]);
175
176         t.ok(returnParams.test == g_Params.test, "correct params returned from Util.getParameters() when valid center, zoom, layers");
177         t.ok(returnParams.zoom == zoom, "params.zoom set correctly when valid center, zoom, layers");
178         t.eq(returnParams.lon, 1.23457, "lon set and rounded correctly when valid center, zoom, layers");
179         t.eq(returnParams.lat, 9.87654, "lat set and rounded correctly when valid center, zoom, layers");
180         t.eq(returnParams.layers, "0BTF", "layers processed correctly when valid center, zoom, layers")
181
182
183       //null center, zoom, layers, with displayProjection
184         g_Params = { 'test': {} };
185         g_Projection = {};
186         m = {
187             'baseLayer': baseLayer,
188             'getProjectionObject':  function() { return g_Projection; },
189             'center': { 'lon': {}, 'lat': {} },
190             'getCenter': function() { return this.center; },
191             'zoom': {},
192             'getZoom': function() { return this.zoom; },
193            'layers': [
194                 { 'isBaseLayer': false, 'getVisibility': function() { return true; } },
195                 baseLayer,
196                 { 'isBaseLayer': false, 'getVisibility': function() { return false; } },
197                 { 'isBaseLayer': true }
198             ],
199             'getLayers': function() { return this.layers; }
200         };
201         pl = {
202             'base': {},
203             'map': m,
204             'displayProjection': {}
205         };
206        
207         old_transform = OpenLayers.Projection.transform;
208         OpenLayers.Projection.transform = function(point, projObj, dispProj) {
209             t.ok(point.x = m.center.lon, "correct x value passed into transform");
210             t.ok(point.y = m.center.lat, "correct x value passed into transform");
211             t.ok(projObj == g_Projection, "correct projection object from map passed into transform");
212             t.ok(dispProj == pl.displayProjection, "correct displayProjection from control passed into transform");
213
214             return { 'x': 9.8765432109, 'y': 1.2345678901 };
215         };
216        
217         center = zoom = layers = null;
218
219         var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, [center, zoom, layers]);
220         t.ok(returnParams.test == g_Params.test, "correct params returned from Util.getParameters() when null center, zoom, layers, with displayProjection");
221         t.ok(returnParams.zoom == m.zoom, "params.zoom set correctly when null center, zoom, layers, with displayProjection");
222         t.eq(returnParams.lon, 9.87654, "lon set, transformed, and rounded correctly when null center, zoom, layers, with displayProjection");
223         t.eq(returnParams.lat, 1.23457, "lat set, transformed, and rounded correctly when null center, zoom, layers, with displayProjection");
224         t.eq(returnParams.layers, "TBF0", "layers processed correctly when null center, zoom, layers, with displayProjection");
225
226         OpenLayers.Util.getParameters = old_getParameters;
227         OpenLayers.Projection.transform = old_transform;
228     }
229    
230   </script>
231 </head>
232 <body>
233     <a id="permalink" href="">Permalink</a> <br />
234     <a id="edit_permalink" href="">Edit</a> <br />
235     <div id="map" style="width: 1024px; height: 512px;"/>
236 </body>
237 </html>
Note: See TracBrowser for help on using the browser.