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"> |
| 7 </svg> |
| 8 </div> |
| 9 <div id=log></div> |
| 10 <script> |
| 11 var svg = document.querySelector("svg"); |
| 12 var EPSILON = Math.pow(2, -8); |
| 13 var lengths = [ 10, 0, 360, 500, 90, 180, 45, 25.9]; |
| 14 |
| 15 var units = { |
| 16 "" : 1, |
| 17 "%": 2, |
| 18 "em": 3, |
| 19 "ex": 4, |
| 20 "px": 5, |
| 21 "cm": 6, |
| 22 "mm": 7, |
| 23 "in": 8, |
| 24 "pt": 9, |
| 25 "pc": 10, |
| 26 "rem": 11 |
| 27 }; |
| 28 var highestExposedUnit = 10; // SVG_LENGTHTYPE_PC |
| 29 var unitconstants = { |
| 30 "UNKNOWN" : 0, |
| 31 "NUMBER": 1, |
| 32 "PERCENTAGE": 2, |
| 33 "EMS": 3, |
| 34 "EXS": 4, |
| 35 "PX": 5, |
| 36 "CM": 6, |
| 37 "MM": 7, |
| 38 "IN": 8, |
| 39 "PT": 9, |
| 40 "PC": 10, |
| 41 }; |
| 42 var nonexposedunitconstants = { |
| 43 "REMS": 11 |
| 44 }; |
| 45 |
| 46 function convertTo(value, unit, outunit) { |
| 47 var userUnits; |
| 48 var cssPixelsPerInch = 96; |
| 49 var cssPixelsPerCentimeter = cssPixelsPerInch / 2.54; //2.54 cm/in |
| 50 var cssPixelsPerMillimeter = cssPixelsPerCentimeter / 10; |
| 51 var cssPixelsPerPoint = cssPixelsPerInch / 72; |
| 52 var cssPixelsPerPica = cssPixelsPerInch / 6; |
| 53 |
| 54 switch(unit) { |
| 55 case "": |
| 56 case "px": |
| 57 userUnits = value; |
| 58 break; |
| 59 case "%": |
| 60 case "em": |
| 61 case "ex": |
| 62 case "rem": |
| 63 return value; |
| 64 case "cm": |
| 65 userUnits = value * cssPixelsPerCentimeter; |
| 66 break; |
| 67 case "mm": |
| 68 userUnits = value * cssPixelsPerMillimeter; |
| 69 break; |
| 70 case "in": |
| 71 userUnits = value * cssPixelsPerInch; |
| 72 break; |
| 73 case "pt": |
| 74 userUnits = value * cssPixelsPerPoint; |
| 75 break; |
| 76 case "pc": |
| 77 userUnits = value * cssPixelsPerPica; |
| 78 break; |
| 79 } |
| 80 |
| 81 switch(outunit) { |
| 82 case "": |
| 83 case "px": |
| 84 return userUnits; |
| 85 case "%": |
| 86 case "em": |
| 87 case "ex": |
| 88 case "rem": |
| 89 return value; |
| 90 case "cm": |
| 91 return userUnits / cssPixelsPerCentimeter; |
| 92 case "mm": |
| 93 return userUnits / cssPixelsPerMillimeter; |
| 94 case "in": |
| 95 return userUnits / cssPixelsPerInch; |
| 96 case "pt": |
| 97 return userUnits / cssPixelsPerPoint; |
| 98 case "pc": |
| 99 return userUnits / cssPixelsPerPica; |
| 100 } |
| 101 } |
| 102 |
| 103 function createLength(valuestr) { |
| 104 var length = svg.createSVGLength(); |
| 105 length.valueAsString = valuestr; |
| 106 return length; |
| 107 } |
| 108 |
| 109 for(var unit in units) { |
| 110 test(function() { |
| 111 var result = undefined; |
| 112 try { |
| 113 var a = createLength(10 + unit); |
| 114 result = a.unitType; |
| 115 } |
| 116 catch(e) {} |
| 117 if (units[unit] > highestExposedUnit) |
| 118 assert_equals(result, undefined); |
| 119 else |
| 120 assert_equals(result, units[unit]); |
| 121 }, "SVGLength(10" + unit + ").unitType"); |
| 122 } |
| 123 |
| 124 for(var constant in unitconstants) { |
| 125 var str = "SVG_LENGTHTYPE_" + constant; |
| 126 test(function() { |
| 127 assert_exists(SVGLength, str, ""); |
| 128 }, "SVGLength." + str); |
| 129 } |
| 130 for(var constant in nonexposedunitconstants) { |
| 131 var str = "SVG_LENGTHTYPE_" + constant; |
| 132 test(function() { |
| 133 assert_not_exists(SVGLength, str, ""); |
| 134 }, "SVGLength." + str); |
| 135 } |
| 136 |
| 137 lengths.forEach(function(length) { |
| 138 for(var unit in units) { |
| 139 var lengthstr = length + unit; |
| 140 var ref; |
| 141 try { |
| 142 ref = createLength(lengthstr); |
| 143 } |
| 144 catch(e) { |
| 145 continue; |
| 146 } |
| 147 |
| 148 test(function() { |
| 149 assert_approx_equals(length, ref.valueInSpecifiedUnits, EPSILON); |
| 150 }, "SVGLength(" + lengthstr + ").valueInSpecifiedUnits"); |
| 151 |
| 152 for (var otherunit in units) { |
| 153 test(function() { |
| 154 var a = createLength(lengthstr); |
| 155 try { |
| 156 a.convertToSpecifiedUnits(units[otherunit]); |
| 157 } |
| 158 catch(e) {} |
| 159 |
| 160 // unknown unit |
| 161 if (units[otherunit] > highestExposedUnit) |
| 162 assert_approx_equals(a.valueInSpecifiedUnits, length, EPSILO
N); |
| 163 else |
| 164 assert_approx_equals(a.valueInSpecifiedUnits, convertTo(leng
th, unit, otherunit), EPSILON); |
| 165 }, "SVGLength(" + lengthstr + ").convertToSpecifiedUnits(" + units[o
therunit] + " /*" + (otherunit ? otherunit : "unspecified") + "*/)"); |
| 166 |
| 167 test(function() { |
| 168 var result = ""; |
| 169 try { |
| 170 var a = createLength(47 + otherunit); |
| 171 a.newValueSpecifiedUnits(units[unit], length); |
| 172 result = a.valueAsString; |
| 173 } |
| 174 catch(e) { |
| 175 } |
| 176 |
| 177 // unknown unit |
| 178 if (units[unit] > highestExposedUnit || units[otherunit] > highe
stExposedUnit) |
| 179 assert_equals(result, ""); |
| 180 else |
| 181 assert_equals(result, ref.valueAsString); |
| 182 }, "newValueSpecifiedUnits(" + units[unit] + ", " + length + ")" ); |
| 183 }; |
| 184 |
| 185 } |
| 186 }); |
| 187 |
| 188 |
| 189 </script> |
OLD | NEW |