OLD | NEW |
| (Empty) |
1 <!doctype html> | |
2 <title>SVGlength tests</title> | |
3 <script src=../../resources/testharness.js></script> | |
4 <script src=../../resources/testharnessreport.js></script> | |
5 <div id="testcontainer"> | |
6 <svg width="1" height="1" visibility="hidden"> </svg> | |
7 </div> | |
8 <script> | |
9 | |
10 var svg = document.querySelector("svg"); | |
11 | |
12 var EPSILON = Math.pow(2, -8); | |
13 var lengths = [10, 0, 360, 500, 90, 180, 45, 25.9]; | |
14 var validUnits = { | |
15 "" : 1, | |
16 "%": 2, | |
17 "em": 3, | |
18 "ex": 4, | |
19 "px": 5, | |
20 "cm": 6, | |
21 "mm": 7, | |
22 "in": 8, | |
23 "pt": 9, | |
24 "pc": 10, | |
25 }; | |
26 | |
27 function createLength(valuestr) { | |
28 var length = svg.createSVGLength(); | |
29 length.valueAsString = valuestr; | |
30 return length; | |
31 } | |
32 | |
33 function convertTo(value, unit, outunit) { | |
34 var userUnits; | |
35 var cssPixelsPerInch = 96; | |
36 var cssPixelsPerCentimeter = cssPixelsPerInch / 2.54; //2.54 cm/in | |
37 var cssPixelsPerMillimeter = cssPixelsPerCentimeter / 10; | |
38 var cssPixelsPerPoint = cssPixelsPerInch / 72; | |
39 var cssPixelsPerPica = cssPixelsPerInch / 6; | |
40 | |
41 switch(unit) { | |
42 case "": | |
43 case "px": | |
44 userUnits = value; | |
45 break; | |
46 case "%": | |
47 case "em": | |
48 case "ex": | |
49 case "rem": | |
50 case "ch": | |
51 return value; | |
52 case "cm": | |
53 userUnits = value * cssPixelsPerCentimeter; | |
54 break; | |
55 case "mm": | |
56 userUnits = value * cssPixelsPerMillimeter; | |
57 break; | |
58 case "in": | |
59 userUnits = value * cssPixelsPerInch; | |
60 break; | |
61 case "pt": | |
62 userUnits = value * cssPixelsPerPoint; | |
63 break; | |
64 case "pc": | |
65 userUnits = value * cssPixelsPerPica; | |
66 break; | |
67 } | |
68 | |
69 switch(outunit) { | |
70 case "": | |
71 case "px": | |
72 return userUnits; | |
73 case "%": | |
74 case "em": | |
75 case "ex": | |
76 case "rem": | |
77 case "ch": | |
78 return value; | |
79 case "cm": | |
80 return userUnits / cssPixelsPerCentimeter; | |
81 case "mm": | |
82 return userUnits / cssPixelsPerMillimeter; | |
83 case "in": | |
84 return userUnits / cssPixelsPerInch; | |
85 case "pt": | |
86 return userUnits / cssPixelsPerPoint; | |
87 case "pc": | |
88 return userUnits / cssPixelsPerPica; | |
89 } | |
90 } | |
91 | |
92 test(function() { | |
93 for (var unit in validUnits) { | |
94 var length = createLength(10 + unit); | |
95 assert_equals(length.unitType, validUnits[unit]); | |
96 } | |
97 }, "Test valid unit types are accepted in valueAsString"); | |
98 | |
99 test(function() { | |
100 var invalidUnits = { | |
101 "rem": 1, | |
102 "ch": 2 | |
103 }; | |
104 for (var unit in invalidUnits) { | |
105 assert_throws(null, function() { createLength(10 + unit) }); | |
106 } | |
107 }, "Test invalid unit types are not accepted in valueAsString"); | |
108 | |
109 test(function() { | |
110 var unitConstants = { | |
111 "UNKNOWN" : 0, | |
112 "NUMBER": 1, | |
113 "PERCENTAGE": 2, | |
114 "EMS": 3, | |
115 "EXS": 4, | |
116 "PX": 5, | |
117 "CM": 6, | |
118 "MM": 7, | |
119 "IN": 8, | |
120 "PT": 9, | |
121 "PC": 10, | |
122 }; | |
123 for (var constant in unitConstants) { | |
124 var str = "SVG_LENGTHTYPE_" + constant; | |
125 assert_exists(SVGLength, str, str + " should exist in SVGlength"); | |
126 } | |
127 }, "Test that unit constants that are supposed to be exposed are available"); | |
128 | |
129 test(function() { | |
130 var nonexposedUnitConstants = { | |
131 "REMS": 11, | |
132 "CHS":12 | |
133 }; | |
134 for (var constant in nonexposedUnitConstants) { | |
135 var str = "SVG_LENGTHTYPE_" + constant; | |
136 assert_not_exists(SVGLength, str, str + " should not be exposed in SVGlength
"); | |
137 } | |
138 }, "Test that unit constants that are not supposed to be exposed are not availab
le"); | |
139 | |
140 test(function() { | |
141 for (var i = 0; i < validUnits.length; ++i) { | |
142 var unit = validUnits[i]; | |
143 for (var j = 0; j < lengths.length; ++j) { | |
144 var length = lengths[i]; | |
145 var value = createLength(length + unit); | |
146 assert_equals(length, value.valueInSpecifiedUnits); | |
147 } | |
148 } | |
149 }, "Test result from valueInSpecifiedUnits"); | |
150 | |
151 test(function() { | |
152 var nonRelativeUnits = ["px", "cm", "mm", "in", "pt", "pc"]; | |
153 | |
154 for (var i = 0; i < lengths.length; ++i) { | |
155 var length = lengths[i]; | |
156 for (var j = 0; j < nonRelativeUnits.length; ++j) { | |
157 var unit = nonRelativeUnits[j]; | |
158 var lengthStr = length + unit; | |
159 for (var k = 0; k < nonRelativeUnits.length; ++k) { | |
160 var otherUnit = nonRelativeUnits[k]; | |
161 var svgLength = createLength(lengthStr); | |
162 svgLength.convertToSpecifiedUnits(validUnits[otherUnit]); | |
163 assert_approx_equals(svgLength.valueInSpecifiedUnits, convertTo(length,
unit, otherUnit), EPSILON); | |
164 } | |
165 } | |
166 } | |
167 }, "Test converting unit types for non-relative units"); | |
168 | |
169 test(function() { | |
170 for (var i = 0; i < lengths.length; ++i) { | |
171 var length = lengths[i]; | |
172 for (var j = 0; j < validUnits.length; ++j) { | |
173 var unit = validUnits[j]; | |
174 var ref = createLength(length + unit); | |
175 | |
176 for (var k = 0; k < validUnits.length; ++k) { | |
177 var otherUnit = validUnits[k]; | |
178 | |
179 var value = createLength(47 + otherUnit); | |
180 value.newValueSpecifiedUnits(unit, length); | |
181 | |
182 assert_equals(value.valueAsString, ref.valueAsString); | |
183 } | |
184 } | |
185 } | |
186 }, "Test newValueSpecifiedUnits for each unit"); | |
187 | |
188 </script> | |
189 | |
OLD | NEW |