OLD | NEW |
---|---|
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> | 1 <!DOCTYPE HTML> |
2 <html> | 2 <title>Checks SVGLength - converting from px to all other unit types</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> | |
6 <body> | |
7 <p id="description"></p> | 5 <p id="description"></p> |
8 <div id="console"></div> | 6 <script> |
9 <script src="script-tests/SVGLength-px.js"></script> | 7 function calculateDPI() |
10 </body> | 8 { |
Srirama
2016/08/25 05:18:51
nit: move it to previous line.
| |
11 </html> | 9 // Crude hack to determine the DPI, instead of hardcoding our 96 dpi here. |
10 var divElement = document.createElement("div"); | |
11 divElement.setAttribute("style", "height: 1in"); | |
12 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.
| |
13 var cssPixelsPerInch = divElement.offsetHeight; | |
14 document.getElementById("description").removeChild(divElement); | |
15 | |
16 // Crude hack to make this test pass with Opera/Mac | |
17 if (navigator.userAgent.indexOf("Opera") != -1) { | |
18 if (navigator.userAgent.indexOf("Macintosh") != -1) { | |
19 cssPixelsPerInch = 72; | |
20 } | |
21 } | |
22 | |
23 return cssPixelsPerInch; | |
24 } | |
25 | |
26 test(function() { | |
27 var cssPixelsPerInch = calculateDPI(); | |
28 var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg") ; | |
29 var length = svgElement.createSVGLength(); | |
30 | |
31 // Set value to be 2px | |
32 length.valueAsString = "2px"; | |
33 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); | |
34 assert_equals(length.value, 2); | |
35 assert_equals(length.valueInSpecifiedUnits, 2); | |
36 assert_equals(length.valueAsString, "2px"); | |
37 | |
38 // Convert from px to unitless | |
39 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBER); | |
40 assert_equals(length.valueAsString, "2"); | |
41 assert_equals(length.value, 2); | |
42 assert_equals(length.valueInSpecifiedUnits, 2); | |
43 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_NUMBER); | |
44 | |
45 // Reset to 2px | |
46 length.valueAsString = "2px"; | |
47 | |
48 // 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 | |
49 assert_throws(null, function() { length.convertToSpecifiedUnits(SVGLength.SVG_ LENGTHTYPE_PERCENTAGE); }); | |
50 assert_equals(length.valueAsString, "2px"); | |
51 assert_equals(length.value, 2); | |
52 assert_equals(length.valueInSpecifiedUnits, 2); | |
53 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); | |
54 | |
55 // Reset to 2px | |
56 length.valueAsString = "2px"; | |
57 | |
58 // 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 | |
59 assert_throws(null, function() { length.convertToSpecifiedUnits(SVGLength.SVG_ LENGTHTYPE_EMS); }); | |
60 assert_equals(length.valueAsString, "2px"); | |
61 assert_equals(length.value, 2); | |
62 assert_equals(length.valueInSpecifiedUnits, 2); | |
63 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); | |
64 | |
65 // Reset to 2px | |
66 length.valueAsString = "2px"; | |
67 | |
68 // 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 | |
69 assert_throws(null, function() { length.convertToSpecifiedUnits(SVGLength.SVG_ LENGTHTYPE_EXS); }); | |
70 assert_equals(length.valueAsString, "2px"); | |
71 assert_equals(length.value, 2); | |
72 assert_equals(length.valueInSpecifiedUnits, 2); | |
73 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PX); | |
74 | |
75 // Reset to 2px | |
76 length.valueAsString = "2px"; | |
77 | |
78 // Convert from px to cm | |
79 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM); | |
80 referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7); | |
81 assert_equals(length.valueAsString, referenceValue + "cm"); | |
82 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); | |
83 assert_equals(length.value.toFixed(1), "2.0"); | |
84 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_CM); | |
85 | |
86 // Reset to 2px | |
87 length.valueAsString = "2px"; | |
88 | |
89 // Convert from px to mm | |
90 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM); | |
91 referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6); | |
92 assert_equals(length.valueAsString, referenceValue + "mm"); | |
93 assert_equals(length.valueInSpecifiedUnits.toFixed(6), referenceValue); | |
94 assert_equals(length.value.toFixed(1), "2.0"); | |
95 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_MM); | |
96 | |
97 // Reset to 2px | |
98 length.valueAsString = "2px"; | |
99 | |
100 // Convert from px to in | |
101 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN); | |
102 referenceValue = Number(2 / cssPixelsPerInch).toFixed(7); | |
103 assert_equals(length.valueAsString, referenceValue + "in"); | |
104 assert_equals(length.valueInSpecifiedUnits.toFixed(7), referenceValue); | |
105 assert_equals(length.value.toFixed(1), "2.0"); | |
106 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_IN); | |
107 | |
108 // Reset to 2px | |
109 length.valueAsString = "2px"; | |
110 | |
111 // Convert from px to pt | |
112 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PT); | |
113 referenceValue = Number(2 / cssPixelsPerInch * 72); | |
114 assert_equals(length.valueAsString, referenceValue + "pt"); | |
115 assert_equals(length.valueInSpecifiedUnits, referenceValue); | |
116 assert_equals(length.value.toFixed(1), "2.0"); | |
117 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PT); | |
118 | |
119 // Reset to 2px | |
120 length.valueAsString = "2px"; | |
121 | |
122 // Convert from px to pc | |
123 length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC); | |
124 referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3); | |
125 // Don't check valueAsString here, it's unreliable across browsers. | |
126 assert_equals(length.valueInSpecifiedUnits.toFixed(3), referenceValue); | |
127 assert_equals(length.value.toFixed(1), "2.0"); | |
128 assert_equals(length.unitType, SVGLength.SVG_LENGTHTYPE_PC); | |
129 }); | |
130 </script> | |
OLD | NEW |