OLD | NEW |
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> | 1 <!DOCTYPE HTML> |
2 <html> | 2 <title>SVGLength, converting from 'px' to other units (detached)</title> |
3 <head> | 3 <script src="../../resources/testharness.js"></script> |
4 <script src="../../resources/js-test.js"></script> | 4 <script src="../../resources/testharnessreport.js"></script> |
5 </head> | 5 <script> |
6 <body> | 6 var cssPixelsPerInch = 96; |
7 <p id="description"></p> | 7 setup(function() { |
8 <div id="console"></div> | 8 var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg")
; |
9 <script src="script-tests/SVGLength-px.js"></script> | 9 window.length = svgElement.createSVGLength(); |
10 </body> | 10 }); |
11 </html> | 11 |
| 12 test(function() { |
| 13 length.valueAsString = "2px"; |
| 14 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER); |
| 15 assert_equals(length.valueAsString, "2"); |
| 16 assert_equals(length.value, 2); |
| 17 assert_equals(length.valueInSpecifiedUnits, 2); |
| 18 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); |
| 19 }, document.title + ", unitless"); |
| 20 |
| 21 test(function() { |
| 22 length.valueAsString = "2px"; |
| 23 // Try converting from px to percentage, should fail as the SVGLength is not a
ssociated with a SVGSVGElement, and thus no viewport information is available. |
| 24 assert_throws("NotSupportedError", function() { length.convertToSpecifiedUnits
(SVGLength.SVG_LENGTHTYPE_PERCENTAGE); }); |
| 25 assert_equals(length.valueAsString, "2px"); |
| 26 assert_equals(length.value, 2); |
| 27 assert_equals(length.valueInSpecifiedUnits, 2); |
| 28 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); |
| 29 }, document.title + ", percentage"); |
| 30 |
| 31 test(function() { |
| 32 length.valueAsString = "2px"; |
| 33 // Try converting from px to ems, should fail as the SVGLength is not associat
ed with a SVGSVGElement, and thus no font-size information is available. |
| 34 assert_throws("NotSupportedError", function() { length.convertToSpecifiedUnits
(SVGLength.SVG_LENGTHTYPE_EMS); }); |
| 35 assert_equals(length.valueAsString, "2px"); |
| 36 assert_equals(length.value, 2); |
| 37 assert_equals(length.valueInSpecifiedUnits, 2); |
| 38 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); |
| 39 }, document.title + ", ems"); |
| 40 |
| 41 test(function() { |
| 42 length.valueAsString = "2px"; |
| 43 // Try converting from px to exs, should fail as the SVGLength is not associat
ed with a SVGSVGElement, and thus no font-size information is available. |
| 44 assert_throws("NotSupportedError", function() { length.convertToSpecifiedUnits
(SVGLength.SVG_LENGTHTYPE_EXS); }); |
| 45 assert_equals(length.valueAsString, "2px"); |
| 46 assert_equals(length.value, 2); |
| 47 assert_equals(length.valueInSpecifiedUnits, 2); |
| 48 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); |
| 49 }, document.title + ", exs"); |
| 50 |
| 51 test(function() { |
| 52 length.valueAsString = "2px"; |
| 53 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM); |
| 54 referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7); |
| 55 assert_equals(length.valueAsString, referenceValue + "cm"); |
| 56 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); |
| 57 assert_equals(length.value.toFixed(1), "2.0"); |
| 58 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_CM); |
| 59 }, document.title + ", cm"); |
| 60 |
| 61 test(function() { |
| 62 length.valueAsString = "2px"; |
| 63 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM); |
| 64 referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6); |
| 65 assert_equals(length.valueAsString, referenceValue + "mm"); |
| 66 assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); |
| 67 assert_equals(length.value.toFixed(1), "2.0"); |
| 68 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_MM); |
| 69 }, document.title + ", mm"); |
| 70 |
| 71 test(function() { |
| 72 length.valueAsString = "2px"; |
| 73 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN); |
| 74 referenceValue = Number(2 / cssPixelsPerInch).toFixed(7); |
| 75 assert_equals(length.valueAsString, referenceValue + "in"); |
| 76 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); |
| 77 assert_equals(length.value.toFixed(1), "2.0"); |
| 78 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_IN); |
| 79 }, document.title + ", in"); |
| 80 |
| 81 test(function() { |
| 82 length.valueAsString = "2px"; |
| 83 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PT); |
| 84 referenceValue = Number(2 / cssPixelsPerInch * 72); |
| 85 assert_equals(length.valueAsString, referenceValue + "pt"); |
| 86 assert_equals(length.valueInSpecifiedUnits, referenceValue); |
| 87 assert_equals(length.value.toFixed(1), "2.0"); |
| 88 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PT); |
| 89 }, document.title + ", pt"); |
| 90 |
| 91 test(function() { |
| 92 length.valueAsString = "2px"; |
| 93 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC); |
| 94 referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3); |
| 95 // Don't check valueAsString here, it's unreliable across browsers. |
| 96 assert_equals(length.valueInSpecifiedUnits.toFixed(3), referenceValue); |
| 97 assert_equals(length.value.toFixed(1), "2.0"); |
| 98 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PC); |
| 99 }, document.title + ", pc"); |
| 100 </script> |
OLD | NEW |