OLD | NEW |
| (Empty) |
1 description("This test checks SVGLength - converting from px to all other unit t
ypes"); | |
2 | |
3 function calculateDPI() | |
4 { | |
5 // Crude hack to determine the DPI, instead of hardcoding our 96 dpi here. | |
6 var divElement = document.createElement("div"); | |
7 divElement.setAttribute("style", "height: 1in"); | |
8 document.getElementById("description").appendChild(divElement); | |
9 var cssPixelsPerInch = divElement.offsetHeight; | |
10 document.getElementById("description").removeChild(divElement); | |
11 | |
12 // Crude hack to make this test pass with Opera/Mac | |
13 if (navigator.userAgent.indexOf("Opera") != -1) { | |
14 if (navigator.userAgent.indexOf("Macintosh") != -1) { | |
15 cssPixelsPerInch = 72; | |
16 } | |
17 } | |
18 | |
19 return cssPixelsPerInch; | |
20 } | |
21 | |
22 var cssPixelsPerInch = calculateDPI(); | |
23 | |
24 var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg"); | |
25 var length = svgElement.createSVGLength(); | |
26 | |
27 debug(""); | |
28 debug("Set value to be 2px"); | |
29 length.valueAsString = "2px"; | |
30 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PX"); | |
31 shouldBe("length.value", "2"); | |
32 shouldBe("length.valueInSpecifiedUnits", "2"); | |
33 shouldBeEqualToString("length.valueAsString", "2px"); | |
34 | |
35 debug(""); | |
36 debug("Convert from px to unitless"); | |
37 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBE
R)"); | |
38 shouldBeEqualToString("length.valueAsString", "2"); | |
39 shouldBe("length.value", "2"); | |
40 shouldBe("length.valueInSpecifiedUnits", "2"); | |
41 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_NUMBER"); | |
42 | |
43 debug(""); | |
44 debug("Reset to 2px"); | |
45 length.valueAsString = "2px"; | |
46 | |
47 debug(""); | |
48 debug("Try converting from px to percentage, should fail as the SVGLength is not
associated with a SVGSVGElement, and thus no viewport information is available"
); | |
49 shouldThrow("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE)
"); | |
50 shouldBeEqualToString("length.valueAsString", "2px"); | |
51 shouldBe("length.value", "2"); | |
52 shouldBe("length.valueInSpecifiedUnits", "2"); | |
53 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PX"); | |
54 | |
55 debug(""); | |
56 debug("Reset to 2px"); | |
57 length.valueAsString = "2px"; | |
58 | |
59 debug(""); | |
60 debug("Try converting from px to ems, should fail as the SVGLength is not associ
ated with a SVGSVGElement, and thus no font-size information is available"); | |
61 shouldThrow("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS)"); | |
62 shouldBeEqualToString("length.valueAsString", "2px"); | |
63 shouldBe("length.value", "2"); | |
64 shouldBe("length.valueInSpecifiedUnits", "2"); | |
65 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PX"); | |
66 | |
67 debug(""); | |
68 debug("Reset to 2px"); | |
69 length.valueAsString = "2px"; | |
70 | |
71 debug(""); | |
72 debug("Try converting from px to exs, should fail as the SVGLength is not associ
ated with a SVGSVGElement, and thus no font-size information is available"); | |
73 shouldThrow("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS)"); | |
74 shouldBeEqualToString("length.valueAsString", "2px"); | |
75 shouldBe("length.value", "2"); | |
76 shouldBe("length.valueInSpecifiedUnits", "2"); | |
77 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PX"); | |
78 | |
79 debug(""); | |
80 debug("Reset to 2px"); | |
81 length.valueAsString = "2px"; | |
82 | |
83 debug(""); | |
84 debug("Convert from px to cm"); | |
85 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM)")
; | |
86 referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7); | |
87 shouldBeEqualToString("length.valueAsString", referenceValue + "cm"); | |
88 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(7)", referenceValue)
; | |
89 shouldBeEqualToString("length.value.toFixed(1)", "2.0"); | |
90 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_CM"); | |
91 | |
92 debug(""); | |
93 debug("Reset to 2px"); | |
94 length.valueAsString = "2px"; | |
95 | |
96 debug(""); | |
97 debug("Convert from px to mm"); | |
98 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM)")
; | |
99 referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6); | |
100 shouldBeEqualToString("length.valueAsString", referenceValue + "mm"); | |
101 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(6)", referenceValue)
; | |
102 shouldBeEqualToString("length.value.toFixed(1)", "2.0"); | |
103 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_MM"); | |
104 | |
105 debug(""); | |
106 debug("Reset to 2px"); | |
107 length.valueAsString = "2px"; | |
108 | |
109 debug(""); | |
110 debug("Convert from px to in"); | |
111 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN)")
; | |
112 referenceValue = Number(2 / cssPixelsPerInch).toFixed(7); | |
113 shouldBeEqualToString("length.valueAsString", referenceValue + "in"); | |
114 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(7)", referenceValue)
; | |
115 shouldBeEqualToString("length.value.toFixed(1)", "2.0"); | |
116 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_IN"); | |
117 | |
118 debug(""); | |
119 debug("Reset to 2px"); | |
120 length.valueAsString = "2px"; | |
121 | |
122 debug(""); | |
123 debug("Convert from px to pt"); | |
124 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PT)")
; | |
125 referenceValue = Number(2 / cssPixelsPerInch * 72); | |
126 shouldBeEqualToString("length.valueAsString", referenceValue + "pt"); | |
127 shouldBe("length.valueInSpecifiedUnits", referenceValue.toString()); | |
128 shouldBeEqualToString("length.value.toFixed(1)", "2.0"); | |
129 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PT"); | |
130 | |
131 debug(""); | |
132 debug("Reset to 2px"); | |
133 length.valueAsString = "2px"; | |
134 | |
135 debug(""); | |
136 debug("Convert from px to pc"); | |
137 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC)")
; | |
138 referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3); | |
139 // Don't check valueAsString here, it's unreliable across browsers. | |
140 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(3)", referenceValue)
; | |
141 shouldBeEqualToString("length.value.toFixed(1)", "2.0"); | |
142 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PC"); | |
143 | |
144 successfullyParsed = true; | |
OLD | NEW |