<!DOCTYPE html>
<title>Inserting animation elements that end before current presentation time</title>
<link rel="help" href="https://www.w3.org/TR/2001/REC-smil-animation-20010904/#Timing-BeginEnd-LC-Start">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/rendering-utils.js"></script>
<svg>
  <rect width="100" height="100" fill="orange">
    <animate attributeName="fill" values="blue;red" dur="10ms" fill="freeze"/>
  </rect>
</svg>
<script>
  promise_test(t => {
    const rect = document.querySelector('rect');
    const endWatcher = new Promise(resolve => {
      document.querySelector('animate').onend = resolve;
    });
    return endWatcher
      .then(() => {
        const oldAnimation = rect.firstElementChild;
        const newAnimation = oldAnimation.cloneNode(false);
        newAnimation.setAttribute('values', 'red;green');
        rect.replaceChild(newAnimation, oldAnimation);
        return waitForAtLeastOneFrame();
      })
      .then(() => {
        assert_equals(getComputedStyle(rect).fill, 'rgb(0, 128, 0)', 'new animation applies');
      });
  });
</script>
