Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/svg/dom/SVGLength-px.html |
| diff --git a/third_party/WebKit/LayoutTests/svg/dom/SVGLength-px.html b/third_party/WebKit/LayoutTests/svg/dom/SVGLength-px.html |
| index 592427a7fb82a26877cdee4d548af380c7bb759c..407dc0aea15495864aecc39161a32d5c91430c90 100644 |
| --- a/third_party/WebKit/LayoutTests/svg/dom/SVGLength-px.html |
| +++ b/third_party/WebKit/LayoutTests/svg/dom/SVGLength-px.html |
| @@ -1,11 +1,130 @@ |
| -<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |
| -<html> |
| -<head> |
| -<script src="../../resources/js-test.js"></script> |
| -</head> |
| -<body> |
| +<!DOCTYPE HTML> |
| +<title>Checks SVGLength - converting from px to all other unit types</title> |
| +<script src="../../resources/testharness.js"></script> |
| +<script src="../../resources/testharnessreport.js"></script> |
| <p id="description"></p> |
| -<div id="console"></div> |
| -<script src="script-tests/SVGLength-px.js"></script> |
| -</body> |
| -</html> |
| +<script> |
| +function calculateDPI() |
| +{ |
|
Srirama
2016/08/25 05:18:51
nit: move it to previous line.
|
| + // Crude hack to determine the DPI, instead of hardcoding our 96 dpi here. |
| + var divElement = document.createElement("div"); |
| + divElement.setAttribute("style", "height: 1in"); |
| + document.getElementById("description").appendChild(divElement); |
|
Srirama
2016/08/25 05:18:51
you can directly use querySelector("p") and remove
Shanmuga Pandi
2016/08/25 07:14:52
Done.
|
| + var cssPixelsPerInch = divElement.offsetHeight; |
| + document.getElementById("description").removeChild(divElement); |
| + |
| + // Crude hack to make this test pass with Opera/Mac |
| + if (navigator.userAgent.indexOf("Opera") != -1) { |
| + if (navigator.userAgent.indexOf("Macintosh") != -1) { |
| + cssPixelsPerInch = 72; |
| + } |
| + } |
| + |
| + return cssPixelsPerInch; |
| +} |
| + |
| +test(function() { |
| + var cssPixelsPerInch = calculateDPI(); |
| + var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg"); |
| + var length = svgElement.createSVGLength(); |
| + |
| + // Set value to be 2px |
| + length.valueAsString = "2px"; |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); |
| + assert_equals(length.value, 2); |
| + assert_equals(length.valueInSpecifiedUnits, 2); |
| + assert_equals(length.valueAsString, "2px"); |
| + |
| + // Convert from px to unitless |
| + length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER); |
| + assert_equals(length.valueAsString, "2"); |
| + assert_equals(length.value, 2); |
| + assert_equals(length.valueInSpecifiedUnits, 2); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); |
| + |
| + // Reset to 2px |
| + length.valueAsString = "2px"; |
| + |
| + // Try converting from px to percentage, should fail as the SVGLength is not associated with a SVGSVGElement, and thus no viewport information is available |
| + assert_throws(null, function() { length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE); }); |
| + assert_equals(length.valueAsString, "2px"); |
| + assert_equals(length.value, 2); |
| + assert_equals(length.valueInSpecifiedUnits, 2); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); |
| + |
| + // Reset to 2px |
| + length.valueAsString = "2px"; |
| + |
| + // Try converting from px to ems, should fail as the SVGLength is not associated with a SVGSVGElement, and thus no font-size information is available |
| + assert_throws(null, function() { length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS); }); |
| + assert_equals(length.valueAsString, "2px"); |
| + assert_equals(length.value, 2); |
| + assert_equals(length.valueInSpecifiedUnits, 2); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); |
| + |
| + // Reset to 2px |
| + length.valueAsString = "2px"; |
| + |
| + // Try converting from px to exs, should fail as the SVGLength is not associated with a SVGSVGElement, and thus no font-size information is available |
| + assert_throws(null, function() { length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS); }); |
| + assert_equals(length.valueAsString, "2px"); |
| + assert_equals(length.value, 2); |
| + assert_equals(length.valueInSpecifiedUnits, 2); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); |
| + |
| + // Reset to 2px |
| + length.valueAsString = "2px"; |
| + |
| + // Convert from px to cm |
| + length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM); |
| + referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7); |
| + assert_equals(length.valueAsString, referenceValue + "cm"); |
| + assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); |
| + assert_equals(length.value.toFixed(1), "2.0"); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_CM); |
| + |
| + // Reset to 2px |
| + length.valueAsString = "2px"; |
| + |
| + // Convert from px to mm |
| + length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM); |
| + referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6); |
| + assert_equals(length.valueAsString, referenceValue + "mm"); |
| + assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); |
| + assert_equals(length.value.toFixed(1), "2.0"); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_MM); |
| + |
| + // Reset to 2px |
| + length.valueAsString = "2px"; |
| + |
| + // Convert from px to in |
| + length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN); |
| + referenceValue = Number(2 / cssPixelsPerInch).toFixed(7); |
| + assert_equals(length.valueAsString, referenceValue + "in"); |
| + assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); |
| + assert_equals(length.value.toFixed(1), "2.0"); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_IN); |
| + |
| + // Reset to 2px |
| + length.valueAsString = "2px"; |
| + |
| + // Convert from px to pt |
| + length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PT); |
| + referenceValue = Number(2 / cssPixelsPerInch * 72); |
| + assert_equals(length.valueAsString, referenceValue + "pt"); |
| + assert_equals(length.valueInSpecifiedUnits, referenceValue); |
| + assert_equals(length.value.toFixed(1), "2.0"); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PT); |
| + |
| + // Reset to 2px |
| + length.valueAsString = "2px"; |
| + |
| + // Convert from px to pc |
| + length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC); |
| + referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3); |
| + // Don't check valueAsString here, it's unreliable across browsers. |
| + assert_equals(length.valueInSpecifiedUnits.toFixed(3), referenceValue); |
| + assert_equals(length.value.toFixed(1), "2.0"); |
| + assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PC); |
| +}); |
| +</script> |