<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../../../resources/js-test-pre.js"></script>
</head>
<body>
<script>

description("Test the translate attribute.");

var parent = document.createElement("div");
document.body.appendChild(parent);

function testFor(initialAttribute, initialExpectation, setValue, lastExpectation, lastAttributeExpectation)
{
    var target = document.createElement("span");
    parent.appendChild(target);

    window.target = target;
    window.initialExpectation = initialExpectation;
    window.lastExpectation = lastExpectation;
    window.lastAttributeExpectation = lastAttributeExpectation;

    if (undefined !== initialAttribute)
        target.setAttribute("translate", initialAttribute);
    shouldBe("target.translate", "initialExpectation");

    if (undefined !== setValue)
        target.translate = setValue;
    shouldBe("target.translate", "lastExpectation");
    shouldBe("target.getAttribute('translate')", "lastAttributeExpectation");

    parent.removeChild(target);
}

window.target = document.createElement("p");
parent.appendChild(target);

debug('translate should be "yes" by default.');
shouldBeTrue("target.translate");
shouldBeNull("target.getAttribute('translate')");

debug('\ntarget.translate = false;');
target.translate = false;
shouldBeFalse("target.translate");
shouldBeEqualToString("target.getAttribute('translate')", "no");

debug('\ntarget.setAttribute("translate", "yes");');
target.setAttribute("translate", "yes");
shouldBeTrue("target.translate");
shouldBeEqualToString("target.getAttribute('translate')", "yes");

debug('\ntarget.setAttribute("translate", "no");');
target.setAttribute("translate", "no");
shouldBeFalse("target.translate");
shouldBeEqualToString("target.getAttribute('translate')", "no");

debug('\ntarget.setAttribute("translate", "YES");');
target.setAttribute("translate", "YES");
shouldBeTrue("target.translate");
shouldBeEqualToString("target.getAttribute('translate')", "YES");

debug('\ntarget.setAttribute("translate", "NO");');
target.setAttribute("translate", "NO");
shouldBeFalse("target.translate");
shouldBeEqualToString("target.getAttribute('translate')", "NO");

debug('\ntarget.setAttribute("translate", "INVALID");');
target.setAttribute("translate", "INVALID");
shouldBeTrue("target.translate");
shouldBeEqualToString("target.getAttribute('translate')", "INVALID");

debug("\nRemoving translate attribute.");
target.removeAttribute("translate");
shouldBeNull("target.getAttribute('translate')");
shouldBeTrue("target.translate");

debug("\nCreating targetChild element as a child of target.");
window.targetChild = document.createElement("span");
target.appendChild(targetChild);
shouldBeTrue("targetChild.translate");
shouldBeNull("targetChild.getAttribute('translate')");

debug("\nSetting target.translate = false. targetChild should inherit the translate value from its parents.");
target.translate = false;
shouldBeFalse("targetChild.translate");
shouldBeNull("targetChild.getAttribute('translate')");

debug("\nSetting targetChild.setAttribute('translate', 'INVALID'). Should inherit the translate value from its parents.");
targetChild.setAttribute("translate", "INVALID");
shouldBeFalse("target.translate");
shouldBeEqualToString("target.getAttribute('translate')", "no");
shouldBeFalse("targetChild.translate");
shouldBeEqualToString("targetChild.getAttribute('translate')", "INVALID");

debug("\ntargetChild.translate = true;");
targetChild.translate = true;
shouldBeTrue("targetChild.translate");
shouldBeEqualToString("targetChild.getAttribute('translate')", "yes");
shouldBeFalse("target.translate");
shouldBeEqualToString("target.getAttribute('translate')", "no");

parent.removeChild(target);

debug("\n");

testFor(undefined, true, undefined, true, null);
testFor(undefined, true, false, false, "no");
testFor(undefined, true, true, true, "yes");
testFor(undefined, true, 0, false, "no"); // 0 will be coerced to false
testFor(undefined, true, 1, true, "yes"); // 0 will be coerced to true
testFor(undefined, true, "invalid", true, "yes"); // string will be coerced to true
testFor(undefined, true, "false", true, "yes"); // ...even if the string is "false" (as Firefox does).

testFor("yes", true, undefined, true, "yes");
testFor("yes", true, false, false, "no");
testFor("yes", true, true, true, "yes");
testFor("yes", true, 0, false, "no");
testFor("yes", true, 1, true, "yes");
testFor("yes", true, "invalid", true, "yes");
testFor("yes", true, "false", true, "yes");

testFor("no", false, undefined, false, "no");
testFor("no", false, false, false, "no");
testFor("no", false, true, true, "yes");
testFor("no", false, 0, false, "no");
testFor("no", false, 1, true, "yes");
testFor("no", false, "invalid", true, "yes");
testFor("no", false, "false", true, "yes");

// various initial values
testFor("", true, undefined, true, "");
testFor("", true, 1, true, "yes");
testFor("YES", true, undefined, true, "YES");
testFor("YES", true, 1, true, "yes");
testFor("NO", false, undefined, false, "NO");
testFor("NO", false, 0, false, "no");
testFor("invalid", true, undefined, true, "invalid");
testFor("invalid", true, 1, true, "yes");
testFor("no  ", true, undefined, true, "no  ");
testFor("no  ", true, 1, true, "yes");
testFor("no  ", true, 0, false, "no");
testFor("0", true, undefined, true, "0");
testFor("0", true, 0, false, "no");
testFor("1", true, undefined, true, "1");
testFor("1", true, 0, false, "no");
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>
