<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=611138
-->
<head>
  <title>Generic tests for SVG animated length lists</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="MutationEventChecker.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=611138">Mozilla Bug 611138</a>
<p id="display"></p>
<div id="content" style="display:none;">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" width="100" height="100">
  <path id="path"/>
</svg>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
<![CDATA[

SimpleTest.waitForExplicitFinish();

/*
This file runs a series of SVGPathSegList specific tests. Generic SVGXxxList
tests can be found in test_SVGxxxList.xhtml. Anything that can be generalized
to other list types belongs there.
*/

function run_tests()
{
  document.getElementById('svg').pauseAnimations();

  var d;
  var seg;
  var path = document.getElementById("path");
  var list = path.pathSegList;

  // See https://bugzilla.mozilla.org/show_bug.cgi?id=611138
  // Here we are doing a replace with a segment (arc) that has more arguments
  // than the total number of arguments in the entire path + 2 (the +2
  // refering to the encoding of the segment types for the two segments).
  path.setAttribute('d', 'M0,0 L100,100');
  var arc = path.createSVGPathSegArcAbs(400, 0, 200, 200, 0, 1, 1);
  list.replaceItem(arc, 1);

  is(list.numberOfItems, 2, 'The length of the list should be the same after a valid replaceItem() call');
  is(list.getItem(1), arc, 'The inserted object should now be the object at the index being replaced');

  // Test whether and when we normalize the 'd' attribute:

  d = "  \n  M  10  ,  10  \n  L  20  10  \n  ";
  path.setAttribute('d', d);
  is(path.getAttribute('d'), d, "Values passed to setAttribute for the 'd' attribute should not be normalised");
  list.getItem(1).y = 20;
  isnot(path.getAttribute('d'), d, "The 'd' attribute should change when its underlying DOM list changes");

  // Test that path manipulations still work even when the path is invalid due
  // to it not starting with a moveto segment:

  path.setAttribute('d', 'M0,0 L1,1');
  is(list.numberOfItems, 2, 'setAttribute should result in two items')

  seg = list.getItem(1);
  list.removeItem(0);
  ok(list.numberOfItems == 1 && list.getItem(0) == seg,
    'If removeItem removes the initial moveto leaving an invalid path, the other items should still be left in the list')

  seg = path.createSVGPathSegLinetoAbs(1, 2);
  list.appendItem(seg);
  ok(list.numberOfItems == 2 && list.getItem(1) == seg,
    'appendItem should be able to append to an invalid path');

  seg = path.createSVGPathSegLinetoAbs(1, 2);
  list.replaceItem(seg, 1);
  ok(list.numberOfItems == 2 && list.getItem(1) == seg,
    'replaceItem should be able to replace items in an invalid path');

  seg = path.createSVGPathSegLinetoAbs(1, 2);
  list.insertItemBefore(seg, 1);
  ok(list.numberOfItems == 3 && list.getItem(1) == seg,
    'insertItemBefore should be able insert items into an invalid path');

  seg = path.createSVGPathSegLinetoAbs(1, 2);
  list.initialize(seg);
  ok(list.numberOfItems == 1 && list.getItem(0) == seg,
    'initialize should be able initialize an invalid path with a non-moveto item');

  // Test mutation events

  eventChecker = new MutationEventChecker;
  d = 'M0,0 L12,34'
  path.setAttribute('d', d);
  eventChecker.watchAttr(path, "d");

  // -- Actual changes
  eventChecker.expect("modify modify modify");
  list[0].x = 10;
  list[0].y = 5;
  path.setAttribute("d", "M20,5 L12,34");

  // -- Redundant changes
  eventChecker.expect("");
  list[0].x = 20;
  list[1].y = 34;
  path.setAttribute("d", "M20,5 L12,34");

  // -- Attribute removal
  eventChecker.expect("remove");
  path.removeAttribute("d");

  // -- Non-existent attribute removal
  eventChecker.expect("");
  path.removeAttribute("d");
  path.removeAttributeNS(null, "d");
  eventChecker.finish();

  SimpleTest.finish();
}

window.addEventListener("load", run_tests, false);

]]>
</script>
</pre>
</body>
</html>