3 <script src="../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_Control_Permalink_constructor (t) {
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" );
13 function test_Control_Permalink_uncentered (t) {
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.");
22 function test_Control_Permalink_initwithelem (t) {
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.");
28 function test_Control_Permalink_updateLinks (t) {
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'});
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});
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');
44 map.layers[1].setVisibility(false);
46 t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"?zoom=2&lat=0&lon=1.75781&layers=BF"), 'setVisibility sets permalink');
48 function test_Control_Permalink_updateLinksBase (t) {
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'});
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");
62 function test_Control_Permalink_noElement (t) {
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." );
70 function test_Control_Permalink_base_with_query (t) {
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" );
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");
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 '&'");
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 '?'");
97 function test_Control_Permalink_nonRepeating (t) {
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'});
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");
112 function test_Control_Permalink_customized(t) {
115 var argParserClass = OpenLayers.Class(OpenLayers.Control.ArgParser, {
116 CLASS_NAME: "CustomArgParser"
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";
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'});
130 if (!map.getCenter()) map.zoomToMaxExtent();
131 map.addControl(control);
132 map.pan(5, 0, {animate:false});
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.");
138 function test_Control_Permalink_createParams(t) {
141 var baseLayer = { 'isBaseLayer': true };
144 'getCenter': function() { return null; }
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()");
158 //null center, null map.getCenter()
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");
164 //valid center, zoom, layers
165 g_Params = { 'test': {} };
166 var center = { 'lon': 1.2345678901, 'lat': 9.8765432109 };
169 { 'isBaseLayer': true },
171 { 'isBaseLayer': false, 'getVisibility': function() { return true; } },
172 { 'isBaseLayer': false, 'getVisibility': function() { return false; } }
174 var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, [center, zoom, layers]);
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")
183 //null center, zoom, layers, with displayProjection
184 g_Params = { 'test': {} };
187 'baseLayer': baseLayer,
188 'getProjectionObject': function() { return g_Projection; },
189 'center': { 'lon': {}, 'lat': {} },
190 'getCenter': function() { return this.center; },
192 'getZoom': function() { return this.zoom; },
194 { 'isBaseLayer': false, 'getVisibility': function() { return true; } },
196 { 'isBaseLayer': false, 'getVisibility': function() { return false; } },
197 { 'isBaseLayer': true }
199 'getLayers': function() { return this.layers; }
204 'displayProjection': {}
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");
214 return { 'x': 9.8765432109, 'y': 1.2345678901 };
217 center = zoom = layers = null;
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");
226 OpenLayers.Util.getParameters = old_getParameters;
227 OpenLayers.Projection.transform = old_transform;
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;"/>