Files
openlayers/tests/test_Feature.html
crschmidt b9a04a0e9b "In Feature.js the popup is always newly created, instead of reusing the
existing popup.", reported by (and original fix provided by) Bart. Patch
reworked, and tests modified to accomodate for destruction of popup. (Closes
#815) 


git-svn-id: http://svn.openlayers.org/trunk/openlayers@4927 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2007-10-12 01:36:12 +00:00

199 lines
7.4 KiB
HTML

<html>
<head>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
var map;
var feature, layer;
function test_01_Feature_constructor (t) {
t.plan( 7 );
var layer = {};
var lonlat = new OpenLayers.LonLat(2,1);
var iconURL = 'http://boston.openguides.org/features/ORANGE.png';
var iconSize = new OpenLayers.Size(12, 17);
var data = { iconURL: iconURL,
iconSize: iconSize
};
feature = new OpenLayers.Feature(layer, lonlat, data);
t.ok( feature instanceof OpenLayers.Feature, "new OpenLayers.Feature returns Feature object" );
t.eq( feature.layer, layer, "feature.layer set correctly" );
t.ok(OpenLayers.String.startsWith(feature.id, "OpenLayers.Feature_"),
"feature.id set correctly");
t.ok( feature.lonlat.equals(lonlat), "feature.lonlat set correctly" );
t.eq( feature.data.iconURL, iconURL, "feature.data.iconURL set correctly" );
t.ok( feature.data.iconSize.equals(iconSize), "feature.data.iconSize set correctly" );
t.ok( feature.popupClass == OpenLayers.Popup.AnchoredBubble, "default popupClass is AnchoredBubble");
}
function test_02_Feature_createPopup (t) {
t.plan(1);
var layer = {};
var lonlat = new OpenLayers.LonLat(2,1);
var iconURL = 'http://boston.openguides.org/features/ORANGE.png';
var iconSize = new OpenLayers.Size(12, 17);
var data = { iconURL: iconURL,
iconSize: iconSize,
'overflow':'auto'
};
feature = new OpenLayers.Feature(layer, lonlat, data);
popup = feature.createPopup();
t.eq(popup.contentDiv.style.overflow, "auto", 'overflow on popup is correct');
}
function test_02_Feature_createMarker (t) {
t.plan(1);
t.ok(true);
/*
t.plan( 11 );
feature = new OpenLayers.Feature("myfeature", new OpenLayers.LonLat(2,1),
{
iconURL:'http://boston.openguides.org/features/ORANGE.png',
iconW: 12,
iconH: 17
});
layer = new OpenLayers.Layer.Markers('Marker Layer');
t.ok( feature instanceof OpenLayers.Feature, "new OpenLayers.Feature returns Feature object" );
t.ok( layer instanceof OpenLayers.Layer.Markers, "Layer is a marker layer" );
feature.createMarker(layer);
t.ok( feature.marker instanceof OpenLayers.Marker,
"createMarker sets a marker property to a marker" );
t.ok( layer.markers[0] === feature.marker,
"First marker in layer is the feature marker" );
t.ok( feature.marker.lonlat instanceof OpenLayers.LonLat,
"createMarker sets a marker lontlat property to a lonlat" );
t.ok( layer.markers[0].lonlat === feature.lonlat,
"First marker in the layer matches feature lonlat" );
t.ok( feature.marker.icon instanceof OpenLayers.Icon,
"createMarker sets a marker icon property to an icon" );
t.eq( feature.marker.icon.url,
"http://boston.openguides.org/features/ORANGE.png",
"createMarker sets marker url correctly" );
var map = new OpenLayers.Map('map');
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0,0),0);
t.ok( map.layers[0] == layer,
"Marker layer added to map okay." );
if (!isMozilla)
t.ok( true, "skipping element test outside of Mozilla");
else
t.ok( map.layers[0].div.firstChild instanceof HTMLImageElement,
"layer div firstChild is an image" );
t.eq( map.layers[0].div.firstChild.src,
"http://boston.openguides.org/features/ORANGE.png",
"Layer div img contains correct url" );
*/
}
function test_03_Feature_onScreen(t) {
t.plan( 2 );
var map = new OpenLayers.Map("map");
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
var wms = new OpenLayers.Layer.WMS(name, url);
map.addLayer(wms);
var layer = new OpenLayers.Layer("foo");
map.addLayer(layer);
map.zoomToExtent(new OpenLayers.Bounds(-50,-50,50,50));
//onscreen feature
var feature1 = new OpenLayers.Feature(layer,
new OpenLayers.LonLat(0,0));
t.ok( feature1.onScreen(), "feature knows it's onscreen" );
//onscreen feature
var feature2 = new OpenLayers.Feature(layer,
new OpenLayers.LonLat(100,100));
t.ok( !feature2.onScreen(), "feature knows it's offscreen" );
}
function test_04_Feature_createPopup(t) {
t.plan(11);
//no lonlat
var f = {
'popup': null
};
var ret = OpenLayers.Feature.prototype.createPopup.apply(f, []);
t.ok((ret == f.popup) && (f.popup == null), "if no 'lonlat' set on feature, still returns reference to this.popup (though it is null)");
f.popupClass = OpenLayers.Class({
initialize: function(id, lonlat, size, contentHTML, anchor, closeBox) {
t.eq(id, "Campion_popup", "correctly generates new popup id from feature's id");
t.eq(lonlat, f.lonlat, "correctly passes feature's lonlat to popup constructor");
t.eq(size, f.data.popupSize, "correctly passes feature's data.popupSize to popup constructor");
t.eq(contentHTML, f.data.popupContentHTML, "correctly passes feature's data.popupContentHTML to popup constructor");
t.eq(anchor, g_ExpectedAnchor, "passes correct anchor to popup constructor");
t.eq(closeBox, g_CloseBox, "correctly relays closeBox argument to popup constructor");
}
});
//valid lonlat but no anchor
f.popup = null;
f.id = "Campion";
f.lonlat = {};
f.data = {
'popupSize': {},
'popupContentHTML': {}
};
g_ExpectedAnchor = null;
g_CloseBox = {};
ret = OpenLayers.Feature.prototype.createPopup.apply(f, [g_CloseBox]);
t.ok((ret == f.popup) && (f.popup != null), "a valid popup has been set and returned")
t.ok( f.popup.feature == f, "popup's 'feature' property correctly set");
//valid lonlat with anchor
f.marker = {
'icon': {}
};
g_ExpectedAnchor = f.marker.icon;
ret = OpenLayers.Feature.prototype.createPopup.apply(f, [g_CloseBox]);
t.ok((ret == f.popup) && (f.popup != null), "a valid popup has been set and returned")
t.ok( f.popup.feature == f, "popup's 'feature' property correctly set");
}
function test_04_Feature_destroyPopup(t) {
t.plan(2);
var f = {
'popup': {
'feature': {},
'destroy': function() {
t.ok(true, "default destroyPopup() calls popup.destroy");
}
}
};
OpenLayers.Feature.prototype.destroyPopup.apply(f, []);
t.ok(f.popup == null, "popup property nullified on destroy");
}
</script>
</head>
<body>
<div id="map" style="width: 500px; height: 300px;"></div>
</body>
</html>