Permalink.html 11.7 KB
<html>
<head>
  <script src="../../lib/OpenLayers.js"></script>
  <script type="text/javascript">
    var map; 
    function test_Control_Permalink_constructor (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.Permalink();
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        t.eq( control.displayClass,  "olControlPermalink", "displayClass is correct" );
    }
    function test_Control_Permalink_uncentered (t) {
        t.plan( 1 );
    
        control = new OpenLayers.Control.Permalink('permalink');
        map = new OpenLayers.Map('map');
        map.addControl(control);
        map.events.triggerEvent("changelayer", {});
        t.ok(true, "permalink didn't bomb out.");
    }   
    function test_Control_Permalink_initwithelem (t) {
        t.plan( 1 );
    
        control = new OpenLayers.Control.Permalink(OpenLayers.Util.getElement('permalink'));
        t.ok(true, "If the above line doesn't throw an error, we're safe."); 
    }
    function test_Control_Permalink_updateLinks (t) {
        t.plan( 3 );
    
        control = new OpenLayers.Control.Permalink('permalink');
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        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});
        map.addLayer(layer);
        layer.setVisibility(true);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
        t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"?zoom=2&lat=0&lon=1.75781&layers=BT"), 'pan sets permalink');
        
        map.layers[1].setVisibility(false);
        
        t.ok(OpenLayers.Util.isEquivalentUrl(OpenLayers.Util.getElement('permalink').href, location+"?zoom=2&lat=0&lon=1.75781&layers=BF"), 'setVisibility sets permalink');
    }
    function test_Control_Permalink_updateLinksBase (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.Permalink('permalink', "./edit.html" );
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
        OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
        t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base");
  }
  function test_Control_Permalink_noElement (t) {
        t.plan( 2 );
        control = new OpenLayers.Control.Permalink( );
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        map.addControl(control);
        t.eq(map.controls[4].div.firstChild.nodeName, "A", "Permalink control creates div with 'a' inside." );
  }
  function test_Control_Permalink_base_with_query (t) {
      t.plan( 3 );
  
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html?foo=bar" );
      map = new OpenLayers.Map('map');
      layer = new OpenLayers.Layer.WMS('Test Layer', "http://example.com" );
      map.addLayer(layer);
      if (!map.getCenter())  map.zoomToMaxExtent();
      map.addControl(control);
      map.pan(5, 0, {animate:false});
      OpenLayers.Util.getElement('edit_permalink').href = './edit.html?foo=bar&zoom=2&lat=0&lon=1.75781&layers=B';
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring");
      
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html?foo=bar&" );
      map.addControl(control);
      map.pan(0, 0, {animate:false});
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring ending with '&'");
      
      control = new OpenLayers.Control.Permalink('permalink', "./edit.html?" );
      OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
      map.addControl(control);
      map.pan(5, 0, {animate:false});
      map.pan(-5, 0, {animate:false});
      t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base and querystring ending with '?'");

  }

    function test_Control_Permalink_nonRepeating (t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.Permalink('permalink', "./edit.html?zoom=3" );
        t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
        OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
        t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with existing zoom in base");
  }
  
    function test_Control_Permalink_customized(t) {
        t.plan(2);
      
        var argParserClass = OpenLayers.Class(OpenLayers.Control.ArgParser, {
            CLASS_NAME: "CustomArgParser"
        });
      
        control = new OpenLayers.Control.Permalink(null, "./edit.html", {
            argParserClass: argParserClass,
            createParams: function(center, zoom, layers) {
                var params = OpenLayers.Control.Permalink.prototype.createParams.apply(control, arguments);
                params.customParam = "foo";
                return params;
            }
        });
        map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
        map.addLayer(layer);
        if (!map.getCenter())  map.zoomToMaxExtent();
        map.addControl(control);
        map.pan(5, 0, {animate:false});
      
        t.eq(this.map.controls[this.map.controls.length-1].CLASS_NAME, "CustomArgParser", "Custom ArgParser added correctly.");
        t.eq(control.div.firstChild.getAttribute("href"), "./edit.html?zoom=2&lat=0&lon=1.75781&layers=B&customParam=foo", "Custom parameter encoded correctly.");
    }
    
    function test_Control_Permalink_createParams(t) {
        t.plan(18);

        var baseLayer = { 'isBaseLayer': true };

        var m = {
            'getCenter': function() { return null; }
        };
        
        var pl = {
            'map': m,
            'base': {}
        };

        old_getParameters = OpenLayers.Util.getParameters;
        OpenLayers.Util.getParameters = function(base) {
            t.ok(base == pl.base, "correct base sent in to Util.getParameters()");
            return g_Params;
        };
        
      //null center, null map.getCenter()
        g_Params = {};
        m.baseLayer = baseLayer;
        var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, []);
        t.ok(returnParams == g_Params, "correct params returned on null center");

      //valid center, zoom, layers
        g_Params = { 'test': {} };
        var center = { 'lon': 1.2345678901, 'lat': 9.8765432109 };
        var zoom = {};
        var layers = [
            { 'isBaseLayer': true },
            baseLayer,
            { 'isBaseLayer': false, 'getVisibility': function() { return true; } },
            { 'isBaseLayer': false, 'getVisibility': function() { return false; } }
        ];
        var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, [center, zoom, layers]);

        t.ok(returnParams.test == g_Params.test, "correct params returned from Util.getParameters() when valid center, zoom, layers");
        t.ok(returnParams.zoom == zoom, "params.zoom set correctly when valid center, zoom, layers");
        t.eq(returnParams.lon, 1.23457, "lon set and rounded correctly when valid center, zoom, layers");
        t.eq(returnParams.lat, 9.87654, "lat set and rounded correctly when valid center, zoom, layers");
        t.eq(returnParams.layers, "0BTF", "layers processed correctly when valid center, zoom, layers")


      //null center, zoom, layers, with displayProjection
        g_Params = { 'test': {} };
        g_Projection = {};
        m = {
            'baseLayer': baseLayer,
            'getProjectionObject':  function() { return g_Projection; },
            'center': { 'lon': {}, 'lat': {} },
            'getCenter': function() { return this.center; },
            'zoom': {},
            'getZoom': function() { return this.zoom; },
           'layers': [
                { 'isBaseLayer': false, 'getVisibility': function() { return true; } },
                baseLayer,
                { 'isBaseLayer': false, 'getVisibility': function() { return false; } },
                { 'isBaseLayer': true }
            ],
            'getLayers': function() { return this.layers; }
        };
        pl = { 
            'base': {},
            'map': m,
            'displayProjection': {}
        };
        
        old_transform = OpenLayers.Projection.transform;
        OpenLayers.Projection.transform = function(point, projObj, dispProj) {
            t.ok(point.x = m.center.lon, "correct x value passed into transform");
            t.ok(point.y = m.center.lat, "correct x value passed into transform");
            t.ok(projObj == g_Projection, "correct projection object from map passed into transform");
            t.ok(dispProj == pl.displayProjection, "correct displayProjection from control passed into transform");

            return { 'x': 9.8765432109, 'y': 1.2345678901 };
        };
        
        center = zoom = layers = null;

        var returnParams = OpenLayers.Control.Permalink.prototype.createParams.apply(pl, [center, zoom, layers]);
        t.ok(returnParams.test == g_Params.test, "correct params returned from Util.getParameters() when null center, zoom, layers, with displayProjection");
        t.ok(returnParams.zoom == m.zoom, "params.zoom set correctly when null center, zoom, layers, with displayProjection");
        t.eq(returnParams.lon, 9.87654, "lon set, transformed, and rounded correctly when null center, zoom, layers, with displayProjection");
        t.eq(returnParams.lat, 1.23457, "lat set, transformed, and rounded correctly when null center, zoom, layers, with displayProjection");
        t.eq(returnParams.layers, "TBF0", "layers processed correctly when null center, zoom, layers, with displayProjection");

        OpenLayers.Util.getParameters = old_getParameters;
        OpenLayers.Projection.transform = old_transform;
    }
    
  </script>
</head>
<body>
    <a id="permalink" href="">Permalink</a> <br />
    <a id="edit_permalink" href="">Edit</a> <br />
    <div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>