Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(438)

Side by Side Diff: third_party/WebKit/LayoutTests/svg/dom/script-tests/SVGLength-px-with-context.js

Issue 2271223002: Convert LayoutTests/svg/dom/SVGLength*.html js-tests.js tests to testharness.js based tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: moved length to local Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/svg/dom/script-tests/SVGLength-px.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 function calculateXHeight()
23 {
24 // Crude hack to calculate the x-height
25 var divElement = document.createElement("div");
26 divElement.setAttribute("style", "height: 1ex; font-size: 12px; font-family: Ahem;");
27 document.getElementById("description").appendChild(divElement);
28 var xHeight = divElement.offsetHeight;
29 document.getElementById("description").removeChild(divElement);
30 return xHeight;
31 }
32
33 // Setup a real SVG document, so SVGLength can resolve relative units.
34 var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg");
35 svgElement.setAttribute("width", "150");
36 svgElement.setAttribute("height", "50");
37
38 var rectElement = document.createElementNS("http://www.w3.org/2000/svg", "rect") ;
39 rectElement.setAttribute("style", "visibility: hidden; font-size: 12px; font-fam ily: Ahem;");
40 svgElement.appendChild(rectElement);
41 document.getElementById("description").appendChild(svgElement);
42
43 // Extract test information
44 var length = rectElement.x.baseVal;
45 var svgWidth = svgElement.width.baseVal.value;
46 var svgHeight = svgElement.height.baseVal.value;
47 var fontSize = parseInt(rectElement.style.fontSize);
48 var cssPixelsPerInch = calculateDPI();
49
50 debug("");
51 debug("Set value to be 2px");
52 length.valueAsString = "2px";
53 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PX");
54 shouldBe("length.value", "2");
55 shouldBe("length.valueInSpecifiedUnits", "2");
56 shouldBeEqualToString("length.valueAsString", "2px");
57
58 debug("");
59 debug("Convert from px to unitless");
60 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_NUMBE R)");
61 shouldBeEqualToString("length.valueAsString", "2");
62 shouldBe("length.value", "2");
63 shouldBe("length.valueInSpecifiedUnits", "2");
64 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_NUMBER");
65
66 debug("");
67 debug("Reset to 2px");
68 length.valueAsString = "2px";
69
70 debug("");
71 debug("Convert from px to percentage");
72 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCE NTAGE)");
73 referenceValue = Number(2 / svgWidth * 100).toFixed(5);
74 shouldBeEqualToString("length.valueAsString", referenceValue + "%");
75 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(5)", referenceValue) ;
76 shouldBeEqualToString("length.value.toFixed(1)", "2.0");
77 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PERCENTAGE");
78
79 debug("");
80 debug("Reset to 2px");
81 length.valueAsString = "2px";
82
83 debug("");
84 debug("Convert from px to ems");
85 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS)" );
86 referenceValue = Number(2 / fontSize).toFixed(6);
87 shouldBeEqualToString("length.valueAsString", referenceValue + "em");
88 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(6)", referenceValue) ;
89 shouldBeEqualToString("length.value.toFixed(1)", "2.0");
90 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_EMS");
91
92 debug("");
93 debug("Reset to 2px");
94 length.valueAsString = "2px";
95
96 debug("");
97 debug("Convert from px to exs");
98 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS)" );
99 referenceValue = Number(2 / calculateXHeight()).toFixed(1);
100 // Don't check valueAsString here, it's unreliable across browsers.
101 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(1)", referenceValue) ;
102 shouldBeEqualToString("length.value.toFixed(1)", "2.0");
103 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_EXS");
104
105 debug("");
106 debug("Reset to 2px");
107 length.valueAsString = "2px";
108
109 debug("");
110 debug("Convert from px to cm");
111 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM)") ;
112 referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7);
113 shouldBeEqualToString("length.valueAsString", referenceValue + "cm");
114 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(7)", referenceValue) ;
115 shouldBeEqualToString("length.value.toFixed(1)", "2.0");
116 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_CM");
117
118 debug("");
119 debug("Reset to 2px");
120 length.valueAsString = "2px";
121
122 debug("");
123 debug("Convert from px to mm");
124 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM)") ;
125 referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6);
126 shouldBeEqualToString("length.valueAsString", referenceValue + "mm");
127 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(6)", referenceValue) ;
128 shouldBeEqualToString("length.value.toFixed(1)", "2.0");
129 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_MM");
130
131 debug("");
132 debug("Reset to 2px");
133 length.valueAsString = "2px";
134
135 debug("");
136 debug("Convert from px to in");
137 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN)") ;
138 referenceValue = Number(2 / cssPixelsPerInch).toFixed(7);
139 shouldBeEqualToString("length.valueAsString", referenceValue + "in");
140 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(7)", referenceValue) ;
141 shouldBeEqualToString("length.value.toFixed(1)", "2.0");
142 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_IN");
143
144 debug("");
145 debug("Reset to 2px");
146 length.valueAsString = "2px";
147
148 debug("");
149 debug("Convert from px to pt");
150 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PT)") ;
151 referenceValue = Number(2 / cssPixelsPerInch * 72);
152 shouldBeEqualToString("length.valueAsString", referenceValue + "pt");
153 shouldBe("length.valueInSpecifiedUnits", referenceValue.toString());
154 shouldBeEqualToString("length.value.toFixed(1)", "2.0");
155 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PT");
156
157 debug("");
158 debug("Reset to 2px");
159 length.valueAsString = "2px";
160
161 debug("");
162 debug("Convert from px to pc");
163 shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC)") ;
164 referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3);
165 // Don't check valueAsString here, it's unreliable across browsers.
166 shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(3)", referenceValue) ;
167 shouldBeEqualToString("length.value.toFixed(1)", "2.0");
168 shouldBe("length.unitType", "SVGLength.SVG_LENGTHTYPE_PC");
169
170 successfullyParsed = true;
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/svg/dom/script-tests/SVGLength-px.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698