chromium/third_party/blink/web_tests/svg/dom/svgangle-units.html

<!doctype html>
<title>SVGAngle tests</title>
<script src=../../resources/testharness.js></script>
<script src=../../resources/testharnessreport.js></script>
<div id="testcontainer">
<svg width="1" height="1" visibility="hidden">
<defs><marker/></defs>
</svg>
</div>
<div id=log></div>
<script>
var svg = document.querySelector("svg");
var marker = document.querySelector("marker");
var EPSILON = Math.pow(2, -8);
var angles = [ 10, 0, 360, 500, 90, 180, 45, 25.9, 145, 270, 0.5, 0.2, 1.37, 3.14159 /* Math.PI */, 0.523599 /* Math.PI/6 */ ];
var units = {
	"" : 1,
	"deg": 2,
	"rad": 3,
	"grad": 4,
	"turn": 5
};
var highestExposedUnit = 4; // SVG_ANGLETYPE_GRAD
var unitconstants = {
	"UNKNOWN" : 0,
	"UNSPECIFIED": 1,
	"DEG": 2,
	"RAD": 3,
	"GRAD": 4,
};
var nonexposedunitconstants = {
	"TURN": 5
};

function convertTo(value, unit, outunit) {
	switch(unit) {
	    case "":
	    case "deg":
			switch(outunit) {
				case "":
				case "deg":
					return value;
				case "rad":
					return value*(Math.PI/180);
				case "grad":
					return value*(400/360);
				case "turn":
					return value/360;
			}
		case "rad":
			switch(outunit) {
				case "":
				case "deg":
					return value * 180 / Math.PI;
				case "rad":
					return value;
				case "grad":
					return value * 180 / Math.PI * 400 / 360;
				case "turn":
					return value / (2 * Math.PI);
			}
		case "grad":
			switch(outunit) {
				case "":
				case "deg":
					return value * 360 / 400;
				case "rad":
					return value * Math.PI * 2 / 400;
				case "grad":
					return value;
				case "turn":
					return value / 400;
			}
		case "turn":
			switch(outunit) {
				case "":
				case "deg":
					return value * 360;
				case "rad":
					return value * Math.PI * 2;
				case "grad":
					return value * 400;
				case "turn":
					return value;
			}
	}
}

function createAngle(valuestr) {
	var angle = svg.createSVGAngle();
	angle.valueAsString = valuestr;
	return angle;
}

for(var unit in units) {
	test(function() {
		var result = undefined;
		try {
			var a = createAngle(47 + unit);
			result = a.unitType;
		}
		catch(e) {}
		if (units[unit] > highestExposedUnit)
			assert_equals(result, undefined);
		else
			assert_equals(result, units[unit]);
	}, "SVGAngle(47" + unit + ").unitType");
}

for(var constant in unitconstants) {
	var str = "SVG_ANGLETYPE_" + constant;
	test(function() {
		assert_own_property(SVGAngle, str);
	}, "SVGAngle." + str);
}
for(var constant in nonexposedunitconstants) {
	var str = "SVG_ANGLETYPE_" + constant;
	test(function() {
		assert_false(str in SVGAngle);
	}, "SVGAngle." + str);
}

angles.forEach(function(angle) {
	for(var unit in units) {
		var anglestr = angle + unit;
		var ref;
		try {
			ref = createAngle(anglestr);
		}
		catch(e) {
			continue;
		}

		test(function() {
			assert_approx_equals(angle, ref.valueInSpecifiedUnits, EPSILON);
		}, "SVGAngle(" + anglestr + ").valueInSpecifiedUnits");

		try {
			marker.setAttribute("orient", anglestr);

			test(function() {
				assert_equals(marker.orientAngle.baseVal.valueAsString, anglestr);
			}, "orient=\"" + anglestr + "\".valueAsString");

			test(function() {
				assert_approx_equals(marker.orientAngle.baseVal.value, convertTo(angle, unit, "deg"), EPSILON);
			}, "orient=\"" + anglestr + "\".value");
		}
		finally {
			marker.removeAttribute("orient");
		}

		for (var otherunit in units) {
			test(function() {
				var a = createAngle(anglestr);
				try {
					a.convertToSpecifiedUnits(units[otherunit]);
				}
				catch(e) {}

				// unknown unit
				if (units[otherunit] > highestExposedUnit)
					assert_approx_equals(a.valueInSpecifiedUnits, angle, EPSILON);
				else
					assert_approx_equals(a.valueInSpecifiedUnits, convertTo(angle, unit, otherunit), EPSILON);
			}, "SVGAngle(" + anglestr + ").convertToSpecifiedUnits(" + units[otherunit] + " /*" + (otherunit ? otherunit : "unspecified") + "*/)");

			test(function() {
				var result = "";
				try {
					var a = createAngle(47 + otherunit);
					a.newValueSpecifiedUnits(units[unit], angle);
					result = a.valueAsString;
				}
				catch(e) {
				}

				// unknown unit
				if (units[unit] > highestExposedUnit || units[otherunit] > highestExposedUnit)
					assert_equals(result, "");
				else
					assert_equals(result, ref.valueAsString);
			}, "SVGAngle(47" + otherunit + ").newValueSpecifiedUnits(" + units[unit] + ", " + angle + ")");
		};
	}
});

</script>