chromium/third_party/blink/web_tests/svg/dom/SVGAngle.html

<!DOCTYPE HTML>
<title>SVGAngle interface</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
test(function() {
  // This test checks the SVGAngle API.

  var svgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  var angle = svgElement.createSVGAngle();

  // Check initial angle values.
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);
  assert_equals(angle.valueAsString, "0");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);

  // Spec: Raised if unitType is SVG_ANGLETYPE_UNKNOWN or not a valid unit type constant (one of the other SVG_ANGLETYPE_* constants defined on this interface).
  // Check invalid arguments for 'convertToSpecifiedUnits'.
  assert_throws_dom("NotSupportedError", function() { angle.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_UNKNOWN); });
  assert_throws_dom("NotSupportedError", function() { angle.convertToSpecifiedUnits(-1); });
  assert_throws_dom("NotSupportedError", function() { angle.convertToSpecifiedUnits(5); });
  // 'aString' converts to short 0 (through NaN) according to ECMA-262, ToUint16.
  // Therefore this throws NOT_SUPPORTED_ERR.
  assert_throws_dom("NotSupportedError", function() { angle.convertToSpecifiedUnits('aString'); });
  // Same here, via ToString conversion of object.
  assert_throws_dom("NotSupportedError", function() { angle.convertToSpecifiedUnits(angle); });
  // Same here, via ToString conversion of object.
  assert_throws_dom("NotSupportedError", function() { angle.convertToSpecifiedUnits(svgElement); });
  assert_throws_js(TypeError, function() { angle.convertToSpecifiedUnits(); });
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  // Check valid arguments for 'convertToSpecifiedUnits', that should only modify the 'valueAsString'");
  angle.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_RAD);
  assert_equals(angle.valueAsString, "0rad");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_RAD);

  angle.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_GRAD);
  assert_equals(angle.valueAsString, "0grad");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_GRAD);

  angle.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG);
  assert_equals(angle.valueAsString, "0deg");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_DEG);

  angle.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);
  assert_equals(angle.valueAsString, "0");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  // Spec: Raised if unitType is SVG_ANGLETYPE_UNKNOWN or not a valid unit type constant (one of the other SVG_ANGLETYPE_* constants defined on this interface).
  // Check invalid arguments for 'newValueSpecifiedUnits'.
  assert_throws_dom("NotSupportedError", function() { angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_UNKNOWN, 50); });
  assert_throws_dom("NotSupportedError", function() { angle.newValueSpecifiedUnits(-1, 50); });
  assert_throws_dom("NotSupportedError", function() { angle.newValueSpecifiedUnits(5, 50); });
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG); });
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, 'aString'); });
  assert_equals(angle.value, 0);

  angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, 0);
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, angle); });
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, svgElement); });
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, NaN); });
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, Infinity); });
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG); });

  // All of the following unitType arguments convert to 0 (SVG_ANGLETYPE_UNKNOWN).
  assert_throws_dom("NotSupportedError", function() { angle.newValueSpecifiedUnits('aString', 4); });
  assert_throws_dom("NotSupportedError", function() { angle.newValueSpecifiedUnits(angle, 4); });
  assert_throws_dom("NotSupportedError", function() { angle.newValueSpecifiedUnits(svgElement, 4); });
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits('aString', 'aString'); });
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(angle, angle); });
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(svgElement, svgElement); });
  assert_throws_js(TypeError, function() { angle.newValueSpecifiedUnits(); });
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_DEG);

  // Check valid arguments for 'newValueSpecifiedUnits', that should only modify the 'valueAsString'.
  angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_RAD, parseFloat(Math.PI.toFixed(5)));
  assert_equals(angle.valueAsString, Math.PI.toFixed(5) + "rad");
  assert_equals(angle.value.toFixed(1), "180.0");
  assert_equals(angle.valueInSpecifiedUnits.toFixed(5), Math.PI.toFixed(5));
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_RAD);

  angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_GRAD, 400);
  assert_equals(angle.valueAsString, "400grad");
  assert_equals(angle.value.toFixed(1), "360.0");
  assert_equals(angle.valueInSpecifiedUnits, 400);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_GRAD);

  angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, 360);
  assert_equals(angle.valueAsString, "360deg");
  assert_equals(angle.value, 360);
  assert_equals(angle.valueInSpecifiedUnits, 360);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_DEG);

  angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_UNSPECIFIED, 180);
  assert_equals(angle.valueAsString, "180");
  assert_equals(angle.value, 180);
  assert_equals(angle.valueInSpecifiedUnits, 180);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  // Reset to initial angle state.
  angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_UNSPECIFIED, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  // Spec: Raised if the assigned string cannot be parsed as a valid <angle>.
  // Check setting invalid 'valueAsString' arguments.
  assert_throws_dom("SyntaxError", function() { angle.valueAsString = '10px'; });
  assert_equals(angle.valueAsString, "0");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  assert_throws_dom("SyntaxError", function() { angle.valueAsString = '10x'; });
  assert_equals(angle.valueAsString, "0");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  assert_throws_dom("SyntaxError", function() { angle.valueAsString = '5graD'; });
  assert_equals(angle.valueAsString, "0");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  assert_throws_dom("SyntaxError", function() { angle.valueAsString = '5Rad'; });
  assert_equals(angle.valueAsString, "0");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  assert_throws_dom("SyntaxError", function() { angle.valueAsString = ',5 rad'; });
  assert_equals(angle.valueAsString, "0");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  assert_throws_dom("SyntaxError", function() { angle.valueAsString = null; });
  assert_equals(angle.valueAsString, "0");
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  // Check setting invalid 'valueInSpecifiedUnits' arguments
  assert_throws_js(TypeError, function() { angle.valueInSpecifiedUnits = 'test'; });
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);
  angle.valueInSpecifiedUnits = 0;
  assert_equals(angle.valueInSpecifiedUnits, 0);

  assert_throws_js(TypeError, function() { angle.valueInSpecifiedUnits = angle; });
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.valueInSpecifiedUnits = NaN; });
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.valueInSpecifiedUnits = Infinity; });
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  // Check setting invalid 'value' arguments.
  angle.value = 0;
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.value = 'test'; });
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  angle.value = 0;
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.value = angle; });
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.value = NaN; });
  assert_equals(angle.value, 0);
  assert_throws_js(TypeError, function() { angle.value = Infinity; });
  assert_equals(angle.value, 0);
  assert_equals(angle.valueInSpecifiedUnits, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_UNSPECIFIED);

  // Reset to angle in degree units.
  angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG, 0);
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_DEG);

  // Check setting valid 'value' arguments, assure that 'valueInSpecifiedUnits' and 'valueAsString' are synchronized.
  angle.value = 50;
  assert_equals(angle.value, 50);
  assert_equals(angle.valueInSpecifiedUnits, 50);
  assert_equals(angle.valueAsString, "50deg");
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_DEG);

  // Try modifiying the readonly 'unitType', needs to fail.
  angle.unitType = SVGAngle.SVG_ANGLETYPE_RAD;
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_DEG);

  // Check setting valid 'valueInSpecifiedUnits' arguments, assure that 'value' and 'valueAsString' are synchronized.
  angle.valueInSpecifiedUnits = 100;
  assert_equals(angle.valueInSpecifiedUnits, 100);
  assert_equals(angle.value, 100);
  assert_equals(angle.valueAsString, "100deg");
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_DEG);

  // Check setting valid 'valueAsString' arguments, assure that 'value' and 'valueInSpecifiedUnits' are synchronized.
  angle.valueAsString = '200grad';
  assert_equals(angle.valueAsString, "200grad");
  assert_equals(angle.valueInSpecifiedUnits, 200);
  assert_equals(angle.value.toFixed(1), "180.0");
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_GRAD);

  // Now convert the GRAD value into a RAD value, and assure that all properties have been synchronized.
  angle.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_RAD);
  assert_equals(angle.value.toFixed(1), "180.0");
  assert_equals(angle.valueInSpecifiedUnits.toFixed(5), Math.PI.toFixed(5));
  assert_equals(angle.valueAsString, Math.PI.toFixed(5) + "rad");
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_RAD);

  // Now try converting the RAD value into an unknown value, that should fail and throw;
  assert_throws_dom("NotSupportedError", function() { angle.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_UNKNOWN); });
  assert_equals(angle.value.toFixed(1), "180.0");
  assert_equals(angle.valueInSpecifiedUnits.toFixed(5), Math.PI.toFixed(5));
  assert_equals(angle.valueAsString, Math.PI.toFixed(5) + "rad");
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_RAD);

  // Now convert the RAD value into a DEG value, and assure that all properties have been synchronized.
  angle.convertToSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_DEG);
  assert_equals(angle.value.toFixed(1), "180.0");
  assert_equals(angle.valueInSpecifiedUnits.toFixed(1), "180.0");
  assert_equals(angle.valueAsString, "180deg");
  assert_equals(angle.unitType, SVGAngle.SVG_ANGLETYPE_DEG);
});
</script>