Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Unified Diff: third_party/WebKit/LayoutTests/svg/dom/SVGLength-px-with-context.html

Issue 2271223002: Convert LayoutTests/svg/dom/SVGLength*.html js-tests.js tests to testharness.js based tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/svg/dom/SVGLength-px-with-context.html
diff --git a/third_party/WebKit/LayoutTests/svg/dom/SVGLength-px-with-context.html b/third_party/WebKit/LayoutTests/svg/dom/SVGLength-px-with-context.html
index aacdecdaea8e63306c9502e60e19856d610892dd..0cd2101af3e0faf9652b460abfc07da569873dce 100644
--- a/third_party/WebKit/LayoutTests/svg/dom/SVGLength-px-with-context.html
+++ b/third_party/WebKit/LayoutTests/svg/dom/SVGLength-px-with-context.html
@@ -1,11 +1,157 @@
-<!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-with-context.js"></script>
-</body>
-</html>
+<script>
+function calculateDPI()
+{
Srirama 2016/08/25 05:18:51 nit: move it to previous line.
Shanmuga Pandi 2016/08/25 07:14:52 Done.
+ // Crude hack to determine the DPI, instead of hardcoding our 96 dpi here.
+ var divElement = document.createElement("div");
Srirama 2016/08/25 05:18:51 same comments as in previous test to use querysele
Shanmuga Pandi 2016/08/25 07:14:52 Done.
+ divElement.setAttribute("style", "height: 1in");
+ document.getElementById("description").appendChild(divElement);
+ 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;
+}
+
+function calculateXHeight()
+{
Srirama 2016/08/25 05:18:51 nit:move it to previous line.
Shanmuga Pandi 2016/08/25 07:14:52 Done.
+ // Crude hack to calculate the x-height
+ var divElement = document.createElement("div");
+ divElement.setAttribute("style", "height: 1ex; font-size: 12px; font-family: Ahem;");
+ document.getElementById("description").appendChild(divElement);
Srirama 2016/08/25 05:18:51 same here as well.
Shanmuga Pandi 2016/08/25 07:14:52 Done.
+ var xHeight = divElement.offsetHeight;
+ document.getElementById("description").removeChild(divElement);
+ return xHeight;
+}
+
+test(function() {
+ // Setup a real SVG document, so SVGLength can resolve relative units.
+ var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg");
+ svgElement.setAttribute("width", "150");
+ svgElement.setAttribute("height", "50");
+
+ var rectElement = document.createElementNS("http://www.w3.org/2000/svg", "rect");
+ rectElement.setAttribute("style", "visibility: hidden; font-size: 12px; font-family: Ahem;");
+ svgElement.appendChild(rectElement);
+ document.getElementById("description").appendChild(svgElement);
+
+ // Extract test information
+ var length = rectElement.x.baseVal;
+ var svgWidth = svgElement.width.baseVal.value;
+ var svgHeight = svgElement.height.baseVal.value;
+ var fontSize = parseInt(rectElement.style.fontSize);
+ var cssPixelsPerInch = calculateDPI();
+
+ // 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";
+
+ // Convert from px to percentage
+ length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE);
+ referenceValue = Number(2 / svgWidth * 100).toFixed(5);
+ assert_equals(length.valueAsString, referenceValue + "%");
+ assert_equals(length.valueInSpecifiedUnits.toFixed(5), referenceValue);
+ assert_equals(length.value.toFixed(1), "2.0");
+ assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PERCENTAGE);
+
+ // Reset to 2px
+ length.valueAsString = "2px";
+
+ // Convert from px to ems
+ length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS);
+ referenceValue = Number(2 / fontSize).toFixed(6);
+ assert_equals(length.valueAsString, referenceValue + "em");
+ assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue);
+ assert_equals(length.value.toFixed(1), "2.0");
+ assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_EMS);
+
+ // Reset to 2px
+ length.valueAsString = "2px";
+
+ // Convert from px to exs
+ length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS);
+ referenceValue = Number(2 / calculateXHeight()).toFixed(1);
+ // Don't check valueAsString here, it's unreliable across browsers.
+ assert_equals(length.valueInSpecifiedUnits.toFixed(1), referenceValue);
+ assert_equals(length.value.toFixed(1), "2.0");
+ assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_EXS);
+
+ // 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>

Powered by Google App Engine
This is Rietveld 408576698