<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=418756
-->
<head>
  <title>Test for Bug 418756</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Mozilla bug
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=418756">418756</a>
<p id="display"></p>
<div id="content">
  <form id="f1">
  </form>
  <form id="f2">
  </form>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 418756 **/
var group1;
var group2;
var group3;

function bounce(node) {
  let n = node.nextSibling;
  let p = node.parentNode;
  p.removeChild(node);
  p.insertBefore(node, n);
}

var createdNodes = [];

function cleanup() {
  for (let node of createdNodes) {
    if (node.parentNode) {
      node.remove();
    }
  }

  createdNodes = [];
}

var typeMapper = {
 'c': 'checkbox',
 'r': 'radio'
};

var id = 0;

// type can be 'c' for 'checkbox' and 'r' for 'radio'
function createNode(type, name, checked) {
  let node = document.createElement("input");
  node.setAttribute("type",  typeMapper[type]);
  if (checked) {
    node.setAttribute("checked", "checked");
  }
  node.setAttribute("id", type + (++id));
  node.setAttribute("name", name);
  createdNodes.push(node);
  return node;
}

var types = ['c', 'r'];

// First make sure that setting .checked makes .defaultChecked changes no
// longer affect .checked.
for (let type of types) {
  let n = createNode(type, '', false);
  is(n.defaultChecked, false, "Bogus defaultChecked on " + typeMapper[type]);
  is(n.checked, false, "Bogus checked on " + typeMapper[type]);
  n.defaultChecked = true;
  is(n.defaultChecked, true, "Bogus defaultChecked on " + typeMapper[type] +
                             "after mutation");
  is(n.checked, true, "Bogus checked on " + typeMapper[type] +
                      "after mutation");
  n.checked = false;
  is(n.defaultChecked, true, "Bogus defaultChecked on " + typeMapper[type] +
                             "after second mutation");
  is(n.checked, false, "Bogus checked on " + typeMapper[type] +
                       "after second mutation");
  n.defaultChecked = false;
  is(n.defaultChecked, false, "Bogus defaultChecked on " + typeMapper[type] +
                              "after third mutation");
  is(n.checked, false, "Bogus checked on " + typeMapper[type] +
                       "after third mutation");
  n.defaultChecked = true;
  is(n.defaultChecked, true, "Bogus defaultChecked on " + typeMapper[type] +
                             "after fourth mutation");
  is(n.checked, false, "Bogus checked on " + typeMapper[type] +
                       "after fourth mutation");
}

cleanup();

// Now check that bouncing a control that's the only one of its kind has no
// effect
for (let type of types) {
  let n = createNode(type, 'test1', true);
  $("f1").appendChild(n);
  n.checked = false;
  n.defaultChecked = false;
  bounce(n);
  n.defaultChecked = true;
  is(n.checked, false, "We set .checked on this " + typeMapper[type]);
}

cleanup();

// Now check that playing with a single radio in a group affects all
// other radios in the group (but not radios not in that group)
group1 = [ createNode('r', 'g1', false),
           createNode('r', 'g1', false),
           createNode('r', 'g1', false) ];
group2 = [ createNode('r', 'g2', false),
           createNode('r', 'g2', false),
           createNode('r', 'g2', false) ];
group3 = [ createNode('r', 'g1', false),
           createNode('r', 'g1', false),
           createNode('r', 'g1', false) ];
for (let g of group1) {
  $("f1").appendChild(g);
}
for (let g of group2) {
  $("f1").appendChild(g);
}
for (let g of group3) {
  $("f2").appendChild(g);
}

for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, false,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 1");
    is(g.checked, false,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checkedhecked wrong pass 1");
  }
}

group1[1].defaultChecked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && group1.indexOf(g) == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 2");
    is(g.checked, n == 1 && group1.indexOf(g) == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 2");
  }
}

group1[0].defaultChecked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && (group1.indexOf(g) == 1 ||
                                    group1.indexOf(g) == 0),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 3");
    is(g.checked, n == 1 && group1.indexOf(g) == 0,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 3");
  }
}

group1[2].defaultChecked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 4");
    is(g.checked, n == 1 && group1.indexOf(g) == 2,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 4");
  }
}

var next = group1[1].nextSibling;
var p = group1[1].parentNode;
p.removeChild(group1[1]);
group1[1].defaultChecked = false;
group1[1].defaultChecked = true;
p.insertBefore(group1[1], next);
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 5");
    is(g.checked, n == 1 && group1.indexOf(g) == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 5");
  }
}

for (let g of group1) {
  g.defaultChecked = false;
}
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, false,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 6");
    is(g.checked, false,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checkedhecked wrong pass 6");
  }
}

group1[1].checked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, false,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 7");
    is(g.checked, n == 1 && group1.indexOf(g) == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 7");
  }
}

group1[0].defaultChecked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && group1.indexOf(g) == 0,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 8");
    is(g.checked, n == 1 && group1.indexOf(g) == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 8");
  }
}

group1[2].defaultChecked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && (group1.indexOf(g) == 0 ||
                                    group1.indexOf(g) == 2),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 9");
    is(g.checked, n == 1 && group1.indexOf(g) == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 9");
  }
}
group1[1].remove();
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && (group1.indexOf(g) == 0 ||
                                    group1.indexOf(g) == 2),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 10");
    is(g.checked, n == 1 && group1.indexOf(g) == 1,
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 10");
  }
}

group1[2].checked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && (group1.indexOf(g) == 0 ||
                                    group1.indexOf(g) == 2),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 11");
    is(g.checked, n == 1 && (group1.indexOf(g) == 1 ||
                             group1.indexOf(g) == 2),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 11");
  }
}

group1[0].checked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && (group1.indexOf(g) == 0 ||
                                    group1.indexOf(g) == 2),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 12");
    is(g.checked, n == 1 && (group1.indexOf(g) == 1 ||
                             group1.indexOf(g) == 0),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 12");
  }
}

next = group2[1].nextSibling;
p = group2[1].parentNode;
p.removeChild(group2[1]);
p.insertBefore(group2[1], next);
group2[0].checked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && (group1.indexOf(g) == 0 ||
                                    group1.indexOf(g) == 2),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 13");
    is(g.checked, (n == 1 && (group1.indexOf(g) == 1 ||
                              group1.indexOf(g) == 0)) ||
                  (n == 2 && group2.indexOf(g) == 0), 
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 13");
  }
}

p.insertBefore(group2[1], next);
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, n == 1 && (group1.indexOf(g) == 0 ||
                                    group1.indexOf(g) == 2),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 14");
    is(g.checked, (n == 1 && (group1.indexOf(g) == 1 ||
                              group1.indexOf(g) == 0)) ||
                  (n == 2 && group2.indexOf(g) == 0), 
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 14");
  }
}

group2[1].defaultChecked = true;
for (let n of [1, 2, 3]) {
  for (let g of window["group"+n]) {
    is(g.defaultChecked, (n == 1 && (group1.indexOf(g) == 0 ||
                                     group1.indexOf(g) == 2)) ||
                         (n == 2 && group2.indexOf(g) == 1),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] defaultChecked wrong pass 15");
    is(g.checked, (n == 1 && (group1.indexOf(g) == 1 ||
                              group1.indexOf(g) == 0)) ||
                  (n == 2 && group2.indexOf(g) == 0),
       "group" + n + "[" + window["group"+n].indexOf(g) +
       "] checked wrong pass 15");
  }
}

cleanup();

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

