Merge pull request #699 from bartvde/kmlxmleql3

Use xmleql in the KML tests (r=@ahocevar)
This commit is contained in:
Bart van den Eijnden
2013-05-10 03:14:35 -07:00
7 changed files with 62 additions and 127 deletions
+28 -20
View File
@@ -250,9 +250,10 @@
// for text nodes compare value // for text nodes compare value
if (node1.nodeType === 3) { if (node1.nodeType === 3) {
try { try {
expect(node1.nodeValue).to.equal(node2.nodeValue); // TODO should we make this optional?
expect(node1.nodeValue.replace(/\s/g, '')).to.equal(node2.nodeValue.replace(/\s/g, ''));
} catch(e) { } catch(e) {
errors.push('nodeValue test failed for: ' + node1.nodeName + ' | ' + e.message); errors.push('nodeValue test failed | ' + e.message);
} }
} }
// for element type nodes compare namespace, attributes, and children // for element type nodes compare namespace, attributes, and children
@@ -281,23 +282,27 @@
var node2Attr = {}; var node2Attr = {};
var ga, ea, gn, en; var ga, ea, gn, en;
var i, ii; var i, ii;
for (i=0, ii=node1.attributes.length; i<ii; ++i) { if (node1.attributes) {
ga = node1.attributes[i]; for (i=0, ii=node1.attributes.length; i<ii; ++i) {
if (ga.specified === undefined || ga.specified === true) { ga = node1.attributes[i];
if (ga.name.split(':').shift() != 'xmlns') { if (ga.specified === undefined || ga.specified === true) {
gn = testPrefix ? ga.name : ga.name.split(':').pop(); if (ga.name.split(':').shift() != 'xmlns') {
node1Attr[gn] = ga; gn = testPrefix ? ga.name : ga.name.split(':').pop();
++node1AttrLen; node1Attr[gn] = ga;
++node1AttrLen;
}
} }
} }
} }
for (i=0, ii=node2.attributes.length; i<ii; ++i) { if (node2.attributes) {
ea = node2.attributes[i]; for (i=0, ii=node2.attributes.length; i<ii; ++i) {
if (ea.specified === undefined || ea.specified === true) { ea = node2.attributes[i];
if (ea.name.split(':').shift() != 'xmlns') { if (ea.specified === undefined || ea.specified === true) {
en = testPrefix ? ea.name : ea.name.split(':').pop(); if (ea.name.split(':').shift() != 'xmlns') {
node2Attr[en] = ea; en = testPrefix ? ea.name : ea.name.split(':').pop();
++node2AttrLen; node2Attr[en] = ea;
++node2AttrLen;
}
} }
} }
} }
@@ -331,8 +336,11 @@
} catch(e) { } catch(e) {
errors.push('Number of childNodes test failed for: ' + node1.nodeName + ' | ' + e.message); errors.push('Number of childNodes test failed for: ' + node1.nodeName + ' | ' + e.message);
} }
for (var j=0, jj=node1ChildNodes.length; j<jj; ++j) { // only compare if they are equal
assertElementNodesEqual(node1ChildNodes[j], node2ChildNodes[j], options, errors); if (node1ChildNodes.length === node2ChildNodes.length) {
for (var j=0, jj=node1ChildNodes.length; j<jj; ++j) {
assertElementNodesEqual(node1ChildNodes[j], node2ChildNodes[j], options, errors);
}
} }
} }
}; };
@@ -715,9 +723,9 @@
var ns = "http://www.w3.org/1999/xhtml"; var ns = "http://www.w3.org/1999/xhtml";
var container = document.createElementNS(ns, '_'); var container = document.createElementNS(ns, '_');
var elemProto = (window.HTMLElement || window.Element).prototype; var elemProto = (window.HTMLElement || window.Element).prototype;
var xmlSerializer = new XMLSerializer();
var html; var html;
if (document.xmlVersion) { if (typeof XMLSerializer != 'undefined') {
var xmlSerializer = new XMLSerializer();
return xmlSerializer.serializeToString(element); return xmlSerializer.serializeToString(element);
} else { } else {
container.appendChild(element.cloneNode(false)); container.appendChild(element.cloneNode(false));
+22 -76
View File
@@ -10,22 +10,7 @@ describe('ol.parser.kml', function() {
afterLoadXml(url, function(xml) { afterLoadXml(url, function(xml) {
var obj = parser.read(xml); var obj = parser.read(xml);
var output = parser.write(obj); var output = parser.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' + expect(xml).to.xmleql(goog.dom.xml.loadXml(output));
'<Document><name>Polygon.kml</name><open>0</open><Placemark>' +
'<name>hollow box</name><Polygon><outerBoundaryIs><LinearRing>' +
'<coordinates>-122.366278,37.818844,30 ' +
'-122.365248,37.819267,30 ' +
'-122.36564,37.819861,30 ' +
'-122.366669,37.819429,30 ' +
'-122.366278,37.818844,30</coordinates></LinearRing>' +
'</outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>' +
'-122.366212,37.818977,30 ' +
'-122.365424,37.819294,30 ' +
'-122.365704,37.819731,30 ' +
'-122.366488,37.819402,30 ' +
'-122.366212,37.818977,30</coordinates></LinearRing>' +
'</innerBoundaryIs></Polygon></Placemark></Document></kml>';
expect(output).to.eql(expected);
expect(obj.features.length).to.eql(1); expect(obj.features.length).to.eql(1);
var geom = obj.features[0].getGeometry(); var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.Polygon).to.be.ok(); expect(geom instanceof ol.geom.Polygon).to.be.ok();
@@ -37,14 +22,7 @@ describe('ol.parser.kml', function() {
afterLoadXml(url, function(xml) { afterLoadXml(url, function(xml) {
var obj = parser.read(xml); var obj = parser.read(xml);
var output = parser.write(obj); var output = parser.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' + expect(xml).to.xmleql(goog.dom.xml.loadXml(output));
'<Document><name>LineString.kml</name><open>1</open><Placemark>' +
'<name>unextruded</name><LineString><coordinates>-122.364383,' +
'37.824664,0 -122.364152,37.824322,0</coordinates></LineString>' +
'</Placemark><Placemark><name>extruded</name><LineString>' +
'<coordinates>-122.364167,37.824787,50 -122.363917,37.824423,50' +
'</coordinates></LineString></Placemark></Document></kml>';
expect(output).to.eql(expected);
expect(obj.features.length).to.eql(2); expect(obj.features.length).to.eql(2);
var geom = obj.features[0].getGeometry(); var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.LineString).to.be.ok(); expect(geom instanceof ol.geom.LineString).to.be.ok();
@@ -58,13 +36,7 @@ describe('ol.parser.kml', function() {
afterLoadXml(url, function(xml) { afterLoadXml(url, function(xml) {
var obj = parser.read(xml); var obj = parser.read(xml);
var output = parser.write(obj); var output = parser.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' + expect(xml).to.xmleql(goog.dom.xml.loadXml(output));
'<Document><Placemark><name>Simple placemark</name><description>' +
'Attached to the ground. Intelligently places itself \n' +
' at the height of the underlying terrain.</description>' +
'<Point><coordinates>-122.0822035425683,37.42228990140251,0' +
'</coordinates></Point></Placemark></Document></kml>';
expect(output).to.eql(expected);
expect(obj.features.length).to.eql(1); expect(obj.features.length).to.eql(1);
var geom = obj.features[0].getGeometry(); var geom = obj.features[0].getGeometry();
expect(geom instanceof ol.geom.Point).to.be.ok(); expect(geom instanceof ol.geom.Point).to.be.ok();
@@ -131,17 +103,8 @@ describe('ol.parser.kml', function() {
afterLoadXml(url, function(xml) { afterLoadXml(url, function(xml) {
var obj = parser.read(xml); var obj = parser.read(xml);
var geom = obj.features[0].getGeometry(); var geom = obj.features[0].getGeometry();
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document><name>Polygon.kml</name><open>0</open><Placemark>' +
'<name>SF Marina Harbor Master</name><MultiGeometry><LineString>' +
'<coordinates>-122.4425587930444,37.80666418607323,0 ' +
'-122.4428379594768,37.80663578323093,0</coordinates>' +
'</LineString><LineString><coordinates>-122.4425509770566,' +
'37.80662588061205,0 -122.4428340530617,37.8065999493009,0' +
'</coordinates></LineString></MultiGeometry></Placemark>' +
'</Document></kml>';
var output = parser.write(obj); var output = parser.write(obj);
expect(output).to.eql(expected); expect(xml).to.xmleql(goog.dom.xml.loadXml(output));
expect(geom instanceof ol.geom.MultiLineString).to.be.ok(); expect(geom instanceof ol.geom.MultiLineString).to.be.ok();
}); });
}); });
@@ -193,20 +156,16 @@ describe('ol.parser.kml', function() {
expect(obj.features[0].get('name')).to.eql('Pezinok'); expect(obj.features[0].get('name')).to.eql('Pezinok');
}); });
it('Test line style', function() { it('Test line style', function() {
var test_style = '<kml xmlns="http://earth.google.com/kml/2.0"> ' + var test_style = '<kml xmlns="http://www.opengis.net/kml/2.2"> ' +
'<Placemark> <Style> <LineStyle> <color>870000ff</color> ' + '<Document><Placemark><Style><LineStyle> <color>870000ff</color> ' +
'<width>10</width> </LineStyle> </Style> <LineString> ' + '<width>10</width> </LineStyle> </Style> <LineString> ' +
'<coordinates> -112,36 -113,37 </coordinates> </LineString>' + '<coordinates> -112,36 -113,37 </coordinates> </LineString>' +
'</Placemark></kml>'; '</Placemark></Document></kml>';
var p = new ol.parser.KML({extractStyles: true}); var p = new ol.parser.KML({extractStyles: true});
var obj = p.read(test_style); var obj = p.read(test_style);
var output = p.write(obj); var output = p.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' + expect(goog.dom.xml.loadXml(test_style)).to.xmleql(
'<Document><Placemark><Style><LineStyle><color>870000ff</color>' + goog.dom.xml.loadXml(output));
'<width>10</width></LineStyle></Style><LineString><coordinates>' +
'-112,36 -113,37</coordinates></LineString></Placemark>' +
'</Document></kml>';
expect(output).to.eql(expected);
var symbolizer = obj.features[0].getSymbolizerLiterals()[0]; var symbolizer = obj.features[0].getSymbolizerLiterals()[0];
expect(symbolizer instanceof ol.style.LineLiteral).to.be.ok(); expect(symbolizer instanceof ol.style.LineLiteral).to.be.ok();
expect(symbolizer.strokeColor).to.eql('#ff0000'); expect(symbolizer.strokeColor).to.eql('#ff0000');
@@ -214,8 +173,8 @@ describe('ol.parser.kml', function() {
expect(symbolizer.strokeWidth).to.eql(10); expect(symbolizer.strokeWidth).to.eql(10);
}); });
it('Test style fill', function() { it('Test style fill', function() {
var test_style_fill = '<kml xmlns="http://earth.google.com/kml/2.0"> ' + var test_style_fill = '<kml xmlns="http://www.opengis.net/kml/2.2"> ' +
'<Placemark> <Style> <PolyStyle> <fill>1</fill> ' + '<Document><Placemark> <Style> <PolyStyle> <fill>1</fill> ' +
'<color>870000ff</color> <width>10</width> </PolyStyle> </Style>' + '<color>870000ff</color> <width>10</width> </PolyStyle> </Style>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' + '<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'5.001370157823406,49.26855713824488 8.214706453896161,' + '5.001370157823406,49.26855713824488 8.214706453896161,' +
@@ -228,25 +187,20 @@ describe('ol.parser.kml', function() {
'5.001370157823406,49.26855713824488 8.214706453896161,' + '5.001370157823406,49.26855713824488 8.214706453896161,' +
'49.630662409673505 8.397385910100951,48.45172350357396 ' + '49.630662409673505 8.397385910100951,48.45172350357396 ' +
'5.001370157823406,49.26855713824488</coordinates></LinearRing>' + '5.001370157823406,49.26855713824488</coordinates></LinearRing>' +
'</outerBoundaryIs></Polygon></Placemark></kml>'; '</outerBoundaryIs></Polygon></Placemark></Document></kml>';
var p = new ol.parser.KML({extractStyles: true}); var style_fill_write = '<kml xmlns="http://www.opengis.net/kml/2.2"> ' +
var obj = p.read(test_style_fill); '<Document><Placemark> <Style> <PolyStyle> <fill>1</fill> ' +
var output = p.write(obj); '<color>870000ff</color> <width>10</width> </PolyStyle> </Style>' +
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2"><Document>' +
'<Placemark><Style><PolyStyle><fill>1</fill><color>870000ff' +
'</color><width>10</width></PolyStyle></Style><Polygon>' +
'<outerBoundaryIs><LinearRing><coordinates>5.001370157823406,' +
'49.26855713824488 8.214706453896161,49.630662409673505 ' +
'8.397385910100951,48.45172350357396 5.001370157823406,' +
'49.26855713824488</coordinates></LinearRing></outerBoundaryIs>' +
'</Polygon></Placemark><Placemark><Style><PolyStyle><fill>0</fill>' +
'<color>00000ff</color><width>10</width></PolyStyle></Style>' +
'<Polygon><outerBoundaryIs><LinearRing><coordinates>' + '<Polygon><outerBoundaryIs><LinearRing><coordinates>' +
'5.001370157823406,49.26855713824488 8.214706453896161,' + '5.001370157823406,49.26855713824488 8.214706453896161,' +
'49.630662409673505 8.397385910100951,48.45172350357396 ' + '49.630662409673505 8.397385910100951,48.45172350357396 ' +
'5.001370157823406,49.26855713824488</coordinates></LinearRing>' + '5.001370157823406,49.26855713824488</coordinates></LinearRing>' +
'</outerBoundaryIs></Polygon></Placemark></Document></kml>'; '</outerBoundaryIs></Polygon></Placemark></Document></kml>';
expect(output).to.eql(expected); var p = new ol.parser.KML({extractStyles: true});
var obj = p.read(test_style_fill);
var output = p.write(p.read(style_fill_write));
expect(goog.dom.xml.loadXml(style_fill_write)).to.xmleql(
goog.dom.xml.loadXml(output));
var symbolizer1 = obj.features[0].getSymbolizerLiterals()[0]; var symbolizer1 = obj.features[0].getSymbolizerLiterals()[0];
var symbolizer2 = obj.features[1].getSymbolizerLiterals()[0]; var symbolizer2 = obj.features[1].getSymbolizerLiterals()[0];
expect(symbolizer1.fillColor).to.eql('#ff0000'); expect(symbolizer1.fillColor).to.eql('#ff0000');
@@ -258,15 +212,7 @@ describe('ol.parser.kml', function() {
var p = new ol.parser.KML({extractStyles: true}); var p = new ol.parser.KML({extractStyles: true});
var obj = p.read(xml); var obj = p.read(xml);
var output = p.write(obj); var output = p.write(obj);
var expected = '<kml xmlns="http://www.opengis.net/kml/2.2">' + expect(xml).to.xmleql(goog.dom.xml.loadXml(output));
'<Document><Style id="pushpin"><IconStyle id="mystyle">' +
'<Icon><href>http://maps.google.com/mapfiles/kml/pushpin/' +
'ylw-pushpin.png</href></Icon></IconStyle></Style>' +
'<Placemark><name>Pin on a mountaintop</name><styleUrl>' +
'#pushpin</styleUrl><Point><coordinates>170.1435558771009,' +
'-43.60505741890396,0</coordinates></Point></Placemark>' +
'</Document></kml>';
expect(output).to.eql(expected);
var symbolizer = obj.features[0].getSymbolizerLiterals()[0]; var symbolizer = obj.features[0].getSymbolizerLiterals()[0];
var url = 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png'; var url = 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png';
expect(symbolizer.url).to.eql(url); expect(symbolizer.url).to.eql(url);
@@ -277,7 +223,7 @@ describe('ol.parser.kml', function() {
}); });
}); });
goog.require('goog.net.XhrIo'); goog.require('goog.dom.xml');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.geom.GeometryCollection'); goog.require('ol.geom.GeometryCollection');
+1 -6
View File
@@ -2,17 +2,12 @@
<Document> <Document>
<Style id="pushpin"> <Style id="pushpin">
<IconStyle id="mystyle"> <IconStyle id="mystyle">
<color>ffffffff</color>
<colorMode>normal</colorMode>
<scale>1</scale>
<heading>0</heading>
<Icon> <Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href> <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon> </Icon>
<hotSpot x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
</IconStyle> </IconStyle>
</Style> </Style>
<Placemark id="mountainpin1"> <Placemark>
<name>Pin on a mountaintop</name> <name>Pin on a mountaintop</name>
<styleUrl>#pushpin</styleUrl> <styleUrl>#pushpin</styleUrl>
<Point> <Point>
-13
View File
@@ -3,19 +3,9 @@
<Document> <Document>
<name>LineString.kml</name> <name>LineString.kml</name>
<open>1</open> <open>1</open>
<LookAt>
<longitude>-122.36415</longitude>
<latitude>37.824553</latitude>
<altitude>0</altitude>
<range>150</range>
<tilt>50</tilt>
<heading>0</heading>
</LookAt>
<Placemark> <Placemark>
<name>unextruded</name> <name>unextruded</name>
<LineString> <LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<coordinates> <coordinates>
-122.364383,37.824664,0 -122.364152,37.824322,0 -122.364383,37.824664,0 -122.364152,37.824322,0
</coordinates> </coordinates>
@@ -24,9 +14,6 @@
<Placemark> <Placemark>
<name>extruded</name> <name>extruded</name>
<LineString> <LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>relativeToGround</altitudeMode>
<coordinates> <coordinates>
-122.364167,37.824787,50 -122.363917,37.824423,50 -122.364167,37.824787,50 -122.363917,37.824423,50
</coordinates> </coordinates>
@@ -5,7 +5,6 @@
<open>0</open> <open>0</open>
<Placemark> <Placemark>
<name>SF Marina Harbor Master</name> <name>SF Marina Harbor Master</name>
<visibility>0</visibility>
<MultiGeometry> <MultiGeometry>
<LineString> <LineString>
<!-- north wall --> <!-- north wall -->
+10 -8
View File
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> <kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark> <Document>
<name>Simple placemark</name> <Placemark>
<description>Attached to the ground. Intelligently places itself <name>Simple placemark</name>
at the height of the underlying terrain.</description> <description>Attached to the ground. Intelligently places itself
<Point> at the height of the underlying terrain.</description>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates> <Point>
</Point> <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Placemark> </Point>
</Placemark>
</Document>
</kml> </kml>
+1 -3
View File
@@ -6,14 +6,12 @@
<Placemark> <Placemark>
<name>hollow box</name> <name>hollow box</name>
<Polygon> <Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs> <outerBoundaryIs>
<LinearRing> <LinearRing>
<coordinates> <coordinates>
-122.366278,37.818844,30 -122.366278,37.818844,30
-122.365248,37.819267,30 -122.365248,37.819267,30
-122.365640,37.819861,30 -122.36564,37.819861,30
-122.366669,37.819429,30 -122.366669,37.819429,30
-122.366278,37.818844,30 -122.366278,37.818844,30
</coordinates> </coordinates>